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