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