altos/telegps: Create new flight if current flight is erased
[fw/altos] / src / kernel / ao_log.c
1 /*
2  * Copyright © 2009 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.h"
19 #include <ao_log.h>
20 #include <ao_config.h>
21 #if HAS_TRACKER
22 #include <ao_tracker.h>
23 #endif
24
25 __xdata uint8_t ao_log_mutex;
26 __pdata uint32_t ao_log_current_pos;
27 __pdata uint32_t ao_log_end_pos;
28 __pdata uint32_t ao_log_start_pos;
29 __xdata uint8_t ao_log_running;
30 __pdata enum ao_flight_state ao_log_state;
31 __xdata uint16_t ao_flight_number;
32
33 void
34 ao_log_flush(void)
35 {
36         ao_storage_flush();
37 }
38
39 /*
40  * When erasing a flight log, make sure the config block
41  * has an up-to-date version of the current flight number
42  */
43
44 struct ao_log_erase {
45         uint8_t mark;
46         uint16_t flight;
47 };
48
49 static __xdata struct ao_log_erase erase;
50
51 #ifndef LOG_MAX_ERASE
52 #define LOG_MAX_ERASE   16
53 #endif
54
55 #ifndef LOG_ERASE_MARK
56 #if USE_EEPROM_CONFIG
57 #error "Must define LOG_ERASE_MARK with USE_EEPROM_CONFIG"
58 #endif
59 #define LOG_ERASE_MARK  0x00
60 #endif
61
62 static uint32_t
63 ao_log_erase_pos(uint8_t i)
64 {
65         return i * sizeof (struct ao_log_erase) + AO_CONFIG_MAX_SIZE;
66 }
67
68 void
69 ao_log_write_erase(uint8_t pos)
70 {
71         erase.mark = LOG_ERASE_MARK;
72         erase.flight = ao_flight_number;
73         ao_config_write(ao_log_erase_pos(pos),  &erase, sizeof (erase));
74
75 #if USE_EEPROM_CONFIG
76         if (pos == 0) {
77                 uint8_t i;
78                 for (i = 1; i < LOG_MAX_ERASE; i++) {
79                         erase.mark = ~LOG_ERASE_MARK;
80                         erase.flight = 0;
81                         ao_config_write(ao_log_erase_pos(i), &erase, sizeof (erase));
82                 }
83         }
84 #endif
85
86         ao_config_flush();
87 }
88
89 static void
90 ao_log_read_erase(uint8_t pos)
91 {
92         ao_config_read(ao_log_erase_pos(pos), &erase, sizeof (erase));
93 }
94
95
96 static void
97 ao_log_erase_mark(void)
98 {
99         uint8_t                         i;
100
101         for (i = 0; i < LOG_MAX_ERASE; i++) {
102                 ao_log_read_erase(i);
103                 if (erase.mark == LOG_ERASE_MARK && erase.flight == ao_flight_number)
104                         return;
105                 if (erase.mark != LOG_ERASE_MARK) {
106                         ao_log_write_erase(i);
107                         return;
108                 }
109         }
110         ao_config_put();
111 }
112
113 static uint8_t
114 ao_log_slots()
115 {
116         return (uint8_t) (ao_storage_log_max / ao_config.flight_log_max);
117 }
118
119 uint32_t
120 ao_log_pos(uint8_t slot)
121 {
122         return ((slot) * ao_config.flight_log_max);
123 }
124
125 static uint16_t
126 ao_log_max_flight(void)
127 {
128         uint8_t         log_slot;
129         uint8_t         log_slots;
130         uint16_t        log_flight;
131         uint16_t        max_flight = 0;
132
133         /* Scan the log space looking for the biggest flight number */
134         log_slots = ao_log_slots();
135         for (log_slot = 0; log_slot < log_slots; log_slot++) {
136                 log_flight = ao_log_flight(log_slot);
137                 if (!log_flight)
138                         continue;
139                 if (max_flight == 0 || (int16_t) (log_flight - max_flight) > 0)
140                         max_flight = log_flight;
141         }
142         return max_flight;
143 }
144
145 void
146 ao_log_scan(void) __reentrant
147 {
148         uint8_t         log_slot;
149         uint8_t         log_slots;
150         uint8_t         log_want;
151
152         ao_config_get();
153
154         ao_flight_number = ao_log_max_flight();
155         if (ao_flight_number)
156                 if (++ao_flight_number == 0)
157                         ao_flight_number = 1;
158
159         /* Now look through the log of flight numbers from erase operations and
160          * see if the last one is bigger than what we found above
161          */
162         for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) {
163                 ao_log_read_erase(log_slot);
164                 if (erase.mark == LOG_ERASE_MARK) {
165                         if (ao_flight_number == 0 ||
166                             (int16_t) (erase.flight - ao_flight_number) > 0)
167                                 ao_flight_number = erase.flight;
168                         break;
169                 }
170         }
171         if (ao_flight_number == 0)
172                 ao_flight_number = 1;
173
174         /* With a flight number in hand, find a place to write a new log,
175          * use the target flight number to index the available log slots so
176          * that we write logs to each spot about the same number of times.
177          */
178
179         /* Find a log slot for the next flight, if available */
180         ao_log_current_pos = ao_log_end_pos = 0;
181         log_slots = ao_log_slots();
182         log_want = (ao_flight_number - 1) % log_slots;
183         log_slot = log_want;
184         do {
185                 if (ao_log_flight(log_slot) == 0) {
186                         ao_log_current_pos = ao_log_pos(log_slot);
187                         ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
188                         break;
189                 }
190                 if (++log_slot >= log_slots)
191                         log_slot = 0;
192         } while (log_slot != log_want);
193
194         ao_wakeup(&ao_flight_number);
195 }
196
197 void
198 ao_log_start(void)
199 {
200         /* start logging */
201         ao_log_running = 1;
202         ao_wakeup(&ao_log_running);
203 }
204
205 void
206 ao_log_stop(void)
207 {
208         ao_log_running = 0;
209         ao_log_flush();
210 }
211
212 uint8_t
213 ao_log_present(void)
214 {
215         return ao_log_max_flight() != 0;
216 }
217
218 uint8_t
219 ao_log_full(void)
220 {
221         return ao_log_current_pos == ao_log_end_pos;
222 }
223
224 #ifndef LOG_ADC
225 #define LOG_ADC HAS_ADC
226 #endif
227
228 #if LOG_ADC
229 static __xdata struct ao_task ao_log_task;
230 #endif
231
232 void
233 ao_log_list(void) __reentrant
234 {
235         uint8_t slot;
236         uint8_t slots;
237         uint16_t flight;
238
239         slots = ao_log_slots();
240         for (slot = 0; slot < slots; slot++)
241         {
242                 flight = ao_log_flight(slot);
243                 if (flight)
244                         printf ("flight %d start %x end %x\n",
245                                 flight,
246                                 (uint16_t) (ao_log_pos(slot) >> 8),
247                                 (uint16_t) (ao_log_pos(slot+1) >> 8));
248         }
249         printf ("done\n");
250 }
251
252 void
253 ao_log_delete(void) __reentrant
254 {
255         uint8_t slot;
256         uint8_t slots;
257         uint32_t log_current_pos, log_end_pos;
258
259         ao_cmd_decimal();
260         if (ao_cmd_status != ao_cmd_success)
261                 return;
262
263         slots = ao_log_slots();
264         /* Look for the flight log matching the requested flight */
265         if (ao_cmd_lex_i) {
266                 for (slot = 0; slot < slots; slot++) {
267                         if (ao_log_flight(slot) == ao_cmd_lex_i) {
268 #if HAS_TRACKER
269                                 ao_tracker_erase_start(ao_cmd_lex_i);
270 #endif
271                                 ao_log_erase_mark();
272                                 log_current_pos = ao_log_pos(slot);
273                                 log_end_pos = log_current_pos + ao_config.flight_log_max;
274                                 while (log_current_pos < log_end_pos) {
275                                         uint8_t i;
276                                         static __xdata uint8_t b;
277
278                                         /*
279                                          * Check to see if we've reached the end of
280                                          * the used memory to avoid re-erasing the same
281                                          * memory over and over again
282                                          */
283                                         for (i = 0; i < 16; i++) {
284                                                 if (ao_storage_read(log_current_pos + i, &b, 1))
285                                                         if (b != 0xff)
286                                                                 break;
287                                         }
288                                         if (i == 16)
289                                                 break;
290                                         ao_storage_erase(log_current_pos);
291                                         log_current_pos += ao_storage_block;
292                                 }
293 #if HAS_TRACKER
294                                 ao_tracker_erase_end();
295 #endif
296                                 puts("Erased");
297                                 return;
298                         }
299                 }
300         }
301         printf("No such flight: %d\n", ao_cmd_lex_i);
302 }
303
304 __code struct ao_cmds ao_log_cmds[] = {
305         { ao_log_list,  "l\0List logs" },
306         { ao_log_delete,        "d <flight-number>\0Delete flight" },
307         { 0,    NULL },
308 };
309
310 void
311 ao_log_init(void)
312 {
313         ao_log_running = 0;
314
315         /* For now, just log the flight starting at the begining of eeprom */
316         ao_log_state = ao_flight_invalid;
317
318         ao_cmd_register(&ao_log_cmds[0]);
319
320 #ifndef HAS_ADC
321 #error Define HAS_ADC for ao_log.c
322 #endif
323 #if LOG_ADC
324         /* Create a task to log events to eeprom */
325         ao_add_task(&ao_log_task, ao_log, "log");
326 #endif
327 }