altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / src / kernel / ao_log_single.c
1 /*
2  * Copyright © 2011 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 /*
20  * ao_log_single.c
21  *
22  * Stores a sequence of fixed-size (32 byte) chunks
23  * without splitting memory up into separate flights
24  */
25
26 #include "ao.h"
27 #include "ao_product.h"
28
29 static struct ao_task ao_log_single_task;
30
31 uint8_t         ao_log_running;
32 uint8_t         ao_log_mutex;
33 uint32_t        ao_log_start_pos;
34 uint32_t        ao_log_end_pos;
35 uint32_t        ao_log_current_pos;
36
37 union ao_log_single ao_log_single_write_data;
38 union ao_log_single ao_log_single_read_data;
39
40 uint8_t
41 ao_log_single_write(void)
42 {
43         uint8_t wrote = 0;
44
45         ao_mutex_get(&ao_log_mutex); {
46                 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
47                         ao_log_single_stop();
48                 if (ao_log_running) {
49                         wrote = 1;
50                         ao_storage_write(ao_log_current_pos,
51                                          &ao_log_single_write_data,
52                                          AO_LOG_SINGLE_SIZE);
53                         ao_log_current_pos += AO_LOG_SINGLE_SIZE;
54                 }
55         } ao_mutex_put(&ao_log_mutex);
56         return wrote;
57 }
58
59 static uint8_t
60 ao_log_single_valid(void)
61 {
62         uint8_t *d = ao_log_single_read_data.bytes;
63         uint8_t i;
64         for (i = 0; i < AO_LOG_SINGLE_SIZE; i++)
65                 if (*d++ != 0xff)
66                         return 1;
67         return 0;
68 }
69
70 uint8_t
71 ao_log_single_read(uint32_t pos)
72 {
73         if (!ao_storage_read(pos, &ao_log_single_read_data, AO_LOG_SINGLE_SIZE))
74                 return 0;
75         return ao_log_single_valid();
76 }
77
78 void
79 ao_log_single_start(void)
80 {
81         if (!ao_log_running) {
82                 ao_log_running = 1;
83                 ao_wakeup(&ao_log_running);
84         }
85 }
86
87 void
88 ao_log_single_stop(void)
89 {
90         if (ao_log_running) {
91                 ao_log_running = 0;
92         }
93 }
94
95 void
96 ao_log_single_restart(void)
97 {
98         /* Find end of data */
99         ao_log_end_pos = ao_storage_config;
100         for (ao_log_current_pos = 0;
101              ao_log_current_pos < ao_storage_config;
102              ao_log_current_pos += ao_storage_block)
103         {
104                 if (!ao_log_single_read(ao_log_current_pos))
105                         break;
106         }
107         if (ao_log_current_pos > 0) {
108                 ao_log_current_pos -= ao_storage_block;
109                 for (; ao_log_current_pos < ao_storage_config;
110                      ao_log_current_pos += sizeof (struct ao_log_telescience))
111                 {
112                         if (!ao_log_single_read(ao_log_current_pos))
113                                 break;
114                 }
115         }
116 }
117
118 void
119 ao_log_single_set(void)
120 {
121         uint16_t r;
122         printf("Logging currently %s\n", ao_log_running ? "on" : "off");
123         r = ao_cmd_hex();
124         if (ao_cmd_status == ao_cmd_success) {
125                 if (r) {
126                         printf("Logging from %ld to %ld\n", ao_log_current_pos, ao_log_end_pos);
127                         ao_log_single_start();
128                 } else {
129                         printf ("Log stopped at %ld\n", ao_log_current_pos);
130                         ao_log_single_stop();
131                 }
132         }
133         ao_cmd_status = ao_cmd_success;
134 }
135
136 void
137 ao_log_single_delete(void)
138 {
139         uint32_t        pos;
140         uint16_t        r = ao_cmd_hex();
141         if (ao_cmd_status != ao_cmd_success)
142                 return;
143         if (r != 1) {
144                 ao_cmd_status = ao_cmd_syntax_error;
145                 printf("No such flight: %d\n", r);
146                 return;
147         }
148         ao_log_single_stop();
149         for (pos = 0; pos < ao_storage_config; pos += ao_storage_block) {
150                 if (!ao_log_single_read(pos))
151                         break;
152                 ao_storage_erase(pos);
153         }
154         ao_log_current_pos = ao_log_start_pos = 0;
155         if (pos == 0)
156                 printf("No such flight: %d\n", r);
157         else
158                 printf ("Erased\n");
159 }
160
161 uint8_t
162 ao_log_full(void)
163 {
164         return ao_log_current_pos >= ao_log_end_pos;
165 }
166
167 uint8_t
168 ao_log_present(void)
169 {
170         return ao_log_single_read(0);
171 }
172
173 static void
174 ao_log_single_query(void)
175 {
176         printf("Logging enabled: %d\n", ao_log_running);
177         printf("Log start: %ld\n", ao_log_start_pos);
178         printf("Log cur: %ld\n", ao_log_current_pos);
179         printf("Log end: %ld\n", ao_log_end_pos);
180         ao_log_single_extra_query();
181 }
182
183 const struct ao_cmds ao_log_single_cmds[] = {
184         { ao_log_single_set,    "L <0 off, 1 on>\0Set logging" },
185         { ao_log_single_list,   "l\0List stored logs" },
186         { ao_log_single_delete, "d 1\0Delete all stored logs" },
187         { ao_log_single_query, "q\0Query log status" },
188         { 0,    NULL },
189 };
190
191 void
192 ao_log_single_init(void)
193 {
194         ao_log_running = 0;
195
196         ao_cmd_register(&ao_log_single_cmds[0]);
197
198         ao_add_task(&ao_log_single_task, ao_log_single, "log");
199 }