34e2deedad12c0aa7bece87e4fc1ec4594daa6f8
[fw/altos] / aoview / 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                 aoview_file_printf(eeprom_file, "%s\n", line);
70                 if (cmd == 'S' && a == 8) {
71                         aoview_eeprom_done(serial);
72                         return FALSE;
73                 }
74
75                 if (!eeprom_save_shown)
76                 {
77                         name = aoview_file_name(eeprom_file);
78                         if (name) {
79                                 utf8_name = g_filename_to_utf8(name, -1, NULL, NULL, NULL);
80                                 if (!utf8_name)
81                                         utf8_name = (char *) name;
82                                 gtk_widget_set_sensitive(eeprom_save_close, FALSE);
83                                 gtk_window_set_title(GTK_WINDOW(eeprom_save_done),
84                                                      "Saving EEPROM data");
85                                 gtk_message_dialog_set_markup(eeprom_save_done,
86                                                               "<b>Saving EEPROM data as</b>");
87                                 gtk_message_dialog_format_secondary_text(eeprom_save_done, "%s",
88                                                                          utf8_name);
89                                 if (utf8_name != name)
90                                         g_free(utf8_name);
91                                 gtk_container_check_resize(GTK_CONTAINER(eeprom_save_done));
92                                 gtk_widget_show(GTK_WIDGET(eeprom_save_done));
93                                 eeprom_save_shown = TRUE;
94                                 eeprom_save_close = gtk_window_get_default_widget(GTK_WINDOW(eeprom_save_done));
95                                 if (eeprom_save_close)
96                                         gtk_widget_set_sensitive(eeprom_save_close, FALSE);
97                         }
98                 }
99         }
100         return TRUE;
101 }
102
103 static void
104 aoview_eeprom_callback(gpointer user_data,
105                        struct aoview_serial *serial,
106                        gint revents)
107 {
108         int     c;
109
110         if (revents & (G_IO_HUP|G_IO_ERR)) {
111                 aoview_eeprom_disconnect(serial);
112                 return;
113         }
114         if (revents & G_IO_IN) {
115                 for (;;) {
116                         c = aoview_serial_getc(serial);
117                         if (c == -1)
118                                 break;
119                         if (c == '\r')
120                                 continue;
121                         if (c == '\n') {
122                                 eeprom_line[eeprom_pos] = '\0';
123                                 if (eeprom_pos)
124                                 if (!aoview_eeprom_parse(serial, eeprom_line))
125                                         break;
126                                 eeprom_pos = 0;
127                         } else if (eeprom_pos < EEPROM_LEN)
128                                 eeprom_line[eeprom_pos++] = c;
129                 }
130         }
131 }
132
133 gboolean
134 aoview_eeprom_save(const char *device)
135 {
136         struct aoview_serial    *serial;
137
138         gtk_widget_hide(GTK_WIDGET(eeprom_save_done));
139         eeprom_save_shown = FALSE;
140         serial = aoview_serial_open(device);
141         if (!serial)
142                 return FALSE;
143         aoview_serial_set_callback(serial, aoview_eeprom_callback);
144         aoview_serial_printf(serial, "v\nl\n");
145         return TRUE;
146 }
147
148 void
149 aoview_eeprom_init(GladeXML *xml)
150 {
151         eeprom_file = aoview_file_new("eeprom");
152         assert(eeprom_file);
153
154         eeprom_save_done = GTK_MESSAGE_DIALOG(glade_xml_get_widget(xml, "ao_save_done"));
155         assert(eeprom_save_done);
156
157 }