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