NMEA2000 Library  0.1
Library to handle NMEA 2000 Communication written in C++
N2kDeviceList.h
Go to the documentation of this file.
1/*
2 * N2kDeviceList.h
3 * Copyright (c) 2015-2024 Timo Lappalainen, Kave Oy, www.kave.fi
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21*/
22
23/*************************************************************************/
30#ifndef _N2kDeviceList_H_
31#define _N2kDeviceList_H_
32
33#include "NMEA2000.h"
34
38#define N2kMaxBusDevices 254
39
42#define N2kDL_TimeForFirstRequest 1000
43
45#define N2kDL_TimeBetweenPIRequest 1000
46
48#define N2kDL_TimeBetweenCIRequest 1000
49
50/************************************************************************/
73 protected:
74 /**********************************************************************/
81 protected:
86
90 uint16_t ConfISize;
92 uint16_t ManISize;
94 uint16_t InstDesc1Size;
96 uint16_t InstDesc2Size;
97
99 char *ConfI;
106
110 unsigned long *TransmitPGNs;
114 unsigned long *ReceivePGNs;
115
116 public:
120 unsigned long ProdIRequested;
125 unsigned long ConfIRequested;
130 unsigned long PGNsRequested;
133
135 unsigned long LastMessageTime;
136
137 public:
138 /******************************************************************/
146 tInternalDevice(uint64_t _Name, uint8_t _Source=255);
147 /********************************************** *******************/
152
153 /******************************************************************/
158 void SetSource(uint8_t _Source) { Source=_Source; }
159
160 /******************************************************************/
165 void SetDeviceInformation(uint64_t _Name) { DevI.SetName(_Name); }
166
167 /*******************************************************************/
183 void SetProductInformation(const char *_ModelSerialCode,
184 unsigned short _ProductCode=0xffff,
185 const char *_ModelID=0,
186 const char *_SwCode=0,
187 const char *_ModelVersion=0,
188 unsigned char _LoadEquivalency=0xff,
189 unsigned short _N2kVersion=0xffff,
190 unsigned char _CertificationLevel=0xff
191 ) {
192 ProdI.Set(_ModelSerialCode,_ProductCode,_ModelID,_SwCode,_ModelVersion,_LoadEquivalency,_N2kVersion,_CertificationLevel);
193 ProdILoaded=true;
194 }
195
196 /******************************************************************/
201 bool HasProductInformation() const { return ProdILoaded; }
202 /******************************************************************/
206 unsigned short GetN2kVersion() const { return ProdI.N2kVersion; }
207 /******************************************************************/
211 unsigned short GetProductCode() const { return ProdI.ProductCode; }
212 /******************************************************************/
216 const char * GetModelID() const { return ProdI.N2kModelID; }
217 /******************************************************************/
221 const char * GetSwCode() const { return ProdI.N2kSwCode; }
222 /******************************************************************/
226 const char * GetModelVersion() const { return ProdI.N2kModelVersion; }
227 /******************************************************************/
231 const char * GetModelSerialCode() const { return ProdI.N2kModelSerialCode; }
232 /******************************************************************/
236 unsigned short GetCertificationLevel() const { return ProdI.CertificationLevel; }
237 /******************************************************************/
241 unsigned short GetLoadEquivalency() const { return ProdI.LoadEquivalency; }
242
243 /******************************************************************/
250 /******************************************************************/
255 /******************************************************************/
260 /******************************************************************/
265
266 /******************************************************************/
279 char * InitConfigurationInformation(size_t &_ManISize, size_t &_InstDesc1Size, size_t &_InstDesc2Size);
280
281 /******************************************************************/
286 /******************************************************************/
291 /******************************************************************/
296
297 /******************************************************************/
301 const unsigned long * GetTransmitPGNs() const { return TransmitPGNs; }
302 /******************************************************************/
306 const unsigned long * GetReceivePGNs() const { return ReceivePGNs; }
307
308 /******************************************************************/
315 unsigned long * InitTransmitPGNs(uint8_t count);
316
317 /******************************************************************/
324 unsigned long * InitReceivePGNs(uint8_t count);
325
326 /******************************************************************/
333 bool ShouldRequestName() { return GetName()==0 && nNameRequested<20; }
334 /****************************************************************/
338 /****************************************************************/
342 /****************************************************************/
345 /******************************************************************/
354 /******************************************************************/
364 /****************************************************************/
368 /****************************************************************/
374 if (ProdI.IsSame(Other)) {
375 ProdILoaded=true; return true;
376 } else {
377 return false;
378 }
379 }
380
381 /****************************************************************/
385 /******************************************************************/
394 /******************************************************************/
405 /****************************************************************/
410
411 /****************************************************************/
415 /******************************************************************/
423 bool ShouldRequestPGNList() { return ( ((TransmitPGNs==0) || (ReceivePGNs==0)) && nPGNsRequested<4 ); } // We do not have it and not tried enough
424 /****************************************************************/
429 /******************************************************************/
439 }; // tInternalDevice
440
441 protected:
442 /********************************************************************/
447 uint8_t MaxDevices;
452
453 protected:
454 /********************************************************************/
468 void HandleIsoAddressClaim(const tN2kMsg &N2kMsg);
469
470 /********************************************************************/
480 void HandleProductInformation(const tN2kMsg &N2kMsg);
481 /********************************************************************/
491 void HandleConfigurationInformation(const tN2kMsg &N2kMsg);
492 /********************************************************************/
505 void HandleSupportedPGNList(const tN2kMsg &N2kMsg);
506 /********************************************************************/
515 void HandleOther(const tN2kMsg &N2kMsg);
516 /********************************************************************/
523 /********************************************************************/
530 /********************************************************************/
539 tN2kDeviceList::tInternalDevice * LocalFindDeviceByIDs(uint16_t ManufacturerCode, uint32_t UniqueNumber) const;
540 /********************************************************************/
551 tN2kDeviceList::tInternalDevice * LocalFindDeviceByProduct(uint16_t ManufacturerCode, uint16_t ProductCode, uint8_t Source=0xff) const;
552 /********************************************************************/
562 bool RequestProductInformation(uint8_t Source);
563 /********************************************************************/
574 bool RequestConfigurationInformation(uint8_t Source);
575 /********************************************************************/
585 bool RequestSupportedPGNList(uint8_t Source);
586 /********************************************************************/
593 bool RequestIsoAddressClaim(uint8_t Source);
594 /********************************************************************/
599 void AddDevice(uint8_t Source);
600 /********************************************************************/
606 void SaveDevice(tInternalDevice *pDevice, uint8_t Source);
607
608 public:
609 /********************************************************************/
616 tN2kDeviceList(tNMEA2000 *_pNMEA2000);
617 /********************************************************************/
629 void HandleMsg(const tN2kMsg &N2kMsg);
630
631 //
632 /********************************************************************/
640 const tNMEA2000::tDevice * FindDeviceBySource(uint8_t Source) const { return LocalFindDeviceBySource(Source); }
641
642 // Return device last message time in milliseconds.
643 unsigned long GetDeviceLastMessageTime(uint8_t Source) const {
645 return ( dev!=0?dev->LastMessageTime:0 );
646 }
647
648 //
649 /********************************************************************/
661 const tNMEA2000::tDevice * FindDeviceByName(uint64_t NAME) const { return LocalFindDeviceByName(NAME); }
662
663 /********************************************************************/
675 const tNMEA2000::tDevice * FindDeviceByIDs(uint16_t ManufacturerCode, uint32_t UniqueNumber) const { return LocalFindDeviceByIDs(ManufacturerCode, UniqueNumber); }
676
677 /********************************************************************/
694 const tNMEA2000::tDevice * FindDeviceByProduct(uint16_t ManufacturerCode, uint16_t ProductCode, uint8_t Source=0xff) const { return LocalFindDeviceByProduct(ManufacturerCode, ProductCode, Source); }
695
696 /************************************************************************/
706 bool ReadResetIsListUpdated() { if ( ListUpdated ) { ListUpdated=false; return true; } else { return false; } }
707
708 /************************************************************************/
713 uint8_t Count() const;
714};
715
716#endif
#define N2kDL_TimeBetweenCIRequest
Time in ms between configuration information requests.
Definition: N2kDeviceList.h:48
#define N2kMaxBusDevices
Maximum allowed number of devices on the CAN BUS bus system is 254.
Definition: N2kDeviceList.h:38
#define N2kDL_TimeBetweenPIRequest
Time in ms between product information requests.
Definition: N2kDeviceList.h:45
#define N2kDL_TimeForFirstRequest
Time in ms for first request after device has been noticed on the bus.
Definition: N2kDeviceList.h:42
uint32_t N2kMillis()
Definition: N2kTimer.cpp:48
bool N2kHasElapsed(uint32_t Start, uint32_t Elapsed, uint32_t Now=N2kMillis())
Has time elapsed since start.
Definition: N2kTimer.h:111
This file contains the class tNMEA2000, which consists the main functionality of the library.
This class represents an internal device.
Definition: N2kDeviceList.h:80
char * InstallationDescription1
Pointer to the Installation Description 1.
void ClearConfigurationInformationLoaded()
Resets the Configuration Information Loaded values of the device.
unsigned short GetLoadEquivalency() const
Get the Load Equivalency (x * 50 mA) of this device.
uint16_t InstDesc1Size
Size of the Installation Description 1 (number of bytes)
Definition: N2kDeviceList.h:94
unsigned long * InitReceivePGNs(uint8_t count)
Initialize an array for received PGNs This functions frees if needed old reservation and reserves new...
bool ShouldRequestProductInformation()
Should the Device Product Information be requested As long as the device Product Information is not s...
const char * GetInstallationDescription1() const
Get the Installation Description 1 of this device.
const char * GetSwCode() const
Get the Software Code of this device.
uint8_t nProdIRequested
How many times we have requested the Product information.
bool ProdILoaded
Product Information has been loaded.
Definition: N2kDeviceList.h:83
const char * GetInstallationDescription2() const
Get the Installation Description 2 of this device.
const char * GetModelVersion() const
Get the Model Version of this device.
const unsigned long * GetReceivePGNs() const
Get the received PGNs of this device.
void SetDeviceInformation(uint64_t _Name)
Set the Device Information.
uint16_t InstDesc2Size
Size of the Installation Description 2 (number of bytes)
Definition: N2kDeviceList.h:96
void SetConfigurationInformationRequested()
Increments the Number of how often the Configuration Information has already been requested and store...
unsigned short GetProductCode() const
Get the Product code of this device.
uint8_t nConfIRequested
How many times we have requested the Config information.
char * GetManufacturerInformation()
Get the Manufacturer Information of this device.
char * GetInstallationDescription2()
Get the Installation Description 2 of this device.
const char * GetModelSerialCode() const
Get the Model Serial Code of this device.
const char * GetManufacturerInformation() const
Get the Manufacturer Information of this device.
char * ManufacturerInformation
Pointer to the Manufacturer Information.
bool ShouldRequestName()
Should the Device Name be requested As long as the device name is not set yet and the number of reque...
unsigned long * TransmitPGNs
Transmitted PGNs.
bool HasProductInformation() const
Has the Product Information for the device already been loaded.
char * InitConfigurationInformation(size_t &_ManISize, size_t &_InstDesc1Size, size_t &_InstDesc2Size)
Initialize the Configuration Information of this device.
bool ShouldRequestPGNList()
Should the Device PGN List be requested As long as the device PGN List is not set yet and the number ...
uint8_t TransmitPGNsSize
Size of the transmitted PGN (number of bytes)
void ClearProductInformation()
Clears the Product Information the device.
unsigned long ProdIRequested
Time for last request on the Product Information.
unsigned long * InitTransmitPGNs(uint8_t count)
Initialize an array for transmitted PGNs This functions frees if needed old reservation and reserves ...
unsigned long ConfIRequested
Time for last request on the Config Information.
uint16_t ConfISize
Size of the Config Information (number of bytes)
Definition: N2kDeviceList.h:90
bool ReadyForRequestProductInformation()
Ready for the next request of Device Product Information.
bool ShouldRequestConfigurationInformation()
Should the Device Configuration Information be requested As long as the device Configuration Informat...
void ClearPGNListLoaded()
Resets the PGN List Loaded values of the device.
void SetProductInformation(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)
Set the Product Information of the device.
uint8_t nPGNsRequested
How many times we have requested the PGN.
tInternalDevice(uint64_t _Name, uint8_t _Source=255)
Construct a new Internal Device object.
char * GetInstallationDescription1()
Get the Installation Description 1 of this device.
uint8_t ReceivePGNsSize
Size of the received PGN (number of bytes)
bool ReadyForRequestPGNList()
Ready for the next request of Device PGN List.
unsigned long LastMessageTime
Time of the last message.
const char * GetModelID() const
Get the Model ID of this device.
const unsigned long * GetTransmitPGNs() const
Get the transmitted PGNs of this device.
~tInternalDevice()
Destroy the Internal Device object Clean up all the memory.
uint8_t nNameRequested
How many times we have requested the name.
void SetProductInformationRequested()
Increments the Number of how often the Product Information has already been requested and stores the ...
void SetSource(uint8_t _Source)
Set the Source address of the device.
bool IsSameProductInformation(tNMEA2000::tProductInformation &Other)
Compares two Product Informations.
uint16_t ManISize
Size of the Manufacturer Information (number of bytes)
Definition: N2kDeviceList.h:92
tNMEA2000::tProductInformation ProdI
Product Information of this device.
Definition: N2kDeviceList.h:85
bool ReadyForRequestConfigurationInformation()
Ready for the next request of Device Configuration Information.
void ClearProductInformationLoaded()
Resets the Product Information Loaded values of the device.
char * ConfI
Pointer to the Config Information.
Definition: N2kDeviceList.h:99
bool ConfILoaded
Product Information has been loaded.
Definition: N2kDeviceList.h:88
unsigned short GetCertificationLevel() const
Get the Certification Level of this device.
bool HasConfigurationInformation() const
Has the Configuration Information for the device already been loaded.
unsigned short GetN2kVersion() const
Get the N2k version of this device.
void SetPGNListRequested()
Increments the Number of how often the PGN List has already been requested and stores the timestamp.
unsigned long * ReceivePGNs
Received PGNs.
void SetNameRequested()
Increments the Number of how often the name has already been requested.
unsigned long PGNsRequested
Time for last request on the PGN.
char * InstallationDescription2
Pointer to the Installation Description 2.
Helper class to keep track of all devices on the bus.
Definition: N2kDeviceList.h:72
void HandleProductInformation(const tN2kMsg &N2kMsg)
Handle a Product Information message - PGN 126996.
void SaveDevice(tInternalDevice *pDevice, uint8_t Source)
Saves a device to Sources.
void HandleOther(const tN2kMsg &N2kMsg)
Handles all Other messages.
void HandleSupportedPGNList(const tN2kMsg &N2kMsg)
Handle a Product Information message - PGN 126464.
uint8_t MaxDevices
Number of NMEA2000 devices stored in Sources.
bool RequestIsoAddressClaim(uint8_t Source)
Request the ISO AddressClaim for a specific device on the bus.
tN2kDeviceList::tInternalDevice * LocalFindDeviceBySource(uint8_t Source) const
Find a device in Sources by the source address.
const tNMEA2000::tDevice * FindDeviceByProduct(uint16_t ManufacturerCode, uint16_t ProductCode, uint8_t Source=0xff) const
Find a device in Sources by the manufacturer and product code.
bool ReadResetIsListUpdated()
Check if device list has updated.
void HandleConfigurationInformation(const tN2kMsg &N2kMsg)
Handle a Configuration Information message - PGN 126998.
bool RequestSupportedPGNList(uint8_t Source)
Request the supported PGNs of a specific device on the bus.
const tNMEA2000::tDevice * FindDeviceByName(uint64_t NAME) const
Find a device in Sources by the NAME of the device.
bool RequestProductInformation(uint8_t Source)
Request the product information of a specific device on the bus.
tInternalDevice * Sources[N2kMaxBusDevices]
List of NMEA2000 devices found on the bus.
const tNMEA2000::tDevice * FindDeviceBySource(uint8_t Source) const
Return device by it's bus source address.
void HandleIsoAddressClaim(const tN2kMsg &N2kMsg)
Handle ISO Address Claim Message - PGN 60928.
bool RequestConfigurationInformation(uint8_t Source)
Request the configuration information of a specific device on the bus.
uint8_t Count() const
Return number of known devices in Sources.
tN2kDeviceList::tInternalDevice * LocalFindDeviceByName(uint64_t Name) const
Find a device in Sources by the name of the device.
tN2kDeviceList::tInternalDevice * LocalFindDeviceByProduct(uint16_t ManufacturerCode, uint16_t ProductCode, uint8_t Source=0xff) const
Find a device in Sources by the manufacturer and product code.
unsigned long GetDeviceLastMessageTime(uint8_t Source) const
const tNMEA2000::tDevice * FindDeviceByIDs(uint16_t ManufacturerCode, uint32_t UniqueNumber) const
Find a device in Sources by the manufacturer code and unique ID.
tN2kDeviceList(tNMEA2000 *_pNMEA2000)
Constructor for the class.
tN2kDeviceList::tInternalDevice * LocalFindDeviceByIDs(uint16_t ManufacturerCode, uint32_t UniqueNumber) const
Find a device in Sources by the manufacturer code and unique ID.
void AddDevice(uint8_t Source)
Adds a device to Sources.
void HandleMsg(const tN2kMsg &N2kMsg)
Handle NMEA2000 messages.
bool HasPendingRequests
There are still requests pending.
bool ListUpdated
The list of devices has been updated.
This class contains all the data of an NMEA2000 message.
Definition: N2kMsg.h:656
This class represents a N2k device.
Definition: NMEA2000.h:481
unsigned long GetCreateTime() const
Returns the Time of Creation of this device.
Definition: NMEA2000.h:507
uint64_t GetName() const
Get the Name of this device.
Definition: NMEA2000.h:513
uint8_t Source
Source address on bus for this device.
Definition: NMEA2000.h:488
tDeviceInformation DevI
This object holds all necessary device informations.
Definition: NMEA2000.h:484
void SetName(uint64_t _Name)
Set the Name to the Device Information.
Definition: NMEA2000.h:467
Message handler class.
Definition: NMEA2000.h:616
tNMEA2000 device class definition.
Definition: NMEA2000.h:138
Structure that holds all the product information.
Definition: NMEA2000.h:216
unsigned short N2kVersion
Version of NMEA2000 Standard that is supported.
Definition: NMEA2000.h:219
unsigned short ProductCode
Product Code of the device.
Definition: NMEA2000.h:221
char N2kModelID[Max_N2kModelID_len+1]
Max length of ModelID Note that we reserve one extra char for null termination
Definition: NMEA2000.h:225
unsigned char LoadEquivalency
Load Equivalency of the device A Load Equivalence Number express the amount of current that is drawn ...
Definition: NMEA2000.h:243
unsigned char CertificationLevel
Certification level of the device.
Definition: NMEA2000.h:236
char N2kModelVersion[Max_N2kModelVersion_len+1]
Max length of Model Version Note that we reserve one extra char for null termination.
Definition: NMEA2000.h:231
bool IsSame(const tProductInformation &Other)
Compares two product information structures.
Definition: NMEA2000.cpp:632
void Clear()
Clears out all data
Definition: NMEA2000.cpp:627
char N2kSwCode[Max_N2kSwCode_len+1]
Max length of Software Code Note that we reserve one extra char for null termination
Definition: NMEA2000.h:228
char N2kModelSerialCode[Max_N2kModelSerialCode_len+1]
Max length of Serial Code Note that we reserve one extra char for null termination.
Definition: NMEA2000.h:234
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)
Set all the product information data of the structure.
Definition: NMEA2000.h:262