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