a stab at turning on rudimentary logging for telefiretwo
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20 #include <ao_log.h>
21 #include <ao_config.h>
22 #if HAS_TRACKER
23 #include <ao_tracker.h>
24 #endif
25
26 __xdata uint8_t ao_log_mutex;
27 __pdata uint32_t ao_log_current_pos;
28 __pdata uint32_t ao_log_end_pos;
29 __pdata uint32_t ao_log_start_pos;
30 __xdata uint8_t ao_log_running;
31 __pdata enum ao_flight_state ao_log_state;
32 __xdata uint16_t ao_flight_number;
33
34 void
35 ao_log_flush(void)
36 {
37         ao_storage_flush();
38 }
39
40 /*
41  * When erasing a flight log, make sure the config block
42  * has an up-to-date version of the current flight number
43  */
44
45 struct ao_log_erase {
46         uint8_t mark;
47         uint16_t flight;
48 };
49
50 static __xdata struct ao_log_erase erase;
51
52 #ifndef LOG_MAX_ERASE
53 #define LOG_MAX_ERASE   16
54 #endif
55
56 #ifndef LOG_ERASE_MARK
57 #if USE_EEPROM_CONFIG
58 #error "Must define LOG_ERASE_MARK with USE_EEPROM_CONFIG"
59 #endif
60 #define LOG_ERASE_MARK  0x00
61 #endif
62
63 static uint32_t
64 ao_log_erase_pos(uint8_t i)
65 {
66         return i * sizeof (struct ao_log_erase) + AO_CONFIG_MAX_SIZE;
67 }
68
69 void
70 ao_log_write_erase(uint8_t pos)
71 {
72         erase.mark = LOG_ERASE_MARK;
73         erase.flight = ao_flight_number;
74         ao_config_write(ao_log_erase_pos(pos),  &erase, sizeof (erase));
75
76 #if USE_EEPROM_CONFIG
77         if (pos == 0) {
78                 uint8_t i;
79                 for (i = 1; i < LOG_MAX_ERASE; i++) {
80                         erase.mark = ~LOG_ERASE_MARK;
81                         erase.flight = 0;
82                         ao_config_write(ao_log_erase_pos(i), &erase, sizeof (erase));
83                 }
84         }
85 #endif
86
87         ao_config_flush();
88 }
89
90 static void
91 ao_log_read_erase(uint8_t pos)
92 {
93         ao_config_read(ao_log_erase_pos(pos), &erase, sizeof (erase));
94 }
95
96
97 static void
98 ao_log_erase_mark(void)
99 {
100         uint8_t                         i;
101
102         for (i = 0; i < LOG_MAX_ERASE; i++) {
103                 ao_log_read_erase(i);
104                 if (erase.mark == LOG_ERASE_MARK && erase.flight == ao_flight_number)
105                         return;
106                 if (erase.mark != LOG_ERASE_MARK) {
107                         ao_log_write_erase(i);
108                         return;
109                 }
110         }
111         ao_config_put();
112 }
113
114 static uint8_t
115 ao_log_slots()
116 {
117         return (uint8_t) (ao_storage_log_max / ao_config.flight_log_max);
118 }
119
120 uint32_t
121 ao_log_pos(uint8_t slot)
122 {
123         return ((slot) * ao_config.flight_log_max);
124 }
125
126 static uint16_t
127 ao_log_max_flight(void)
128 {
129         uint8_t         log_slot;
130         uint8_t         log_slots;
131         uint16_t        log_flight;
132         uint16_t        max_flight = 0;
133
134         /* Scan the log space looking for the biggest flight number */
135         log_slots = ao_log_slots();
136         for (log_slot = 0; log_slot < log_slots; log_slot++) {
137                 log_flight = ao_log_flight(log_slot);
138                 if (!log_flight)
139                         continue;
140                 if (max_flight == 0 || (int16_t) (log_flight - max_flight) > 0)
141                         max_flight = log_flight;
142         }
143         return max_flight;
144 }
145
146 static void
147 ao_log_erase(uint8_t slot) __reentrant
148 {
149         uint32_t log_current_pos, log_end_pos;
150
151         ao_log_erase_mark();
152         log_current_pos = ao_log_pos(slot);
153         log_end_pos = log_current_pos + ao_config.flight_log_max;
154         while (log_current_pos < log_end_pos) {
155                 uint8_t i;
156                 static __xdata uint8_t b;
157
158                 /*
159                  * Check to see if we've reached the end of
160                  * the used memory to avoid re-erasing the same
161                  * memory over and over again
162                  */
163                 for (i = 0; i < 16; i++) {
164                         if (ao_storage_read(log_current_pos + i, &b, 1))
165                                 if (b != 0xff)
166                                         break;
167                 }
168                 if (i == 16)
169                         break;
170                 ao_storage_erase(log_current_pos);
171                 log_current_pos += ao_storage_block;
172         }
173 }
174
175 static void
176 ao_log_find_max_erase_flight(void) __reentrant
177 {
178         uint8_t log_slot;
179
180         /* Now look through the log of flight numbers from erase operations and
181          * see if the last one is bigger than what we found above
182          */
183         for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) {
184                 ao_log_read_erase(log_slot);
185                 if (erase.mark == LOG_ERASE_MARK) {
186                         if (ao_flight_number == 0 ||
187                             (int16_t) (erase.flight - ao_flight_number) > 0)
188                                 ao_flight_number = erase.flight;
189                         break;
190                 }
191         }
192         if (ao_flight_number == 0)
193                 ao_flight_number = 1;
194 }
195
196 uint8_t
197 ao_log_scan(void) __reentrant
198 {
199         uint8_t         log_slot;
200         uint8_t         log_slots;
201 #if FLIGHT_LOG_APPEND
202         uint8_t         ret;
203 #else
204         uint8_t         log_want;
205 #endif
206
207         ao_config_get();
208
209         /* Get any existing flight number */
210         ao_flight_number = ao_log_max_flight();
211
212 #if FLIGHT_LOG_APPEND
213
214         /* Deal with older OS versions which stored multiple
215          * flights in rom by erasing everything after the first
216          * slot
217          */
218         if (ao_config.flight_log_max != ao_storage_log_max) {
219                 log_slots = ao_log_slots();
220                 for (log_slot = 1; log_slot < log_slots; log_slot++) {
221                         if (ao_log_flight(log_slot) != 0)
222                                 ao_log_erase(log_slot);
223                 }
224                 ao_config_log_fix_append();
225         }
226         ao_log_current_pos = ao_log_pos(0);
227         ao_log_end_pos = ao_log_current_pos + ao_storage_log_max;
228
229         if (ao_flight_number) {
230                 uint32_t        full = ao_log_current_pos;
231                 uint32_t        empty = ao_log_end_pos - ao_log_size;
232
233                 /* If there's already a flight started, then find the
234                  * end of it
235                  */
236                 for (;;) {
237                         ao_log_current_pos = (full + empty) >> 1;
238                         ao_log_current_pos -= ao_log_current_pos % ao_log_size;
239
240                         if (ao_log_current_pos == full) {
241                                 if (ao_log_check(ao_log_current_pos))
242                                         ao_log_current_pos += ao_log_size;
243                                 break;
244                         }
245                         if (ao_log_current_pos == empty)
246                                 break;
247
248                         if (ao_log_check(ao_log_current_pos)) {
249                                 full = ao_log_current_pos;
250                         } else {
251                                 empty = ao_log_current_pos;
252                         }
253                 }
254                 ret = 1;
255         } else {
256                 ao_log_find_max_erase_flight();
257                 ret = 0;
258         }
259         ao_wakeup(&ao_flight_number);
260         return ret;
261 #else
262
263         if (ao_flight_number)
264                 if (++ao_flight_number == 0)
265                         ao_flight_number = 1;
266
267         ao_log_find_max_erase_flight();
268
269         /* With a flight number in hand, find a place to write a new log,
270          * use the target flight number to index the available log slots so
271          * that we write logs to each spot about the same number of times.
272          */
273
274         /* Find a log slot for the next flight, if available */
275         ao_log_current_pos = ao_log_end_pos = 0;
276         log_slots = ao_log_slots();
277         log_want = (ao_flight_number - 1) % log_slots;
278         log_slot = log_want;
279         do {
280                 if (ao_log_flight(log_slot) == 0) {
281                         ao_log_current_pos = ao_log_pos(log_slot);
282                         ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
283                         break;
284                 }
285                 if (++log_slot >= log_slots)
286                         log_slot = 0;
287         } while (log_slot != log_want);
288         ao_wakeup(&ao_flight_number);
289         return 0;
290 #endif
291 }
292
293 void
294 ao_log_start(void)
295 {
296         /* start logging */
297         ao_log_running = 1;
298         ao_wakeup(&ao_log_running);
299 }
300
301 void
302 ao_log_stop(void)
303 {
304         ao_log_running = 0;
305         ao_log_flush();
306 }
307
308 uint8_t
309 ao_log_present(void)
310 {
311         return ao_log_max_flight() != 0;
312 }
313
314 uint8_t
315 ao_log_full(void)
316 {
317         return ao_log_current_pos == ao_log_end_pos;
318 }
319
320 #ifndef LOG_ADC
321 #define LOG_ADC HAS_ADC
322 #endif
323
324 #if LOG_ADC
325 static __xdata struct ao_task ao_log_task;
326 #endif
327
328 void
329 ao_log_list(void) __reentrant
330 {
331         uint8_t slot;
332         uint8_t slots;
333         uint16_t flight;
334
335         slots = ao_log_slots();
336         for (slot = 0; slot < slots; slot++)
337         {
338                 flight = ao_log_flight(slot);
339                 if (flight)
340                         printf ("flight %d start %x end %x\n",
341                                 flight,
342                                 (uint16_t) (ao_log_pos(slot) >> 8),
343                                 (uint16_t) (ao_log_pos(slot+1) >> 8));
344         }
345         printf ("done\n");
346 }
347
348 void
349 ao_log_delete(void) __reentrant
350 {
351         uint8_t slot;
352         uint8_t slots;
353
354         ao_cmd_decimal();
355         if (ao_cmd_status != ao_cmd_success)
356                 return;
357
358         slots = ao_log_slots();
359         /* Look for the flight log matching the requested flight */
360         if (ao_cmd_lex_i) {
361                 for (slot = 0; slot < slots; slot++) {
362                         if (ao_log_flight(slot) == ao_cmd_lex_i) {
363 #if HAS_TRACKER
364                                 ao_tracker_erase_start(ao_cmd_lex_i);
365 #endif
366                                 ao_log_erase(slot);
367 #if HAS_TRACKER
368                                 ao_tracker_erase_end();
369 #endif
370                                 puts("Erased");
371                                 return;
372                         }
373                 }
374         }
375         printf("No such flight: %d\n", ao_cmd_lex_i);
376 }
377
378 __code struct ao_cmds ao_log_cmds[] = {
379         { ao_log_list,  "l\0List logs" },
380         { ao_log_delete,        "d <flight-number>\0Delete flight" },
381         { 0,    NULL },
382 };
383
384 void
385 ao_log_init(void)
386 {
387         ao_log_running = 0;
388
389         /* For now, just log the flight starting at the begining of eeprom */
390         ao_log_state = ao_flight_invalid;
391
392         ao_cmd_register(&ao_log_cmds[0]);
393
394 #ifndef HAS_ADC
395 #error Define HAS_ADC for ao_log.c
396 #endif
397 #if LOG_ADC
398         /* Create a task to log events to eeprom */
399         ao_add_task(&ao_log_task, ao_log, "log");
400 #endif
401 }