forked from ttlappalainen/NMEA2000
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActisenseReader.cpp
More file actions
164 lines (139 loc) · 5.19 KB
/
Copy pathActisenseReader.cpp
File metadata and controls
164 lines (139 loc) · 5.19 KB
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
/*
ActisenseReader.cpp
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.
This is class for reading Actisense format messages from given stream.
*/
#include "ActisenseReader.h"
#include <string.h>
//*****************************************************************************
tActisenseReader::tActisenseReader() {
ReadStream=0;
ClearBuffer();
}
//*****************************************************************************
void tActisenseReader::ClearBuffer() {
MsgWritePos=0;
byteSum=0;
StartOfTextReceived=false;
MsgIsComing=false;
EscapeReceived=false;
}
//*****************************************************************************
bool tActisenseReader::AddByteToBuffer(char NewByte) {
if (MsgWritePos>=MAX_STREAM_MSG_BUF_LEN) return false;
MsgBuf[MsgWritePos]=NewByte;
MsgWritePos++;
if ( MsgBuf[1]+3!=MsgWritePos ) byteSum+=NewByte; // !Do not add CRC to byteSum
return true;
}
#define Escape 0x10
#define StartOfText 0x02
#define EndOfText 0x03
#define MsgTypeN2k 0x93
//*****************************************************************************
bool tActisenseReader::CheckMessage(tN2kMsg &N2kMsg) {
N2kMsg.Clear();
if (MsgWritePos!=MsgBuf[1]+3) {
return false; // Length does not match. Add type, length and crc
}
uint8_t CheckSum = (uint8_t)((byteSum == 0) ? 0 : (256 - byteSum));
if ( CheckSum!=MsgBuf[MsgWritePos-1] ) {
return false; // Checksum does not match
}
if (MsgBuf[12]>tN2kMsg::MaxDataLen) {
return false; // Too long data
}
N2kMsg.Priority=MsgBuf[2];
N2kMsg.PGN=(unsigned long)(MsgBuf[3]) | (unsigned long)(MsgBuf[4])<<8 | (unsigned long)(MsgBuf[5])<<16;
N2kMsg.Destination=MsgBuf[6];
N2kMsg.Source=MsgBuf[7];
// N2kMsg.MsgTime=*((unsigned long *)(&(MsgBuf[8]))); // this causes warning: dereferencing type-punned pointer will break strict-aliasing rules
memcpy(&(N2kMsg.MsgTime), &(MsgBuf[8]), 4);
N2kMsg.DataLen=MsgBuf[12];
for (int i=13, j=0; i<MsgWritePos-1; i++, j++) N2kMsg.Data[j]=MsgBuf[i];
return true;
}
//*****************************************************************************
// Read Actisense formatted NMEA2000 message from stream
// Actisense Format:
// <10><02><93><length (1)><priority (1)><PGN (3)><destination (1)><source (1)><time (4)><len (1)><data (len)><CRC (1)><10><03>
bool tActisenseReader::GetMessageFromStream(tN2kMsg &N2kMsg) {
bool result=false;
if (ReadStream==0)
return false;
int NewByte;
while ((NewByte = ReadStream->read()) != -1 && !result) {
// Serial.println((char)NewByte,HEX);
if (MsgIsComing) {
if (EscapeReceived) {
switch (NewByte) {
case Escape: // Escaped Escape
EscapeReceived=false;
if (!AddByteToBuffer(NewByte)) ClearBuffer();
break;
case EndOfText: // Message ready
switch (MsgBuf[0]) {
case MsgTypeN2k:
result=CheckMessage(N2kMsg);
break;
default:
result=false;
}
ClearBuffer();
break;
case StartOfText: // Start new message
ClearBuffer();
StartOfTextReceived=true;
break;
default: // Error
ClearBuffer();
}
} else {
if (NewByte==Escape) {
EscapeReceived=true;
} else {
if (!AddByteToBuffer(NewByte)) ClearBuffer();
}
}
} else {
switch (NewByte) {
case StartOfText:
StartOfTextReceived=false;
if (EscapeReceived) {
ClearBuffer();
StartOfTextReceived=true;
}
break;
default:
if (StartOfTextReceived) {
StartOfTextReceived=false;
MsgIsComing=true;
AddByteToBuffer(NewByte);
}
}
EscapeReceived=(NewByte==Escape);
}
}
return result;
}
//*****************************************************************************
void tActisenseReader::ParseMessages() {
tN2kMsg N2kMsg;
while (GetMessageFromStream(N2kMsg)) {
if (MsgHandler!=0) MsgHandler(N2kMsg);
}
}