comparison rvinterf/old/before-rvinterf/packetrx.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
comparison
equal deleted inserted replaced
-1:000000000000 0:e7502631a0f9
1 /*
2 * This module handles the lowest level of serial packet Rx
3 */
4
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include "pktmux.h"
10
11 extern int target_fd;
12
13 #define MAXPKT 512
14 u_char rxpkt[MAXPKT];
15 size_t rxpkt_len;
16
17 extern char pr_item[];
18
19 static int in_pkt, dle_state, toobig;
20
21 static void
22 process_inbyte(inb)
23 {
24 if (!in_pkt) {
25 if (inb != STX || dle_state) {
26 rxpkt_len++;
27 dle_state = (inb == DLE);
28 return;
29 }
30 if (rxpkt_len) {
31 sprintf(pr_item,
32 "Warning: Rx %u byte%s outside of a packet",
33 (unsigned)rxpkt_len, rxpkt_len != 1 ? "s" : "");
34 print_item();
35 rxpkt_len = 0;
36 }
37 in_pkt = 1;
38 toobig = 0;
39 return;
40 }
41 if (dle_state) {
42 dle_state = 0;
43 if (inb != STX && inb != DLE) {
44 sprintf(pr_item,
45 "Rx framing error: %02X after DLE\n", inb);
46 print_item();
47 in_pkt = 0;
48 rxpkt_len = 0;
49 return;
50 }
51 goto data;
52 }
53 if (inb == DLE) {
54 dle_state = 1;
55 return;
56 } else if (inb == STX) {
57 if (!rxpkt_len)
58 return;
59 in_pkt = 0;
60 handle_rx_packet();
61 rxpkt_len = 0;
62 return;
63 }
64 data: if (rxpkt_len >= MAXPKT) {
65 if (!toobig) {
66 sprintf(pr_item, "Error: Rx packet too big!\n");
67 print_item();
68 toobig = 1;
69 }
70 return;
71 }
72 rxpkt[rxpkt_len++] = inb;
73 }
74
75 void
76 process_serial_rx()
77 {
78 u_char rdbuf[512];
79 int cc, i;
80
81 cc = read(target_fd, rdbuf, sizeof rdbuf);
82 if (cc <= 0) {
83 perror("Error/EOF reading from target");
84 exit(1);
85 }
86 for (i = 0; i < cc; i++)
87 process_inbyte(rdbuf[i]);
88 }