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