/src/gpsd/gpsd-3.27.6~dev/drivers/driver_nmea2000.c
Line | Count | Source |
1 | | /* |
2 | | * NMEA2000 over CAN. |
3 | | * |
4 | | * NMEA2000 is proprietary and the doc is not public. |
5 | | * Much of this code is reverse engineered or built from |
6 | | * the sparse doc some vendors provide on their interpretation |
7 | | * of the specification. |
8 | | * |
9 | | * Here is one good source of reverse engineered info: |
10 | | * https://github.com/canboat/canboat |
11 | | * https://canboat.github.io/canboat/canboat.html |
12 | | * |
13 | | * Message contents can be had from canboat/analyzer: |
14 | | * analyzer -explain |
15 | | * |
16 | | * This file is Copyright by the GPSD project |
17 | | * SPDX-License-Identifier: BSD-2-clause |
18 | | */ |
19 | | |
20 | | #include "../include/gpsd_config.h" // must be before all includes |
21 | | |
22 | | #if defined(NMEA2000_ENABLE) |
23 | | |
24 | | #include <ctype.h> |
25 | | #include <errno.h> // for strerror(errno), errno) |
26 | | #include <fcntl.h> |
27 | | #include <linux/can.h> // for struct can_frame |
28 | | #include <linux/can/error.h> // for CAN_ERR_BUSOFF, etc. |
29 | | #include <linux/can/raw.h> |
30 | | #include <math.h> |
31 | | #include <net/if.h> |
32 | | #include <stdarg.h> |
33 | | #include <stdbool.h> |
34 | | #include <stdio.h> |
35 | | #include <stdlib.h> |
36 | | #include <string.h> |
37 | | #include <sys/ioctl.h> |
38 | | #include <sys/socket.h> |
39 | | #include <time.h> |
40 | | #include <unistd.h> |
41 | | |
42 | | #include "../include/gpsd.h" |
43 | | #include "../include/libgps.h" |
44 | | #include "../include/driver_nmea2000.h" |
45 | | #include "../include/bits.h" |
46 | | #include "../include/timespec.h" |
47 | | |
48 | | |
49 | | #define LOG_FILE 1 |
50 | 0 | #define NMEA2000_NETS 4 |
51 | | /* NMEA 2000 unit is a byte, but 254 is "request for address claim" |
52 | | * and 255 is broadcast address. So 253 is the number of units possible, |
53 | | * and the highest unit number. */ |
54 | 0 | #define NMEA2000_UNITS 253 |
55 | | #define CAN_NAMELEN 32 |
56 | 0 | #define MIN(a,b) ((a < b) ? a : b) |
57 | | |
58 | | #define NMEA2000_DEBUG_AIS 0 |
59 | | #define NMEA2000_FAST_DEBUG 0 |
60 | | |
61 | | static struct gps_device_t *nmea2000_units[NMEA2000_NETS][NMEA2000_UNITS]; |
62 | | static char can_interface_name[NMEA2000_NETS][CAN_NAMELEN+1]; |
63 | | |
64 | | typedef struct PGN { |
65 | | unsigned int pgn; |
66 | | unsigned int fast; |
67 | | unsigned int type; |
68 | | gps_mask_t (* func)(unsigned char *bu, int len, struct PGN *pgn, |
69 | | struct gps_device_t *session); |
70 | | const char *name; |
71 | | } PGN; |
72 | | |
73 | | |
74 | | #if LOG_FILE |
75 | | FILE *logFile = NULL; |
76 | | #endif // of if LOG_FILE |
77 | | |
78 | | extern bool __attribute__ ((weak)) gpsd_add_device(const char *device_name, |
79 | | bool flag_nowait); |
80 | | |
81 | 0 | #define SHIFT32 0x100000000l |
82 | | |
83 | | static int scale_int(int32_t var, const int64_t factor) |
84 | 0 | { |
85 | 0 | int64_t ret; |
86 | |
|
87 | 0 | ret = var; |
88 | 0 | ret *= factor; |
89 | 0 | ret >>= 32; |
90 | |
|
91 | 0 | return (int)ret; |
92 | 0 | } |
93 | | |
94 | | static void print_data(struct gps_context_t *context, |
95 | | unsigned char *buffer, int len, PGN *pgn) |
96 | 0 | { |
97 | 0 | if (LOG_IO <= libgps_debuglevel) { |
98 | 0 | int l1; |
99 | 0 | char bu[128]; |
100 | |
|
101 | 0 | int ptr = 0; |
102 | 0 | int l2 = sprintf(&bu[ptr], "got data:%6u:%3d: ", pgn->pgn, len); |
103 | 0 | ptr += l2; |
104 | 0 | for (l1 = 0; l1 < len; l1++) { |
105 | 0 | if (((l1 % 20) == 0) && (l1 != 0)) { |
106 | 0 | GPSD_LOG(LOG_IO, &context->errout, "%s\n", bu); |
107 | 0 | ptr = 0; |
108 | 0 | l2 = sprintf(&bu[ptr], " : "); |
109 | 0 | ptr += l2; |
110 | 0 | } |
111 | 0 | l2 = sprintf(&bu[ptr], "%02ux ", (unsigned int)buffer[l1]); |
112 | 0 | ptr += l2; |
113 | 0 | } |
114 | 0 | GPSD_LOG(LOG_IO, &context->errout, "%s\n", bu); |
115 | 0 | } |
116 | 0 | } |
117 | | |
118 | | static gps_mask_t get_mode(struct gps_device_t *session) |
119 | 0 | { |
120 | 0 | if (1 & session->driver.nmea2000.mode_valid) { |
121 | 0 | session->newdata.mode = session->driver.nmea2000.mode; |
122 | 0 | } else { |
123 | 0 | session->newdata.mode = MODE_NOT_SEEN; |
124 | 0 | } |
125 | |
|
126 | 0 | if (2 & session->driver.nmea2000.mode_valid) { |
127 | 0 | return MODE_SET | USED_IS; |
128 | 0 | } else { |
129 | 0 | return MODE_SET; |
130 | 0 | } |
131 | 0 | } |
132 | | |
133 | | |
134 | | static int decode_ais_header(struct gps_context_t *context, |
135 | | unsigned char *bu, |
136 | | int len, |
137 | | struct ais_t *ais, |
138 | | unsigned int mask) |
139 | 0 | { |
140 | 0 | if (4 < len) { |
141 | 0 | ais->type = (unsigned int) ( bu[0] & 0x3f); |
142 | 0 | ais->repeat = (unsigned int) ((bu[0] >> 6) & 0x03); |
143 | 0 | ais->mmsi = (unsigned int) getleu32(bu, 1); |
144 | 0 | ais->mmsi &= mask; |
145 | 0 | GPSD_LOG(LOG_INF, &context->errout, |
146 | 0 | "NMEA2000 AIS message type %u, MMSI %09d:\n", |
147 | 0 | ais->type, ais->mmsi); |
148 | 0 | return 1; |
149 | 0 | } else { |
150 | 0 | ais->type = 0; |
151 | 0 | ais->repeat = 0; |
152 | 0 | ais->mmsi = 0; |
153 | 0 | GPSD_LOG(LOG_ERROR, &context->errout, |
154 | 0 | "NMEA2000 AIS message type %u, too short message.\n", |
155 | 0 | ais->type); |
156 | 0 | } |
157 | 0 | return 0; |
158 | 0 | } |
159 | | |
160 | | |
161 | | static void decode_ais_channel_info(unsigned char *bu, |
162 | | int len, |
163 | | unsigned int offset, |
164 | | struct gps_device_t *session) |
165 | 0 | { |
166 | 0 | unsigned int pos, bpos; |
167 | 0 | uint16_t x; |
168 | |
|
169 | 0 | pos = offset / 8; |
170 | 0 | bpos = offset % 8; |
171 | 0 | if (pos >= (unsigned int)len) { |
172 | 0 | session->driver.aivdm.ais_channel = 'A'; |
173 | 0 | return; |
174 | 0 | } |
175 | 0 | x = getleu16(bu, pos); |
176 | 0 | x = (uint16_t)((x >> bpos) & 0x1f); |
177 | 0 | switch (x) { |
178 | 0 | case 1: |
179 | 0 | FALLTHROUGH |
180 | 0 | case 3: |
181 | 0 | session->driver.aivdm.ais_channel = 'B'; |
182 | 0 | break; |
183 | 0 | default: |
184 | 0 | session->driver.aivdm.ais_channel = 'A'; |
185 | 0 | break; |
186 | 0 | } |
187 | 0 | return; |
188 | 0 | } |
189 | | |
190 | | |
191 | | static int ais_turn_rate(int rate) |
192 | 0 | { |
193 | 0 | if (0 > rate) { |
194 | 0 | return -ais_turn_rate(-rate); |
195 | 0 | } |
196 | 0 | return (int)(4.733 * sqrt(rate * RAD_2_DEG * .0001 * 60.0)); |
197 | 0 | } |
198 | | |
199 | | |
200 | | static double ais_direction(unsigned int val, double scale) |
201 | 0 | { |
202 | 0 | if ((0xffff == val) && |
203 | 0 | (1.0 == scale)) { |
204 | 0 | return 511.0; |
205 | 0 | } |
206 | 0 | return val * RAD_2_DEG * 0.0001 * scale; |
207 | 0 | } |
208 | | |
209 | | |
210 | | /* |
211 | | * PGN 59392: ISO Acknowledgment |
212 | | */ |
213 | | static gps_mask_t hnd_059392(unsigned char *bu, int len, PGN *pgn, |
214 | | struct gps_device_t *session) |
215 | 0 | { |
216 | 0 | print_data(session->context, bu, len, pgn); |
217 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
218 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
219 | 0 | return 0; |
220 | 0 | } |
221 | | |
222 | | |
223 | | /* |
224 | | * PGN 60928: ISO Address Claim |
225 | | */ |
226 | | static gps_mask_t hnd_060928(unsigned char *bu, int len, PGN *pgn, |
227 | | struct gps_device_t *session) |
228 | 0 | { |
229 | 0 | print_data(session->context, bu, len, pgn); |
230 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
231 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
232 | 0 | return 0; |
233 | 0 | } |
234 | | |
235 | | |
236 | | /* |
237 | | * PGN 126208: NMEA Command/Request/Acknowledge |
238 | | */ |
239 | | static gps_mask_t hnd_126208(unsigned char *bu, int len, PGN *pgn, |
240 | | struct gps_device_t *session) |
241 | 0 | { |
242 | 0 | print_data(session->context, bu, len, pgn); |
243 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
244 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
245 | 0 | return 0; |
246 | 0 | } |
247 | | |
248 | | |
249 | | /* |
250 | | * PGN 126464: ISO Transmit/Receive PGN List |
251 | | */ |
252 | | static gps_mask_t hnd_126464(unsigned char *bu, int len, PGN *pgn, |
253 | | struct gps_device_t *session) |
254 | 0 | { |
255 | 0 | print_data(session->context, bu, len, pgn); |
256 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
257 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
258 | 0 | return 0; |
259 | 0 | } |
260 | | |
261 | | |
262 | | /* |
263 | | * PGN 126996: ISO Product Information |
264 | | */ |
265 | | static gps_mask_t hnd_126996(unsigned char *bu, int len, PGN *pgn, |
266 | | struct gps_device_t *session) |
267 | 0 | { |
268 | 0 | print_data(session->context, bu, len, pgn); |
269 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
270 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
271 | 0 | return 0; |
272 | 0 | } |
273 | | |
274 | | |
275 | | /* |
276 | | * PGN 127258: GNSS Magnetic Variation |
277 | | * |
278 | | * 1 Sequence ID |
279 | | * 2 Variation Source |
280 | | * 3 Reserved Bits |
281 | | * 4 Age of Service (Date) |
282 | | * 5 Variation |
283 | | * 6 Reserved B |
284 | | */ |
285 | | static gps_mask_t hnd_127258(unsigned char *bu, int len, PGN *pgn, |
286 | | struct gps_device_t *session) |
287 | 0 | { |
288 | 0 | print_data(session->context, bu, len, pgn); |
289 | | // FIXME? Get magnetic variation |
290 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
291 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
292 | 0 | return 0; |
293 | 0 | } |
294 | | |
295 | | |
296 | | /* |
297 | | * PGN 129025: GNSS Position Rapid Update |
298 | | */ |
299 | | static gps_mask_t hnd_129025(unsigned char *bu, int len, PGN *pgn, |
300 | | struct gps_device_t *session) |
301 | 0 | { |
302 | 0 | print_data(session->context, bu, len, pgn); |
303 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
304 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
305 | |
|
306 | 0 | session->newdata.latitude = getles32(bu, 0) * 1e-7; |
307 | 0 | session->newdata.longitude = getles32(bu, 4) * 1e-7; |
308 | |
|
309 | 0 | return LATLON_SET | get_mode(session); |
310 | 0 | } |
311 | | |
312 | | |
313 | | /* |
314 | | * PGN 129026: GNSS COG and SOG Rapid Update |
315 | | */ |
316 | | static gps_mask_t hnd_129026(unsigned char *bu, int len, PGN *pgn, |
317 | | struct gps_device_t *session) |
318 | 0 | { |
319 | 0 | print_data(session->context, bu, len, pgn); |
320 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
321 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
322 | |
|
323 | 0 | session->driver.nmea2000.sid[0] = bu[0]; |
324 | |
|
325 | 0 | session->newdata.track = getleu16(bu, 2) * 1e-4 * RAD_2_DEG; |
326 | 0 | session->newdata.speed = getleu16(bu, 4) * 1e-2; |
327 | |
|
328 | 0 | return SPEED_SET | TRACK_SET | get_mode(session); |
329 | 0 | } |
330 | | |
331 | | |
332 | | /* |
333 | | * PGN: 126992 / 00370020 / 1F010 - 8 - System Time |
334 | | * |
335 | | * Field #1: SID |
336 | | * Bits: 8 |
337 | | * Signed: false |
338 | | * Field #2: Source |
339 | | * Bits: 4 |
340 | | * Type: Lookup table |
341 | | * Signed: false |
342 | | * Lookup: 0=GPS |
343 | | * Lookup: 1=GLONASS |
344 | | * Lookup: 2=Radio Station |
345 | | * Lookup: 3=Local Cesium clock |
346 | | * Lookup: 4=Local Rubidium clock |
347 | | * Lookup: 5=Local Crystal clock |
348 | | * Field #3: Reserved - Reserved |
349 | | * Bits: 4 |
350 | | * Type: Binary data |
351 | | * Signed: false |
352 | | * Field #4: Date - Days since January 1, 1970 |
353 | | * Bits: 16 |
354 | | * Units: days |
355 | | * Type: Date |
356 | | * Resolution: 1 |
357 | | * Signed: false |
358 | | * Field #5: Time - Seconds since midnight |
359 | | * Bits: 32 |
360 | | * Units: s |
361 | | * Type: Time |
362 | | * Resolution: 0.0001 |
363 | | * Signed: false |
364 | | * |
365 | | */ |
366 | | static gps_mask_t hnd_126992(unsigned char *bu, int len, PGN *pgn, |
367 | | struct gps_device_t *session) |
368 | 0 | { |
369 | | // uint8_t sid; |
370 | | // uint8_t source; |
371 | 0 | uint64_t usecs; // time in us |
372 | |
|
373 | 0 | print_data(session->context, bu, len, pgn); |
374 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
375 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
376 | | |
377 | | // sid = bu[0]; |
378 | | // source = bu[1] & 0x0f; |
379 | |
|
380 | 0 | usecs = getleu32(bu, 4) * (uint64_t)100; |
381 | 0 | USTOTS(&session->newdata.time, usecs); |
382 | 0 | session->newdata.time.tv_sec += (time_t)(getleu16(bu, 2) * 24 * 60 * 60); |
383 | |
|
384 | 0 | return TIME_SET | get_mode(session); |
385 | 0 | } |
386 | | |
387 | | |
388 | | static const int mode_tab[] = {MODE_NO_FIX, MODE_2D, MODE_3D, MODE_NO_FIX, |
389 | | MODE_NO_FIX, MODE_NO_FIX, MODE_NO_FIX, |
390 | | MODE_NO_FIX}; |
391 | | |
392 | | /* |
393 | | * PGN 129539: GNSS DOPs |
394 | | */ |
395 | | static gps_mask_t hnd_129539(unsigned char *bu, int len, PGN *pgn, |
396 | | struct gps_device_t *session) |
397 | 0 | { |
398 | 0 | gps_mask_t mask; |
399 | 0 | unsigned int req_mode; |
400 | 0 | unsigned int act_mode; |
401 | |
|
402 | 0 | print_data(session->context, bu, len, pgn); |
403 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
404 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
405 | |
|
406 | 0 | mask = 0; |
407 | 0 | session->driver.nmea2000.sid[1] = bu[0]; |
408 | |
|
409 | 0 | session->driver.nmea2000.mode_valid |= 1; |
410 | |
|
411 | 0 | req_mode = (unsigned int)((bu[1] >> 0) & 0x07); |
412 | 0 | act_mode = (unsigned int)((bu[1] >> 3) & 0x07); |
413 | | |
414 | | /* This is a workaround for some GARMIN plotter, |
415 | | * actual mode auto makes no sense for me! */ |
416 | 0 | if ((3 == act_mode) && |
417 | 0 | (3 != req_mode)) { |
418 | 0 | act_mode = req_mode; |
419 | 0 | } |
420 | |
|
421 | 0 | session->driver.nmea2000.mode = mode_tab[act_mode]; |
422 | |
|
423 | 0 | session->gpsdata.dop.hdop = getleu16(bu, 2) * 1e-2; |
424 | 0 | session->gpsdata.dop.vdop = getleu16(bu, 4) * 1e-2; |
425 | 0 | session->gpsdata.dop.tdop = getleu16(bu, 6) * 1e-2; |
426 | 0 | mask |= DOP_SET; |
427 | |
|
428 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
429 | 0 | "pgn %6d(%3d): sid:%02x hdop:%5.2f vdop:%5.2f tdop:%5.2f\n", |
430 | 0 | pgn->pgn, |
431 | 0 | session->driver.nmea2000.unit, |
432 | 0 | session->driver.nmea2000.sid[1], |
433 | 0 | session->gpsdata.dop.hdop, |
434 | 0 | session->gpsdata.dop.vdop, |
435 | 0 | session->gpsdata.dop.tdop); |
436 | |
|
437 | 0 | return mask | get_mode(session); |
438 | 0 | } |
439 | | |
440 | | |
441 | | /* |
442 | | * PGN 129540: GNSS Satellites in View |
443 | | */ |
444 | | static gps_mask_t hnd_129540(unsigned char *bu, int len, PGN *pgn, |
445 | | struct gps_device_t *session) |
446 | 0 | { |
447 | 0 | int l1; |
448 | 0 | int expected_len; |
449 | |
|
450 | 0 | print_data(session->context, bu, len, pgn); |
451 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
452 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
453 | |
|
454 | 0 | session->driver.nmea2000.sid[2] = bu[0]; |
455 | 0 | session->gpsdata.satellites_visible = (int)bu[2]; |
456 | 0 | if (MAXCHANNELS <= session->gpsdata.satellites_visible) { |
457 | | // Handle a CVE for overrunning skyview[] |
458 | 0 | GPSD_LOG(LOG_WARN, &session->context->errout, |
459 | 0 | "pgn %6d(%3d): Too many sats %d\n", |
460 | 0 | pgn->pgn, session->driver.nmea2000.unit, |
461 | 0 | session->gpsdata.satellites_visible); |
462 | 0 | session->gpsdata.satellites_visible = MAXCHANNELS; |
463 | 0 | } |
464 | 0 | expected_len = 3 + (12 * session->gpsdata.satellites_visible); |
465 | 0 | if (len != expected_len) { |
466 | 0 | GPSD_LOG(LOG_WARN, &session->context->errout, |
467 | 0 | "pgn %6d(%3d): wrong length %d s/b %d\n", |
468 | 0 | pgn->pgn, session->driver.nmea2000.unit, |
469 | 0 | len, expected_len); |
470 | 0 | return 0; |
471 | 0 | } |
472 | | |
473 | 0 | memset(session->gpsdata.skyview, '\0', sizeof(session->gpsdata.skyview)); |
474 | 0 | for (l1 = 0; l1 < session->gpsdata.satellites_visible; l1++) { |
475 | 0 | int offset = 3 + (12 * l1); |
476 | 0 | double elev = getles16(bu, offset + 1) * 1e-4 * RAD_2_DEG; |
477 | 0 | double azi = getleu16(bu, offset + 3) * 1e-4 * RAD_2_DEG; |
478 | 0 | double snr = getles16(bu, offset + 5) * 1e-2; |
479 | |
|
480 | 0 | int svt = (int)(bu[offset + 11] & 0x0f); |
481 | |
|
482 | 0 | session->gpsdata.skyview[l1].elevation = elev; |
483 | 0 | session->gpsdata.skyview[l1].azimuth = azi; |
484 | 0 | session->gpsdata.skyview[l1].ss = snr; |
485 | 0 | session->gpsdata.skyview[l1].PRN = (int16_t)bu[offset]; |
486 | 0 | session->gpsdata.skyview[l1].used = false; |
487 | 0 | if ((2 == svt) || |
488 | 0 | (5 == svt)) { |
489 | 0 | session->gpsdata.skyview[l1].used = true; |
490 | 0 | } |
491 | 0 | } |
492 | 0 | session->driver.nmea2000.mode_valid |= 2; |
493 | 0 | return SATELLITE_SET | USED_IS; |
494 | 0 | } |
495 | | |
496 | | |
497 | | /* |
498 | | * PGN: 129029 / 00374005 / 1F805 - 51 - GNSS Position Data |
499 | | * |
500 | | * The last 3 fields repeat until the data is exhausted. |
501 | | * |
502 | | * Field #1: SID |
503 | | * Bits: 8 |
504 | | * Signed: false |
505 | | * Field #2: Date - Days since January 1, 1970 |
506 | | * Bits: 16 |
507 | | * Units: days |
508 | | * Type: Date |
509 | | * Resolution: 1 |
510 | | * Signed: false |
511 | | * Field #3: Time - Seconds since midnight |
512 | | * Bits: 32 |
513 | | * Units: s |
514 | | * Type: Time |
515 | | * Resolution: 0.0001 |
516 | | * Signed: false |
517 | | * Field #4: Latitude |
518 | | * Bits: 64 |
519 | | * Units: deg |
520 | | * Type: Latitude |
521 | | * Resolution: 0.0000000000000001 |
522 | | * Signed: true |
523 | | * Field #5: Longitude |
524 | | * Bits: 64 |
525 | | * Units: deg |
526 | | * Type: Longitude |
527 | | * Resolution: 0.0000000000000001 |
528 | | * Signed: true |
529 | | * Field #6: Altitude - Altitude referenced to WGS-84 |
530 | | * Bits: 64 |
531 | | * Units: m |
532 | | * Resolution: 1e-06 |
533 | | * Signed: true |
534 | | * Field #7: GNSS type |
535 | | * Bits: 4 |
536 | | * Type: Lookup table |
537 | | * Signed: false |
538 | | * Lookup: 0=GPS |
539 | | * Lookup: 1=GLONASS |
540 | | * Lookup: 2=GPS+GLONASS |
541 | | * Lookup: 3=GPS+SBAS/WAAS |
542 | | * Lookup: 4=GPS+SBAS/WAAS+GLONASS |
543 | | * Lookup: 5=Chayka |
544 | | * Lookup: 6=integrated |
545 | | * Lookup: 7=surveyed |
546 | | * Lookup: 8=Galileo |
547 | | * Field #8: Method |
548 | | * Bits: 4 |
549 | | * Type: Lookup table |
550 | | * Signed: false |
551 | | * Lookup: 0=no GNSS |
552 | | * Lookup: 1=GNSS fix |
553 | | * Lookup: 2=DGNSS fix |
554 | | * Lookup: 3=Precise GNSS |
555 | | * Lookup: 4=RTK Fixed Integer |
556 | | * Lookup: 5=RTK float |
557 | | * Lookup: 6=Estimated (DR) mode |
558 | | * Lookup: 7=Manual Input |
559 | | * Lookup: 8=Simulate mode |
560 | | * Field #9: Integrity |
561 | | * Bits: 2 |
562 | | * Type: Lookup table |
563 | | * Signed: false |
564 | | * Lookup: 0=No integrity checking |
565 | | * Lookup: 1=Safe |
566 | | * Lookup: 2=Caution |
567 | | * Field #10: Reserved - Reserved |
568 | | * Bits: 6 |
569 | | * Type: Binary data |
570 | | * Signed: false |
571 | | * Field #11: Number of SVs - Number of satellites used in solution |
572 | | * Bits: 8 |
573 | | * Signed: false |
574 | | * Field #12: HDOP - Horizontal dilution of precision |
575 | | * Bits: 16 |
576 | | * Resolution: 0.01 |
577 | | * Signed: true |
578 | | * Field #13: PDOP - Probable dilution of precision |
579 | | * Bits: 16 |
580 | | * Resolution: 0.01 |
581 | | * Signed: true |
582 | | * Field #14: Geoidal Separation - Geoidal Separation |
583 | | * Bits: 32 |
584 | | * Units: m |
585 | | * Resolution: 0.01 |
586 | | * Signed: true |
587 | | * Field #15: Reference Stations - Number of reference stations |
588 | | * Bits: 8 |
589 | | * Signed: false |
590 | | * Field #16: Reference Station Type |
591 | | * Bits: 4 |
592 | | * Type: Lookup table |
593 | | * Signed: false |
594 | | * Lookup: 0=GPS |
595 | | * Lookup: 1=GLONASS |
596 | | * Lookup: 2=GPS+GLONASS |
597 | | * Lookup: 3=GPS+SBAS/WAAS |
598 | | * Lookup: 4=GPS+SBAS/WAAS+GLONASS |
599 | | * Lookup: 5=Chayka |
600 | | * Lookup: 6=integrated |
601 | | * Lookup: 7=surveyed |
602 | | * Lookup: 8=Galileo |
603 | | * Field #17: Reference Station ID |
604 | | * Bits: 12 |
605 | | * Units: |
606 | | * Signed: false |
607 | | * Field #18: Age of DGNSS Corrections |
608 | | * Bits: 16 |
609 | | * Units: s |
610 | | * Resolution: 0.01 |
611 | | * Signed: false |
612 | | * |
613 | | */ |
614 | | static gps_mask_t hnd_129029(unsigned char *bu, int len, PGN *pgn, |
615 | | struct gps_device_t *session) |
616 | 0 | { |
617 | 0 | gps_mask_t mask; |
618 | 0 | uint64_t usecs; // time in us |
619 | |
|
620 | 0 | print_data(session->context, bu, len, pgn); |
621 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
622 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
623 | |
|
624 | 0 | mask = 0; |
625 | 0 | session->driver.nmea2000.sid[3] = bu[0]; |
626 | | |
627 | | // field 3 is time in 0.1 ms |
628 | 0 | usecs = getleu32(bu, 3) * (uint64_t)100; |
629 | 0 | USTOTS(&session->newdata.time, usecs); |
630 | | // add in the date from field 2 |
631 | 0 | session->newdata.time.tv_sec += (time_t)(getleu16(bu,1) * 24 * 60 * 60); |
632 | 0 | mask |= TIME_SET; |
633 | |
|
634 | 0 | session->newdata.latitude = getles64(bu, 7) * 1e-16; |
635 | 0 | session->newdata.longitude = getles64(bu, 15) * 1e-16; |
636 | 0 | mask |= LATLON_SET; |
637 | |
|
638 | 0 | session->newdata.altHAE = getles64(bu, 23) * 1e-6; |
639 | 0 | mask |= ALTITUDE_SET; |
640 | | |
641 | | // printf("mode %x %x\n", (bu[31] >> 4) & 0x0f, bu[31]); |
642 | 0 | switch ((bu[31] >> 4) & 0x0f) { |
643 | 0 | case 0: |
644 | 0 | session->newdata.status = STATUS_UNK; |
645 | 0 | break; |
646 | 0 | case 1: |
647 | 0 | session->newdata.status = STATUS_GPS; |
648 | 0 | break; |
649 | 0 | case 2: |
650 | 0 | session->newdata.status = STATUS_DGPS; |
651 | 0 | break; |
652 | 0 | case 3: |
653 | 0 | session->newdata.status = STATUS_PPS_FIX; |
654 | 0 | break; |
655 | 0 | case 4: |
656 | 0 | session->newdata.status = STATUS_RTK_FIX; |
657 | 0 | break; |
658 | 0 | case 5: |
659 | 0 | session->newdata.status = STATUS_RTK_FLT; |
660 | 0 | break; |
661 | 0 | case 6: |
662 | 0 | session->newdata.status = STATUS_DR; |
663 | 0 | break; |
664 | 0 | case 7: |
665 | 0 | session->newdata.status = STATUS_TIME; |
666 | 0 | break; |
667 | 0 | case 8: |
668 | 0 | session->newdata.status = STATUS_SIM; |
669 | 0 | break; |
670 | 0 | default: |
671 | 0 | session->newdata.status = STATUS_UNK; |
672 | 0 | break; |
673 | 0 | } |
674 | 0 | mask |= STATUS_SET; |
675 | |
|
676 | 0 | session->newdata.geoid_sep = getles32(bu, 38) / 100.0; |
677 | |
|
678 | 0 | session->gpsdata.satellites_used = (int)bu[33]; |
679 | |
|
680 | 0 | session->gpsdata.dop.hdop = getleu16(bu, 34) * 1e-2; |
681 | 0 | session->gpsdata.dop.pdop = getleu16(bu, 36) * 1e-2; |
682 | 0 | mask |= DOP_SET; |
683 | |
|
684 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
685 | 0 | "pgn %6d(%3d): sid:%02x hdop:%5.2f pdop:%5.2f\n", |
686 | 0 | pgn->pgn, |
687 | 0 | session->driver.nmea2000.unit, |
688 | 0 | session->driver.nmea2000.sid[1], |
689 | 0 | session->gpsdata.dop.hdop, |
690 | 0 | session->gpsdata.dop.pdop); |
691 | 0 | return mask | get_mode(session); |
692 | 0 | } |
693 | | |
694 | | |
695 | | /* |
696 | | * PGN 129038: AIS Class A Position Report |
697 | | */ |
698 | | static gps_mask_t hnd_129038(unsigned char *bu, int len, PGN *pgn, |
699 | | struct gps_device_t *session) |
700 | 0 | { |
701 | 0 | struct ais_t *ais; |
702 | |
|
703 | 0 | ais = &session->gpsdata.ais; |
704 | 0 | print_data(session->context, bu, len, pgn); |
705 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
706 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
707 | |
|
708 | 0 | if (0 != decode_ais_header(session->context, bu, len, ais, 0xffffffffU)) { |
709 | 0 | ais->type1.lon = (int)scale_int(getles32(bu, 5), |
710 | 0 | (int64_t)(SHIFT32 *.06L)); |
711 | 0 | ais->type1.lat = (int)scale_int(getles32(bu, 9), |
712 | 0 | (int64_t)(SHIFT32 *.06L)); |
713 | 0 | ais->type1.accuracy = (bool) ((bu[13] >> 0) & 0x01); |
714 | 0 | ais->type1.raim = (bool) ((bu[13] >> 1) & 0x01); |
715 | 0 | ais->type1.second = (unsigned int) ((bu[13] >> 2) & 0x3f); |
716 | 0 | ais->type1.course = (unsigned int)ais_direction( |
717 | 0 | (unsigned int)getleu16(bu, 14), 10.0); |
718 | 0 | ais->type1.speed = (unsigned int)(getleu16(bu, 16) * |
719 | 0 | MPS_TO_KNOTS * 0.01 / 0.1); |
720 | 0 | ais->type1.radio = (unsigned int) (getleu32(bu, 18) & 0x7ffff); |
721 | 0 | ais->type1.heading = |
722 | 0 | (unsigned int)ais_direction((unsigned int)getleu16(bu, 21), 1.0); |
723 | 0 | ais->type1.turn = ais_turn_rate((int)getles16(bu, 23)); |
724 | 0 | ais->type1.status = (unsigned int) ((bu[25] >> 0) & 0x0f); |
725 | 0 | ais->type1.maneuver = 0; // Not transmitted ???? |
726 | 0 | decode_ais_channel_info(bu, len, 163, session); |
727 | |
|
728 | 0 | return ONLINE_SET | AIS_SET; |
729 | 0 | } |
730 | 0 | return 0; |
731 | 0 | } |
732 | | |
733 | | |
734 | | /* |
735 | | * PGN 129039: AIS Class B Position Report |
736 | | */ |
737 | | static gps_mask_t hnd_129039(unsigned char *bu, int len, PGN *pgn, |
738 | | struct gps_device_t *session) |
739 | 0 | { |
740 | 0 | struct ais_t *ais; |
741 | |
|
742 | 0 | ais = &session->gpsdata.ais; |
743 | 0 | print_data(session->context, bu, len, pgn); |
744 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
745 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
746 | |
|
747 | 0 | if (0 != decode_ais_header(session->context, bu, len, ais, 0xffffffffU)) { |
748 | 0 | ais->type18.lon = (int)scale_int(getles32(bu, 5), |
749 | 0 | (int64_t)(SHIFT32 *.06L)); |
750 | 0 | ais->type18.lat = (int)scale_int(getles32(bu, 9), |
751 | 0 | (int64_t)(SHIFT32 *.06L)); |
752 | 0 | ais->type18.accuracy = (bool) ((bu[13] >> 0) & 0x01); |
753 | 0 | ais->type18.raim = (bool) ((bu[13] >> 1) & 0x01); |
754 | 0 | ais->type18.second = (unsigned int) ((bu[13] >> 2) & 0x3f); |
755 | 0 | ais->type18.course = |
756 | 0 | (unsigned int)ais_direction((unsigned int) getleu16(bu, 14), 10.0); |
757 | 0 | ais->type18.speed = (unsigned int)(getleu16(bu, 16) * |
758 | 0 | MPS_TO_KNOTS * 0.01 / 0.1); |
759 | 0 | ais->type18.radio = (unsigned int) (getleu32(bu, 18) & 0x7ffff); |
760 | 0 | ais->type18.heading = |
761 | 0 | (unsigned int)ais_direction((unsigned int) getleu16(bu, 21), 1.0); |
762 | 0 | ais->type18.reserved = 0; |
763 | 0 | ais->type18.regional = (unsigned int) ((bu[24] >> 0) & 0x03); |
764 | 0 | ais->type18.cs = (bool) ((bu[24] >> 2) & 0x01); |
765 | 0 | ais->type18.display = (bool) ((bu[24] >> 3) & 0x01); |
766 | 0 | ais->type18.dsc = (bool) ((bu[24] >> 4) & 0x01); |
767 | 0 | ais->type18.band = (bool) ((bu[24] >> 5) & 0x01); |
768 | 0 | ais->type18.msg22 = (bool) ((bu[24] >> 6) & 0x01); |
769 | 0 | ais->type18.assigned = (bool) ((bu[24] >> 7) & 0x01); |
770 | 0 | decode_ais_channel_info(bu, len, 163, session); |
771 | |
|
772 | 0 | return ONLINE_SET | AIS_SET; |
773 | 0 | } |
774 | 0 | return 0; |
775 | 0 | } |
776 | | |
777 | | |
778 | | /* |
779 | | * PGN 129040: AIS Class B Extended Position Report |
780 | | * |
781 | | * No test case for this message at the moment |
782 | | */ |
783 | | static gps_mask_t hnd_129040(unsigned char *bu, int len, PGN *pgn, |
784 | | struct gps_device_t *session) |
785 | 0 | { |
786 | 0 | struct ais_t *ais; |
787 | |
|
788 | 0 | ais = &session->gpsdata.ais; |
789 | 0 | print_data(session->context, bu, len, pgn); |
790 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
791 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
792 | |
|
793 | 0 | if (0 != decode_ais_header(session->context, bu, len, ais, 0xffffffffU)) { |
794 | 0 | uint16_t length, beam, to_bow, to_starboard; |
795 | 0 | int l; |
796 | |
|
797 | 0 | ais->type19.lon = (int)scale_int(getles32(bu, 5), |
798 | 0 | (int64_t)(SHIFT32 *.06L)); |
799 | 0 | ais->type19.lat = (int)scale_int(getles32(bu, 9), |
800 | 0 | (int64_t)(SHIFT32 *.06L)); |
801 | 0 | ais->type19.accuracy = (bool) ((bu[13] >> 0) & 0x01); |
802 | 0 | ais->type19.raim = (bool) ((bu[13] >> 1) & 0x01); |
803 | 0 | ais->type19.second = (unsigned int) ((bu[13] >> 2) & 0x3f); |
804 | 0 | ais->type19.course = |
805 | 0 | (unsigned int)ais_direction((unsigned int)getleu16(bu, 14), 10.0); |
806 | 0 | ais->type19.speed = |
807 | 0 | (unsigned int)(getleu16(bu, 16) * MPS_TO_KNOTS * 0.01 / 0.1); |
808 | 0 | ais->type19.reserved = (unsigned int) ((bu[18] >> 0) & 0xff); |
809 | 0 | ais->type19.regional = (unsigned int) ((bu[19] >> 0) & 0x0f); |
810 | 0 | ais->type19.shiptype = (unsigned int) ((bu[20] >> 0) & 0xff); |
811 | 0 | ais->type19.heading = |
812 | 0 | (unsigned int) ais_direction((unsigned int) getleu16(bu, 21), 1.0); |
813 | 0 | length = getleu16(bu, 24); |
814 | 0 | beam = getleu16(bu, 26); |
815 | 0 | to_starboard = getleu16(bu, 28); |
816 | 0 | to_bow = getleu16(bu, 30); |
817 | 0 | if ((length == 0xffff) || (to_bow == 0xffff)) { |
818 | 0 | length = 0; |
819 | 0 | to_bow = 0; |
820 | 0 | } |
821 | 0 | if ((beam == 0xffff) || (to_starboard == 0xffff)) { |
822 | 0 | beam = 0; |
823 | 0 | to_starboard = 0; |
824 | 0 | } |
825 | 0 | ais->type19.to_bow = (unsigned int) (to_bow/10); |
826 | 0 | ais->type19.to_stern = (unsigned int) ((length-to_bow)/10); |
827 | 0 | ais->type19.to_port = (unsigned int) ((beam-to_starboard)/10); |
828 | 0 | ais->type19.to_starboard = (unsigned int) (to_starboard/10); |
829 | 0 | ais->type19.epfd = (unsigned int) ((bu[23] >> 4) & 0x0f); |
830 | 0 | ais->type19.dte = (unsigned int) ((bu[52] >> 0) & 0x01); |
831 | 0 | ais->type19.assigned = (bool) ((bu[52] >> 1) & 0x01); |
832 | 0 | for (l = 0; l < AIS_SHIPNAME_MAXLEN; l++) { |
833 | 0 | ais->type19.shipname[l] = (char)bu[32+l]; |
834 | 0 | } |
835 | 0 | ais->type19.shipname[AIS_SHIPNAME_MAXLEN] = (char) 0; |
836 | 0 | decode_ais_channel_info(bu, len, 422, session); |
837 | |
|
838 | 0 | return ONLINE_SET | AIS_SET; |
839 | 0 | } |
840 | 0 | return 0; |
841 | 0 | } |
842 | | |
843 | | |
844 | | /* |
845 | | * PGN 129793: AIS UTC and Date Report |
846 | | */ |
847 | | static gps_mask_t hnd_129793(unsigned char *bu, int len, PGN *pgn, |
848 | | struct gps_device_t *session) |
849 | 0 | { |
850 | 0 | struct ais_t *ais; |
851 | |
|
852 | 0 | ais = &session->gpsdata.ais; |
853 | 0 | print_data(session->context, bu, len, pgn); |
854 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
855 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
856 | |
|
857 | 0 | if (0 != decode_ais_header(session->context, bu, len, ais, 0xffffffffU)) { |
858 | 0 | uint32_t time; |
859 | 0 | uint32_t date; |
860 | 0 | time_t date1; |
861 | 0 | struct tm date2; |
862 | |
|
863 | 0 | ais->type4.lon = (int)scale_int(getles32(bu, 5), |
864 | 0 | (int64_t)(SHIFT32 *.06L)); |
865 | 0 | ais->type4.lat = (int)scale_int(getles32(bu, 9), |
866 | 0 | (int64_t)(SHIFT32 *.06L)); |
867 | 0 | ais->type4.accuracy = (bool) ((bu[13] >> 0) & 0x01); |
868 | 0 | ais->type4.raim = (bool) ((bu[13] >> 1) & 0x01); |
869 | |
|
870 | 0 | time = getleu32(bu, 14); |
871 | 0 | if (0xffffffff != time) { |
872 | 0 | time = time / 10000; |
873 | 0 | ais->type4.second = time % 60; time = time / 60; |
874 | 0 | ais->type4.minute = time % 60; time = time / 60; |
875 | 0 | ais->type4.hour = time % 24; |
876 | 0 | } else { |
877 | 0 | ais->type4.second = AIS_SECOND_NOT_AVAILABLE; |
878 | 0 | ais->type4.minute = AIS_MINUTE_NOT_AVAILABLE; |
879 | 0 | ais->type4.hour = AIS_HOUR_NOT_AVAILABLE; |
880 | 0 | } |
881 | |
|
882 | 0 | ais->type4.radio = (unsigned int) (getleu32(bu, 18) & 0x7ffff); |
883 | |
|
884 | 0 | date = getleu16(bu, 21); |
885 | 0 | if (0xffff != date) { |
886 | 0 | date1 = (time_t)date * (24L *60L *60L); |
887 | 0 | (void) gmtime_r(&date1, &date2); |
888 | 0 | ais->type4.year = (unsigned int) (date2.tm_year+1900); |
889 | 0 | ais->type4.month = (unsigned int) (date2.tm_mon+1); |
890 | 0 | ais->type4.day = (unsigned int) (date2.tm_mday); |
891 | 0 | } else { |
892 | 0 | ais->type4.day = AIS_DAY_NOT_AVAILABLE; |
893 | 0 | ais->type4.month = AIS_MONTH_NOT_AVAILABLE; |
894 | 0 | ais->type4.year = AIS_YEAR_NOT_AVAILABLE; |
895 | 0 | } |
896 | |
|
897 | 0 | ais->type4.epfd = (unsigned int) ((bu[23] >> 4) & 0x0f); |
898 | |
|
899 | 0 | decode_ais_channel_info(bu, len, 163, session); |
900 | |
|
901 | 0 | return ONLINE_SET | AIS_SET; |
902 | 0 | } |
903 | 0 | return 0; |
904 | 0 | } |
905 | | |
906 | | |
907 | | /* |
908 | | * PGN 129794: AIS Class A Static and Voyage Related Data |
909 | | */ |
910 | | static gps_mask_t hnd_129794(unsigned char *bu, int len, PGN *pgn, |
911 | | struct gps_device_t *session) |
912 | 0 | { |
913 | 0 | struct ais_t *ais; |
914 | |
|
915 | 0 | ais = &session->gpsdata.ais; |
916 | 0 | print_data(session->context, bu, len, pgn); |
917 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
918 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
919 | |
|
920 | 0 | if (0 != decode_ais_header(session->context, bu, len, ais, 0xffffffffU)) { |
921 | 0 | uint16_t length, beam, to_bow, to_starboard, date; |
922 | 0 | int l; |
923 | 0 | uint32_t time; |
924 | 0 | time_t date1; |
925 | 0 | struct tm date2; |
926 | 0 | int cpy_stop; |
927 | |
|
928 | 0 | ais->type5.ais_version = (unsigned int) ((bu[73] >> 0) & 0x03); |
929 | 0 | ais->type5.imo = (unsigned int) getleu32(bu, 5); |
930 | 0 | if (0xffffffffU == ais->type5.imo) { |
931 | 0 | ais->type5.imo = 0; |
932 | 0 | } |
933 | 0 | ais->type5.shiptype = (unsigned int) ((bu[36] >> 0) & 0xff); |
934 | 0 | length = getleu16(bu, 37); |
935 | 0 | beam = getleu16(bu, 39); |
936 | 0 | to_starboard = getleu16(bu, 41); |
937 | 0 | to_bow = getleu16(bu, 43); |
938 | 0 | if ((0xffff == length) || |
939 | 0 | (0xffff == to_bow)) { |
940 | 0 | length = 0; |
941 | 0 | to_bow = 0; |
942 | 0 | } |
943 | 0 | if ((0xffff == beam) || |
944 | 0 | (0xffff == to_starboard)) { |
945 | 0 | beam = 0; |
946 | 0 | to_starboard = 0; |
947 | 0 | } |
948 | 0 | ais->type5.to_bow = (unsigned int) (to_bow/10); |
949 | 0 | ais->type5.to_stern = (unsigned int) ((length-to_bow)/10); |
950 | 0 | ais->type5.to_port = (unsigned int) ((beam-to_starboard)/10); |
951 | 0 | ais->type5.to_starboard = (unsigned int) (to_starboard/10); |
952 | 0 | ais->type5.epfd = (unsigned int) ((bu[73] >> 2) & 0x0f); |
953 | 0 | date = getleu16(bu, 45); |
954 | 0 | time = getleu32(bu, 47); |
955 | 0 | date1 = (time_t) (date*24*60*60); |
956 | 0 | (void) gmtime_r(&date1, &date2); |
957 | 0 | ais->type5.month = (unsigned int) (date2.tm_mon+1); |
958 | 0 | ais->type5.day = (unsigned int) (date2.tm_mday); |
959 | 0 | ais->type5.minute = (unsigned int) (time/(10000*60)); |
960 | 0 | ais->type5.hour = (unsigned int) (ais->type5.minute/60); |
961 | 0 | ais->type5.minute = |
962 | 0 | (unsigned int)(ais->type5.minute-(ais->type5.hour * 60)); |
963 | |
|
964 | 0 | ais->type5.draught = (unsigned int) (getleu16(bu, 51)/10); |
965 | 0 | ais->type5.dte = (unsigned int) ((bu[73] >> 6) & 0x01); |
966 | |
|
967 | 0 | for (l = 0, cpy_stop = 0; l < 7; l++) { |
968 | 0 | char next; |
969 | |
|
970 | 0 | next = (char) bu[9+l]; |
971 | 0 | if ((next < ' ') || |
972 | 0 | (next > 0x7e)) { |
973 | 0 | cpy_stop = 1; |
974 | 0 | } |
975 | 0 | if (cpy_stop == 0) { |
976 | 0 | ais->type5.callsign[l] = next; |
977 | 0 | } else { |
978 | 0 | ais->type5.callsign[l] = 0; |
979 | 0 | } |
980 | 0 | } |
981 | 0 | ais->type5.callsign[7] = (char) 0; |
982 | |
|
983 | 0 | for (l = 0, cpy_stop = 0; l < AIS_SHIPNAME_MAXLEN; l++) { |
984 | 0 | char next; |
985 | |
|
986 | 0 | next = (char) bu[16+l]; |
987 | 0 | if ((next < ' ') || |
988 | 0 | (next > 0x7e)) { |
989 | 0 | cpy_stop = 1; |
990 | 0 | } |
991 | 0 | if (cpy_stop == 0) { |
992 | 0 | ais->type5.shipname[l] = next; |
993 | 0 | } else { |
994 | 0 | ais->type5.shipname[l] = 0; |
995 | 0 | } |
996 | 0 | } |
997 | 0 | ais->type5.shipname[AIS_SHIPNAME_MAXLEN] = (char) 0; |
998 | |
|
999 | 0 | for (l = 0, cpy_stop = 0; l < 20; l++) { |
1000 | 0 | char next; |
1001 | |
|
1002 | 0 | next = (char) bu[53+l]; |
1003 | 0 | if ((next < ' ') || |
1004 | 0 | (next > 0x7e)) { |
1005 | 0 | cpy_stop = 1; |
1006 | 0 | } |
1007 | 0 | if (cpy_stop == 0) { |
1008 | 0 | ais->type5.destination[l] = next; |
1009 | 0 | } else { |
1010 | 0 | ais->type5.destination[l] = 0; |
1011 | 0 | } |
1012 | 0 | } |
1013 | 0 | ais->type5.destination[20] = (char) 0; |
1014 | | #if NMEA2000_DEBUG_AIS |
1015 | | printf("AIS: MMSI: %09u\n", |
1016 | | ais->mmsi); |
1017 | | printf("AIS: name: %-20.20s i:%8u c:%-8.8s b:%6u s:%6u p:%6u" |
1018 | | "s:%6u dr:%4.1f\n", |
1019 | | ais->type5.shipname, |
1020 | | ais->type5.imo, |
1021 | | ais->type5.callsign, |
1022 | | ais->type5.to_bow, |
1023 | | ais->type5.to_stern, |
1024 | | ais->type5.to_port, |
1025 | | ais->type5.to_starboard, |
1026 | | ais->type5.draught/10.0); |
1027 | | printf("AIS: arrival:%-20.20s at %02u-%02u-%04d %02u:%0u\n", |
1028 | | ais->type5.destination, |
1029 | | ais->type5.day, |
1030 | | ais->type5.month, |
1031 | | date2.tm_year+1900, |
1032 | | ais->type5.hour, |
1033 | | ais->type5.minute); |
1034 | | #endif // end of #if NMEA2000_DEBUG_AIS |
1035 | 0 | decode_ais_channel_info(bu, len, 592, session); |
1036 | 0 | return ONLINE_SET | AIS_SET; |
1037 | 0 | } |
1038 | 0 | return 0; |
1039 | 0 | } |
1040 | | |
1041 | | |
1042 | | /* |
1043 | | * PGN 129798: AIS SAR Aircraft Position Report |
1044 | | * |
1045 | | * No test case for this message at the moment |
1046 | | */ |
1047 | | static gps_mask_t hnd_129798(unsigned char *bu, int len, PGN *pgn, |
1048 | | struct gps_device_t *session) |
1049 | 0 | { |
1050 | 0 | struct ais_t *ais; |
1051 | |
|
1052 | 0 | ais = &session->gpsdata.ais; |
1053 | 0 | print_data(session->context, bu, len, pgn); |
1054 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1055 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1056 | |
|
1057 | 0 | if (0 != decode_ais_header(session->context, bu, len, ais, 0xffffffffU)) { |
1058 | 0 | ais->type9.lon = (int)scale_int(getles32(bu, 5), |
1059 | 0 | (int64_t)(SHIFT32 *.06L)); |
1060 | 0 | ais->type9.lat = (int)scale_int(getles32(bu, 9), |
1061 | 0 | (int64_t)(SHIFT32 *.06L)); |
1062 | 0 | ais->type9.accuracy = (bool) ((bu[13] >> 0) & 0x01); |
1063 | 0 | ais->type9.raim = (bool) ((bu[13] >> 1) & 0x01); |
1064 | 0 | ais->type9.second = (unsigned int) ((bu[13] >> 2) & 0x3f); |
1065 | 0 | ais->type9.course = |
1066 | 0 | (unsigned int)ais_direction((unsigned int)getleu16(bu, 14), 10.0); |
1067 | 0 | ais->type9.speed = |
1068 | 0 | (unsigned int)(getleu16(bu, 16) * MPS_TO_KNOTS * 0.01 / 0.1); |
1069 | 0 | ais->type9.radio = (unsigned int) (getleu32(bu, 18) & 0x7ffff); |
1070 | 0 | ais->type9.alt = (unsigned int) (getleu64(bu, 21)/1000000); |
1071 | 0 | ais->type9.regional = (unsigned int) ((bu[29] >> 0) & 0xff); |
1072 | 0 | ais->type9.dte = (unsigned int) ((bu[30] >> 0) & 0x01); |
1073 | | // ais->type9.spare = (bu[30] >> 1) & 0x7f; |
1074 | 0 | ais->type9.assigned = 0; // Not transmitted ???? |
1075 | 0 | decode_ais_channel_info(bu, len, 163, session); |
1076 | |
|
1077 | 0 | return ONLINE_SET | AIS_SET; |
1078 | 0 | } |
1079 | 0 | return 0; |
1080 | 0 | } |
1081 | | |
1082 | | |
1083 | | /* |
1084 | | * PGN 129802: AIS Safety Related Broadcast Message |
1085 | | * |
1086 | | * No test case for this message at the moment |
1087 | | */ |
1088 | | static gps_mask_t hnd_129802(unsigned char *bu, int len, PGN *pgn, |
1089 | | struct gps_device_t *session) |
1090 | 0 | { |
1091 | 0 | struct ais_t *ais; |
1092 | |
|
1093 | 0 | ais = &session->gpsdata.ais; |
1094 | 0 | print_data(session->context, bu, len, pgn); |
1095 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1096 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1097 | |
|
1098 | 0 | if (0 != decode_ais_header(session->context, bu, len, ais, 0x3fffffff)) { |
1099 | 0 | int l; |
1100 | | |
1101 | | // ais->type14.channel = (bu[ 5] >> 0) & 0x1f; |
1102 | 0 | for (l = 0; l < 36; l++) { |
1103 | 0 | ais->type14.text[l] = (char) bu[6+l]; |
1104 | 0 | } |
1105 | 0 | ais->type14.text[36] = (char) 0; |
1106 | 0 | decode_ais_channel_info(bu, len, 40, session); |
1107 | |
|
1108 | 0 | return ONLINE_SET | AIS_SET; |
1109 | 0 | } |
1110 | 0 | return 0; |
1111 | 0 | } |
1112 | | |
1113 | | |
1114 | | /* |
1115 | | * PGN 129809: AIS Class B CS Static Data Report, Part A |
1116 | | */ |
1117 | | static gps_mask_t hnd_129809(unsigned char *bu, int len, PGN *pgn, |
1118 | | struct gps_device_t *session) |
1119 | 0 | { |
1120 | 0 | struct ais_t *ais; |
1121 | |
|
1122 | 0 | ais = &session->gpsdata.ais; |
1123 | 0 | print_data(session->context, bu, len, pgn); |
1124 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1125 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1126 | |
|
1127 | 0 | if (0 != decode_ais_header(session->context, bu, len, ais, 0xffffffffU)) { |
1128 | 0 | int l; |
1129 | 0 | int index = session->driver.aivdm.context[0].type24_queue.index; |
1130 | 0 | struct ais_type24a_t *saveptr = |
1131 | 0 | &session->driver.aivdm.context[0].type24_queue.ships[index]; |
1132 | |
|
1133 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
1134 | 0 | "NMEA2000: AIS message 24A from %09u stashed.\n", |
1135 | 0 | ais->mmsi); |
1136 | |
|
1137 | 0 | for (l = 0; l < AIS_SHIPNAME_MAXLEN; l++) { |
1138 | 0 | ais->type24.shipname[l] = (char) bu[ 5+l]; |
1139 | 0 | saveptr->shipname[l] = (char) bu[ 5+l]; |
1140 | 0 | } |
1141 | 0 | ais->type24.shipname[AIS_SHIPNAME_MAXLEN] = (char) 0; |
1142 | 0 | saveptr->shipname[AIS_SHIPNAME_MAXLEN] = (char) 0; |
1143 | |
|
1144 | 0 | saveptr->mmsi = ais->mmsi; |
1145 | |
|
1146 | 0 | index += 1; |
1147 | 0 | index %= MAX_TYPE24_INTERLEAVE; |
1148 | 0 | session->driver.aivdm.context[0].type24_queue.index = index; |
1149 | |
|
1150 | 0 | decode_ais_channel_info(bu, len, 200, session); |
1151 | |
|
1152 | 0 | ais->type24.part = part_a; |
1153 | 0 | return ONLINE_SET | AIS_SET; |
1154 | 0 | } |
1155 | 0 | return 0; |
1156 | 0 | } |
1157 | | |
1158 | | |
1159 | | /* |
1160 | | * PGN 129810: AIS Class B CS Static Data Report, Part B |
1161 | | */ |
1162 | | static gps_mask_t hnd_129810(unsigned char *bu, int len, PGN *pgn, |
1163 | | struct gps_device_t *session) |
1164 | 0 | { |
1165 | 0 | struct ais_t *ais; |
1166 | |
|
1167 | 0 | ais = &session->gpsdata.ais; |
1168 | 0 | print_data(session->context, bu, len, pgn); |
1169 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1170 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1171 | |
|
1172 | 0 | if (0 != decode_ais_header(session->context, bu, len, ais, 0xffffffffU)) { |
1173 | 0 | int l, i; |
1174 | |
|
1175 | 0 | ais->type24.shiptype = (unsigned int) ((bu[ 5] >> 0) & 0xff); |
1176 | |
|
1177 | 0 | for (l = 0; l < 7; l++) { |
1178 | 0 | ais->type24.vendorid[l] = (char) bu[ 6+l]; |
1179 | 0 | } |
1180 | 0 | ais->type24.vendorid[7] = (char) 0; |
1181 | |
|
1182 | 0 | for (l = 0; l < 7; l++) { |
1183 | 0 | ais->type24.callsign[l] = (char) bu[13+l]; |
1184 | 0 | } |
1185 | 0 | ais->type24.callsign[7] = (char )0; |
1186 | |
|
1187 | 0 | ais->type24.model = 0; |
1188 | 0 | ais->type24.serial = 0; |
1189 | |
|
1190 | 0 | if (AIS_AUXILIARY_MMSI(ais->mmsi)) { |
1191 | 0 | ais->type24.mothership_mmsi = (unsigned int) (getleu32(bu, 28)); |
1192 | 0 | } else { |
1193 | 0 | uint16_t length, beam, to_bow, to_starboard; |
1194 | |
|
1195 | 0 | length = getleu16(bu, 20); |
1196 | 0 | beam = getleu16(bu, 22); |
1197 | 0 | to_starboard = getleu16(bu, 24); |
1198 | 0 | to_bow = getleu16(bu, 26); |
1199 | 0 | if ((length == 0xffff) || (to_bow == 0xffff)) { |
1200 | 0 | length = 0; |
1201 | 0 | to_bow = 0; |
1202 | 0 | } |
1203 | 0 | if ((beam == 0xffff) || (to_starboard == 0xffff)) { |
1204 | 0 | beam = 0; |
1205 | 0 | to_starboard = 0; |
1206 | 0 | } |
1207 | 0 | ais->type24.dim.to_bow = (unsigned int) (to_bow/10); |
1208 | 0 | ais->type24.dim.to_stern = (unsigned int) ((length-to_bow)/10); |
1209 | 0 | ais->type24.dim.to_port = (unsigned int) ((beam-to_starboard)/10); |
1210 | 0 | ais->type24.dim.to_starboard = (unsigned int) (to_starboard/10); |
1211 | 0 | } |
1212 | |
|
1213 | 0 | for (i = 0; i < MAX_TYPE24_INTERLEAVE; i++) { |
1214 | 0 | if (session->driver.aivdm.context[0].type24_queue.ships[i].mmsi == |
1215 | 0 | ais->mmsi) { |
1216 | 0 | for (l = 0; l < AIS_SHIPNAME_MAXLEN; l++) { |
1217 | 0 | ais->type24.shipname[l] = |
1218 | 0 | (char)(session->driver.aivdm.context[0].type24_queue.ships[i].shipname[l]); |
1219 | 0 | } |
1220 | 0 | ais->type24.shipname[AIS_SHIPNAME_MAXLEN] = (char) 0; |
1221 | |
|
1222 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
1223 | 0 | "NMEA2000: AIS 24B from %09u matches a 24A.\n", |
1224 | 0 | ais->mmsi); |
1225 | | // prevent false match if a 24B is repeated |
1226 | 0 | session->driver.aivdm.context[0].type24_queue.ships[i].mmsi = 0; |
1227 | | #if NMEA2000_DEBUG_AIS |
1228 | | printf("AIS: MMSI: %09u\n", ais->mmsi); |
1229 | | printf("AIS: name: %-20.20s v:%-8.8s c:%-8.8s b:%6u " |
1230 | | "s:%6u p:%6u s:%6u\n", |
1231 | | ais->type24.shipname, |
1232 | | ais->type24.vendorid, |
1233 | | ais->type24.callsign, |
1234 | | ais->type24.dim.to_bow, |
1235 | | ais->type24.dim.to_stern, |
1236 | | ais->type24.dim.to_port, |
1237 | | ais->type24.dim.to_starboard); |
1238 | | #endif // of #if NMEA2000_DEBUG_AIS |
1239 | |
|
1240 | 0 | decode_ais_channel_info(bu, len, 264, session); |
1241 | 0 | ais->type24.part = both; |
1242 | 0 | return ONLINE_SET | AIS_SET; |
1243 | 0 | } |
1244 | 0 | } |
1245 | | #if NMEA2000_DEBUG_AIS |
1246 | | printf("AIS: MMSI : %09u\n", ais->mmsi); |
1247 | | printf("AIS: vendor: %-8.8s c:%-8.8s b:%6u s:%6u p:%6u s:%6u\n", |
1248 | | ais->type24.vendorid, |
1249 | | ais->type24.callsign, |
1250 | | ais->type24.dim.to_bow, |
1251 | | ais->type24.dim.to_stern, |
1252 | | ais->type24.dim.to_port, |
1253 | | ais->type24.dim.to_starboard); |
1254 | | #endif // of #if NMEA2000_DEBUG_AIS |
1255 | 0 | decode_ais_channel_info(bu, len, 264, session); |
1256 | 0 | ais->type24.part = part_b; |
1257 | 0 | return ONLINE_SET | AIS_SET; |
1258 | 0 | } |
1259 | 0 | return 0; |
1260 | 0 | } |
1261 | | |
1262 | | |
1263 | | /* |
1264 | | * PGN 127506: PWR DC Detailed Status |
1265 | | */ |
1266 | | static gps_mask_t hnd_127506(unsigned char *bu, int len, PGN *pgn, |
1267 | | struct gps_device_t *session) |
1268 | 0 | { |
1269 | 0 | print_data(session->context, bu, len, pgn); |
1270 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1271 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1272 | 0 | return 0; |
1273 | 0 | } |
1274 | | |
1275 | | |
1276 | | /* |
1277 | | * PGN 127508: PWR Battery Status |
1278 | | */ |
1279 | | static gps_mask_t hnd_127508(unsigned char *bu, int len, PGN *pgn, |
1280 | | struct gps_device_t *session) |
1281 | 0 | { |
1282 | 0 | print_data(session->context, bu, len, pgn); |
1283 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1284 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1285 | 0 | return 0; |
1286 | 0 | } |
1287 | | |
1288 | | |
1289 | | /* |
1290 | | * PGN 127513: PWR Battery Configuration Status |
1291 | | */ |
1292 | | static gps_mask_t hnd_127513(unsigned char *bu, int len, PGN *pgn, |
1293 | | struct gps_device_t *session) |
1294 | 0 | { |
1295 | 0 | print_data(session->context, bu, len, pgn); |
1296 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1297 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1298 | 0 | return 0; |
1299 | 0 | } |
1300 | | |
1301 | | |
1302 | | /* |
1303 | | * PGN 127245: NAV Rudder |
1304 | | */ |
1305 | | static gps_mask_t hnd_127245(unsigned char *bu, int len, PGN *pgn, |
1306 | | struct gps_device_t *session) |
1307 | 0 | { |
1308 | 0 | print_data(session->context, bu, len, pgn); |
1309 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1310 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1311 | 0 | return 0; |
1312 | 0 | } |
1313 | | |
1314 | | |
1315 | | /* |
1316 | | * PGN 127250: NAV Vessel Heading |
1317 | | */ |
1318 | | static gps_mask_t hnd_127250(unsigned char *bu, int len, |
1319 | | PGN *pgn, struct gps_device_t *session) |
1320 | 0 | { |
1321 | 0 | int aux; |
1322 | |
|
1323 | 0 | print_data(session->context, bu, len, pgn); |
1324 | |
|
1325 | 0 | session->gpsdata.attitude.heading = getleu16(bu, 1) * RAD_2_DEG * 0.0001; |
1326 | | // printf("ATT 0:%8.3f\n",session->gpsdata.attitude.heading); |
1327 | 0 | aux = getles16(bu, 3); |
1328 | 0 | if (0x07fff != aux) { |
1329 | 0 | session->gpsdata.attitude.heading += aux * RAD_2_DEG * 0.0001; |
1330 | 0 | } |
1331 | | // printf("ATT 1:%8.3f %6x\n",session->gpsdata.attitude.heading, aux); |
1332 | 0 | aux = getles16(bu, 5); |
1333 | 0 | if (0x07fff != aux) { |
1334 | 0 | session->gpsdata.attitude.heading += aux * RAD_2_DEG * 0.0001; |
1335 | 0 | } |
1336 | | // printf("ATT 2:%8.3f %6x\n",session->gpsdata.attitude.heading, aux); |
1337 | |
|
1338 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1339 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1340 | 0 | return ONLINE_SET | ATTITUDE_SET; |
1341 | 0 | } |
1342 | | |
1343 | | |
1344 | | /* |
1345 | | * PGN 128259: NAV Speed |
1346 | | */ |
1347 | | static gps_mask_t hnd_128259(unsigned char *bu, int len, PGN *pgn, |
1348 | | struct gps_device_t *session) |
1349 | 0 | { |
1350 | 0 | print_data(session->context, bu, len, pgn); |
1351 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1352 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1353 | 0 | return 0; |
1354 | 0 | } |
1355 | | |
1356 | | |
1357 | | /* |
1358 | | * PGN 128267: NAV Water Depth |
1359 | | */ |
1360 | | static gps_mask_t hnd_128267(unsigned char *bu, int len, PGN *pgn, |
1361 | | struct gps_device_t *session) |
1362 | 0 | { |
1363 | 0 | print_data(session->context, bu, len, pgn); |
1364 | |
|
1365 | 0 | session->gpsdata.attitude.depth = getleu32(bu, 1) *.01; |
1366 | |
|
1367 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1368 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1369 | 0 | return ONLINE_SET | ATTITUDE_SET; |
1370 | 0 | } |
1371 | | |
1372 | | |
1373 | | /* |
1374 | | * PGN 128275: NAV Distance Log |
1375 | | */ |
1376 | | static gps_mask_t hnd_128275(unsigned char *bu, int len, PGN *pgn, |
1377 | | struct gps_device_t *session) |
1378 | 0 | { |
1379 | 0 | print_data(session->context, bu, len, pgn); |
1380 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1381 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1382 | 0 | return 0; |
1383 | 0 | } |
1384 | | |
1385 | | |
1386 | | /* |
1387 | | * PGN 129283: NAV Cross Track Error |
1388 | | */ |
1389 | | static gps_mask_t hnd_129283(unsigned char *bu, int len, PGN *pgn, |
1390 | | struct gps_device_t *session) |
1391 | 0 | { |
1392 | 0 | print_data(session->context, bu, len, pgn); |
1393 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1394 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1395 | 0 | return 0; |
1396 | 0 | } |
1397 | | |
1398 | | |
1399 | | /* |
1400 | | * PGN 129284: NAV Navigation Data |
1401 | | */ |
1402 | | static gps_mask_t hnd_129284(unsigned char *bu, int len, PGN *pgn, |
1403 | | struct gps_device_t *session) |
1404 | 0 | { |
1405 | 0 | print_data(session->context, bu, len, pgn); |
1406 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1407 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1408 | 0 | return 0; |
1409 | 0 | } |
1410 | | |
1411 | | |
1412 | | /* |
1413 | | * PGN 129285: NAV Navigation - Route/WP Information |
1414 | | */ |
1415 | | static gps_mask_t hnd_129285(unsigned char *bu, int len, PGN *pgn, |
1416 | | struct gps_device_t *session) |
1417 | 0 | { |
1418 | 0 | print_data(session->context, bu, len, pgn); |
1419 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1420 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1421 | 0 | return 0; |
1422 | 0 | } |
1423 | | |
1424 | | |
1425 | | /* |
1426 | | * PGN 130306: NAV Wind Data |
1427 | | */ |
1428 | | static gps_mask_t hnd_130306(unsigned char *bu, int len, PGN *pgn, |
1429 | | struct gps_device_t *session) |
1430 | 0 | { |
1431 | 0 | print_data(session->context, bu, len, pgn); |
1432 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1433 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1434 | 0 | return 0; |
1435 | 0 | } |
1436 | | |
1437 | | |
1438 | | /* |
1439 | | * PGN 130310: NAV Water Temp., Outside Air Temp., Atmospheric Pressure |
1440 | | */ |
1441 | | static gps_mask_t hnd_130310(unsigned char *bu, int len, PGN *pgn, |
1442 | | struct gps_device_t *session) |
1443 | 0 | { |
1444 | 0 | print_data(session->context, bu, len, pgn); |
1445 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1446 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1447 | 0 | return 0; |
1448 | 0 | } |
1449 | | |
1450 | | |
1451 | | /* |
1452 | | * PGN 130311: NAV Environmental Parameters |
1453 | | */ |
1454 | | static gps_mask_t hnd_130311(unsigned char *bu, int len, PGN *pgn, |
1455 | | struct gps_device_t *session) |
1456 | 0 | { |
1457 | 0 | print_data(session->context, bu, len, pgn); |
1458 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1459 | 0 | "pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit); |
1460 | 0 | return 0; |
1461 | 0 | } |
1462 | | |
1463 | | |
1464 | | static const char msg_059392[] = {"ISO Acknowledgment"}; |
1465 | | static const char msg_060928[] = {"ISO Address Claim"}; |
1466 | | static const char msg_126208[] = {"NMEA Command/Request/Acknowledge"}; |
1467 | | static const char msg_126464[] = {"ISO Transmit/Receive PGN List"}; |
1468 | | static const char msg_126992[] = {"GNSS System Time"}; |
1469 | | static const char msg_126996[] = {"ISO Product Information"}; |
1470 | | |
1471 | | static const char msg_127506[] = {"PWR DC Detailed Status"}; |
1472 | | static const char msg_127508[] = {"PWR Battery Status"}; |
1473 | | static const char msg_127513[] = {"PWR Battery Configuration Status"}; |
1474 | | |
1475 | | static const char msg_127258[] = {"GNSS Magnetic Variation"}; |
1476 | | static const char msg_129025[] = {"GNSS Position Rapid Update"}; |
1477 | | static const char msg_129026[] = {"GNSS COG and SOG Rapid Update"}; |
1478 | | static const char msg_129029[] = {"GNSS Positition Data"}; |
1479 | | static const char msg_129539[] = {"GNSS DOPs"}; |
1480 | | static const char msg_129540[] = {"GNSS Satellites in View"}; |
1481 | | |
1482 | | static const char msg_129038[] = {"AIS Class A Position Report"}; |
1483 | | static const char msg_129039[] = {"AIS Class B Position Report"}; |
1484 | | static const char msg_129040[] = {"AIS Class B Extended Position Report"}; |
1485 | | static const char msg_129793[] = {"AIS UTC and Date report"}; |
1486 | | static const char msg_129794[] = {"AIS Class A Static and Voyage Related Data"}; |
1487 | | static const char msg_129798[] = {"AIS SAR Aircraft Position Report"}; |
1488 | | static const char msg_129802[] = {"AIS Safety Related Broadcast Message"}; |
1489 | | static const char msg_129809[] = {"AIS Class B CS Static Data Report, Part A"}; |
1490 | | static const char msg_129810[] = {"AIS Class B CS Static Data Report, Part B"}; |
1491 | | |
1492 | | static const char msg_127245[] = {"NAV Rudder"}; |
1493 | | static const char msg_127250[] = {"NAV Vessel Heading"}; |
1494 | | static const char msg_128259[] = {"NAV Speed"}; |
1495 | | static const char msg_128267[] = {"NAV Water Depth"}; |
1496 | | static const char msg_128275[] = {"NAV Distance Log"}; |
1497 | | |
1498 | | static const char msg_129283[] = {"NAV Cross Track Error"}; |
1499 | | static const char msg_129284[] = {"NAV Navigation Data"}; |
1500 | | static const char msg_129285[] = {"NAV Navigation - Route/WP Information"}; |
1501 | | |
1502 | | static const char msg_130306[] = {"NAV Wind Data"}; |
1503 | | static const char msg_130310[] = {"NAV Water Temp., Outside Air Temp.," |
1504 | | "Atmospheric Pressure"}; |
1505 | | static const char msg_130311[] = {"NAV Environmental Parameters"}; |
1506 | | |
1507 | | static const char msg_error [] = {"**error**"}; |
1508 | | |
1509 | | static PGN gpspgn[] = {{ 59392, 0, 0, hnd_059392, &msg_059392[0]}, |
1510 | | { 60928, 0, 0, hnd_060928, &msg_060928[0]}, |
1511 | | {126208, 0, 0, hnd_126208, &msg_126208[0]}, |
1512 | | {126464, 1, 0, hnd_126464, &msg_126464[0]}, |
1513 | | {126992, 0, 0, hnd_126992, &msg_126992[0]}, |
1514 | | {126996, 1, 0, hnd_126996, &msg_126996[0]}, |
1515 | | {127258, 0, 0, hnd_127258, &msg_127258[0]}, |
1516 | | {129025, 0, 1, hnd_129025, &msg_129025[0]}, |
1517 | | {129026, 0, 1, hnd_129026, &msg_129026[0]}, |
1518 | | {129029, 1, 1, hnd_129029, &msg_129029[0]}, |
1519 | | {129283, 0, 0, hnd_129283, &msg_129283[0]}, |
1520 | | {129284, 1, 0, hnd_129284, &msg_129284[0]}, |
1521 | | {129285, 1, 0, hnd_129285, &msg_129285[0]}, |
1522 | | {129539, 0, 1, hnd_129539, &msg_129539[0]}, |
1523 | | {129540, 1, 1, hnd_129540, &msg_129540[0]}, |
1524 | | {0 , 0, 0, NULL, &msg_error [0]}}; |
1525 | | |
1526 | | static PGN aispgn[] = {{ 59392, 0, 0, hnd_059392, &msg_059392[0]}, |
1527 | | { 60928, 0, 0, hnd_060928, &msg_060928[0]}, |
1528 | | {126208, 0, 0, hnd_126208, &msg_126208[0]}, |
1529 | | {126464, 1, 0, hnd_126464, &msg_126464[0]}, |
1530 | | {126992, 0, 0, hnd_126992, &msg_126992[0]}, |
1531 | | {126996, 1, 0, hnd_126996, &msg_126996[0]}, |
1532 | | {129038, 1, 2, hnd_129038, &msg_129038[0]}, |
1533 | | {129039, 1, 2, hnd_129039, &msg_129039[0]}, |
1534 | | {129040, 1, 2, hnd_129040, &msg_129040[0]}, |
1535 | | {129793, 1, 2, hnd_129793, &msg_129793[0]}, |
1536 | | {129794, 1, 2, hnd_129794, &msg_129794[0]}, |
1537 | | {129798, 1, 2, hnd_129798, &msg_129798[0]}, |
1538 | | {129802, 1, 2, hnd_129802, &msg_129802[0]}, |
1539 | | {129809, 1, 2, hnd_129809, &msg_129809[0]}, |
1540 | | {129810, 1, 2, hnd_129810, &msg_129810[0]}, |
1541 | | {0 , 0, 0, NULL, &msg_error [0]}}; |
1542 | | |
1543 | | static PGN pwrpgn[] = {{ 59392, 0, 0, hnd_059392, &msg_059392[0]}, |
1544 | | { 60928, 0, 0, hnd_060928, &msg_060928[0]}, |
1545 | | {126208, 0, 0, hnd_126208, &msg_126208[0]}, |
1546 | | {126464, 1, 0, hnd_126464, &msg_126464[0]}, |
1547 | | {126992, 0, 0, hnd_126992, &msg_126992[0]}, |
1548 | | {126996, 1, 0, hnd_126996, &msg_126996[0]}, |
1549 | | {127506, 1, 3, hnd_127506, &msg_127506[0]}, |
1550 | | {127508, 1, 3, hnd_127508, &msg_127508[0]}, |
1551 | | {127513, 1, 3, hnd_127513, &msg_127513[0]}, |
1552 | | {0 , 0, 0, NULL, &msg_error [0]}}; |
1553 | | |
1554 | | static PGN navpgn[] = {{ 59392, 0, 0, hnd_059392, &msg_059392[0]}, |
1555 | | { 60928, 0, 0, hnd_060928, &msg_060928[0]}, |
1556 | | {126208, 0, 0, hnd_126208, &msg_126208[0]}, |
1557 | | {126464, 1, 0, hnd_126464, &msg_126464[0]}, |
1558 | | {126992, 0, 0, hnd_126992, &msg_126992[0]}, |
1559 | | {126996, 1, 0, hnd_126996, &msg_126996[0]}, |
1560 | | {127245, 0, 4, hnd_127245, &msg_127245[0]}, |
1561 | | {127250, 0, 4, hnd_127250, &msg_127250[0]}, |
1562 | | {127258, 0, 0, hnd_127258, &msg_127258[0]}, |
1563 | | {128259, 0, 4, hnd_128259, &msg_128259[0]}, |
1564 | | {128267, 0, 4, hnd_128267, &msg_128267[0]}, |
1565 | | {128275, 1, 4, hnd_128275, &msg_128275[0]}, |
1566 | | {129283, 0, 0, hnd_129283, &msg_129283[0]}, |
1567 | | {129284, 1, 0, hnd_129284, &msg_129284[0]}, |
1568 | | {129285, 1, 0, hnd_129285, &msg_129285[0]}, |
1569 | | {130306, 0, 4, hnd_130306, &msg_130306[0]}, |
1570 | | {130310, 0, 4, hnd_130310, &msg_130310[0]}, |
1571 | | {130311, 0, 4, hnd_130311, &msg_130311[0]}, |
1572 | | {0 , 0, 0, NULL, &msg_error [0]}}; |
1573 | | |
1574 | | |
1575 | | static PGN *search_pgnlist(unsigned int pgn, PGN *pgnlist) |
1576 | 0 | { |
1577 | 0 | int l1; |
1578 | 0 | PGN *work; |
1579 | |
|
1580 | 0 | l1 = 0; |
1581 | 0 | work = NULL; |
1582 | 0 | while (0 != pgnlist[l1].pgn) { |
1583 | 0 | if (pgnlist[l1].pgn == pgn) { |
1584 | 0 | work = &pgnlist[l1]; |
1585 | 0 | break; |
1586 | 0 | } else { |
1587 | 0 | l1 = l1 + 1; |
1588 | 0 | } |
1589 | 0 | } |
1590 | 0 | return work; |
1591 | 0 | } |
1592 | | |
1593 | | static void find_pgn(struct can_frame *frame, struct gps_device_t *session) |
1594 | 0 | { |
1595 | 0 | unsigned int can_net; |
1596 | |
|
1597 | 0 | GPSD_LOG(LOG_RAW, &session->context->errout, |
1598 | 0 | "NMEA2000 find_pgn()\n"); |
1599 | 0 | session->driver.nmea2000.workpgn = NULL; |
1600 | 0 | can_net = session->driver.nmea2000.can_net; |
1601 | 0 | if ((NMEA2000_NETS - 1) < can_net) { |
1602 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1603 | 0 | "NMEA2000 find_pgn: Invalid can network %d.\n", can_net); |
1604 | 0 | return; |
1605 | 0 | } |
1606 | | |
1607 | 0 | if (frame->can_id & CAN_ERR_FLAG) { |
1608 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1609 | 0 | "NMEA2000 CAN_ERR_FLAG set %d.\n", frame->can_id); |
1610 | 0 | return; |
1611 | 0 | } |
1612 | 0 | if (frame->can_id & CAN_EFF_FLAG) { |
1613 | 0 | unsigned int source_prio; |
1614 | 0 | unsigned int daddr; |
1615 | 0 | unsigned int source_pgn; |
1616 | 0 | unsigned int source_unit; |
1617 | |
|
1618 | 0 | #if LOG_FILE |
1619 | 0 | if (logFile != NULL) { |
1620 | 0 | struct timespec msgTime; |
1621 | |
|
1622 | 0 | clock_gettime(CLOCK_REALTIME, &msgTime); |
1623 | 0 | (void)fprintf(logFile, |
1624 | 0 | "(%010lld.%06ld) can0 %08x#", |
1625 | 0 | (long long)msgTime.tv_sec, |
1626 | 0 | msgTime.tv_nsec / 1000, |
1627 | 0 | frame->can_id & 0x1ffffff); |
1628 | 0 | if (0 < (frame->can_dlc & 0x0f)) { |
1629 | 0 | int l1; |
1630 | 0 | for (l1 = 0; l1 < (frame->can_dlc & 0x0f); l1++) { |
1631 | 0 | (void)fprintf(logFile, "%02x", frame->data[l1]); |
1632 | 0 | } |
1633 | 0 | } |
1634 | 0 | (void)fprintf(logFile, "\n"); |
1635 | 0 | } |
1636 | 0 | #endif // of if LOG_FILE |
1637 | 0 | source_unit = frame->can_id & 0x0ff; |
1638 | 0 | if (NMEA2000_UNITS < source_unit) { |
1639 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
1640 | 0 | "NMEA2000 ignoring unit %d.\n", source_unit); |
1641 | 0 | return; |
1642 | 0 | } |
1643 | 0 | session->driver.nmea2000.can_msgcnt += 1; |
1644 | 0 | source_pgn = (frame->can_id >> 8) & 0x1ffff; |
1645 | 0 | source_prio = (frame->can_id >> 26) & 0x7; |
1646 | |
|
1647 | 0 | if (240 > ((source_pgn & 0x0ff00) >> 8)) { |
1648 | 0 | daddr = source_pgn & 0x000ff; |
1649 | 0 | source_pgn = source_pgn & 0x1ff00; |
1650 | 0 | } else { |
1651 | 0 | daddr = 0xff; |
1652 | 0 | } |
1653 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1654 | 0 | "nmea2000: source_prio %u daddr %u\n", |
1655 | 0 | source_prio, daddr); |
1656 | |
|
1657 | 0 | if (!session->driver.nmea2000.unit_valid) { |
1658 | 0 | unsigned int l1, l2; |
1659 | |
|
1660 | 0 | for (l1 = 0; l1 < NMEA2000_NETS; l1++) { |
1661 | 0 | for (l2 = 0; l2 < NMEA2000_UNITS; l2++) { |
1662 | 0 | if (session == nmea2000_units[l1][l2]) { |
1663 | 0 | session->driver.nmea2000.unit = l2; |
1664 | 0 | session->driver.nmea2000.unit_valid = true; |
1665 | 0 | session->driver.nmea2000.can_net = l1; |
1666 | 0 | can_net = l1; |
1667 | 0 | } |
1668 | 0 | } |
1669 | 0 | } |
1670 | |
|
1671 | 0 | session->driver.nmea2000.unit = source_unit; |
1672 | 0 | session->driver.nmea2000.unit_valid = true; |
1673 | 0 | nmea2000_units[can_net][source_unit] = session; |
1674 | 0 | } |
1675 | |
|
1676 | 0 | if (source_unit == session->driver.nmea2000.unit) { |
1677 | 0 | PGN *work; |
1678 | 0 | if (NULL != session->driver.nmea2000.pgnlist) { |
1679 | 0 | work = search_pgnlist(source_pgn, |
1680 | 0 | session->driver.nmea2000.pgnlist); |
1681 | 0 | } else { |
1682 | 0 | PGN *pgnlist; |
1683 | |
|
1684 | 0 | pgnlist = &gpspgn[0]; |
1685 | 0 | work = search_pgnlist(source_pgn, pgnlist); |
1686 | 0 | if (work == NULL) { |
1687 | 0 | pgnlist = &aispgn[0]; |
1688 | 0 | work = search_pgnlist(source_pgn, pgnlist); |
1689 | 0 | } |
1690 | 0 | if (work == NULL) { |
1691 | 0 | pgnlist = &pwrpgn[0]; |
1692 | 0 | work = search_pgnlist(source_pgn, pgnlist); |
1693 | 0 | } |
1694 | 0 | if (work == NULL) { |
1695 | 0 | pgnlist = &navpgn[0]; |
1696 | 0 | work = search_pgnlist(source_pgn, pgnlist); |
1697 | 0 | } |
1698 | 0 | if ((work != NULL) && (work->type > 0)) { |
1699 | 0 | session->driver.nmea2000.pgnlist = pgnlist; |
1700 | 0 | } |
1701 | 0 | } |
1702 | 0 | if (work != NULL) { |
1703 | 0 | if (work->fast == 0) { |
1704 | 0 | size_t l2; |
1705 | |
|
1706 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1707 | 0 | "pgn %6d:%s \n", work->pgn, work->name); |
1708 | 0 | session->driver.nmea2000.workpgn = (void *) work; |
1709 | 0 | session->lexer.outbuflen = frame->can_dlc & 0x0f; |
1710 | 0 | for (l2 = 0; l2 < session->lexer.outbuflen; l2++) { |
1711 | 0 | session->lexer.outbuffer[l2]= frame->data[l2]; |
1712 | 0 | } |
1713 | 0 | } else if (0 == (frame->data[0] & 0x1f)) { |
1714 | 0 | unsigned int l2; |
1715 | |
|
1716 | 0 | session->driver.nmea2000.fast_packet_len = frame->data[1]; |
1717 | 0 | session->driver.nmea2000.idx = frame->data[0]; |
1718 | | #if NMEA2000_FAST_DEBUG |
1719 | | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1720 | | "Set idx %2x %2x %2x %6d\n", |
1721 | | frame->data[0], |
1722 | | session->driver.nmea2000.unit, |
1723 | | frame->data[1], |
1724 | | source_pgn); |
1725 | | #endif // of #if NMEA2000_FAST_DEBUG |
1726 | 0 | session->lexer.inbuflen = 0; |
1727 | 0 | session->driver.nmea2000.idx += 1; |
1728 | 0 | for (l2 = 2; l2 < 8; l2++) { |
1729 | 0 | session->lexer.inbuffer[session->lexer.inbuflen++] = |
1730 | 0 | frame->data[l2]; |
1731 | 0 | } |
1732 | 0 | GPSD_LOG(LOG_DATA, &session->context->errout, |
1733 | 0 | "pgn %6d:%s \n", work->pgn, work->name); |
1734 | 0 | } else if (frame->data[0] == session->driver.nmea2000.idx) { |
1735 | 0 | unsigned int l2; |
1736 | |
|
1737 | 0 | for (l2 = 1; l2 < 8; l2++) { |
1738 | 0 | if (session->driver.nmea2000.fast_packet_len > |
1739 | 0 | session->lexer.inbuflen) { |
1740 | 0 | session->lexer.inbuffer[session->lexer.inbuflen++] = |
1741 | 0 | frame->data[l2]; |
1742 | 0 | } |
1743 | 0 | } |
1744 | 0 | if (session->lexer.inbuflen == |
1745 | 0 | session->driver.nmea2000.fast_packet_len) { |
1746 | | #if NMEA2000_FAST_DEBUG |
1747 | | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1748 | | "Fast done %2x %2x %2x %2x %6d\n", |
1749 | | session->driver.nmea2000.idx, |
1750 | | frame->data[0], |
1751 | | session->driver.nmea2000.unit, |
1752 | | (unsigned int)session->driver.nmea2000.fast_packet_len, |
1753 | | source_pgn); |
1754 | | #endif // of #if NMEA2000_FAST_DEBUG |
1755 | 0 | session->driver.nmea2000.workpgn = (void *) work; |
1756 | 0 | session->lexer.outbuflen = |
1757 | 0 | session->driver.nmea2000.fast_packet_len; |
1758 | 0 | for (l2 = 0; |
1759 | 0 | l2 < (unsigned int)session->lexer.outbuflen; |
1760 | 0 | l2++) { |
1761 | 0 | session->lexer.outbuffer[l2] = |
1762 | 0 | session->lexer.inbuffer[l2]; |
1763 | 0 | } |
1764 | 0 | session->driver.nmea2000.fast_packet_len = 0; |
1765 | 0 | } else { |
1766 | 0 | session->driver.nmea2000.idx += 1; |
1767 | 0 | } |
1768 | 0 | } else { |
1769 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1770 | 0 | "Fast error %2x %2x %2x %2x %6d\n", |
1771 | 0 | session->driver.nmea2000.idx, |
1772 | 0 | frame->data[0], |
1773 | 0 | session->driver.nmea2000.unit, |
1774 | 0 | (unsigned int)session->driver.nmea2000.fast_packet_len, |
1775 | 0 | source_pgn); |
1776 | 0 | } |
1777 | 0 | } else { |
1778 | 0 | GPSD_LOG(LOG_WARN, &session->context->errout, |
1779 | 0 | "PGN not found %08d %08x \n", |
1780 | 0 | source_pgn, source_pgn); |
1781 | 0 | } |
1782 | 0 | } else { |
1783 | | // we got an unknown unit number |
1784 | 0 | if (NULL == nmea2000_units[can_net][source_unit]) { |
1785 | 0 | char buffer[55]; |
1786 | |
|
1787 | 0 | (void) snprintf(buffer, |
1788 | 0 | sizeof(buffer), |
1789 | 0 | "nmea2000://%s:%u", |
1790 | 0 | can_interface_name[can_net], |
1791 | 0 | source_unit); |
1792 | 0 | if (NULL != gpsd_add_device) { |
1793 | 0 | (void) gpsd_add_device(buffer, true); |
1794 | 0 | } |
1795 | 0 | } |
1796 | 0 | } |
1797 | 0 | } else { |
1798 | | // we got RTR or 2.0A CAN frame, not used |
1799 | 0 | } |
1800 | 0 | } |
1801 | | |
1802 | | |
1803 | | static ssize_t nmea2000_get(struct gps_device_t *session) |
1804 | 0 | { |
1805 | 0 | struct can_frame frame; |
1806 | 0 | ssize_t status; |
1807 | |
|
1808 | 0 | errno = 0; |
1809 | 0 | session->lexer.outbuflen = 0; |
1810 | | // FIXME: read() into a struct is not guaranteed in C |
1811 | | // sizeof(frame) === 16 |
1812 | 0 | status = read(session->gpsdata.gps_fd, &frame, sizeof(frame)); |
1813 | 0 | if ((ssize_t)sizeof(frame) == status) { |
1814 | 0 | session->lexer.type = NMEA2000_PACKET; |
1815 | 0 | find_pgn(&frame, session); |
1816 | |
|
1817 | 0 | return frame.can_dlc & 0x0f; |
1818 | 0 | } |
1819 | 0 | if (EAGAIN == status) { |
1820 | | // nothing to read |
1821 | 0 | return 0; |
1822 | 0 | } |
1823 | 0 | GPSD_LOG(LOG_WARN, &session->context->errout, |
1824 | 0 | "NMEA2000 nmea2000_get() status %ld %s(%d) \n", |
1825 | 0 | status, strerror(errno), errno); |
1826 | 0 | return 0; |
1827 | 0 | } |
1828 | | |
1829 | | static gps_mask_t nmea2000_parse_input(struct gps_device_t *session) |
1830 | 0 | { |
1831 | 0 | gps_mask_t mask; |
1832 | 0 | PGN *work; |
1833 | 0 | char buf[128]; |
1834 | | |
1835 | | // printf("NMEA2000 parse_input called\n"); |
1836 | 0 | GPSD_LOG(LOG_RAW, &session->context->errout, |
1837 | 0 | "NMEA2000 nmea2000_parse_input(%s)\n", |
1838 | 0 | gps_hexdump(buf, sizeof(buf), |
1839 | 0 | session->lexer.outbuffer, |
1840 | 0 | session->lexer.outbuflen)); |
1841 | 0 | mask = 0; |
1842 | 0 | work = (PGN *)session->driver.nmea2000.workpgn; |
1843 | |
|
1844 | 0 | if (NULL != work) { |
1845 | 0 | mask = (work->func)(&session->lexer.outbuffer[0], |
1846 | 0 | (int)session->lexer.outbuflen, work, session); |
1847 | 0 | session->driver.nmea2000.workpgn = NULL; |
1848 | 0 | } |
1849 | 0 | session->lexer.outbuflen = 0; |
1850 | |
|
1851 | 0 | return mask; |
1852 | 0 | } |
1853 | | |
1854 | | |
1855 | | int nmea2000_open(struct gps_device_t *session) |
1856 | 0 | { |
1857 | 0 | char interface_name[GPS_PATH_MAX]; |
1858 | 0 | socket_t sock; |
1859 | 0 | int status; |
1860 | 0 | int unit_number; |
1861 | 0 | int can_net; |
1862 | 0 | unsigned int l; |
1863 | 0 | struct ifreq ifr; |
1864 | 0 | struct sockaddr_can addr; |
1865 | 0 | char *unit_ptr; |
1866 | 0 | can_err_mask_t err_mask; |
1867 | 0 | int rcvbuf_size = 1000000; // requested receiver buffer size |
1868 | 0 | int curr_rcvbuf_size; |
1869 | 0 | socklen_t curr_rcvbuf_size_len = sizeof(curr_rcvbuf_size); |
1870 | | |
1871 | |
|
1872 | 0 | INVALIDATE_SOCKET(session->gpsdata.gps_fd); |
1873 | |
|
1874 | 0 | session->driver.nmea2000.can_net = 0; |
1875 | 0 | can_net = -1; |
1876 | |
|
1877 | 0 | unit_number = -1; |
1878 | |
|
1879 | 0 | (void)strlcpy(interface_name, session->gpsdata.dev.path + 11, |
1880 | 0 | sizeof(interface_name)); |
1881 | 0 | unit_ptr = NULL; |
1882 | 0 | for (l = 0; l < strnlen(interface_name, sizeof(interface_name)); l++) { |
1883 | 0 | if (':' == interface_name[l]) { |
1884 | 0 | unit_ptr = &interface_name[l+1]; |
1885 | 0 | interface_name[l] = 0; |
1886 | 0 | continue; |
1887 | 0 | } |
1888 | 0 | if (NULL != unit_ptr) { |
1889 | 0 | if (0 == isdigit(interface_name[l])) { |
1890 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1891 | 0 | "NMEA2000 open: Invalid character in unit number.\n"); |
1892 | 0 | return -1; |
1893 | 0 | } |
1894 | 0 | } |
1895 | 0 | } |
1896 | | |
1897 | 0 | if (NULL != unit_ptr) { |
1898 | 0 | unit_number = atoi(unit_ptr); |
1899 | 0 | if ((0 > unit_number) || |
1900 | 0 | ((NMEA2000_UNITS - 1) < unit_number)) { |
1901 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1902 | 0 | "NMEA2000 open: Unit number out of range.\n"); |
1903 | 0 | return -1; |
1904 | 0 | } |
1905 | 0 | for (l = 0; l < NMEA2000_NETS; l++) { |
1906 | 0 | if (strncmp(can_interface_name[l], |
1907 | 0 | interface_name, |
1908 | 0 | MIN(sizeof(interface_name), |
1909 | 0 | sizeof(can_interface_name[l]))) == 0) { |
1910 | 0 | can_net = l; |
1911 | 0 | break; |
1912 | 0 | } |
1913 | 0 | } |
1914 | 0 | if (0 > can_net) { |
1915 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1916 | 0 | "NMEA2000 open: CAN device not open: %s .\n", |
1917 | 0 | interface_name); |
1918 | 0 | return -1; |
1919 | 0 | } |
1920 | 0 | } else { |
1921 | 0 | for (l = 0; l < NMEA2000_NETS; l++) { |
1922 | 0 | if (strncmp(can_interface_name[l], |
1923 | 0 | interface_name, |
1924 | 0 | MIN(sizeof(interface_name), |
1925 | 0 | sizeof(can_interface_name[l]))) == 0) { |
1926 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1927 | 0 | "NMEA2000 open: CAN device duplicate open: %s .\n", |
1928 | 0 | interface_name); |
1929 | 0 | return -1; |
1930 | 0 | } |
1931 | 0 | } |
1932 | 0 | for (l = 0; l < NMEA2000_NETS; l++) { |
1933 | 0 | if (can_interface_name[l][0] == 0) { |
1934 | 0 | can_net = l; |
1935 | 0 | break; |
1936 | 0 | } |
1937 | 0 | } |
1938 | 0 | if (0 > can_net) { |
1939 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1940 | 0 | "NMEA2000 open: Too many CAN networks open.\n"); |
1941 | 0 | return -1; |
1942 | 0 | } |
1943 | 0 | } |
1944 | | |
1945 | | // Create the socket |
1946 | 0 | sock = socket(PF_CAN, SOCK_RAW, CAN_RAW); |
1947 | |
|
1948 | 0 | if (BAD_SOCKET(sock)) { |
1949 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1950 | 0 | "NMEA2000 open: socket(PF_CAN) %s(%d).\n", |
1951 | 0 | strerror(errno), errno); |
1952 | 0 | return -1; |
1953 | 0 | } |
1954 | | |
1955 | 0 | status = fcntl(sock, F_SETFL, O_NONBLOCK); |
1956 | 0 | if (0 != status) { |
1957 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1958 | 0 | "NMEA2000 open: fcntl(O_NONBLOCK) %s(%d).\n", |
1959 | 0 | strerror(errno), errno); |
1960 | 0 | close(sock); |
1961 | 0 | return -1; |
1962 | 0 | } |
1963 | | |
1964 | | // turn on CANBUS error reporting |
1965 | 0 | err_mask = CAN_ERR_ACK | CAN_ERR_BUSOFF | CAN_ERR_CRTL | CAN_ERR_LOSTARB | |
1966 | 0 | CAN_ERR_PROT | CAN_ERR_RESTARTED | CAN_ERR_TRX | |
1967 | 0 | CAN_ERR_TX_TIMEOUT; |
1968 | |
|
1969 | 0 | status = setsockopt(sock, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, |
1970 | 0 | &err_mask, sizeof(err_mask)); |
1971 | 0 | if (0 != status) { |
1972 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1973 | 0 | "NMEA2000 open: setsockopt() %s(%d)\n", |
1974 | 0 | strerror(errno), errno); |
1975 | 0 | } |
1976 | | |
1977 | | /* enbiggen the receiver buffer size |
1978 | | * try SO_RCVBUFFORCE first, if we run with CAP_NET_ADMIN */ |
1979 | 0 | if (setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE, |
1980 | 0 | &rcvbuf_size, sizeof(rcvbuf_size)) < 0) { |
1981 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1982 | 0 | "NMEA2000 open:SO_RCVBUFFORCE failed try RCVBUF. " |
1983 | 0 | "%s(%d)\n", |
1984 | 0 | strerror(errno), errno); |
1985 | 0 | if (0 > setsockopt(sock, SOL_SOCKET, SO_RCVBUF, |
1986 | 0 | &rcvbuf_size, sizeof(rcvbuf_size))) { |
1987 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1988 | 0 | "NMEA2000 open:setsockopt(SO_RCVBUF) %s(%d).\n", |
1989 | 0 | strerror(errno), errno); |
1990 | 0 | } |
1991 | 0 | } |
1992 | 0 | if (0 > getsockopt(sock, SOL_SOCKET, SO_RCVBUF, |
1993 | 0 | &curr_rcvbuf_size, &curr_rcvbuf_size_len)) { |
1994 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1995 | 0 | "NMEA2000 open:getsockopt(SO_RCVBUF) %s(%d)\n", |
1996 | 0 | strerror(errno), errno); |
1997 | 0 | } else { |
1998 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
1999 | 0 | "NMEA2000 open:getsockopt(SO_RCVBUF) = %d\n", |
2000 | 0 | curr_rcvbuf_size); |
2001 | 0 | } |
2002 | | |
2003 | | // Locate the interface you wish to use |
2004 | 0 | strlcpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name)); |
2005 | 0 | status = ioctl(sock, SIOCGIFINDEX, &ifr); /* ifr.ifr_ifindex gets filled |
2006 | | * with that device's index */ |
2007 | |
|
2008 | 0 | if (0 != status) { |
2009 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
2010 | 0 | "NMEA2000 open: can not find CAN device.\n"); |
2011 | 0 | close(sock); |
2012 | 0 | return -1; |
2013 | 0 | } |
2014 | | |
2015 | | // Select that CAN interface, and bind the socket to it. |
2016 | 0 | addr.can_family = AF_CAN; |
2017 | 0 | addr.can_ifindex = ifr.ifr_ifindex; |
2018 | 0 | status = bind(sock, (struct sockaddr*)&addr, sizeof(addr) ); |
2019 | 0 | if (0 != status) { |
2020 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
2021 | 0 | "NMEA2000 open: bind failed.\n"); |
2022 | 0 | close(sock); |
2023 | 0 | return -1; |
2024 | 0 | } |
2025 | | |
2026 | 0 | gpsd_switch_driver(session, "NMEA2000"); |
2027 | 0 | session->gpsdata.gps_fd = sock; |
2028 | 0 | session->sourcetype = SOURCE_CAN; |
2029 | 0 | session->servicetype = SERVICE_SENSOR; |
2030 | 0 | session->driver.nmea2000.can_net = can_net; |
2031 | |
|
2032 | 0 | if (NULL != unit_ptr) { |
2033 | 0 | nmea2000_units[can_net][unit_number] = session; |
2034 | 0 | session->driver.nmea2000.unit = unit_number; |
2035 | 0 | session->driver.nmea2000.unit_valid = true; |
2036 | 0 | } else { |
2037 | 0 | strlcpy(can_interface_name[can_net], |
2038 | 0 | interface_name, |
2039 | 0 | MIN(sizeof(can_interface_name[0]), sizeof(interface_name))); |
2040 | 0 | session->driver.nmea2000.unit_valid = false; |
2041 | 0 | for (l = 0; l < NMEA2000_UNITS; l++) { |
2042 | 0 | nmea2000_units[can_net][l] = NULL; |
2043 | 0 | } |
2044 | 0 | } |
2045 | |
|
2046 | 0 | session->gpsdata.dev.parity = 'N'; |
2047 | 0 | session->gpsdata.dev.baudrate = 250000; |
2048 | 0 | session->gpsdata.dev.stopbits = 0; |
2049 | 0 | return session->gpsdata.gps_fd; |
2050 | 0 | } |
2051 | | |
2052 | | void nmea2000_close(struct gps_device_t *session) |
2053 | 0 | { |
2054 | 0 | if (!BAD_SOCKET(session->gpsdata.gps_fd)) { |
2055 | | // cast for 32-bit ints. |
2056 | 0 | GPSD_LOG(LOG_SPIN, &session->context->errout, |
2057 | 0 | "close(%ld) in nmea2000_close(%s)\n", |
2058 | 0 | (long)session->gpsdata.gps_fd, session->gpsdata.dev.path); |
2059 | 0 | (void)close(session->gpsdata.gps_fd); |
2060 | 0 | INVALIDATE_SOCKET(session->gpsdata.gps_fd); |
2061 | |
|
2062 | 0 | if (session->driver.nmea2000.unit_valid) { |
2063 | 0 | unsigned int l1, l2; |
2064 | |
|
2065 | 0 | for (l1 = 0; l1 < NMEA2000_NETS; l1++) { |
2066 | 0 | for (l2 = 0; l2 < NMEA2000_UNITS; l2++) { |
2067 | 0 | if (session == nmea2000_units[l1][l2]) { |
2068 | 0 | session->driver.nmea2000.unit_valid = false; |
2069 | 0 | session->driver.nmea2000.unit = 0; |
2070 | 0 | session->driver.nmea2000.can_net = 0; |
2071 | | nmea2000_units[l1][l2] = NULL; |
2072 | 0 | } |
2073 | 0 | } |
2074 | 0 | } |
2075 | 0 | } |
2076 | 0 | } |
2077 | 0 | } |
2078 | | |
2079 | | // *INDENT-OFF* |
2080 | | const struct gps_type_t driver_nmea2000 = { |
2081 | | .type_name = "NMEA2000", // full name of type |
2082 | | .packet_type = NMEA2000_PACKET, // associated lexer packet type |
2083 | | .flags = DRIVER_STICKY, // remember this |
2084 | | .trigger = NULL, // detect their main sentence |
2085 | | .channels = 12, // not an actual GPS at all |
2086 | | .probe_detect = NULL, |
2087 | | .get_packet = nmea2000_get, // how to get a packet |
2088 | | .parse_packet = nmea2000_parse_input, // how to interpret a packet |
2089 | | .rtcm_writer = NULL, // Don't send RTCM to this |
2090 | | .init_query = NULL, // non-perturbing query |
2091 | | .event_hook = NULL, |
2092 | | .speed_switcher = NULL, // no speed switcher |
2093 | | .mode_switcher = NULL, // no mode switcher |
2094 | | .rate_switcher = NULL, // no rate switcher |
2095 | | .min_cycle.tv_sec = 1, // not relevant, no rate switch |
2096 | | .min_cycle.tv_nsec = 0, // not relevant, no rate switch |
2097 | | .control_send = NULL, // how to send control strings |
2098 | | .time_offset = NULL, |
2099 | | }; |
2100 | | // *INDENT-ON* |
2101 | | |
2102 | | // end |
2103 | | |
2104 | | #else // of defined(NMEA2000_ENABLE) |
2105 | | /* dummy variable to some old linkers do not complain about empty |
2106 | | * object file */ |
2107 | | int nmea2000_dummy = 1; |
2108 | | #endif // of defined(NMEA2000_ENABLE) |
2109 | | |
2110 | | // vim: set expandtab shiftwidth=4 |