forked from ttlappalainen/NMEA2000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN2kMsg.cpp
651 lines (535 loc) · 20.6 KB
/
N2kMsg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
N2kMsg.cpp
Copyright (c) 2015-2016 Timo Lappalainen, Kave Oy, www.kave.fi
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "N2kMsg.h"
#include <math.h>
#include <stdlib.h>
//#include <MemoryFree.h> // For testing used memory
#define Escape 0x10
#define StartOfText 0x02
#define EndOfText 0x03
#define MsgTypeN2k 0x93
#define MaxActisenseMsgBuf 400
//*****************************************************************************
// On Arduino round() is a macro, hence the definition check. On other systems
// it is a function that may or may not be implemented so we do it ourselves.
#if !defined(round)
double round(double val) {
return val >= 0
? floor(val + 0.5)
: ceil(val - 0.5);
}
#endif
//*****************************************************************************
tN2kMsg::tN2kMsg(unsigned char _Source) {
Init(6,0,_Source,255);
}
//*****************************************************************************
void tN2kMsg::SetPGN(unsigned long _PGN) {
Clear();
PGN=_PGN;
MsgTime=millis();
}
//*****************************************************************************
void tN2kMsg::Init(unsigned char _Priority, unsigned long _PGN, unsigned char _Source, unsigned char _Destination) {
DataLen=0;
Priority=_Priority & 0x7;
SetPGN(_PGN);
Source=_Source;
Destination=_Destination;
}
//*****************************************************************************
void tN2kMsg::Clear() {
PGN=0;
DataLen=0;
MsgTime=0;
}
//*****************************************************************************
void tN2kMsg::Add8ByteDouble(double v, double precision, double UndefVal) {
if (v!=UndefVal) {
SetBuf8ByteDouble(v,precision,DataLen,Data);
} else {
SetBuf4ByteUInt(N2kUInt32NA,DataLen,Data);
SetBuf4ByteUInt(N2kInt32NA,DataLen,Data);
}
}
//*****************************************************************************
void tN2kMsg::Add4ByteDouble(double v, double precision, double UndefVal) {
if (v!=UndefVal) {
SetBuf4ByteDouble(v,precision,DataLen,Data);
} else {
SetBuf4ByteUInt(N2kInt32NA,DataLen,Data);
}
}
//*****************************************************************************
void tN2kMsg::Add4ByteUDouble(double v, double precision, double UndefVal) {
if (v!=UndefVal) {
SetBuf4ByteUDouble(v,precision,DataLen,Data);
} else {
SetBuf4ByteUInt(N2kUInt32NA,DataLen,Data);
}
}
//*****************************************************************************
void tN2kMsg::Add3ByteDouble(double v, double precision, double UndefVal) {
if (v!=UndefVal) {
SetBuf3ByteDouble(v,precision,DataLen,Data);
} else {
SetBuf3ByteInt(0x7fffff,DataLen,Data);
}
}
//*****************************************************************************
void tN2kMsg::Add2ByteDouble(double v, double precision, double UndefVal) {
if (v!=UndefVal) {
SetBuf2ByteDouble(v,precision,DataLen,Data);
} else {
SetBuf2ByteUInt(N2kInt16NA,DataLen,Data);
}
}
//*****************************************************************************
void tN2kMsg::Add2ByteUDouble(double v, double precision, double UndefVal) {
if (v!=UndefVal) {
SetBuf2ByteUDouble(v,precision,DataLen,Data);
} else {
SetBuf2ByteUInt(N2kUInt16NA,DataLen,Data);
}
}
//*****************************************************************************
void tN2kMsg::Add1ByteDouble(double v, double precision, double UndefVal) {
if (v!=UndefVal) {
SetBuf1ByteDouble(v,precision,DataLen,Data);
} else {
AddByte(N2kInt8NA);
}
}
//*****************************************************************************
void tN2kMsg::Add1ByteUDouble(double v, double precision, double UndefVal) {
if (v!=UndefVal) {
SetBuf1ByteUDouble(v,precision,DataLen,Data);
} else {
AddByte(N2kUInt8NA);
}
}
//*****************************************************************************
void tN2kMsg::Add2ByteInt(int16_t v) {
SetBuf2ByteInt(v,DataLen,Data);
}
//*****************************************************************************
void tN2kMsg::Add2ByteUInt(uint16_t v) {
SetBuf2ByteUInt(v,DataLen,Data);
}
//*****************************************************************************
void tN2kMsg::Add3ByteInt(int32_t v) {
SetBuf3ByteInt(v,DataLen,Data);
}
//*****************************************************************************
void tN2kMsg::Add4ByteUInt(uint32_t v) {
SetBuf4ByteUInt(v,DataLen,Data);
}
//*****************************************************************************
void tN2kMsg::AddUInt64(uint64_t v) {
SetBufUInt64(v,DataLen,Data);
}
//*****************************************************************************
void tN2kMsg::AddByte(unsigned char v) {
Data[DataLen]=v; DataLen++;
}
//*****************************************************************************
void tN2kMsg::AddStr(const char *str, int len) {
SetBufStr(str,len,DataLen,Data);
}
//*****************************************************************************
unsigned char tN2kMsg::GetByte(int &Index) const {
if (Index<DataLen) {
return Data[Index++];
} else return 0xff;
}
//*****************************************************************************
int16_t tN2kMsg::Get2ByteInt(int &Index, int16_t def) const {
if (Index+2<=DataLen) {
return GetBuf2ByteInt(Index,Data);
} else return def;
}
//*****************************************************************************
uint16_t tN2kMsg::Get2ByteUInt(int &Index, uint16_t def) const {
if (Index+2<=DataLen) {
return GetBuf2ByteUInt(Index,Data);
} else return def;
}
//*****************************************************************************
uint32_t tN2kMsg::Get4ByteUInt(int &Index, uint32_t def) const {
if (Index+4<=DataLen) {
return GetBuf4ByteUInt(Index,Data);
} else return def;
}
//*****************************************************************************
double tN2kMsg::Get1ByteDouble(double precision, int &Index, double def) const {
if (Index<DataLen) {
return GetBuf1ByteDouble(precision,Index,Data,def);
} else return def;
}
//*****************************************************************************
double tN2kMsg::Get1ByteUDouble(double precision, int &Index, double def) const {
if (Index<DataLen) {
return GetBuf1ByteUDouble(precision,Index,Data,def);
} else return def;
}
//*****************************************************************************
double tN2kMsg::Get2ByteDouble(double precision, int &Index, double def) const {
if (Index+2<=DataLen) {
return GetBuf2ByteDouble(precision,Index,Data,def);
} else return def;
}
//*****************************************************************************
double tN2kMsg::Get2ByteUDouble(double precision, int &Index, double def) const {
if (Index+2<=DataLen) {
return GetBuf2ByteUDouble(precision,Index,Data,def);
} else return def;
}
//*****************************************************************************
double tN2kMsg::Get3ByteDouble(double precision, int &Index, double def) const {
if (Index+3<=DataLen) {
return GetBuf3ByteDouble(precision,Index,Data,def);
} else return def;
}
//*****************************************************************************
double tN2kMsg::Get4ByteDouble(double precision, int &Index, double def) const {
if (Index+4<=DataLen) {
return GetBuf4ByteDouble(precision,Index,Data,def);
} else return def;
}
//*****************************************************************************
double tN2kMsg::Get4ByteUDouble(double precision, int &Index, double def) const {
if (Index+4<=DataLen) {
return GetBuf4ByteUDouble(precision,Index,Data,def);
} else return def;
}
//*****************************************************************************
double tN2kMsg::Get8ByteDouble(double precision, int &Index, double def) const {
if (Index+8<=DataLen) {
return GetBuf8ByteDouble(precision,Index,Data,def);
} else return def;
}
//*****************************************************************************
bool tN2kMsg::GetStr(char *StrBuf, int Length, int &Index) const {
unsigned char vb;
bool nullReached = false;
StrBuf[0] = '\0';
if (Index+Length<=DataLen) {
for (int i=0; i<Length; i++) {
vb = GetByte(Index);
if (! nullReached) {
if (vb == 0x00 || vb == '@') {
nullReached = true; // either null or '@' (AIS null character)
StrBuf[i] = '\0';
StrBuf[i+1] = '\0';
} else {
StrBuf[i] = vb;
StrBuf[i+1] = '\0';
}
} else {
StrBuf[i] = '\0';
StrBuf[i+1] = '\0';
}
}
return true;
} else return false;
}
//*****************************************************************************
bool tN2kMsg::Set2ByteUInt(uint16_t v, int &Index) {
if (Index+2<=DataLen) {
SetBuf2ByteUInt(v,Index,Data);
return true;
} else
return false;
}
//*****************************************************************************
void SetBuf8ByteDouble(double v, double precision, int &index, unsigned char *buf) {
double fp=precision*1e6;
long long fpll=1/fp;
long long vll=v*1e6;
vll*=fpll;
/* Does not work. Why?
long long *vll=(long long *)(&buf[index]);
(*vll)=v*1e6;
(*vll)*=fpll;
index+=8;
*/
buf[index]=vll&255; index++;
vll>>=8;
buf[index]=vll&255; index++;
vll>>=8;
buf[index]=vll&255; index++;
vll>>=8;
buf[index]=vll&255; index++;
vll>>=8;
buf[index]=vll&255; index++;
vll>>=8;
buf[index]=vll&255; index++;
vll>>=8;
buf[index]=vll&255; index++;
vll>>=8;
buf[index]=vll&255; index++;
}
//*****************************************************************************
void SetBuf4ByteDouble(double v, double precision, int &index, unsigned char *buf) {
int32_t *vi=(int32_t *)(&buf[index]);
index+=4;
(*vi)=(int32_t)round(v/precision);
}
//*****************************************************************************
void SetBuf4ByteUDouble(double v, double precision, int &index, unsigned char *buf) {
uint32_t *vi=(uint32_t *)(&buf[index]);
index+=4;
(*vi)=(uint32_t)round(v/precision);
}
//*****************************************************************************
void SetBuf3ByteDouble(double v, double precision, int &index, unsigned char *buf) {
long vl;
vl=(long)round(v/precision);
SetBuf3ByteInt(vl,index,buf);
}
//*****************************************************************************
int16_t GetBuf2ByteInt(int &index, const unsigned char *buf) {
int16_t *vi=(int16_t *)(&buf[index]);
index+=2;
return *vi;
}
//*****************************************************************************
uint16_t GetBuf2ByteUInt(int &index, const unsigned char *buf) {
uint16_t *vi=(uint16_t *)(&buf[index]);
index+=2;
return *vi;
}
//*****************************************************************************
uint32_t GetBuf4ByteUInt(int &index, const unsigned char *buf) {
uint32_t *vi=(uint32_t *)(&buf[index]);
index+=4;
return *vi;
}
//*****************************************************************************
double GetBuf1ByteDouble(double precision, int &index, const unsigned char *buf, double def) {
int8_t *vi=(int8_t *)(&buf[index]);
index+=1;
if (*vi==0x7f) return def;
return ((double)(*vi))*precision;
}
//*****************************************************************************
double GetBuf1ByteUDouble(double precision, int &index, const unsigned char *buf, double def) {
uint8_t *vi=(uint8_t *)(&buf[index]);
index+=1;
if (*vi==0xff) return def;
return ((double)(*vi))*precision;
}
//*****************************************************************************
double GetBuf2ByteDouble(double precision, int &index, const unsigned char *buf, double def) {
int16_t *vi=(int16_t *)(&buf[index]);
index+=2;
if (*vi==0x7fff) return def;
return ((double)(*vi))*precision;
}
//*****************************************************************************
double GetBuf2ByteUDouble(double precision, int &index, const unsigned char *buf, double def) {
uint16_t *vi=(uint16_t *)(&buf[index]);
index+=2;
if (*vi==0xffff) return def;
return ((double)(*vi))*precision;
}
//*****************************************************************************
double GetBuf8ByteDouble(double precision, int &index, const unsigned char *buf, double def) {
// Next does not work on any board I tested
// long long *vll=(long long *)(&buf[index]);
// index+=8;
unsigned long *vllo=(unsigned long *)(&buf[index]);
index+=4;
long *vlhi=(long *)(&buf[index]);
index+=4;
if ( (*vlhi==0x7fff) && (*vllo==0xffff) ) return def;
double v=*vlhi * 4294967296.0;
if (v>=0) { v += *vllo; } else { v -= *vllo; }
// Below did not work even with Due
//long long vll=*vlhi;
//vll<<=32;
//vll|=*vllo;
//Serial.print(*vlhi,HEX); Serial.print(","); Serial.println(*vllo,HEX);
// if (((unsigned long long)(vll))==0xffffffffffffffff) return def;
return v*precision;
}
//*****************************************************************************
double GetBuf3ByteDouble(double precision, int &index, const unsigned char *buf, double def) {
long *vl=(long *)(&buf[index]);
long vll=*vl;
index+=3;
// We use only 3 bytes, so set highest byte to 0
vll&=0x00ffffff;
if (vll==0x007fffff) return def;
return ((double)(vll))*precision;
}
//*****************************************************************************
double GetBuf4ByteDouble(double precision, int &index, const unsigned char *buf, double def) {
long *vl=(long *)(&buf[index]);
index+=4;
if (*vl==0x7fffffff) return def;
return ((double)(*vl))*precision;
}
//*****************************************************************************
double GetBuf4ByteUDouble(double precision, int &index, const unsigned char *buf, double def) {
unsigned long *vl=(unsigned long *)(&buf[index]);
index+=4;
if (*vl==0xffffffff) return def;
return ((double)(*vl))*precision;
}
//*****************************************************************************
void SetBuf2ByteDouble(double v, double precision, int &index, unsigned char *buf) {
int16_t *vi=(int16_t *)(&buf[index]);
index+=2;
(*vi)=(int16_t)round(v/precision);
}
//*****************************************************************************
void SetBuf2ByteUDouble(double v, double precision, int &index, unsigned char *buf) {
uint16_t *vi=(uint16_t *)(&buf[index]);
index+=2;
(*vi)=(uint16_t)round(v/precision);
}
//*****************************************************************************
void SetBuf1ByteDouble(double v, double precision, int &index, unsigned char *buf) {
int8_t *vi=(int8_t *)(&buf[index]);
index+=1;
(*vi)=(int8_t)round(v/precision);
}
//*****************************************************************************
void SetBuf1ByteUDouble(double v, double precision, int &index, unsigned char *buf) {
uint8_t *vi=(uint8_t *)(&buf[index]);
index+=1;
(*vi)=(uint8_t)round(v/precision);
}
//*****************************************************************************
void SetBuf2ByteInt(int16_t v, int &index, unsigned char *buf) {
buf[index]=v&0xff; v>>=8; index++;
buf[index]=v&0xff; index++;
}
//*****************************************************************************
void SetBuf2ByteUInt(uint16_t v, int &index, unsigned char *buf) {
buf[index]=v&0xff; v>>=8; index++;
buf[index]=v&0xff; index++;
}
//*****************************************************************************
void SetBuf3ByteInt(int32_t v, int &index, unsigned char *buf) {
buf[index]=v&0xff; v>>=8; index++;
buf[index]=v&0xff; v>>=8; index++;
buf[index]=v&0xff; index++;
}
//*****************************************************************************
void SetBuf4ByteUInt(uint32_t v, int &index, unsigned char *buf) {
uint32_t *vl=(uint32_t *)(&buf[index]);
index+=4;
(*vl)=v;
}
//*****************************************************************************
void SetBufUInt64(uint64_t v, int &index, unsigned char *buf) {
buf[index]=v&0xff; v>>=8; index++;
buf[index]=v&0xff; v>>=8; index++;
buf[index]=v&0xff; v>>=8; index++;
buf[index]=v&0xff; v>>=8; index++;
buf[index]=v&0xff; v>>=8; index++;
buf[index]=v&0xff; v>>=8; index++;
buf[index]=v&0xff; v>>=8; index++;
buf[index]=v&0xff; index++;
}
//*****************************************************************************
void SetBufStr(const char *str, int len, int &index, unsigned char *buf) {
int i=0;
for (; i<len && str[i]!=0; i++, index++) {
buf[index]=str[i];
}
for (; i<len; i++, index++) {
buf[index]=0;
}
}
//*****************************************************************************
void PrintBuf(N2kStream *port, unsigned char len, const unsigned char *pData, bool AddLF) {
if (port==0) return;
for(int i = 0; i<len; i++) {
if (i>0) { port->print(F(",")); };
// Print bytes as hex.
port->print(pData[i], 16);
}
if (AddLF) port->println(F(""));
}
//*****************************************************************************
void tN2kMsg::Print(N2kStream *port, bool NoData) const {
if (port==0 || !IsValid()) return;
port->print(F("Pri:")); port->print(Priority);
port->print(F(" PGN:")); port->print(PGN);
port->print(F(" Source:")); port->print(Source);
port->print(F(" Dest:")); port->print(Destination);
port->print(F(" Len:")); port->print(DataLen);
if (!NoData) {
port->print(F(" Data:"));
PrintBuf(port,DataLen,Data);
}
port->println(F(""));
}
//*****************************************************************************
void AddByteEscapedToBuf(unsigned char byteToAdd, uint8_t &idx, unsigned char *buf, int &byteSum)
{
buf[idx++]=byteToAdd;
byteSum+=byteToAdd;
if (byteToAdd == Escape) {
buf[idx++]=Escape;
}
}
//*****************************************************************************
// Actisense Format:
// <10><02><93><length (1)><priority (1)><PGN (3)><destination (1)><source (1)><time (4)><len (1)><data (len)><CRC (1)><10><03>
void tN2kMsg::SendInActisenseFormat(N2kStream *port) const {
unsigned long _PGN=PGN;
unsigned long _MsgTime=MsgTime;
uint8_t msgIdx=0;
int byteSum = 0;
uint8_t CheckSum;
unsigned char ActisenseMsgBuf[MaxActisenseMsgBuf];
if (port==0 || !IsValid()) return;
// Serial.print("freeMemory()="); Serial.println(freeMemory());
ActisenseMsgBuf[msgIdx++]=Escape;
ActisenseMsgBuf[msgIdx++]=StartOfText;
AddByteEscapedToBuf(MsgTypeN2k,msgIdx,ActisenseMsgBuf,byteSum);
AddByteEscapedToBuf(DataLen+11,msgIdx,ActisenseMsgBuf,byteSum); //length does not include escaped chars
AddByteEscapedToBuf(Priority,msgIdx,ActisenseMsgBuf,byteSum);
AddByteEscapedToBuf(_PGN & 0xff,msgIdx,ActisenseMsgBuf,byteSum); _PGN>>=8;
AddByteEscapedToBuf(_PGN & 0xff,msgIdx,ActisenseMsgBuf,byteSum); _PGN>>=8;
AddByteEscapedToBuf(_PGN & 0xff,msgIdx,ActisenseMsgBuf,byteSum);
AddByteEscapedToBuf(Destination,msgIdx,ActisenseMsgBuf,byteSum);
AddByteEscapedToBuf(Source,msgIdx,ActisenseMsgBuf,byteSum);
// Time?
AddByteEscapedToBuf(_MsgTime & 0xff,msgIdx,ActisenseMsgBuf,byteSum); _MsgTime>>=8;
AddByteEscapedToBuf(_MsgTime & 0xff,msgIdx,ActisenseMsgBuf,byteSum); _MsgTime>>=8;
AddByteEscapedToBuf(_MsgTime & 0xff,msgIdx,ActisenseMsgBuf,byteSum); _MsgTime>>=8;
AddByteEscapedToBuf(_MsgTime & 0xff,msgIdx,ActisenseMsgBuf,byteSum);
AddByteEscapedToBuf(DataLen,msgIdx,ActisenseMsgBuf,byteSum);
for (int i = 0; i < DataLen; i++) AddByteEscapedToBuf(Data[i],msgIdx,ActisenseMsgBuf,byteSum);
byteSum %= 256;
CheckSum = (uint8_t)((byteSum == 0) ? 0 : (256 - byteSum));
ActisenseMsgBuf[msgIdx++]=CheckSum;
if (CheckSum==Escape) ActisenseMsgBuf[msgIdx++]=CheckSum;
ActisenseMsgBuf[msgIdx++] = Escape;
ActisenseMsgBuf[msgIdx++] = EndOfText;
port->write(ActisenseMsgBuf,msgIdx);
// Serial.print("Actisense data:");
// PrintBuf(msgIdx,ActisenseMsgBuf);
// Serial.print("\r\n");
}