FreeCalypso > hg > freecalypso-sw
comparison rvinterf/etmsync/interf.c @ 277:e23fc1228efd
fc-fsio: low-level rvinterf link implemented
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Mon, 24 Feb 2014 03:42:47 +0000 |
parents | |
children | f77480d3dd21 |
comparison
equal
deleted
inserted
replaced
276:909f00c15f27 | 277:e23fc1228efd |
---|---|
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 "limits.h" | |
11 #include "localsock.h" | |
12 #include "pktmux.h" | |
13 #include "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 } |