comparison rvinterf/lowlevel/packetrx.c @ 173:f42854da4563

rvinterf: beginning of refactoring
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 22 Nov 2013 05:56:07 +0000
parents
children 3256dc6e84ae
comparison
equal deleted inserted replaced
172:019120585a1c 173:f42854da4563
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 }