FreeCalypso > hg > freecalypso-sw
annotate rvinterf/libg23/fmtdispatch.c @ 528:1affe428bf72
getting closer to compiling l1_cust.c
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Tue, 15 Jul 2014 16:54:52 +0000 |
parents | 28b4d3c9e85d |
children | 42c91c51ca7f |
rev | line source |
---|---|
332
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
1 /* |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
2 * This libg23 module exports the format_g23_packet() function, which |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
3 * validates the packet, then dispatches it to format_g23_trace(), |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
4 * format_g23_sysprim() or format_g23_psprim() as appropriate, or |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
5 * prints it in raw hex if malformed. |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
6 */ |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
7 |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
8 #include <sys/types.h> |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
9 #include <stdio.h> |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
10 #include <string.h> |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
11 #include <strings.h> |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 static int |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 basic_checks(rxpkt, rxpkt_len) |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
15 u_char *rxpkt; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 { |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 int i, c; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 if (rxpkt_len < 17) |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 return(0); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
21 /* check version bits in the header byte */ |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
22 if ((rxpkt[1] & 0xC0) != 0x80) |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
23 return(0); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
24 /* check the length */ |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
25 c = rxpkt[2] | rxpkt[3] << 8; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
26 if (c + 4 != rxpkt_len) |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
27 return(0); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
28 /* ensure that the "from" and "to" are printable ASCII */ |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
29 for (i = 8; i < 16; i++) { |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
30 c = rxpkt[i]; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
31 if (c < ' ' || c > '~') |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
32 return(0); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
33 } |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
34 /* basic checks pass */ |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
35 return(1); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
36 } |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
37 |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
38 static int |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
39 psprim_extra_checks(rxpkt, rxpkt_len) |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
40 u_char *rxpkt; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
41 { |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
42 int i, c; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
43 |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
44 if (rxpkt_len < 24) |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
45 return(0); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
46 /* "original rcvr" field needs to be printable ASCII */ |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
47 for (i = 16; i < 20; i++) { |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
48 c = rxpkt[i]; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
49 if (c < ' ' || c > '~') |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
50 return(0); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
51 } |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
52 /* checks pass */ |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
53 return(1); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
54 } |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
55 |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
56 static void |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
57 print_malformed(rxpkt, rxpkt_len, outbuf) |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
58 u_char *rxpkt; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
59 char *outbuf; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
60 { |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
61 int i; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
62 char *dp; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
63 |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
64 dp = outbuf; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
65 strcpy(dp, "G23 UNK:"); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
66 dp += 8; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
67 for (i = 1; i < rxpkt_len; i++) { |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
68 sprintf(dp, " %02X", rxpkt[i]); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
69 dp += 3; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
70 } |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 *dp = '\0'; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 } |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
73 |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 void |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
75 format_g23_packet(rxpkt, rxpkt_len, outbuf) |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
76 u_char *rxpkt; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
77 char *outbuf; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
78 { |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
79 if (!basic_checks(rxpkt, rxpkt_len)) { |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
80 print_malformed(rxpkt, rxpkt_len, outbuf); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
81 return; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
82 } |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
83 /* dispatch by type */ |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
84 switch (rxpkt[1] & 0x30) { |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
85 case 0x10: |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
86 /* PS primitive */ |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
87 if (psprim_extra_checks(rxpkt, rxpkt_len)) |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
88 format_g23_psprim(rxpkt, rxpkt_len, outbuf); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
89 else |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
90 print_malformed(rxpkt, rxpkt_len, outbuf); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
91 return; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
92 case 0x20: |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
93 /* trace */ |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
94 format_g23_trace(rxpkt, rxpkt_len, outbuf); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
95 return; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
96 case 0x30: |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
97 /* system primitive */ |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
98 format_g23_sysprim(rxpkt, rxpkt_len, outbuf); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
99 return; |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
100 default: |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
101 print_malformed(rxpkt, rxpkt_len, outbuf); |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
102 } |
28b4d3c9e85d
rvinterf/libg23: complete for now
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
103 } |