-
Notifications
You must be signed in to change notification settings - Fork 0
/
N2kGroupFunction.h
218 lines (177 loc) · 10.4 KB
/
N2kGroupFunction.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
N2kGroupFunction.h
Copyright (c) 2015-2020 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.
*/
#ifndef _N2kGroupFunction_H_
#define _N2kGroupFunction_H_
#ifndef ARDUINO
#include <cstring>
#endif
#include "NMEA2000_CompilerDefns.h"
#include "N2kMsg.h"
#if !defined(N2K_NO_GROUP_FUNCTION_SUPPORT)
enum tN2kGroupFunctionCode {
N2kgfc_Request=0,
N2kgfc_Command=1,
N2kgfc_Acknowledge=2,
N2kgfc_Read=3,
N2kgfc_ReadReply=4,
N2kgfc_Write=5,
N2kgfc_WriteReply=6
};
enum tN2kGroupFunctionPGNErrorCode {
N2kgfPGNec_Acknowledge=0,
N2kgfPGNec_PGNNotSupported=1,
N2kgfPGNec_PGNTemporarilyNotAvailable=2,
N2kgfPGNec_AccessDenied=3,
N2kgfPGNec_RequestOrCommandNotSupported=4,
N2kgfPGNec_DefinerTagNotSupported=5,
N2kgfPGNec_ReadOrWriteNotSupported=6
};
enum tN2kGroupFunctionTransmissionOrPriorityErrorCode {
N2kgfTPec_Acknowledge=0,
N2kgfTPec_TransmitIntervalOrPriorityNotSupported=1,
N2kgfTPec_TransmitIntervalIsLessThanMeasurementInterval=2,
N2kgfTPec_AccessDenied=3,
N2kgfTPec_RequestNotSupported=4
};
enum tN2kGroupFunctionParameterErrorCode {
N2kgfpec_Acknowledge=0,
N2kgfpec_InvalidRequestOrCommandParameterField=1,
N2kgfpec_TemporarilyUnableToComply=2,
N2kgfpec_RequestOrCommandParameterOutOfRange=3,
N2kgfpec_AccessDenied=4,
N2kgfpec_RequestOrCommandNotSupported=5,
N2kgfpec_ReadOrWriteIsNotSupported=6
};
class tNMEA2000;
class tN2kGroupFunctionHandler {
public:
template <typename T> void MatchRequestField(T FieldVal, T MatchVal, T Mask, bool &Match, tN2kGroupFunctionParameterErrorCode &ErrorCode)
{
if ( (FieldVal&Mask)!=MatchVal ) {
ErrorCode=N2kgfpec_RequestOrCommandParameterOutOfRange;
Match=false;
} else ErrorCode=N2kgfpec_Acknowledge;
}
void MatchRequestField(const char * FieldVal, const char * MatchVal, bool &Match, tN2kGroupFunctionParameterErrorCode &ErrorCode)
{
Match&=(strcmp(FieldVal,MatchVal)==0);
ErrorCode = ( Match ? N2kgfpec_Acknowledge : N2kgfpec_RequestOrCommandParameterOutOfRange );
}
private:
tN2kGroupFunctionHandler *pNext;
friend class tNMEA2000;
protected:
unsigned long PGN;
bool Proprietary;
tNMEA2000 *pNMEA2000;
protected:
// This is default handler for Complex Request transmission interval setting. Overwrite it, if your PGN will support
// changing interval.
virtual tN2kGroupFunctionTransmissionOrPriorityErrorCode GetRequestGroupFunctionTransmissionOrPriorityErrorCode(uint32_t TransmissionInterval);
virtual bool HandleRequest(const tN2kMsg &N2kMsg,
uint32_t TransmissionInterval,
uint16_t TransmissionIntervalOffset,
uint8_t NumberOfParameterPairs,
int iDev);
virtual bool HandleCommand(const tN2kMsg &N2kMsg, uint8_t PrioritySetting, uint8_t NumberOfParameterPairs, int iDev);
// This function handles Acknowledge group function, whis is response for Request, Command, ReadFields or WriteFields
// group function. So you need to override this, if your device will send one of those commands.
virtual bool HandleAcknowledge(const tN2kMsg &N2kMsg,
tN2kGroupFunctionPGNErrorCode PGNErrorCode,
tN2kGroupFunctionTransmissionOrPriorityErrorCode TransmissionOrPriorityErrorCode,
uint8_t NumberOfParameterPairs,
int iDev);
virtual bool HandleReadFields(const tN2kMsg &N2kMsg,
uint16_t ManufacturerCode, // This will be set to 0xffff for non-propprietary PNGs
uint8_t IndustryGroup, // This will be set to 0xff for non-propprietary PNGs
uint8_t UniqueID,
uint8_t NumberOfSelectionPairs,
uint8_t NumberOfParameterPairs,
int iDev);
// Under construction
virtual bool HandleReadFieldsReply(const tN2kMsg &N2kMsg,int iDev);
virtual bool HandleWriteFields(const tN2kMsg &N2kMsg,
uint16_t ManufacturerCode, // This will be set to 0xffff for non-propprietary PNGs
uint8_t IndustryGroup, // This will be set to 0xff for non-propprietary PNGs
uint8_t UniqueID,
uint8_t NumberOfSelectionPairs,
uint8_t NumberOfParameterPairs,
int iDev);
// Under construction
virtual bool HandleWriteFieldsReply(const tN2kMsg &N2kMsg,int iDev);
public:
tN2kGroupFunctionHandler(tNMEA2000 *_pNMEA2000, unsigned long _PGN);
virtual bool Handle(const tN2kMsg &N2kMsg, tN2kGroupFunctionCode GroupFunctionCode, unsigned long PGNForGroupFunction, int iDev);
// Static functions
static unsigned long GetPGNForGroupFunction(const tN2kMsg &N2kMsg);
// Parse group function code and PGN for it
static bool Parse(const tN2kMsg &N2kMsg,
tN2kGroupFunctionCode &GroupFunctionCode,
unsigned long &PGNForGroupFunction);
static bool ParseRequestParams(const tN2kMsg &N2kMsg,
uint32_t &TransmissionInterval,
uint16_t &TransmissionIntervalOffset,
uint8_t &NumberOfParameterPairs);
static bool StartParseRequestPairParameters(const tN2kMsg &N2kMsg, int &Index);
static bool ParseCommandParams(const tN2kMsg &N2kMsg,
uint8_t &PrioritySetting,
uint8_t &NumberOfParameterPairs);
static bool StartParseCommandPairParameters(const tN2kMsg &N2kMsg, int &Index);
static bool ParseAcknowledgeParams(const tN2kMsg &N2kMsg,
tN2kGroupFunctionPGNErrorCode &PGNErrorCode,
tN2kGroupFunctionTransmissionOrPriorityErrorCode &TransmissionOrPriorityErrorCode,
uint8_t &NumberOfParameterPairs);
static bool StartParseReadOrWriteParameters(const tN2kMsg &N2kMsg, bool Proprietary, int &Index);
static bool ParseReadOrWriteParams(const tN2kMsg &N2kMsg,
uint16_t &ManufacturerCode,
uint8_t &IndustryGroup,
uint8_t &UniqueID,
uint8_t &NumberOfSelectionPairs,
uint8_t &NumberOfParameterPairs,
bool Proprietary=false);
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);
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);
static void SetStartAcknowledge(tN2kMsg &N2kMsg, unsigned char Destination, unsigned long PGN,
tN2kGroupFunctionPGNErrorCode PGNErrorCode,
tN2kGroupFunctionTransmissionOrPriorityErrorCode TransmissionOrPriorityErrorCode,
uint8_t NumberOfParameterPairs=0);
static void ChangePNGErrorCode(tN2kMsg &N2kMsg, tN2kGroupFunctionPGNErrorCode PGNErrorCode);
static void AddAcknowledgeParameter(tN2kMsg &N2kMsg,
uint8_t ParameterPairIndex,
tN2kGroupFunctionParameterErrorCode ErrorCode=N2kgfpec_ReadOrWriteIsNotSupported);
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);
};
#endif
#endif