Skip to content

Commit

Permalink
User ISO Request Handler
Browse files Browse the repository at this point in the history
Added capability for user to define ISO Request message handler.
  • Loading branch information
thomasonw committed Jul 21, 2016
1 parent d80b511 commit b5beec2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
23 changes: 22 additions & 1 deletion NMEA2000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ tNMEA2000::tNMEA2000() {
MaxN2kCANMsgs=0;

MsgHandler=0;
RqstHandler=0;

ForwardStream=&Serial;
N2kSource[0]=0;
DeviceReady=false;
Expand Down Expand Up @@ -517,6 +519,7 @@ void tNMEA2000::SendConfigurationInformation(int DeviceIndex) {
void tNMEA2000::HandleISORequest(const tN2kMsg &N2kMsg) {
unsigned long RequestedPGN;
unsigned char Destination=0xff;
tN2kMsg N2kMsgR;
int iDev=FindSourceDeviceIndex(N2kMsg.Destination);

if ( N2kMsg.Destination!=0xff && iDev==-1) return; // if destination is not for us, we do nothing
Expand All @@ -534,7 +537,13 @@ void tNMEA2000::HandleISORequest(const tN2kMsg &N2kMsg) {
case 126998L: /* Configuration information */
SendConfigurationInformation(iDev);
break;
// ToDo: Write default handler so that user can add their own.
default:
if (RqstHandler!=0 ) RqstHandler(N2kMsg); /* See if user wants to handle it */
else {
SetN2kPGNISOAcknowledgement(N2kMsgR,1,0xff,RequestedPGN); // Send NAK
N2kMsgR.Destination = Destination; // Direct the response to original requester.
SendMsg(N2kMsgR);
}
}
}

Expand Down Expand Up @@ -645,6 +654,11 @@ void tNMEA2000::SetMsgHandler(void (*_MsgHandler)(const tN2kMsg &N2kMsg)) {
MsgHandler=_MsgHandler;
}

//*****************************************************************************
void tNMEA2000::SetRqstHandler(void (*_RqstHandler)(const tN2kMsg &N2kMsg)) {
RqstHandler=_RqstHandler;
}

//*****************************************************************************
/// ISO Acknowledgement
void SetN2kPGN59392(tN2kMsg &N2kMsg, unsigned char Control, unsigned char GroupFunction, unsigned long PGN) {
Expand Down Expand Up @@ -707,6 +721,13 @@ void SetN2kPGN126996(tN2kMsg &N2kMsg, unsigned int N2kVersion, unsigned int Prod

//*****************************************************************************
// Iso request
void SetN2kPGN59904(tN2kMsg &N2kMsg, uint8_t Destination, unsigned long RequestedPGN) {
N2kMsg.SetPGN(59904L);
N2kMsg.Destination=Destination;
N2kMsg.Priority=6;
N2kMsg.Add3ByteInt((unsigned int)RequestedPGN);
}

bool ParseN2kPGN59904(const tN2kMsg &N2kMsg, unsigned long &RequestedPGN) {
int result=(N2kMsg.DataLen==3);
RequestedPGN=0;
Expand Down
17 changes: 14 additions & 3 deletions NMEA2000.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ class tNMEA2000
tN2kCANMsg *N2kCANMsgBuf;
unsigned char MaxN2kCANMsgs;

// Handler callback
void (*MsgHandler)(const tN2kMsg &N2kMsg);
// Handler callbacks
void (*MsgHandler)(const tN2kMsg &N2kMsg); // Normal messages
void (*RqstHandler)(const tN2kMsg &N2kMsg); // 'ISORequest' messages


protected:
// Virtual functions for different interfaces. Currently there are own classes
Expand Down Expand Up @@ -275,7 +277,9 @@ class tNMEA2000
void ParseMessages();

// Set the message handler for incoming N2kMessages.
void SetMsgHandler(void (*_MsgHandler)(const tN2kMsg &N2kMsg));
void SetMsgHandler(void (*_MsgHandler)(const tN2kMsg &N2kMsg)); // Normal messages
void SetRqstHandler(void (*_RqstHandler)(const tN2kMsg &N2kMsg)); // ISORequest messages


// Read address for current device.
// Multidevice support is under construction.
Expand Down Expand Up @@ -345,6 +349,13 @@ inline void SetN2kProductInformation(tN2kMsg &N2kMsg, unsigned int N2kVersion, u
SertificationLevel,LoadEquivalency);
}


void SetN2kPGN59904(tN2kMsg &N2kMsg, uint8_t Destination, unsigned long RequestedPGN);

inline void SetN2kPGNISORequest(tN2kMsg &N2kMsg, uint8_t Destination, unsigned long RequestedPGN) {
SetN2kPGN59904(N2kMsg,Destination,RequestedPGN);
}

bool ParseN2kPGN59904(const tN2kMsg &N2kMsg, unsigned long &RequestedPGN);

inline bool ParseN2kPGNISORequest(const tN2kMsg &N2kMsg, unsigned long &RequestedPGN) {
Expand Down

0 comments on commit b5beec2

Please sign in to comment.