-
Notifications
You must be signed in to change notification settings - Fork 1
/
DCSProtocolHandler.cpp
195 lines (158 loc) · 4.3 KB
/
DCSProtocolHandler.cpp
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
/*
* Copyright (C) 2012,2013 by Jonathan Naylor G4KLX
* Copyright (c) 2017 by Thomas A. Early N7TAE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "DCSProtocolHandler.h"
#include "Utils.h"
// #define DUMP_TX
const unsigned int BUFFER_LENGTH = 2000U;
CDCSProtocolHandler::CDCSProtocolHandler(unsigned int port, const std::string& addr) :
m_socket(addr, port),
m_type(DC_NONE),
m_buffer(NULL),
m_length(0U),
m_yourAddress(),
m_yourPort(0U),
m_myPort(port)
{
m_buffer = new unsigned char[BUFFER_LENGTH];
}
CDCSProtocolHandler::~CDCSProtocolHandler()
{
delete[] m_buffer;
}
bool CDCSProtocolHandler::open()
{
return m_socket.open();
}
unsigned int CDCSProtocolHandler::getPort() const
{
return m_myPort;
}
bool CDCSProtocolHandler::writeData(const CAMBEData& data)
{
unsigned char buffer[100U];
unsigned int length = data.getDCSData(buffer, 100U);
#if defined(DUMP_TX)
CUtils::dump("Sending Data", buffer, length);
#endif
return m_socket.write(buffer, length, data.getYourAddress(), data.getYourPort());
}
bool CDCSProtocolHandler::writePoll(const CPollData& poll)
{
unsigned char buffer[25U];
unsigned int length = poll.getDCSData(buffer, 25U);
#if defined(DUMP_TX)
CUtils::dump("Sending Poll", buffer, length);
#endif
return m_socket.write(buffer, length, poll.getYourAddress(), poll.getYourPort());
}
bool CDCSProtocolHandler::writeConnect(const CConnectData& connect)
{
unsigned char buffer[520U];
unsigned int length = connect.getDCSData(buffer, 520U);
#if defined(DUMP_TX)
CUtils::dump("Sending Connect", buffer, length);
#endif
return m_socket.write(buffer, length, connect.getYourAddress(), connect.getYourPort());
}
DCS_TYPE CDCSProtocolHandler::read()
{
bool res = true;
// Loop until we have no more data from the socket or we have data for the higher layers
while (res)
res = readPackets();
return m_type;
}
bool CDCSProtocolHandler::readPackets()
{
m_type = DC_NONE;
// No more data?
int length = m_socket.read(m_buffer, BUFFER_LENGTH, m_yourAddress, m_yourPort);
if (length <= 0)
return false;
m_length = length;
if (m_buffer[0] == '0' && m_buffer[1] == '0' && m_buffer[2] == '0' && m_buffer[3] == '1') {
if (m_length == 100U) {
m_type = DC_DATA;
return false;
}
} else if (m_buffer[0] == 'E' && m_buffer[1] == 'E' && m_buffer[2] == 'E' && m_buffer[3] == 'E') {
// CUtils::dump("Status data", m_buffer, m_length);
return true;
} else {
switch (m_length) {
case 17U:
case 22U:
m_type = DC_POLL;
return false;
case 14U:
case 19U:
case 519U:
m_type = DC_CONNECT;
return false;
case 35U:
// CUtils::dump("Status data", m_buffer, m_length);
return true;
default:
break;
}
}
// An unknown type
// CUtils::dump("Unknown packet type from DCS", m_buffer, m_length);
return true;
}
CAMBEData* CDCSProtocolHandler::readData()
{
if (m_type != DC_DATA)
return NULL;
CAMBEData* data = new CAMBEData;
bool res = data->setDCSData(m_buffer, m_length, m_yourAddress, m_yourPort, m_myPort);
if (!res) {
delete data;
return NULL;
}
return data;
}
CPollData* CDCSProtocolHandler::readPoll()
{
if (m_type != DC_POLL)
return NULL;
CPollData* poll = new CPollData;
bool res = poll->setDCSData(m_buffer, m_length, m_yourAddress, m_yourPort, m_myPort);
if (!res) {
delete poll;
return NULL;
}
return poll;
}
CConnectData* CDCSProtocolHandler::readConnect()
{
if (m_type != DC_CONNECT)
return NULL;
CConnectData* connect = new CConnectData;
bool res = connect->setDCSData(m_buffer, m_length, m_yourAddress, m_yourPort, m_myPort);
if (!res) {
delete connect;
return NULL;
}
return connect;
}
void CDCSProtocolHandler::close()
{
m_socket.close();
}