forked from angrytongan/c2api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc2api.cpp
More file actions
452 lines (370 loc) · 11.5 KB
/
c2api.cpp
File metadata and controls
452 lines (370 loc) · 11.5 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* Concept2 rower API.
*/
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "c2api.h"
#include "c2api_priv.h"
#include "csafe.h"
#include "csafe_util.h"
#include "debug.h"
#include "utils.h"
#define HID_READ_TIMEOUT 500
/*
* Setup a context for doing all C2 communications.
*
* Arguments:
* - pointer to c2ctx_t structure.
*
* Returns:
* - nothing.
*/
void c2init(c2ctx_t *ctx) {
memset(ctx, 0, sizeof(c2ctx_t));
ctx->last_error = C2ERR_NONE;
}
/*
* Cleanup.
*
* Arguments:
* - pointer to c2ctx_t structure.
*
* Returns:
* - 0 on success.
* - -1 on failure; call c2error() to get the last error.
*/
void c2close(c2ctx_t *ctx) {
int i;
if (ctx) {
for (i = 0; i < ctx->num_rowers; i++) {
if (ctx->rowers[i].device) {
hid_close(ctx->rowers[i].device);
free(ctx->rowers[i].serial_number);
}
}
free(ctx->rowers);
}
}
/*
* Return last error.
*
* Arguments:
* - none.
*
* Returns:
* - value of the last error that occurred.
*
* Side effect:
* - clears the last error.
*/
int c2error(c2ctx_t *ctx) {
int e = ctx->last_error;
ctx->last_error = C2ERR_NONE;
return e;
}
/*
* Discover all rowers.
*
* Arguments:
* - C2 context pointer (from c2init).
*
* Returns:
* - number of rowers discovered on success.
* - -1 on failure; call c2error() to get last error.
*/
int c2discover(c2ctx_t *ctx) {
struct hid_device_info *devices, *d;
int num_rowers = 0;
int i = 0;
if (!ctx) {
return -1;
}
if (hid_init() == -1) {
ctx->last_error = C2ERR_FAILED_HID_INIT;
return -1;
}
devices = hid_enumerate(HID_C2_VENDOR_ID, 0);
for (d = devices; d; d = d->next) {
num_rowers++;
}
if (num_rowers) {
ctx->rowers = (c2rower_t*)calloc(num_rowers, sizeof(c2rower_t));
if (!ctx->rowers) {
ctx->last_error = C2ERR_FAILED_ALLOC_ROWERS;
return -1;
}
for (d = devices, i = 0; d; d = d->next) {
ctx->rowers[i].device = hid_open(
d->vendor_id,
d->product_id,
d->serial_number);
if (ctx->rowers[i].device) {
ctx->rowers[i].id = i;
ctx->rowers[i].vendor_id = d->vendor_id;
ctx->rowers[i].product_id = d->product_id;
ctx->rowers[i].serial_number = wcsdup(d->serial_number);
i++;
} else {
error("failed to open device %ls\n", d->serial_number);
}
}
}
hid_free_enumeration(devices);
ctx->num_rowers = i;
return i;
}
static int tx_rx(c2ctx_t *ctx, unsigned int id,
csafe_buffer_t *in, csafe_buffer_t *out, csafe_data_t *o) {
int bytes;
unsigned char tx_rx_buff[CSAFE_MAX_FRAME_SIZE+1] = { 0 };
unsigned char *t;
if (!ctx) {
return -1;
}
tx_rx_buff[0] = 0x2; /* insert HID report id */
memcpy(tx_rx_buff+1, in->buff, in->index);
warning("%s: request\n", __FUNCTION__);
hexdump(in->buff, in->index);
bytes = hid_write(ctx->rowers[id].device, tx_rx_buff, sizeof(tx_rx_buff));
if (bytes == -1) {
error("%s: hid_write() returned -1 (%ls)\n",
__FUNCTION__, hid_error(ctx->rowers[id].device));
return -1;
}
bytes = hid_read_timeout(ctx->rowers[id].device, tx_rx_buff, sizeof(tx_rx_buff),
HID_READ_TIMEOUT);
if (bytes == -1) {
warning("%s: hid_read_timeout() returned -1 (%ls)\n",
__FUNCTION__, hid_error(ctx->rowers[id].device));
return -1;
}
memcpy(out->buff, tx_rx_buff+1, sizeof(tx_rx_buff)-1); /* strip HID report id */
for (t = out->buff; *t != 0xf2 && t - out->buff < CSAFE_MAX_FRAME_SIZE; t++);
if (*t != 0xf2) {
error("%s: failed to find end marker 0xf2\n", __FUNCTION__);
return -1;
}
out->index = t - out->buff + 1;
warning("%s: response\n", __FUNCTION__);
hexdump(out->buff, out->index);
return csafe_unpack(out, o);
}
static int send_to_rower(c2ctx_t *ctx, int id, csafe_buffer_t *out,
csafe_buffer_t *in, csafe_data_t *d) {
int i = 0;
int num_errors = 0;
/*
* XXX data returned is for the last rower
*/
if (id == -1) {
for (i = 0; i < ctx->num_rowers; i++) {
num_errors += tx_rx(ctx, i, out, in, d) == -1;
}
} else {
num_errors += tx_rx(ctx, id, out, in, d) == -1;
}
return num_errors;
}
/*
*/
int c2syncClock(c2ctx_t *ctx, int id) {
csafe_buffer_t b;
csafe_data_t d = { 0 };
struct timeval tv = { 0, 0 };
struct tm r;
csafe_init_data(&d);
if (gettimeofday(&tv, NULL) == -1) {
ctx->last_error = C2ERR_FAILED_GETTIMEOFDAY;
return -1;
}
if (!localtime_r(&tv.tv_sec, &r)) {
ctx->last_error = C2ERR_FAILED_LOCALTIME;
return -1;
}
d.setdate_year = r.tm_year;
d.setdate_month = r.tm_mon + 1;
d.setdate_day = r.tm_mday;
d.settime_hour = r.tm_hour;
d.settime_minute = r.tm_min;
d.settime_second = r.tm_sec;
csafe_init_standard_cmd(&b);
csafe_push_cmd(CSAFE_SETDATE_CMD, &b, &d);
csafe_push_cmd(CSAFE_SETTIME_CMD, &b, &d);
csafe_end_cmd(&b);
return send_to_rower(ctx, id, &b, &b, &d);
}
int c2reset(c2ctx_t *c, int id) {
csafe_buffer_t b;
csafe_data_t d;
csafe_init_data(&d);
csafe_init_standard_cmd(&b);
csafe_push_cmd(CSAFE_RESET_CMD, &b, &d);
csafe_end_cmd(&b);
return send_to_rower(c, id, &b, &b, &d);
}
int c2setWorkoutTime(c2ctx_t *c, int id, int h, int m, int s) {
csafe_buffer_t b;
csafe_data_t d;
csafe_init_data(&d);
d.settwork_hours = h;
d.settwork_minutes = m;
d.settwork_seconds = s;
csafe_init_standard_cmd(&b);
csafe_push_cmd(CSAFE_SETTWORK_CMD, &b, &d);
csafe_end_cmd(&b);
return send_to_rower(c, id, &b, &b, &d);
}
int c2setWorkoutDistance(c2ctx_t *c, int id, int distance) {
csafe_buffer_t b;
csafe_data_t d;
csafe_init_data(&d);
d.sethorizontal = distance;
d.sethorizontal_units = 0; /* XXX spec and header ambiguous? */
csafe_init_standard_cmd(&b);
csafe_push_cmd(CSAFE_SETHORIZONTAL_CMD, &b, &d);
csafe_end_cmd(&b);
return send_to_rower(c, id, &b, &b, &d);
}
int c2setWorkoutCalories(c2ctx_t *c, int id, int calories) {
csafe_buffer_t b;
csafe_data_t d;
csafe_init_data(&d);
d.setcalories = calories;
csafe_init_standard_cmd(&b);
csafe_push_cmd(CSAFE_SETCALORIES_CMD, &b, &d);
csafe_end_cmd(&b);
return send_to_rower(c, id, &b, &b, &d);
}
int c2setProgram(c2ctx_t *c, int id, int program) {
csafe_buffer_t b;
csafe_data_t d;
csafe_init_data(&d);
d.program = program;
csafe_init_standard_cmd(&b);
csafe_push_cmd(CSAFE_SETPROGRAM_CMD, &b, &d);
csafe_end_cmd(&b);
return send_to_rower(c, id, &b, &b, &d);
}
/*
* http://www.concept2.com.au/service/monitors/pm5/how-to-use/using-monitor-display-options
*
* time / distance elapsed / remaining
* pace per 500m / calories / watts
* average pace per 500m / calories / watts
* split time, metres, pace, calories or watts
* heart rate
* projected finish:
* time workokuts: number of metres at end of workout
* distance workout: time to complete workout (just row: time is 30 minutes)
* interval workout: projected finish or time for interval
*/
int c2get(c2ctx_t *c, int id, char *cmd, csafe_data_t *d) {
csafe_buffer_t b;
csafe_init_data(d);
csafe_init_standard_cmd(&b);
if (!strcmp(cmd, "status")) {
csafe_push_cmd(CSAFE_GETSTATUS_CMD, &b, d);
csafe_push_cmd(CSAFE_GETERRORCODE_CMD, &b, d);
csafe_push_cmd(CSAFE_PM_GET_ERRORVALUE, &b, d);
} else if (!strcmp(cmd, "monitor")) {
csafe_push_cmd(CSAFE_GETTWORK_CMD, &b, d);
csafe_push_cmd(CSAFE_GETHORIZONTAL_CMD, &b, d);
csafe_push_cmd(CSAFE_GETPACE_CMD, &b, d);
csafe_push_cmd(CSAFE_GETPOWER_CMD, &b, d);
csafe_push_cmd(CSAFE_GETCALORIES_CMD, &b, d);
csafe_push_cmd(CSAFE_GETCADENCE_CMD, &b, d);
csafe_push_cmd(CSAFE_GETHRCUR_CMD, &b, d);
} else if (!strcmp(cmd, "forceplot")) {
csafe_push_cmd(CSAFE_PM_GET_FORCEPLOTDATA, &b, d);
csafe_push_cmd(CSAFE_PM_GET_HEARTBEATDATA, &b, d);
} else if (!strcmp(cmd, "workout")) {
csafe_push_cmd(CSAFE_PM_GET_WORKTIME, &b, d);
csafe_push_cmd(CSAFE_PM_GET_WORKDISTANCE, &b, d);
csafe_push_cmd(CSAFE_PM_GET_STROKESTATE, &b, d);
csafe_push_cmd(CSAFE_PM_GET_WORKOUTTYPE, &b, d);
csafe_push_cmd(CSAFE_PM_GET_DRAGFACTOR, &b, d);
csafe_push_cmd(CSAFE_PM_GET_RESTTIME, &b, d);
csafe_push_cmd(CSAFE_PM_GET_INTERVALTYPE, &b, d);
csafe_push_cmd(CSAFE_PM_GET_WORKOUTINTERVALCOUNT, &b, d);
} else if (!strcmp(cmd, "erg")) {
csafe_push_cmd(CSAFE_GETODOMETER_CMD, &b, d);
csafe_push_cmd(CSAFE_GETVERSION_CMD, &b, d);
csafe_push_cmd(CSAFE_GETSERIAL_CMD, &b, d);
} else {
c->last_error = C2ERR_UNIMPLEMENTED;
return -1;
}
csafe_end_cmd(&b);
return send_to_rower(c, id, &b, &b, d);
}
int c2setState(c2ctx_t *c, int id, char *cmd) {
csafe_cmd command;
csafe_buffer_t b;
csafe_data_t d;
bool ok = false;
/*
* Get status first.
*/
csafe_init_data(&d);
csafe_init_standard_cmd(&b);
csafe_push_cmd(CSAFE_GETSTATUS_CMD, &b, &d);
csafe_end_cmd(&b);
send_to_rower(c, id, &b, &b, &d);
/*
* Figure out requested command.
*/
command =
!strcmp(cmd, "finished") ? CSAFE_GOFINISHED_CMD :
!strcmp(cmd, "idle") ? CSAFE_GOIDLE_CMD :
!strcmp(cmd, "inuse") ? CSAFE_GOINUSE_CMD :
!strcmp(cmd, "haveid") ? CSAFE_GOHAVEID_CMD :
!strcmp(cmd, "ready") ? CSAFE_GOREADY_CMD :
CSAFE_GETSTATUS_CMD;
/*
* Do we need to transition?
*/
if ((d.status == CSAFE_MACHINE_STATE_READY && command == CSAFE_GOREADY_CMD) ||
(d.status == CSAFE_MACHINE_STATE_IDLE && command == CSAFE_GOIDLE_CMD) ||
(d.status == CSAFE_MACHINE_STATE_HAVEID && command == CSAFE_GOHAVEID_CMD) ||
(d.status == CSAFE_MACHINE_STATE_INUSE && command == CSAFE_GOINUSE_CMD) ||
(d.status == CSAFE_MACHINE_STATE_FINISHED && command == CSAFE_GOFINISHED_CMD)) {
return 0;
}
/*
* Allowed to transition?
*/
switch (d.status) {
case CSAFE_MACHINE_STATE_READY:
ok = command == CSAFE_GOIDLE_CMD || command == CSAFE_GOINUSE_CMD;
break;
case CSAFE_MACHINE_STATE_IDLE:
ok = command == CSAFE_GOHAVEID_CMD || command == CSAFE_GOINUSE_CMD;
break;
case CSAFE_MACHINE_STATE_HAVEID:
ok = command == CSAFE_GOIDLE_CMD || command == CSAFE_GOINUSE_CMD;
break;
case CSAFE_MACHINE_STATE_PAUSED:
ok = command == CSAFE_GOFINISHED_CMD;
break;
case CSAFE_MACHINE_STATE_FINISHED:
ok = command == CSAFE_GOREADY_CMD || command == CSAFE_GOIDLE_CMD;
break;
case CSAFE_MACHINE_STATE_OFFLINE:
ok = command == CSAFE_GOREADY_CMD;
break;
default:
break;
}
/*
* Do the transition.
*/
if (command != CSAFE_GETSTATUS_CMD && ok) {
csafe_init_data(&d);
csafe_init_standard_cmd(&b);
csafe_push_cmd(command, &b, &d);
csafe_end_cmd(&b);
return send_to_rower(c, id, &b, &b, &d);
}
return -1;
}