5fb086cee816712d4347ceb99c37fa37a794ec62
[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 int16_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 #ifndef AO_LOG_UNCOMMON
115 /*
116  * Common logging functions which depend on the type of the log data
117  * structure.
118  */
119
120 __xdata ao_log_type ao_log_data;
121
122 static uint8_t
123 ao_log_csum(__xdata uint8_t *b) __reentrant
124 {
125         uint8_t sum = 0x5a;
126         uint8_t i;
127
128         for (i = 0; i < sizeof (ao_log_type); i++)
129                 sum += *b++;
130         return -sum;
131 }
132
133 uint8_t
134 ao_log_write(__xdata ao_log_type *log) __reentrant
135 {
136         uint8_t wrote = 0;
137         /* set checksum */
138         log->csum = 0;
139         log->csum = ao_log_csum((__xdata uint8_t *) log);
140         ao_mutex_get(&ao_log_mutex); {
141                 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
142                         ao_log_stop();
143                 if (ao_log_running) {
144                         wrote = 1;
145                         ao_storage_write(ao_log_current_pos,
146                                          log,
147                                          sizeof (ao_log_type));
148                         ao_log_current_pos += sizeof (ao_log_type);
149                 }
150         } ao_mutex_put(&ao_log_mutex);
151         return wrote;
152 }
153
154 uint8_t
155 ao_log_check_data(void)
156 {
157         if (ao_log_csum((uint8_t *) &ao_log_data) != 0)
158                 return 0;
159         return 1;
160 }
161
162 uint8_t
163 ao_log_check_clear(void)
164 {
165         uint8_t *b = (uint8_t *) &ao_log_data;
166         uint8_t i;
167
168         for (i = 0; i < sizeof (ao_log_type); i++) {
169                 if (*b++ != 0xff)
170                         return 0;
171         }
172         return 1;
173 }
174
175 int16_t
176 ao_log_flight(uint8_t slot)
177 {
178         if (!ao_storage_read(ao_log_pos(slot),
179                              &ao_log_data,
180                              sizeof (ao_log_type)))
181                 return -(int16_t) (slot + 1);
182
183         if (ao_log_check_clear())
184                 return 0;
185
186         if (!ao_log_check_data() || ao_log_data.type != AO_LOG_FLIGHT)
187                 return -(int16_t) (slot + 1);
188
189         return ao_log_data.u.flight.flight;
190 }
191 #endif
192
193 static uint8_t
194 ao_log_slots()
195 {
196         return (uint8_t) (ao_storage_log_max / ao_config.flight_log_max);
197 }
198
199 uint32_t
200 ao_log_pos(uint8_t slot)
201 {
202         return ((slot) * ao_config.flight_log_max);
203 }
204
205 static int16_t
206 ao_log_max_flight(void)
207 {
208         uint8_t         log_slot;
209         uint8_t         log_slots;
210         int16_t         log_flight;
211         int16_t         max_flight = 0;
212
213         /* Scan the log space looking for the biggest flight number */
214         log_slots = ao_log_slots();
215         for (log_slot = 0; log_slot < log_slots; log_slot++) {
216                 log_flight = ao_log_flight(log_slot);
217                 if (log_flight <= 0)
218                         continue;
219                 if (max_flight == 0 || log_flight > max_flight)
220                         max_flight = log_flight;
221         }
222         return max_flight;
223 }
224
225 static void
226 ao_log_erase(uint8_t slot) __reentrant
227 {
228         uint32_t log_current_pos, log_end_pos;
229
230         ao_log_erase_mark();
231         log_current_pos = ao_log_pos(slot);
232         log_end_pos = log_current_pos + ao_config.flight_log_max;
233         while (log_current_pos < log_end_pos) {
234                 uint8_t i;
235                 static __xdata uint8_t b;
236
237                 /*
238                  * Check to see if we've reached the end of
239                  * the used memory to avoid re-erasing the same
240                  * memory over and over again
241                  */
242                 for (i = 0; i < 16; i++) {
243                         if (ao_storage_read(log_current_pos + i, &b, 1))
244                                 if (b != 0xff)
245                                         break;
246                 }
247                 if (i == 16)
248                         break;
249                 ao_storage_erase(log_current_pos);
250                 log_current_pos += ao_storage_block;
251         }
252 }
253
254 static void
255 ao_log_find_max_erase_flight(void) __reentrant
256 {
257         uint8_t log_slot;
258
259         /* Now look through the log of flight numbers from erase operations and
260          * see if the last one is bigger than what we found above
261          */
262         for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) {
263                 ao_log_read_erase(log_slot);
264                 if (erase.mark == LOG_ERASE_MARK) {
265                         if (ao_flight_number == 0 ||
266                             (int16_t) (erase.flight - ao_flight_number) > 0)
267                                 ao_flight_number = erase.flight;
268                         break;
269                 }
270         }
271         if (ao_flight_number == 0)
272                 ao_flight_number = 1;
273 }
274
275 uint8_t
276 ao_log_scan(void) __reentrant
277 {
278         uint8_t         log_slot;
279         uint8_t         log_slots;
280 #if FLIGHT_LOG_APPEND
281         uint8_t         ret;
282 #else
283         uint8_t         log_want;
284 #endif
285
286         ao_config_get();
287
288         /* Get any existing flight number */
289         ao_flight_number = ao_log_max_flight();
290
291 #if FLIGHT_LOG_APPEND
292
293         /* Deal with older OS versions which stored multiple
294          * flights in rom by erasing everything after the first
295          * slot
296          */
297         if (ao_config.flight_log_max != ao_storage_log_max) {
298                 log_slots = ao_log_slots();
299                 for (log_slot = 1; log_slot < log_slots; log_slot++) {
300                         if (ao_log_flight(log_slot) != 0)
301                                 ao_log_erase(log_slot);
302                 }
303                 ao_config_log_fix_append();
304         }
305         ao_log_current_pos = ao_log_pos(0);
306         ao_log_end_pos = ao_log_current_pos + ao_storage_log_max;
307
308         if (ao_flight_number) {
309                 uint32_t        full = ao_log_current_pos;
310                 uint32_t        empty = ao_log_end_pos - AO_LOG_SIZE;
311
312                 /* If there's already a flight started, then find the
313                  * end of it
314                  */
315                 for (;;) {
316                         ao_log_current_pos = (full + empty) >> 1;
317                         ao_log_current_pos -= ao_log_current_pos % AO_LOG_SIZE;
318
319                         if (ao_log_current_pos == full) {
320                                 if (ao_log_check(ao_log_current_pos) != AO_LOG_EMPTY)
321                                         ao_log_current_pos += AO_LOG_SIZE;
322                                 break;
323                         }
324                         if (ao_log_current_pos == empty)
325                                 break;
326
327                         if (ao_log_check(ao_log_current_pos) != AO_LOG_EMPTY) {
328                                 full = ao_log_current_pos;
329                         } else {
330                                 empty = ao_log_current_pos;
331                         }
332                 }
333                 ret = 1;
334         } else {
335                 ao_log_find_max_erase_flight();
336                 ret = 0;
337         }
338         ao_wakeup(&ao_flight_number);
339         return ret;
340 #else
341         if (ao_flight_number) {
342                 ++ao_flight_number;
343                 if (ao_flight_number <= 0)
344                         ao_flight_number = 1;
345         }
346
347         ao_log_find_max_erase_flight();
348
349         /* With a flight number in hand, find a place to write a new log,
350          * use the target flight number to index the available log slots so
351          * that we write logs to each spot about the same number of times.
352          */
353
354         /* Find a log slot for the next flight, if available */
355         ao_log_current_pos = ao_log_end_pos = 0;
356         log_slots = ao_log_slots();
357         log_want = (ao_flight_number - 1) % log_slots;
358         log_slot = log_want;
359         do {
360                 if (ao_log_flight(log_slot) == 0) {
361                         ao_log_current_pos = ao_log_pos(log_slot);
362                         ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
363                         break;
364                 }
365                 if (++log_slot >= log_slots)
366                         log_slot = 0;
367         } while (log_slot != log_want);
368         ao_wakeup(&ao_flight_number);
369         return 0;
370 #endif
371 }
372
373 void
374 ao_log_start(void)
375 {
376         /* start logging */
377         ao_log_running = 1;
378         ao_wakeup(&ao_log_running);
379 }
380
381 void
382 ao_log_stop(void)
383 {
384         ao_log_running = 0;
385         ao_log_flush();
386 }
387
388 uint8_t
389 ao_log_present(void)
390 {
391         return ao_log_max_flight() != 0;
392 }
393
394 uint8_t
395 ao_log_full(void)
396 {
397         return ao_log_current_pos == ao_log_end_pos;
398 }
399
400 #ifndef LOG_ADC
401 #define LOG_ADC HAS_ADC
402 #endif
403
404 #if LOG_ADC
405 static __xdata struct ao_task ao_log_task;
406 #endif
407
408 void
409 ao_log_list(void) __reentrant
410 {
411         uint8_t slot;
412         uint8_t slots;
413         int16_t flight;
414
415         slots = ao_log_slots();
416         for (slot = 0; slot < slots; slot++)
417         {
418                 flight = ao_log_flight(slot);
419                 if (flight)
420                         printf ("flight %d start %x end %x\n",
421                                 flight,
422                                 (uint16_t) (ao_log_pos(slot) >> 8),
423                                 (uint16_t) (ao_log_pos(slot+1) >> 8));
424         }
425         printf ("done\n");
426 }
427
428 void
429 ao_log_delete(void) __reentrant
430 {
431         uint8_t slot;
432         uint8_t slots;
433         int16_t cmd_flight = 1;
434
435         ao_cmd_white();
436         if (ao_cmd_lex_c == '-') {
437                 cmd_flight = -1;
438                 ao_cmd_lex();
439         }
440         ao_cmd_decimal();
441         if (ao_cmd_status != ao_cmd_success)
442                 return;
443         cmd_flight *= (int16_t) ao_cmd_lex_i;
444
445         slots = ao_log_slots();
446         /* Look for the flight log matching the requested flight */
447         if (cmd_flight) {
448                 for (slot = 0; slot < slots; slot++) {
449                         if (ao_log_flight(slot) == cmd_flight) {
450 #if HAS_TRACKER
451                                 ao_tracker_erase_start(cmd_flight);
452 #endif
453                                 ao_log_erase(slot);
454 #if HAS_TRACKER
455                                 ao_tracker_erase_end();
456 #endif
457                                 puts("Erased");
458                                 return;
459                         }
460                 }
461         }
462         printf("No such flight: %d\n", cmd_flight);
463 }
464
465 __code struct ao_cmds ao_log_cmds[] = {
466         { ao_log_list,  "l\0List logs" },
467         { ao_log_delete,        "d <flight-number>\0Delete flight" },
468         { 0,    NULL },
469 };
470
471 void
472 ao_log_init(void)
473 {
474         ao_log_running = 0;
475
476         /* For now, just log the flight starting at the begining of eeprom */
477         ao_log_state = ao_flight_invalid;
478
479         ao_cmd_register(&ao_log_cmds[0]);
480
481 #ifndef HAS_ADC
482 #error Define HAS_ADC for ao_log.c
483 #endif
484 #if LOG_ADC
485         /* Create a task to log events to eeprom */
486         ao_add_task(&ao_log_task, ao_log, "log");
487 #endif
488 }