forked from networkupstools/nut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathal175.c
1386 lines (1088 loc) · 30.9 KB
/
al175.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
/*
* al175.c - NUT support for Eltek AL175 alarm module.
* AL175 shall be in COMLI mode.
*
* Copyright (C) 2004 Marine & Bridge Navigation Systems <http://mns.spb.ru>
* Copyright (C) 2004 Kirill Smelkov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stddef.h>
#include <ctype.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include "main.h"
#include "serial.h"
#include "al175.h"
#define DEBUG 1
#define STX 0x02
#define ETX 0x03
#define ACK 0x06
typedef unsigned char byte_t;
#if DEBUG
#define XTRACE(fmt, ...) do { \
upsdebugx(1, "%s: " fmt, __FUNCTION__ __VA_ARGS__); \
} while (0)
#else
#define XTRACE(fmt, ...) do {} while (0)
#endif
/************
* RAW DATA *
************/
/**
* raw_data buffer representation
*/
typedef struct {
byte_t *buf; //!< the whole buffer address
unsigned buf_size; //!< the whole buffer size
byte_t *begin; //!< begin of content
byte_t *end; //!< one-past-end of content
} raw_data_t;
/**
* constant raw_data buffer representation
*/
#if 0
typedef struct {
const byte_t *buf; //!< the whole buffer address
unsigned buf_size; //!< the whole buffer size
const byte_t *begin; //!< begin of content
const byte_t *end; //!< one-past-end of content
} const_raw_data_t;
#else
typedef raw_data_t const_raw_data_t;
#endif
/**
* alloca raw_data buffer
* @param size the size in bytes
* @return alloca'ed memory as raw_data
*/
#define raw_alloca(size) \
({ \
raw_data_t data; \
\
data.buf = alloca(size); \
data.buf_size = size; \
\
data.begin = data.buf; \
data.end = data.buf; \
\
data; \
})
/**
* xmalloc raw buffer
* @param size the size in bytes
* @return xmalloc'ed memory as raw_data
*/
raw_data_t raw_xmalloc(size_t size)
{
raw_data_t data = {
.buf = xmalloc(size),
.buf_size = size,
/* XXX: how to handle this?
.begin = .buf,
.end = .buf
*/
};
data.begin = data.buf;
data.end = data.buf;
return data;
}
/**
* free raw_data buffer
* @param buf the buffer to be freed
*/
void raw_free(raw_data_t *buf)
{
free(buf->buf);
buf->buf = NULL;
buf->buf_size = 0;
buf->begin = NULL;
buf->end = NULL;
}
/* taken from www.asciitable.com */
static const char* ascii_symb[] = {
"NUL", /* 0x00 */
"SOH", /* 0x01 */
"STX", /* 0x02 */
"ETX", /* 0x03 */
"EOT", /* 0x04 */
"ENQ", /* 0x05 */
"ACK", /* 0x06 */
"BEL", /* 0x07 */
"BS", /* 0x08 */
"TAB", /* 0x09 */
"LF", /* 0x0A */
"VT", /* 0x0B */
"FF", /* 0x0C */
"CR", /* 0x0D */
"SO", /* 0x0E */
"SI", /* 0x0F */
"DLE", /* 0x10 */
"DC1", /* 0x11 */
"DC2", /* 0x12 */
"DC3", /* 0x13 */
"DC4", /* 0x14 */
"NAK", /* 0x15 */
"SYN", /* 0x16 */
"ETB", /* 0x17 */
"CAN", /* 0x18 */
"EM", /* 0x19 */
"SUB", /* 0x1A */
"ESC", /* 0x1B */
"FS", /* 0x1C */
"GS", /* 0x1D */
"RS", /* 0x1E */
"US" /* 0x1F */
};
/**
* dump raw_data buffer to file stream
* @param out output stream
* @param buf the buffer to dump
*/
void raw_dump(FILE *out, const_raw_data_t buf)
{
byte_t *p;
fprintf(out, "( ");
for (p=buf.begin; p!=buf.end; ++p)
if (*p < 0x20)
fprintf(out, "%4s ", ascii_symb[*p]);
else
fprintf(out, "'%c' ", *p);
fprintf(out, ")\t[ ");
for (p=buf.begin; p!=buf.end; ++p)
fprintf(out, "0x%02x ", *p);
fprintf(out, "]\n");
}
/***************************************************************************/
/***************
* COMLI types *
***************/
/**
* COMLI message header info
* @see 1. INTRODUCTION
*/
typedef struct {
int id; //!< Id[1:2]
int stamp; //!< Stamp[3]
int type; //!< Mess Type[4]
} msg_head_t;
/**
* COMLI IO header info
* @see 1. INTRODUCTION
*/
typedef struct {
unsigned addr; //!< Addr[5:8]
unsigned len; //!< NOB[9:10]
} io_head_t;
/**
* maximum allowed io.len value
*/
#define IO_LEN_MAX 0xff
/**
* COMLI header info
* @see 1. INTRODUCTION
*/
typedef struct {
msg_head_t msg; //!< message header [1:4]
io_head_t io; //!< io header [5:10]
} comli_head_t;
/******************
* MISC UTILITIES *
******************/
/**
* convert hex string to int
* @param head input string
* @param count string length
* @return parsed value (>=0) if success, -1 on error
*/
static long from_hex(const char *head, unsigned len)
{
long val=0;
while (len-- != 0) {
int ch = *head;
if (!isxdigit(ch))
return -1; /* wrong character */
val *= 0x10;
ch = toupper(ch); /* ? locale ? */
if (isdigit(ch))
val += (ch-'0');
else
val += 0x0a + (ch-'A');
++head;
}
return val;
}
/**
* compute checksum of a buffer
* @see 10. CHECKSUM BCC
* @param buf buffer address
* @param count no. of bytes in the buffer
* @return computed checksum
*/
static byte_t compute_bcc(const void *buf, size_t count)
{
byte_t bcc=0;
unsigned i;
for (i=0; i<count; ++i)
bcc ^= *((byte_t *)buf + i);
return bcc;
}
/* XXX: not optimal */
/**
* revers bits in a byte from right to left and vice-versa
* @see 6. CODING AND DECODING OF REGISTER VALUES
*/
#define REVERSE_BITS(x) (\
( ((x) & 0x80) >> 7 ) | \
( ((x) & 0x40) >> 5 ) | \
( ((x) & 0x20) >> 3 ) | \
( ((x) & 0x10) >> 1 ) | \
( ((x) & 0x08) << 1 ) | \
( ((x) & 0x04) << 3 ) | \
( ((x) & 0x02) << 5 ) | \
( ((x) & 0x01) << 7 ) )
/**
* reverse bits in a buffer
* @param buf buffer address
* @param count no. of bytes in the buffer
*/
static void reverse_bits(void *buf, size_t count)
{
byte_t *mem = buf;
while (count!=0) {
*mem = REVERSE_BITS(*mem);
++mem;
--count;
}
}
/********************************************************************/
/*
* communication basics
*
* ME (Monitor Equipment)
* PRS (? Power System ? )
*
* there are 2 types of transactions:
*
* 'ACTIVATE COMMAND'
* ME -> PRS (al_prep_activate)
* ME <- PRS [ack] (al_check_ack)
*
*
* 'READ REGISTER'
* ME -> PRS (al_prep_read_req)
* ME <- PRS [data] (al_parse_reply)
*
*/
/********************
* COMLI primitives *
********************/
/************************
* COMLI: OUTPUT FRAMES *
************************/
/**
* prepare COMLI sentence
* @see 1. INTRODUCTION
* @param dest [out] where to put the result
* @param h COMLI header info
* @param buf data part of the sentence
* @param count amount of data bytes in the sentence]
*
* @note: the data are copied into the sentence "as-is", that is no conversion is done.
* if the coller want to reevrse bits it is necessary to call reverse_bits(...) prior
* to comli_prepare.
*/
static void comli_prepare(raw_data_t *dest, const comli_head_t *h, const void *buf, size_t count)
{
/*
* 0 1 2 3 4 5 6 7 8 9 10 11 - - - N-1 N
* +-----+---------+-------+------+-------------------------+-----------+------------+-----+-----+
* | STX | IDh IDl | Stamp | type | addr1 addr2 addr3 addr4 | NOBh NOBl | ...data... | ETX | BCC |
* +-----+---------+-------+------+-------------------------+-----------+------------+-----+-----+
*
* ^ ^
* | |
*begin end
*/
byte_t *out = dest->begin;
/* it's caller responsibility to allocate enough space.
else it is a bug in the program */
if ( (out+11+count+2) > (dest->buf + dest->buf_size) )
fatalx(EXIT_FAILURE, "too small dest in comli_prepare\n");
out[0] = STX;
snprintf(out+1, 10+1, "%02X%1i%1i%04X%02X", h->msg.id, h->msg.stamp, h->msg.type, h->io.addr, h->io.len);
memcpy(out+11, buf, count);
reverse_bits(out+11, count);
out[11+count] = ETX;
out[12+count] = compute_bcc(out+1, 10+count+1);
dest->end = dest->begin + (11+count+2);
}
/**
* prepare AL175 read data request
* @see 2. MESSAGE TYPE 2 (COMMAND SENT FROM MONITORING EQUIPMENT)
* @param dest [out] where to put the result
* @param addr start address of requested area
* @param count no. of requested bytes
*/
static void al_prep_read_req(raw_data_t *dest, unsigned addr, size_t count)
{
comli_head_t h = {
.msg = {
.id = 0x14,
.stamp = 1,
.type = 2,
},
.io = {
.addr = addr,
.len = count,
},
};
return comli_prepare(dest, &h, NULL, 0);
}
/**
* prepare AL175 activate command
* @see 4. MESSAGE TYPE 0 (ACTIVATE COMMAND)
* @param dest [out] where to put the result
* @param cmd command type [11]
* @param subcmd command subtype [12]
* @param pr1 first parameter [13:14]
* @param pr2 second parameter [15:16]
* @param pr3 third parameter [17:18]
*/
static void al_prep_activate(raw_data_t *dest, int cmd, int subcmd, int pr1, int pr2, int pr3)
{
comli_head_t h = {
.msg = {
.id = 0x14,
.stamp = 1,
.type = 0,
},
.io = {
.addr = 0x4500,
.len = 8,
},
};
char data[8+1];
data[0] = cmd; // XXX: shall be ASCII here
data[1] = subcmd;
snprintf(data+2, 6+1, "%2X%2X%2X", pr1, pr2, pr3);
return comli_prepare(dest, &h, data, 8);
}
/***********************
* COMLI: INPUT FRAMES *
***********************/
/**
* check COMLI frame for correct layout and bcc
* @param f frame to check
*
* @return 0 (ok) -1 (error)
*/
static int comli_check_frame(const_raw_data_t f)
{
int bcc;
byte_t *tail;
if (*f.begin!=STX)
return -1;
tail = f.end - 2;
if (tail <= f.begin)
return -1;
if (tail[0]!=ETX)
return -1;
bcc = compute_bcc(f.begin+1, (f.end - f.begin) - 2/*STX & BCC*/);
if (bcc!= tail[1])
return -1;
return 0;
}
/**
* parse reply header from PRS
* @see 3. MESSAGE TYPE 0 (REPLY FROM PRS ON MESSAGE TYPE 2)
*
* @param io [out] parsed io_header
* @param raw_reply_head [in] raw reply header from PRS
* @return 0 (ok), -1 (error)
*
* @see al_parse_reply
*/
static int al_parse_reply_head(io_head_t *io, const raw_data_t raw_reply_head)
{
/*
* 0 1 2 3 4 5 6 7 8 9 10
* +-----+---------+-------+------+-------------------------+-----------+-----------+
* | STX | IDh IDl | Stamp | type | addr1 addr2 addr3 addr4 | NOBh NOBl | ......... |
* +-----+---------+-------+------+-------------------------+-----------+-----------+
*
* ^ ^
* | |
* begin end
*/
unsigned long io_addr, io_len;
const byte_t *reply_head = raw_reply_head.begin - 1;
if ( (raw_reply_head.end - raw_reply_head.begin) != 10) {
XTRACE("wrong size");
return -1; /* wrong size */
}
if (reply_head[1]!='0' || reply_head[2]!='0') {
XTRACE("wrong id");
return -1; /* wrong id */
}
if (reply_head[3]!='1') {
XTRACE("wrong stamp");
return -1; /* wrong stamp */
}
if (reply_head[4]!='0') {
XTRACE("wrong type");
return -1; /* wrong type */
}
io_addr = from_hex(&reply_head[5], 4);
if (io_addr==-1UL) {
XTRACE("wrong addr");
return -1; /* wrong addr */
}
io_len = from_hex(&reply_head[9], 2);
if (io_len==-1UL) {
XTRACE("wrong nob");
return -1; /* wrong NOB */
}
if (io_len > IO_LEN_MAX) {
XTRACE("nob too big");
return -1; /* too much data claimed */
}
io->addr = io_addr;
io->len = io_len;
return 0;
}
/**
* parse reply from PRS
* @see 3. MESSAGE TYPE 0 (REPLY FROM PRS ON MESSAGE TYPE 2)
* @param io_head [out] parsed io_header
* @param io_buf [in] [out] raw_data where to place incoming data (see ...data... below)
* @param raw_reply raw reply from PRS to check
* @return 0 (ok), -1 (error)
*
* @see al_parse_reply_head
*/
static int al_parse_reply(io_head_t *io_head, raw_data_t *io_buf, const_raw_data_t raw_reply)
{
/*
* 0 1 2 3 4 5 6 7 8 9 10 11 - - - N-1 N
* +-----+---------+-------+------+-------------------------+-----------+------------+-----+-----+
* | STX | IDh IDl | Stamp | type | addr1 addr2 addr3 addr4 | NOBh NOBl | ...data... | ETX | BCC |
* +-----+---------+-------+------+-------------------------+-----------+------------+-----+-----+
*
* ^ ^
* | |
* begin end
*/
int err;
unsigned i;
const byte_t *reply = raw_reply.begin - 1;
/* 1: extract header and parse it */
const_raw_data_t raw_reply_head = raw_reply;
if (raw_reply_head.begin + 10 <= raw_reply_head.end)
raw_reply_head.end = raw_reply_head.begin + 10;
err = al_parse_reply_head(io_head, raw_reply_head);
if (err==-1)
return -1;
/* 2: process data */
reply = raw_reply.begin - 1;
if ( (raw_reply.end - raw_reply.begin) != (ptrdiff_t)(10 + io_head->len)) {
XTRACE("corrupt sentence");
return -1; /* corrupt sentence */
}
/* extract the data */
if (io_buf->buf_size < io_head->len) { // XXX: maybe write from io_buf->begin ?
XTRACE("too much data to fit in io_buf.");
return -1; /* too much data to fit in io_buf */
}
io_buf->begin = io_buf->buf; // XXX: see ^^^
io_buf->end = io_buf->begin;
for (i=0; i<io_head->len; ++i)
*(io_buf->end++) = reply[11+i];
reverse_bits(io_buf->begin, (io_buf->end - io_buf->begin) );
#if DEBUG
fprintf(stderr, "rx_buf:\t\t");
raw_dump(stderr, *io_buf);
#endif
return 0; /* all ok */
}
/**
* check acknowledge from PRS
* @see 5. ACKNOWLEDGE FROM PRS
* @param raw_ack raw acknowledge from PRS to check
* @return 0 on success, -1 on error
*/
static int al_check_ack(const_raw_data_t raw_ack)
{
/*
* 0 1 2 3 4 5 6 7
* +-----+---------+-------+------+-----+-----+-----+
* | STX | IDh IDl | Stamp | type | ACK | ETX | BCC |
* +-----+---------+-------+------+-----+-----+-----+
*
* ^ ^
* | |
* begin end
*/
const byte_t *ack = raw_ack.begin - 1;
if ( (raw_ack.end - raw_ack.begin) !=5) {
XTRACE("wrong size");
return -1; /* wrong size */
}
if (ack[1]!='0' || ack[2]!='0') {
XTRACE("wrong id");
return -1; /* wrong id */
}
/* the following in not mandated. it is just said it will be
* "same as one received". but we always send '1' (0x31) as stamp
* (see 4. MESSAGE TYPE 0 (ACTIVATE COMMAND). Hence, stamp checking
* is hardcoded here.
*/
if (ack[3]!='1') {
XTRACE("wrong stamp");
return -1; /* wrong stamp */
}
if (ack[4]!='1') {
XTRACE("wrong type");
return -1; /* wrong type */
}
if (ack[4]!=ACK) {
XTRACE("wrong ack");
return -1; /* wrong ack */
}
return 0;
}
/******************************************************************/
/**********
* SERIAL *
**********/
static void alarm_handler(int sig)
{
/* just do nothing */
XTRACE("timeout...\n"); /* XXX: doing so is wrong from signal handler... */
}
/* clear any flow control (stolen from powercom.c) */
static void ser_disable_flow_control (void)
{
struct termios tio;
tcgetattr (upsfd, &tio);
tio.c_iflag &= ~ (IXON | IXOFF);
tio.c_cc[VSTART] = _POSIX_VDISABLE;
tio.c_cc[VSTOP] = _POSIX_VDISABLE;
upsdebugx(2, "Flow control disable");
/* disable any flow control */
tcsetattr(upsfd, TCSANOW, &tio);
}
static void flush_rx_queue()
{
ser_flush_in(upsfd, "", 1/*verbose*/);
}
/**
* transmit frame to PRS
*
* @param frame the frame to tansmit
* @return 0 (ok) -1 (error)
*/
static int tx(const_raw_data_t frame)
{
int err;
#if DEBUG
fprintf(stderr, "tx:\t\t");
raw_dump(stderr, frame);
#endif
err = ser_send_buf(upsfd, frame.begin, (frame.end - frame.begin) );
if (err==-1) {
upslogx(LOG_ERR, "failed to send frame to PRS: %s", strerror(errno));
return -1;
}
if (err != (frame.end - frame.begin)) {
upslogx(LOG_ERR, "sent incomplete frame to PRS");
return -1;
}
return 0;
}
/***********
* CHATTER *
***********/
static int get_char(char *ch)
{
/* 999 here means infinity.
* all timeouts are processed via alarm(2)
*/
return ser_get_char(upsfd, ch, 999, 0);
}
static int get_buf(char *buf, size_t len)
{
return ser_get_buf_len(upsfd, buf, len, 999, 0);
}
/**
* scan incoming bytes for specific character
*
* @return 0 (got it) -1 (error)
*/
static int scan_for(char c)
{
char in;
int err;
while (1) {
err = get_char(&in);
if (err==-1)
return -1;
if (in==c)
break;
}
return 0;
}
/**
* receive 'activate command' ACK from PRS
*
* @return 0 (ok) -1 (error)
*/
static int recv_command_ack()
{
int err;
raw_data_t ack;
/* 1: STX */
err = scan_for(STX);
if (err==-1)
return -1;
ack = raw_alloca(8);
*(ack.end++) = STX;
/* 2: ID1 ID2 STAMP MSG_TYPE ACK ETX BCC */
err = get_buf(ack.end, 7);
if (err!=7)
return -1;
ack.end += 7;
/* frame constructed - let's verify it */
#if DEBUG
fprintf(stderr, "rx:\t\t");
raw_dump(stderr, ack);
#endif
/* generic layout */
err = comli_check_frame(ack);
if (err==-1)
return -1;
/* shrink frame */
ack.begin += 1;
ack.end -= 2;
return al_check_ack(ack);
}
/**
* receive 'read register' data from PRS
* @param io [out] io header of received data
* @param io_buf [in] [out] where to place incoming data
*
* @return 0 (ok) -1 (error)
*/
static int recv_register_data(io_head_t *io, raw_data_t *io_buf)
{
int err;
raw_data_t reply_head;
raw_data_t reply;
/* 1: STX */
err = scan_for(STX);
if (err==-1)
return -1;
reply_head = raw_alloca(11);
*(reply_head.end++) = STX;
/* 2: ID1 ID2 STAMP MSG_TYPE ADDR1 ADDR2 ADDR3 ADDR4 LEN1 LEN2 */
err = get_buf(reply_head.end, 10);
if (err!=10)
return -1;
reply_head.end += 10;
#if DEBUG
fprintf(stderr, "rx_head:\t");
raw_dump(stderr, reply_head);
#endif
/* 3: check header, extract IO info */
reply_head.begin += 1; /* temporarily strip STX */
err = al_parse_reply_head(io, reply_head);
if (err==-1)
return -1;
reply_head.begin -= 1; /* restore STX */
#if DEBUG
fprintf(stderr, "io: %x/%x\n", io->addr, io->len);
#endif
/* 4: allocate space for full reply and copy header there */
reply = raw_alloca(11/*head*/ + io->len/*data*/ + 2/*ETX BCC*/);
memcpy(reply.end, reply_head.begin, (reply_head.end - reply_head.begin));
reply.end += (reply_head.end - reply_head.begin);
/* 5: receive tail of the frame */
err = get_buf(reply.end, io->len + 2);
if (err!=(int)(io->len+2)) {
XTRACE("rx_tail failed");
return -1;
}
reply.end += io->len + 2;
/* frame constructed, let's verify it */
#if DEBUG
fprintf(stderr, "rx:\t\t");
raw_dump(stderr, reply);
#endif
/* generic layout */
err = comli_check_frame(reply);
if (err==-1) {
XTRACE("corrupt frame");
return -1;
}
/* shrink frame */
reply.begin += 1;
reply.end -= 2;
// XXX: a bit of processing duplication here
return al_parse_reply(io, io_buf, reply);
}
/*****************************************************************/
/*********************
* AL175: DO COMMAND *
*********************/
/**
* do 'ACTIVATE COMMAND'
*
* @return 0 (ok) -1 (error)
*/
static int al175_do(int cmd, int subcmd, int pr1, int pr2, int pr3)
{
int err;
raw_data_t CTRL_frame = raw_alloca(512);
al_prep_activate(&CTRL_frame, cmd, subcmd, pr1, pr2, pr3);
flush_rx_queue(); /* DROP */
err = tx(CTRL_frame); /* TX */
if (err==-1)
return -1;
return recv_command_ack(); /* RX */
}
/**
* 'READ REGISTER'
*
*/
static int al175_read(byte_t *dst, unsigned addr, size_t count)
{
int err;
raw_data_t REQ_frame = raw_alloca(512);
raw_data_t rx_data;
io_head_t io;
al_prep_read_req(&REQ_frame, addr, count);
flush_rx_queue(); /* DROP */
err = tx(REQ_frame); /* TX */
if (err==-1)
return -1;
rx_data.buf = dst;
rx_data.buf_size = count;
rx_data.begin = dst;
rx_data.end = dst;
err = recv_register_data(&io, &rx_data);
if (err==-1)
return -1;
if (rx_data.begin != dst) // XXX: paranoia
return -1;
if ((rx_data.end - rx_data.begin) != (int)count)