-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpc5.c
1855 lines (1512 loc) · 40.5 KB
/
pc5.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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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"
#define STX ((char)0x2)
#define ETX ((char)0x3)
#define BLOCK_ACK ((const unsigned char *)"\x06")
#define BLOCK_NAK ((const unsigned char *)"\x15")
#define BLOCK_ABRT ((const unsigned char *)"\xaa")
#define PC5_BUFSIZE 1024
#define PC5_PKT_SIZE 64
#define PC5_PKT_CHUNKS 11u
/*
* connection handle
*/
struct _srmio_pc5_t {
/* connnection */
int stxetx; /* use stx/etx headers + encoding? */
time_t nready; /* PCV accepts next command */
/* xfer */
struct tm pctime;
struct _srmio_pc_xfer_block_t block;
size_t block_num;
unsigned char pkt_data[PC5_BUFSIZE];
unsigned pkt_cnt;
/* current pkt */
int pkt_num; /* -1 / 0..pkts-1 */
srmio_time_t pkt_time;
unsigned long pkt_dist;
int pkt_temp;
srmio_time_t pkt_recint;
/* current chunk */
unsigned chunk_num; /* within pkt 0..10 */
};
typedef struct _srmio_pc5_t *srmio_pc5_t;
#define SELF(x) ((srmio_pc5_t)x->child)
/*
* supported baudrates in order to try
* default is 9600 8n1
* srmwin tries:
* 9600,4800,19200,2400 Baud
* each baudrate with none, then with even parity
*/
static const srmio_io_baudrate_t _srmio_pc5_baud_probe[] = {
srmio_io_baud_9600,
srmio_io_baud_19200,
srmio_io_baud_4800,
srmio_io_baud_2400,
srmio_io_baud_max,
};
static const srmio_io_parity_t _srmio_pc5_parity_probe[] = {
srmio_io_parity_none,
srmio_io_parity_even,
srmio_io_parity_max,
};
static int _srmio_pc5_msg_decode(
unsigned char *out, size_t olen,
const unsigned char *in, size_t ilen,
srmio_error_t *err );
static bool _srmio_pc5_msg_send( srmio_pc_t conn,
char cmd, const unsigned char *arg, size_t alen,
srmio_error_t *err );
/************************************************************
*
* low level IO (open/read/write/close)
*
************************************************************/
/*
* send data towards SRM,
*
* returns true on success
*/
static bool _srmio_pc5_write( srmio_pc_t conn, const unsigned char *buf,
size_t blen, srmio_error_t *err )
{
srmio_error_t lerr;
int ret;
SRMIO_PC_DUMP( conn, buf, blen, "" );
ret = srmio_io_write( conn->io, buf, blen, &lerr );
if( ret < 0 ){
SRMIO_PC_ERROR( conn, err, "write failed: %s",
lerr.message );
return false;
}
if( (size_t)ret < blen ){
SRMIO_PC_ERROR( conn, err, "incomplete write: %d/%d",
ret, blen);
return false;
}
return true;
}
/*
* read up to specified number of bytes
*
* returns number of chars read
* returns -1 on error
*/
static int _srmio_pc5_read( srmio_pc_t conn, unsigned char *buf,
size_t want, srmio_error_t *err )
{
srmio_error_t lerr;
int ret;
ret = srmio_io_read( conn->io, buf, want, &lerr );
if( 0 > ret ){
SRMIO_PC_ERROR( conn, err, "read failed: %s",
lerr.message );
}
return ret;
}
/*
* set serial parameter
* wake up PCV
* send "hello"
* check returned version
* decode version
*
* returns true on success
*/
static bool _srmio_pc5_init(
srmio_pc_t conn,
const srmio_io_baudrate_t baudrate,
const srmio_io_parity_t parity,
srmio_error_t *err )
{
srmio_error_t lerr;
int ret;
unsigned char buf[20];
unsigned char verbuf[2];
unsigned baudname;
char parityname;
assert( conn );
assert( baudrate < srmio_io_baud_max );
assert( parity < srmio_io_parity_max );
SELF(conn)->stxetx = 1;
srmio_io_baud2name( baudrate, &baudname );
srmio_io_parity2name( parity, &parityname );
SRMIO_PC_LOG( conn, "trying comm %d/8%c1",
baudname,
parityname );
if( ! srmio_io_set_baudrate( conn->io, baudrate, &lerr ) ){
SRMIO_PC_ERROR( conn, err, "set baudrate failed: %s",
lerr.message );
return false;
}
if( ! srmio_io_set_parity( conn->io, parity, &lerr ) ){
SRMIO_PC_ERROR( conn, err, "set parity failed: %s",
lerr.message );
return false;
}
if( ! srmio_io_update( conn->io, &lerr )){
SRMIO_PC_ERROR( conn, err, "io update failed: %s",
lerr.message );
return false;
}
if( ! srmio_io_send_break( conn->io, &lerr ) ){
SRMIO_PC_ERROR( conn, err, "send break failed: %s",
lerr.message );
return false;
}
if( ! srmio_io_flush( conn->io, &lerr ) ){
SRMIO_PC_ERROR( conn, err, "flush failed: %s",
lerr.message );
return false;
}
/* send opening 'P' to verify communication works */
if( ! _srmio_pc5_msg_send( conn, 'P', NULL, 0, err ) )
return false;
ret = _srmio_pc5_read( conn, buf, 20, err );
if( ret < 0 ){
return false;
}
if( ret == 0 ){
SRMIO_PC_ERROR( conn, err, "got no opening response" );
return false;
}
SRMIO_PC_DUMP( conn, buf, ret, "got opening" );
/* autodetect communcitation type */
if( *buf == STX ){
if( ret != 7 || buf[1] != 'P' ){
SRMIO_PC_ERROR( conn, err, "opening response is garbled" );
return false;
}
if( 0 > _srmio_pc5_msg_decode( verbuf, 2, &buf[2], 4, &lerr ) ){
SRMIO_PC_ERROR( conn, err, "%s", lerr.message );
return false;
}
} else {
if( ret != 3 || buf[0] != 'P' ){
SRMIO_PC_ERROR( conn, err, "opening response is garbled" );
return false;
}
SELF(conn)->stxetx = 0;
SRMIO_PC_DEBUG( conn, "disabling stx/etx" );
memcpy( verbuf, &buf[1], 2 );
}
conn->baudrate = baudrate;
conn->parity = parity;
conn->firmware = buf_get_buint16( verbuf, 0 );
SRMIO_PC_LOG( conn, "found Powercontrol V, version 0x%x at %d/8%c1",
conn->firmware,
baudname,
parityname );
return true;
}
static bool _srmio_pc5_probe_parity( srmio_pc_t conn,
srmio_io_baudrate_t rate, srmio_error_t *err )
{
srmio_error_t lerr;
const srmio_io_parity_t *parity;
unsigned baud;
*lerr.message = 0;
if( ! srmio_io_baud2name( rate, &baud ) ){
SRMIO_PC_ERROR( conn, err, "invalid baudrate" );
return false;
}
for( parity = _srmio_pc5_parity_probe;
*parity < srmio_io_parity_max; ++parity ){
char pname;
if( _srmio_pc5_init( conn, rate, *parity, &lerr ) )
return true;
srmio_io_parity2name( *parity, &pname );
SRMIO_PC_LOG( conn, "probe comm %u/8%c1 failed: %s",
baud, pname, lerr.message );
}
SRMIO_PC_ERROR( conn, err, "no PCV found at %u/8x1: %s",
baud, lerr.message );
return false;
}
static bool _srmio_pc5_probe_baud( srmio_pc_t conn,
srmio_error_t *err )
{
srmio_error_t lerr;
const srmio_io_baudrate_t *rate;
*lerr.message = 0;
for( rate = _srmio_pc5_baud_probe; *rate < srmio_io_baud_max; ++rate ){
unsigned baud;
srmio_io_baud2name( *rate, &baud );
if( conn->parity == srmio_io_parity_max ){
if( _srmio_pc5_probe_parity( conn, *rate, &lerr ) )
return true;
SRMIO_PC_LOG( conn, "probe comm %u/8x1 failed: %s",
baud, lerr.message );
} else {
if( _srmio_pc5_init( conn, *rate, conn->parity, &lerr ) )
return true;
char pname;
srmio_io_parity2name( conn->parity, &pname );
SRMIO_PC_LOG( conn, "probe comm %u/8%c1 failed: %s",
baud, pname, lerr.message );
}
}
SRMIO_PC_ERROR( conn, err, "no PCV found at any baudrate: %s",
lerr.message );
return false;
}
/*
* serial handle must be opened before this
* set serial device properties
* get version from initial greeting
*
*/
static bool _srmio_pc5_open( srmio_pc_t conn, srmio_error_t *err )
{
srmio_error_t lerr;
assert( conn );
assert( conn->io );
SRMIO_PC_DEBUG( conn, "" );
if( ! srmio_io_set_flow( conn->io, srmio_io_flow_none, &lerr ) ){
SRMIO_PC_ERROR( conn, err, "set flow failed: %s",
lerr.message );
return false;
}
/* set serial parameter and get PCV version */
if( conn->baudrate == srmio_io_baud_max ){
if( ! _srmio_pc5_probe_baud( conn, err ) )
return false;
} else if( conn->parity == srmio_io_parity_max ){
if( ! _srmio_pc5_probe_parity( conn, conn->baudrate, err ) )
return false;
} else if( ! _srmio_pc5_init( conn, conn->baudrate, conn->parity, err)) {
return false;
}
return true;
}
/*
* close serial device
*
* parameter:
* conn: connection handle
*
* returns true on success
*/
bool _srmio_pc5_close( srmio_pc_t conn, srmio_error_t *err )
{
assert( conn );
(void) conn;
(void) err;
return true;
}
/************************************************************
*
* send/receive "messages" + helper
*
************************************************************/
/*
* Some SRM Firmware versions seem to use a protcoll heavyliy inspired by
* ASCII control sequences - namely STX, ETX, ACK and NAK. Furthermore
* most data is encoded "ASCII-friendly" - actually I was quite puzzled to
* read timestamps as-is within the *hexdump*.
*
* Other Firmware versions use the same "commands", but omit the STX/ETX and
* don't encode the data. The data is transmitted as-is. In this case you either
* have to know how much data to read from the SRM or read until it times
* out.
*
* In general communication has to be initiated by the computer by sending
* some command:
*
* - computer sends STX <command_char> <optional_data> ETX - SRM responds
* with STX <command_char> <optional_data> ETX
*
* command_char is a single char. The same command_char is used to - get
* the SRM's individual settings or - adjust the SRM's settings.
*
* When optional data is encoded the "ASCII-friendly" way,
* - each byte's actual value is converted to a hex string,
* - each hex-digit is used as a seperate byte + 0x30.
* eg. 0x3f is encoded as 0x33,0x3f
*
* To get a current setting, the computer sends 0x00 as optional_data. If
* necessary this is encoded "ASCII-friendly".
*
* To change a setting, the computer appends the new setting to the
* command as optional_data. If necessary, it's encoded ASCII-friendly.
* The SRM responds without any optional_data to confirm the operation.
*
* The response to commands to get the actual ride data off the SRM is
* always transmitted "unencoded. (Though the whole response might be
* wrapped in STX/ETX). Furthermore the SRM waits for confirmation
* (ACK/NAK) periodically. The download may be aborted by sending 0xAA,
* instead.
*
* FYI: I've tried to keep this as bitsex- - ehm - endianess-independent
* as possible. Therefore I've decided against read()ing a struct and
* parse the bytes manually... Please send patches if you can do this more
* elegant!
*
*/
/*
* encode data "ASCII-friendly"
*
* returns number of encoded bytes
* returns -1 on error
*/
static int _srmio_pc5_msg_encode(
unsigned char *out, size_t olen,
const unsigned char *in, size_t ilen )
{
size_t i;
size_t o=0;
assert( ! ilen || in );
assert( out );
assert( ! ilen || 2 * ilen > ilen ); /* overflow* */
assert( olen >= 2 * ilen ); /* output buffer size */
for( i=0; i < ilen; ++i ){
out[o++] = ((in[i] & 0xf0 ) >> 4) | 0x30;
out[o++] = (in[i] & 0x0f ) | 0x30;
}
DUMPHEX( "", out, o );
return o;
}
/*
* decode "ASCII-friendly" encoded data
*
* returns number of decoded bytes
* returns -1 on error
*/
static int _srmio_pc5_msg_decode(
unsigned char *out, size_t olen,
const unsigned char *in, size_t ilen,
srmio_error_t *err )
{
unsigned i;
unsigned o=0;
assert( ! ilen || in );
assert( out );
assert( ilen % 2 == 0 ); /* input must be multiple of 2 */
assert( olen * 2 >= ilen ); /* output buffer size */
for( i=0; i < ilen ; i += 2 ){
if( (in[i] & 0xf0) != 0x30
|| (in[i+1] & 0xf0) != 0x30 ){
srmio_error_set(err, "invalid input at %u", i );
return -1;
}
out[o] = ((in[i] & 0x0f) << 4)
| (in[i+1] & 0x0f);
++o;
}
DUMPHEX( "", out, o );
return o;
}
/*
* send command to SRM,
*
* takes care of stx/etx/encode when neccessary.
*
* returns decoded response length on success
* returns true on success
*/
static bool _srmio_pc5_msg_send( srmio_pc_t conn, char cmd,
const unsigned char *arg, size_t alen,
srmio_error_t *err )
{
srmio_error_t lerr;
unsigned char buf[PC5_BUFSIZE];
unsigned len = 0;
SRMIO_PC_DUMP( conn, arg, alen, "cmd=%c", cmd );
assert( conn );
assert( ! alen || arg );
assert( 2 * alen +3 >= alen ); /* owerflow? */
assert( 2 * alen +3 <= PC5_BUFSIZE ); /* request < buffer */
/* build command */
if( SELF(conn)->stxetx ){
buf[len++] = STX;
buf[len++] = cmd;
len += _srmio_pc5_msg_encode( &buf[len], PC5_BUFSIZE-2, arg, alen );
buf[len++] = ETX;
} else {
buf[len++] = cmd;
if( alen > 0 ){
memcpy(&buf[len], arg, alen );
len += alen;
}
}
/* send */
if( ! srmio_io_flush( conn->io, &lerr ) ){
SRMIO_PC_ERROR( conn, err, "flush failed: %s",
lerr.message );
return false;
}
SRMIO_PC_DUMP( conn, buf, len, "sending");
if( ! _srmio_pc5_write( conn, buf, len, err ) )
return false;
return true;
}
/*
* read response from SRM up to wanted length
*
* takes care of stx/etx/decode internaly when neccessary.
*
* response (without command char) is returned in rbuf (when rbuf != NULL)
*
* returns decoded response length on success
* returns -1 on error
*/
static int _srmio_pc5_msg_recv( srmio_pc_t conn,
unsigned char *rbuf, size_t rsize, size_t want,
srmio_error_t *err )
{
srmio_error_t lerr;
unsigned char buf[PC5_BUFSIZE];
size_t rlen = 0;
int ret;
assert( conn );
assert( ! rsize || rbuf ); /* opt. rbuf */
assert( ! rbuf || want <= rsize ); /* opt. buf size */
assert( 2 * want + 3 > want ); /* overflow */
assert( 2*want +3 <= PC5_BUFSIZE );
if( SELF(conn)->stxetx )
/* stx, etc, encoding */
want = 2*want +2;
/* cmd char */
++want;
SRMIO_PC_DEBUG( conn, "will read %lu ", (unsigned long)want );
/* read till
* - rsize is read
* - ETX is found (when stxetx is used)
* - read times out
*/
while( rlen < want ){
ret = _srmio_pc5_read( conn, &buf[rlen], 1, err );
if( ret < 0 ){
SRMIO_PC_DUMP( conn, buf, rlen, "" );
return -1;
}
/* timeout */
if( ret < 1 ){
SRMIO_PC_ERROR( conn, err, "read timed out" );
SRMIO_PC_DUMP( conn, buf, rlen, "" );
return -1;
}
++rlen;
/* avoid running into timeout */
if( SELF(conn)->stxetx && buf[rlen-1] == ETX ){
SRMIO_PC_DEBUG( conn, "msg complete - got ETX" );
break;
}
}
SRMIO_PC_DUMP( conn, buf, rlen, "" );
if( rlen < want ){
SRMIO_PC_ERROR( conn, err, "got incomplete response: %u/%u",
rlen, want );
return -1;
}
if( SELF(conn)->stxetx ){
if( rlen < 3 ){
SRMIO_PC_ERROR( conn, err, "response is too short for stxetx: %u",
rlen );
return -1;
}
if( buf[0] != STX ){
SRMIO_PC_ERROR( conn, err, "response is missing STX" );
return -1;
}
if( buf[rlen-1] != ETX ){
SRMIO_PC_ERROR( conn, err, "response is missing ETX" );
return -1;
}
rlen -= 3; /* stx, cmd, etx */
if( rbuf ){
ret = _srmio_pc5_msg_decode(rbuf, rsize,
&buf[2], rlen, &lerr );
if( ret < 0 ){
SRMIO_PC_ERROR( conn, err, "decode failed: %s",
lerr.message );
return -1;
}
rlen = ret;
} else
rlen /= 2;
} else {
if( rlen < 1 ){
SRMIO_PC_ERROR( conn, err, "response is too short: %u",
rlen );
return -1;
}
--rlen; /* cmd */
if( rbuf )
memcpy(rbuf, &buf[1], rlen);
}
return rlen;
}
/*
* Some commands keep the PCV busy after it responded. This
* sets the time to wait until the PCV accepts new commands.
*
* parameters:
* conn: connection handle
* delay: seconds the PCV will be busy
*
* return true on success
*/
static bool _srmio_pc5_msg_busy( srmio_pc_t conn, unsigned delay )
{
time( &SELF(conn)->nready );
SELF(conn)->nready += delay;
return true;
}
/*
* Wait for PCV to finish last command and become ready to
* accept the next one (if necessary)
*
*/
static void _srmio_pc5_msg_ready( srmio_pc_t conn )
{
time_t now;
unsigned delta;
time( &now );
if( SELF(conn)->nready <= now )
return;
delta = SELF(conn)->nready - now;
SRMIO_PC_LOG( conn, "PCV still busy for %usec, waiting...", delta );
sleep( delta );
return;
}
/*
* process one complete command (send, receive),
* retry if necesssary
*
* returns decoded response length on success
* returns -1 on error
*/
static int _srmio_pc5_msg( srmio_pc_t conn, char cmd,
const unsigned char *arg,
size_t alen,
unsigned char *buf,
size_t blen,
size_t want,
srmio_error_t *err )
{
int retries;
int ret;
assert( conn );
if( conn->xfer_state != srmio_pc_xfer_state_new ){
SRMIO_PC_ERROR( conn, err, "another command is still running" );
return -1;
}
_srmio_pc5_msg_ready( conn );
for( retries = 0; retries < 3; ++retries ){
if( retries ){
SRMIO_PC_LOG( conn,
"SRM isn't responding, send break and retry" );
srmio_io_send_break( conn->io, err );
sleep(1);
srmio_io_flush( conn->io, err );
}
if( ! _srmio_pc5_msg_send( conn, cmd, arg, alen, err ) )
return -1;
ret = _srmio_pc5_msg_recv( conn, buf, blen, want, err );
if( ret >= 0 )
break;
}
return ret;
}
/************************************************************
*
* set/get individual SRM parameter
*
************************************************************/
#define TIMEDEC(x) \
( ( ((x) & 0xf0) >> 4) * 10 + ((x) & 0x0f) )
#define TIMEENC(x) \
( ((unsigned char)((x) / 10) << 4 ) | ( (x) % 10 ) )
/*
* setting: get Firmware Version, readonly
* command: P
*/
/*
* parameter:
* conn: connection handle
*
* gets integer firmware version
* returns true on success
*/
bool srmio_pc5_cmd_get_version( srmio_pc_t conn, unsigned *version,
srmio_error_t *err )
{
unsigned char buf[20];
int ret;
assert( conn );
assert( conn->is_open );
assert( version );
ret = _srmio_pc5_msg( conn, 'P', NULL, 0, buf, 20, 2, err );
if( ret < 0 )
return false;
if( ret < 2 ){
SRMIO_PC_ERROR( conn, err, "got truncated version response" );
return false;
}
*version = buf_get_buint16( buf, 0 );
return true;
}
/*
* setting: athlete name
* command: N
* argument: cccccc\x20\x20 - it's 6 chars, zero-padded
*/
/*
* parameter:
* conn: connection handle
*
* gets malloc()ed string on succuess
* returns true on success
*/
static bool _srmio_pc5_cmd_get_athlete( srmio_pc_t conn, char **name,
srmio_error_t *err )
{
unsigned char buf[20];
int ret;
assert( conn );
assert( conn->is_open );
assert( name );
ret = _srmio_pc5_msg( conn, 'N', (unsigned char*)"\x0", 1,
buf, 20, 8, err );
if( ret < 0 )
return false;
if( ret < 6 ){
SRMIO_PC_ERROR( conn, err, "got truncated athlete response" );
return false;
}
*name = buf_get_string( buf, 0, 6 );
SRMIO_PC_DEBUG( conn, "athlete=%s", (char*)*name );
/* TODO: iconv cp850 -> internal ?? */
return true;
}
/*
* setting: current time
* command: M
* argument: 0xdd 0xmm 0xyy 0xHH 0xMM 0xSS
*/
/*
* get PCV's current date + time
*
* parameter:
* conn: connection handle
* timep: upated with PCV time (see "man mktime" for struct tm)
*
* returns true on success
*/
static bool _srmio_pc5_cmd_get_time( srmio_pc_t conn, struct tm *timep,
srmio_error_t *err )
{
unsigned char buf[20];
int ret;
assert( conn );
assert( conn->is_open );
assert( timep );
ret = _srmio_pc5_msg( conn, 'M', (unsigned char*)"\x0", 1,
buf, 20, 6, err );
if( ret < 0 )
return false;
if( ret < 6 ){
SRMIO_PC_ERROR( conn, err, "got truncated time response" );
return false;
}
timep->tm_mday = TIMEDEC( buf[0] );
timep->tm_mon = TIMEDEC( buf[1] ) -1;
timep->tm_year = TIMEDEC( buf[2] ) + 100;
timep->tm_hour = TIMEDEC( buf[3] );
timep->tm_min = TIMEDEC( buf[4] );
timep->tm_sec = TIMEDEC( buf[5] );
timep->tm_isdst = -1;
SRMIO_PC_DEBUG( conn, "time=%s", asctime( timep ) );
return true;
}
/*
* set PCV clock to specified date + time
*
* parameter:
* conn: connection handle
* timep: time to set (see "man mktime" for struct tm)
*
* returns true on success
*/
static bool _srmio_pc5_cmd_set_time( srmio_pc_t conn, struct tm *timep,
srmio_error_t *err )
{
unsigned char buf[6];
assert( conn );
assert( conn->is_open );
assert( timep );
/* TODO: check for overflows */
buf[0] = TIMEENC( timep->tm_mday );
buf[1] = TIMEENC( timep->tm_mon +1 );
buf[2] = TIMEENC( timep->tm_year -100 );
buf[3] = TIMEENC( timep->tm_hour );
buf[4] = TIMEENC( timep->tm_min );
buf[5] = TIMEENC( timep->tm_sec );
if( 0 > _srmio_pc5_msg( conn, 'M', buf, 6, NULL, 0, 0, err ))
return false;
SRMIO_PC_DEBUG( conn, "time=%s", asctime(timep) );
_srmio_pc5_msg_busy( conn, 2 );
return true;
}
/*
* setting: wheel circumference
* command: G
* argument: 0x0000 - uint16
*/
/*
* get wheel circumference
*
* parameter:
* conn: connection handle
*
* gets circumference
* returns true on success
*/
static bool _srmio_pc5_cmd_get_circum( srmio_pc_t conn, unsigned *circum,
srmio_error_t *err )
{
unsigned char buf[20];
int ret;
assert( conn );
assert( conn->is_open );
assert( circum );
ret = _srmio_pc5_msg( conn, 'G', (unsigned char*)"\x0\x0", 2,
buf, 20, 2, err );
if( ret < 0 )
return false;
if( ret < 2 ){
SRMIO_PC_ERROR( conn, err, "got truncated circumference response" );
return false;
}
*circum = buf_get_buint16( buf, 0 );
SRMIO_PC_DEBUG( conn, "circum=%d", *circum );
return true;
}
/*
* setting: slope
* command: E
* argument: 0x0000 - uint16
*/
/*
* get slope
*
* parameter:
* conn: connection handle
*
* gets slope eg. 17.4
* returns true on success
*/
static bool _srmio_pc5_cmd_get_slope( srmio_pc_t conn, double *slope,
srmio_error_t *err )
{
unsigned char buf[20];
int ret;
assert( conn );
assert( conn->is_open );
assert( slope );
ret = _srmio_pc5_msg( conn, 'E', (unsigned char*)"\x0\x0", 2, buf,
20, 2, err );
if( ret < 0 )
return false;