2 * Copyright © 2017 Keith Packard <keithp@keithp.com>
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, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
15 #include "ao_eeprom_read.h"
16 #include <json-c/json.h>
20 static struct json_object *
21 ao_eeprom_read_config(FILE *file)
24 struct json_tokener *tok;
25 struct json_object *obj = NULL;
26 enum json_tokener_error err;
28 tok = json_tokener_new();
33 if (fgets(line, sizeof(line), file) == NULL)
35 obj = json_tokener_parse_ex(tok, line, strlen(line));
36 err = json_tokener_get_error(tok);
37 if (err == json_tokener_success)
39 if (err != json_tokener_continue)
42 json_tokener_free(tok);
45 json_tokener_free(tok);
51 ao_eeprom_read_byte(FILE *file)
54 if (fscanf(file, "%x", &byte) != 1)
60 ao_eeprom_read_data(FILE *file, struct ao_eeprom *eeprom)
62 uint8_t *data = NULL, *ndata;
67 data = malloc(size = 64);
70 while ((byte = ao_eeprom_read_byte(file)) != EOF) {
72 ndata = realloc(data, size *= 2);
77 data[len++] = (uint8_t) byte;
89 ao_json_get_int(struct json_object *obj, const char *key, int def)
91 struct json_object *value;
94 if (!json_object_object_get_ex(obj, key, &value))
97 i = (int) json_object_get_int(value);
104 ao_json_get_string(struct json_object *obj, const char *key, const char *def)
106 struct json_object *value;
109 if (!json_object_object_get_ex(obj, key, &value))
112 str = json_object_get_string(value);
122 ao_eeprom_get_pyro(struct ao_config *config, struct json_object *obj)
124 struct json_object *pyros;
125 struct json_object *pyro;
128 if (!json_object_object_get_ex(obj, "pyros", &pyros))
131 if (json_object_get_type(pyros) != json_type_array)
134 for (i = 0; i < json_object_array_length(pyros); i++) {
135 pyro = json_object_array_get_idx(pyros, i);
137 p = ao_json_get_int(pyro, "channel", -1);
138 if (0 <= p && p < AO_PYRO_NUM) {
139 config->pyro[p].flags = ao_json_get_int(pyro, "flags", 0);
140 config->pyro[p].accel_less = ao_json_get_int(pyro, "accel_less", 0);
141 config->pyro[p].accel_greater = ao_json_get_int(pyro, "accel_greater", 0);
142 config->pyro[p].speed_less = ao_json_get_int(pyro, "speed_less", 0);
143 config->pyro[p].speed_greater = ao_json_get_int(pyro, "speed_greater", 0);
144 config->pyro[p].height_less = ao_json_get_int(pyro, "height_less", 0);
145 config->pyro[p].height_greater = ao_json_get_int(pyro, "height_greater", 0);
146 config->pyro[p].orient_less = ao_json_get_int(pyro, "orient_less", 0);
147 config->pyro[p].orient_greater = ao_json_get_int(pyro, "orient_greater", 0);
148 config->pyro[p].time_less = ao_json_get_int(pyro, "time_less", 0);
149 config->pyro[p].time_greater = ao_json_get_int(pyro, "time_greater", 0);
150 config->pyro[p].delay = ao_json_get_int(pyro, "delay", 0);
151 config->pyro[p].state_less = ao_json_get_int(pyro, "state_less", 0);
152 config->pyro[p].state_greater_or_equal = ao_json_get_int(pyro, "state_greater_or_equal", 0);
153 config->pyro[p].motor = ao_json_get_int(pyro, "motor", 0);
162 ao_eeprom_get_ms5607(struct ao_ms5607_prom *ms5607_prom, struct json_object *obj)
164 struct json_object *ms5607;
166 if (!json_object_object_get_ex(obj, "ms5607", &ms5607))
169 if (json_object_get_type(ms5607) != json_type_object)
172 ms5607_prom->reserved = ao_json_get_int(ms5607, "reserved", 0);
173 ms5607_prom->sens = ao_json_get_int(ms5607, "sens", 0);
174 ms5607_prom->off = ao_json_get_int(ms5607, "off", 0);
175 ms5607_prom->tcs = ao_json_get_int(ms5607, "tcs", 0);
176 ms5607_prom->tco = ao_json_get_int(ms5607, "tco", 0);
177 ms5607_prom->tref = ao_json_get_int(ms5607, "tref", 0);
178 ms5607_prom->tempsens = ao_json_get_int(ms5607, "tempsens", 0);
179 ms5607_prom->crc = ao_json_get_int(ms5607, "crc", 0);
184 ao_eeprom_get_config(struct ao_eeprom *ao_eeprom, struct json_object *obj)
186 struct ao_config *config = &ao_eeprom->config;
189 if (json_object_get_type(obj) != json_type_object)
192 ao_eeprom->log_format = ao_json_get_int(obj, "log_format", 0);
193 ao_eeprom->serial_number = ao_json_get_int(obj, "serial", 0);
195 config->major = ao_json_get_int(obj, "config_major", 0);
196 config->minor = ao_json_get_int(obj, "config_minor", 0);
197 if (config->major == 0 || config->minor == 0)
200 config->main_deploy = ao_json_get_int(obj, "main_deploy", 250);
201 config->accel_plus_g = ao_json_get_int(obj, "accel_cal_plus", 0);
203 s = ao_json_get_string(obj, "callsign", "N0CALL");
204 strncpy(config->callsign, s, sizeof(config->callsign));
206 config->apogee_delay = ao_json_get_int(obj, "apogee_delay", 0);
207 config->accel_minus_g = ao_json_get_int(obj, "accel_cal_minus", 0);
208 config->radio_cal = ao_json_get_int(obj, "radio_calibration", 0);
209 config->flight_log_max = ao_json_get_int(obj, "flight_log_max", 0);
210 config->ignite_mode = ao_json_get_int(obj, "ignite_mode", 0);
211 config->pad_orientation = ao_json_get_int(obj, "pad_orientation", 0);
212 config->radio_setting = ao_json_get_int(obj, "radio_setting", 0);
213 config->radio_enable = ao_json_get_int(obj, "radio_enable", 1);
214 config->frequency = ao_json_get_int(obj, "frequency", 434550);
215 config->apogee_lockout = ao_json_get_int(obj, "apogee_lockout", 0);
217 if (!ao_eeprom_get_pyro(config, obj))
220 config->ignite_mode = ao_json_get_int(obj, "ignite_mode", 0);
221 config->ignite_mode = ao_json_get_int(obj, "ignite_mode", 0);
222 config->ignite_mode = ao_json_get_int(obj, "ignite_mode", 0);
223 config->ignite_mode = ao_json_get_int(obj, "ignite_mode", 0);
224 config->ignite_mode = ao_json_get_int(obj, "ignite_mode", 0);
225 config->ignite_mode = ao_json_get_int(obj, "ignite_mode", 0);
227 if (!ao_eeprom_get_ms5607(&ao_eeprom->ms5607_prom, obj))
234 ao_eeprom_read(FILE *file)
236 struct ao_eeprom *ao_eeprom;
237 struct json_object *obj;
240 ao_eeprom = calloc(1, sizeof (struct ao_eeprom));
244 obj = ao_eeprom_read_config(file);
248 ret = ao_eeprom_get_config(ao_eeprom, obj);
249 json_object_put(obj);
253 if (!ao_eeprom_read_data(file, ao_eeprom))