annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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>
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include "pktmux.h"
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 extern int target_fd;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 #define MAXPKT 512
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14 u_char rxpkt[MAXPKT];
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 extern char pr_item[];
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 int in_pkt, dle_state, toobig;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21 static void
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 process_inbyte(inb)
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 {
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) {
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 sprintf(pr_item,
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" : "");
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 print_item();
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) {
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 sprintf(pr_item,
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 "Rx framing error: %02X after DLE\n", inb);
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 print_item();
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 }
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 data: if (rxpkt_len >= MAXPKT) {
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 if (!toobig) {
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66 sprintf(pr_item, "Error: Rx packet too big!\n");
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 print_item();
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 toobig = 1;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 }
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 return;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 }
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 rxpkt[rxpkt_len++] = inb;
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
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75 void
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
76 process_serial_rx()
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
77 {
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
78 u_char rdbuf[512];
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
79 int cc, i;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
80
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
81 cc = read(target_fd, rdbuf, sizeof rdbuf);
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
82 if (cc <= 0) {
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
83 perror("Error/EOF reading from target");
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
84 exit(1);
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
85 }
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
86 for (i = 0; i < cc; i++)
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
87 process_inbyte(rdbuf[i]);
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
88 }