NMEA2000 Library  0.1
Library to handle NMEA 2000 Communication written in C++
N2kGroupFunction.h
Go to the documentation of this file.
1/*
2 * N2kGroupFunction.h
3
4 * Copyright (c) 2015-2024 Timo Lappalainen, Kave Oy, www.kave.fi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22*/
23
24/************************************************************************/
57#ifndef _N2kGroupFunction_H_
58#define _N2kGroupFunction_H_
59
60#ifndef ARDUINO
61#include <cstring>
62#endif
64#include "N2kMsg.h"
65
66#if !defined(N2K_NO_GROUP_FUNCTION_SUPPORT)
67
68#define N2k_KEEP_TRANSMISSION_INTERVAL 0xffffffff
69#define N2k_RESTORE_TRANSMISSION_INTERVAL 0xfffffffe // Restore default
70#define N2k_MAX_TRANSMISSION_INTERVAL 0xfffffff0
71#define N2k_MAX_TRANSMISSION_INTERVAL_OFFSET 0xfffc
72
73/************************************************************************/
159 };
160
161/************************************************************************/
184 };
185
186/************************************************************************/
206 };
207
208/************************************************************************/
231 };
232
233class tNMEA2000;
234/************************************************************************/
261 public:
262 /**********************************************************************/
277 template <typename T> void MatchRequestField(T FieldVal, T MatchVal, T Mask, bool &Match, tN2kGroupFunctionParameterErrorCode &ErrorCode)
278 {
279 if ( (FieldVal&Mask)!=MatchVal ) {
281 Match=false;
282 } else ErrorCode=N2kgfpec_Acknowledge;
283 }
284
285 /*********************************************************************/
298 void MatchRequestField(const char * FieldVal, const char * MatchVal, bool &Match, tN2kGroupFunctionParameterErrorCode &ErrorCode)
299 {
300 Match&=(strcmp(FieldVal,MatchVal)==0);
302 }
303
304 private:
307 friend class tNMEA2000;
308
309 protected:
311 unsigned long PGN;
316
317 protected:
318 // deprecated! Use new version with offset test too.
319 // deprecated attribute does not work with overrided methods, so easier just force users to update code.
320 // virtual tN2kGroupFunctionTransmissionOrPriorityErrorCode GetRequestGroupFunctionTransmissionOrPriorityErrorCode(uint32_t TransmissionInterval) __attribute__ ((deprecated));
321
322 /**********************************************************************/
358 uint32_t TransmissionInterval,
359 uint16_t TransmissionIntervalOffset,
360 bool UseIntervalLimits=false,
361 uint32_t IntervalMax=N2k_MAX_TRANSMISSION_INTERVAL,
362 uint32_t IntervalMin=50,
363 bool UseOffsetLimits=false,
364 uint16_t OffsetMax=N2k_MAX_TRANSMISSION_INTERVAL_OFFSET
365 );
366
367 /**********************************************************************/
393 virtual bool HandleRequest(const tN2kMsg &N2kMsg,
394 uint32_t TransmissionInterval,
395 uint16_t TransmissionIntervalOffset,
396 uint8_t NumberOfParameterPairs,
397 int iDev);
398
399 /**********************************************************************/
419 virtual bool HandleCommand(const tN2kMsg &N2kMsg, uint8_t PrioritySetting, uint8_t NumberOfParameterPairs, int iDev);
420
421 /**********************************************************************/
442 virtual bool HandleAcknowledge(const tN2kMsg &N2kMsg,
444 tN2kGroupFunctionTransmissionOrPriorityErrorCode TransmissionOrPriorityErrorCode,
445 uint8_t NumberOfParameterPairs,
446 int iDev);
447
448 /**********************************************************************/
472 virtual bool HandleReadFields(const tN2kMsg &N2kMsg,
473 uint16_t ManufacturerCode,
474 uint8_t IndustryGroup,
475 uint8_t UniqueID,
476 uint8_t NumberOfSelectionPairs,
477 uint8_t NumberOfParameterPairs,
478 int iDev);
479
480 /**********************************************************************/
493 virtual bool HandleReadFieldsReply(const tN2kMsg &N2kMsg,int iDev);
494
495 /**********************************************************************/
519 virtual bool HandleWriteFields(const tN2kMsg &N2kMsg,
520 uint16_t ManufacturerCode,
521 uint8_t IndustryGroup,
522 uint8_t UniqueID,
523 uint8_t NumberOfSelectionPairs,
524 uint8_t NumberOfParameterPairs,
525 int iDev);
526
527 /**********************************************************************/
539 virtual bool HandleWriteFieldsReply(const tN2kMsg &N2kMsg,int iDev);
540
541 public:
542 /**********************************************************************/
549 tN2kGroupFunctionHandler(tNMEA2000 *_pNMEA2000, unsigned long _PGN);
550
551 /**********************************************************************/
578 virtual bool Handle(const tN2kMsg &N2kMsg, tN2kGroupFunctionCode GroupFunctionCode, unsigned long PGNForGroupFunction, int iDev);
579
580 /**********************************************************************/
592 static unsigned long GetPGNForGroupFunction(const tN2kMsg &N2kMsg);
593
594 /**********************************************************************/
608 static bool Parse(const tN2kMsg &N2kMsg,
609 tN2kGroupFunctionCode &GroupFunctionCode,
610 unsigned long &PGNForGroupFunction);
611
612 /**********************************************************************/
627 static bool ParseRequestParams(const tN2kMsg &N2kMsg,
628 uint32_t &TransmissionInterval,
629 uint16_t &TransmissionIntervalOffset,
630 uint8_t &NumberOfParameterPairs);
631
632 /**********************************************************************/
644 static bool StartParseRequestPairParameters(const tN2kMsg &N2kMsg, int &Index);
645
646 /**********************************************************************/
660 static bool ParseCommandParams(const tN2kMsg &N2kMsg,
661 uint8_t &PrioritySetting,
662 uint8_t &NumberOfParameterPairs);
663
664 /**********************************************************************/
676 static bool StartParseCommandPairParameters(const tN2kMsg &N2kMsg, int &Index);
677
678 /**********************************************************************/
694 static bool ParseAcknowledgeParams(const tN2kMsg &N2kMsg,
695 tN2kGroupFunctionPGNErrorCode &PGNErrorCode,
696 tN2kGroupFunctionTransmissionOrPriorityErrorCode &TransmissionOrPriorityErrorCode,
697 uint8_t &NumberOfParameterPairs);
698
699 /**********************************************************************/
711 static bool StartParseReadOrWriteParameters(const tN2kMsg &N2kMsg, bool Proprietary, int &Index);
712
713 /**********************************************************************/
732 static bool ParseReadOrWriteParams(const tN2kMsg &N2kMsg,
733 uint16_t &ManufacturerCode,
734 uint8_t &IndustryGroup,
735 uint8_t &UniqueID,
736 uint8_t &NumberOfSelectionPairs,
737 uint8_t &NumberOfParameterPairs,
738 bool Proprietary=false);
739
740 /**********************************************************************/
761 static void SetStartReadReply(tN2kMsg &N2kMsg, unsigned char Destination, unsigned long PGN,
762 uint16_t ManufacturerCode,
763 uint8_t IndustryGroup,
764 uint8_t UniqueID,
765 uint8_t NumberOfSelectionPairs,
766 uint8_t NumberOfParameterPairs,
767 bool Proprietary);
768
769 /**********************************************************************/
790 static void SetStartWriteReply(tN2kMsg &N2kMsg, unsigned char Destination, unsigned long PGN,
791 uint16_t ManufacturerCode,
792 uint8_t IndustryGroup,
793 uint8_t UniqueID,
794 uint8_t NumberOfSelectionPairs,
795 uint8_t NumberOfParameterPairs,
796 bool Proprietary);
797
798 /**********************************************************************/
814 static void SetStartAcknowledge(tN2kMsg &N2kMsg, unsigned char Destination, unsigned long PGN,
816 tN2kGroupFunctionTransmissionOrPriorityErrorCode TransmissionOrPriorityErrorCode,
817 uint8_t NumberOfParameterPairs=0);
818
819 /**********************************************************************/
830 static void ChangePNGErrorCode(tN2kMsg &N2kMsg, tN2kGroupFunctionPGNErrorCode PGNErrorCode);
831
832 /**********************************************************************/
845 static void ChangeTransmissionOrPriorityErrorCode(tN2kMsg &N2kMsg, tN2kGroupFunctionTransmissionOrPriorityErrorCode TransmissionOrPriorityErrorCode);
846
847 /**********************************************************************/
859 static void AddAcknowledgeParameter(tN2kMsg &N2kMsg,
860 uint8_t ParameterPairIndex,
862
863 /**********************************************************************/
882 static void SendAcknowledge(tNMEA2000 *pNMEA2000, unsigned char Destination, int iDev, unsigned long PGN,
884 tN2kGroupFunctionTransmissionOrPriorityErrorCode TransmissionOrPriorityErrorCode,
885 uint8_t NumberOfParameterPairs=0,
887
888};
889
890#endif
891
892#endif
#define N2k_MAX_TRANSMISSION_INTERVAL_OFFSET
tN2kGroupFunctionParameterErrorCode
Error Codes Command used by acknowledge group function.
@ N2kgfpec_AccessDenied
@ N2kgfpec_TemporarilyUnableToComply
@ N2kgfpec_RequestOrCommandNotSupported
@ N2kgfpec_InvalidRequestOrCommandParameterField
@ N2kgfpec_ReadOrWriteIsNotSupported
@ N2kgfpec_RequestOrCommandParameterOutOfRange
@ N2kgfpec_Acknowledge
tN2kGroupFunctionPGNErrorCode
PGN error code used by acknowledge group function.
@ N2kgfPGNec_PGNTemporarilyNotAvailable
@ N2kgfPGNec_AccessDenied
@ N2kgfPGNec_Acknowledge
@ N2kgfPGNec_RequestOrCommandNotSupported
@ N2kgfPGNec_DefinerTagNotSupported
@ N2kgfPGNec_PGNNotSupported
@ N2kgfPGNec_ReadOrWriteNotSupported
tN2kGroupFunctionTransmissionOrPriorityErrorCode
Error codes Transmit interval used by acknowledge group function.
@ N2kgfTPec_TransmitIntervalIsLessThanMeasurementInterval
@ N2kgfTPec_AccessDenied
@ N2kgfTPec_Acknowledge
@ N2kgfTPec_TransmitIntervalOrPriorityNotSupported
@ N2kgfTPec_RequestNotSupported
tN2kGroupFunctionCode
FunctionCode for the group function.
@ N2kgfc_Command
@ N2kgfc_Write
@ N2kgfc_Request
@ N2kgfc_WriteReply
@ N2kgfc_ReadReply
@ N2kgfc_Acknowledge
@ N2kgfc_Read
#define N2k_MAX_TRANSMISSION_INTERVAL
This File contains the class tN2kMsg and all necessary functions to handle a NMEA2000 Message.
Specific compiler definition to adapt the library to your needs.
Base handler class for Group Functions.
static bool ParseRequestParams(const tN2kMsg &N2kMsg, uint32_t &TransmissionInterval, uint16_t &TransmissionIntervalOffset, uint8_t &NumberOfParameterPairs)
Parse parameters from request group function message.
static bool ParseCommandParams(const tN2kMsg &N2kMsg, uint8_t &PrioritySetting, uint8_t &NumberOfParameterPairs)
Parse parameters from command group function message.
virtual bool HandleAcknowledge(const tN2kMsg &N2kMsg, tN2kGroupFunctionPGNErrorCode PGNErrorCode, tN2kGroupFunctionTransmissionOrPriorityErrorCode TransmissionOrPriorityErrorCode, uint8_t NumberOfParameterPairs, int iDev)
Default handle function for acknowledge group function.
static bool ParseAcknowledgeParams(const tN2kMsg &N2kMsg, tN2kGroupFunctionPGNErrorCode &PGNErrorCode, tN2kGroupFunctionTransmissionOrPriorityErrorCode &TransmissionOrPriorityErrorCode, uint8_t &NumberOfParameterPairs)
Parse parameters from acknowledge group function message.
bool Proprietary
Flag, if the Parameter Group is proprietary
static bool StartParseRequestPairParameters(const tN2kMsg &N2kMsg, int &Index)
Get start Index of pair parameters from request group function message.
virtual bool HandleReadFieldsReply(const tN2kMsg &N2kMsg, int iDev)
Handle read fields reply group function.
void MatchRequestField(T FieldVal, T MatchVal, T Mask, bool &Match, tN2kGroupFunctionParameterErrorCode &ErrorCode)
Template class to test does field on group function match to PGN field value.
virtual bool Handle(const tN2kMsg &N2kMsg, tN2kGroupFunctionCode GroupFunctionCode, unsigned long PGNForGroupFunction, int iDev)
Handle group function message.
static bool Parse(const tN2kMsg &N2kMsg, tN2kGroupFunctionCode &GroupFunctionCode, unsigned long &PGNForGroupFunction)
Parse group function code and requested/commanded etc. PGN from group function message.
virtual bool HandleWriteFields(const tN2kMsg &N2kMsg, uint16_t ManufacturerCode, uint8_t IndustryGroup, uint8_t UniqueID, uint8_t NumberOfSelectionPairs, uint8_t NumberOfParameterPairs, int iDev)
Handle response for write fields group function.
tN2kGroupFunctionHandler(tNMEA2000 *_pNMEA2000, unsigned long _PGN)
Construct a new tN2kGroupFunctionHandler object.
static unsigned long GetPGNForGroupFunction(const tN2kMsg &N2kMsg)
Get requested/commanded etc. PGN from group function message.
static bool ParseReadOrWriteParams(const tN2kMsg &N2kMsg, uint16_t &ManufacturerCode, uint8_t &IndustryGroup, uint8_t &UniqueID, uint8_t &NumberOfSelectionPairs, uint8_t &NumberOfParameterPairs, bool Proprietary=false)
Parse parameters from read or write group function message.
static void SendAcknowledge(tNMEA2000 *pNMEA2000, unsigned char Destination, int iDev, unsigned long PGN, tN2kGroupFunctionPGNErrorCode PGNErrorCode, tN2kGroupFunctionTransmissionOrPriorityErrorCode TransmissionOrPriorityErrorCode, uint8_t NumberOfParameterPairs=0, tN2kGroupFunctionParameterErrorCode ParameterErrorCodeForAll=N2kgfpec_Acknowledge)
Send out an acknowledge group function message.
virtual bool HandleWriteFieldsReply(const tN2kMsg &N2kMsg, int iDev)
Handle write fields reply group function.
virtual tN2kGroupFunctionTransmissionOrPriorityErrorCode GetRequestGroupFunctionTransmissionOrPriorityErrorCode(uint32_t TransmissionInterval, uint16_t TransmissionIntervalOffset, bool UseIntervalLimits=false, uint32_t IntervalMax=N2k_MAX_TRANSMISSION_INTERVAL, uint32_t IntervalMin=50, bool UseOffsetLimits=false, uint16_t OffsetMax=N2k_MAX_TRANSMISSION_INTERVAL_OFFSET)
Get request group function transmission or priority error code.
static void SetStartWriteReply(tN2kMsg &N2kMsg, unsigned char Destination, unsigned long PGN, uint16_t ManufacturerCode, uint8_t IndustryGroup, uint8_t UniqueID, uint8_t NumberOfSelectionPairs, uint8_t NumberOfParameterPairs, bool Proprietary)
Setup start parameters for write reply group function message.
static void SetStartAcknowledge(tN2kMsg &N2kMsg, unsigned char Destination, unsigned long PGN, tN2kGroupFunctionPGNErrorCode PGNErrorCode, tN2kGroupFunctionTransmissionOrPriorityErrorCode TransmissionOrPriorityErrorCode, uint8_t NumberOfParameterPairs=0)
Setting up the group function message for Acknowledge.
virtual bool HandleReadFields(const tN2kMsg &N2kMsg, uint16_t ManufacturerCode, uint8_t IndustryGroup, uint8_t UniqueID, uint8_t NumberOfSelectionPairs, uint8_t NumberOfParameterPairs, int iDev)
Handle response for read fields group function.
tNMEA2000 * pNMEA2000
NMEA2000 object in order to send messages to the bus
static bool StartParseCommandPairParameters(const tN2kMsg &N2kMsg, int &Index)
Get start Index of pair parameters from command group function message.
static void ChangeTransmissionOrPriorityErrorCode(tN2kMsg &N2kMsg, tN2kGroupFunctionTransmissionOrPriorityErrorCode TransmissionOrPriorityErrorCode)
Change transmission interval or priority error code for acknowledge group function message.
unsigned long PGN
Parameter Group Number (PGN) of this Group Function.
virtual bool HandleCommand(const tN2kMsg &N2kMsg, uint8_t PrioritySetting, uint8_t NumberOfParameterPairs, int iDev)
Handle response for command group function.
void MatchRequestField(const char *FieldVal, const char *MatchVal, bool &Match, tN2kGroupFunctionParameterErrorCode &ErrorCode)
Class to test does string field on group function match to PGN field value.
virtual bool HandleRequest(const tN2kMsg &N2kMsg, uint32_t TransmissionInterval, uint16_t TransmissionIntervalOffset, uint8_t NumberOfParameterPairs, int iDev)
Handle response for request group function.
static void AddAcknowledgeParameter(tN2kMsg &N2kMsg, uint8_t ParameterPairIndex, tN2kGroupFunctionParameterErrorCode ErrorCode=N2kgfpec_ReadOrWriteIsNotSupported)
Add parameter error code to acknowledge group function message.
static void ChangePNGErrorCode(tN2kMsg &N2kMsg, tN2kGroupFunctionPGNErrorCode PGNErrorCode)
Change the PGN error code for a acknowledge group function message.
static bool StartParseReadOrWriteParameters(const tN2kMsg &N2kMsg, bool Proprietary, int &Index)
Get start index of pair parameters on the read or write group function message.
static void SetStartReadReply(tN2kMsg &N2kMsg, unsigned char Destination, unsigned long PGN, uint16_t ManufacturerCode, uint8_t IndustryGroup, uint8_t UniqueID, uint8_t NumberOfSelectionPairs, uint8_t NumberOfParameterPairs, bool Proprietary)
Setup start parameters for read reply group function message.
This class contains all the data of an NMEA2000 message.
Definition: N2kMsg.h:656
tNMEA2000 device class definition.
Definition: NMEA2000.h:138