Bug Summary

File:builds/wireshark/wireshark/epan/dissectors/packet-celestron-aux.c
Warning:line 1608, column 34
The right operand of '|' is a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name packet-celestron-aux.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-22/lib/clang/22 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -isystem /builds/wireshark/wireshark/epan/dissectors -isystem /builds/wireshark/wireshark/build/epan/dissectors -isystem /usr/include/mit-krb5 -isystem /builds/wireshark/wireshark/epan -isystem /usr/include/libxml2 -D CARES_NO_DEPRECATED -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_BUILD_DLL -D WS_DEBUG -D WS_DEBUG_UTF_8 -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-22/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-nonliteral -std=gnu17 -ferror-limit 19 -fvisibility=hidden -fwrapv -fwrapv-pointer -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /builds/wireshark/wireshark/sbout/2026-11-28-100325-3662-1 -x c /builds/wireshark/wireshark/epan/dissectors/packet-celestron-aux.c
1/* packet-celestron-aux.c
2 * Routines for the Celestron AUX bus protocol dissection
3 * Copyright 2021-2026, Patricio Latini <p_latini@hotmail.com>
4 *
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
12/*
13 * The AUX bus links the parts of a Celestron telescope mount: the hand
14 * controller, the two motor controllers, the GPS, StarSense cameras, focusers,
15 * dew heaters and the Wi-Fi module. SkyPortal and Evolution Wi-Fi modules carry
16 * the bus as a plain TCP byte stream (usually on port 2000, which Wireshark
17 * already gives to Skinny, so use Decode As or the heuristic), with frames split
18 * across segments at any point.
19 *
20 * Two frame formats share the bus:
21 *
22 * AUX packet: 3B | len | src | dst | cmd | data... | checksum
23 * StarSense frame: 3C | len (4 bytes, big-endian) | payload... | checksum
24 *
25 * len of an AUX packet counts src, dst, cmd and the data. The checksum is the
26 * two's complement of the sum of every byte from len to the last data byte. In a
27 * StarSense frame (a star list, 8 bytes per star) every 3B in the length and
28 * payload is escaped as 3B 02 02; its checksum covers the unescaped bytes.
29 *
30 * Celestron does not publish the protocol. The names and payload formats here
31 * come from captures of real devices and community reverse engineering, as
32 * collected by celestron-aux (https://github.com/platini2/celestron-aux). Its
33 * payload decoders are ported here function by function, with the same names,
34 * and give the same text; the tables between the GENERATED markers are written
35 * from its catalog.
36 *
37 * pcap files exported by celestron-aux (--export-pcap) use LINKTYPE_USER0, one
38 * frame per record; map DLT User 0 to "celestron_aux" to read them. Records
39 * starting with "#MARK " are notes taken during the capture.
40 */
41
42#include "config.h"
43
44#include <math.h>
45#include <string.h>
46#include <time.h>
47
48#include <epan/packet.h>
49#include <epan/conversation.h>
50#include <epan/expert.h>
51#include <epan/proto_data.h>
52#include <epan/tfs.h>
53#include <wsutil/pint.h>
54#include <wsutil/sign_ext.h>
55#include <wsutil/str_util.h>
56#include <wsutil/time_util.h>
57#include <wsutil/to_str.h>
58
59void proto_register_celestron_aux(void);
60void proto_reg_handoff_celestron_aux(void);
61
62//static dissector_handle_t celestron_aux_handle;
63static dissector_handle_t celestron_aux_tcp_handle;
64
65static int proto_celestron_aux;
66
67static int hf_celestron_aux_preamble;
68static int hf_celestron_aux_length;
69static int hf_celestron_aux_src;
70static int hf_celestron_aux_dst;
71static int hf_celestron_aux_cmd;
72static int hf_celestron_aux_cmd_name;
73static int hf_celestron_aux_data;
74static int hf_celestron_aux_value;
75static int hf_celestron_aux_checksum;
76static int hf_celestron_aux_checksum_status;
77static int hf_celestron_aux_starsense_length;
78static int hf_celestron_aux_starsense_stars;
79static int hf_celestron_aux_starsense_data;
80static int hf_celestron_aux_mark;
81
82static int ett_celestron_aux;
83
84static expert_field ei_celestron_aux_bad_checksum;
85static expert_field ei_celestron_aux_unexpected_format;
86static expert_field ei_celestron_aux_outside_frame;
87static expert_field ei_celestron_aux_cut_short;
88
89#define AUX_PREAMBLE0x3b 0x3b
90#define STARSENSE_PREAMBLE0x3c 0x3c
91#define AUX_MIN_LENGTH3 3 /* src, dst and cmd */
92#define STARSENSE_MAX_PAYLOAD0xffff 0xffff
93#define MARK_PREFIX"#MARK " "#MARK "
94#define MARK_PREFIX_LEN6 6
95
96typedef struct {
97 wmem_allocator_t *scope;
98 packet_info *pinfo;
99 unsigned key; /* the frame's offset in the packet: one TCP segment can hold several */
100 tvbuff_t *tvb; /* the payload, for its floating-point values */
101} celestron_aux_ctx_t;
102
103/* A payload decoder: the text celestron-aux shows for the payload, or NULL when
104 * the payload does not fit the command's known format. */
105typedef char *(*celestron_aux_decoder_t)(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply);
106
107/* BEGIN GENERATED TABLES: from the celestron-aux catalog */
108static const value_string celestron_aux_devices[] = {
109 { 0x01, "Main Board" },
110 { 0x04, "Nexstar HC" },
111 { 0x0d, "Nexstar+ HC" },
112 { 0x0e, "Starsense HC" },
113 { 0x10, "AZM MC" },
114 { 0x11, "ALT MC" },
115 { 0x12, "Focuser" },
116 { 0x17, "Dew Heater Controller" },
117 { 0x20, "CPWI" },
118 { 0x21, "CFM" },
119 { 0x22, "Focuser Utility" },
120 { 0x2f, "AUXBUS Scanner" },
121 { 0x30, "CGX RA Switch" },
122 { 0x31, "CGX DEC Switch" },
123 { 0x32, "CGX DEC Autoguide Pt" },
124 { 0xb0, "GPS" },
125 { 0xb2, "RTC" },
126 { 0xb3, "Skyportal Accessory" },
127 { 0xb4, "Starsense Camera" },
128 { 0xb5, "Nexstar EVO WiFi" },
129 { 0xb6, "Battery Power Cont" },
130 { 0xb7, "Charge Port" },
131 { 0xb8, "Starsense Camera SW" },
132 { 0xb9, "Starsense AutoGuider" },
133 { 0xbb, "Dew Heater Controller" },
134 { 0xbd, "Starsense AutoAlign V2" },
135 { 0xbf, "Mount Lights" },
136 { 0, NULL((void*)0) }
137};
138
139static const value_string celestron_aux_generic_commands[] = {
140 { 0xfe, "GET_VERSION" },
141 { 0, NULL((void*)0) }
142};
143
144static const value_string celestron_aux_commands_01[] = {
145 { 0xfe, "MB_GET_FW_VER" },
146 { 0, NULL((void*)0) }
147};
148
149static const value_string celestron_aux_commands_04[] = {
150 { 0xfe, "NXS_GET_FW_VER" },
151 { 0, NULL((void*)0) }
152};
153
154static const value_string celestron_aux_commands_10[] = {
155 { 0x01, "MTR_GET_POSITION" },
156 { 0x02, "MTR_GOTO_FAST" },
157 { 0x04, "MTR_SET_POSITION" },
158 { 0x05, "MTR_GET_MODEL" },
159 { 0x06, "MTR_PTRACK" },
160 { 0x07, "MTR_NTRACK" },
161 { 0x08, "MTR_REMOTE_SWITCH_ALIVE" },
162 { 0x09, "MTR_SWITCH_STATE_CHANGE" },
163 { 0x0a, "MTR_AG_STATE_CHANGE" },
164 { 0x0b, "MTR_MVSWITCH" },
165 { 0x0c, "MTR_PECTRAIN" },
166 { 0x0d, "MTR_PECPLAY" },
167 { 0x0e, "MTR_GET_PEC_BIN" },
168 { 0x10, "MTR_SET_POS_BACKLASH" },
169 { 0x11, "MTR_SET_NEG_BACKLASH" },
170 { 0x12, "MTR_IS_MVSWITCH_OVER" },
171 { 0x13, "MTR_IS_GOTO_OVER" },
172 { 0x14, "MTR_PEC_STATE_CHANGE" },
173 { 0x15, "MTR_IS_PECTRAIN_OVER" },
174 { 0x16, "MTR_CANCEL_PECTRAIN" },
175 { 0x17, "MTR_GOTO_SLOW" },
176 { 0x18, "MTR_IS_INDEX_FOUND" },
177 { 0x19, "MTR_FIND_INDEX" },
178 { 0x1a, "MTR_SET_USER_LIMIT_MIN" },
179 { 0x1b, "MTR_SET_USER_LIMIT_MAX" },
180 { 0x1c, "MTR_GET_USER_LIMIT_MIN" },
181 { 0x1d, "MTR_GET_USER_LIMIT_MAX" },
182 { 0x1e, "MTR_IS_USER_LIMIT_ENABLED" },
183 { 0x1f, "MTR_SET_USER_LIMIT_ENABLED" },
184 { 0x20, "MTR_SET_CUSTOM_RATE9" },
185 { 0x21, "MTR_GET_CUSTOM_RATE9" },
186 { 0x22, "MTR_SET_CUSTOM_RATE9_ENA" },
187 { 0x23, "MTR_GET_CUSTOM_RATE9_ENA" },
188 { 0x24, "MTR_PMSLEW_RATE" },
189 { 0x25, "MTR_NMSLEW_RATE" },
190 { 0x26, "MTR_AUX_GUIDE" },
191 { 0x27, "MTR_IS_AUX_GUIDE_ACTIVE" },
192 { 0x2a, "MTR_HS_CALIBRATION_ENABLE" },
193 { 0x2b, "MTR_IS_HS_CALIBRATED" },
194 { 0x2c, "MTR_GET_HS_POSITIONS" },
195 { 0x30, "MTR_EEPROM_READ" },
196 { 0x31, "MTR_EEPROM_WRITE" },
197 { 0x32, "MTR_PROGRAM_READ" },
198 { 0x38, "MTR_CORDWRAP_ON" },
199 { 0x39, "MTR_CORDWRAP_OFF" },
200 { 0x3a, "MTR_SET_CORDWRAP_POSITION" },
201 { 0x3b, "MTR_IS_CORDWRAP_ON" },
202 { 0x3c, "MTR_GET_CORDWRAP_POSITION" },
203 { 0x3d, "MTR_SET_SHUTTER" },
204 { 0x40, "MTR_GET_POS_BACKLASH" },
205 { 0x41, "MTR_GET_NEG_BACKLASH" },
206 { 0x46, "MTR_SET_AUTOGUIDE_RATE" },
207 { 0x47, "MTR_GET_AUTOGUIDE_RATE" },
208 { 0x48, "MTR_SET_SWITCH_CALIBRATION" },
209 { 0x49, "MTR_GET_SWITCH_CALIBRATION" },
210 { 0x4a, "MTR_SET_PRN_VALUE" },
211 { 0x4b, "MTR_GET_PRN_VALUE" },
212 { 0x50, "MTR_SEND_WARNING" },
213 { 0x51, "MTR_SEND_ERROR" },
214 { 0x5b, "MTR_SET_PID_KP" },
215 { 0x5c, "MTR_SET_PID_KI" },
216 { 0x5d, "MTR_SET_PID_KD" },
217 { 0x5f, "MTR_ENABLE_PID_ANALYSIS" },
218 { 0x81, "MTR_PROGRAM_ENTER" },
219 { 0x82, "MTR_PROGRAM_INIT" },
220 { 0x83, "MTR_PROGRAM_DATA" },
221 { 0x84, "MTR_PROGRAM_END" },
222 { 0x8a, "MTR_PROGRAMMER_ENABLE" },
223 { 0xee, "MTR_GET_HARDSWITCH_ENABLE" },
224 { 0xef, "MTR_SET_HARDSWITCH_ENABLE" },
225 { 0xfa, "MTR_GET_CHIPVERSION" },
226 { 0xfb, "MTR_GET_BOOTVERSION" },
227 { 0xfc, "MTR_IS_APPROACH_DIR_NEG" },
228 { 0xfd, "MTR_SET_APPROACH_DIR_NEG" },
229 { 0xfe, "MTR_GET_VERSION" },
230 { 0, NULL((void*)0) }
231};
232
233static const value_string celestron_aux_commands_12[] = {
234 { 0x01, "FOCUS_GET_POSITION" },
235 { 0x02, "FOCUS_GOTO_FAST" },
236 { 0x05, "FOCUS_GET_MODEL" },
237 { 0x13, "FOCUS_SLEW_DONE" },
238 { 0x24, "FOCUS_MOVE_POS" },
239 { 0x25, "FOCUS_MOVE_NEG" },
240 { 0x2a, "FOCUS_CALIBRATION_ENABLE" },
241 { 0x2b, "FOCUS_IS_CALIBRATED" },
242 { 0x2c, "FOCUS_GET_CALIBRATION_POSITIONS" },
243 { 0x3b, "FOCUS_UNKNOWN_3B" },
244 { 0xfe, "FOCUS_GET_FW_VER" },
245 { 0, NULL((void*)0) }
246};
247
248static const value_string celestron_aux_commands_17[] = {
249 { 0x00, "DEWHEATER_GET_CURRENT_POWER" },
250 { 0x02, "DEWHEATER_GET_LED_BRIGHTNESS" },
251 { 0x03, "DEWHEATER_SET_INPUT_LIMIT" },
252 { 0x04, "DEWHEATER_GET_LIMIT_POWER" },
253 { 0x10, "DEWHEATER_GET_POWER_PORTS" },
254 { 0x11, "DEWHEATER_GET_PORTS" },
255 { 0x12, "DEWHEATER_GET_STATUS" },
256 { 0x14, "DEWHEATER_ENABLE_PORT" },
257 { 0x16, "DEWHEATER_SET_AUTO_AGGRESSION" },
258 { 0x17, "DEWHEATER_SET_MANUAL_POWER" },
259 { 0x18, "DEWHEATER_GET_AMBIENT" },
260 { 0x19, "DEWHEATER_RECALIBRATE_SENSOR" },
261 { 0x1a, "DEWHEATER_IS_CALIBRATING" },
262 { 0x20, "DEWHEATER_SET_LED_BRIGHTNESS" },
263 { 0x21, "DEWHEATER_UNKNOWN_21" },
264 { 0xfe, "GET_VERSION" },
265 { 0, NULL((void*)0) }
266};
267
268static const value_string celestron_aux_commands_b0[] = {
269 { 0x01, "GPS_GET_LAT" },
270 { 0x02, "GPS_GET_LONG" },
271 { 0x03, "GPS_GET_DATE" },
272 { 0x04, "GPS_GET_YEAR" },
273 { 0x07, "GPS_GET_SAT_INFO" },
274 { 0x08, "GPS_GET_RCVR_STATUS" },
275 { 0x33, "GPS_GET_TIME" },
276 { 0x36, "GPS_TIME_VALID" },
277 { 0x37, "GPS_LINKED" },
278 { 0x55, "GPS_GET_HW_VER" },
279 { 0xa0, "GPS_GET_COMPASS" },
280 { 0xfe, "GPS_GET_FW_VER" },
281 { 0, NULL((void*)0) }
282};
283
284static const value_string celestron_aux_commands_b3[] = {
285 { 0xfe, "WIFI_GET_FW_VER" },
286 { 0, NULL((void*)0) }
287};
288
289static const value_string celestron_aux_commands_b4[] = {
290 { 0x3e, "SS_SET_ALIGN_CENTER" },
291 { 0x3f, "SS_GET_ALIGN_CENTER" },
292 { 0x90, "SS_RESET_CAPTURE" },
293 { 0x91, "SS_GET_STATUS" },
294 { 0x92, "SS_GET_CAPTURE_DATA" },
295 { 0x94, "SS_START_CAPTURE" },
296 { 0x9f, "SS_GET_IMAGE3" },
297 { 0xfe, "SS_GET_FW_VER" },
298 { 0, NULL((void*)0) }
299};
300
301static const value_string celestron_aux_commands_b5[] = {
302 { 0x10, "WIFI_SET_ENABLE" },
303 { 0xfe, "WIFI_GET_FW_VER" },
304 { 0, NULL((void*)0) }
305};
306
307static const value_string celestron_aux_commands_b6[] = {
308 { 0x10, "BAT_GET_VOLTAGE" },
309 { 0x18, "BAT_GET_MAX_CURRENT" },
310 { 0xfe, "BAT_GET_FW_VER" },
311 { 0, NULL((void*)0) }
312};
313
314static const value_string celestron_aux_commands_b7[] = {
315 { 0x10, "CHG_MODE" },
316 { 0xfe, "CHG_GET_FW_VER" },
317 { 0, NULL((void*)0) }
318};
319
320static const value_string celestron_aux_commands_b9[] = {
321 { 0x01, "SSAG_START_PLATE_SOLVE" },
322 { 0x02, "SSAG_GET_PLATE_STATUS" },
323 { 0x03, "SSAG_START_CALIBRATE" },
324 { 0x04, "SSAG_GET_CALIBRATE_STATUS" },
325 { 0x07, "SSAG_UNKNOWN_07" },
326 { 0x20, "SSAG_SET_GUIDING" },
327 { 0x23, "SSAG_GET_GUIDING_DATA" },
328 { 0x30, "SSAG_SET_MOUNT_INFO" },
329 { 0x31, "SSAG_SET_GEOLOCATION" },
330 { 0x32, "SSAG_SET_TIME_NOW" },
331 { 0x45, "SSAG_GET_IMAGE_ROTATION" },
332 { 0xfe, "SSAG_GET_FW_VER" },
333 { 0, NULL((void*)0) }
334};
335
336static const value_string celestron_aux_commands_bb[] = {
337 { 0xfe, "DEWHEATER_GET_FW_VER" },
338 { 0, NULL((void*)0) }
339};
340
341static const value_string celestron_aux_commands_bd[] = {
342 { 0x01, "SS2_START_PLATE_SOLVE" },
343 { 0x02, "SS2_GET_PLATE_STATUS" },
344 { 0x03, "SS2_START_CALIBRATE" },
345 { 0x04, "SS2_GET_CALIBRATE_STATUS" },
346 { 0x07, "SS2_IS_CALIBRATED" },
347 { 0x08, "SS2_GET_CALIBRATION_DATA" },
348 { 0x13, "SS2_SET_ASPA" },
349 { 0x15, "SS2_GET_ASPA_POLE" },
350 { 0x16, "SS2_GET_ASPA_ERROR" },
351 { 0x17, "SS2_GET_ASPA_PROGRESS" },
352 { 0x30, "SS2_SET_MOUNT_INFO" },
353 { 0x31, "SS2_SET_GEOLOCATION" },
354 { 0x32, "SS2_SET_TIME_NOW" },
355 { 0x45, "SS2_GET_IMAGE_ROTATION" },
356 { 0x4b, "SS2_GET_TELESCOPE_POSITION" },
357 { 0xfe, "SS2_GET_FW_VER" },
358 { 0, NULL((void*)0) }
359};
360
361static const value_string celestron_aux_commands_bf[] = {
362 { 0x10, "LIGHT_LEVEL" },
363 { 0xfe, "LIGHT_GET_FW_VER" },
364 { 0, NULL((void*)0) }
365};
366
367/* The command names of a device, or NULL when only the generic ones are known. */
368static const value_string *
369celestron_aux_command_table(uint8_t device)
370{
371 switch (device) {
372 case 0x01:
373 return celestron_aux_commands_01;
374 case 0x04:
375 case 0x0d:
376 case 0x0e:
377 case 0x20:
378 return celestron_aux_commands_04;
379 case 0x10:
380 case 0x11:
381 return celestron_aux_commands_10;
382 case 0x12:
383 return celestron_aux_commands_12;
384 case 0x17:
385 return celestron_aux_commands_17;
386 case 0xb0:
387 return celestron_aux_commands_b0;
388 case 0xb3:
389 return celestron_aux_commands_b3;
390 case 0xb4:
391 return celestron_aux_commands_b4;
392 case 0xb5:
393 return celestron_aux_commands_b5;
394 case 0xb6:
395 return celestron_aux_commands_b6;
396 case 0xb7:
397 return celestron_aux_commands_b7;
398 case 0xb9:
399 return celestron_aux_commands_b9;
400 case 0xbb:
401 return celestron_aux_commands_bb;
402 case 0xbd:
403 return celestron_aux_commands_bd;
404 case 0xbf:
405 return celestron_aux_commands_bf;
406 default:
407 return NULL((void*)0);
408 }
409}
410
411static bool_Bool
412celestron_aux_is_controller(uint8_t addr)
413{
414 switch (addr) {
415 case 0x04:
416 case 0x0d:
417 case 0x0e:
418 case 0x20:
419 case 0x21:
420 case 0x22:
421 case 0x2f:
422 return true1;
423 default:
424 return false0;
425 }
426}
427
428static bool_Bool
429celestron_aux_is_motor_client(uint8_t addr)
430{
431 switch (addr) {
432 case 0xb9:
433 return true1;
434 default:
435 return false0;
436 }
437}
438
439static bool_Bool
440celestron_aux_is_motor(uint8_t addr)
441{
442 switch (addr) {
443 case 0x10:
444 case 0x11:
445 return true1;
446 default:
447 return false0;
448 }
449}
450
451#define CELESTRON_AUX_SCANNER0x2f 0x2f
452
453static const value_string celestron_aux_mount_models[] = {
454 { 0x0001, "Nexstar GPS" },
455 { 0x0682, "Advanced GT" },
456 { 0x0783, "Nexstar SLT" },
457 { 0x0b83, "4/5SE" },
458 { 0x0c82, "6/8SE" },
459 { 0x1189, "CPC Deluxe" },
460 { 0x1283, "GT Series" },
461 { 0x1485, "AVX" },
462 { 0x1687, "Nexstar Evolution" },
463 { 0x1788, "CGX" },
464 { 0x1888, "CGX-L" },
465 { 0x1d8a, "Origin" },
466 { 0, NULL((void*)0) }
467};
468
469static const value_string celestron_aux_mount_families[] = {
470 { 0x05, "CGE" },
471 { 0x06, "Advanced GT / CG-5" },
472 { 0x07, "Nexstar SLT" },
473 { 0x09, "CPC" },
474 { 0x0a, "GT" },
475 { 0x0b, "4/5SE" },
476 { 0x0c, "6/8SE" },
477 { 0x0d, "CGE Pro" },
478 { 0x0e, "CGEM DX" },
479 { 0x0f, "LCM" },
480 { 0x10, "SkyProdigy" },
481 { 0x11, "CPC Deluxe" },
482 { 0x12, "GT Series" },
483 { 0x13, "StarSeeker" },
484 { 0x14, "AVX" },
485 { 0x15, "Cosmos" },
486 { 0x16, "Nexstar Evolution" },
487 { 0x17, "CGX" },
488 { 0x18, "CGX-L" },
489 { 0x19, "AstroFi" },
490 { 0x1a, "SkyWatcher" },
491 { 0x1d, "Origin" },
492 { 0, NULL((void*)0) }
493};
494/* END GENERATED TABLES */
495
496/* rint() rounds halves to the even neighbour, as Python's round() does; this
497 * keeps its result within int64_t. */
498#define ROUNDABLE(x)(__builtin_isfinite (x) && fabs(x) <= 9e15) (isfinite(x)__builtin_isfinite (x) && fabs(x) <= 9e15)
499
500/* --- formatting helpers, matching celestron-aux's Python output ------------ */
501
502#define DEG"\xc2\xb0" "\xc2\xb0" /* the degree sign, UTF-8 */
503#define EPOCH_2000946684800L INT64_C(946684800)946684800L /* 2000-01-01 00:00:00 UTC in Unix seconds */
504#define MAX_TIMESTAMP253402300800.0 253402300800.0 /* the year 10000 */
505#define MAX_ANGLE1e9 1e9 /* larger angles come from garbage bytes */
506
507/* _labelled: "value - label", or just the value. */
508static char *
509labelled(celestron_aux_ctx_t *ctx, unsigned value, const value_string *labels)
510{
511 const char *name = try_val_to_str(value, labels);
512
513 return name ? wmem_strdup_printf(ctx->scope, "%u - %s", value, name) : wmem_strdup_printf(ctx->scope, "%u", value);
514}
515
516/* A 0/1 flag as a standard true/false string (NULL: "True"/"False"); any other value as its number. */
517static char *
518flag_str(celestron_aux_ctx_t *ctx, unsigned value, const true_false_string *tfs)
519{
520 return value <= 1 ? wmem_strdup(ctx->scope, tfs_get_string(value == 1, tfs)) : wmem_strdup_printf(ctx->scope, "%u", value);
521}
522
523/* The same, as "value - label" like labelled(). */
524static char *
525flag_labelled(celestron_aux_ctx_t *ctx, unsigned value, const true_false_string *tfs)
526{
527 return value <= 1 ? wmem_strdup_printf(ctx->scope, "%u - %s", value, tfs_get_string(value == 1, tfs))
528 : wmem_strdup_printf(ctx->scope, "%u", value);
529}
530
531/* bam_to_degrees(data[:3]): a signed big-endian binary angle of 1-3 bytes. */
532static double
533bam_degrees(const uint8_t *data, unsigned len)
534{
535 switch (len) {
536 case 1:
537 return (int8_t)data[0] * (360.0 / 256);
538 case 2:
539 return (int16_t)pntohu16(data) * (360.0 / 65536);
540 default:
541 return (int32_t)ws_sign_ext32(pntohu24(data), 24) * (360.0 / 16777216);
542 }
543}
544
545static char *
546dms(celestron_aux_ctx_t *ctx, double degrees)
547{
548 int64_t hundredths, whole, minutes, rest;
549
550 if (isnan(degrees)__builtin_isnan (degrees) || fabs(degrees) > MAX_ANGLE1e9)
551 return NULL((void*)0);
552 if (!ROUNDABLE(degrees * 360000)(__builtin_isfinite (degrees * 360000) && fabs(degrees
* 360000) <= 9e15)
)
553 return NULL((void*)0);
554 hundredths = (int64_t)rint(fabs(degrees) * 360000);
555 whole = hundredths / 360000;
556 rest = hundredths % 360000;
557 minutes = rest / 6000;
558 rest %= 6000;
559 return wmem_strdup_printf(ctx->scope, "%s%" PRId64"l" "d" DEG"\xc2\xb0" "%02" PRId64"l" "d" "'%05.2f\"", degrees < 0 ? "-" : "",
560 whole, minutes, (double)rest / 100);
561}
562
563static char *
564hms(celestron_aux_ctx_t *ctx, double degrees)
565{
566 double wrapped = fmod(degrees, 360);
567 int64_t hundredths, hours, minutes, rest;
568
569 if (wrapped < 0)
570 wrapped += 360; /* Python's %: the sign of the divisor */
571 if (!ROUNDABLE(wrapped / 15 * 360000)(__builtin_isfinite (wrapped / 15 * 360000) && fabs(wrapped
/ 15 * 360000) <= 9e15)
)
572 return NULL((void*)0);
573 hundredths = (int64_t)rint(wrapped / 15 * 360000);
574 hundredths %= 24 * 360000;
575 hours = hundredths / 360000;
576 rest = hundredths % 360000;
577 minutes = rest / 6000;
578 rest %= 6000;
579 return wmem_strdup_printf(ctx->scope, "%" PRId64"l" "d" "h%02" PRId64"l" "d" "m%05.2fs", hours, minutes, (double)rest / 100);
580}
581
582static char *
583version(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len)
584{
585 wmem_strbuf_t *text;
586
587 if (len >= 4)
588 return wmem_strdup_printf(ctx->scope, "%u.%u.%u", data[0], data[1], (unsigned)pntohu16(data + 2));
589 text = wmem_strbuf_new(ctx->scope, "");
590 for (unsigned i = 0; i < len; i++)
591 wmem_strbuf_append_printf(text, i ? ".%u" : "%u", data[i]);
592 return wmem_strbuf_finalize(text);
593}
594
595static char *
596mount_name(celestron_aux_ctx_t *ctx, uint64_t model)
597{
598 const char *name = model <= UINT32_MAX(4294967295U) ? try_val_to_str((unsigned)model, celestron_aux_mount_models) : NULL((void*)0);
599 const char *family = NULL((void*)0);
600
601 if (name)
602 return wmem_strdup(ctx->scope, name);
603 if (model > 0xff && (model >> 8) <= UINT32_MAX(4294967295U))
604 family = try_val_to_str((unsigned)(model >> 8), celestron_aux_mount_families);
605 if (family)
606 return wmem_strdup_printf(ctx->scope, "%s family (0x%04" PRIx64"l" "x" ")", family, model);
607 return wmem_strdup_printf(ctx->scope, "Unknown (0x%04" PRIx64"l" "x" ")", model);
608}
609
610/* The RA and Dec at ``offset``: big-endian float32 in radians. */
611static char *
612ra_dec(celestron_aux_ctx_t *ctx, unsigned offset, unsigned len)
613{
614 char *ra, *dec;
615
616 if (len < offset + 8)
617 return NULL((void*)0);
618 ra = hms(ctx, tvb_get_ntohieee_float(ctx->tvb, offset) * (180.0 / G_PI3.1415926535897932384626433832795028841971693993751));
619 dec = dms(ctx, tvb_get_ntohieee_float(ctx->tvb, offset + 4) * (180.0 / G_PI3.1415926535897932384626433832795028841971693993751));
620 if (!ra || !dec)
621 return NULL((void*)0);
622 return wmem_strdup_printf(ctx->scope, "RA %s, Dec %s", ra, dec);
623}
624
625static char *
626utc(celestron_aux_ctx_t *ctx, int64_t seconds, bool_Bool with_seconds)
627{
628 time_t when = (time_t)seconds;
629 struct tm tm;
630
631 if (!ws_gmtime_r(&when, &tm))
632 return NULL((void*)0);
633 if (with_seconds)
634 return wmem_strdup_printf(ctx->scope, "%04d-%02d-%02d %02d:%02d:%02d UTC", tm.tm_year + 1900, tm.tm_mon + 1,
635 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
636 return wmem_strdup_printf(ctx->scope, "%04d-%02d-%02d %02d:%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
637 tm.tm_hour, tm.tm_min);
638}
639
640static bool_Bool
641all_zero(const uint8_t *data, unsigned len)
642{
643 for (unsigned i = 0; i < len; i++)
644 if (data[i])
645 return false0;
646 return true1;
647}
648
649/* --- payload decoders: ports of celestron_aux/decoding.py ------------------- */
650
651#define NEED(size)do { if (len < (size)) return ((void*)0); } while (0) do { if (len < (size)) return NULL((void*)0); } while (0)
652
653static const value_string ack_labels[] = { { 1, "ACK" }, { 0, NULL((void*)0) } };
654
655#define IN_PROGRESS0 0
656#define SOLVED1 1
657#define FAILED4 4
658
659static char *
660decode_version(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
661{
662 return version(ctx, data, len);
663}
664
665static char *
666decode_motor_position(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
667{
668 return dms(ctx, bam_degrees(data, len));
669}
670
671static char *
672decode_motor_goto2(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply)
673{
674 if (reply)
675 return labelled(ctx, data[0], ack_labels);
676 return dms(ctx, bam_degrees(data, len));
677}
678
679static char *
680decode_motor_track_rate(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply)
681{
682 if (reply)
683 return labelled(ctx, data[0], ack_labels);
684 /* Up to three bytes: shorter payloads give a smaller number, as in celestron-aux. */
685 return wmem_strdup_printf(ctx->scope, "%.4f arcsec/s",
686 (double)tvb_get_uint64_with_length(ctx->tvb, 0, MIN(len, 3)(((len) < (3)) ? (len) : (3)), ENC_BIG_ENDIAN0x00000000) / 1024);
687}
688
689static char *
690decode_motor_model(celestron_aux_ctx_t *ctx, const uint8_t *data _U___attribute__((unused)), unsigned len, bool_Bool reply _U___attribute__((unused)))
691{
692 uint64_t model;
693
694 if (len > 8)
695 return NULL((void*)0); /* Python prints integers of any size; real replies have 1 or 2 bytes */
696 model = tvb_get_uint64_with_length(ctx->tvb, 0, len, ENC_BIG_ENDIAN0x00000000);
697 return wmem_strdup_printf(ctx->scope, "0x%04" PRIx64"l" "x" " (%s)", model, mount_name(ctx, model));
698}
699
700static const value_string goto_over_labels[] = { { 0, "Not done" }, { 1, "Check" }, { 0xff, "Done" }, { 0, NULL((void*)0) } };
701static const value_string cordwrap_labels[] = { { 0, "Disabled" }, { 0xff, "Enabled" }, { 0, NULL((void*)0) } };
702static const value_string approach_labels[] = { { 0, "Positive" }, { 1, "Negative" }, { 0, NULL((void*)0) } };
703
704static char *
705decode_motor_switch_over(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
706{
707 return flag_labelled(ctx, data[0], NULL((void*)0));
708}
709
710static char *
711decode_motor_goto_over(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
712{
713 return labelled(ctx, data[0], goto_over_labels);
714}
715
716static char *
717decode_motor_custom_rate_enabled(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
718{
719 return flag_labelled(ctx, data[0], &tfs_enabled_disabled);
720}
721
722static char *
723decode_motor_single_byte(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
724{
725 return wmem_strdup_printf(ctx->scope, "%u", data[0]);
726}
727
728static char *
729decode_motor_cordwrap_on(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
730{
731 return labelled(ctx, data[0], cordwrap_labels);
732}
733
734static char *
735decode_motor_autoguide_rate(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
736{
737 return wmem_strdup_printf(ctx->scope, "%g%%", 100.0 * data[0] / 256);
738}
739
740static char *
741decode_motor_approach_direction(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
742{
743 return labelled(ctx, data[0], approach_labels);
744}
745
746static char *
747decode_gps_coordinate(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
748{
749 return dms(ctx, bam_degrees(data, len));
750}
751
752static char *
753decode_gps_date(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
754{
755 NEED(2)do { if (len < (2)) return ((void*)0); } while (0);
756 return wmem_strdup_printf(ctx->scope, "%u/%u", data[0], data[1]);
757}
758
759static char *
760decode_gps_year(celestron_aux_ctx_t *ctx, const uint8_t *data _U___attribute__((unused)), unsigned len, bool_Bool reply _U___attribute__((unused)))
761{
762 if (len > 8)
763 return NULL((void*)0); /* as decode_motor_model */
764 return wmem_strdup_printf(ctx->scope, "%" PRIu64"l" "u", tvb_get_uint64_with_length(ctx->tvb, 0, len, ENC_BIG_ENDIAN0x00000000));
765}
766
767static char *
768decode_gps_satellites(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
769{
770 NEED(2)do { if (len < (2)) return ((void*)0); } while (0);
771 return wmem_strdup_printf(ctx->scope, "%u - %u", data[0], data[1]);
772}
773
774static char *
775decode_gps_time(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
776{
777 NEED(3)do { if (len < (3)) return ((void*)0); } while (0);
778 return wmem_strdup_printf(ctx->scope, "%02u:%02u:%02u", data[0], data[1], data[2]);
779}
780
781static char *
782decode_gps_hardware_version(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
783{
784 return wmem_strdup_printf(ctx->scope, "0x%02x%s", data[0], data[0] == 0xab ? " (Celestron GPS)" : "");
785}
786
787static char *
788decode_gps_flag(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
789{
790 return flag_labelled(ctx, data[0], &tfs_yes_no);
791}
792
793static char *
794decode_starsense_align_center(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
795{
796 if (len == 8)
797 return wmem_strdup_printf(ctx->scope, "%u - %u", pletohu32(data), pletohu32(data + 4));
798 return wmem_strdup_printf(ctx->scope, "%u", data[0]);
799}
800
801static const value_string starsense_status_labels[] = {
802 { 1, "Idle / none found" }, { 3, "Ready" }, { 9, "Finished" }, { 0, NULL((void*)0) }
803};
804
805static char *
806decode_starsense_status(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
807{
808 char *status = labelled(ctx, data[0], starsense_status_labels);
809
810 return len > 1 ? wmem_strdup_printf(ctx->scope, "%s - Stars: %u", status, data[1]) : status;
811}
812
813static char *
814decode_ssag_plate_status(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
815{
816 char *position;
817
818 if (data[0] == IN_PROGRESS0)
819 return wmem_strdup(ctx->scope, "In progress");
820 if (data[0] == FAILED4)
821 return wmem_strdup_printf(ctx->scope, "Failed (status %u)", data[0]);
822 if (!(position = ra_dec(ctx, 1, len)))
823 return NULL((void*)0);
824 if (data[0] == SOLVED1)
825 return wmem_strdup_printf(ctx->scope, "Solved, %s", position);
826 return wmem_strdup_printf(ctx->scope, "Status %u, %s", data[0], position);
827}
828
829static char *
830decode_ssag_start_calibrate(celestron_aux_ctx_t *ctx, const uint8_t *data _U___attribute__((unused)), unsigned len, bool_Bool reply _U___attribute__((unused)))
831{
832 return ra_dec(ctx, 0, len);
833}
834
835static char *
836decode_ssag_calibrate_status(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
837{
838 if (data[0] == IN_PROGRESS0)
839 return wmem_strdup(ctx->scope, "In progress");
840 if (data[0] == FAILED4)
841 return wmem_strdup_printf(ctx->scope, "Failed (status %u)", data[0]);
842 NEED(5)do { if (len < (5)) return ((void*)0); } while (0);
843 return wmem_strdup_printf(ctx->scope, "%s, x %u, y %u",
844 data[0] == 1 ? "Solved" : wmem_strdup_printf(ctx->scope, "Status %u", data[0]),
845 (unsigned)pntohu16(data + 1), (unsigned)pntohu16(data + 3));
846}
847
848static char *
849decode_ssag_set_guiding(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
850{
851 if (data[0] == 0)
852 return wmem_strdup(ctx->scope, "Disabled");
853 return wmem_strdup_printf(ctx->scope, "Enabled, flags 0x%02x", data[0]);
854}
855
856static char *
857decode_ss2_is_calibrated(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply)
858{
859 if (reply)
860 return flag_str(ctx, data[0], &tfs_yes_no);
861 return bytes_to_str_maxlen(ctx->scope, data, len, 0);
862}
863
864static const value_string aspa_labels[] = { { 0, "Stop" }, { 1, "Start" }, { 4, "Adjust" }, { 0, NULL((void*)0) } };
865
866static char *
867decode_ss2_set_aspa(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply)
868{
869 const char *name;
870
871 if (reply)
872 return data[0] == 0 ? wmem_strdup(ctx->scope, "OK") : bytes_to_str_maxlen(ctx->scope, data, len, 0);
873 name = try_val_to_str(data[0], aspa_labels);
874 return name ? wmem_strdup(ctx->scope, name) : bytes_to_str_maxlen(ctx->scope, data, len, 0);
875}
876
877static char *
878decode_ss2_aspa_pole(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
879{
880 char *position;
881
882 if (data[0] == SOLVED1 && len >= 9) {
883 if (!(position = ra_dec(ctx, 1, len)))
884 return NULL((void*)0);
885 return wmem_strdup_printf(ctx->scope, "Polar axis %s", position);
886 }
887 if (data[0] == FAILED4)
888 return wmem_strdup(ctx->scope, "No estimate yet");
889 return wmem_strdup_printf(ctx->scope, "Status %u", data[0]);
890}
891
892static char *
893decode_ss2_aspa_error(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
894{
895 if (data[0] != SOLVED1 || len < 9) {
896 if (all_zero(data, len))
897 return wmem_strdup(ctx->scope, "No measurement");
898 return wmem_strdup_printf(ctx->scope, "Status %u", data[0]);
899 }
900 return wmem_strdup_printf(ctx->scope, "Altitude error %+.2f', azimuth error %+.2f'",
901 tvb_get_ntohieee_float(ctx->tvb, 1) * 60.0, tvb_get_ntohieee_float(ctx->tvb, 5) * 60.0);
902}
903
904static char *
905decode_ss2_aspa_progress(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
906{
907 char *text;
908
909 NEED(6)do { if (len < (6)) return ((void*)0); } while (0);
910 text = wmem_strdup_printf(ctx->scope, "Completion %u %%, uncertainty %.2f" DEG"\xc2\xb0", data[1],
911 tvb_get_ntohieee_float(ctx->tvb, 2));
912 return data[0] == SOLVED1 ? text : wmem_strdup_printf(ctx->scope, "Waiting (%s)", text);
913}
914
915static char *
916decode_ssag_image_rotation(celestron_aux_ctx_t *ctx, const uint8_t *data _U___attribute__((unused)), unsigned len, bool_Bool reply _U___attribute__((unused)))
917{
918 NEED(4)do { if (len < (4)) return ((void*)0); } while (0);
919 return wmem_strdup_printf(ctx->scope, "Rotation %.2f" DEG"\xc2\xb0", tvb_get_ntohieee_float(ctx->tvb, 0));
920}
921
922/* The stored calibration (0x08), one slot per request. A reply does not say
923 * which slot it holds, so the slot requested last in the conversation is
924 * remembered, and the slot each reply got is kept with its frame. */
925static const value_string calibration_slot_labels[] = {
926 { 0, "offset position RA" },
927 { 1, "offset position Dec" },
928 { 2, "calibration star RA" },
929 { 3, "calibration star Dec" },
930 { 4, "image rotation" },
931 { 5, "calibration time" },
932 { 0, NULL((void*)0) }
933};
934
935#define NO_SLOT-1 -1
936
937typedef struct {
938 int last_slot;
939} celestron_aux_conv_t;
940
941static char *
942slot_name(celestron_aux_ctx_t *ctx, int slot)
943{
944 return wmem_strdup_printf(ctx->scope, "Slot %d: %s", slot, val_to_str_const(slot, calibration_slot_labels, "unknown"));
945}
946
947static int
948calibration_slot(celestron_aux_ctx_t *ctx, int requested)
949{
950 conversation_t *conversation = find_or_create_conversation(ctx->pinfo);
951 celestron_aux_conv_t *state = (celestron_aux_conv_t *)conversation_get_proto_data(conversation, proto_celestron_aux);
952 void *stored;
953
954 if (!state) {
955 state = wmem_new(wmem_file_scope(), celestron_aux_conv_t)((celestron_aux_conv_t*)wmem_alloc((wmem_file_scope()), sizeof
(celestron_aux_conv_t)))
;
956 state->last_slot = NO_SLOT-1;
957 conversation_add_proto_data(conversation, proto_celestron_aux, state);
958 }
959 if (requested != NO_SLOT-1) { /* a request */
960 if (!PINFO_FD_VISITED(ctx->pinfo)((ctx->pinfo)->fd->visited))
961 state->last_slot = requested;
962 return requested;
963 }
964 stored = p_get_proto_data(wmem_file_scope(), ctx->pinfo, proto_celestron_aux, ctx->key);
965 if (stored) /* stored as slot + 2, so NO_SLOT is not NULL */
966 return GPOINTER_TO_INT(stored)((gint) (glong) (stored)) - 2;
967 p_add_proto_data(wmem_file_scope(), ctx->pinfo, proto_celestron_aux, ctx->key,
968 GINT_TO_POINTER(state->last_slot + 2)((gpointer) (glong) (state->last_slot + 2)));
969 return state->last_slot;
970}
971
972static char *
973decode_ss2_calibration_data(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply)
974{
975 double value;
976 int slot;
977 char *text;
978
979 if (!reply)
980 return slot_name(ctx, calibration_slot(ctx, data[0]));
981 NEED(4)do { if (len < (4)) return ((void*)0); } while (0);
982 value = tvb_get_ntohieee_float(ctx->tvb, 0);
983 slot = calibration_slot(ctx, NO_SLOT-1);
984 if (value == 0) {
985 text = wmem_strdup(ctx->scope, "0 (empty)");
986 } else if (slot == 5 || (slot == NO_SLOT-1 && value > 1e9)) {
987 char *when;
988
989 if (!(value >= 0 && value < MAX_TIMESTAMP253402300800.0) || !(when = utc(ctx, (int64_t)floor(value), false0)))
990 return NULL((void*)0);
991 text = wmem_strdup_printf(ctx->scope, "%s UTC (%.0f)", when, value);
992 } else if (slot == 0 || slot == 2) {
993 char *hours = hms(ctx, value);
994
995 if (!hours)
996 return NULL((void*)0);
997 text = wmem_strdup_printf(ctx->scope, "%s (%.4f" DEG"\xc2\xb0" ")", hours, value);
998 } else if (slot == 1 || slot == 3) {
999 if (!(text = dms(ctx, value)))
1000 return NULL((void*)0);
1001 } else if (slot == 4) {
1002 text = wmem_strdup_printf(ctx->scope, "%.2f" DEG"\xc2\xb0", value);
1003 } else {
1004 text = wmem_strdup_printf(ctx->scope, "%.6g", value);
1005 }
1006 return slot == NO_SLOT-1 ? text : wmem_strdup_printf(ctx->scope, "%s, %s", slot_name(ctx, slot), text);
1007}
1008
1009static char *
1010decode_ssag_mount_info(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
1011{
1012 const char *family;
1013
1014 NEED(4)do { if (len < (4)) return ((void*)0); } while (0);
1015 family = try_val_to_str(data[0], celestron_aux_mount_families);
1016 return wmem_strdup_printf(ctx->scope, "Model 0x%02x%s, mount mode %u, AZM firmware %u.%u", data[0],
1017 family ? wmem_strdup_printf(ctx->scope, " (%s family)", family) : "", data[1], data[2], data[3]);
1018}
1019
1020static char *
1021decode_ssag_geolocation(celestron_aux_ctx_t *ctx, const uint8_t *data _U___attribute__((unused)), unsigned len, bool_Bool reply _U___attribute__((unused)))
1022{
1023 NEED(8)do { if (len < (8)) return ((void*)0); } while (0);
1024 return wmem_strdup_printf(ctx->scope, "Lat %.4f, Lon %.4f",
1025 tvb_get_ntohieee_float(ctx->tvb, 0), tvb_get_ntohieee_float(ctx->tvb, 4));
1026}
1027
1028static char *
1029decode_ssag_time(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
1030{
1031 NEED(4)do { if (len < (4)) return ((void*)0); } while (0);
1032 return utc(ctx, EPOCH_2000946684800L + (int64_t)pntohu32(data), true1);
1033}
1034
1035static char *
1036decode_dew_power(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
1037{
1038 NEED(4)do { if (len < (4)) return ((void*)0); } while (0);
1039 return wmem_strdup_printf(ctx->scope, "%g V - %g A", pntohu16(data) / 1000.0, pntohu16(data + 2) / 1000.0);
1040}
1041
1042static char *
1043decode_dew_power_limit(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
1044{
1045 NEED(4)do { if (len < (4)) return ((void*)0); } while (0);
1046 return wmem_strdup_printf(ctx->scope, "%g A - %g A", pntohu16(data) / 1000.0, pntohu16(data + 2) / 1000.0);
1047}
1048
1049static char *
1050decode_dew_power_ports(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
1051{
1052 return wmem_strdup_printf(ctx->scope, "%u", data[0]);
1053}
1054
1055static const value_string dew_ports_labels[] = { { 0, "Get 12V ports" }, { 1, "Get dew heater ports" }, { 0, NULL((void*)0) } };
1056static const value_string dew_port_labels[] = { { 0, "Port 1" }, { 1, "Port 2" }, { 0, NULL((void*)0) } };
1057static const value_string dew_mode_labels[] = { { 0, "Manual" }, { 1, "Auto" }, { 0, NULL((void*)0) } };
1058
1059static char *
1060decode_dew_ports(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply)
1061{
1062 if (reply)
1063 return wmem_strdup_printf(ctx->scope, "%u", data[0]);
1064 return labelled(ctx, data[0], dew_ports_labels);
1065}
1066
1067static char *
1068decode_dew_status(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply)
1069{
1070 char *text;
1071
1072 if (!reply)
1073 return labelled(ctx, data[0], dew_port_labels);
1074 NEED(6)do { if (len < (6)) return ((void*)0); } while (0);
1075 text = wmem_strdup_printf(ctx->scope, "Port %u - %s - Power: %s %u - Aggression %u", data[0],
1076 val_to_str(ctx->scope, data[1], dew_mode_labels, "%u"), tfs_get_string(data[2] != 0, &tfs_on_off), data[2], data[5]);
1077 if (data[2]) {
1078 NEED(10)do { if (len < (10)) return ((void*)0); } while (0);
1079 text = wmem_strdup_printf(ctx->scope, "%s - Temp %g" DEG"\xc2\xb0" "C", text, pntohu32(data + 6) / 1000.0);
1080 }
1081 return text;
1082}
1083
1084static char *
1085decode_dew_led_brightness(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
1086{
1087 return wmem_strdup_printf(ctx->scope, "Brightness %u", data[0]);
1088}
1089
1090static char *
1091decode_dew_set_input_limit(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
1092{
1093 NEED(2)do { if (len < (2)) return ((void*)0); } while (0);
1094 return wmem_strdup_printf(ctx->scope, "%g A", pntohu16(data) / 1000.0);
1095}
1096
1097static char *
1098decode_dew_enable_port(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
1099{
1100 const char *state;
1101
1102 NEED(2)do { if (len < (2)) return ((void*)0); } while (0);
1103 state = flag_str(ctx, data[1], &tfs_on_off);
1104 return wmem_strdup_printf(ctx->scope, "Port %u %s", data[0], wmem_ascii_strdown(ctx->scope, state, -1));
1105}
1106
1107static char *
1108decode_dew_set_aggression(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
1109{
1110 NEED(2)do { if (len < (2)) return ((void*)0); } while (0);
1111 return wmem_strdup_printf(ctx->scope, "Heater %u, aggression %u", data[0] + 1, data[1]);
1112}
1113
1114static char *
1115decode_dew_set_manual_power(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
1116{
1117 int64_t percent;
1118
1119 NEED(2)do { if (len < (2)) return ((void*)0); } while (0);
1120 percent = (int64_t)rint(data[1] / 2.55);
1121 return wmem_strdup_printf(ctx->scope, "Heater %u, power %" PRId64"l" "d" "%%", data[0] + 1, percent);
1122}
1123
1124static char *
1125decode_dew_recalibrate(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
1126{
1127 return wmem_strdup(ctx->scope, data[0] ? "Start" : "Stop");
1128}
1129
1130static char *
1131decode_dew_is_calibrating(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
1132{
1133 return wmem_strdup(ctx->scope, data[0] ? "Calibrating" : "Not calibrating");
1134}
1135
1136static char *
1137decode_dew_ambient(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
1138{
1139 NEED(9)do { if (len < (9)) return ((void*)0); } while (0);
1140 return wmem_strdup_printf(ctx->scope, "Temp: %g" DEG"\xc2\xb0" "C Dew: %g" DEG"\xc2\xb0" "C Hum: %u%%", pntohu32(data) / 1000.0,
1141 pntohu32(data + 4) / 1000.0, data[8]);
1142}
1143
1144static const value_string battery_level_labels[] = { { 0, "low" }, { 1, "medium" }, { 2, "high" }, { 0, NULL((void*)0) } };
1145
1146static char *
1147decode_battery_status(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply)
1148{
1149 const char *level;
1150
1151 if (!reply)
1152 return bytes_to_str_maxlen(ctx->scope, data, len, 0);
1153 NEED(6)do { if (len < (6)) return ((void*)0); } while (0);
1154 level = try_val_to_str(data[1], battery_level_labels);
1155 return wmem_strdup_printf(ctx->scope, "%.3f V, status %u%s, %s", pntohu32(data + 2) / 1000000.0, data[1],
1156 level ? wmem_strdup_printf(ctx->scope, " (%s)", level) : "", data[0] ? "charging" : "not charging");
1157}
1158
1159static char *
1160decode_wifi_enable(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
1161{
1162 return flag_str(ctx, data[0], &tfs_on_off);
1163}
1164
1165static char *
1166decode_battery_current(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
1167{
1168 NEED(2)do { if (len < (2)) return ((void*)0); } while (0);
1169 return wmem_strdup_printf(ctx->scope, "%u mA", (unsigned)pntohu16(data));
1170}
1171
1172static char *
1173decode_charger_mode(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply)
1174{
1175 char *state = wmem_ascii_strdown(ctx->scope, flag_str(ctx, data[0], &tfs_on_off), -1);
1176
1177 return wmem_strdup_printf(ctx->scope, reply ? "Charging %s" : "Set charging %s", state);
1178}
1179
1180static const value_string light_labels[] = { { 0, "tray" }, { 1, "logo" }, { 2, "Wi-Fi" }, { 0, NULL((void*)0) } };
1181
1182static char *
1183decode_light_level(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply)
1184{
1185 const char *name;
1186 char *light;
1187
1188 if (reply)
1189 return wmem_strdup_printf(ctx->scope, "Level %u", data[0]);
1190 name = try_val_to_str(data[0], light_labels);
1191 light = name ? wmem_strdup(ctx->scope, name) : wmem_strdup_printf(ctx->scope, "light %u", data[0]);
1192 if (len >= 2)
1193 return wmem_strdup_printf(ctx->scope, "Set %s light to %u", light, data[1]);
1194 light[0] = g_ascii_toupper(light[0]);
1195 return wmem_strdup_printf(ctx->scope, "%s light", light);
1196}
1197
1198static char *
1199decode_focuser_position(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
1200{
1201 NEED(3)do { if (len < (3)) return ((void*)0); } while (0);
1202 return wmem_strdup_printf(ctx->scope, "%u", (unsigned)pntohu24(data));
1203}
1204
1205static char *
1206decode_focuser_goto(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply)
1207{
1208 if (reply)
1209 return labelled(ctx, data[0], ack_labels);
1210 NEED(3)do { if (len < (3)) return ((void*)0); } while (0);
1211 return wmem_strdup_printf(ctx->scope, "To %u", (unsigned)pntohu24(data));
1212}
1213
1214static const value_string slew_done_labels[] = { { 0, "Moving" }, { 0xff, "Done" }, { 0, NULL((void*)0) } };
1215
1216static char *
1217decode_focuser_slew_done(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply _U___attribute__((unused)))
1218{
1219 return labelled(ctx, data[0], slew_done_labels);
1220}
1221
1222static char *
1223decode_focuser_move_rate(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len _U___attribute__((unused)), bool_Bool reply)
1224{
1225 if (reply)
1226 return labelled(ctx, data[0], ack_labels);
1227 return wmem_strdup_printf(ctx->scope, "Rate %u", data[0]);
1228}
1229
1230static char *
1231decode_focuser_limits(celestron_aux_ctx_t *ctx, const uint8_t *data, unsigned len, bool_Bool reply _U___attribute__((unused)))
1232{
1233 NEED(8)do { if (len < (8)) return ((void*)0); } while (0);
1234 return wmem_strdup_printf(ctx->scope, "Limits %u to %u", (unsigned)pntohu32(data), (unsigned)pntohu32(data + 4));
1235}
1236
1237/* BEGIN GENERATED DISPATCH: from the celestron-aux catalog */
1238/* The decoder of a device's command, as decoding.py registers them: the device's
1239 * own decoder first, then one that applies to every device. */
1240static celestron_aux_decoder_t
1241celestron_aux_decoder(uint8_t device, uint8_t command)
1242{
1243 switch (device) {
1244 case 0x10:
1245 switch (command) {
1246 case 0x01: return decode_motor_position;
1247 case 0x02: return decode_motor_position;
1248 case 0x05: return decode_motor_model;
1249 case 0x06: return decode_motor_track_rate;
1250 case 0x07: return decode_motor_track_rate;
1251 case 0x12: return decode_motor_switch_over;
1252 case 0x13: return decode_motor_goto_over;
1253 case 0x17: return decode_motor_goto2;
1254 case 0x23: return decode_motor_custom_rate_enabled;
1255 case 0x24: return decode_motor_single_byte;
1256 case 0x25: return decode_motor_single_byte;
1257 case 0x3a: return decode_motor_position;
1258 case 0x3b: return decode_motor_cordwrap_on;
1259 case 0x3c: return decode_motor_position;
1260 case 0x40: return decode_motor_single_byte;
1261 case 0x41: return decode_motor_single_byte;
1262 case 0x46: return decode_motor_autoguide_rate;
1263 case 0x47: return decode_motor_autoguide_rate;
1264 case 0xfc: return decode_motor_approach_direction;
1265 case 0xfd: return decode_motor_approach_direction;
1266 default: break;
1267 }
1268 break;
1269 case 0x11:
1270 switch (command) {
1271 case 0x01: return decode_motor_position;
1272 case 0x02: return decode_motor_position;
1273 case 0x05: return decode_motor_model;
1274 case 0x06: return decode_motor_track_rate;
1275 case 0x07: return decode_motor_track_rate;
1276 case 0x12: return decode_motor_switch_over;
1277 case 0x13: return decode_motor_goto_over;
1278 case 0x17: return decode_motor_goto2;
1279 case 0x23: return decode_motor_custom_rate_enabled;
1280 case 0x24: return decode_motor_single_byte;
1281 case 0x25: return decode_motor_single_byte;
1282 case 0x3a: return decode_motor_position;
1283 case 0x3b: return decode_motor_cordwrap_on;
1284 case 0x3c: return decode_motor_position;
1285 case 0x40: return decode_motor_single_byte;
1286 case 0x41: return decode_motor_single_byte;
1287 case 0x46: return decode_motor_autoguide_rate;
1288 case 0x47: return decode_motor_autoguide_rate;
1289 case 0xfc: return decode_motor_approach_direction;
1290 case 0xfd: return decode_motor_approach_direction;
1291 default: break;
1292 }
1293 break;
1294 case 0x12:
1295 switch (command) {
1296 case 0x01: return decode_focuser_position;
1297 case 0x02: return decode_focuser_goto;
1298 case 0x13: return decode_focuser_slew_done;
1299 case 0x24: return decode_focuser_move_rate;
1300 case 0x25: return decode_focuser_move_rate;
1301 case 0x2c: return decode_focuser_limits;
1302 default: break;
1303 }
1304 break;
1305 case 0x17:
1306 switch (command) {
1307 case 0x00: return decode_dew_power;
1308 case 0x02: return decode_dew_led_brightness;
1309 case 0x03: return decode_dew_set_input_limit;
1310 case 0x04: return decode_dew_power_limit;
1311 case 0x10: return decode_dew_power_ports;
1312 case 0x11: return decode_dew_ports;
1313 case 0x12: return decode_dew_status;
1314 case 0x14: return decode_dew_enable_port;
1315 case 0x16: return decode_dew_set_aggression;
1316 case 0x17: return decode_dew_set_manual_power;
1317 case 0x18: return decode_dew_ambient;
1318 case 0x19: return decode_dew_recalibrate;
1319 case 0x1a: return decode_dew_is_calibrating;
1320 case 0x20: return decode_dew_led_brightness;
1321 default: break;
1322 }
1323 break;
1324 case 0xb0:
1325 switch (command) {
1326 case 0x01: return decode_gps_coordinate;
1327 case 0x02: return decode_gps_coordinate;
1328 case 0x03: return decode_gps_date;
1329 case 0x04: return decode_gps_year;
1330 case 0x07: return decode_gps_satellites;
1331 case 0x33: return decode_gps_time;
1332 case 0x36: return decode_gps_flag;
1333 case 0x37: return decode_gps_flag;
1334 case 0x55: return decode_gps_hardware_version;
1335 default: break;
1336 }
1337 break;
1338 case 0xb4:
1339 switch (command) {
1340 case 0x3f: return decode_starsense_align_center;
1341 case 0x91: return decode_starsense_status;
1342 case 0x92: return decode_starsense_status;
1343 case 0x94: return decode_starsense_status;
1344 case 0x9f: return decode_starsense_status;
1345 default: break;
1346 }
1347 break;
1348 case 0xb5:
1349 switch (command) {
1350 case 0x10: return decode_wifi_enable;
1351 default: break;
1352 }
1353 break;
1354 case 0xb6:
1355 switch (command) {
1356 case 0x10: return decode_battery_status;
1357 case 0x18: return decode_battery_current;
1358 default: break;
1359 }
1360 break;
1361 case 0xb7:
1362 switch (command) {
1363 case 0x10: return decode_charger_mode;
1364 default: break;
1365 }
1366 break;
1367 case 0xb9:
1368 switch (command) {
1369 case 0x02: return decode_ssag_plate_status;
1370 case 0x03: return decode_ssag_start_calibrate;
1371 case 0x04: return decode_ssag_calibrate_status;
1372 case 0x20: return decode_ssag_set_guiding;
1373 case 0x30: return decode_ssag_mount_info;
1374 case 0x31: return decode_ssag_geolocation;
1375 case 0x32: return decode_ssag_time;
1376 case 0x45: return decode_ssag_image_rotation;
1377 default: break;
1378 }
1379 break;
1380 case 0xbd:
1381 switch (command) {
1382 case 0x02: return decode_ssag_plate_status;
1383 case 0x03: return decode_ssag_start_calibrate;
1384 case 0x04: return decode_ssag_calibrate_status;
1385 case 0x07: return decode_ss2_is_calibrated;
1386 case 0x08: return decode_ss2_calibration_data;
1387 case 0x13: return decode_ss2_set_aspa;
1388 case 0x15: return decode_ss2_aspa_pole;
1389 case 0x16: return decode_ss2_aspa_error;
1390 case 0x17: return decode_ss2_aspa_progress;
1391 case 0x30: return decode_ssag_mount_info;
1392 case 0x31: return decode_ssag_geolocation;
1393 case 0x32: return decode_ssag_time;
1394 case 0x45: return decode_ssag_image_rotation;
1395 case 0x4b: return decode_ssag_plate_status;
1396 default: break;
1397 }
1398 break;
1399 case 0xbf:
1400 switch (command) {
1401 case 0x10: return decode_light_level;
1402 default: break;
1403 }
1404 break;
1405 default:
1406 break;
1407 }
1408 switch (command) {
1409 case 0xfe: return decode_version;
1410 default:
1411 return NULL((void*)0);
1412 }
1413}
1414/* END GENERATED DISPATCH */
1415
1416/* --- frames ------------------------------------------------------------------ */
1417
1418/* The device a packet is about: command numbers belong to it. */
1419static uint8_t
1420subject_device(uint8_t src, uint8_t dst)
1421{
1422 if (src == CELESTRON_AUX_SCANNER0x2f)
1423 return dst;
1424 if (dst == CELESTRON_AUX_SCANNER0x2f)
1425 return src;
1426 if (celestron_aux_is_motor_client(src) && celestron_aux_is_motor(dst))
1427 return dst;
1428 return celestron_aux_is_controller(src) ? dst : src;
1429}
1430
1431static const char *
1432command_name(uint8_t device, uint8_t command)
1433{
1434 const value_string *table = celestron_aux_command_table(device);
1435 const char *name = table ? try_val_to_str(command, table) : NULL((void*)0);
1436
1437 if (!name)
1438 name = try_val_to_str(command, celestron_aux_generic_commands);
1439 return name ? name : "Unknown";
1440}
1441
1442static const char *
1443device_name(uint8_t addr)
1444{
1445 return val_to_str_const(addr, celestron_aux_devices, "Unknown");
1446}
1447
1448/* Reads one logical byte of a StarSense frame at *offset, undoing 3B 02 02
1449 * escapes. Returns false when the buffer ends first. */
1450static bool_Bool
1451starsense_byte(tvbuff_t *tvb, unsigned *offset, uint8_t *byte)
1452{
1453 unsigned available = tvb_captured_length(tvb);
1454
1455 if (*offset >= available)
15
Assuming the condition is true
16
Taking true branch
1456 return false0;
17
Returning without writing to '*byte'
1457 *byte = tvb_get_uint8(tvb, *offset);
1458 if (*byte != AUX_PREAMBLE0x3b) {
1459 *offset += 1;
1460 return true1;
1461 }
1462 if (*offset + 2 >= available)
1463 return false0;
1464 *offset += (tvb_get_uint8(tvb, *offset + 1) == 2 && tvb_get_uint8(tvb, *offset + 2) == 2) ? 3 : 1;
1465 return true1;
1466}
1467
1468#define NEED_MORE0 0
1469#define NO_FRAME-1 -1
1470
1471/* The size of the frame starting at offset, NEED_MORE when the buffer ends
1472 * first, or NO_FRAME when no frame can start there (as celestron-aux's parser). */
1473static int
1474frame_length(tvbuff_t *tvb, unsigned offset)
1475{
1476 unsigned available = tvb_captured_length_remaining(tvb, offset);
1477 uint8_t first = tvb_get_uint8(tvb, offset);
1478
1479 if (first == AUX_PREAMBLE0x3b) {
1480 unsigned length;
1481
1482 if (available < 2)
1483 return NEED_MORE0;
1484 length = tvb_get_uint8(tvb, offset + 1);
1485 if (length < AUX_MIN_LENGTH3)
1486 return NO_FRAME-1;
1487 return available < length + 3 ? NEED_MORE0 : (int)(length + 3);
1488 }
1489 if (first == STARSENSE_PREAMBLE0x3c) {
1490 unsigned position = offset + 1;
1491 uint32_t payload = 0;
1492 uint8_t byte;
1493
1494 for (int i = 0; i < 4; i++) {
1495 if (!starsense_byte(tvb, &position, &byte))
1496 return NEED_MORE0;
1497 payload = (payload << 8) | byte;
1498 }
1499 if (payload > STARSENSE_MAX_PAYLOAD0xffff)
1500 return NO_FRAME-1;
1501 for (uint32_t i = 0; i < payload; i++)
1502 if (!starsense_byte(tvb, &position, &byte))
1503 return NEED_MORE0;
1504 if (position >= tvb_captured_length(tvb))
1505 return NEED_MORE0;
1506 return (int)(position + 1 - offset);
1507 }
1508 return NO_FRAME-1;
1509}
1510
1511/* The checksum a frame should end with: the two's complement of the sum of
1512 * its bytes after the preamble (unescaped, for a StarSense frame). */
1513static uint8_t
1514expected_checksum(tvbuff_t *tvb, unsigned offset, unsigned size)
1515{
1516 uint32_t sum = 0;
1517
1518 if (tvb_get_uint8(tvb, offset) == AUX_PREAMBLE0x3b) {
1519 for (unsigned i = offset + 1; i < offset + size - 1; i++)
1520 sum += tvb_get_uint8(tvb, i);
1521 } else {
1522 unsigned position = offset + 1;
1523 uint8_t byte;
1524
1525 while (position < offset + size - 1 && starsense_byte(tvb, &position, &byte))
1526 sum += byte;
1527 }
1528 return (uint8_t)((0x100 - (sum & 0xff)) & 0xff);
1529}
1530
1531static bool_Bool
1532checksum_ok(tvbuff_t *tvb, unsigned offset, unsigned size)
1533{
1534 return tvb_get_uint8(tvb, offset + size - 1) == expected_checksum(tvb, offset, size);
1535}
1536
1537static char *
1538dissect_aux_packet(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, unsigned key)
1539{
1540 unsigned length = tvb_get_uint8(tvb, 1);
1541 unsigned data_length = length - AUX_MIN_LENGTH3;
1542 uint8_t src = tvb_get_uint8(tvb, 2);
1543 uint8_t dst = tvb_get_uint8(tvb, 3);
1544 uint8_t cmd = tvb_get_uint8(tvb, 4);
1545 uint8_t device = subject_device(src, dst);
1546 const char *name = command_name(device, cmd);
1547 char *value = NULL((void*)0);
1548 bool_Bool unexpected = false0;
1549 const char *shown;
1550 proto_item *ti;
1551
1552 proto_item_append_text(proto_tree_get_parent(tree), ", %s -> %s, %s", device_name(src), device_name(dst), name);
1553 proto_tree_add_item(tree, hf_celestron_aux_preamble, tvb, 0, 1, ENC_NA0x00000000);
1554 proto_tree_add_item(tree, hf_celestron_aux_length, tvb, 1, 1, ENC_NA0x00000000);
1555 proto_tree_add_item(tree, hf_celestron_aux_src, tvb, 2, 1, ENC_NA0x00000000);
1556 proto_tree_add_item(tree, hf_celestron_aux_dst, tvb, 3, 1, ENC_NA0x00000000);
1557 ti = proto_tree_add_item(tree, hf_celestron_aux_cmd, tvb, 4, 1, ENC_NA0x00000000);
1558 proto_item_append_text(ti, " (%s)", name);
1559 proto_tree_add_string(tree, hf_celestron_aux_cmd_name, tvb, 4, 1, name);
1560
1561 if (data_length > 0) {
1562 celestron_aux_decoder_t decoder = celestron_aux_decoder(device, cmd);
1563 const uint8_t *data = tvb_get_ptr(tvb, 5, data_length);
1564
1565 proto_tree_add_item(tree, hf_celestron_aux_data, tvb, 5, data_length, ENC_NA0x00000000);
1566 if (decoder) {
1567 celestron_aux_ctx_t ctx = { pinfo->pool, pinfo, key, tvb_new_subset_length(tvb, 5, data_length) };
1568
1569 value = decoder(&ctx, data, data_length, src == device);
1570 unexpected = (value == NULL((void*)0));
1571 }
1572 if (value) {
1573 proto_tree_add_string(tree, hf_celestron_aux_value, tvb, 5, data_length, value);
1574 proto_item_append_text(proto_tree_get_parent(tree), ", %s", value);
1575 } else if (unexpected) {
1576 proto_tree_add_expert(tree, pinfo, &ei_celestron_aux_unexpected_format, tvb, 5, data_length);
1577 }
1578 }
1579
1580 proto_tree_add_checksum(tree, tvb, length + 2, hf_celestron_aux_checksum, hf_celestron_aux_checksum_status,
1581 &ei_celestron_aux_bad_checksum, pinfo, expected_checksum(tvb, 0, length + 3), ENC_NA0x00000000,
1582 PROTO_CHECKSUM_VERIFY0x01);
1583
1584 /* As celestron-aux's own log: the decoded value, else the data in hex. */
1585 if (value)
1586 shown = value;
1587 else if (data_length > 0)
1588 shown = wmem_strdup_printf(pinfo->pool, "%s%s", tvb_bytes_to_str(pinfo->pool, tvb, 5, data_length),
1589 unexpected ? " (unexpected format)" : "");
1590 else
1591 shown = "";
1592 return wmem_strdup_printf(pinfo->pool, "%s -> %s %s (0x%02x)%s%s%s", device_name(src), device_name(dst),
1593 name, cmd, *shown ? " " : "", shown, checksum_ok(tvb, 0, length + 3) ? "" : " [bad checksum]");
1594}
1595
1596static char *
1597dissect_starsense_frame(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1598{
1599 unsigned size = tvb_captured_length(tvb);
1600 unsigned position = 1;
1601 uint32_t payload = 0;
1602 uint8_t byte;
12
'byte' declared without an initial value
1603 bool_Bool ok = checksum_ok(tvb, 0, size);
1604
1605 proto_tree_add_item(tree, hf_celestron_aux_preamble, tvb, 0, 1, ENC_NA0x00000000);
1606 for (int i = 0; i < 4; i++) {
13
Loop condition is true. Entering loop body
1607 starsense_byte(tvb, &position, &byte); /* frame_length checked the frame is complete */
14
Calling 'starsense_byte'
18
Returning from 'starsense_byte'
1608 payload = (payload << 8) | byte;
19
The right operand of '|' is a garbage value
1609 }
1610 proto_item_append_text(proto_tree_get_parent(tree), ", StarSense star list, %u stars", payload / 8);
1611 proto_tree_add_uint(tree, hf_celestron_aux_starsense_length, tvb, 1, position - 1, payload);
1612 proto_item_set_generated(proto_tree_add_uint(tree, hf_celestron_aux_starsense_stars, tvb, 1, position - 1,
1613 payload / 8));
1614 proto_tree_add_item(tree, hf_celestron_aux_starsense_data, tvb, position, size - 1 - position, ENC_NA0x00000000);
1615 proto_tree_add_checksum(tree, tvb, size - 1, hf_celestron_aux_checksum, hf_celestron_aux_checksum_status,
1616 &ei_celestron_aux_bad_checksum, pinfo, expected_checksum(tvb, 0, size), ENC_NA0x00000000,
1617 PROTO_CHECKSUM_VERIFY0x01);
1618 return wmem_strdup_printf(pinfo->pool, "StarSense star list, %u stars%s", payload / 8,
1619 ok ? "" : " [bad checksum]");
1620}
1621
1622/* Dissects every frame in tvb. Over TCP a frame can span segments: then it
1623 * asks for more data, as the bus is a plain byte stream. */
1624static int
1625dissect_frames(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, bool_Bool can_desegment)
1626{
1627 unsigned offset = 0;
1628 unsigned captured = tvb_captured_length(tvb);
1629 wmem_strbuf_t *info = wmem_strbuf_new(pinfo->pool, "");
1630 bool_Bool any = false0;
1631
1632 while (offset < captured) {
5
Assuming 'offset' is < 'captured'
6
Loop condition is true. Entering loop body
1633 int size = frame_length(tvb, offset);
1634 proto_item *ti;
1635 proto_tree *frame_tree;
1636
1637 if (size
6.1
'size' is not equal to NEED_MORE
== NEED_MORE0 && can_desegment) {
1638 pinfo->desegment_offset = offset;
1639 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT0x0fffffff;
1640 break;
1641 }
1642 if (size
6.2
'size' is not equal to NEED_MORE
== NEED_MORE0) {
7
Taking false branch
1643 ti = proto_tree_add_item(tree, proto_celestron_aux, tvb, offset, -1, ENC_NA0x00000000);
1644 expert_add_info(pinfo, ti, &ei_celestron_aux_cut_short);
1645 wmem_strbuf_append_printf(info, "%sFrame cut short", any ? " | " : "");
1646 any = true1;
1647 offset = captured;
1648 } else if (size == NO_FRAME-1) {
8
Taking false branch
1649 unsigned stop = offset + 1;
1650
1651 while (stop < captured && tvb_get_uint8(tvb, stop) != AUX_PREAMBLE0x3b &&
1652 tvb_get_uint8(tvb, stop) != STARSENSE_PREAMBLE0x3c)
1653 stop++;
1654 ti = proto_tree_add_item(tree, proto_celestron_aux, tvb, offset, stop - offset, ENC_NA0x00000000);
1655 proto_item_append_text(ti, ", bytes outside any frame");
1656 expert_add_info(pinfo, ti, &ei_celestron_aux_outside_frame);
1657 wmem_strbuf_append_printf(info, "%sBytes outside any frame", any ? " | " : "");
1658 any = true1;
1659 offset = stop;
1660 } else {
1661 tvbuff_t *frame = tvb_new_subset_length(tvb, offset, size);
1662 char *text;
1663
1664 ti = proto_tree_add_item(tree, proto_celestron_aux, tvb, offset, size, ENC_NA0x00000000);
1665 frame_tree = proto_item_add_subtree(ti, ett_celestron_aux);
1666 if (tvb_get_uint8(frame, 0) == AUX_PREAMBLE0x3b)
9
Assuming the condition is false
10
Taking false branch
1667 text = dissect_aux_packet(frame, pinfo, frame_tree, offset);
1668 else
1669 text = dissect_starsense_frame(frame, pinfo, frame_tree);
11
Calling 'dissect_starsense_frame'
1670 wmem_strbuf_append_printf(info, "%s%s", any ? " | " : "", text);
1671 any = true1;
1672 /* After a bad checksum, move on one byte only, as celestron-aux's own
1673 * parser does: the "frame" may be noise whose length byte swallowed
1674 * real frames, which are found again by resynchronising. */
1675 offset += checksum_ok(tvb, offset, size) ? (unsigned)size : 1;
1676 }
1677 }
1678 /* Columns only for complete frames: a segment holding just the start of a
1679 * frame stays TCP until the frame is reassembled. */
1680 if (any) {
1681 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Celestron AUX");
1682 col_add_str(pinfo->cinfo, COL_INFO, wmem_strbuf_get_str(info));
1683 }
1684 return captured;
1685}
1686
1687/* One frame (or a celestron-aux mark) per record, as in exported pcap files. */
1688static int
1689dissect_celestron_aux(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U___attribute__((unused)))
1690{
1691 unsigned captured = tvb_captured_length(tvb);
1692
1693 if (captured == 0)
1694 return 0;
1695 if (captured >= MARK_PREFIX_LEN6 && tvb_memeql(tvb, 0, (const uint8_t *)MARK_PREFIX"#MARK ", MARK_PREFIX_LEN6) == 0) {
1696 proto_item *ti = proto_tree_add_item(tree, proto_celestron_aux, tvb, 0, -1, ENC_NA0x00000000);
1697 proto_tree *mark_tree = proto_item_add_subtree(ti, ett_celestron_aux);
1698 const char *text = (const char *)tvb_get_string_enc(pinfo->pool, tvb, MARK_PREFIX_LEN6,
1699 captured - MARK_PREFIX_LEN6, ENC_UTF_80x00000002);
1700
1701 proto_tree_add_string(mark_tree, hf_celestron_aux_mark, tvb, 0, -1, text);
1702 col_set_str(pinfo->cinfo, COL_PROTOCOL, "MARK");
1703 col_add_fstr(pinfo->cinfo, COL_INFO, "MARK: %s", text);
1704 return captured;
1705 }
1706 return dissect_frames(tvb, pinfo, tree, false0);
1707}
1708
1709static int
1710dissect_celestron_aux_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U___attribute__((unused)))
1711{
1712 if (tvb_captured_length(tvb) == 0)
1
Assuming the condition is false
2
Taking false branch
1713 return 0;
1714 return dissect_frames(tvb, pinfo, tree, pinfo->can_desegment > 0);
3
Assuming field 'can_desegment' is > 0
4
Calling 'dissect_frames'
1715}
1716
1717/* A TCP payload that starts with a complete AUX packet whose checksum is right. */
1718static bool_Bool
1719dissect_celestron_aux_heur_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1720{
1721 unsigned captured = tvb_captured_length(tvb);
1722 unsigned length;
1723 uint32_t sum = 0;
1724
1725 if (captured < 6 || tvb_get_uint8(tvb, 0) != AUX_PREAMBLE0x3b)
1726 return false0;
1727 length = tvb_get_uint8(tvb, 1);
1728 if (length < AUX_MIN_LENGTH3 || captured < length + 3)
1729 return false0;
1730 for (unsigned i = 1; i < length + 2; i++)
1731 sum += tvb_get_uint8(tvb, i);
1732 if (tvb_get_uint8(tvb, length + 2) != ((0x100 - (sum & 0xff)) & 0xff))
1733 return false0;
1734 conversation_set_dissector(find_or_create_conversation(pinfo), celestron_aux_tcp_handle);
1735 dissect_celestron_aux_tcp(tvb, pinfo, tree, data);
1736 return true1;
1737}
1738
1739void
1740proto_register_celestron_aux(void)
1741{
1742 static hf_register_info hf[] = {
1743 { &hf_celestron_aux_preamble,
1744 { "Preamble", "celestron_aux.preamble", FT_UINT8, BASE_HEX, NULL((void*)0), 0x0,
1745 "0x3b for an AUX packet, 0x3c for a StarSense frame", HFILL-1, 0, HF_REF_TYPE_NONE, -1, ((void*)0) }},
1746 { &hf_celestron_aux_length,
1747 { "Length", "celestron_aux.length", FT_UINT8, BASE_DEC, NULL((void*)0), 0x0,
1748 "Bytes from source to the end of the data", HFILL-1, 0, HF_REF_TYPE_NONE, -1, ((void*)0) }},
1749 { &hf_celestron_aux_src,
1750 { "Source", "celestron_aux.src", FT_UINT8, BASE_HEX, VALS(celestron_aux_devices)((0 ? (const struct _value_string*)0 : ((celestron_aux_devices
))))
, 0x0,
1751 NULL((void*)0), HFILL-1, 0, HF_REF_TYPE_NONE, -1, ((void*)0) }},
1752 { &hf_celestron_aux_dst,
1753 { "Destination", "celestron_aux.dst", FT_UINT8, BASE_HEX, VALS(celestron_aux_devices)((0 ? (const struct _value_string*)0 : ((celestron_aux_devices
))))
, 0x0,
1754 NULL((void*)0), HFILL-1, 0, HF_REF_TYPE_NONE, -1, ((void*)0) }},
1755 { &hf_celestron_aux_cmd,
1756 { "Command", "celestron_aux.cmd", FT_UINT8, BASE_HEX, NULL((void*)0), 0x0,
1757 "Command number, named by the device the packet is about", HFILL-1, 0, HF_REF_TYPE_NONE, -1, ((void*)0) }},
1758 { &hf_celestron_aux_cmd_name,
1759 { "Command name", "celestron_aux.cmd_name", FT_STRING, BASE_NONE, NULL((void*)0), 0x0,
1760 NULL((void*)0), HFILL-1, 0, HF_REF_TYPE_NONE, -1, ((void*)0) }},
1761 { &hf_celestron_aux_data,
1762 { "Data", "celestron_aux.data", FT_BYTES, BASE_NONE, NULL((void*)0), 0x0,
1763 NULL((void*)0), HFILL-1, 0, HF_REF_TYPE_NONE, -1, ((void*)0) }},
1764 { &hf_celestron_aux_value,
1765 { "Value", "celestron_aux.value", FT_STRING, BASE_NONE, NULL((void*)0), 0x0,
1766 "The data decoded", HFILL-1, 0, HF_REF_TYPE_NONE, -1, ((void*)0) }},
1767 { &hf_celestron_aux_checksum,
1768 { "Checksum", "celestron_aux.checksum", FT_UINT8, BASE_HEX, NULL((void*)0), 0x0,
1769 NULL((void*)0), HFILL-1, 0, HF_REF_TYPE_NONE, -1, ((void*)0) }},
1770 { &hf_celestron_aux_checksum_status,
1771 { "Checksum Status", "celestron_aux.checksum.status", FT_UINT8, BASE_NONE, VALS(proto_checksum_vals)((0 ? (const struct _value_string*)0 : ((proto_checksum_vals)
)))
, 0x0,
1772 NULL((void*)0), HFILL-1, 0, HF_REF_TYPE_NONE, -1, ((void*)0) }},
1773 { &hf_celestron_aux_starsense_length,
1774 { "StarSense payload length", "celestron_aux.starsense.length", FT_UINT32, BASE_DEC, NULL((void*)0), 0x0,
1775 NULL((void*)0), HFILL-1, 0, HF_REF_TYPE_NONE, -1, ((void*)0) }},
1776 { &hf_celestron_aux_starsense_stars,
1777 { "Stars", "celestron_aux.starsense.stars", FT_UINT32, BASE_DEC, NULL((void*)0), 0x0,
1778 NULL((void*)0), HFILL-1, 0, HF_REF_TYPE_NONE, -1, ((void*)0) }},
1779 { &hf_celestron_aux_starsense_data,
1780 { "StarSense frame (escaped)", "celestron_aux.starsense.data", FT_BYTES, BASE_NONE, NULL((void*)0), 0x0,
1781 NULL((void*)0), HFILL-1, 0, HF_REF_TYPE_NONE, -1, ((void*)0) }},
1782 { &hf_celestron_aux_mark,
1783 { "Mark", "celestron_aux.mark", FT_STRING, BASE_NONE, NULL((void*)0), 0x0,
1784 "A note taken while recording with celestron-aux", HFILL-1, 0, HF_REF_TYPE_NONE, -1, ((void*)0) }},
1785 };
1786
1787 static int *ett[] = {
1788 &ett_celestron_aux,
1789 };
1790
1791 static ei_register_info ei[] = {
1792 { &ei_celestron_aux_bad_checksum,
1793 { "celestron_aux.bad_checksum", PI_CHECKSUM0x01000000, PI_ERROR0x00800000, "Bad checksum", EXPFILL0, ((void*)0), 0, ((void*)0), {0, {((void*)0), ((void*)0), FT_NONE
, BASE_NONE, ((void*)0), 0, ((void*)0), -1, 0, HF_REF_TYPE_NONE
, -1, ((void*)0)}}
}},
1794 { &ei_celestron_aux_unexpected_format,
1795 { "celestron_aux.unexpected_format", PI_PROTOCOL0x09000000, PI_WARN0x00600000,
1796 "Data does not fit the command's known format", EXPFILL0, ((void*)0), 0, ((void*)0), {0, {((void*)0), ((void*)0), FT_NONE
, BASE_NONE, ((void*)0), 0, ((void*)0), -1, 0, HF_REF_TYPE_NONE
, -1, ((void*)0)}}
}},
1797 { &ei_celestron_aux_outside_frame,
1798 { "celestron_aux.outside_frame", PI_MALFORMED0x07000000, PI_ERROR0x00800000, "Bytes outside any frame", EXPFILL0, ((void*)0), 0, ((void*)0), {0, {((void*)0), ((void*)0), FT_NONE
, BASE_NONE, ((void*)0), 0, ((void*)0), -1, 0, HF_REF_TYPE_NONE
, -1, ((void*)0)}}
}},
1799 { &ei_celestron_aux_cut_short,
1800 { "celestron_aux.cut_short", PI_MALFORMED0x07000000, PI_ERROR0x00800000, "Frame cut short", EXPFILL0, ((void*)0), 0, ((void*)0), {0, {((void*)0), ((void*)0), FT_NONE
, BASE_NONE, ((void*)0), 0, ((void*)0), -1, 0, HF_REF_TYPE_NONE
, -1, ((void*)0)}}
}},
1801 };
1802
1803 expert_module_t *expert_celestron_aux;
1804
1805 proto_celestron_aux = proto_register_protocol("Celestron AUX bus", "Celestron AUX", "celestron_aux");
1806 proto_register_field_array(proto_celestron_aux, hf, array_length(hf)(sizeof (hf) / sizeof (hf)[0]));
1807 proto_register_subtree_array(ett, array_length(ett)(sizeof (ett) / sizeof (ett)[0]));
1808 expert_celestron_aux = expert_register_protocol(proto_celestron_aux);
1809 expert_register_field_array(expert_celestron_aux, ei, array_length(ei)(sizeof (ei) / sizeof (ei)[0]));
1810
1811 /* celestron_aux_handle = */ register_dissector("celestron_aux", dissect_celestron_aux, proto_celestron_aux);
1812 celestron_aux_tcp_handle = register_dissector("celestron_aux.tcp", dissect_celestron_aux_tcp,
1813 proto_celestron_aux);
1814}
1815
1816void
1817proto_reg_handoff_celestron_aux(void)
1818{
1819 /* TCP port 2000 is Skinny's, so no port by default: use Decode As, the
1820 * TCP port preference, or the heuristic. */
1821 dissector_add_for_decode_as_with_preference("tcp.port", celestron_aux_tcp_handle);
1822 heur_dissector_add("tcp", dissect_celestron_aux_heur_tcp, "Celestron AUX over TCP",
1823 "celestron_aux_tcp", proto_celestron_aux, HEURISTIC_DISABLE);
1824}
1825
1826/*
1827 * Editor modelines - https://www.wireshark.org/tools/modelines.html
1828 *
1829 * Local variables:
1830 * c-basic-offset: 4
1831 * tab-width: 8
1832 * indent-tabs-mode: nil
1833 * End:
1834 *
1835 * vi: set shiftwidth=4 tabstop=8 expandtab:
1836 * :indentSize=4:tabSize=8:noTabs=true:
1837 */