altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / src / test / ao_eeprom_read_old.c
1 /*
2  * Copyright © 2017 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao_eeprom_read.h"
19 #include <string.h>
20 #include <stdlib.h>
21 #include <errno.h>
22
23 static int
24 ao_eeprom_add_u8(struct ao_eeprom *ao_eeprom, uint8_t byte)
25 {
26         if (ao_eeprom->len == ao_eeprom->size) {
27                 uint32_t        nsize = ao_eeprom->size * 2;
28                 uint8_t         *ndata = realloc(ao_eeprom->data, nsize);
29                 if (!ndata)
30                         return 0;
31                 ao_eeprom->data = ndata;
32                 ao_eeprom->size = nsize;
33         }
34         ao_eeprom->data[ao_eeprom->len++] = byte;
35         return 1;
36 }
37
38 static int
39 ao_eeprom_add_u16(struct ao_eeprom *ao_eeprom, uint16_t u16)
40 {
41         if (!ao_eeprom_add_u8(ao_eeprom, u16 & 0xff))
42                 return 0;
43
44         return ao_eeprom_add_u8(ao_eeprom, u16 >> 8);
45 }
46
47 struct ao_eeprom *
48 ao_eeprom_read_old(FILE *file)
49 {
50         struct ao_eeprom        *ao_eeprom;
51         char                    line[1024];
52         char                    *l;
53
54         ao_eeprom = calloc(1, sizeof (struct ao_eeprom));
55         if (!ao_eeprom)
56                 return NULL;
57
58         ao_eeprom->log_format = -1;
59         ao_eeprom->size = 64;
60         ao_eeprom->data = malloc(ao_eeprom->size);
61         if (!ao_eeprom->data) {
62                 free(ao_eeprom);
63                 return NULL;
64         }
65         while (fgets(line, sizeof(line), file) != NULL) {
66                 int nword;
67                 char *words[64];
68                 char *saveptr;
69                 l = line;
70                 for (nword = 0; nword < 64; nword++) {
71                         words[nword] = strtok_r(l, " \t\n", &saveptr);
72                         l = NULL;
73                         if (words[nword] == NULL)
74                                 break;
75                 }
76                 if (((ao_eeprom->log_format == AO_LOG_FORMAT_TELEMEGA_OLD || ao_eeprom->log_format == AO_LOG_FORMAT_TELEMEGA) && nword == 30 && strlen(words[0]) == 1) ||
77                     ((ao_eeprom->log_format == AO_LOG_FORMAT_EASYMINI1 || ao_eeprom->log_format == AO_LOG_FORMAT_EASYMINI2) && nword == 14 && strlen(words[0]) == 1) ||
78                     (ao_eeprom->log_format == AO_LOG_FORMAT_TELEMETRUM && nword == 14 && strlen(words[0]) == 1))
79                 {
80                         int             i;
81                         uint8_t         type;
82                         uint16_t        tick;
83
84                         type = words[0][0];
85                         tick = strtoul(words[1], NULL, 16);
86                         ao_eeprom_add_u8(ao_eeprom, type);
87                         ao_eeprom_add_u8(ao_eeprom, 0); /* checksum */
88                         ao_eeprom_add_u16(ao_eeprom, tick);
89                         for (i = 2; i < nword; i++)
90                                 ao_eeprom_add_u8(ao_eeprom, strtoul(words[i], NULL, 16));
91                 }
92                 else if (nword == 4 && strlen(words[0]) == 1) {
93                         uint8_t         type;
94                         uint16_t        tick, a, b;
95                         type = words[0][0];
96                         tick = strtoul(words[1], NULL, 16);
97                         a = strtoul(words[2], NULL, 16);
98                         b = strtoul(words[3], NULL, 16);
99                         if (type == 'P')
100                                 type = 'A';
101                         ao_eeprom_add_u8(ao_eeprom, type);
102                         ao_eeprom_add_u8(ao_eeprom, 0); /* checksum */
103                         ao_eeprom_add_u16(ao_eeprom, tick);
104                         ao_eeprom_add_u16(ao_eeprom, a);
105                         ao_eeprom_add_u16(ao_eeprom, b);
106                 }
107                 else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
108                         if (strcmp(words[1], "reserved:") == 0)
109                                 ao_eeprom->ms5607_prom.reserved = strtoul(words[2], NULL, 10);
110                         else if (strcmp(words[1], "sens:") == 0)
111                                 ao_eeprom->ms5607_prom.sens = strtoul(words[2], NULL, 10);
112                         else if (strcmp(words[1], "off:") == 0)
113                                 ao_eeprom->ms5607_prom.off = strtoul(words[2], NULL, 10);
114                         else if (strcmp(words[1], "tcs:") == 0)
115                                 ao_eeprom->ms5607_prom.tcs = strtoul(words[2], NULL, 10);
116                         else if (strcmp(words[1], "tco:") == 0)
117                                 ao_eeprom->ms5607_prom.tco = strtoul(words[2], NULL, 10);
118                         else if (strcmp(words[1], "tref:") == 0)
119                                 ao_eeprom->ms5607_prom.tref = strtoul(words[2], NULL, 10);
120                         else if (strcmp(words[1], "tempsens:") == 0)
121                                 ao_eeprom->ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
122                         else if (strcmp(words[1], "crc:") == 0)
123                                 ao_eeprom->ms5607_prom.crc = strtoul(words[2], NULL, 10);
124                         continue;
125                 }
126 #if AO_NUM_PYRO
127                 else if (nword >= 3 && strcmp(words[0], "Pyro") == 0) {
128                         int     p = strtoul(words[1], NULL, 10);
129                         int     i, j;
130                         struct ao_pyro  *pyro = &ao_eeprom->config.pyro[p];
131
132                         for (i = 2; i < nword; i++) {
133                                 for (j = 0; j < NUM_PYRO_VALUES; j++)
134                                         if (!strcmp (words[i], ao_pyro_values[j].name))
135                                                 break;
136                                 if (j == NUM_PYRO_VALUES)
137                                         continue;
138                                 pyro->flags |= ao_pyro_values[j].flag;
139                                 if (ao_pyro_values[j].offset != NO_VALUE && i + 1 < nword) {
140                                         int16_t val = strtoul(words[++i], NULL, 10);
141                                         printf("pyro %d condition %s value %d\n", p, words[i-1], val);
142                                         *((int16_t *) ((char *) pyro + ao_pyro_values[j].offset)) = val;
143                                 }
144                         }
145                 }
146 #endif
147                 else if (nword == 2 && strcmp(words[0], "log-format") == 0) {
148                         ao_eeprom->log_format = strtoul(words[1], NULL, 10);
149                 } else if (nword == 2 && strcmp(words[0], "serial-number") == 0) {
150                         ao_eeprom->serial_number = strtoul(words[1], NULL, 10);
151                 } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
152                         ao_eeprom->config.accel_plus_g = atoi(words[3]);
153                         ao_eeprom->config.accel_minus_g = atoi(words[5]);
154 #if HAS_GYRO
155                 } else if (nword >= 8 && strcmp(words[0], "IMU") == 0) {
156                         ao_eeprom->config.accel_zero_along = atoi(words[3]);
157                         ao_eeprom->config.accel_zero_across = atoi(words[5]);
158                         ao_eeprom->config.accel_zero_through = atoi(words[7]);
159 #endif
160                 } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
161                         ao_eeprom->config.main_deploy = atoi(words[2]);
162                 } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
163                            strcmp(words[1], "lockout:") == 0) {
164                         ao_eeprom->config.apogee_lockout = atoi(words[2]);
165                 } else if (nword >= 3 && strcmp(words[0], "Pad") == 0 &&
166                            strcmp(words[1], "orientation:") == 0) {
167                         ao_eeprom->config.pad_orientation = atoi(words[2]);
168                 }
169         }
170         return ao_eeprom;
171 }
172