]> git.tdb.fi Git - ext/subsurface.git/blob - libdivecomputer.c
More libdivecomputer boilerplate stuff
[ext/subsurface.git] / libdivecomputer.c
1 #include <stdio.h>
2 #include <gtk/gtk.h>
3
4 #include "dive.h"
5 #include "display.h"
6
7 /* libdivecomputer */
8 #include <device.h>
9 #include <suunto.h>
10 #include <reefnet.h>
11 #include <uwatec.h>
12 #include <oceanic.h>
13 #include <mares.h>
14 #include <hw.h>
15 #include <cressi.h>
16 #include <zeagle.h>
17 #include <atomics.h>
18 #include <utils.h>
19
20 static void error(const char *fmt, ...)
21 {
22         va_list args;
23         GError *error;
24
25         va_start(args, fmt);
26         error = g_error_new_valist(
27                 g_quark_from_string("divelog"),
28                 DIVE_ERROR_PARSE, fmt, args);
29         va_end(args);
30         report_error(error);
31         g_error_free(error);
32 }
33
34 typedef struct device_data_t {
35         device_type_t type;
36         const char *name;
37         device_devinfo_t devinfo;
38         device_clock_t clock;
39 } device_data_t;
40
41 static parser_status_t create_parser(device_data_t *devdata, parser_t **parser)
42 {
43         switch (devdata->type) {
44         case DEVICE_TYPE_SUUNTO_SOLUTION:
45                 return suunto_solution_parser_create(parser);
46
47         case DEVICE_TYPE_SUUNTO_EON:
48                 return suunto_eon_parser_create(parser, 0);
49
50         case DEVICE_TYPE_SUUNTO_VYPER:
51                 if (devdata->devinfo.model == 0x01)
52                         return suunto_eon_parser_create(parser, 1);
53                 return suunto_vyper_parser_create(parser);
54
55         case DEVICE_TYPE_SUUNTO_VYPER2:
56         case DEVICE_TYPE_SUUNTO_D9:
57                 return suunto_d9_parser_create(parser, devdata->devinfo.model);
58
59         case DEVICE_TYPE_UWATEC_ALADIN:
60         case DEVICE_TYPE_UWATEC_MEMOMOUSE:
61                 return uwatec_memomouse_parser_create(parser, devdata->clock.devtime, devdata->clock.systime);
62
63         case DEVICE_TYPE_UWATEC_SMART:
64                 return uwatec_smart_parser_create(parser, devdata->devinfo.model, devdata->clock.devtime, devdata->clock.systime);
65
66         case DEVICE_TYPE_REEFNET_SENSUS:
67                 return reefnet_sensus_parser_create(parser, devdata->clock.devtime, devdata->clock.systime);
68
69         case DEVICE_TYPE_REEFNET_SENSUSPRO:
70                 return reefnet_sensuspro_parser_create(parser, devdata->clock.devtime, devdata->clock.systime);
71
72         case DEVICE_TYPE_REEFNET_SENSUSULTRA:
73                 return reefnet_sensusultra_parser_create(parser, devdata->clock.devtime, devdata->clock.systime);
74
75         case DEVICE_TYPE_OCEANIC_VTPRO:
76                 return oceanic_vtpro_parser_create(parser);
77
78         case DEVICE_TYPE_OCEANIC_VEO250:
79                 return oceanic_veo250_parser_create(parser, devdata->devinfo.model);
80
81         case DEVICE_TYPE_OCEANIC_ATOM2:
82                 return oceanic_atom2_parser_create(parser, devdata->devinfo.model);
83
84         case DEVICE_TYPE_MARES_NEMO:
85         case DEVICE_TYPE_MARES_PUCK:
86                 return mares_nemo_parser_create(parser, devdata->devinfo.model);
87
88         case DEVICE_TYPE_MARES_ICONHD:
89                 return mares_iconhd_parser_create(parser);
90
91         case DEVICE_TYPE_HW_OSTC:
92                 return hw_ostc_parser_create(parser);
93
94         case DEVICE_TYPE_CRESSI_EDY:
95         case DEVICE_TYPE_ZEAGLE_N2ITION3:
96                 return cressi_edy_parser_create(parser, devdata->devinfo.model);
97
98         case DEVICE_TYPE_ATOMICS_COBALT:
99                 return atomics_cobalt_parser_create(parser);
100
101         default:
102                 return PARSER_STATUS_ERROR;
103         }
104 }
105
106 static int dive_cb(const unsigned char *data, unsigned int size,
107         const unsigned char *fingerprint, unsigned int fsize,
108         void *userdata)
109 {
110         int rc;
111         parser_t *parser = NULL;
112         device_data_t *devdata = userdata;
113         dc_datetime_t dt = {0};
114
115         rc = create_parser(devdata, &parser);
116         if (rc != PARSER_STATUS_SUCCESS) {
117                 error("Unable to create parser for %s", devdata->name);
118                 return rc;
119         }
120
121         rc = parser_set_data(parser, data, size);
122         if (rc != PARSER_STATUS_SUCCESS) {
123                 error("Error registering the data.");
124                 parser_destroy(parser);
125                 return rc;
126         }
127
128         rc = parser_get_datetime(parser, &dt);
129         if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) {
130                 error("Error parsing the datetime.");
131                 parser_destroy (parser);
132                 return rc;
133         }
134
135         printf("<datetime>%04i-%02i-%02i %02i:%02i:%02i</datetime>\n",
136                 dt.year, dt.month, dt.day,
137                 dt.hour, dt.minute, dt.second);
138
139         parser_destroy(parser);
140         return PARSER_STATUS_SUCCESS;
141 }
142
143
144 static device_status_t import_device_data(device_t *device, device_data_t *devicedata)
145 {
146         return device_foreach(device, dive_cb, devicedata);
147 }
148
149 static device_status_t device_open(const char *devname,
150         device_type_t type,
151         device_t **device)
152 {
153         switch (type) {
154         case DEVICE_TYPE_SUUNTO_SOLUTION:
155                 return suunto_solution_device_open(device, devname);
156
157         case DEVICE_TYPE_SUUNTO_EON:
158                 return suunto_eon_device_open(device, devname);
159
160         case DEVICE_TYPE_SUUNTO_VYPER:
161                 return suunto_vyper_device_open(device, devname);
162
163         case DEVICE_TYPE_SUUNTO_VYPER2:
164                 return suunto_vyper2_device_open(device, devname);
165
166         case DEVICE_TYPE_SUUNTO_D9:
167                 return suunto_d9_device_open(device, devname);
168
169         case DEVICE_TYPE_UWATEC_ALADIN:
170                 return uwatec_aladin_device_open(device, devname);
171
172         case DEVICE_TYPE_UWATEC_MEMOMOUSE:
173                 return uwatec_memomouse_device_open(device, devname);
174
175         case DEVICE_TYPE_UWATEC_SMART:
176                 return uwatec_smart_device_open(device);
177
178         case DEVICE_TYPE_REEFNET_SENSUS:
179                 return reefnet_sensus_device_open(device, devname);
180
181         case DEVICE_TYPE_REEFNET_SENSUSPRO:
182                 return reefnet_sensuspro_device_open(device, devname);
183
184         case DEVICE_TYPE_REEFNET_SENSUSULTRA:
185                 return reefnet_sensusultra_device_open(device, devname);
186
187         case DEVICE_TYPE_OCEANIC_VTPRO:
188                 return oceanic_vtpro_device_open(device, devname);
189
190         case DEVICE_TYPE_OCEANIC_VEO250:
191                 return oceanic_veo250_device_open(device, devname);
192
193         case DEVICE_TYPE_OCEANIC_ATOM2:
194                 return oceanic_atom2_device_open(device, devname);
195
196         case DEVICE_TYPE_MARES_NEMO:
197                 return mares_nemo_device_open(device, devname);
198
199         case DEVICE_TYPE_MARES_PUCK:
200                 return mares_puck_device_open(device, devname);
201
202         case DEVICE_TYPE_MARES_ICONHD:
203                 return mares_iconhd_device_open(device, devname);
204
205         case DEVICE_TYPE_HW_OSTC:
206                 return hw_ostc_device_open(device, devname);
207
208         case DEVICE_TYPE_CRESSI_EDY:
209                 return cressi_edy_device_open(device, devname);
210
211         case DEVICE_TYPE_ZEAGLE_N2ITION3:
212                 return zeagle_n2ition3_device_open(device, devname);
213
214         case DEVICE_TYPE_ATOMICS_COBALT:
215                 return atomics_cobalt_device_open(device);
216
217         default:
218                 return DEVICE_STATUS_ERROR;
219         }
220 }
221
222 static void
223 event_cb (device_t *device, device_event_t event, const void *data, void *userdata)
224 {
225         const device_progress_t *progress = (device_progress_t *) data;
226         const device_devinfo_t *devinfo = (device_devinfo_t *) data;
227         const device_clock_t *clock = (device_clock_t *) data;
228         device_data_t *devdata = (device_data_t *) userdata;
229
230         switch (event) {
231         case DEVICE_EVENT_WAITING:
232                 printf("Event: waiting for user action\n");
233                 break;
234         case DEVICE_EVENT_PROGRESS:
235                 printf("Event: progress %3.2f%% (%u/%u)\n",
236                         100.0 * (double) progress->current / (double) progress->maximum,
237                         progress->current, progress->maximum);
238                 break;
239         case DEVICE_EVENT_DEVINFO:
240                 devdata->devinfo = *devinfo;
241                 printf("Event: model=%u (0x%08x), firmware=%u (0x%08x), serial=%u (0x%08x)\n",
242                         devinfo->model, devinfo->model,
243                         devinfo->firmware, devinfo->firmware,
244                         devinfo->serial, devinfo->serial);
245                 break;
246         case DEVICE_EVENT_CLOCK:
247                 devdata->clock = *clock;
248                 printf("Event: systime=%lld, devtime=%u\n",
249                         clock->systime, clock->devtime);
250                 break;
251         default:
252                 break;
253         }
254 }
255
256 static int
257 cancel_cb (void *userdata)
258 {
259         return 0;
260 }
261
262 static void do_import(const char *computer, device_type_t type)
263 {
264         /* FIXME! Needs user input! */
265         const char *devname = "/dev/ttyUSB0";
266         device_t *device = NULL;
267         device_status_t rc;
268         device_data_t devicedata = {
269                 .type = type,
270                 .name = computer,
271         };
272
273         rc = device_open(devname, type, &device);
274         if (rc != DEVICE_STATUS_SUCCESS) {
275                 error("Unable to open %s (%s)", computer, devname);
276                 return;
277         }
278
279         // Register the event handler.
280         int events = DEVICE_EVENT_WAITING | DEVICE_EVENT_PROGRESS | DEVICE_EVENT_DEVINFO | DEVICE_EVENT_CLOCK;
281         rc = device_set_events(device, events, event_cb, &devicedata);
282         if (rc != DEVICE_STATUS_SUCCESS) {
283                 error("Error registering the event handler.");
284                 device_close(device);
285                 return;
286         }
287
288         // Register the cancellation handler.
289         rc = device_set_cancel(device, cancel_cb, &devicedata);
290         if (rc != DEVICE_STATUS_SUCCESS) {
291                 error("Error registering the cancellation handler.");
292                 device_close(device);
293                 return;
294         }
295
296         rc = import_device_data(device, &devicedata);
297         if (rc != DEVICE_STATUS_SUCCESS) {
298                 error("Dive data import error");
299                 device_close(device);
300                 return;
301         }
302
303         device_close(device);
304 }
305
306 /*
307  * Taken from 'example.c' in libdivecomputer.
308  *
309  * I really wish there was some way to just have
310  * libdivecomputer tell us what devices it supports,
311  * rather than have the application have to know..
312  */
313 struct device_list {
314         const char *name;
315         device_type_t type;
316 } device_list[] = {
317         { "Suunto Solution",    DEVICE_TYPE_SUUNTO_SOLUTION },
318         { "Suunto Eon",         DEVICE_TYPE_SUUNTO_EON },
319         { "Suunto Vyper",       DEVICE_TYPE_SUUNTO_VYPER },
320         { "Suunto Vyper Air",   DEVICE_TYPE_SUUNTO_VYPER2 },
321         { "Suunto D9",          DEVICE_TYPE_SUUNTO_D9 },
322         { "Uwatec Aladin",      DEVICE_TYPE_UWATEC_ALADIN },
323         { "Uwatec Memo Mouse",  DEVICE_TYPE_UWATEC_MEMOMOUSE },
324         { "Uwatec Smart",       DEVICE_TYPE_UWATEC_SMART },
325         { "ReefNet Sensus",     DEVICE_TYPE_REEFNET_SENSUS },
326         { "ReefNet Sensus Pro", DEVICE_TYPE_REEFNET_SENSUSPRO },
327         { "ReefNet Sensus Ultra",DEVICE_TYPE_REEFNET_SENSUSULTRA },
328         { "Oceanic VT Pro",     DEVICE_TYPE_OCEANIC_VTPRO },
329         { "Oceanic Veo250",     DEVICE_TYPE_OCEANIC_VEO250 },
330         { "Oceanic Atom 2",     DEVICE_TYPE_OCEANIC_ATOM2 },
331         { "Mares Nemo",         DEVICE_TYPE_MARES_NEMO },
332         { "Mares Puck",         DEVICE_TYPE_MARES_PUCK },
333         { "Mares Icon HD",      DEVICE_TYPE_MARES_ICONHD },
334         { "OSTC",               DEVICE_TYPE_HW_OSTC },
335         { "Cressi Edy",         DEVICE_TYPE_CRESSI_EDY },
336         { "Zeagle N2iTiON 3",   DEVICE_TYPE_ZEAGLE_N2ITION3 },
337         { "Atomics Cobalt",     DEVICE_TYPE_ATOMICS_COBALT },
338         { NULL }
339 };
340
341 static void fill_computer_list(GtkListStore *store)
342 {
343         GtkTreeIter iter;
344         struct device_list *list = device_list;
345
346         for (list = device_list ; list->name ; list++) {
347                 gtk_list_store_append(store, &iter);
348                 gtk_list_store_set(store, &iter,
349                         0, list->name,
350                         1, list->type,
351                         -1);
352         }
353 }
354
355 static GtkComboBox *dive_computer_selector(GtkWidget *dialog)
356 {
357         GtkWidget *hbox, *combo_box;
358         GtkListStore *model;
359         GtkCellRenderer *renderer;
360
361         hbox = gtk_hbox_new(FALSE, 6);
362         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), hbox);
363
364         model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
365         fill_computer_list(model);
366
367         combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
368         gtk_box_pack_start(GTK_BOX(hbox), combo_box, FALSE, TRUE, 3);
369
370         renderer = gtk_cell_renderer_text_new();
371         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
372         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
373
374         return GTK_COMBO_BOX(combo_box);
375 }
376
377 void import_dialog(GtkWidget *w, gpointer data)
378 {
379         int result;
380         GtkWidget *dialog;
381         GtkComboBox *computer;
382
383         dialog = gtk_dialog_new_with_buttons("Import from dive computer",
384                 GTK_WINDOW(main_window),
385                 GTK_DIALOG_DESTROY_WITH_PARENT,
386                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
387                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
388                 NULL);
389
390         computer = dive_computer_selector(dialog);
391
392         gtk_widget_show_all(dialog);
393         result = gtk_dialog_run(GTK_DIALOG(dialog));
394         switch (result) {
395                 int type;
396                 GtkTreeIter iter;
397                 GtkTreeModel *model;
398                 const char *comp;
399         case GTK_RESPONSE_ACCEPT:
400                 if (!gtk_combo_box_get_active_iter(computer, &iter))
401                         break;
402                 model = gtk_combo_box_get_model(computer);
403                 gtk_tree_model_get(model, &iter,
404                         0, &comp,
405                         1, &type,
406                         -1);
407                 do_import(comp, type);
408                 break;
409         default:
410                 break;
411         }
412         gtk_widget_destroy(dialog);
413 }
414