forked from ttlappalainen/NMEA2000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
N2kMsg.h
137 lines (117 loc) · 6.75 KB
/
N2kMsg.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
/*
N2kMsg.h
Copyright (c) 2015-2016 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.
Definition for NMEA2000 message class used in my NMEA2000 libraries.
*/
#ifndef _tN2kMsg_H_
#define _tN2kMsg_H_
#include "N2kStream.h"
#include "N2kDef.h"
#include <stdint.h>
const double N2kDoubleNA=-1e9;
const uint8_t N2kUInt8NA=0xff;
const int8_t N2kInt8NA=0x7f;
const uint16_t N2kUInt16NA=0xffff;
const int16_t N2kInt16NA=0x7fff;
const uint32_t N2kUInt32NA=0xffffffff;
const int32_t N2kInt32NA=0x7fffffff;
#define BIT(n) (1 << n)
inline bool N2kIsNA(double v) { return v==N2kDoubleNA; }
inline bool N2kIsNA(uint8_t v) { return v==N2kUInt8NA; }
inline bool N2kIsNA(int8_t v) { return v==N2kInt8NA; }
inline bool N2kIsNA(uint16_t v) { return v==N2kUInt16NA; }
inline bool N2kIsNA(int16_t v) { return v==N2kInt16NA; }
inline bool N2kIsNA(uint32_t v) { return v==N2kUInt32NA; }
inline bool N2kIsNA(int32_t v) { return v==N2kInt32NA; }
void SetBuf8ByteDouble(double v, double precision, int &index, unsigned char *buf);
void SetBuf4ByteDouble(double v, double precision, int &index, unsigned char *buf);
void SetBuf4ByteUDouble(double v, double precision, int &index, unsigned char *buf);
void SetBuf3ByteDouble(double v, double precision, int &index, unsigned char *buf);
void SetBuf2ByteDouble(double v, double precision, int &index, unsigned char *buf);
void SetBuf2ByteUDouble(double v, double precision, int &index, unsigned char *buf);
void SetBuf1ByteDouble(double v, double precision, int &index, unsigned char *buf);
void SetBuf1ByteUDouble(double v, double precision, int &index, unsigned char *buf);
void SetBuf2ByteInt(int16_t v, int &index, unsigned char *buf);
void SetBuf2ByteUInt(uint16_t v, int &index, unsigned char *buf);
void SetBuf3ByteInt(int32_t v, int &index, unsigned char *buf);
void SetBuf4ByteUInt(uint32_t v, int &index, unsigned char *buf);
void SetBufUInt64(uint64_t v, int &index, unsigned char *buf);
void SetBufStr(const char *str, int len, int &index, unsigned char *buf);
int16_t GetBuf2ByteInt(int &index, const unsigned char *buf);
uint16_t GetBuf2ByteUInt(int &index, const unsigned char *buf);
uint32_t GetBuf4ByteUInt(int &index, const unsigned char *buf);
double GetBuf1ByteDouble(double precision, int &index, const unsigned char *buf, double def=0);
double GetBuf1ByteUDouble(double precision, int &index, const unsigned char *buf, double def=-1);
double GetBuf2ByteDouble(double precision, int &index, const unsigned char *buf, double def=0);
double GetBuf2ByteUDouble(double precision, int &index, const unsigned char *buf, double def=-1);
double GetBuf3ByteDouble(double precision, int &index, const unsigned char *buf, double def=0);
double GetBuf4ByteDouble(double precision, int &index, const unsigned char *buf, double def=0);
double GetBuf4ByteUDouble(double precision, int &index, const unsigned char *buf, double def=-1);
double GetBuf8ByteDouble(double precision, int &index, const unsigned char *buf, double def=0);
class tN2kMsg
{
public:
static const int MaxDataLen=223; // with fast packet 1. frame can have 6 byte and rest 31 frames 7 bytes
unsigned char Priority;
unsigned long PGN;
mutable unsigned char Source;
mutable unsigned char Destination;
int DataLen;
unsigned char Data[MaxDataLen];
unsigned long MsgTime;
public:
tN2kMsg(unsigned char _Source=15);
void SetPGN(unsigned long _PGN);
void ForceSource(unsigned char _Source) const { Source=_Source; }
void CheckDestination() const { if ( (PGN & 0xff)!=0 ) Destination=0xff; } // We can send to specified destination only for PGN:s low byte=0
void Init(unsigned char _Priority, unsigned long _PGN, unsigned char _Source, unsigned char _Destination=0xff);
void Clear();
bool IsValid() const { return (PGN!=0 && DataLen>0); }
void Add8ByteDouble(double v, double precision, double UndefVal=N2kDoubleNA);
void Add4ByteDouble(double v, double precision, double UndefVal=N2kDoubleNA);
void Add4ByteUDouble(double v, double precision, double UndefVal=N2kDoubleNA);
void Add3ByteDouble(double v, double precision, double UndefVal=N2kDoubleNA);
void Add2ByteUDouble(double v, double precision, double UndefVal=N2kDoubleNA);
void Add2ByteDouble(double v, double precision, double UndefVal=N2kDoubleNA);
void Add1ByteDouble(double v, double precision, double UndefVal=N2kDoubleNA);
void Add1ByteUDouble(double v, double precision, double UndefVal=N2kDoubleNA);
void Add2ByteInt(int16_t v);
void Add2ByteUInt(uint16_t v);
void Add3ByteInt(int32_t v);
void Add4ByteUInt(uint32_t v);
void AddUInt64(uint64_t v);
void AddByte(unsigned char v);
void AddStr(const char *str, int len);
unsigned char GetByte(int &Index) const;
int16_t Get2ByteInt(int &Index, int16_t def=0x7fff) const;
uint16_t Get2ByteUInt(int &Index, uint16_t def=0xffff) const;
uint32_t Get4ByteUInt(int &Index, uint32_t def=0xffffffff) const;
double Get1ByteDouble(double precision, int &Index, double def=N2kDoubleNA) const;
double Get1ByteUDouble(double precision, int &Index, double def=N2kDoubleNA) const;
double Get2ByteDouble(double precision, int &Index, double def=N2kDoubleNA) const;
double Get2ByteUDouble(double precision, int &Index, double def=N2kDoubleNA) const;
double Get3ByteDouble(double precision, int &Index, double def=N2kDoubleNA) const;
double Get4ByteDouble(double precision, int &Index, double def=N2kDoubleNA) const;
double Get4ByteUDouble(double precision, int &Index, double def=N2kDoubleNA) const;
double Get8ByteDouble(double precision, int &Index, double def=N2kDoubleNA) const;
bool GetStr(char *StrBuf, int Length, int &Index) const;
bool Set2ByteUInt(uint16_t v, int &Index);
void Print(N2kStream *port, bool NoData=false) const;
void SendInActisenseFormat(N2kStream *port) const;
};
void PrintBuf(N2kStream *port, unsigned char len, const unsigned char *pData, bool AddLF=false);
#endif