FreeCalypso > hg > freecalypso-sw
annotate rvinterf/lowlevel/packetrx.c @ 361:62f850da5d49
tfc139: log timestamp buglet
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Thu, 15 May 2014 11:44:36 +0000 |
parents | 2f285f20d617 |
children |
rev | line source |
---|---|
173
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
1 /* |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
2 * This module handles the lowest level of serial packet Rx |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
3 */ |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
4 |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
5 #include <sys/types.h> |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
6 #include <stdio.h> |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
7 #include <stdlib.h> |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
8 #include <unistd.h> |
256
2f285f20d617
rvinterf: *.h moved into include subdir
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
174
diff
changeset
|
9 #include "../include/pktmux.h" |
2f285f20d617
rvinterf: *.h moved into include subdir
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
174
diff
changeset
|
10 #include "../include/limits.h" |
173
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
11 |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 extern int target_fd; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 |
174
3256dc6e84ae
rvinterf: refactored rvtdump compiles and works
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
14 u_char rxpkt[MAX_PKT_FROM_TARGET]; |
173
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
15 size_t rxpkt_len; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 static int in_pkt, dle_state, toobig; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 static void |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 process_inbyte(inb) |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
21 { |
174
3256dc6e84ae
rvinterf: refactored rvtdump compiles and works
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
22 char errbuf[128]; |
3256dc6e84ae
rvinterf: refactored rvtdump compiles and works
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
23 |
173
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
24 if (!in_pkt) { |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
25 if (inb != STX || dle_state) { |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
26 rxpkt_len++; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
27 dle_state = (inb == DLE); |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
28 return; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
29 } |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
30 if (rxpkt_len) { |
174
3256dc6e84ae
rvinterf: refactored rvtdump compiles and works
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
31 sprintf(errbuf, |
173
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
32 "Warning: Rx %u byte%s outside of a packet", |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
33 (unsigned)rxpkt_len, rxpkt_len != 1 ? "s" : ""); |
174
3256dc6e84ae
rvinterf: refactored rvtdump compiles and works
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
34 output_line(errbuf); |
173
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
35 rxpkt_len = 0; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
36 } |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
37 in_pkt = 1; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
38 toobig = 0; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
39 return; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
40 } |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
41 if (dle_state) { |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
42 dle_state = 0; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
43 if (inb != STX && inb != DLE) { |
174
3256dc6e84ae
rvinterf: refactored rvtdump compiles and works
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
44 sprintf(errbuf, |
3256dc6e84ae
rvinterf: refactored rvtdump compiles and works
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
45 "Rx framing error: %02X after DLE", inb); |
3256dc6e84ae
rvinterf: refactored rvtdump compiles and works
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
46 output_line(errbuf); |
173
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
47 in_pkt = 0; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
48 rxpkt_len = 0; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
49 return; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
50 } |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
51 goto data; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
52 } |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
53 if (inb == DLE) { |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
54 dle_state = 1; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
55 return; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
56 } else if (inb == STX) { |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
57 if (!rxpkt_len) |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
58 return; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
59 in_pkt = 0; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
60 handle_rx_packet(); |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
61 rxpkt_len = 0; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
62 return; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
63 } |
174
3256dc6e84ae
rvinterf: refactored rvtdump compiles and works
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
64 data: if (rxpkt_len >= MAX_PKT_FROM_TARGET) { |
173
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
65 if (!toobig) { |
174
3256dc6e84ae
rvinterf: refactored rvtdump compiles and works
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
66 output_line("Error: Rx packet too big!"); |
173
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
67 toobig = 1; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
68 } |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
69 return; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
70 } |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 rxpkt[rxpkt_len++] = inb; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 } |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
73 |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 void |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
75 process_serial_rx() |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
76 { |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
77 u_char rdbuf[512]; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
78 int cc, i; |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
79 |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
80 cc = read(target_fd, rdbuf, sizeof rdbuf); |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
81 if (cc <= 0) { |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
82 perror("Error/EOF reading from target"); |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
83 exit(1); |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
84 } |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
85 for (i = 0; i < cc; i++) |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
86 process_inbyte(rdbuf[i]); |
f42854da4563
rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
87 } |