-
Notifications
You must be signed in to change notification settings - Fork 4
/
qttest.cpp
335 lines (279 loc) · 8.75 KB
/
qttest.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Build:
// g++ -fPIC qttest.cpp -I /usr/include/qt5 -l Qt5SerialPort -l Qt5Core
// Sample serial code for downloading/dumping data from a PoolMate Live
// Very little (none) validation, checking, or sanity logic.
#include <unistd.h>
#include <iostream>
#include <inttypes.h>
#include <QtSerialPort/QtSerialPort>
//CRC32 helper function for PoolmateLive protocol
// Non-optimised crc32 calc.
// Doesn't need to be fast so leave readable.
uint32_t crc32a(char *message, int len)
{
int i, j;
uint32_t crc;
char byte;
for (crc=~0,i=0; i<len; ++i)
{
byte = message[i];
for (j = 0; j <= 7; j++)
{
if ((crc>>24 ^ byte) & 0x80)
crc = (crc << 1) ^ 0x04C11DB7;
else
crc = crc << 1;
byte = byte << 1;
}
}
return ~crc;
}
// Locate vendor and product id for serial interface.
QString find()
{
QList<QSerialPortInfo> serialPortInfoList = QSerialPortInfo::availablePorts();
foreach (const QSerialPortInfo &serialPortInfo, serialPortInfoList)
{
if(serialPortInfo.hasProductIdentifier()
&& serialPortInfo.productIdentifier()==0x8b30
&& serialPortInfo.hasVendorIdentifier()
&& serialPortInfo.vendorIdentifier()==0x403)
return serialPortInfo.systemLocation();
}
return QString("");
}
// Captured data packets:
char d1[] = {0x00, 0x63, 0x63, 0x00, 0x00, 0x00}; //5555 prefixed
char d2[] = {0x00, 0x03, 0x00, 0x00, 0x00, 0x00}; // d2 again
//returns 00 42 08 00 00 30 FE 37 5B
char d2b[] = {0x01,0x07,0x16,0x05,0x0f,0x00,0x1c,0x01,0x0b,0x00};
//returns single 00
char d3[] = {0x00, 0x01, 0x00, 0x00, 0x08, 0x08};
//returns 00 2D 01 00 00 4A 86 B8 8A
char d4[] = {0x00, 0x03, 0x04, 0x00, 0x00, 0x00};
//returns 00 33 0B 00 00 09 6A DD 49
char d5[] = {0x00,0x06,0x80,0x00,0x00,0x00}; //Download start
//00 01 00 00 00 00 00 00 00 00 etc
//00 01 2D 95 AA 00 00 00 00 00 etc 45, 149, 170
char d6[] = {0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00}; // workout request #0
char d7[] = {0x01, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00}; // #1
char d8[] = {0x01, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00}; // #8
void display(QByteArray& data)
{
int i;
printf("%d :", data.length());
for (i=0; i < data.length(); ++i)
{
printf("%02x ", (unsigned char)data[i]);
if ((i & 0xff) == 0xff)
{
printf("\n");
}
}
printf("\n");
}
QByteArray data;
void read(QSerialPort *serialPort, unsigned long len)
{
data.clear();
//wait for buffer to fill
while (serialPort->waitForReadyRead(100) != false);
char tmp[len];
serialPort->read(tmp, len); // read back echo
if (serialPort->bytesAvailable())
{
serialPort->read(tmp,1);
data = serialPort->readAll();
}
}
void write(QSerialPort *serialPort, char* data, unsigned long len)
{
QByteArray out(data,len);
serialPort->write(out);
serialPort->waitForBytesWritten(100);
}
void init(QSerialPort* serialPort)
{
// Protocol used by windows app:
serialPort->setBaudRate(250000);
serialPort->setDataBits(QSerialPort::Data8);
serialPort->setParity(QSerialPort::NoParity);
serialPort->setStopBits(QSerialPort::TwoStop);
// serialPort->setFlowControl(QSerialPort::SoftwareControl); // xon/off seems to break with some packets.
serialPort->setFlowControl(QSerialPort::NoFlowControl);
}
// prefix 0x55 packet
void sendandstart(QSerialPort *serialPort, char*data, int len)
{
uint32_t crc = crc32a(data,len);
char buf[256];
char head[]={0x00,0x00,0x55,0x55,0x55,0x55};
memcpy(buf, head, 6);
memcpy(&buf[6], data, len);
memcpy(buf+6+len, &crc, sizeof(uint32_t));
write(serialPort, buf, 6+len+sizeof(uint32_t));
read(serialPort, 6+len+sizeof(uint32_t));
}
// prefix 0xff packet
void sendandwait(QSerialPort *serialPort, char*data, int len)
{
uint32_t crc = crc32a(data,len);
char buf[256];
char head[]={0x00,0x00,0xff,0xff,0xff,0xff};
memcpy(buf, head, 6);
memcpy(&buf[6], data, len);
memcpy(buf+6+len, &crc, sizeof(uint32_t));
write(serialPort, buf, 6+len+sizeof(uint32_t));
read(serialPort,6+len+sizeof(uint32_t));
}
unsigned char * decode_sets(unsigned char* data, int sets)
{
do
{
uint16_t type = *(uint16_t*)data;
if (type == 0x100) //length field
{
double time;
int strks;
if ((data[3] & 0x80) == 0x80)
{
time = (data[2] + ((data[3] & 0x7f)<<8)) / 8.0;
strks = data[4];
data += 6;
}
else // sometimes time field not complete, attempt to detect and skip.
{
printf(" Not complete!\n");
time = (((data[2] & 0x7f) <<8))/8.0;
strks = data[3];
data += 5;
}
printf(" Len time %2.2f, Strks %d\n", time, strks);
}
else if (type == 0x200) //set end
{
int lens = data[2] + ((data[3]&0x7f)<<8);
int h = data[4];
int m = data[5]&0x7f; //add hours
int s = data[6];
int cal = data[10] + ((data[11] & 0x7f)<<8); //add high byte.
data += 16;
sets--;
printf("Set lengths %d, Time: %02d:%02d Cal: %d \n ", lens, m,s,cal);
}
} while(sets>0);
return data; // point to next field
}
char * decode_wrk(char* data)
{
if (data[2] == 1)
{
printf("Chrono\n");
return data+256;
}
else if (data[2] >= 2)
{
printf("Swim: %d\n", data[2]); //Lets see what numbers we get
int Y = data[6]; //see if there's a year high byte.
int h = data[7] & 0x7f;
int M = data[8];
int m = data[9] & 0x7f;
int d = data[10];
int s = data[11] & 0x7f;
int pool = data[14]; //check/grab yards flag
int d_m = data[15]& 0x7f;
int sets = data[16]; //Check >255 sets
int d_s = data[17]& 0x7f;
int d_h = data[13]& 0x7f; //Check hour location
printf("Workout: %02d/%02d/%02d %02d:%02d:%02d\n", d, M, Y+2000, h, m, s);
printf(" Pool: %dm, Sets %d, Dur: %02d:%02d:%02d\n", pool, sets, d_h, d_m, d_s);
data = (char*)decode_sets((unsigned char*)data+20, sets);
}
return data;
}
void download(QSerialPort *serialPort)
{
if (data.length() == 0)
{
printf("Watch attached?\n");
return;
}
uint32_t req;
req = *(uint32_t*)data.data();
printf("Bitflags: %04x\n", req); //bitflags
char buf[20]; //6 header, 10 msg, 4 chksum
char msg[]={0x00,0x00,0xff,0xff,0xff,0xff,
0x01,0x04,
0x00,0x00, //dataset number
0x00,0x00, 0x00,0x00, 0x00,0x00 };
memcpy(buf, msg, 16);
QByteArray buffer;
int i;
for (i=0; i<=0x1f; ++i) // determine upper value
{
if (req & 1)
{
buf[9]=i;
uint32_t crc = crc32a(&buf[6], 10);
memcpy(&buf[16], &crc, sizeof(uint32_t));
if (req & 1)
{
printf("Tag ");
}
printf("Request set %02x ", i);
write(serialPort, buf, 20);
read(serialPort, 20);
printf("Read %d\n", data.length());
//check crc.
buffer.append(data.data(), data.length()-4);
}
req = req >> 1;
// Bitfield doesnt seems to match populated datasets so
// Try to spot packet run ons.
if (data[data.length()-5] != (char)255) // <Improve this logic
{
if ((req & 1) == 0)
{
printf("More data, overriding bitmask.\n");
req |= 1;
}
}
}
printf("\nData buffer:\n");
display(buffer);
uint16_t type;
char *ptr = buffer.data();
do
{
type = *(uint16_t*)ptr;
if (type == 0) // workout header
{
decode_wrk(ptr);
}
ptr += 0x100; //Walk over packets to next workout start
} while (ptr < buffer.data()+ buffer.length()); //Again, track number of packets instead.
printf("done\n");
}
int main()
{
QSerialPort *serialPort = new QSerialPort();
QString serialPortName = find();
serialPort->setPortName(serialPortName);
if (!serialPort->open(QIODevice::ReadWrite)) {
return 1;
}
init(serialPort);
sleep(1);
sendandstart(serialPort, d1, sizeof(d1));
// Ignore these initial requests for now.
// sendandwait(serialPort, d2, sizeof(d2));
// sendandwait(serialPort, d3, sizeof(d3));
// sendandwait(serialPort, d4, sizeof(d4));
sendandwait(serialPort, d5, sizeof(d5)); //get data to decode.
printf("\nData header:\n");
display(data);
//pulldown all data and dump
download(serialPort);
serialPort->close();
delete serialPort;
}