comparison rvinterf/libasync/interf.c @ 0:e7502631a0f9

initial import from freecalypso-sw rev 1033:5ab737ac3ad7
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Jun 2016 00:13:35 +0000
parents
children 09b4fd9b3827
comparison
equal deleted inserted replaced
-1:000000000000 0:e7502631a0f9
1 /*
2 * This module implements the link to rvinterf.
3 */
4
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include "localsock.h"
10
11 extern int sock;
12
13 u_char rvi_msg[LOCALSOCK_MAX_MSG];
14 int rvi_msg_len;
15
16 static int rx_state, rx_left;
17 static u_char *rx_ptr;
18
19 void
20 localsock_prep_for_length_rx()
21 {
22 rx_state = 0;
23 rx_ptr = rvi_msg;
24 rx_left = 2;
25 }
26
27 static void
28 prep_for_message_rx()
29 {
30 rx_state = 1;
31 rx_ptr = rvi_msg;
32 rx_left = rvi_msg_len;
33 }
34
35 void
36 process_msg_from_rvinterf()
37 {
38 switch (rvi_msg[0]) {
39 case RVI2CLI_PKT_FROM_TARGET:
40 process_pkt_from_target();
41 return;
42 case RVI2CLI_LOCAL_CMD_RESP:
43 if (rvi_msg_len < 2)
44 goto bad;
45 if (rvi_msg[1] == '+')
46 return;
47 tty_cleanup();
48 fprintf(stderr, "Error from rvinterf: %.*s\n", rvi_msg_len - 1,
49 rvi_msg + 1);
50 exit(1);
51 default:
52 bad:
53 tty_cleanup();
54 fprintf(stderr,
55 "Error: unexpected message type %02X from rvinterf\n",
56 rvi_msg[0]);
57 exit(1);
58 }
59 }
60
61 void
62 handle_rvinterf_input()
63 {
64 int cc;
65
66 cc = read(sock, rx_ptr, rx_left);
67 if (cc <= 0) {
68 tty_cleanup();
69 perror("read from rvinterf socket");
70 exit(1);
71 }
72 rx_ptr += cc;
73 rx_left -= cc;
74 if (rx_left)
75 return;
76 /* got the thing, process it */
77 if (rx_state) {
78 process_msg_from_rvinterf();
79 localsock_prep_for_length_rx();
80 } else {
81 rvi_msg_len = rvi_msg[0] << 8 | rvi_msg[1];
82 if (rvi_msg_len < 1 || rvi_msg_len > LOCALSOCK_MAX_MSG) {
83 tty_cleanup();
84 fprintf(stderr,
85 "Invalid length from rvinterf: %02X%02X\n",
86 rvi_msg[0], rvi_msg[1]);
87 exit(1);
88 }
89 prep_for_message_rx();
90 }
91 }
92
93 void
94 send_pkt_to_target(pkt, pktlen)
95 u_char *pkt;
96 {
97 u_char hdrbuf[3];
98 int len1;
99
100 len1 = pktlen + 1;
101 hdrbuf[0] = len1 >> 8;
102 hdrbuf[1] = len1 & 0xFF;
103 hdrbuf[2] = CLI2RVI_PKT_TO_TARGET;
104 write(sock, hdrbuf, 3);
105 write(sock, pkt, pktlen);
106 }