FreeCalypso > hg > fc-sim-sniff
annotate sw/sniff-dec/pps.c @ 56:966a54303d68
simsniff-dec: factor out high-level decoding
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Wed, 04 Oct 2023 01:18:50 +0000 |
parents | 2855330ab96f |
children |
rev | line source |
---|---|
41
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * Here we implement PPS request/response message decoding. |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <sys/types.h> |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <stdio.h> |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdlib.h> |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <string.h> |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <strings.h> |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include "state.h" |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 extern char linebuf[]; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 extern int lineno; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 extern unsigned rx_byte; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 extern int state; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 #define SUBST_PPS0 0 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 #define SUBST_PPS1 1 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 #define SUBST_PPS2 2 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 #define SUBST_PPS3 3 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 #define SUBST_PCK 4 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 static char pps_start_timestamp[18]; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 static int pps_start_line; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 static int substate; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 static u_char pps_bytes[6]; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 static unsigned byte_count; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 void |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 start_pps_msg() |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 { |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 strcpy(pps_start_timestamp, linebuf); |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 pps_start_line = lineno; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 pps_bytes[0] = rx_byte; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 byte_count = 1; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 state = STATE_PPS_MSG; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 substate = SUBST_PPS0; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 } |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 static void |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 advance_state() |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 { |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 if (substate == SUBST_PPS1) { |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 if (pps_bytes[1] & 0x10) |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 return; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 substate = SUBST_PPS2; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 } |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 if (substate == SUBST_PPS2) { |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 if (pps_bytes[1] & 0x20) |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 return; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 substate = SUBST_PPS3; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 } |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 if (substate == SUBST_PPS3) { |
54
2855330ab96f
simsniff-dec: bug in PPS decoder
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
54 if (pps_bytes[1] & 0x40) |
41
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 return; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 substate = SUBST_PCK; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 } |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 if (substate == SUBST_PCK) |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 return; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 fprintf(stderr, "BUG in PPS decoder: bad state in advance_state()\n"); |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 abort(); |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 } |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 static void |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 check_pck() |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 { |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 unsigned n, xor; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 xor = 0; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 for (n = 0; n < byte_count; n++) |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 xor ^= pps_bytes[n]; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 printf(" PCK is %s\n", xor ? "bad!" : "correct"); |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 } |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 static void |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 pps_finish() |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 { |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 unsigned n; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 printf("%s line %d: PPS", pps_start_timestamp, pps_start_line); |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 for (n = 0; n < byte_count; n++) |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 printf(" %02X", pps_bytes[n]); |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 putchar('\n'); |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 check_pck(); |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 state = STATE_READY_FOR_CMD; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 } |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 void |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 pps_byte_in() |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 { |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 pps_bytes[byte_count++] = rx_byte; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 switch (substate) { |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 case SUBST_PPS0: |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 substate = SUBST_PPS1; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 advance_state(); |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 return; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 case SUBST_PPS1: |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 substate = SUBST_PPS2; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 advance_state(); |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 return; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 case SUBST_PPS2: |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 substate = SUBST_PPS3; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 advance_state(); |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 return; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 case SUBST_PPS3: |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 substate = SUBST_PCK; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 return; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 case SUBST_PCK: |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 pps_finish(); |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
110 return; |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
111 default: |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
112 fprintf(stderr, |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
113 "BUG in PPS decoder: bad state in pps_byte_in()\n"); |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
114 abort(); |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
115 } |
118a12e9483b
simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
116 } |