-
Notifications
You must be signed in to change notification settings - Fork 0
/
timetrax.cpp
281 lines (247 loc) · 6.96 KB
/
timetrax.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* $DateTime: 2015/08/23 13:00:12 $
* $Id: //depot/sircond/timetrax.cpp#12 $
* $Change: 2154 $
*
* Copyright (C) 2015 Swarga Research (http://www.swarga-research.com)
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
//!
//! \file timetrax.cpp
//!
//! \brief Definitions for the CTTS100 class.
//!
#include "pch.h"
#include "timetrax.h"
#include "sircon.h"
#include "serial.h"
static const uint32_t MAX_AUTH_ATTEMPTS = 5;
//========================================================================
//!
//! \brief Query the attached TTS-100 for its version information
//!
//! \param[out] major Major version number
//! \param[out] minor Minor version number
//!
//! \retval bool Returns TRUE on success, or FALSE if an error occurred.
//!
//========================================================================
bool CTTS100::QueryVersion (uint32_t& major, uint32_t& minor)
{
uint32_t buflen = 0;
uint8_t buf[256];
int32_t rc;
major = minor = 0;
SetDataRate(9600);
// V IS FOR VERSION...
uint8_t v = 'V';
if (m_port->Send(&v, 1, 1000U) < 0)
{
return false;
}
LogWrite(LEVEL_DEBUG, "Version req sent...");
// READ THE VERSION STRING
buflen = 0;
while ((buflen == 0) || (buf[buflen - 1] != '\n'))
{
LogWrite(LEVEL_DEBUG, "Reading %u bytes...", 256 - buflen);
if ((rc = m_port->Recv(buf + buflen, 256 - buflen, 1000U)) < 0)
{
if (rc == sr::CSerialPort::ErrorTimeout)
{
LogWrite(LEVEL_ERROR, "No response to version request.");
}
else
{
LogWrite(LEVEL_ERROR, "Error %d reading from serial port!", rc);
}
break;
}
buflen += rc;
LogWrite(LEVEL_DEBUG, "%d bytes read, %u staged.", rc, buflen);
if (rc == 0U)
{
break;
}
}
LogWrite(LEVEL_DEBUG, "Response len = %u", buflen);
// ATTEMPT TO PARSE THE RESPONSE
if ((buflen < 9) ||
!strstr(reinterpret_cast<const char*>(buf), "Time Trax"))
{
// NOT A TTS100?
return false;
}
// FIND VERSION
char* p = strstr(reinterpret_cast<char*>(buf), "Version");
if (!p)
{
// NOT A TTS100?
return false;
}
// PARSE VERSION INFO
p += 7; // SKIP PAST THE "Version"
p = strtok(p, ". \r\n");
if (p)
{
major = atoi(p);
p = strtok(NULL, " \r\n");
if (p)
{
minor = atoi(p);
}
}
LogWrite(LEVEL_INFO, "TTS-100 version %u.%u detected.", major, minor);
return true;
}
//========================================================================
//!
//! \internal
//! \brief Authenticate to the TTS-100
//!
//! The TTS-100 requires a special challenge-response dialog before the
//! radio control interface is accessible. This was a lame attempt to
//! prevent third-party software from operating with the TTS-100.
//!
//! \retval bool Returns TRUE if the authentication process was successful.
//!
//========================================================================
bool CTTS100::Authenticate ()
{
uint32_t buflen = 0; // NUMBER OF BYTES CURRENTLY IN BUF
uint8_t buf[256]; // A STAGING BUFFER FOR INCOMING DATA
int32_t rc; // A RESULT CODE FOR SERIAL PORT I/O
// AUTHENTICATION TAKES PLACE AT 9600BPS
SetDataRate(9600);
// A IS FOR AUTHENTICATE...
uint8_t a = 'A';
if (m_port->Send(&a, 1, 1000U) < 0)
{
LogWrite(LEVEL_ERROR, "CTTS100::Authenticate(): Send auth request failed.");
return false;
}
// READ THE CHALLENGE DATA
buflen = 0;
while (buflen < 15)
{
if ((rc = m_port->Recv(buf + buflen, 256 - buflen, 1000U)) <= 0)
{
break;
}
buflen += rc;
}
// VERIFY buf[0] == buf[1] == 0x3e
if ((buflen < 15) || (buf[0] != 0x3e) || (buf[1] != 0x3e))
{
LogWrite(LEVEL_ERROR, "CTTS100::Authenticate(): Unexpected response from TTS100.");
return false;
}
// START WITH A CANNED CHALLENGE RESPONSE
uint8_t respbuf[21] = { 0x22, 0xF1, 0x59, 0x37, 0xF6, 0xA7, 0xFA, 0x3F,
0xD8, 0x5E, 0x27, 0x06, 0x0E, 0x39, 0xFA, 0xE8,
0x75, 0x29, 0x2E, 0x16, 0x50 };
// REPLACE THE IMPORTANT BYTES BASED ON THE CHALLENGE DATA
respbuf[18] = buf[2] ^ 0xad;
respbuf[19] = buf[4] ^ 0x3a;
if (m_port->Send(respbuf, 21, 1000U) < 0)
{
LogWrite(LEVEL_ERROR, "CTTS100::Authenticate(): Send auth response failed.");
return false;
}
// GET THE RESULT
buflen = 0;
while (buflen < 3)
{
if ((rc = m_port->Recv(buf + buflen, 256 - buflen, 5000U)) <= 0)
{
break;
}
buflen += rc;
}
// LOOKING FOR "P<CR><LF>" RESPONSE...
if ((buflen >= 3) && ((buf[0] == 0x50) || (buf[0] == 0x70)))
{
// 'P' IS FOR PROCEED
LogWrite(LEVEL_INFO, "CTTS100::Authenticate(): Auth successful.");
return true;
}
LogWrite(LEVEL_ERROR, "CTTS100::Authenticate(): Auth failed!");
return false;
}
//========================================================================
//!
//! \internal
//! \brief Perform startup initialization
//!
//! Attempt to detect the presence of a TTS-100 interface. If one is
//! detected, attempt to authenticate with it.
//!
//! \retval bool Returns TRUE if the initialization succeeded.
//!
//========================================================================
bool CTTS100::OnStart ()
{
bool isTTS100 = false;
uint32_t cnt = 0;
// VERIFY THAT THE SERIAL PORT EXISTS
if (m_port == 0)
{
LogWrite(LEVEL_CRITICAL, "Error accessing serial port - bailing.");
return false;
}
// ATTEMPT TO AUTO-DETECT A TTS-100 INTERFACE
LogWrite(LEVEL_INFO, "Checking for TimeTrax interface...");
for (cnt = 0U; cnt < MAX_AUTH_ATTEMPTS; ++cnt)
{
uint32_t major, minor;
isTTS100 = QueryVersion(major, minor);
if (isTTS100)
{
break;
}
}
// IF A TTS-100 WAS DETECTED, ATTEMPT TO AUTHENTICATE WITH IT
if (isTTS100)
{
bool auth = false;
for (cnt = 0U; !auth && (cnt < MAX_AUTH_ATTEMPTS); ++cnt)
{
if (Authenticate())
{
LogWrite(LEVEL_INFO, "TimeTrax initialization complete.");
auth = true;
}
else
{
LogWrite(LEVEL_DEBUG, "TimeTrax authentication failed - retrying...");
sleep(1);
}
}
if (!auth)
{
LogWrite(LEVEL_CRITICAL, "Throwing up hands.");
return false;
}
}
else
{
LogWrite(LEVEL_INFO, "No TimeTrax interface was detected.");
}
return CSirCon::OnStart();
}
//!
//! @}
//!