-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathNMEA2000.h
More file actions
3353 lines (3131 loc) · 146 KB
/
Copy pathNMEA2000.h
File metadata and controls
3353 lines (3131 loc) · 146 KB
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* NMEA2000.h
*
* The MIT License
*
* Copyright (c) 2015-2024 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.
*/
/*************************************************************************//**
* \file NMEA2000.h
* \brief This file contains the class tNMEA2000, which consists the main
* functionality of the library
*
* With tNMEA2000 class you can easily communicate with NMEA2000 bus.
* Library can be used for any kind of NMEA2000 bus device needs from
* bus traffic listener to complex MFD system.
*
* As default library simply reads all messages from bus and forwards
* them to defined forward stream (e.g., Serial) in Actisense format.
* Using N2km_ListenAndNode mode, one can make NMEA2000 compatible devices and
* even NMEA2000 certified devices by implementing all \ref secRefNMEA2000Certification
* requirements. Simple example is bus device, which provides some sensor
* like temperature, battery or engine information to the bus to be shown or MFD.
*
* For detailed description see \ref tNMEA2000.
*
*/
#ifndef _NMEA2000_H_
#define _NMEA2000_H_
#include "NMEA2000_CompilerDefns.h"
#include "N2kStream.h"
#include "N2kMsg.h"
#include "N2kCANMsg.h"
#include "N2kTimer.h"
#if !defined(N2K_NO_GROUP_FUNCTION_SUPPORT)
#include "N2kGroupFunction.h"
#endif
/** \brief PGN for an ISO Address Claim message */
#define N2kPGNIsoAddressClaim 60928L
/** \brief PGN for a Production Information message */
#define N2kPGNProductInformation 126996L
/** \brief PGN for an Configuration Information message */
#define N2kPGNConfigurationInformation 126998L
// Document says for lengths 33,40,24,32, but then values
// has not been translated right on devices.
/** \brief Max length of ModelID
* Document says for length 33 but then values has not
* been translated right on devices. */
#define Max_N2kModelID_len 32
/** \brief Max length of Software Code
* Document says for length 40 but then values has not
* been translated right on devices. */
#define Max_N2kSwCode_len 32
/** \brief Max length of Model Version
* Document says for length 24 but then values has not
* been translated right on devices. */
#define Max_N2kModelVersion_len 32
/** \brief Max length of SerialCode
* Document says for length 32 but then values has not
* been translated right on devices. */
#define Max_N2kModelSerialCode_len 32
/** \brief Define length of longest info string
* Define length of longest info string (from \ref
* Max_N2kModelID_len, \ref Max_N2kSwCode_len, \ref
* Max_N2kModelVersion_len, \ref Max_N2kModelSerialCode_len)
* + 1 termination char
*/
#define Max_N2kProductInfoStrLen 33
/************************************************************************//**
* \brief Max length of Configuration Info Fields
*
* I do not know what standard says about max field length, but according
* to tests NMEAReader crashed with length >=90. Some device was reported
* not to work string length over 70.
*/
#define Max_N2kConfigurationInfoField_len 71 // 70 + '/0'
/** \brief Message buffer time*/
#define Max_N2kMsgBuf_Time 100
/** \brief Number of message groups */
#define N2kMessageGroups 2
/** \brief Max CAN Bus Address given by the library*/
#define N2kMaxCanBusAddress 251
/** \brief Null Address (???)*/
#define N2kNullCanBusAddress 254
/************************************************************************//**
* \class tNMEA2000CallbackInterface
* \brief Interface for handling various NMEA2000 callbacks using C++ objects
* \ingroup group_helperClass
*
* This is an interface that might be implemented and passed as an instance
* using method 'SetCallbackHandlerInterface'. It's an alternative to use
* instead of 'OnOpen', 'MsgHandler', and 'ISORqstHandler', when there is a
* need to use C++ instances instead of C-like function pointers.
*
*/
class tNMEA2000CallbackInterface
{
public:
virtual ~tNMEA2000CallbackInterface() { }
virtual void OnOpen() = 0;
virtual void OnMessage(const tN2kMsg &N2kMsg) = 0;
virtual bool OnIsoRequest(unsigned long RequestedPGN, unsigned char Requester, int DeviceIndex) = 0;
};
/************************************************************************//**
* \class tNMEA2000
* \brief tNMEA2000 device class definition.
* \ingroup group_core
*
* With tNMEA2000 class you can easily communicate with NMEA2000 bus.
* Library can be used for any kind of NMEA2000 bus device needs from
* bus traffic listener to complex MFD system.
*
* As default library simply reads all messages from bus and forwards
* them to defined forward stream (e.g., Serial) in Actisense format.
* Using N2km_ListenAndNode mode, one can make NMEA2000 compatible devices and
* even NMEA2000 certified devices by implementing all \ref secRefNMEA2000Certification
* requirements. Simple example is bus device, which provides some sensor
* like temperature, battery or engine information to the bus to be shown or MFD.
*
* Class can be used just for reading bus data. More common is to use it as bus device
* (also called node). For bus device mode you provide required basic information
* about device to class, which then takes care of informing other devices on the bus in
* standard way. For other message handling class provides callback methods and method
* to send messages.
*
* Each device on NMEA2000 bus will have own source address on range 0-251. Class takes
* care of \ref secRefTermAddressClaiming "address claiming" (also called dynamic addressing)
* without any developer interaction.
* Class also provides \ref descMultiDeviceSupport "multi device support", if that is necessary.
*/
class tNMEA2000
{
public:
/************************************************************************//**
* \brief Check if the given PGN is proprietary
*
* PGNs that are proprietary by definition: 61184, 65280 - 65535, 126720,
* 130816 - 131071
*
* \param PGN PGN to be checked
* \retval true -> for PGNs:
* - 126720L
* - 130816L ... 131071L
* - 65280L ... 65535L
* \retval false
*/
static bool IsProprietaryMessage(unsigned long PGN);
/************************************************************************//**
* \brief Clears a char array buffer with 0s
*
* \param MaxLen Max length of the buffer
* \param buf Pointer to the buffer
*/
static void ClearCharBuf(size_t MaxLen, char *buf);
/************************************************************************//**
* \brief Setting up a Char Buffer
*
* This functions copies the a to the char buffer and terminates it with 0.
*
* \param str String
* \param MaxLen Max length of the buffer
* \param buf Pointer to the buffer
*/
static void SetCharBuf(const char *str, size_t MaxLen, char *buf);
/************************************************************************//**
* \brief Setting up a clean Char Buffer
*
* This functions clears an existing buffer using \ref ClearCharBuf,
* copies the a string to the char buffer and terminates it with 0.
*
* \param str String
* \param MaxLen Max length of the buffer
* \param buf Pointer to the buffer
*/
static void ClearSetCharBuf(const char *str, size_t MaxLen, char *buf);
/************************************************************************//**
* \brief Delivers Max out of A an B
*
* As max and min are not available on all systems, so use own definition.
*
* \tparam T Datatype
* \param a Value A
* \param b Value B
* \return T
*/
template <typename T> static T N2kMax(T a, T b) { return (a>b?a:b); }
/************************************************************************//**
* \brief Delivers Min out of A an B
*
* As max and min are not available on all systems, so use own definition.
*
* \tparam T Datatype
* \param a Value A
* \param b Value B
* \return T
*/
template <typename T> static T N2kMin(T a, T b) { return (a<b?a:b); }
/************************************************************************//**
* \struct tProductInformation
* \brief Structure that holds all the product information
*
* It is important that every device has proper product information
* available. This struct holds all the data and provides several
* helper functions.
*
*/
struct tProductInformation {
/** \brief Version of NMEA2000 Standard that is supported */
unsigned short N2kVersion;
/** \brief Product Code of the device*/
unsigned short ProductCode;
//
/** \brief Max length of ModelID
* Note that we reserve one extra char for null termination */
char N2kModelID[Max_N2kModelID_len+1];
/** \brief Max length of Software Code
* Note that we reserve one extra char for null termination */
char N2kSwCode[Max_N2kSwCode_len+1];
/** \brief Max length of Model Version
* Note that we reserve one extra char for null termination */
char N2kModelVersion[Max_N2kModelVersion_len+1];
/** \brief Max length of Serial Code
* Note that we reserve one extra char for null termination */
char N2kModelSerialCode[Max_N2kModelSerialCode_len+1];
/** \brief Certification level of the device*/
unsigned char CertificationLevel;
/** \brief Load Equivalency of the device
* A Load Equivalence Number express the amount of current that
* is drawn from an NMEA 2000 network. 1 equals to 50mA. If
* a device draws 151mA of current from the network, then its
* LEN is 4
*/
unsigned char LoadEquivalency;
/********************************************************************//**
* \brief Set all the product information data of the structure
*
* \param _ModelSerialCode Manufacturer's Model serial code,
* default="". Max 32 chars.
* \param _ProductCode Manufacturer's product code,
* default=666
* \param _ModelID Manufacturer's Model ID,
* default="". Max 33 chars
* \param _SwCode Manufacturer's software version code,
* default="". Max 40 chars
* \param _ModelVersion Manufacturer's Model version
* default="". Max 24 chars
* \param _LoadEquivalency Load equivalency ( x * 50mA, default=1)
* \param _N2kVersion N2k Standard version, default=2101
* \param _CertificationLevel Certification level, default=0
*/
void Set(const char *_ModelSerialCode,
unsigned short _ProductCode=0xffff,
const char *_ModelID=0,
const char *_SwCode=0,
const char *_ModelVersion=0,
unsigned char _LoadEquivalency=0xff,
unsigned short _N2kVersion=0xffff,
unsigned char _CertificationLevel=0xff
) {
N2kVersion=(_N2kVersion!=0xffff?_N2kVersion:2101);
ProductCode=_ProductCode;
ClearSetCharBuf(_ModelID,sizeof(N2kModelID),N2kModelID);
ClearSetCharBuf(_SwCode,sizeof(N2kSwCode),N2kSwCode);
ClearSetCharBuf(_ModelVersion,sizeof(N2kModelVersion),N2kModelVersion);
ClearSetCharBuf(_ModelSerialCode,sizeof(N2kModelSerialCode),N2kModelSerialCode);
CertificationLevel=(_CertificationLevel!=0xff?_CertificationLevel:0);
LoadEquivalency=(_LoadEquivalency!=0xff?_LoadEquivalency:1);
}
/*******************************************************************//**
* \brief Clears out all data */
void Clear();
/*******************************************************************//**
* \brief Compares two product information structures
*
* \param Other An other product information structure
* \retval true
* \retval false
*/
bool IsSame(const tProductInformation &Other);
};
/**********************************************************************//**
* \class tDeviceInformation
* \brief Class that holds all the device informations and several
* helper functions to that
*/
class tDeviceInformation {
protected:
/********************************************************************//**
* \union tUnionDeviceInformation
* \brief Union that holds the device informations
*
*/
typedef union {
/** \brief Devicename */
uint64_t Name;
/******************************************************************//**
* \brief Structure for device information
*/
struct {
/** \brief 32 bit number carrying Unique Number and Manufacturer
* Code
*
* ManufacturerCode 11 bits , UniqueNumber 21 bits
*/
uint32_t UnicNumberAndManCode; //
/** \brief Device instance number */
unsigned char DeviceInstance;
/** \brief Device function code
*
* see for Details: [NMEA2000 Device and Function Codes](https://web.archive.org/web/20190531120557/https://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf)
*
*/
unsigned char DeviceFunction;
/** \brief Device class
*
* see for Details: [NMEA2000 Device and Function Codes](https://web.archive.org/web/20190531120557/https://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf)
*
*/
unsigned char DeviceClass;
/** \brief Industry Group and System Instance (each 4bits)
*
* I found document:
* http://www.novatel.com/assets/Documents/Bulletins/apn050.pdf it
* says about next fields:
* The System Instance Field can be utilized to facilitate multiple
* NMEA 2000 networks on these larger marine platforms. NMEA 2000
* devices behind a bridge, router, gateway, or as part of some
* network segment could all indicate this by use and application
* of the System Instance Field. DeviceInstance and SystemInstance
* fields can be now changed by function
* \ref SetDeviceInformationInstances or by NMEA 2000 group
* function. Group function handling is build in the library.
*/
unsigned char IndustryGroupAndSystemInstance;
};
} tUnionDeviceInformation;
/** \brief Union that contains all the Device Information */
tUnionDeviceInformation DeviceInformation;
public:
/*******************************************************************//**
* \brief Construct a new empty Device Information object
*/
tDeviceInformation() { DeviceInformation.Name=0; }
/*******************************************************************//**
* \brief Set a unique Number to the Device Information
* \param _UniqueNumber a unique number for the device (max 21 bits)
*/
void SetUniqueNumber(uint32_t _UniqueNumber) { DeviceInformation.UnicNumberAndManCode=(DeviceInformation.UnicNumberAndManCode&0xffe00000) | (_UniqueNumber&0x1fffff); }
/*******************************************************************//**
* \brief Get the unique Number from the Device Information
* \return uint32_t
*/
uint32_t GetUniqueNumber() const { return DeviceInformation.UnicNumberAndManCode&0x1fffff; }
/*******************************************************************//**
* \brief Set the Manufacturer Code to the Device Information
* \param _ManufacturerCode Manufacturer Code (max 11 bits)
*/
void SetManufacturerCode(uint16_t _ManufacturerCode) { DeviceInformation.UnicNumberAndManCode=(DeviceInformation.UnicNumberAndManCode&0x1fffff) | (((unsigned long)(_ManufacturerCode&0x7ff))<<21); }
/*******************************************************************//**
* \brief Get the Manufacturer Code from the Device Information
* \return uint16_t
*/
uint16_t GetManufacturerCode() const { return DeviceInformation.UnicNumberAndManCode>>21; }
/*******************************************************************//**
* \brief Set the Device Instance to the Device Information
* \param _DeviceInstance Instance for the device
*/
void SetDeviceInstance(unsigned char _DeviceInstance) { DeviceInformation.DeviceInstance=_DeviceInstance; }
/*******************************************************************//**
* \brief Get the Device Instance from the Device Information
* \return unsigned char
*/
unsigned char GetDeviceInstance() const { return DeviceInformation.DeviceInstance; }
/*******************************************************************//**
* \brief Get the Device Instance (lower bits) from the Device Information
* \return unsigned char
*/
unsigned char GetDeviceInstanceLower() const { return DeviceInformation.DeviceInstance & 0x07; }
/*******************************************************************//**
* \brief Get the Device Instance (upper bits) from the Device Information
* \return unsigned char
*/
unsigned char GetDeviceInstanceUpper() const { return (DeviceInformation.DeviceInstance>>3) & 0x1f; }
/*******************************************************************//**
* \brief Set the Device Function to the Device Information
* \param _DeviceFunction Device function code, \ref
* tDeviceInformation::tUnionDeviceInformation::DeviceFunction
*/
void SetDeviceFunction(unsigned char _DeviceFunction) { DeviceInformation.DeviceFunction=_DeviceFunction; }
/*******************************************************************//**
* \brief Get the Device Function from the Device Information
* \return unsigned char -> Device function code, \ref
* tDeviceInformation::tUnionDeviceInformation::DeviceFunction
*/
unsigned char GetDeviceFunction() const { return DeviceInformation.DeviceFunction; }
/*******************************************************************//**
* \brief Set the Device Class to the Device Information
* \param _DeviceClass Device class code, \ref
* tDeviceInformation::tUnionDeviceInformation::DeviceClass
*/
void SetDeviceClass(unsigned char _DeviceClass) { DeviceInformation.DeviceClass=((_DeviceClass&0x7f)<<1); }
/*******************************************************************//**
* \brief Get the Device Class from the Device Information
* \return unsigned char -> Device class code, \ref
* tDeviceInformation::tUnionDeviceInformation::DeviceClass
*/
unsigned char GetDeviceClass() const { return DeviceInformation.DeviceClass>>1; }
/********************************************************************//**
* \brief Set the Industry Group to the Device Information
* \param _IndustryGroup Industry Group
*/
void SetIndustryGroup(unsigned char _IndustryGroup) { DeviceInformation.IndustryGroupAndSystemInstance=(DeviceInformation.IndustryGroupAndSystemInstance&0x0f) | (_IndustryGroup<<4) | 0x80; }
/********************************************************************//**
* \brief Get the Industry Group from the Device Information
* \return unsigned char
*/
unsigned char GetIndustryGroup() const { return (DeviceInformation.IndustryGroupAndSystemInstance>>4) & 0x07; }
/********************************************************************//**
* \brief Set the System Instance to the Device Information
* \param _SystemInstance System Instance
*/
void SetSystemInstance(unsigned char _SystemInstance) { DeviceInformation.IndustryGroupAndSystemInstance=(DeviceInformation.IndustryGroupAndSystemInstance&0xf0) | (_SystemInstance&0x0f); }
/********************************************************************//**
* \brief Get the System Instance from the Device Information
* \return unsigned char
*/
unsigned char GetSystemInstance() const { return DeviceInformation.IndustryGroupAndSystemInstance&0x0f; }
/*** ****************************************************************//**
* \brief Get the Name from the Device Information
* \return uint64_t
*/
uint64_t GetName() const { return DeviceInformation.Name; }
/********************************************************************//**
* \brief Set the Name to the Device Information
* \param _Name Name of the device
*/
void SetName(uint64_t _Name) { DeviceInformation.Name=_Name; }
/********************************************************************//**
* \brief Check if two devices are the same, by comparing the device NAME
* \param Other NAME of the other device
* \retval true
* \retval false
*/
inline bool IsSame(uint64_t Other) { return GetName()==Other; }
};
/************************************************************************//**
* \class tDevice
* \brief This class represents a N2k device
*
*/
class tDevice {
protected:
/** \brief This object holds all necessary device informations*/
tDeviceInformation DevI;
/** \brief Timestamp when this device was created*/
unsigned long CreateTime;
/** \brief Source address on bus for this device*/
uint8_t Source;
public:
/*******************************************************************//**
* \brief Construct a new Device object
*
* Initialize all the attributes of the device.
*
* \param _Name Name of the device
* \param _Source Source address on the bus (default = 255)
*/
tDevice(uint64_t _Name, uint8_t _Source=255) { Source=_Source; DevI.SetName(_Name); CreateTime=N2kMillis(); }
/*******************************************************************//**
* \brief Destroy the Device object */
virtual ~tDevice() {;}
/** \brief Returns the Source Address of this device*/
uint8_t GetSource() const { return Source; }
/** \brief Returns the Time of Creation of this device*/
unsigned long GetCreateTime() const { return CreateTime; }
/******************************************************************//**
* \brief Get the Name of this device
* \return uint64_t
*/
inline uint64_t GetName() const { return DevI.GetName(); }
/******************************************************************//**
* \brief Check if two devices are the same, by comparing the device name
* \param Other Name of the other device
* \retval true
* \retval false
*/
inline bool IsSame(uint64_t Other) { return DevI.IsSame(Other); }
/*******************************************************************//**
* \brief Get the unique Number from the Device Information
* \return uint32_t
*/
inline uint32_t GetUniqueNumber() const { return DevI.GetUniqueNumber(); }
/*******************************************************************//**
* \brief Get the Manufacturer Code from the Device Information
* \return uint16_t
*/
inline uint16_t GetManufacturerCode() const { return DevI.
GetManufacturerCode(); }
/*******************************************************************//**
* \brief Get the Device Instance from the Device Information
* \return unsigned char
*/
inline unsigned char GetDeviceInstance() const { return DevI.GetDeviceInstance(); }
/*******************************************************************//**
* \brief Get the Device Instance (lower bits) from the Device Information
* \return unsigned char
*/
inline unsigned char GetDeviceInstanceLower() const { return DevI.GetDeviceInstanceLower(); }
/*******************************************************************//**
* \brief Get the Device Instance (upper bits) from the Device Information
* \return unsigned char
*/
inline unsigned char GetDeviceInstanceUpper() const { return DevI.GetDeviceInstanceUpper(); }
/*******************************************************************//**
* \brief Get the Device Function from the Device Information
* \return unsigned char -> Device function code, \ref
* tDeviceInformation::tUnionDeviceInformation::DeviceFunction
*/
inline unsigned char GetDeviceFunction() const { return DevI.GetDeviceFunction(); }
/*******************************************************************//**
* \brief Get the Device Class from the Device Information
* \return unsigned char -> Device class code, \ref
* tDeviceInformation::tUnionDeviceInformation::DeviceClass
*/
inline unsigned char GetDeviceClass() const { return DevI.GetDeviceClass(); }
/********************************************************************//**
* \brief Get the Industry Group from the Device Information
* \return unsigned char
*/
inline unsigned char GetIndustryGroup() const { return DevI.GetIndustryGroup(); }
/********************************************************************//**
* \brief Get the System Instance from the Device Information
* \return unsigned char
*/
inline unsigned char GetSystemInstance() const { return DevI.GetSystemInstance(); }
// Product information
/** \brief Get N2k Standard version from the product information
* of this device*/
virtual unsigned short GetN2kVersion() const=0;
/** \brief Get the product code from the product information of
* this device*/
virtual unsigned short GetProductCode() const=0;
/** \brief Get the model ID from the product information of this device*/
virtual const char * GetModelID() const=0;
/** \brief Get the Software version code from the product
* information of this device*/
virtual const char * GetSwCode() const=0;
/** \brief Get the model version from the product information
* of this device*/
virtual const char * GetModelVersion() const=0;
/** \brief Get the model serial code from the product information
* of this device*/
virtual const char * GetModelSerialCode() const=0;
/** \brief Get the certification level from the product information
* of this device*/
virtual unsigned short GetCertificationLevel() const=0;
/** \brief Get the load equivalency from the product information
* of this device*/
virtual unsigned short GetLoadEquivalency() const=0;
// Configuration information
/** \brief Get the manufacturer information from the configuration
* information of this device*/
virtual const char * GetManufacturerInformation() const { return 0; }
/** \brief Get the installation description 1 from the configuration
* information of this device*/
virtual const char * GetInstallationDescription1() const { return 0; }
/** \brief Get the installation description 2 from the configuration
* information of this device*/
virtual const char * GetInstallationDescription2() const { return 0; }
/** \brief Get the list of transmitted PGNs from this device*/
virtual const unsigned long * GetTransmitPGNs() const { return 0; }
/** \brief Get the list of received PGNs from this device*/
virtual const unsigned long * GetReceivePGNs() const { return 0; }
};
/************************************************************************//**
* \class tMsgHandler
* \brief Message handler class
*
*/
class tMsgHandler {
private:
friend class tNMEA2000;
/** \brief Pointer to a message handler*/
tMsgHandler *pNext;
/** \brief Pointer to a tNMEA2000 object*/
tNMEA2000 *pNMEA2000;
protected:
/** Parameter Group Number*/
unsigned long PGN;
/*******************************************************************//**
* \brief Handles a given message *
* \param N2kMsg Reference to a N2kMsg Object
*/
virtual void HandleMsg(const tN2kMsg &N2kMsg)=0;
/** \brief Returns the tNMEA2000 object of this handler
* \return tNMEA2000 */
tNMEA2000 *GetNMEA2000() { return pNMEA2000; }
public:
/******************************************************************//**
* \brief Construct a new Message Handler object
*
* Attaches this message handler to a tNMEA2000 object.
*
* \param _PGN PGN of the message that should be handled
* \param _pNMEA2000 Pointer to tNMEA2000 object, where the handle
* should be attached
*/
tMsgHandler(unsigned long _PGN=0, tNMEA2000 *_pNMEA2000=0) {
PGN=_PGN; pNext=0; pNMEA2000=0;
if ( _pNMEA2000!=0 ) _pNMEA2000->AttachMsgHandler(this);
}
/*******************************************************************//**
* \brief Destroys the Message Handler object
*/
virtual ~tMsgHandler() { if ( pNMEA2000!=0 ) pNMEA2000->DetachMsgHandler(this); }
/*******************************************************************//**
* \brief Return the PGN that is handled by this message handler
* \return unsigned long
*/
inline unsigned long GetPGN() const { return PGN; }
};
public:
/************************************************************************//**
* \enum tForwardType
* \brief Type how to forward messages in listen mode
*/
typedef enum {
/** Forwards messages to output port in Actisense format. Note
* that some Navigation sw uses this. */
fwdt_Actisense,
/** Forwards messages to output port in clear text. This is e.g.
* for debugging. */
fwdt_Text
} tForwardType;
/************************************************************************//**
* \enum tN2kMode
* \brief System mode defines how the device will behave on the
* NMEA2000 bus
*
* \sa
* - \ref secDeviceModes
* - \ref tNMEA2000::SetMode()
*/
typedef enum {
/** Default mode. Listen bus and if message forwarding has been
* enabled forwards messages to defined stream in Actisense format.
* You can not send any data to the bus.
* See example DataDisplay.ino.
*/
N2km_ListenOnly,
/** In this mode device acts as active bus device like display,
* RPM or temperature monitor. Remember to set right device
* information first. Messages will not be forwarded.
* Look example TemperatureMonitor.ino.
*/
N2km_NodeOnly,
/** Mode is as N2km_NodeOnly plus you can control
* \ref secMessageforwarding "message forwarding".
*/
N2km_ListenAndNode,
/** Only for message sending. Device will not inform itself
* to the bus. Messages will not be forwarded to the stream.
* By setting message handler, you can still read messages
* and handle them by yourself.
*/
N2km_SendOnly,
/** Listen bus and forwards messages to given forward stream in
* Actisense format. Messages can be send. Device will not
* inform itself to the bus. This is mode is usable e.g., with
* NMEA Simulator <http://www.kave.fi/Apps/>.
*/
N2km_ListenAndSend
} tN2kMode;
/************************************************************************//**
* \enum tDebugMode
* \brief For debugging we have some cases for SendMsg
*/
typedef enum {
/** Directs data to CAN bus
*/
dm_None,
/** Directs sent data to serial in clear text format
*/
dm_ClearText,
/** Directs sent data to serial as Actisense format.
*/
dm_Actisense,
} tDebugMode;
/************************************************************************//**
* \struct tConfigurationInformation
* \brief Structure that holds the Configuration Information of this device
*/
struct tConfigurationInformation {
/** \brief pointer to char array holding the manufacturer information */
const char *ManufacturerInformation;
/** \brief pointer to char array holding the installation description 1 */
const char *InstallationDescription1;
/** \brief pointer to char array holding the installation description 2 */
const char *InstallationDescription2;
};
protected:
/************************************************************************//**
* \enum tOpenState
* \brief Library open state.
*
* Open state is internal library open state used to avoid any blocking call.
*/
typedef enum {
/** Open has not yet been called. Buffers are still uninitialized.
*/
os_None ///< State none
/** Trying to open CAN driver.
*/
,os_OpenCAN ///< State Open CAN
/** CAN driver open succeed. Wait CAN driver stabilize.
*/
,os_WaitOpen ///< State Wait Open
/** Open and running.
*/
,os_Open ///< State Open
} tOpenState;
/************************************************************************//**
* \class tInternalDevice
* \brief This class represents an internal device
*
*/
class tInternalDevice {
public:
/** \brief Source address of this device on the NMEA2000 bus*/
uint8_t N2kSource;
/** \brief This holds all the Device Informations for this specific device*/
tDeviceInformation DeviceInformation;
// Product information
/** \brief This holds all the Product Informations for this
* specific device*/
const tProductInformation *ProductInformation;
/** \brief This holds all the local (???) Product Informations for this
* specific device*/
tProductInformation *LocalProductInformation;
/** \brief This holds the Manufacturer Code for this
* specific device*/
char *ManufacturerSerialCode;
/** \brief Timestamp set while last \ref tNMEA2000::SendIsoAddressClaim
* was executed*/
tN2kScheduler PendingIsoAddressClaim;
/** \brief Timestamp set while last \ref tNMEA2000::SendProductInformation
* was executed*/
tN2kScheduler PendingProductInformation;
/** \brief Timestamp set while last \ref
* tNMEA2000::SendConfigurationInformation was executed */
tN2kScheduler PendingConfigurationInformation;
/** \brief Timer value for AddressClaim
*/
tN2kScheduler AddressClaimTimer;
/** \brief Pointer to a buffer that holds all supported transmit
* PGNs for this device*/
const unsigned long *TransmitMessages;
/** \brief Pointer to a buffer that holds all supported receive
* PGNs for this device*/
const unsigned long *ReceiveMessages;
/** \brief Fast packet PGNs sequence counters*/
unsigned long *PGNSequenceCounters;
/** \brief Fast packet PGNs sequence counters*/
size_t MaxPGNSequenceCounters;
/** \brief Holds the highest source address for Address Claim process*/
uint8_t AddressClaimEndSource;
/** \brief internal device has pending information*/
bool HasPendingInformation;
#if !defined(N2K_NO_ISO_MULTI_PACKET_SUPPORT)
/** \brief Pending N2k message */
tN2kMsg PendingTPMsg;
/** \brief Timestamp, when next data packet can be send on TP broadcast*/
tN2kScheduler NextDTSendTime;
/** \brief Next Sequence*/
uint8_t NextDTSequence;
#endif
#if !defined(N2K_NO_HEARTBEAT_SUPPORT)
/** \brief Interval for Heartbeat */
#define DefaultHeartbeatInterval 60000
/** \brief Scheduler for the heartbeat message */
tN2kSyncScheduler HeartbeatScheduler;
/** \brief Heartbeat Sequence */
uint8_t HeartbeatSequence;
#endif
public:
/********************************************************************//**
* \brief Construct a new Internal Device object
*
* Initialize all the attributes of this internal device.
*/
tInternalDevice() {
N2kSource=0;
HasPendingInformation=false;
ProductInformation=0; LocalProductInformation=0; ManufacturerSerialCode=0;
AddressClaimEndSource=N2kMaxCanBusAddress; //GetNextAddressFromBeginning=true;
TransmitMessages=0; ReceiveMessages=0;
PGNSequenceCounters=0; MaxPGNSequenceCounters=0;
#if !defined(N2K_NO_ISO_MULTI_PACKET_SUPPORT)
NextDTSequence=0;
#endif
#if !defined(N2K_NO_HEARTBEAT_SUPPORT)
HeartbeatSequence=0;
#endif
}
/*********************************************************************//**
* \brief Set the timestamp for Pending an ISO Address Claim message
* \param FromNow Variable time delay in ms
* \sa tNMEA2000::SendIsoAddressClaim
*/
void SetPendingIsoAddressClaim(unsigned long FromNow=2) { PendingIsoAddressClaim.FromNow(FromNow); HasPendingInformation=true; }
/*********************************************************************//**
* \brief Check if enough time has passed
*
* Checks if enough time has passed since \ref
* SetPendingIsoAddressClaim, so that \ref SendIsoAddressClaim can
* be executed.
*
* \retval true enough time has passed
* \retval false
*/
bool QueryPendingIsoAddressClaim() { return PendingIsoAddressClaim.IsTime(); }
/** \brief Resets \ref PendingIsoAddressClaim to zero*/
void ClearPendingIsoAddressClaim() { PendingIsoAddressClaim.Disable(); UpdateHasPendingInformation(); }
/*********************************************************************//**
* \brief Set the timestamp for Pending a ProductInformation message
*
* This function use strange delays to avoid synchronization of many
* messages.
* \sa tNMEA2000::SendProductInformation
*/
void SetPendingProductInformation() { PendingProductInformation.FromNow(187+N2kSource*8); HasPendingInformation=true; } // Use strange increment to avoid synchronize
/** \brief Resets \ref PendingProductInformation to zero*/
void ClearPendingProductInformation() { PendingProductInformation.Disable(); UpdateHasPendingInformation(); }
/*********************************************************************//**
* \brief Check if enough time has passed
*
* Checks if enough time has passed since \ref
* SetPendingProductInformation, so that \ref SendProductInformation can
* be executed.
*
* \retval true enough time has passed
* \retval false
*/
bool QueryPendingProductInformation() { return PendingProductInformation.IsTime(); }
/*********************************************************************//**
* \brief Set the timestamp for Pending a ConfigurationInformation message
*
* This function use strange delays to avoid synchronization of many
* messages.
* \sa tNMEA2000::SendConfigurationInformation
*/
void SetPendingConfigurationInformation() { PendingConfigurationInformation.FromNow(187+N2kSource*10); HasPendingInformation=true; } // Use strange increment to avoid synchronize
/** \brief Resets \ref PendingConfigurationInformation to zero*/
void ClearPendingConfigurationInformation() { PendingConfigurationInformation.Disable(); UpdateHasPendingInformation(); }
/*********************************************************************//**
* \brief Check if enough time has passed
*
* Checks if enough time has passed since \ref
* SetPendingConfigurationInformation, so that \ref
* SendConfigurationInformation can be executed.
*
* \retval true enough time has passed
* \retval false
*/
bool QueryPendingConfigurationInformation() { return PendingConfigurationInformation.IsTime(); }
/*********************************************************************//**
* \brief Updates \ref AddressClaimEndSource
*/
void UpdateAddressClaimEndSource() {
AddressClaimEndSource=N2kSource;
if ( AddressClaimEndSource>0 ) { AddressClaimEndSource--; } else { AddressClaimEndSource=N2kMaxCanBusAddress; }
}
void UpdateHasPendingInformation() {
HasPendingInformation= PendingIsoAddressClaim.IsEnabled()
| PendingProductInformation.IsEnabled()
| PendingConfigurationInformation.IsEnabled()
#if !defined(N2K_NO_ISO_MULTI_PACKET_SUPPORT)
| NextDTSendTime.IsEnabled()
#endif
;
}
};
protected:
/** \brief Forward mode bit: -> If set, forward is enabled */
static const int FwdModeBit_EnableForward = BIT(0);
/** \brief Forward mode bit: -> System messages will be forwarded */
static const int FwdModeBit_SystemMessages = BIT(1);
/** \brief Forward mode bit: -> Only known messages will be forwarded.
* System messages will be forwarded according its own bit*/
static const int FwdModeBit_OnlyKnownMessages = BIT(2);
/** \brief Forward mode bit: -> Forward also all messages,
* what this device will send */
static const int FwdModeBit_OwnMessages = BIT(3);
/** \brief Forward mode bit: -> Only known messages will be handled */
static const int HandleModeBit_OnlyKnownMessages = BIT(4);
protected:
/** \brief Attribute that holds the actual Debug Mode (default = md_none)*/
tDebugMode dbMode;
/** \brief Actual operation mode of this device (default =
* N2km_ListenOnly)
*/
tN2kMode N2kMode;
/** \brief Actual message forward type (default = fwdt_Actisense)*/
tForwardType ForwardType;
/** \brief Actual message forward operation mode
* (default = all messages - also system and own)*/
unsigned int ForwardMode;
/** \brief Actual stream to be used for forward messaging*/
N2kStream *ForwardStream;
/** \brief Pointer to a buffer for Message Handlers*/
tMsgHandler *MsgHandlers;
/** Open the Scheduler */
tN2kScheduler OpenScheduler;
/** State of the .... */
tOpenState OpenState;
/** \brief Flag that the address has changed */
bool AddressChanged;
/** \brief Flag that the device information has changed */
bool DeviceInformationChanged;
/** \brief Pointer to a buffer for all internal devices */
tInternalDevice *Devices;
/** \brief Number of devices */
int DeviceCount;
// unsigned long N2kSource[Max_N2kDevices];