comparison lcdpoll/interf.c @ 3:06e900c54ae3

fc-lcdpoll program put together, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 14 Mar 2018 23:04:44 +0000
parents
children
comparison
equal deleted inserted replaced
2:bff57443b0f7 3:06e900c54ae3
1 /*
2 * In this module we implement our synchronous interface to the target
3 * via rvinterf.
4 */
5
6 #include <sys/types.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <rvinterf/limits.h>
11 #include <rvinterf/localsock.h>
12 #include <rvinterf/pktmux.h>
13 #include <rvinterf/exitcodes.h>
14
15 extern int sock;
16
17 int rx_enable_state;
18 u_char rvi_msg[LOCALSOCK_MAX_MSG];
19 int rvi_msg_len;
20
21 static void
22 collect_bytes_from_rvi(buf, nbytes)
23 u_char *buf;
24 {
25 int cc;
26
27 while (nbytes) {
28 cc = read(sock, buf, nbytes);
29 if (cc <= 0) {
30 perror("read from rvinterf socket");
31 exit(ERROR_RVINTERF);
32 }
33 buf += cc;
34 nbytes -= cc;
35 }
36 }
37
38 collect_rvi_msg()
39 {
40 u_char lenbuf[2];
41
42 collect_bytes_from_rvi(lenbuf, 2);
43 rvi_msg_len = lenbuf[0] << 8 | lenbuf[1];
44 if (rvi_msg_len < 1 || rvi_msg_len > LOCALSOCK_MAX_MSG) {
45 fprintf(stderr, "Invalid length from rvinterf: %02X%02X\n",
46 lenbuf[0], lenbuf[1]);
47 exit(ERROR_RVINTERF);
48 }
49 collect_bytes_from_rvi(rvi_msg, rvi_msg_len);
50 return(0);
51 }
52
53 send_rvimisc_command(cmdpkt, cmdlen)
54 u_char *cmdpkt;
55 {
56 u_char lenbuf[2];
57
58 lenbuf[0] = 0;
59 lenbuf[1] = cmdlen;
60 write(sock, lenbuf, 2);
61 write(sock, cmdpkt, cmdlen);
62 }
63
64 rx_control(enable)
65 {
66 u_char cmdpkt[2];
67 int cmdlen;
68
69 /* are we already in the desired state? */
70 if (rx_enable_state == enable)
71 return(0);
72 /* no, do the work */
73 if (enable) {
74 cmdpkt[0] = CLI2RVI_WANT_MUXPROTO;
75 cmdpkt[1] = RVT_TM_HEADER;
76 cmdlen = 2;
77 } else {
78 cmdpkt[0] = CLI2RVI_RESET_PACKET_RX;
79 cmdlen = 1;
80 }
81 send_rvimisc_command(cmdpkt, cmdlen);
82 collect_rvi_msg();
83 if (rvi_msg[0] != RVI2CLI_LOCAL_CMD_RESP || rvi_msg_len < 2) {
84 fprintf(stderr,
85 "error: unexpected response to rvinterf local command\n");
86 exit(ERROR_RVINTERF);
87 }
88 if (rvi_msg[1] != '+') {
89 fprintf(stderr, "Error from rvinterf: %.*s\n", rvi_msg_len - 1,
90 rvi_msg + 1);
91 exit(ERROR_RVINTERF);
92 }
93 rx_enable_state = enable;
94 return(0);
95 }
96
97 send_pkt_to_target(pkt, pktlen)
98 u_char *pkt;
99 {
100 u_char hdrbuf[3];
101 int len1;
102
103 len1 = pktlen + 1;
104 hdrbuf[0] = len1 >> 8;
105 hdrbuf[1] = len1 & 0xFF;
106 hdrbuf[2] = CLI2RVI_PKT_TO_TARGET;
107 write(sock, hdrbuf, 3);
108 write(sock, pkt, pktlen);
109 }
110
111 target_pkt_exch(outpkt, outpktlen)
112 u_char *outpkt;
113 {
114 rx_control(1);
115 send_pkt_to_target(outpkt, outpktlen);
116 collect_rvi_msg();
117 if (rvi_msg[0] != RVI2CLI_PKT_FROM_TARGET) {
118 fprintf(stderr,
119 "error: unexpected response type from rvinterf\n");
120 exit(ERROR_RVINTERF);
121 }
122 return(0);
123 }
124
125 etm_pkt_exch(outbuf, outlen)
126 u_char *outbuf;
127 {
128 int i, c;
129
130 outbuf[0] = RVT_TM_HEADER;
131 c = 0;
132 for (i = 1; i <= outlen; i++)
133 c ^= outbuf[i];
134 outbuf[i] = c;
135 target_pkt_exch(outbuf, outlen + 2);
136 if (rvi_msg[1] != RVT_TM_HEADER) {
137 printf("error: packet from target is not ETM!\n");
138 return(ERROR_TARGET);
139 }
140 if (rvi_msg_len < 5) {
141 printf("error: ETM response packet is too short\n");
142 return(ERROR_TARGET);
143 }
144 c = 0;
145 for (i = 2; i < rvi_msg_len; i++)
146 c ^= rvi_msg[i];
147 if (c) {
148 printf("ETM response checksum error!\n");
149 return(ERROR_TARGET);
150 }
151 if (rvi_msg[2] != outbuf[1]) {
152 printf("error: target response is from wrong ETM component\n");
153 return(ERROR_TARGET);
154 }
155 return(0);
156 }