20 static void error(const char *fmt, ...)
26 error = g_error_new_valist(
27 g_quark_from_string("divelog"),
28 DIVE_ERROR_PARSE, fmt, args);
34 typedef struct device_data_t {
37 device_devinfo_t devinfo;
41 static parser_status_t create_parser(device_data_t *devdata, parser_t **parser)
43 switch (devdata->type) {
44 case DEVICE_TYPE_SUUNTO_SOLUTION:
45 return suunto_solution_parser_create(parser);
47 case DEVICE_TYPE_SUUNTO_EON:
48 return suunto_eon_parser_create(parser, 0);
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);
55 case DEVICE_TYPE_SUUNTO_VYPER2:
56 case DEVICE_TYPE_SUUNTO_D9:
57 return suunto_d9_parser_create(parser, devdata->devinfo.model);
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);
63 case DEVICE_TYPE_UWATEC_SMART:
64 return uwatec_smart_parser_create(parser, devdata->devinfo.model, devdata->clock.devtime, devdata->clock.systime);
66 case DEVICE_TYPE_REEFNET_SENSUS:
67 return reefnet_sensus_parser_create(parser, devdata->clock.devtime, devdata->clock.systime);
69 case DEVICE_TYPE_REEFNET_SENSUSPRO:
70 return reefnet_sensuspro_parser_create(parser, devdata->clock.devtime, devdata->clock.systime);
72 case DEVICE_TYPE_REEFNET_SENSUSULTRA:
73 return reefnet_sensusultra_parser_create(parser, devdata->clock.devtime, devdata->clock.systime);
75 case DEVICE_TYPE_OCEANIC_VTPRO:
76 return oceanic_vtpro_parser_create(parser);
78 case DEVICE_TYPE_OCEANIC_VEO250:
79 return oceanic_veo250_parser_create(parser, devdata->devinfo.model);
81 case DEVICE_TYPE_OCEANIC_ATOM2:
82 return oceanic_atom2_parser_create(parser, devdata->devinfo.model);
84 case DEVICE_TYPE_MARES_NEMO:
85 case DEVICE_TYPE_MARES_PUCK:
86 return mares_nemo_parser_create(parser, devdata->devinfo.model);
88 case DEVICE_TYPE_MARES_ICONHD:
89 return mares_iconhd_parser_create(parser);
91 case DEVICE_TYPE_HW_OSTC:
92 return hw_ostc_parser_create(parser);
94 case DEVICE_TYPE_CRESSI_EDY:
95 case DEVICE_TYPE_ZEAGLE_N2ITION3:
96 return cressi_edy_parser_create(parser, devdata->devinfo.model);
98 case DEVICE_TYPE_ATOMICS_COBALT:
99 return atomics_cobalt_parser_create(parser);
102 return PARSER_STATUS_ERROR;
106 static int dive_cb(const unsigned char *data, unsigned int size,
107 const unsigned char *fingerprint, unsigned int fsize,
111 parser_t *parser = NULL;
112 device_data_t *devdata = userdata;
113 dc_datetime_t dt = {0};
115 rc = create_parser(devdata, &parser);
116 if (rc != PARSER_STATUS_SUCCESS) {
117 error("Unable to create parser for %s", devdata->name);
121 rc = parser_set_data(parser, data, size);
122 if (rc != PARSER_STATUS_SUCCESS) {
123 error("Error registering the data.");
124 parser_destroy(parser);
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);
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);
139 parser_destroy(parser);
140 return PARSER_STATUS_SUCCESS;
144 static device_status_t import_device_data(device_t *device, device_data_t *devicedata)
146 return device_foreach(device, dive_cb, devicedata);
149 static device_status_t device_open(const char *devname,
154 case DEVICE_TYPE_SUUNTO_SOLUTION:
155 return suunto_solution_device_open(device, devname);
157 case DEVICE_TYPE_SUUNTO_EON:
158 return suunto_eon_device_open(device, devname);
160 case DEVICE_TYPE_SUUNTO_VYPER:
161 return suunto_vyper_device_open(device, devname);
163 case DEVICE_TYPE_SUUNTO_VYPER2:
164 return suunto_vyper2_device_open(device, devname);
166 case DEVICE_TYPE_SUUNTO_D9:
167 return suunto_d9_device_open(device, devname);
169 case DEVICE_TYPE_UWATEC_ALADIN:
170 return uwatec_aladin_device_open(device, devname);
172 case DEVICE_TYPE_UWATEC_MEMOMOUSE:
173 return uwatec_memomouse_device_open(device, devname);
175 case DEVICE_TYPE_UWATEC_SMART:
176 return uwatec_smart_device_open(device);
178 case DEVICE_TYPE_REEFNET_SENSUS:
179 return reefnet_sensus_device_open(device, devname);
181 case DEVICE_TYPE_REEFNET_SENSUSPRO:
182 return reefnet_sensuspro_device_open(device, devname);
184 case DEVICE_TYPE_REEFNET_SENSUSULTRA:
185 return reefnet_sensusultra_device_open(device, devname);
187 case DEVICE_TYPE_OCEANIC_VTPRO:
188 return oceanic_vtpro_device_open(device, devname);
190 case DEVICE_TYPE_OCEANIC_VEO250:
191 return oceanic_veo250_device_open(device, devname);
193 case DEVICE_TYPE_OCEANIC_ATOM2:
194 return oceanic_atom2_device_open(device, devname);
196 case DEVICE_TYPE_MARES_NEMO:
197 return mares_nemo_device_open(device, devname);
199 case DEVICE_TYPE_MARES_PUCK:
200 return mares_puck_device_open(device, devname);
202 case DEVICE_TYPE_MARES_ICONHD:
203 return mares_iconhd_device_open(device, devname);
205 case DEVICE_TYPE_HW_OSTC:
206 return hw_ostc_device_open(device, devname);
208 case DEVICE_TYPE_CRESSI_EDY:
209 return cressi_edy_device_open(device, devname);
211 case DEVICE_TYPE_ZEAGLE_N2ITION3:
212 return zeagle_n2ition3_device_open(device, devname);
214 case DEVICE_TYPE_ATOMICS_COBALT:
215 return atomics_cobalt_device_open(device);
218 return DEVICE_STATUS_ERROR;
223 event_cb (device_t *device, device_event_t event, const void *data, void *userdata)
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;
231 case DEVICE_EVENT_WAITING:
232 printf("Event: waiting for user action\n");
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);
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);
246 case DEVICE_EVENT_CLOCK:
247 devdata->clock = *clock;
248 printf("Event: systime=%lld, devtime=%u\n",
249 clock->systime, clock->devtime);
257 cancel_cb (void *userdata)
262 static void do_import(const char *computer, device_type_t type)
264 /* FIXME! Needs user input! */
265 const char *devname = "/dev/ttyUSB0";
266 device_t *device = NULL;
268 device_data_t devicedata = {
273 rc = device_open(devname, type, &device);
274 if (rc != DEVICE_STATUS_SUCCESS) {
275 error("Unable to open %s (%s)", computer, devname);
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);
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);
296 rc = import_device_data(device, &devicedata);
297 if (rc != DEVICE_STATUS_SUCCESS) {
298 error("Dive data import error");
299 device_close(device);
303 device_close(device);
307 * Taken from 'example.c' in libdivecomputer.
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..
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 },
341 static void fill_computer_list(GtkListStore *store)
344 struct device_list *list = device_list;
346 for (list = device_list ; list->name ; list++) {
347 gtk_list_store_append(store, &iter);
348 gtk_list_store_set(store, &iter,
355 static GtkComboBox *dive_computer_selector(GtkWidget *dialog)
357 GtkWidget *hbox, *combo_box;
359 GtkCellRenderer *renderer;
361 hbox = gtk_hbox_new(FALSE, 6);
362 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), hbox);
364 model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
365 fill_computer_list(model);
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);
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);
374 return GTK_COMBO_BOX(combo_box);
377 void import_dialog(GtkWidget *w, gpointer data)
381 GtkComboBox *computer;
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,
390 computer = dive_computer_selector(dialog);
392 gtk_widget_show_all(dialog);
393 result = gtk_dialog_run(GTK_DIALOG(dialog));
399 case GTK_RESPONSE_ACCEPT:
400 if (!gtk_combo_box_get_active_iter(computer, &iter))
402 model = gtk_combo_box_get_model(computer);
403 gtk_tree_model_get(model, &iter,
407 do_import(comp, type);
412 gtk_widget_destroy(dialog);