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