FreeCalypso > hg > freecalypso-sw
comparison rvinterf/packetrx.c @ 126:811b138f1bed
rvtdump utility written, compiles
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Thu, 31 Oct 2013 19:59:16 +0000 |
parents | |
children | 56b53c289785 |
comparison
equal
deleted
inserted
replaced
125:17c1e2a38418 | 126:811b138f1bed |
---|---|
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 static int in_pkt, dle_state, toobig; | |
18 | |
19 static void | |
20 process_inbyte(inb) | |
21 { | |
22 if (!in_pkt) { | |
23 if (inb != STX || dle_state) { | |
24 dle_state = (inb == DLE); | |
25 return; | |
26 } | |
27 in_pkt = 1; | |
28 rxpkt_len = 0; | |
29 toobig = 0; | |
30 return; | |
31 } | |
32 if (dle_state) { | |
33 dle_state = 0; | |
34 if (inb != STX && inb != DLE) { | |
35 printf("Rx framing error: %02X after DLE\n", inb); | |
36 in_pkt = 0; | |
37 return; | |
38 } | |
39 goto data; | |
40 } | |
41 if (inb == DLE) { | |
42 dle_state = 1; | |
43 return; | |
44 } else if (inb == STX) { | |
45 if (!rxpkt_len) | |
46 return; | |
47 in_pkt = 0; | |
48 handle_rx_packet(); | |
49 return; | |
50 } | |
51 data: if (rxpkt_len >= MAXPKT) { | |
52 if (!toobig) { | |
53 printf("Rx packet too big!\n"); | |
54 toobig = 1; | |
55 } | |
56 return; | |
57 } | |
58 rxpkt[rxpkt_len++] = inb; | |
59 } | |
60 | |
61 void | |
62 process_serial_rx() | |
63 { | |
64 u_char rdbuf[512]; | |
65 int cc, i; | |
66 | |
67 cc = read(target_fd, rdbuf, sizeof rdbuf); | |
68 if (cc <= 0) { | |
69 perror("Error/EOF reading from target"); | |
70 exit(1); | |
71 } | |
72 for (i = 0; i < cc; i++) | |
73 process_inbyte(rdbuf[i]); | |
74 } |