/* NMEA2000.cpp Copyright (c) 2015-2021 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 "NMEA2000.h" #include "N2kDef.h" #if !defined(N2K_NO_GROUP_FUNCTION_SUPPORT) #include "N2kGroupFunctionDefaultHandlers.h" #endif #include #include #define DebugStream Serial // #define NMEA2000_FRAME_ERROR_DEBUG // #define NMEA2000_FRAME_IN_DEBUG // #define NMEA2000_FRAME_OUT_DEBUG // #define NMEA2000_MSG_DEBUG // #define NMEA2000_BUF_DEBUG // #define NMEA2000_DEBUG #if defined(NMEA2000_FRAME_ERROR_DEBUG) # define N2kFrameErrDbg(fmt, args...) DebugStream.print (fmt , ## args) # define N2kFrameErrDbgln(fmt, args...) DebugStream.println (fmt , ## args) #else # define N2kFrameErrDbg(fmt, args...) # define N2kFrameErrDbgln(fmt, args...) #endif #if defined(NMEA2000_FRAME_IN_DEBUG) # define N2kFrameInDbg(fmt, args...) DebugStream.print (fmt , ## args) # define N2kFrameInDbgln(fmt, args...) DebugStream.println (fmt , ## args) #else # define N2kFrameInDbg(fmt, args...) # define N2kFrameInDbgln(fmt, args...) #endif #if defined(NMEA2000_FRAME_OUT_DEBUG) # define N2kFrameOutDbg(fmt, args...) DebugStream.print (fmt , ## args) # define N2kFrameOutDbgln(fmt, args...) DebugStream.println (fmt , ## args) #else # define N2kFrameOutDbg(fmt, args...) # define N2kFrameOutDbgln(fmt, args...) #endif #if defined(NMEA2000_MSG_DEBUG) # define N2kMsgDbg(fmt, args...) DebugStream.print (fmt , ## args) # define N2kMsgDbgln(fmt, args...) DebugStream.println (fmt , ## args) #else # define N2kMsgDbg(fmt, args...) # define N2kMsgDbgln(fmt, args...) #endif #if defined(NMEA2000_BUF_DEBUG) # define DbgPrintBuf(len, buf, addln) PrintBuf(&DebugStream, len, buf, addln) #else # define DbgPrintBuf(len, buf, addln) #endif #if defined(NMEA2000_DEBUG) # define N2kDbg(fmt, args...) DebugStream.print (fmt , ## args) # define N2kDbgln(fmt, args...) DebugStream.println (fmt , ## args) #else # define N2kDbg(fmt, args...) # define N2kDbgln(fmt, args...) #endif // #define NMEA2000_MEMORY_TEST 1 #if defined(NMEA2000_MEMORY_TEST) #include void N2kPrintFreeMemory(const char *Source) { Serial.print(Source); Serial.print(", free memory="); Serial.println(freeMemory()); } #else #define N2kPrintFreeMemory(a) #endif #define N2kAddressClaimTimeout 250 #define MaxHeartbeatInterval 655320UL #define TP_MAX_FRAMES 5 // Max frames, which can be received at time #define TP_CM 60416L /* Multi packet connection management, TP.CM */ #define TP_DT 60160L /* Multi packet data transfer, TP.DT */ #define TP_CM_BAM 32 #define TP_CM_RTS 16 #define TP_CM_CTS 17 #define TP_CM_ACK 19 #define TP_CM_Abort 255 #define TP_CM_AbortBusy 1 // Already in one or more connection managed sessions and cannot support another. #define TP_CM_AbortNoResources 2 // System resources were needed for another task so this connection managed session was terminated. #define TP_CM_AbortTimeout 3 // A timeout occurred and this is the connection abort to close the session. const unsigned long DefTransmitMessages[] PROGMEM = { 59392L, /* ISO Acknowledgement, pri=6, period=NA */ 59904L, /* ISO Request, pri=6, period=NA */ #if !defined(N2K_NO_ISO_MULTI_PACKET_SUPPORT) TP_DT, /* Multi packet data transfer, TP.DT, pri=6, period=NA */ TP_CM, /* Multi packet connection management, TP.CM, pri=6, period=NA */ #endif 60928L, /* ISO Address Claim, pri=6, period=NA */ #if !defined(N2K_NO_GROUP_FUNCTION_SUPPORT) 126208L, /* NMEA Request/Command/Acknowledge group function, pri=3, period=NA */ #endif 126464L, /* PGN List (Transmit and Receive), pri=6, period=NA */ #if !defined(N2K_NO_HEARTBEAT_SUPPORT) 126993L, /* Heartbeat, pri=7, period=60000 */ #endif 126996L, /* Product information, pri=6, period=NA */ 126998L, /* Configuration information, pri=6, period=NA */ 0}; const unsigned long DefReceiveMessages[] PROGMEM = { 59392L, /* ISO Acknowledgement, pri=6, period=NA */ 59904L, /* ISO Request, pri=6, period=NA */ #if !defined(N2K_NO_ISO_MULTI_PACKET_SUPPORT) TP_DT, /* Multi packet data transfer, TP.DT */ TP_CM, /* Multi packet connection management, TP.CM */ #endif 60928L, /* ISO Address Claim */ 65240L, /* Commanded Address */ #if !defined(N2K_NO_GROUP_FUNCTION_SUPPORT) 126208L, /* NMEA Request/Command/Acknowledge group function */ #endif 0}; bool IsSingleFrameSystemMessage(unsigned long PGN) { switch (PGN) { case 59392L: /* ISO Acknowledgement */ case TP_DT: /* Multi packet data transfer, TP.DT */ case TP_CM: /* Multi packet connection management, TP.CM */ case 59904L: /* ISO Request */ case 60928L: /* ISO Address Claim */ return true; } return false; } bool IsFastPacketSystemMessage(unsigned long PGN) { switch (PGN) { case 65240L: /* Commanded Address*/ case 126208L: /* NMEA Request/Command/Acknowledge group function */ return true; } return false; } bool IsDefaultSingleFrameMessage(unsigned long PGN) { switch (PGN) { case 126992L: // System date/time, pri=3, period=1000 case 126993L: // Heartbeat, pri=7, period=60000 case 127245L: // Rudder, pri=2, period=100 case 127250L: // Vessel Heading, pri=2, period=100 case 127251L: // Rate of Turn, pri=2, period=100 case 127257L: // Attitude, pri=3, period=1000 case 127488L: // Engine parameters rapid, rapid Update, pri=2, period=100 case 127493L: // Transmission parameters: dynamic, pri=2, period=100 case 127501L: // Binary status report, pri=3, period=NA case 127505L: // Fluid level, pri=6, period=2500 case 127508L: // Battery Status, pri=6, period=1500 case 128259L: // Boat speed, pri=2, period=1000 case 128267L: // Water depth, pri=3, period=1000 case 129025L: // Lat/lon rapid, pri=2, period=100 case 129026L: // COG SOG rapid, pri=2, period=250 case 129283L: // Cross Track Error, pri=3, period=1000 case 130306L: // Wind Speed, pri=2, period=100 case 130310L: // Outside Environmental parameters, pri=5, period=500 case 130311L: // Environmental parameters, pri=5, period=500 case 130312L: // Temperature, pri=5, period=2000 case 130313L: // Humidity, pri=5, period=2000 case 130314L: // Pressure, pri=5, period=2000 case 130316L: // Temperature extended range, pri=5, period=NA case 130576L: // Small Craft Status (Trim Tab position), pri=2, period=200 return true; } return false; } bool IsMandatoryFastPacketMessage(unsigned long PGN) { switch (PGN) { case 126464L: // PGN List (Transmit and Receive), pri=6, period=NA case 126996L: // Product information, pri=6, period=NA case 126998L: // Configuration information, pri=6, period=NA return true; } return false; } bool IsDefaultFastPacketMessage(unsigned long PGN) { switch (PGN) { case 126983L: // Alert, pri=2, period=1000 case 126984L: // Alert Response, pri=2, period=NA case 126985L: // Alert Text, pri=2, period=10000 case 126986L: // Alert Configuration, pri=2, period=NA case 126987L: // Alert Threshold, pri=2, period=NA case 126988L: // Alert Value, pri=2, period=10000 case 127233L: // Alert Value, pri=3, period=NA case 127237L: // Heading/Track control, pri=2, period=250 case 127489L: // Engine parameters dynamic, pri=2, period=500 case 127496L: // Trip fuel consumption, vessel, pri=5, period=1000 case 127497L: // Trip fuel consumption, engine, pri=5, period=1000 case 127498L: // Engine parameters static, pri=5, period=NA case 127503L: // AC Input Status, pri=6, period=1500 case 127504L: // AC Output Status, pri=6, period=1500 case 127506L: // DC Detailed status, pri=6, period=1500 case 127507L: // Charger status, pri=6, period=1500 case 127509L: // Inverter status, pri=6, period=1500 case 127510L: // Charger configuration status, pri=6, period=NA case 127511L: // Inverter Configuration Status, pri=6, period=NA case 127512L: // AGS configuration status, pri=6, period=NA case 127513L: // Battery configuration status, pri=6, period=NA case 127514L: // AGS Status, pri=6, period=1500 case 128275L: // Distance log, pri=6, period=1000 case 128520L: // Tracked Target Data, pri=2, period=1000 case 129029L: // GNSS Position Data, pri=3, period=1000 case 129038L: // AIS Class A Position Report, pri=4, period=NA case 129039L: // AIS Class B Position Report, pri=4, period=NA case 129040L: // AIS Class B Extended Position Report, pri=4, period=NA case 129041L: // AIS Aids to Navigation (AtoN) Report, pri=4, period=NA case 129044L: // Datum, pri=6, period=10000 case 129045L: // User Datum Settings, pri=6, period=NA case 129284L: // Navigation info, pri=3, period=1000 case 129285L: // Waypoint list, pri=3, period=NA case 129301L: // Time to/from Mark, pri=3, period=1000 case 129302L: // Bearing and Distance between two Marks, pri=6, period=NA case 129538L: // GNSS Control Status, pri=6, period=NA case 129540L: // GNSS Sats in View, pri=6, period=1000 case 129541L: // GPS Almanac Data, pri=6, period=NA case 129542L: // GNSS Pseudorange Noise Statistics, pri=6, period=1000 case 129545L: // GNSS RAIM Output, pri=6, period=NA case 129547L: // GNSS Pseudorange Error Statistics, pri=6, period=NA case 129549L: // DGNSS Corrections, pri=6, period=NA case 129551L: // GNSS Differential Correction Receiver Signal, pri=6, period=NA case 129556L: // GLONASS Almanac Data, pri=6, period=NA case 129792L: // AIS DGNSS Broadcast Binary Message, pri=6, period=NA case 129793L: // AIS UTC and Date Report, pri=7, period=NA case 129794L: // AIS Class A Static data, pri=6, period=NA case 129795L: // AIS Addressed Binary Message, pri=5, period=NA case 129796L: // AIS Acknowledge, pri=7, period=NA case 129797L: // AIS Binary Broadcast Message, pri=5, period=NA case 129798L: // AIS SAR Aircraft Position Report, pri=4, period=NA case 129799L: // Radio Frequency/Mode/Power, pri=3, period=NA case 129800L: // AIS UTC/Date Inquiry, pri=7, period=NA case 129801L: // AIS Addressed Safety Related Message, pri=5, period=NA case 129802L: // AIS Safety Related Broadcast Message, pri=5, period=NA case 129803L: // AIS Interrogation PGN, pri=7, period=NA case 129804L: // AIS Assignment Mode Command, pri=7, period=NA case 129805L: // AIS Data Link Management Message, pri=7, period=NA case 129806L: // AIS Channel Management, pri=7, period=NA case 129807L: // AIS Group Assignment, pri=7, period=NA case 129808L: // DSC Call Information, pri=8, period=NA case 129809L: // AIS Class B Static Data: Part A, pri=6, period=NA case 129810L: // AIS Class B Static Data Part B, pri=6, period=NA case 129811L: // AIS Single Slot Binary Message, pri=5, period=NA case 129812L: // AIS Multi Slot Binary Message, pri=5, period=NA case 129813L: // AIS Long-Range Broadcast Message, pri=5, period=NA case 130052L: // Loran-C TD Data, pri=3, period=1000 case 130053L: // Loran-C Range Data, pri=3, period=1000 case 130054L: // Loran-C Signal Data, pri=3, period=1000 case 130060L: // Label, pri=7, period=NA case 130061L: // Channel Source Configuration, pri=7, period=NA case 130064L: // Route and WP Service - Database List, pri=7, period=NA case 130065L: // Route and WP Service - Route List, pri=7, period=NA case 130066L: // Route and WP Service - Route/WP-List Attributes, pri=7, period=NA case 130067L: // Route and WP Service - Route - WP Name & Position, pri=7, period=NA case 130068L: // Route and WP Service - Route - WP Name, pri=7, period=NA case 130069L: // Route and WP Service - XTE Limit & Navigation Method, pri=7, period=NA case 130070L: // Route and WP Service - WP Comment, pri=7, period=NA case 130071L: // Route and WP Service - Route Comment, pri=7, period=NA case 130072L: // Route and WP Service - Database Comment, pri=7, period=NA case 130073L: // Route and WP Service - Radius of Turn, pri=7, period=NA case 130074L: // Route and WP Service - WP List - WP Name & Position, pri=7, period=NA case 130320L: // Tide Station Data, pri=6, period=1000 case 130321L: // Salinity Station Data, pri=6, period=1000 case 130322L: // Current Station Data, pri=6, period=1000 case 130323L: // Meteorological Station Data, pri=6, period=1000 case 130324L: // Moored Buoy Station Data, pri=6, period=1000 case 130567L: // Watermaker Input Setting and Status, pri=6, period=2500 case 130577L: // Direction Data PGN, pri=3, period=1000 case 130578L: // Vessel Speed Components, pri=2, period=250 return true; } return false; } bool IsProprietaryFastPacketMessage(unsigned long PGN) { return ( PGN==126720L ) || ( 130816L<=PGN && PGN<=131071L ); } bool tNMEA2000::IsProprietaryMessage(unsigned long PGN) { return IsProprietaryFastPacketMessage(PGN) || ( PGN==61184L ) || ( 65280L<=PGN && PGN<=65535L ); } const tNMEA2000::tProductInformation DefProductInformation PROGMEM = { 2101, // N2kVersion 666, // ProductCode "Arduino N2k->PC", //N2kModelID "1.0.0.0", //N2kSwCode "1.0.0", // N2kModelVersion "00000001", // N2kModelSerialCode 0, // CertificationLevel 1 // LoadEquivalency }; const char DefManufacturerInformation [] PROGMEM = "NMEA2000 library, https://github.com/ttlappalainen/NMEA2000"; const char DefInstallationDescription1 [] PROGMEM = ""; const char DefInstallationDescription2 [] PROGMEM = ""; //***************************************************************************** void tNMEA2000::tProductInformation::Clear() { memset(this,0,sizeof(tProductInformation)); } //***************************************************************************** bool tNMEA2000::tProductInformation::IsSame(const tProductInformation &Other) { return memcmp(this,&Other,sizeof(tProductInformation))==0; } //***************************************************************************** void tNMEA2000::ClearCharBuf(size_t MaxLen, char *buf) { if ( buf==0 ) return; size_t i=0; for (; i=1 && _DeviceCount<10 ) DeviceCount=_DeviceCount; } //***************************************************************************** void tNMEA2000::InitDevices() { if ( Devices==0 ) { N2kDbgln("Init devices"); Devices=new tInternalDevice[DeviceCount]; MaxCANSendFrames*=DeviceCount; // We need bigger buffer for sending all information // for (int i=0; i0 ) { Devices[i].LocalProductInformation=0; Devices[i].ProductInformation=0; } } } } //***************************************************************************** void tNMEA2000::SetProductInformation(const tProductInformation *_ProductInformation, int iDev) { if ( !IsValidDevice(iDev) ) return; InitDevices(); Devices[iDev].ProductInformation=_ProductInformation; if (Devices[iDev].ProductInformation==0) Devices[iDev].ProductInformation=Devices[iDev].LocalProductInformation; if (Devices[iDev].ProductInformation==0) Devices[iDev].ProductInformation=&DefProductInformation; } //***************************************************************************** void tNMEA2000::SetProductInformation(const char *_ModelSerialCode, unsigned short _ProductCode, const char *_ModelID, const char *_SwCode, const char *_ModelVersion, unsigned char _LoadEquivalency, unsigned short _N2kVersion, unsigned char _CertificationLevel, int iDev) { if ( !IsValidDevice(iDev) ) return; InitDevices(); if (Devices[iDev].LocalProductInformation==0) { Devices[iDev].LocalProductInformation=new tProductInformation(); } Devices[iDev].ProductInformation=Devices[iDev].LocalProductInformation; Devices[iDev].LocalProductInformation->Set(_ModelSerialCode,_ProductCode,_ModelID,_SwCode,_ModelVersion,_LoadEquivalency,_N2kVersion,_CertificationLevel); } //***************************************************************************** void tNMEA2000::SetConfigurationInformation(const char *ManufacturerInformation, const char *InstallationDescription1, const char *InstallationDescription2) { if ( LocalConfigurationInformationData!=0 ) free(LocalConfigurationInformationData); // This happens on second call, which is not good. LocalConfigurationInformationData=0; size_t ManInfoLen=(ManufacturerInformation?strlen(ManufacturerInformation)+1:0); #if !defined(N2K_NO_GROUP_FUNCTION_SUPPORT) size_t InstDesc1Len=Max_N2kConfigurationInfoField_len; size_t InstDesc2Len=Max_N2kConfigurationInfoField_len; #else size_t InstDesc1Len=(InstallationDescription1?strlen(InstallationDescription1)+1:0); size_t InstDesc2Len=(InstallationDescription2?strlen(InstallationDescription2)+1:0); #endif if ( ManInfoLen>Max_N2kConfigurationInfoField_len ) ManInfoLen=Max_N2kConfigurationInfoField_len; if ( InstDesc1Len>Max_N2kConfigurationInfoField_len ) InstDesc1Len=Max_N2kConfigurationInfoField_len; if ( InstDesc2Len>Max_N2kConfigurationInfoField_len ) InstDesc2Len=Max_N2kConfigurationInfoField_len; size_t TotalSize=ManInfoLen+InstDesc1Len+InstDesc2Len; void *mem=(TotalSize>0?malloc(TotalSize):0); LocalConfigurationInformationData=(char*)mem; char *Info=LocalConfigurationInformationData; SetCharBuf(InstallationDescription1,InstDesc1Len,Info); ConfigurationInformation.InstallationDescription1=(InstallationDescription1?Info:0); Info+=InstDesc1Len; SetCharBuf(InstallationDescription2,InstDesc2Len,Info); ConfigurationInformation.InstallationDescription2=(InstallationDescription2?Info:0); Info+=InstDesc2Len; SetCharBuf(ManufacturerInformation,ManInfoLen,Info); ConfigurationInformation.ManufacturerInformation=(ManufacturerInformation?Info:0); } //***************************************************************************** void tNMEA2000::SetProgmemConfigurationInformation(const char *ManufacturerInformation, const char *InstallationDescription1, const char *InstallationDescription2) { if ( LocalConfigurationInformationData!=0 ) free(LocalConfigurationInformationData); // This happens on second call, which is not good. LocalConfigurationInformationData=0; ConfigurationInformation.ManufacturerInformation=ManufacturerInformation; ConfigurationInformation.InstallationDescription1=InstallationDescription1; ConfigurationInformation.InstallationDescription2=InstallationDescription2; } //***************************************************************************** size_t tNMEA2000::GetFastPacketTxPGNCount(int iDev) { if ( !IsValidDevice(iDev) ) return 0; unsigned long ListPGN; size_t FPTxPGNCount=0; for (int i=0; (ListPGN=pgm_read_dword(&DefTransmitMessages[i]))!=0; i++) { if ( IsFastPacketPGN(ListPGN) ) FPTxPGNCount++; } if (Devices[iDev].TransmitMessages!=0) { for (int i=0; (ListPGN=pgm_read_dword(&Devices[iDev].TransmitMessages[i]))!=0; i++) { if ( IsFastPacketPGN(ListPGN) ) FPTxPGNCount++; } } return FPTxPGNCount; } //***************************************************************************** int tNMEA2000::GetSequenceCounter(unsigned long PGN, int iDev) { if ( !IsValidDevice(iDev) ) return 0; if ( Devices[iDev].PGNSequenceCounters==0 ) { // Sequence counters has not yet been initialized Devices[iDev].MaxPGNSequenceCounters=GetFastPacketTxPGNCount(iDev)+1; // Reserve 1 for undefined PGNs Devices[iDev].PGNSequenceCounters=new unsigned long[Devices[iDev].MaxPGNSequenceCounters]; for ( size_t i=0; i>24; sc++; if (sc>7) sc=0; // Get next counter Devices[iDev].PGNSequenceCounters[i]=PGN | (sc << 24); return sc; } } // PGN counter not found, so use common sc=Devices[iDev].PGNSequenceCounters[last]; sc++; if (sc>7) sc=0; Devices[iDev].PGNSequenceCounters[last]=sc; return sc; } #if !defined(N2K_NO_GROUP_FUNCTION_SUPPORT) //***************************************************************************** void CopyProgmemString(const char *str, size_t MaxLen, char *buf) { if ( buf==0 || MaxLen==0 ) return; // nothing to do for 0 buffer if ( str==0 ) { buf[0]=0; return; } size_t i=0; char c; for (; iN2kVersion; } else { return pgm_read_word(&ProductInformation->N2kVersion); } } //***************************************************************************** unsigned short tNMEA2000::GetProductCode(int iDev) const { bool IsProgMem; const tProductInformation *ProductInformation=GetProductInformation(iDev,IsProgMem); if ( ProductInformation==0 ) return 0; if ( !IsProgMem ) { return ProductInformation->ProductCode; } else { return pgm_read_word(&ProductInformation->ProductCode); } } //***************************************************************************** void tNMEA2000::GetModelID(char *buf, size_t max_len, int iDev) const { if ( max_len==0 ) return; buf[0]=0; bool IsProgMem; const tProductInformation *ProductInformation=GetProductInformation(iDev,IsProgMem); if ( ProductInformation==0 ) return; if ( !IsProgMem ) { SetCharBuf(ProductInformation->N2kModelID,max_len,buf); } else { CopyProgmemString(ProductInformation->N2kModelID,max_len,buf); } } //***************************************************************************** void tNMEA2000::GetSwCode(char *buf, size_t max_len, int iDev) const { if ( max_len==0 ) return; buf[0]=0; bool IsProgMem; const tProductInformation *ProductInformation=GetProductInformation(iDev,IsProgMem); if ( ProductInformation==0 ) return; if ( !IsProgMem ) { SetCharBuf(ProductInformation->N2kSwCode,max_len,buf); } else { CopyProgmemString(ProductInformation->N2kSwCode,max_len,buf); } } //***************************************************************************** void tNMEA2000::GetModelVersion(char *buf, size_t max_len, int iDev) const { if ( max_len==0 ) return; buf[0]=0; bool IsProgMem; const tProductInformation *ProductInformation=GetProductInformation(iDev,IsProgMem); if ( ProductInformation==0 ) return; if ( !IsProgMem ) { SetCharBuf(ProductInformation->N2kModelVersion,max_len,buf); } else { CopyProgmemString(ProductInformation->N2kModelVersion,max_len,buf); } } //***************************************************************************** void tNMEA2000::GetModelSerialCode(char *buf, size_t max_len, int iDev) const { if ( max_len==0 ) return; buf[0]=0; bool IsProgMem; const tProductInformation *ProductInformation=GetProductInformation(iDev,IsProgMem); if ( ProductInformation==0 ) return; if ( !IsProgMem ) { SetCharBuf(ProductInformation->N2kModelSerialCode,max_len,buf); } else { CopyProgmemString(ProductInformation->N2kModelSerialCode,max_len,buf); } } //***************************************************************************** unsigned char tNMEA2000::GetCertificationLevel(int iDev) const { bool IsProgMem; const tProductInformation *ProductInformation=GetProductInformation(iDev,IsProgMem); if ( ProductInformation==0 ) return 0; if ( !IsProgMem ) { return ProductInformation->CertificationLevel; } else { return pgm_read_byte(&ProductInformation->CertificationLevel); } } //***************************************************************************** unsigned char tNMEA2000::GetLoadEquivalency(int iDev) const { bool IsProgMem; const tProductInformation *ProductInformation=GetProductInformation(iDev,IsProgMem); if ( ProductInformation==0 ) return 0; if ( !IsProgMem ) { return ProductInformation->LoadEquivalency; } else { return pgm_read_byte(&ProductInformation->LoadEquivalency); } } //***************************************************************************** void tNMEA2000::CopyProgmemConfigurationInformationToLocal() { if ( LocalConfigurationInformationData==0 ) { char Buf[Max_N2kConfigurationInfoField_len]; const char *ID1=ConfigurationInformation.InstallationDescription1; const char *ID2=ConfigurationInformation.InstallationDescription2; CopyProgmemString(ConfigurationInformation.ManufacturerInformation,Max_N2kConfigurationInfoField_len,Buf); SetConfigurationInformation(Buf); CopyProgmemString(ID1,Max_N2kConfigurationInfoField_len,Buf); SetInstallationDescription1(Buf); CopyProgmemString(ID2,Max_N2kConfigurationInfoField_len,Buf); SetInstallationDescription2(Buf); } } //***************************************************************************** void tNMEA2000::SetInstallationDescription1(const char *InstallationDescription1) { CopyProgmemConfigurationInformationToLocal(); // Get pointer to local InstallationDescription1, which is after char *Info=LocalConfigurationInformationData; SetCharBuf(InstallationDescription1,Max_N2kConfigurationInfoField_len,Info); ConfigurationInformation.InstallationDescription1=(InstallationDescription1?Info:0); InstallationDescriptionChanged=true; } //***************************************************************************** void tNMEA2000::SetInstallationDescription2(const char *InstallationDescription2) { CopyProgmemConfigurationInformationToLocal(); char *Info=LocalConfigurationInformationData+Max_N2kConfigurationInfoField_len; SetCharBuf(InstallationDescription2,Max_N2kConfigurationInfoField_len,Info); ConfigurationInformation.InstallationDescription2=(InstallationDescription2?Info:0); InstallationDescriptionChanged=true; } //***************************************************************************** void tNMEA2000::GetInstallationDescription1(char *buf, size_t max_len) { if ( LocalConfigurationInformationData!=0 ) { SetCharBuf(ConfigurationInformation.InstallationDescription1,max_len,buf); } else { CopyProgmemString(ConfigurationInformation.InstallationDescription1,max_len,buf); } } //***************************************************************************** void tNMEA2000::GetInstallationDescription2(char *buf, size_t max_len) { if ( LocalConfigurationInformationData!=0 ) { SetCharBuf(ConfigurationInformation.InstallationDescription2,max_len,buf); } else { CopyProgmemString(ConfigurationInformation.InstallationDescription2,max_len,buf); } } //***************************************************************************** void tNMEA2000::GetManufacturerInformation(char *buf, size_t max_len) { if ( LocalConfigurationInformationData!=0 ) { SetCharBuf(ConfigurationInformation.ManufacturerInformation,max_len,buf); } else { CopyProgmemString(ConfigurationInformation.ManufacturerInformation,max_len,buf); } } //***************************************************************************** bool tNMEA2000::ReadResetInstallationDescriptionChanged() { bool result=InstallationDescriptionChanged; InstallationDescriptionChanged=false; return result; } #endif //***************************************************************************** void tNMEA2000::SetDeviceInformation(unsigned long _UniqueNumber, unsigned char _DeviceFunction, unsigned char _DeviceClass, uint16_t _ManufacturerCode, unsigned char _IndustryGroup, int iDev ) { if ( !IsValidDevice(iDev) ) return; InitDevices(); if (_ManufacturerCode!=0xffff) Devices[iDev].DeviceInformation.SetManufacturerCode(_ManufacturerCode); if (_UniqueNumber!=0xffffffff) Devices[iDev].DeviceInformation.SetUniqueNumber(_UniqueNumber); if (_DeviceFunction!=0xff) Devices[iDev].DeviceInformation.SetDeviceFunction(_DeviceFunction); if (_DeviceClass!=0xff) Devices[iDev].DeviceInformation.SetDeviceClass(_DeviceClass); if (_IndustryGroup!=0xff) Devices[iDev].DeviceInformation.SetIndustryGroup(_IndustryGroup); } //***************************************************************************** void tNMEA2000::SetDeviceInformationInstances( uint8_t _DeviceInstanceLower, uint8_t _DeviceInstanceUpper, uint8_t _SystemInstance, int iDev ) { if ( !IsValidDevice(iDev) ) return; InitDevices(); uint8_t DeviceInstance=Devices[iDev].DeviceInformation.GetDeviceInstance(); if (_DeviceInstanceLower!=0xff ) { DeviceInstance=( (DeviceInstance & ~0x07) | (_DeviceInstanceLower & 0x07) ); } if (_DeviceInstanceUpper!=0xff ) { DeviceInstance=( (DeviceInstance & ~0xF8) | ((_DeviceInstanceUpper&0x1f)<<3) ); } if ( Devices[iDev].DeviceInformation.GetDeviceInstance()!=DeviceInstance) { Devices[iDev].DeviceInformation.SetDeviceInstance(DeviceInstance); DeviceInformationChanged=true; } if (_SystemInstance!=0xff && Devices[iDev].DeviceInformation.GetSystemInstance()!=_SystemInstance) { Devices[iDev].DeviceInformation.SetSystemInstance(_SystemInstance); DeviceInformationChanged=true; } // Send delayed. Had problems with some devices with too fast response. if ( IsReadyToSend() ) SendIsoAddressClaim(0xff,iDev,2); } //***************************************************************************** void tNMEA2000::SetSingleFrameMessages(const unsigned long *_SingleFrameMessages) { SingleFrameMessages[0]=_SingleFrameMessages; } //***************************************************************************** void tNMEA2000::SetFastPacketMessages(const unsigned long *_FastPacketMessages) { FastPacketMessages[0]=_FastPacketMessages; } //***************************************************************************** void tNMEA2000::ExtendSingleFrameMessages(const unsigned long *_SingleFrameMessages) { SingleFrameMessages[1]=_SingleFrameMessages; } //***************************************************************************** void tNMEA2000::ExtendFastPacketMessages(const unsigned long *_FastPacketMessages) { FastPacketMessages[1]=_FastPacketMessages; } //***************************************************************************** void tNMEA2000::ExtendTransmitMessages(const unsigned long *_Messages, int iDev) { if ( !IsValidDevice(iDev) ) return; InitDevices(); Devices[iDev].TransmitMessages=_Messages; } //***************************************************************************** void tNMEA2000::ExtendReceiveMessages(const unsigned long *_Messages, int iDev) { if ( !IsValidDevice(iDev) ) return; InitDevices(); Devices[iDev].ReceiveMessages=_Messages; } //***************************************************************************** void tNMEA2000::SetMode(tN2kMode _N2kMode, uint8_t _N2kSource) { InitDevices(); N2kMode=_N2kMode; for (int i=0; i0 ) CANSendFrameBuf = new tCANSendFrame[MaxCANSendFrames]; N2kDbg("Initialize frame buffer. Size: "); N2kDbg(MaxCANSendFrames); N2kDbg(", address:"); N2kDbgln((uint32_t)CANSendFrameBuf); CANSendFrameBufferWrite=0; CANSendFrameBufferRead=0; } // Receive buffer has sense only with interrupt handling. So it must be handled on inherited class. } //***************************************************************************** bool tNMEA2000::Open() { if (!DeviceReady) { InitCANFrameBuffers(); InitDevices(); if ( N2kCANMsgBuf==0 ) { if ( MaxN2kCANMsgs==0 ) MaxN2kCANMsgs=5; N2kCANMsgBuf = new tN2kCANMsg[MaxN2kCANMsgs]; for (int i=0; iprintln(F("CAN device ready")); } else { ForwardStream->println(F("CAN device failed to open")); } } delay(200); for (int i=0; i> 16); unsigned char CanIdPS = (unsigned char) (id >> 8); unsigned char CanIdDP = (unsigned char) (id >> 24) & 1; src = (unsigned char) id >> 0; prio = (unsigned char) ((id >> 26) & 0x7); if (CanIdPF < 240) { /* PDU1 format, the PS contains the destination address */ dst = CanIdPS; pgn = (((unsigned long)CanIdDP) << 16) | (((unsigned long)CanIdPF) << 8); } else { /* PDU2 format, the destination is implied global and the PGN is extended */ dst = 0xff; pgn = (((unsigned long)CanIdDP) << 16) | (((unsigned long)CanIdPF) << 8) | (unsigned long)CanIdPS; } } //***************************************************************************** unsigned long N2ktoCanID(unsigned char priority, unsigned long PGN, unsigned long Source, unsigned char Destination) { unsigned char CanIdPF = (unsigned char) (PGN >> 8); if (CanIdPF < 240) { // PDU1 format if ( (PGN & 0xff) != 0 ) return 0; // for PDU1 format PGN lowest byte has to be 0 for the destination. return ( ((unsigned long)(priority & 0x7))<<26 | PGN<<8 | ((unsigned long)Destination)<<8 | (unsigned long)Source); } else { // PDU2 format return ( ((unsigned long)(priority & 0x7))<<26 | PGN<<8 | (unsigned long)Source); } } //***************************************************************************** bool tNMEA2000::SendFrames() { uint16_t temp; if ( CANSendFrameBuf==0 ) return true; // This can be in case, where inherited class defines own buffering. while (CANSendFrameBufferRead!=CANSendFrameBufferWrite) { temp = (CANSendFrameBufferRead + 1) % MaxCANSendFrames; if ( CANSendFrame(CANSendFrameBuf[temp].id, CANSendFrameBuf[temp].len, CANSendFrameBuf[temp].buf, CANSendFrameBuf[temp].wait_sent) ) { CANSendFrameBufferRead=temp; N2kFrameOutDbg("Frame unbuffered "); N2kFrameOutDbgln(CANSendFrameBuf[temp].id); } else return false; } return true; } //***************************************************************************** bool tNMEA2000::SendFrame(unsigned long id, unsigned char len, const unsigned char *buf, bool wait_sent) { if ( !SendFrames() || !CANSendFrame(id,len,buf,wait_sent) ) { // If we can not sent frame immediately, add it to buffer tCANSendFrame *Frame=GetNextFreeCANSendFrame(); if ( Frame==0 ) { N2kFrameOutDbg("Frame failed "); N2kFrameOutDbgln(id); return false; } len=N2kMin(len,8); Frame->id=id; Frame->len=len; Frame->wait_sent=wait_sent; for (int i=0; ibuf[i]=buf[i]; N2kFrameOutDbg("Frame buffered "); N2kFrameOutDbgln(id); } return true; } #if !defined(N2K_NO_HEARTBEAT_SUPPORT) //***************************************************************************** void tNMEA2000::SetHeartbeatInterval(unsigned long interval, bool SetAsDefault, int iDev) { if (interval==0xffffffff) return; // Do not change InitDevices(); for (int i=(iDev<0?0:iDev); iMaxHeartbeatInterval ) interval=MaxHeartbeatInterval; if ( interval<1000 ) interval=0; Devices[i].HeartbeatInterval=interval; } if (SetAsDefault) Devices[i].DefaultHeartbeatInterval=Devices[i].HeartbeatInterval; Devices[i].NextHeartbeatSentTime=(millis()/Devices[i].HeartbeatInterval+1)*Devices[i].HeartbeatInterval; } } //***************************************************************************** void tNMEA2000::SendHeartbeat(int iDev) { if ( !IsValidDevice(iDev) ) return; tN2kMsg N2kMsg; SetHeartbeat(N2kMsg,Devices[iDev].HeartbeatInterval,0); SendMsg(N2kMsg,iDev); } //***************************************************************************** void tNMEA2000::SendHeartbeat(bool force) { if ( !IsActiveNode() ) return; for (int iDev=0; iDev0 && Devices[iDev].NextHeartbeatSentTime=DeviceCount) return result; N2kMsg.CheckDestination(); if (DeviceIndex>=0) { N2kMsg.ForceSource(Devices[DeviceIndex].N2kSource); } else { DeviceIndex=0; } if ( N2kMsg.Source>N2kMaxCanBusAddress && N2kMsg.PGN!=N2kPGNIsoAddressClaim ) return false; // CAN bus address range is 0-251. Anyway allow ISO address claim mgs. unsigned long canId=N2ktoCanID(N2kMsg.Priority,N2kMsg.PGN,N2kMsg.Source, N2kMsg.Destination); if ( canId==0 ) { // PGN validity - N2ktoCanID returns 0 for invalid PGN // if (ForwardStream!=0 && ForwardType==tNMEA2000::fwdt_Text) { ForwardStream->print(F("Invalid PGN ")); ForwardStream->println(N2kMsg.PGN); } return false; } if (N2kMode==N2km_ListenOnly) return false; // Do not send anything on listen only mode if (N2kMsg.PGN==0) return false; switch (dbMode) { case dm_None: N2kMsgDbg("Send PGN:"); N2kMsgDbgln(N2kMsg.PGN); N2kMsgDbg("Can ID:"); N2kMsgDbgln(canId); if ( IsAddressClaimStarted(DeviceIndex) && N2kMsg.PGN!=N2kPGNIsoAddressClaim ) return false; if (N2kMsg.DataLen<=8 && !IsFastPacket(N2kMsg) ) { // We can send single frame DbgPrintBuf(N2kMsg.DataLen, N2kMsg.Data,true); result=SendFrame(canId, N2kMsg.DataLen, N2kMsg.Data,false); if (!result && ForwardStream!=0 && ForwardType==tNMEA2000::fwdt_Text) { ForwardStream->print(F("PGN ")); ForwardStream->print(N2kMsg.PGN); ForwardStream->println(F(" send failed")); } N2kPrintFreeMemory("SendMsg, single frame"); } else { // Send it as fast packet in multiple frames #if !defined(N2K_NO_ISO_MULTI_PACKET_SUPPORT) if ( N2kMsg.IsTPMessage() ) { result=StartSendTPMessage(N2kMsg,DeviceIndex); } else #endif { unsigned char temp[8]; // {0,0,0,0,0,0,0,0}; int cur=0; int frames=(N2kMsg.DataLen>6 ? (N2kMsg.DataLen-6-1)/7+1+1 : 1 ); int Order=GetSequenceCounter(N2kMsg.PGN,DeviceIndex)<<5; result=true; for (int i = 0; iprint(F("PGN ")); ForwardStream->print(N2kMsg.PGN); ForwardStream->print(F(", frame:")); ForwardStream->print(i); ForwardStream->print(F("/")); ForwardStream->print(frames); ForwardStream->println(F(" send failed")); } } } }; if ( ForwardOwnMessages() ) ForwardMessage(N2kMsg); break; case dm_ClearText: result=true; N2kMsg.Print(ForwardStream); break; case dm_Actisense: result=true; N2kMsg.SendInActisenseFormat(ForwardStream); break; } return result; } //***************************************************************************** void tNMEA2000::SetDebugMode(tDebugMode _dbMode) { dbMode=_dbMode; } //***************************************************************************** bool tNMEA2000::IsFastPacketPGN(unsigned long PGN) { if ( IsFastPacketSystemMessage(PGN) || IsMandatoryFastPacketMessage(PGN) || ( FastPacketMessages[0]==0 && IsDefaultFastPacketMessage(PGN) ) || IsProprietaryFastPacketMessage(PGN) ) return true; int i; for (unsigned char igroup=0; (igroup=0x80) return false; // Special handling for force to send message as single frame. return IsFastPacketPGN(N2kMsg.PGN); } //***************************************************************************** bool tNMEA2000::CheckKnownMessage(unsigned long PGN, bool &SystemMessage, bool &FastPacket) { int i; // return true; FastPacket=false; SystemMessage=false; if ( PGN==0 ) { return false; } // Unknown // Check other messages if ( SingleFrameMessages[0]==0 && IsDefaultSingleFrameMessage(PGN) ) return true; if ( (FastPacket=IsMandatoryFastPacketMessage(PGN)) ) return true; if ( FastPacketMessages[0]==0 && (FastPacket=IsDefaultFastPacketMessage(PGN))==true ) return true; // Check system messages if ( IsSingleFrameSystemMessage(PGN) || (FastPacket=IsFastPacketSystemMessage(PGN))==true ) { SystemMessage=true; return true; } // Check user defined messages for (unsigned char igroup=0; (igroup(1,tNMEA2000::N2kMin(nPackets,TP_MAX_FRAMES)); } //***************************************************************************** void tNMEA2000::SendTPCM_CTS(unsigned long PGN, unsigned char Destination, int iDev, unsigned char nPackets, unsigned char NextPacketNumber) { tN2kMsg N2kMsg; if ( !IsActiveNode() ) return; N2kMsg.Source=Devices[iDev].N2kSource; N2kMsg.Destination=Destination; N2kMsg.SetPGN(TP_CM); N2kMsg.Priority=6; N2kMsg.AddByte(TP_CM_CTS); N2kMsg.AddByte(TPCtsPackets(nPackets)); N2kMsg.AddByte(NextPacketNumber); N2kMsg.AddByte(0xff); // Reserved; N2kMsg.AddByte(0xff); // Reserved; N2kMsg.Add3ByteInt(PGN); SendMsg(N2kMsg,iDev); } //***************************************************************************** void tNMEA2000::SendTPCM_EndAck(unsigned long PGN, unsigned char Destination, int iDev, uint16_t nBytes, unsigned char nPackets) { tN2kMsg N2kMsg; if ( !IsActiveNode() ) return; N2kMsg.Source=Devices[iDev].N2kSource; N2kMsg.Destination=Destination; N2kMsg.SetPGN(TP_CM); N2kMsg.Priority=6; N2kMsg.AddByte(TP_CM_ACK); N2kMsg.Add2ByteUInt(nBytes); N2kMsg.AddByte(nPackets); N2kMsg.AddByte(0xff); // Reserved; N2kMsg.Add3ByteInt(PGN); SendMsg(N2kMsg,iDev); } //***************************************************************************** void tNMEA2000::SendTPCM_Abort(unsigned long PGN, unsigned char Destination, int iDev, unsigned char AbortCode) { tN2kMsg N2kMsg; if ( !IsActiveNode() ) return; N2kMsg.Source=Devices[iDev].N2kSource; N2kMsg.Destination=Destination; N2kMsg.SetPGN(TP_CM); N2kMsg.Priority=6; N2kMsg.AddByte(TP_CM_Abort); N2kMsg.AddByte(AbortCode); N2kMsg.AddByte(0xff); // Reserved; N2kMsg.AddByte(0xff); // Reserved; N2kMsg.AddByte(0xff); // Reserved; N2kMsg.Add3ByteInt(PGN); SendMsg(N2kMsg,iDev); } //***************************************************************************** // Caller should take care of not calling this after all has been done. // Use HasAllTPDTSent for checking. bool tNMEA2000::SendTPDT(int iDev) { tN2kMsg N2kMsg; N2kMsg.Source=Devices[iDev].N2kSource; N2kMsg.Destination=Devices[iDev].PendingTPMsg.Destination; N2kMsg.SetPGN(TP_DT); N2kMsg.Priority=6; N2kMsg.AddByte(Devices[iDev].NextDTSequence+1); int iByteToSend=Devices[iDev].NextDTSequence*7; for ( int i=0; i<7; i++,iByteToSend++ ) { if ( iByteToSend=Devices[iDev].PendingTPMsg.DataLen ); } //***************************************************************************** bool tNMEA2000::TestHandleTPMessage(unsigned long PGN, unsigned char Source, unsigned char Destination, unsigned char len, unsigned char *buf, uint8_t &MsgIndex) { MsgIndex=MaxN2kCANMsgs; int iDev=FindSourceDeviceIndex(Destination); if ( PGN==TP_CM ) { unsigned char TP_CM_Control=buf[0]; int Index=5; unsigned long TransportPGN=GetBuf3ByteUInt(Index,buf); switch (TP_CM_Control) { case TP_CM_BAM: case TP_CM_RTS: { N2kMsgDbgln("Got TP Command"); Index=1; uint16_t nBytes=GetBuf2ByteUInt(Index,buf); uint8_t TPMaxPackets=buf[Index++]; //Index++; // reserved FindFreeCANMsgIndex(TransportPGN,Source,Destination,true,MsgIndex); if (MsgIndex==MaxN2kCANMsgs) { // No free msg place N2kMsgDbgln("No free msg slot"); if ( (TP_CM_Control==TP_CM_RTS) && (iDev>=0) ) { // If it was for us and not broadcast, we need to abort transport SendTPCM_Abort(TransportPGN,Source,iDev,TP_CM_AbortBusy); } } else { // Start transport N2kMsgDbg("Use msg slot: "); N2kMsgDbgln(MsgIndex); bool FastPacket; N2kCANMsgBuf[MsgIndex].KnownMessage=CheckKnownMessage(TransportPGN,N2kCANMsgBuf[MsgIndex].SystemMessage,FastPacket); if ( nBytes < tN2kMsg::MaxDataLen && // Currently we can handle only tN2kMsg::MaxDataLen long messages (N2kCANMsgBuf[MsgIndex].KnownMessage || !HandleOnlyKnownMessages()) ) { N2kCANMsgBuf[MsgIndex].FreeMsg=false; N2kCANMsgBuf[MsgIndex].N2kMsg.Init(7 /* Priority? */,TransportPGN,Source,Destination); N2kCANMsgBuf[MsgIndex].CopiedLen=0; N2kCANMsgBuf[MsgIndex].LastFrame=0; N2kCANMsgBuf[MsgIndex].N2kMsg.DataLen=nBytes; N2kCANMsgBuf[MsgIndex].N2kMsg.SetIsTPMessage(); N2kCANMsgBuf[MsgIndex].TPMaxPackets=TPMaxPackets; if ( (TP_CM_Control==TP_CM_RTS) && (iDev>=0) ) { // If it was for us and not broadcast, we need to response SendTPCM_CTS(TransportPGN,Source,iDev,N2kCANMsgBuf[MsgIndex].TPMaxPackets,N2kCANMsgBuf[MsgIndex].LastFrame+1); N2kCANMsgBuf[MsgIndex].TPRequireCTS=TPCtsPackets(N2kCANMsgBuf[MsgIndex].TPMaxPackets); } else { N2kCANMsgBuf[MsgIndex].TPMaxPackets=0xff; // TPMaxPackets>0 indicates that it is TP message } } else { // Too long or unknown if ( (TP_CM_Control==TP_CM_RTS) && (iDev>=0) ) { // If it was for us and not broadcast, we need to response SendTPCM_Abort(TransportPGN,Source,iDev,TP_CM_AbortBusy); // Abort } } } break; } case TP_CM_CTS: if ( !IsValidDevice(iDev) ) break; // Should never fail N2kMsgDbgln("Got TP CTS"); if ( IsBroadcast(Devices[iDev].PendingTPMsg.Destination) ) break; // We should not get controls for broadcast TP msg if ( Devices[iDev].PendingTPMsg.PGN!=TransportPGN ) { // Some failure on communication EndSendTPMessage(iDev); // Should we retry from beginning? break; } // Now respond with next data packets if ( buf[1]>0 ) { // Note that with 0, receiver wants to have break if ( buf[2]-1!=Devices[iDev].NextDTSequence ) { // We got sequence error EndSendTPMessage(iDev); // Should we retry from beginning? break; } uint8_t MaxTPSequences=buf[1]; bool TPDTResult=true; for ( uint8_t iSeq=0; TPDTResult &&iSeq=N2kCANMsgBuf[MsgIndex].N2kMsg.DataLen ) { // all done N2kCANMsgBuf[MsgIndex].Ready=true; if ( N2kCANMsgBuf[MsgIndex].TPRequireCTS>0 && iDev>=0 ) { // send response SendTPCM_EndAck(N2kCANMsgBuf[MsgIndex].N2kMsg.PGN,Source,iDev,N2kCANMsgBuf[MsgIndex].N2kMsg.DataLen,N2kCANMsgBuf[MsgIndex].LastFrame); } } else { if ( N2kCANMsgBuf[MsgIndex].TPRequireCTS>0 && ((N2kCANMsgBuf[MsgIndex].LastFrame)%N2kCANMsgBuf[MsgIndex].TPRequireCTS)==0 ) { // send response SendTPCM_CTS(N2kCANMsgBuf[MsgIndex].N2kMsg.PGN,Source,iDev,N2kCANMsgBuf[MsgIndex].TPMaxPackets,N2kCANMsgBuf[MsgIndex].LastFrame+1); } } } else { // Wrong packet - either we lost packet or sender sends wrong, so free this N2kMsgDbg("Invalid packet: "); N2kMsgDbgln(buf[0]); if ( N2kCANMsgBuf[MsgIndex].TPRequireCTS>0 && iDev>=0 ) { // We need to abort transport SendTPCM_Abort(N2kCANMsgBuf[MsgIndex].N2kMsg.PGN,Source,iDev,TP_CM_AbortTimeout); // Abort transport } N2kCANMsgBuf[MsgIndex].FreeMessage(); } if ( !N2kCANMsgBuf[MsgIndex].Ready ) MsgIndex=MaxN2kCANMsgs; } return true; // We handled message } return false; } //***************************************************************************** bool tNMEA2000::StartSendTPMessage(const tN2kMsg& msg, int iDev) { if ( !IsValidDevice(iDev) ) return false; if ( Devices[iDev].PendingTPMsg.PGN!=0 ) return false; // No room for sending TP message int result=false; Devices[iDev].PendingTPMsg=msg; Devices[iDev].NextDTSequence=0; Devices[iDev].NextDTSendTime=millis()+50; if ( IsBroadcast(msg.Destination) ) { // Start with BAM result=SendTPCM_BAM(iDev); } else { result=SendTPCM_RTS(iDev); } if ( !result ) EndSendTPMessage(iDev); // Currently no retry return result; } //***************************************************************************** void tNMEA2000::EndSendTPMessage(int iDev) { Devices[iDev].PendingTPMsg.Clear(); Devices[iDev].NextDTSendTime=0; } //***************************************************************************** void tNMEA2000::SendPendingTPMessages() { for (int iDev=0; iDev respond from all devices for (iDev=0; iDevPGN!=PGNForGroupFunction && pGroupFunctionHandler->PGN!=0; pGroupFunctionHandler=pGroupFunctionHandler->pNext); if ( pGroupFunctionHandler==0 || !pGroupFunctionHandler->Handle(N2kMsg,GroupFunctionCode, PGNForGroupFunction,iDev) ) { // Default handler is at last on the list, so currently do nothing here } } //***************************************************************************** // Document https://www.nmea.org/Assets/20140109%20nmea-2000-corrigendum-tc201401031%20pgn%20126208.pdf // defines that systems should respond to NMEA Request/Command/Acknowledge group function PGN 126208. // On the document it is not clear can request be send as broadcast, so we handle it, if we can. void tNMEA2000::HandleGroupFunction(const tN2kMsg &N2kMsg) { tN2kGroupFunctionCode GroupFunctionCode; unsigned long PGNForGroupFunction; int iDev=FindSourceDeviceIndex(N2kMsg.Destination); if ( !tNMEA2000::IsBroadcast(N2kMsg.Destination) && iDev==-1) return; // if destination is not for us, we do nothing if (!tN2kGroupFunctionHandler::Parse(N2kMsg,GroupFunctionCode,PGNForGroupFunction)) return; N2kMsgDbg("Group function: "); N2kMsgDbgln(PGNForGroupFunction); if ( tNMEA2000::IsBroadcast(N2kMsg.Destination) ) { // broadcast -> respond from all devices for (iDev=0; iDevprint(F("Start address claim for device ")); ForwardStream->println(iDev); } SendIsoAddressClaim(0xff,iDev); Devices[iDev].AddressClaimStarted=millis(); } } //***************************************************************************** bool tNMEA2000::IsAddressClaimStarted(int iDev) { // Reset address claim after timeout if ( Devices[iDev].AddressClaimStarted!=0 ) { if ( Devices[iDev].AddressClaimStarted+N2kAddressClaimTimeoutN2kMaxCanBusAddress ) Devices[DeviceIndex].N2kSource=0; } else { Devices[DeviceIndex].N2kSource=N2kNullCanBusAddress; // Force null address = cannot claim address return; } FoundSame=false; // Check that we do not have same on our list for (int i=0; iGetPGN()==0; MsgHandler=MsgHandler->pNext) MsgHandler->HandleMsg(N2kMsg); // Loop through specific pgn handlers for ( ;MsgHandler!=0 && MsgHandler->GetPGN()<=N2kMsg.PGN; MsgHandler=MsgHandler->pNext) { if ( MsgHandler->GetPGN()==N2kMsg.PGN ) MsgHandler->HandleMsg(N2kMsg); } } //***************************************************************************** void tNMEA2000::SetMsgHandler(void (*_MsgHandler)(const tN2kMsg &N2kMsg)) { MsgHandler=_MsgHandler; } //***************************************************************************** void tNMEA2000::AttachMsgHandler(tMsgHandler *_MsgHandler) { if ( _MsgHandler==0 ) return; if ( _MsgHandler->pNMEA2000==this ) return; // Already attached DetachMsgHandler(_MsgHandler); if ( MsgHandlers==0 ) { MsgHandlers=_MsgHandler; } else { tMsgHandler *MsgHandler=MsgHandlers; if ( MsgHandler->GetPGN()>_MsgHandler->GetPGN() ) { // Add to first _MsgHandler->pNext=MsgHandler; MsgHandlers=_MsgHandler; } else { for ( ; MsgHandler->pNext!=0 && MsgHandler->pNext->GetPGN()<_MsgHandler->GetPGN(); MsgHandler=MsgHandler->pNext ); _MsgHandler->pNext=MsgHandler->pNext; MsgHandler->pNext=_MsgHandler; } } _MsgHandler->pNMEA2000=this; } //***************************************************************************** void tNMEA2000::DetachMsgHandler(tMsgHandler *_MsgHandler) { if ( _MsgHandler==0 || _MsgHandler->pNMEA2000==0 ) return; tMsgHandler *MsgHandler=_MsgHandler->pNMEA2000->MsgHandlers; if ( MsgHandler==_MsgHandler ) { // Is this at first _MsgHandler->pNMEA2000->MsgHandlers=MsgHandler->pNext; } else { for ( ; MsgHandler!=0 && MsgHandler->pNext!=_MsgHandler; MsgHandler=MsgHandler->pNext ); if ( MsgHandler!=0 ) MsgHandler->pNext=_MsgHandler->pNext; } _MsgHandler->pNext=0; _MsgHandler->pNMEA2000=0; } //***************************************************************************** void tNMEA2000::SetISORqstHandler(bool(*ISORequestHandler)(unsigned long RequestedPGN, unsigned char Requester, int DeviceIndex)) { ISORqstHandler=ISORequestHandler; } #if !defined(N2K_NO_GROUP_FUNCTION_SUPPORT) //***************************************************************************** void tNMEA2000::RemoveGroupFunctionHandler(tN2kGroupFunctionHandler *pGroupFunctionHandler) { if (pGroupFunctionHandler==0 || pGroupFunctionHandlers==0 ) return; tN2kGroupFunctionHandler* pPrevGroupFunctionHandler=pGroupFunctionHandlers; // Handle, if first if ( pPrevGroupFunctionHandler==pGroupFunctionHandler ) { pGroupFunctionHandlers=pPrevGroupFunctionHandler->pNext; } else { for ( ;pPrevGroupFunctionHandler!=0 && pPrevGroupFunctionHandler->pNext!=pGroupFunctionHandler; pPrevGroupFunctionHandler = pPrevGroupFunctionHandler->pNext); if ( pPrevGroupFunctionHandler!=0 && pPrevGroupFunctionHandler->pNext==pGroupFunctionHandler ) { pPrevGroupFunctionHandler->pNext=pGroupFunctionHandler->pNext; } } pGroupFunctionHandler->pNext=0; } //***************************************************************************** void tNMEA2000::AddGroupFunctionHandler(tN2kGroupFunctionHandler *pGroupFunctionHandler) { if (pGroupFunctionHandler==0) return; RemoveGroupFunctionHandler(pGroupFunctionHandler); // Add to the end on the list if ( pGroupFunctionHandlers==0 ) { // If there is none set, put it to first pGroupFunctionHandlers=pGroupFunctionHandler; } else { tN2kGroupFunctionHandler* pLastGroupFunctionHandler; // find last for (pLastGroupFunctionHandler = pGroupFunctionHandlers; pLastGroupFunctionHandler->pNext != 0 && pLastGroupFunctionHandler->pNext->PGN != 0; pLastGroupFunctionHandler = pLastGroupFunctionHandler->pNext); // Insert the new handler before the default handler if the default handler is present. if ( pLastGroupFunctionHandler->pNext != 0 && pLastGroupFunctionHandler->pNext->PGN == 0 ) { pGroupFunctionHandler->pNext = pLastGroupFunctionHandler->pNext; } // Add the new handler to the list. pLastGroupFunctionHandler->pNext = pGroupFunctionHandler; } } #endif //***************************************************************************** /// ISO Acknowledgement void SetN2kPGN59392(tN2kMsg &N2kMsg, unsigned char Control, unsigned char GroupFunction, unsigned long PGN) { N2kMsg.SetPGN(59392L); N2kMsg.Priority=6; N2kMsg.AddByte(Control); N2kMsg.AddByte(GroupFunction); N2kMsg.AddByte(0xff); // Reserved N2kMsg.AddByte(0xff); // Reserved N2kMsg.AddByte(0xff); // Reserved N2kMsg.Add3ByteInt(PGN); } //***************************************************************************** // ISO Address Claim void SetN2kPGN60928(tN2kMsg &N2kMsg, unsigned long UniqueNumber, int ManufacturerCode, unsigned char DeviceFunction, unsigned char DeviceClass, unsigned char DeviceInstance, unsigned char SystemInstance, unsigned char IndustryGroup ) { N2kMsg.SetPGN(N2kPGNIsoAddressClaim); N2kMsg.Priority=6; N2kMsg.Add4ByteUInt((UniqueNumber&0x1FFFFF) | ((unsigned long)(ManufacturerCode&0x7ff))<<21); N2kMsg.AddByte(DeviceInstance); N2kMsg.AddByte(DeviceFunction); N2kMsg.AddByte((DeviceClass&0x7f)<<1); N2kMsg.AddByte( 0x80 | ((IndustryGroup&0x7)<<4) | (SystemInstance&0x0f) ); } //***************************************************************************** // ISO Address Claim void SetN2kPGN60928(tN2kMsg &N2kMsg, uint64_t Name) { N2kMsg.SetPGN(N2kPGNIsoAddressClaim); N2kMsg.Priority=6; N2kMsg.AddUInt64(Name); } //***************************************************************************** // Product Information void SetN2kPGN126996(tN2kMsg &N2kMsg, unsigned int N2kVersion, unsigned int ProductCode, const char *ModelID, const char *SwCode, const char *ModelVersion, const char *ModelSerialCode, unsigned char CertificationLevel, unsigned char LoadEquivalency) { N2kMsg.SetPGN(N2kPGNProductInformation); N2kMsg.Priority=6; N2kMsg.Add2ByteUInt(N2kVersion); N2kMsg.Add2ByteUInt(ProductCode); N2kMsg.AddStr(ModelID, Max_N2kModelID_len); N2kMsg.AddStr(SwCode, Max_N2kSwCode_len); N2kMsg.AddStr(ModelVersion, Max_N2kModelVersion_len); N2kMsg.AddStr(ModelSerialCode, Max_N2kModelSerialCode_len); N2kMsg.AddByte(CertificationLevel); N2kMsg.AddByte(LoadEquivalency); } bool ParseN2kPGN126996(const tN2kMsg& N2kMsg, unsigned short &N2kVersion, unsigned short &ProductCode, int ModelIDSize, char *ModelID, int SwCodeSize, char *SwCode, int ModelVersionSize, char *ModelVersion, int ModelSerialCodeSize, char *ModelSerialCode, unsigned char &CertificationLevel, unsigned char &LoadEquivalency) { if (N2kMsg.PGN!=N2kPGNProductInformation) return false; int Index=0; N2kVersion=N2kMsg.Get2ByteUInt(Index); ProductCode=N2kMsg.Get2ByteUInt(Index); N2kMsg.GetStr(ModelIDSize,ModelID,Max_N2kModelID_len,0xff,Index); N2kMsg.GetStr(SwCodeSize,SwCode,Max_N2kSwCode_len,0xff,Index); N2kMsg.GetStr(ModelVersionSize,ModelVersion,Max_N2kModelVersion_len,0xff,Index); N2kMsg.GetStr(ModelSerialCodeSize,ModelSerialCode,Max_N2kModelSerialCode_len,0xff,Index); CertificationLevel=N2kMsg.GetByte(Index); LoadEquivalency=N2kMsg.GetByte(Index); return true; } //***************************************************************************** size_t ProgmemStrLen(const char *str) { size_t len; if (str==0) return 0; for (len=0; pgm_read_byte(&(str[len]))!=0; len++ ); return len; } //***************************************************************************** size_t StrLen(const char *str) { if (str==0) return 0; return strlen(str); } //***************************************************************************** // Configuration information void SetN2kPGN126998(tN2kMsg &N2kMsg, const char *ManufacturerInformation, const char *InstallationDescription1, const char *InstallationDescription2, bool UsePgm) { size_t TotalLen; size_t MaxLen=tN2kMsg::MaxDataLen-6; // Each field has 2 extra bytes size_t ManInfoLen; size_t InstDesc1Len; size_t InstDesc2Len; if ( UsePgm ) { ManInfoLen=ProgmemStrLen(ManufacturerInformation); InstDesc1Len=ProgmemStrLen(InstallationDescription1); InstDesc2Len=ProgmemStrLen(InstallationDescription2); } else { ManInfoLen=StrLen(ManufacturerInformation); InstDesc1Len=StrLen(InstallationDescription1); InstDesc2Len=StrLen(InstallationDescription2); } if ( ManInfoLen>Max_N2kConfigurationInfoField_len ) ManInfoLen=Max_N2kConfigurationInfoField_len; if ( InstDesc1Len>Max_N2kConfigurationInfoField_len ) InstDesc1Len=Max_N2kConfigurationInfoField_len; if ( InstDesc2Len>Max_N2kConfigurationInfoField_len ) InstDesc2Len=Max_N2kConfigurationInfoField_len; TotalLen=0; if (TotalLen+ManInfoLen>MaxLen) ManInfoLen=MaxLen-TotalLen; TotalLen+=ManInfoLen; if (TotalLen+InstDesc1Len>MaxLen) InstDesc1Len=MaxLen-TotalLen; TotalLen+=InstDesc1Len; if (TotalLen+InstDesc2Len>MaxLen) InstDesc2Len=MaxLen-TotalLen; TotalLen+=InstDesc2Len; N2kMsg.SetPGN(N2kPGNConfigurationInformation); N2kMsg.Priority=6; // InstallationDescription1 N2kMsg.AddByte(InstDesc1Len+2); N2kMsg.AddByte(0x01); N2kMsg.AddStr(InstallationDescription1,InstDesc1Len,UsePgm); // InstallationDescription2 N2kMsg.AddByte(InstDesc2Len+2); N2kMsg.AddByte(0x01); N2kMsg.AddStr(InstallationDescription2,InstDesc2Len,UsePgm); // ManufacturerInformation N2kMsg.AddByte(ManInfoLen+2); N2kMsg.AddByte(0x01); N2kMsg.AddStr(ManufacturerInformation,ManInfoLen,UsePgm); } bool ParseN2kPGN126998(const tN2kMsg& N2kMsg, size_t &ManufacturerInformationSize, char *ManufacturerInformation, size_t &InstallationDescription1Size, char *InstallationDescription1, size_t &InstallationDescription2Size, char *InstallationDescription2) { if (N2kMsg.PGN!=N2kPGNConfigurationInformation) return false; int Index=0; return ( N2kMsg.GetVarStr(InstallationDescription1Size,InstallationDescription1,Index) && N2kMsg.GetVarStr(InstallationDescription2Size,InstallationDescription2,Index) && N2kMsg.GetVarStr(ManufacturerInformationSize,ManufacturerInformation,Index) ); } //***************************************************************************** // Iso request void SetN2kPGN59904(tN2kMsg &N2kMsg, uint8_t Destination, unsigned long RequestedPGN) { N2kMsg.SetPGN(59904L); N2kMsg.Destination=Destination; N2kMsg.Priority=6; N2kMsg.Add3ByteInt(RequestedPGN); } bool ParseN2kPGN59904(const tN2kMsg &N2kMsg, unsigned long &RequestedPGN) { int result=((N2kMsg.DataLen>=3) && (N2kMsg.DataLen<=8)); RequestedPGN=0; if (result) { int Index=0; RequestedPGN=N2kMsg.Get3ByteUInt(Index); } return result; } //***************************************************************************** // PGN List (Transmit and Receive) void SetN2kPGN126464(tN2kMsg &N2kMsg, uint8_t Destination, tN2kPGNList tr, const unsigned long *PGNs) { unsigned long PGN; N2kMsg.SetPGN(126464L); N2kMsg.Destination=Destination; N2kMsg.Priority=6; N2kMsg.AddByte(tr); for (int i=0; (PGN=pgm_read_dword(&PGNs[i]))!=0; i++) { N2kMsg.Add3ByteInt(PGN); } } #if !defined(N2K_NO_HEARTBEAT_SUPPORT) //***************************************************************************** // Heartbeat void SetN2kPGN126993(tN2kMsg &N2kMsg, uint32_t timeInterval_ms, uint8_t statusByte) { N2kMsg.SetPGN(126993L); N2kMsg.Priority=7; if ( timeInterval_ms>MaxHeartbeatInterval ) { N2kMsg.Add2ByteUInt(65532); } else { N2kMsg.Add2ByteUInt((uint16_t)(timeInterval_ms)); } N2kMsg.AddByte(statusByte); N2kMsg.AddByte(0xff); // Reserved N2kMsg.Add4ByteUInt(0xffffffff); // Reserved } #endif