altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / ao-tools / ao-view / aoview_eeprom.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; 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 #include "aoview.h"
20
21 #define EEPROM_LEN      1024
22
23 static struct aoview_file       *eeprom_file;
24 static char                     eeprom_line[EEPROM_LEN + 1];
25 static int                      eeprom_pos;
26 static GtkMessageDialog         *eeprom_save_done;
27 static GtkWidget                *eeprom_save_close;
28 static gboolean                 eeprom_save_shown;
29
30 static void
31 aoview_eeprom_disconnect(struct aoview_serial *serial)
32 {
33         aoview_file_finish(eeprom_file);
34 }
35
36 static void
37 aoview_eeprom_done(struct aoview_serial *serial)
38 {
39         gtk_window_set_title(GTK_WINDOW(eeprom_save_done),
40                              "EEPROM data saved");
41         gtk_message_dialog_set_markup(eeprom_save_done,
42                                       "<b>EEPROM data saved as</b>");
43         if (!eeprom_save_shown)
44                 gtk_widget_show(GTK_WIDGET(eeprom_save_done));
45         eeprom_save_close = gtk_window_get_default_widget(GTK_WINDOW(eeprom_save_done));
46         if (eeprom_save_close)
47                 gtk_widget_set_sensitive(eeprom_save_close, TRUE);
48         aoview_eeprom_disconnect(serial);
49 }
50
51 static gboolean
52 aoview_eeprom_parse(struct aoview_serial *serial,
53                     char *line)
54 {
55         char            cmd;
56         int             tick;
57         int             a;
58         int             b;
59         int             serial_number;
60         const char      *name;
61         char            *utf8_name;
62
63         if (!strcmp(line, "end")) {
64                 aoview_eeprom_done(serial);
65                 return FALSE;
66         }
67         if (sscanf(line, "serial-number %u", &serial_number) == 1) {
68                 aoview_file_set_serial(eeprom_file, serial_number);
69         } else if (sscanf(line, "%c %x %x %x", &cmd, &tick, &a, &b) == 4) {
70                 if (cmd == 'F')
71                         aoview_file_set_flight(eeprom_file, b);
72                 aoview_file_printf(eeprom_file, "%s\n", line);
73                 if (cmd == 'S' && a == 8) {
74                         aoview_eeprom_done(serial);
75                         return FALSE;
76                 }
77
78                 if (!eeprom_save_shown)
79                 {
80                         name = aoview_file_name(eeprom_file);
81                         if (name) {
82                                 utf8_name = g_filename_to_utf8(name, -1, NULL, NULL, NULL);
83                                 if (!utf8_name)
84                                         utf8_name = (char *) name;
85                                 gtk_widget_set_sensitive(eeprom_save_close, FALSE);
86                                 gtk_window_set_title(GTK_WINDOW(eeprom_save_done),
87                                                      "Saving EEPROM data");
88                                 gtk_message_dialog_set_markup(eeprom_save_done,
89                                                               "<b>Saving EEPROM data as</b>");
90                                 gtk_message_dialog_format_secondary_text(eeprom_save_done, "%s",
91                                                                          utf8_name);
92                                 if (utf8_name != name)
93                                         g_free(utf8_name);
94                                 gtk_container_check_resize(GTK_CONTAINER(eeprom_save_done));
95                                 gtk_widget_show(GTK_WIDGET(eeprom_save_done));
96                                 eeprom_save_shown = TRUE;
97                                 eeprom_save_close = gtk_window_get_default_widget(GTK_WINDOW(eeprom_save_done));
98                                 if (eeprom_save_close)
99                                         gtk_widget_set_sensitive(eeprom_save_close, FALSE);
100                         }
101                 }
102         }
103         return TRUE;
104 }
105
106 static void
107 aoview_eeprom_callback(gpointer user_data,
108                        struct aoview_serial *serial,
109                        gint revents)
110 {
111         int     c;
112
113         if (revents & (G_IO_HUP|G_IO_ERR)) {
114                 aoview_eeprom_disconnect(serial);
115                 return;
116         }
117         if (revents & G_IO_IN) {
118                 for (;;) {
119                         c = aoview_serial_getc(serial);
120                         if (c == -1)
121                                 break;
122                         if (c == '\r')
123                                 continue;
124                         if (c == '\n') {
125                                 eeprom_line[eeprom_pos] = '\0';
126                                 if (eeprom_pos)
127                                 if (!aoview_eeprom_parse(serial, eeprom_line))
128                                         break;
129                                 eeprom_pos = 0;
130                         } else if (eeprom_pos < EEPROM_LEN)
131                                 eeprom_line[eeprom_pos++] = c;
132                 }
133         }
134 }
135
136 gboolean
137 aoview_eeprom_save(const char *device)
138 {
139         struct aoview_serial    *serial;
140
141         gtk_widget_hide(GTK_WIDGET(eeprom_save_done));
142         eeprom_save_shown = FALSE;
143         serial = aoview_serial_open(device);
144         if (!serial)
145                 return FALSE;
146         aoview_serial_set_callback(serial, aoview_eeprom_callback);
147         aoview_serial_printf(serial, "v\nl\n");
148         return TRUE;
149 }
150
151 void
152 aoview_eeprom_init(GladeXML *xml)
153 {
154         eeprom_file = aoview_file_new("eeprom");
155         assert(eeprom_file);
156
157         eeprom_save_done = GTK_MESSAGE_DIALOG(glade_xml_get_widget(xml, "ao_save_done"));
158         assert(eeprom_save_done);
159
160 }