-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpc.c
603 lines (458 loc) · 11.9 KB
/
pc.c
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
* Copyright (c) 2008 Rainer Clasen
*
* This program is free software; you can redistribute it and/or modify
* it under the terms described in the file LICENSE included in this
* distribution.
*
*/
#include "pc.h"
#include <stdarg.h>
void srmio_pc_logv( srmio_pc_t pch, const char *fmt, va_list ap )
{
char buf[SRMIO_ERROR_MSG_SIZE];
if( ! pch->lfunc )
return;
if( 0 > vsnprintf( buf, SRMIO_ERROR_MSG_SIZE, fmt, ap ) )
return;
(*pch->lfunc)( buf, pch->ldata );
}
void srmio_pc_log( srmio_pc_t pch, const char *fmt, ... )
{
va_list ap;
va_start( ap, fmt );
srmio_pc_logv( pch, fmt, ap );
va_end( ap );
}
srmio_pc_t srmio_pc_new( const srmio_pc_methods_t *methods, void *child,
srmio_error_t *err )
{
srmio_pc_t pch;
assert( methods );
assert( child );
if( NULL == (pch = malloc( sizeof(struct _srmio_pc_t)))){
srmio_error_errno( err, "pc new" );
return NULL;
}
memset( pch, 0, sizeof(struct _srmio_pc_t) );
pch->methods = methods;
pch->child = child;
pch->xfer_state = srmio_pc_xfer_state_new;
pch->xfer_type = srmio_pc_xfer_type_new;
return pch;
}
void srmio_pc_free( srmio_pc_t pch )
{
assert(pch->methods->free);
if( pch->is_open )
srmio_pc_close( pch, NULL );
(*pch->methods->free)( pch );
free(pch);
}
bool srmio_pc_open( srmio_pc_t pch, srmio_error_t *err )
{
assert( pch );
assert( pch->io );
assert(pch->methods->open);
SRMIO_PC_DEBUG(pch, "");
if( pch->is_open ){
SRMIO_PC_ERROR( pch, err, "device is already open" );
return false;
}
if( ! srmio_io_is_open( pch->io ) ){
SRMIO_PC_ERROR( pch, err, "io device is already open" );
return false;
}
srmio_io_set_baudrate( pch->io, pch->baudrate, err );
srmio_io_set_parity( pch->io, pch->parity, err );
if( ! (*pch->methods->open)( pch, err ) )
return false;
pch->is_open = true;
return true;
}
bool srmio_pc_close( srmio_pc_t pch, srmio_error_t *err )
{
assert( pch );
assert( pch->io );
assert(pch->methods->close);
SRMIO_PC_DEBUG(pch, "");
if( pch->xfer_state != srmio_pc_xfer_state_new )
srmio_pc_xfer_finish( pch, err );
if( ! (*pch->methods->close)( pch, err ) )
return false;
pch->is_open = false;
return true;
}
bool srmio_pc_set_debugfunc( srmio_pc_t pch,
srmio_logfunc_t func, void *data,
srmio_error_t *err )
{
assert( pch );
(void)err;
pch->dfunc = func;
pch->ddata = data;
return true;
}
bool srmio_pc_set_logfunc( srmio_pc_t pch,
srmio_logfunc_t func, void *data,
srmio_error_t *err )
{
assert( pch );
(void)err;
pch->lfunc = func;
pch->ldata = data;
return true;
}
/*
* sets the device filename, that's len for communicating.
* does *not* take ownership of io handle!!! You need to free it yourself!
*/
bool srmio_pc_set_device( srmio_pc_t pch, srmio_io_t h, srmio_error_t *err )
{
assert( pch );
assert( h );
if( pch->is_open ){
SRMIO_PC_ERROR( pch, err, "device is open, can't change");
return false;
}
pch->io = h;
return true;
}
/*
* retrieve the IO handle
*/
bool srmio_pc_get_device( srmio_pc_t pch, srmio_io_t *h, srmio_error_t *err )
{
(void)err;
assert( pch );
assert( h );
*h = pch->io;
return true;
}
/*
* sets the baudrate to use for communication with the PowerControl.
* with srmio_io_baud_max, all supported baud rates are probed.
*/
bool srmio_pc_set_baudrate( srmio_pc_t pch, srmio_io_baudrate_t rate,
srmio_error_t *err )
{
assert( pch );
assert( rate <= srmio_io_baud_max );
if( pch->is_open ){
SRMIO_PC_ERROR( pch, err, "device is open, can't change");
return false;
}
pch->baudrate = rate;
return true;
}
/*
* sets the parity to use for communication with the PowerControl.
* with srmio_io_parity_max, all supported parity settings are probed.
*/
bool srmio_pc_set_parity( srmio_pc_t pch, srmio_io_parity_t parity,
srmio_error_t *err )
{
assert( pch );
assert( parity <= srmio_io_parity_max );
if( pch->is_open ){
SRMIO_PC_ERROR( pch, err, "device is open, can't change");
return false;
}
pch->parity = parity;
return true;
}
bool srmio_pc_get_baudrate( srmio_pc_t pch, srmio_io_baudrate_t *rate,
srmio_error_t *err )
{
assert(pch);
assert(rate);
if( pch->baudrate >= srmio_io_baud_max ){
SRMIO_PC_ERROR( pch, err, "baudrate is unset" );
return false;
}
*rate = pch->baudrate;
return true;
}
bool srmio_pc_get_parity( srmio_pc_t pch, srmio_io_parity_t *parity,
srmio_error_t *err )
{
assert(pch);
assert(parity);
if( pch->parity >= srmio_io_parity_max ){
SRMIO_PC_ERROR( pch, err, "parity is unset" );
return false;
}
*parity = pch->parity;
return true;
}
/*
* gets the firmware as discovered when opening the device
*/
bool srmio_pc_get_version( srmio_pc_t pch, unsigned *version,
srmio_error_t *err )
{
(void)err;
assert( pch );
assert( version );
*version = pch->firmware;
return true;
}
bool srmio_pc_set_xfer( srmio_pc_t pch, srmio_pc_xfer_type_t type,
srmio_error_t *err )
{
(void)err;
assert( pch );
assert( type < srmio_pc_xfer_type_max );
SRMIO_PC_DEBUG(pch, "%d", type );
pch->xfer_type = type;
return true;
}
bool srmio_pc_can_preview( srmio_pc_t conn )
{
assert(conn);
return conn->can_preview;
}
bool srmio_pc_xfer_get_blocks( srmio_pc_t conn, size_t *blocks,
srmio_error_t *err )
{
(void)err;
assert( conn );
assert( blocks );
*blocks = conn->block_cnt;
return true;
}
bool srmio_pc_cmd_get_athlete( srmio_pc_t pch, char **athlete,
srmio_error_t *err )
{
assert(pch);
assert(pch->methods->cmd_get_athlete);
return (*pch->methods->cmd_get_athlete)( pch, athlete, err );
}
bool srmio_pc_cmd_set_time( srmio_pc_t pch, struct tm *t,
srmio_error_t *err )
{
assert(pch);
assert(pch->methods->cmd_set_time);
return (*pch->methods->cmd_set_time)( pch, t, err );
}
bool srmio_pc_cmd_set_recint( srmio_pc_t pch, srmio_time_t t,
srmio_error_t *err )
{
assert(pch);
assert(pch->methods->cmd_set_recint);
return (*pch->methods->cmd_set_recint)( pch, t, err );
}
bool srmio_pc_cmd_get_time( srmio_pc_t pch, struct tm *timep, srmio_error_t *err )
{
assert(pch);
assert(pch->methods->cmd_get_time);
return (*pch->methods->cmd_get_time)( pch, timep, err );
}
bool srmio_pc_cmd_get_circum( srmio_pc_t pch, unsigned *circum, srmio_error_t *err )
{
assert(pch);
assert(pch->methods->cmd_get_circum);
return (*pch->methods->cmd_get_circum)( pch, circum, err );
}
bool srmio_pc_cmd_get_slope( srmio_pc_t pch, double *slope, srmio_error_t *err )
{
assert(pch);
assert(pch->methods->cmd_get_slope);
return (*pch->methods->cmd_get_slope)( pch, slope, err );
}
bool srmio_pc_cmd_get_zeropos( srmio_pc_t pch, unsigned *zeropos, srmio_error_t *err )
{
assert(pch);
assert(pch->methods->cmd_get_zeropos);
return (*pch->methods->cmd_get_zeropos)( pch, zeropos, err );
}
bool srmio_pc_cmd_get_recint( srmio_pc_t pch, srmio_time_t *recint, srmio_error_t *err )
{
assert(pch);
assert(pch->methods->cmd_get_recint);
return (*pch->methods->cmd_get_recint)( pch, recint, err );
}
bool srmio_pc_cmd_clear( srmio_pc_t pch, srmio_error_t *err )
{
assert(pch);
assert(pch->methods->cmd_clear);
return (*pch->methods->cmd_clear)( pch, err );
}
bool srmio_pc_xfer_start( srmio_pc_t pch, srmio_error_t *err )
{
assert(pch);
assert(pch->methods->xfer_start);
return (*pch->methods->xfer_start)( pch, err );
}
bool srmio_pc_xfer_block_next( srmio_pc_t pch, srmio_pc_xfer_block_t block )
{
assert(pch);
assert( block );
assert(pch->methods->xfer_block_next);
return (*pch->methods->xfer_block_next)( pch, block );
}
bool srmio_pc_xfer_chunk_next( srmio_pc_t pch, srmio_chunk_t chunk,
bool *is_intervall, bool *start_intervall )
{
assert(pch);
assert( chunk );
assert(pch->methods->xfer_chunk_next);
return (*pch->methods->xfer_chunk_next)( pch, chunk,
is_intervall, start_intervall );
}
bool srmio_pc_xfer_finish( srmio_pc_t pch, srmio_error_t *err )
{
assert(pch);
assert(pch->methods->xfer_finish);
return (*pch->methods->xfer_finish)( pch, err );
}
srmio_pc_xfer_state_t srmio_pc_xfer_status( srmio_pc_t pch,
srmio_error_t *err)
{
assert( pch );
if( pch->xfer_state == srmio_pc_xfer_state_failed )
srmio_error_copy( err, &pch->err );
return pch->xfer_state;
}
bool srmio_pc_xfer_block_progress( srmio_pc_t pch, size_t *block_done )
{
assert( pch );
assert(pch->methods->xfer_block_progress);
return (*pch->methods->xfer_block_progress)( pch, block_done );
}
/************************************************************
*
* use srmio_pc_xfer_all() to fill srmio_data_t structure
* with all chunks.
*
* Also serves as example on how to use the download API.
*
************************************************************/
/*
* retrieve recorded data from PC and build "friendly" srmio_data_t structure.
*
* parameter:
* pch: conn handle
*
*/
bool srmio_pc_xfer_all( srmio_pc_t pch,
srmio_data_t data,
srmio_progress_t pfunc, void *prog_data,
srmio_error_t *err )
{
int mfirst = -1;
struct _srmio_pc_xfer_block_t block;
struct _srmio_chunk_t chunk;
size_t done_chunks = 0;
size_t block_cnt, block_num = 0;
size_t prog_prev = 0, prog_sum=0;
assert( pch );
assert( data );
block.athlete = NULL;
SRMIO_PC_DEBUG(pch, "");
if( ! srmio_pc_xfer_start( pch, err ) )
return false;
if( ! srmio_pc_xfer_get_blocks( pch, &block_cnt, err ) )
goto clean;
srmio_pc_log( pch, "found %d ride blocks", block_cnt );
if( block_cnt > 1 ){
if( srmio_pc_can_preview( pch ) ){
while( srmio_pc_xfer_block_next( pch, &block ) ){
prog_sum += block.total;
if( block.athlete )
free( block.athlete );
block.athlete = NULL;
}
SRMIO_PC_DEBUG(pch, "prog_sum %u", prog_sum );
/* finalize / restart xfer */
if( srmio_pc_xfer_state_success !=
srmio_pc_xfer_status( pch, err ) )
goto clean;
srmio_pc_xfer_finish( pch, NULL );
if( ! srmio_pc_xfer_start( pch, err ) )
goto clean;
}
}
while( srmio_pc_xfer_block_next( pch, &block ) ){
bool is_int;
bool is_first;
size_t prog_total;
srmio_pc_log( pch, "downloading ride block %d/%d",
block_num, block_cnt );
data->slope = block.slope;
data->zeropos = block.zeropos;
data->circum = block.circum;
if( block.athlete ){
if( data->athlete )
free(data->athlete);
data->athlete = strdup(block.athlete);
}
if( prog_sum ){
prog_total = prog_sum;
} else if( block_cnt == 1 ){
prog_total = block.total +1;
} else {
prog_total = block_cnt * 1000;
}
while( srmio_pc_xfer_chunk_next( pch, &chunk, &is_int, &is_first ) ){
if( pfunc && 0 == done_chunks % 16 ){
size_t block_done = 0;
srmio_pc_xfer_block_progress( pch, &block_done );
if( prog_sum ){
block_done += prog_prev;
} else if( block_cnt == 1 ){
/* unchanged */
} else {
block_done = (double)block_num * 1000 + 1000 *
block.total / block_done;
}
SRMIO_PC_DEBUG( pch,
"prog_total %d, prog_prev %d, block_done %d",
prog_total, prog_prev, block_done );
(*pfunc)( prog_total, block_done, prog_data );
}
if( ! srmio_data_add_chunk( data, &chunk, err ) )
goto clean;
++done_chunks;
/* finish previous marker */
if( mfirst >= 0 && ( ! is_int || is_first ) )
if( ! srmio_data_add_marker( data, mfirst,
data->cused -2, err ) )
goto clean;
/* start marker */
if( is_first ){
mfirst = (int)data->cused -1;
SRMIO_PC_DEBUG(pch, "new marker at %d", mfirst );
} else if( ! is_int ){
mfirst = -1;
}
}
/* finalize marker at block end */
if( mfirst >= 0 ){
SRMIO_PC_DEBUG(pch, "finalizing marker at block end" );
if( ! srmio_data_add_marker( data, mfirst,
data->cused -1, err ) )
goto clean;
mfirst = -1;
}
if( prog_sum )
prog_prev += block.total;
else
prog_prev += 1000;
free( block.athlete );
block.athlete = NULL;
++block_num;
}
if( ! done_chunks ){
SRMIO_PC_ERROR( pch, err, "no data");
goto clean;
}
if( srmio_pc_xfer_state_success != srmio_pc_xfer_status( pch, err) )
goto clean;
srmio_pc_xfer_finish( pch, NULL );
srmio_pc_log( pch, "got %d records", data->cused );
return true;
clean:
srmio_pc_xfer_finish( pch, NULL );
return false;
}