/
file_srm.c
802 lines (652 loc) · 16.3 KB
/
file_srm.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*
* 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 "common.h"
/* used for list of blocks within SRM files */
struct _srm_block_t {
srmio_time_t daydelta;
unsigned chunks;
};
static bool _xread( FILE *fh, unsigned char *buf, size_t len, srmio_error_t *err )
{
size_t ret;
assert( fh );
assert( buf );
assert( len );
ret = fread( buf, 1, len, fh );
if( ret == 0){
srmio_error_errno( err, "read" );
return false;
}
if( ret < len ){
srmio_error_set( err, "incomplete read %u/%u", ret, len);
return false;
}
return true;
}
static bool _xwrite( FILE *fh, unsigned char *buf, size_t len, srmio_error_t *err )
{
size_t ret;
assert( fh );
assert( buf );
assert( len );
ret = fwrite( buf, 1, len, fh );
if( ret == 0){
srmio_error_errno( err, "write" );
return false;
}
if( ret < len ){
srmio_error_set( err, "incomplete write %d/%d", ret, len );
return false;
}
return true;
}
/* days since 0000-01-01 */
#define DAYS_SRM 686656u
#define DAYS_EPOCH 719528u
#define SRM2EPOCH (DAYS_EPOCH - DAYS_SRM)
/* convert "days since 1880-01-01" to srmio_time_t */
static srmio_time_t _srm_mktime( unsigned days, srmio_error_t *err )
{
time_t ret;
struct tm t;
memset( &t, 0, sizeof(struct tm));
t.tm_mday = 1;
t.tm_year = 70;
t.tm_isdst = -1;
if( days < SRM2EPOCH ){
/* TODO: SRM2EPOCH is a hack that might cause problems
* with misadjusted SRM clocks or other time glitches */
srmio_error_set( err, "date is before supported range" );
return (srmio_time_t)-1;
}
t.tm_mday += days - SRM2EPOCH;
ret = mktime( &t );
if( ret == (time_t) -1 ){
srmio_error_errno( err, "mktime" );
return (srmio_time_t)-1;
}
#ifdef DEBUG_FILE
DPRINTF( "%u days -> %lu", days, (unsigned long)ret );
#endif
return (srmio_time_t)ret * 10;
}
const unsigned cumul_days[12] = {
0, /* begin of jan */
31,
59,
90,
120,
151,
181,
212,
243,
273,
304,
334 /* begin of dec */
};
/* convert time_t into "days since 1880-01-01" */
static unsigned _srm_mkdays( srmio_time_t input, srmio_error_t *err )
{
time_t tstamp;
struct tm tm;
unsigned year;
unsigned days;
tstamp = input / 10;
#ifdef HAVE_LOCALTIME_R
if( NULL == localtime_r( &tstamp, &tm ) ){
srmio_error_errno( err, "localtime" );
return -1;
}
#else
{ struct tm *tmp;
if( NULL == ( tmp = localtime( &tstamp ))){
srmio_error_errno( err, "localtime" );
return -1;
}
memcpy( &tm, tmp, sizeof(struct tm));
}
#endif
year = tm.tm_year + 1900;
days = 365 * year
+ (year - 1)/4
- (year - 1)/100
+ (year - 1)/400
+ cumul_days[tm.tm_mon]
+ tm.tm_mday;
if( tm.tm_mon >=2 ){
if( year % 4 == 0 )
++days;
if( year % 100 == 0 )
--days;
if( year % 400 == 0 )
++days;
}
if( days < DAYS_SRM ){
srmio_error_set( err, "resulting date %u-%02u-%02u is outside supported range",
year, tm.tm_mon+1, tm.tm_mday );
return -1;
}
days -= DAYS_SRM;
#ifdef DEBUG_FILE
DPRINTF( "%.1f -> %u",
(double)input/10,
days );
#endif
return days;
}
typedef srmio_chunk_t (*srmio_file_read_cfunc)( const unsigned char *buf,
srmio_error_t *err );
static srmio_chunk_t _srmio_data_chunk_srm6( const unsigned char *buf,
srmio_error_t *err )
{
srmio_chunk_t ck;
uint8_t c0, c1, c2;
if( NULL == (ck = srmio_chunk_new(err) ))
return NULL;
c0 = buf_get_uint8( buf, 0 );
c1 = buf_get_uint8( buf, 1 );
c2 = buf_get_uint8( buf, 2 );
ck->pwr = ( c1 & 0x0f) | ( c2 << 4 );
ck->speed = (double)( ((c1 & 0xf0) << 3)
| (c0 & 0x7f) ) * 3 / 26;
ck->cad = buf_get_uint8( buf, 5 );
ck->hr = buf_get_uint8( buf, 4 );
return ck;
}
static srmio_chunk_t _srmio_data_chunk_srm7( const unsigned char *buf,
srmio_error_t *err )
{
srmio_chunk_t ck;
if( NULL == (ck = srmio_chunk_new(err) ))
return NULL;
ck->pwr = buf_get_luint16( buf, 0 );
ck->cad = buf_get_uint8( buf, 2 );
ck->hr = buf_get_uint8( buf, 3 );
ck->speed = ( (double)buf_get_lint32( buf, 4 ) * 3.6) / 1000;
if( ck->speed < 0 )
ck->speed = 0;
ck->ele = buf_get_lint32( buf, 8 );
ck->temp = 0.1 * buf_get_lint16( buf, 12 );
return ck;
}
/*
* read SRM5/6/7 files, fill newly allocated data structure.
*
* on success data pointer is returned.
* returns NULL and sets errno on failure.
*/
srmio_data_t srmio_file_srm_read( FILE *fh, srmio_error_t *err )
{
srmio_data_t tmp;
unsigned char buf[1024];
srmio_time_t recint;
srmio_time_t timerefday;
srmio_file_read_cfunc cfunc = NULL;
unsigned chunklen;
unsigned mcmtlen;
unsigned bcnt;
struct _srm_block_t **blocks = NULL;
unsigned mcnt;
unsigned ckcnt;
unsigned i;
if( NULL == (tmp = srmio_data_new(err)))
return NULL;
/* header */
if( ! _xread( fh, buf, 86, err ) )
goto clean2;
#ifdef DEBUG_FILE
DUMPHEX( "head", buf, 86 );
#endif
if( 0 != strncmp( (char*)buf, "SRM", 3 )){
srmio_error_set( err, "unrecognized file format");
goto clean2;
}
switch( buf[3] ){
case '5':
mcmtlen = 3;
chunklen = 5;
cfunc = _srmio_data_chunk_srm6;
break;
case '6':
mcmtlen = 255;
chunklen = 5;
cfunc = _srmio_data_chunk_srm6;
break;
case '7':
mcmtlen = 255;
chunklen = 14;
cfunc = _srmio_data_chunk_srm7;
break;
default:
srmio_error_set( err, "unsupported file format version: %c", buf[3] );
goto clean2;
}
if( (srmio_time_t)-1 == (timerefday = _srm_mktime( buf_get_luint16( buf, 4), err )))
goto clean2;
#ifdef DEBUG_FILE
{
time_t t = timerefday / 10;
DPRINTF( "timerefday %u %.1f %s",
(unsigned)buf_get_luint16( buf, 4),
(double)timerefday/10, ctime( &t));
}
#endif
tmp->circum = buf_get_luint16( buf, 6);
recint = 10 * buf_get_uint8( buf, 8 )
/ buf_get_uint8( buf, 9 );
bcnt = buf_get_luint16( buf, 10);
mcnt = buf_get_luint16( buf, 12);
#ifdef DEBUG_FILE
DPRINTF( "bcnt=%u mcnt=%u(+1)", bcnt, mcnt );
#endif
/* "notes" is preceeded by length + zero padded */
/* TODO: iconv notes cp850 -> internal */
if( NULL == (tmp->notes = buf_get_string( buf, 16, 70 ))){
srmio_error_errno( err, "get notes" );
goto clean2;
}
/* marker */
if( NULL == (tmp->marker = malloc( (mcnt +1) * sizeof(srmio_marker_t *)))){
srmio_error_errno( err, "alloc marker" );
goto clean2;
}
*tmp->marker = NULL;
tmp->mavail = mcnt;
/* first marker is just used for the athlete name */
if( ! _xread( fh, buf, mcmtlen + 15, err ))
goto clean2;
if( NULL == (tmp->athlete = buf_get_string( buf, 0, mcmtlen ))){
srmio_error_errno( err, "get athlete" );
goto clean2;
}
/* TODO: iconv athlete cp850 -> internal */
/* remaining marker */
for( ; tmp->mused < mcnt; ++tmp->mused ){
srmio_marker_t tm;
if( ! _xread( fh, buf, mcmtlen + 15, err ))
goto clean2;
if( NULL == (tm = srmio_marker_new(err)))
goto clean2;
tmp->marker[tmp->mused] = tm;
tmp->marker[tmp->mused+1] = NULL;
tm->first = buf_get_luint16( buf, mcmtlen +1)-1;
tm->last = buf_get_luint16( buf, mcmtlen +3)-1;
if( NULL == (tm->notes = buf_get_string( buf, 0, mcmtlen))){
srmio_error_errno( err, "get notes");
goto clean2;
}
/* TODO: iconv notes cp850 -> internal */
#ifdef DEBUG_FILE
DPRINTF( "marker %u %u %s",
tm->first,
tm->last,
tm->notes );
#endif
}
/* blocks */
if( NULL == (blocks = malloc( (bcnt+2) * sizeof( struct _srm_block_t *)))){
srmio_error_errno( err, "alloc blocks" );
goto clean2;
}
*blocks=NULL;
for( i = 0; i < bcnt; ++i ){
struct _srm_block_t *tb;
if( ! _xread( fh, buf, 6, err ))
goto clean3;
if( NULL == (tb = malloc(sizeof(struct _srm_block_t)))){
srmio_error_errno( err, "alloc block" );
goto clean3;
}
blocks[i] = tb;
blocks[i+1] = NULL;;
tb->daydelta = buf_get_luint32( buf, 0) / 10;
tb->chunks = buf_get_luint16( buf, 4);
#ifdef DEBUG_FILE
{
time_t t = (timerefday + tb->daydelta) / 10;
DPRINTF( "block %.1f %u %s",
(double)tb->daydelta/10,
tb->chunks,
ctime( &t) );
}
#endif
}
/* calibration */
if( ! _xread( fh, buf, 7, err ))
goto clean3;
#ifdef DEBUG_FILE
DUMPHEX( "calibration", buf, 7 );
#endif
tmp->zeropos = buf_get_luint16( buf, 0);
tmp->slope = (double)(buf_get_luint16( buf, 2) * 140) / 42781;
ckcnt = buf_get_luint16( buf, 4);
#ifdef DEBUG_FILE
DPRINTF( "cal zpos=%d slope=%.1f, chunks=%u",
tmp->zeropos, tmp->slope, ckcnt );
#endif
/* synthesize block for SRM5 files */
if( bcnt == 0 ){
blocks[1] = NULL;;
if( NULL == (blocks[0] = malloc(sizeof(struct _srm_block_t)))){
srmio_error_errno( err, "alloc block" );
goto clean3;
}
blocks[0]->daydelta = recint;
blocks[0]->chunks = ckcnt;
}
/* chunks */
for( i = 0; blocks[i]; ++i ){
unsigned ci;
for( ci = 0; ci < blocks[i]->chunks; ++ci ){
srmio_chunk_t ck;
if( ! _xread( fh, buf, chunklen, err )){
if( ! tmp->cused )
goto clean3;
STATMSG( "failed to read all chunks" );
goto premature_end;
}
if( NULL == (ck = (*cfunc)( buf, err )))
goto clean3;
ck->time = timerefday + blocks[i]->daydelta +
ci * recint;
ck->dur = recint;
if( ck->time < timerefday ){
srmio_error_set( err, "time overflow in block %u", i );
goto clean3;
}
#ifdef DEBUG_FILE
DPRINTF( "chunk "
"time=%.1f, "
"temp=%.1f, "
"pwr=%u, "
"spd=%.3f, "
"cad=%u, "
"hr=%u ",
(double)ck->time/10,
ck->temp,
ck->pwr,
ck->speed,
ck->cad,
ck->hr );
#endif
if( ! srmio_data_add_chunkp( tmp, ck, err ) )
goto clean3;
}
free( blocks[i] );
}
free( blocks );
return tmp;
premature_end:
/* premature end of file, fix marker */
ckcnt = tmp->cused -1;
for( i = 0; i < tmp->mused; ++i ){
srmio_marker_t mk = tmp->marker[i];
if( mk->first > ckcnt )
mk->first = ckcnt;
if( mk->last > ckcnt )
mk->last = ckcnt;
}
return tmp;
clean3:
for( i = 0; blocks[i]; ++i )
free( blocks[i] );
free( blocks );
clean2:
srmio_data_free(tmp);
return NULL;
}
/* TODO: srmio_file_srm6_write */
bool set_marker( unsigned char *buf, char *note, unsigned first,
unsigned last, srmio_error_t *err )
{
/* TODO: iconv notes -> cp850 */
buf_set_string( buf, 0, note, 255 );
buf_set_uint8( buf, 255, 1 ); /* active */
if( ! buf_set_luint16( buf, 256, first+1 ) ){
srmio_error_errno( err, "set marker" );
return false;
}
if( ! buf_set_luint16( buf, 258, last+1 ) ){
srmio_error_errno( err, "set marker" );
return false;
}
memset( &buf[260], 0, 10 );
return true;
}
/*
* write contents of data structure into specified file
*/
bool srmio_file_srm7_write( srmio_data_t data, FILE *fh, srmio_error_t *err )
{
unsigned char buf[1024];
srmio_marker_t *blocks;
srmio_time_t timerefday;
srmio_time_t recint;
unsigned i;
if( ! data ){
srmio_error_set( err, "no data to write" );
return false;
}
if( data->notes && strlen(data->notes) > 255 ){
srmio_error_set( err, "notes are too long" );
return false;
}
if( data->athlete && strlen(data->athlete) > 255 ){
srmio_error_set( err, "athlete name ist too long" );
return false;
}
if( data->cused < 1 || data->cused > UINT16_MAX ){
srmio_error_set( err, "too few/many chunks" );
return false;
}
if( data->mused > UINT16_MAX ){
srmio_error_set( err, "too many marker" );
return false;
}
if( ! srmio_data_recint( data, &recint, err ) )
return false;
if( NULL == (blocks = srmio_data_blocks( data, err )))
return false;
/* header */
{
srmio_chunk_t *ck;
srmio_time_t mintime = -1;
unsigned long bcnt;
unsigned mcnt = data->mused; /* +1 for all_workout marker */
unsigned days;
/* count blocks */
for( bcnt = 0; blocks[bcnt] && bcnt <= ULONG_MAX; ++bcnt );
/* time-jumps in PCV lead to nonlinear timestamps, find
* lowest one: */
for( ck = data->chunks; *ck; ++ck ){
if( mintime > (*ck)->time )
mintime = (*ck)->time;
}
if( (unsigned)-1 == ( days = _srm_mkdays( mintime, err )))
goto clean1;
if( (srmio_time_t)-1 == (timerefday = _srm_mktime( days, err )))
goto clean1;
DPRINTF( "mcnt=%u bcnt=%lu "
"mintime=%.1f "
"days=%u timerefday=%.1f "
"'%s'",
mcnt,
bcnt,
(double)mintime/10,
days,
(double)timerefday/10,
data->notes );
if( timerefday > mintime ){
srmio_error_set( err, "start time is big" );
goto clean1;
}
/* header */
memcpy( buf, "SRM7", 4 );
if( ! buf_set_luint16( buf, 4, days ) ){
srmio_error_errno( err, "set days" );
goto clean1;
}
if( ! buf_set_luint16( buf, 6, data->circum ) ){
srmio_error_errno( err, "set circum" );
goto clean1;
}
if( recint < 10 ){
buf_set_uint8( buf, 8, (unsigned char)(recint % 10) );
buf_set_uint8( buf, 9, 10u );
} else {
if( ! buf_set_uint8( buf, 8, recint / 10 ) ){
srmio_error_errno( err, "set recint" );
goto clean1;
}
buf_set_uint8( buf, 9, 1u );
}
if( ! buf_set_luint16( buf, 10, bcnt ) ){
srmio_error_errno( err, "set block count" );
goto clean1;
}
if( ! buf_set_luint16( buf, 12, mcnt ) ){
srmio_error_errno( err, "set marker count" );
goto clean1;
}
buf_set_uint8( buf, 14, 0 );
{
int len = 0;
if( data->notes )
len = strlen( data->notes );
if( len > 70 ){
DPRINTF( "notes are too long, truncating" );
len = 70;
}
buf_set_uint8( buf, 15, len );
}
/* TODO: iconv notes -> cp850 */
buf_set_string( buf, 16, data->notes, 70 );
if( ! _xwrite( fh, buf, 86, err ))
goto clean2;
}
/* first "marker" for athlete name */
if( ! set_marker( buf, data->athlete, 0, data->cused-1, err ) )
goto clean2;
if( ! _xwrite( fh, buf, 270, err ))
goto clean2;
/* other markers */
for( i = 0; i < data->mused; ++i ){
srmio_marker_t mk = data->marker[i];
if( ! set_marker( buf, mk->notes, mk->first, mk->last, err ))
goto clean2;
DPRINTF( "marker @0x%lx %u %u %s",
(unsigned long)ftell(fh),
mk->first,
mk->last,
mk->notes );
if( ! _xwrite( fh, buf, 270, err ))
goto clean2;
}
/* blocks */
for( i = 0; blocks[i]; ++i ){
srmio_marker_t bk = blocks[i];
srmio_chunk_t ck = data->chunks[bk->first];
unsigned blockdelta;
unsigned len = bk->last - bk->first +1;
blockdelta = ck->time - timerefday;
if( blockdelta * 10 < blockdelta ){
srmio_error_set(err, "block %u ref=%.1f: "
"timespan %u too large, "
"data covers too much time",
i,
(double)timerefday/10,
blockdelta );
goto clean2;
}
blockdelta *= 10;
DPRINTF( "block @0x%lx %.1f %u",
(unsigned long)ftell( fh ),
(double)ck->time/10,
blockdelta );
if( ! buf_set_luint32( buf, 0, blockdelta) ){
srmio_error_errno( err, "set block time" );
goto clean1;
}
if( ! buf_set_luint16( buf, 4, len) ){
srmio_error_errno( err, "set block chunks" );
goto clean1;
}
if( ! _xwrite( fh, buf, 6, err ))
goto clean2;
}
/* calibration */
DPRINTF( "cal @0x%lx", (unsigned long)ftell( fh ) );
{
/* TODO: check overflow: slope */
unsigned slope = 0.5 + ( data->slope * 42781) / 140;
if( ! buf_set_luint16( buf, 0, data->zeropos ) ){
srmio_error_errno( err, "set zeropos" );
goto clean1;
}
if( ! buf_set_luint16( buf, 2, slope ) ){
srmio_error_errno( err, "set slope" );
goto clean1;
}
if( ! buf_set_luint16( buf, 4, data->cused ) ){
srmio_error_errno( err, "set chunk count" );
goto clean1;
}
buf_set_uint8( buf, 6, 0 );
if( ! _xwrite( fh, buf, 7, err ))
goto clean2;
}
/* data */
DPRINTF( "data @0x%lx", (unsigned long)ftell( fh ) );
for( i = 0; blocks[i]; ++i ){
srmio_marker_t bk = blocks[i];
unsigned ci;
DPRINTF( "block#%u from %u to %u",
i,
bk->first,
bk->last );
for( ci = bk->first; ci <= bk->last; ++ci ){
srmio_chunk_t ck = data->chunks[ci];
/* TODO: check overflow: speed, temp */
int32_t speed = 0.5 + ( ck->speed * 1000) / 3.6;
int16_t temp = 0.5 + ck->temp * 10;
if( ! buf_set_luint16( buf, 0, ck->pwr ) ){
srmio_error_errno( err, "set power" );
goto clean1;
}
if( ! buf_set_uint8( buf, 2, ck->cad ) ){
srmio_error_errno( err, "set cadence" );
goto clean1;
}
if( ! buf_set_uint8( buf, 3, ck->hr ) ){
srmio_error_errno( err, "set heartrate" );
goto clean1;
}
if( ! buf_set_lint32( buf, 4, speed ) ){
srmio_error_errno( err, "set speed" );
goto clean1;
}
if( ! buf_set_lint32( buf, 8, ck->ele ) ){
srmio_error_errno( err, "set elevation" );
goto clean1;
}
if( ! buf_set_lint16( buf, 12, temp ) ){
srmio_error_errno( err, "set temperature" );
goto clean1;
}
if( ! _xwrite( fh, buf, 14, err ))
goto clean2;
}
srmio_marker_free(blocks[i]);
}
free( blocks );
return true;
clean2:
clean1:
return false;
}