device.c (14322B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2008 Jef Driesen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #include <assert.h> 23 #include <stdlib.h> 24 #include <string.h> 25 26 #include "suunto_d9.h" 27 #include "suunto_eon.h" 28 #include "suunto_eonsteel.h" 29 #include "suunto_solution.h" 30 #include "suunto_vyper2.h" 31 #include "suunto_vyper.h" 32 #include "reefnet_sensus.h" 33 #include "reefnet_sensuspro.h" 34 #include "reefnet_sensusultra.h" 35 #include "uwatec_aladin.h" 36 #include "uwatec_memomouse.h" 37 #include "uwatec_smart.h" 38 #include "oceanic_atom2.h" 39 #include "oceanic_veo250.h" 40 #include "oceanic_vtpro.h" 41 #include "pelagic_i330r.h" 42 #include "mares_darwin.h" 43 #include "mares_iconhd.h" 44 #include "mares_nemo.h" 45 #include "mares_puck.h" 46 #include "hw_ostc.h" 47 #include "hw_ostc3.h" 48 #include "cressi_edy.h" 49 #include "cressi_leonardo.h" 50 #include "cressi_goa.h" 51 #include "zeagle_n2ition3.h" 52 #include "atomics_cobalt.h" 53 #include "shearwater_petrel.h" 54 #include "shearwater_predator.h" 55 #include "diverite_nitekq.h" 56 #include "citizen_aqualand.h" 57 #include "divesystem_idive.h" 58 #include "cochran_commander.h" 59 #include "tecdiving_divecomputereu.h" 60 #include "mclean_extreme.h" 61 #include "liquivision_lynx.h" 62 #include "sporasub_sp2.h" 63 #include "deepsix_excursion.h" 64 #include "seac_screen.h" 65 #include "deepblu_cosmiq.h" 66 #include "oceans_s1.h" 67 #include "divesoft_freedom.h" 68 #include "halcyon_symbios.h" 69 70 // Not merged upstream yet 71 #include "garmin.h" 72 73 #include "device-private.h" 74 #include "context-private.h" 75 76 dc_device_t * 77 dc_device_allocate (dc_context_t *context, const dc_device_vtable_t *vtable) 78 { 79 dc_device_t *device = NULL; 80 81 assert(vtable != NULL); 82 assert(vtable->size >= sizeof(dc_device_t)); 83 84 // Allocate memory. 85 device = (dc_device_t *) malloc (vtable->size); 86 if (device == NULL) { 87 ERROR (context, "Failed to allocate memory."); 88 return device; 89 } 90 91 device->vtable = vtable; 92 93 device->context = context; 94 95 device->event_mask = 0; 96 device->event_callback = NULL; 97 device->event_userdata = NULL; 98 99 device->cancel_callback = NULL; 100 device->cancel_userdata = NULL; 101 102 memset (&device->devinfo, 0, sizeof (device->devinfo)); 103 memset (&device->clock, 0, sizeof (device->clock)); 104 105 return device; 106 } 107 108 void 109 dc_device_deallocate (dc_device_t *device) 110 { 111 free (device); 112 } 113 114 dc_status_t 115 dc_device_open (dc_device_t **out, dc_context_t *context, dc_descriptor_t *descriptor, dc_iostream_t *iostream) 116 { 117 dc_status_t rc = DC_STATUS_SUCCESS; 118 dc_device_t *device = NULL; 119 120 if (out == NULL || descriptor == NULL) 121 return DC_STATUS_INVALIDARGS; 122 123 switch (dc_descriptor_get_type (descriptor)) { 124 case DC_FAMILY_SUUNTO_SOLUTION: 125 rc = suunto_solution_device_open (&device, context, iostream); 126 break; 127 case DC_FAMILY_SUUNTO_EON: 128 rc = suunto_eon_device_open (&device, context, iostream); 129 break; 130 case DC_FAMILY_SUUNTO_VYPER: 131 rc = suunto_vyper_device_open (&device, context, iostream); 132 break; 133 case DC_FAMILY_SUUNTO_VYPER2: 134 rc = suunto_vyper2_device_open (&device, context, iostream); 135 break; 136 case DC_FAMILY_SUUNTO_D9: 137 rc = suunto_d9_device_open (&device, context, iostream, dc_descriptor_get_model (descriptor)); 138 break; 139 case DC_FAMILY_SUUNTO_EONSTEEL: 140 rc = suunto_eonsteel_device_open (&device, context, iostream, dc_descriptor_get_model (descriptor)); 141 break; 142 case DC_FAMILY_UWATEC_ALADIN: 143 rc = uwatec_aladin_device_open (&device, context, iostream); 144 break; 145 case DC_FAMILY_UWATEC_MEMOMOUSE: 146 rc = uwatec_memomouse_device_open (&device, context, iostream); 147 break; 148 case DC_FAMILY_UWATEC_SMART: 149 rc = uwatec_smart_device_open (&device, context, iostream); 150 break; 151 case DC_FAMILY_REEFNET_SENSUS: 152 rc = reefnet_sensus_device_open (&device, context, iostream); 153 break; 154 case DC_FAMILY_REEFNET_SENSUSPRO: 155 rc = reefnet_sensuspro_device_open (&device, context, iostream); 156 break; 157 case DC_FAMILY_REEFNET_SENSUSULTRA: 158 rc = reefnet_sensusultra_device_open (&device, context, iostream); 159 break; 160 case DC_FAMILY_OCEANIC_VTPRO: 161 rc = oceanic_vtpro_device_open (&device, context, iostream, dc_descriptor_get_model (descriptor)); 162 break; 163 case DC_FAMILY_OCEANIC_VEO250: 164 rc = oceanic_veo250_device_open (&device, context, iostream); 165 break; 166 case DC_FAMILY_OCEANIC_ATOM2: 167 rc = oceanic_atom2_device_open (&device, context, iostream, dc_descriptor_get_model (descriptor)); 168 break; 169 case DC_FAMILY_PELAGIC_I330R: 170 rc = pelagic_i330r_device_open (&device, context, iostream, dc_descriptor_get_model (descriptor)); 171 break; 172 case DC_FAMILY_MARES_NEMO: 173 rc = mares_nemo_device_open (&device, context, iostream); 174 break; 175 case DC_FAMILY_MARES_PUCK: 176 rc = mares_puck_device_open (&device, context, iostream); 177 break; 178 case DC_FAMILY_MARES_DARWIN: 179 rc = mares_darwin_device_open (&device, context, iostream, dc_descriptor_get_model (descriptor)); 180 break; 181 case DC_FAMILY_MARES_ICONHD: 182 rc = mares_iconhd_device_open (&device, context, iostream, dc_descriptor_get_model (descriptor)); 183 break; 184 case DC_FAMILY_HW_OSTC: 185 rc = hw_ostc_device_open (&device, context, iostream); 186 break; 187 case DC_FAMILY_HW_FROG: 188 rc = hw_ostc3_device_open (&device, context, iostream, 1); 189 break; 190 case DC_FAMILY_HW_OSTC3: 191 rc = hw_ostc3_device_open (&device, context, iostream, 0); 192 break; 193 case DC_FAMILY_CRESSI_EDY: 194 rc = cressi_edy_device_open (&device, context, iostream); 195 break; 196 case DC_FAMILY_CRESSI_LEONARDO: 197 rc = cressi_leonardo_device_open (&device, context, iostream); 198 break; 199 case DC_FAMILY_CRESSI_GOA: 200 rc = cressi_goa_device_open (&device, context, iostream); 201 break; 202 case DC_FAMILY_ZEAGLE_N2ITION3: 203 rc = zeagle_n2ition3_device_open (&device, context, iostream); 204 break; 205 case DC_FAMILY_ATOMICS_COBALT: 206 rc = atomics_cobalt_device_open (&device, context, iostream); 207 break; 208 case DC_FAMILY_SHEARWATER_PREDATOR: 209 rc = shearwater_predator_device_open (&device, context, iostream); 210 break; 211 case DC_FAMILY_SHEARWATER_PETREL: 212 rc = shearwater_petrel_device_open (&device, context, iostream, dc_descriptor_get_model (descriptor)); 213 break; 214 case DC_FAMILY_DIVERITE_NITEKQ: 215 rc = diverite_nitekq_device_open (&device, context, iostream); 216 break; 217 case DC_FAMILY_CITIZEN_AQUALAND: 218 rc = citizen_aqualand_device_open (&device, context, iostream); 219 break; 220 case DC_FAMILY_DIVESYSTEM_IDIVE: 221 rc = divesystem_idive_device_open (&device, context, iostream, dc_descriptor_get_model (descriptor)); 222 break; 223 case DC_FAMILY_COCHRAN_COMMANDER: 224 rc = cochran_commander_device_open (&device, context, iostream); 225 break; 226 case DC_FAMILY_TECDIVING_DIVECOMPUTEREU: 227 rc = tecdiving_divecomputereu_device_open (&device, context, iostream); 228 break; 229 case DC_FAMILY_MCLEAN_EXTREME: 230 rc = mclean_extreme_device_open (&device, context, iostream); 231 break; 232 case DC_FAMILY_LIQUIVISION_LYNX: 233 rc = liquivision_lynx_device_open (&device, context, iostream); 234 break; 235 case DC_FAMILY_SPORASUB_SP2: 236 rc = sporasub_sp2_device_open (&device, context, iostream); 237 break; 238 case DC_FAMILY_DEEPSIX_EXCURSION: 239 rc = deepsix_excursion_device_open (&device, context, iostream); 240 break; 241 case DC_FAMILY_SEAC_SCREEN: 242 rc = seac_screen_device_open (&device, context, iostream); 243 break; 244 case DC_FAMILY_DEEPBLU_COSMIQ: 245 rc = deepblu_cosmiq_device_open (&device, context, iostream); 246 break; 247 case DC_FAMILY_OCEANS_S1: 248 rc = oceans_s1_device_open (&device, context, iostream); 249 break; 250 case DC_FAMILY_DIVESOFT_FREEDOM: 251 rc = divesoft_freedom_device_open (&device, context, iostream); 252 break; 253 case DC_FAMILY_HALCYON_SYMBIOS: 254 rc = halcyon_symbios_device_open (&device, context, iostream); 255 break; 256 default: 257 return DC_STATUS_INVALIDARGS; 258 259 // Not merged upstream yet 260 case DC_FAMILY_GARMIN: 261 rc = garmin_device_open (&device, context, iostream, dc_descriptor_get_model (descriptor)); 262 break; 263 } 264 265 *out = device; 266 267 return rc; 268 } 269 270 271 int 272 dc_device_isinstance (dc_device_t *device, const dc_device_vtable_t *vtable) 273 { 274 if (device == NULL) 275 return 0; 276 277 return device->vtable == vtable; 278 } 279 280 281 dc_family_t 282 dc_device_get_type (dc_device_t *device) 283 { 284 if (device == NULL) 285 return DC_FAMILY_NULL; 286 287 return device->vtable->type; 288 } 289 290 291 dc_status_t 292 dc_device_set_cancel (dc_device_t *device, dc_cancel_callback_t callback, void *userdata) 293 { 294 if (device == NULL) 295 return DC_STATUS_UNSUPPORTED; 296 297 device->cancel_callback = callback; 298 device->cancel_userdata = userdata; 299 300 return DC_STATUS_SUCCESS; 301 } 302 303 304 dc_status_t 305 dc_device_set_events (dc_device_t *device, unsigned int events, dc_event_callback_t callback, void *userdata) 306 { 307 if (device == NULL) 308 return DC_STATUS_UNSUPPORTED; 309 310 device->event_mask = events; 311 device->event_callback = callback; 312 device->event_userdata = userdata; 313 314 return DC_STATUS_SUCCESS; 315 } 316 317 318 dc_status_t 319 dc_device_set_fingerprint (dc_device_t *device, const unsigned char data[], unsigned int size) 320 { 321 if (device == NULL) 322 return DC_STATUS_UNSUPPORTED; 323 324 if (device->vtable->set_fingerprint == NULL) 325 return DC_STATUS_UNSUPPORTED; 326 327 HEXDUMP (device->context, DC_LOGLEVEL_INFO, "Fingerprint", data, size); 328 329 return device->vtable->set_fingerprint (device, data, size); 330 } 331 332 333 dc_status_t 334 dc_device_read (dc_device_t *device, unsigned int address, unsigned char data[], unsigned int size) 335 { 336 if (device == NULL) 337 return DC_STATUS_UNSUPPORTED; 338 339 if (device->vtable->read == NULL) 340 return DC_STATUS_UNSUPPORTED; 341 342 return device->vtable->read (device, address, data, size); 343 } 344 345 346 dc_status_t 347 dc_device_write (dc_device_t *device, unsigned int address, const unsigned char data[], unsigned int size) 348 { 349 if (device == NULL) 350 return DC_STATUS_UNSUPPORTED; 351 352 if (device->vtable->write == NULL) 353 return DC_STATUS_UNSUPPORTED; 354 355 return device->vtable->write (device, address, data, size); 356 } 357 358 359 dc_status_t 360 dc_device_dump (dc_device_t *device, dc_buffer_t *buffer) 361 { 362 if (device == NULL) 363 return DC_STATUS_UNSUPPORTED; 364 365 if (device->vtable->dump == NULL) 366 return DC_STATUS_UNSUPPORTED; 367 368 if (buffer == NULL) 369 return DC_STATUS_INVALIDARGS; 370 371 dc_buffer_clear (buffer); 372 373 return device->vtable->dump (device, buffer); 374 } 375 376 377 dc_status_t 378 device_dump_read (dc_device_t *device, unsigned int address, unsigned char data[], unsigned int size, unsigned int blocksize) 379 { 380 if (device == NULL) 381 return DC_STATUS_UNSUPPORTED; 382 383 if (device->vtable->read == NULL) 384 return DC_STATUS_UNSUPPORTED; 385 386 // Enable progress notifications. 387 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 388 progress.maximum = size; 389 device_event_emit (device, DC_EVENT_PROGRESS, &progress); 390 391 unsigned int nbytes = 0; 392 while (nbytes < size) { 393 // Calculate the packet size. 394 unsigned int len = size - nbytes; 395 if (len > blocksize) 396 len = blocksize; 397 398 // Read the packet. 399 dc_status_t rc = device->vtable->read (device, address + nbytes, data + nbytes, len); 400 if (rc != DC_STATUS_SUCCESS) 401 return rc; 402 403 // Update and emit a progress event. 404 progress.current += len; 405 device_event_emit (device, DC_EVENT_PROGRESS, &progress); 406 407 nbytes += len; 408 } 409 410 return DC_STATUS_SUCCESS; 411 } 412 413 414 dc_status_t 415 dc_device_foreach (dc_device_t *device, dc_dive_callback_t callback, void *userdata) 416 { 417 if (device == NULL) 418 return DC_STATUS_UNSUPPORTED; 419 420 if (device->vtable->foreach == NULL) 421 return DC_STATUS_UNSUPPORTED; 422 423 return device->vtable->foreach (device, callback, userdata); 424 } 425 426 427 dc_status_t 428 dc_device_timesync (dc_device_t *device, const dc_datetime_t *datetime) 429 { 430 if (device == NULL) 431 return DC_STATUS_UNSUPPORTED; 432 433 if (device->vtable->timesync == NULL) 434 return DC_STATUS_UNSUPPORTED; 435 436 if (datetime == NULL) 437 return DC_STATUS_INVALIDARGS; 438 439 return device->vtable->timesync (device, datetime); 440 } 441 442 443 dc_status_t 444 dc_device_close (dc_device_t *device) 445 { 446 dc_status_t status = DC_STATUS_SUCCESS; 447 448 if (device == NULL) 449 return DC_STATUS_SUCCESS; 450 451 // Disable the cancellation callback. 452 device->cancel_callback = NULL; 453 device->cancel_userdata = NULL; 454 455 if (device->vtable->close) { 456 status = device->vtable->close (device); 457 } 458 459 dc_device_deallocate (device); 460 461 return status; 462 } 463 464 465 // AI-generated (Claude) 466 void 467 device_set_hw_id (dc_device_t *device, unsigned int hw_id) 468 { 469 device->devinfo.hw_id = hw_id; 470 } 471 472 void 473 device_event_emit (dc_device_t *device, dc_event_type_t event, const void *data) 474 { 475 const dc_event_progress_t *progress = (const dc_event_progress_t *) data; 476 477 // Check the event data for errors. 478 switch (event) { 479 case DC_EVENT_WAITING: 480 assert (data == NULL); 481 break; 482 case DC_EVENT_PROGRESS: 483 assert (progress != NULL); 484 assert (progress->maximum != 0); 485 assert (progress->maximum >= progress->current); 486 break; 487 case DC_EVENT_DEVINFO: 488 assert (data != NULL); 489 break; 490 case DC_EVENT_CLOCK: 491 assert (data != NULL); 492 break; 493 default: 494 break; 495 } 496 497 if (device == NULL) 498 return; 499 500 // Cache the event data. 501 switch (event) { 502 case DC_EVENT_DEVINFO: 503 { 504 const dc_event_devinfo_t *devinfo = (const dc_event_devinfo_t *) data; 505 device->devinfo.model = devinfo->model; 506 device->devinfo.firmware = devinfo->firmware; 507 device->devinfo.serial = devinfo->serial; 508 memset (device->devinfo.product_name, 0, sizeof (device->devinfo.product_name)); 509 memcpy (device->devinfo.product_name, devinfo->product_name, 510 sizeof (device->devinfo.product_name) - 1); 511 data = &device->devinfo; 512 } 513 break; 514 case DC_EVENT_CLOCK: 515 device->clock = *(const dc_event_clock_t *) data; 516 break; 517 default: 518 break; 519 } 520 521 // Check if there is a callback function registered. 522 if (device->event_callback == NULL) 523 return; 524 525 // Check the event mask. 526 if ((event & device->event_mask) == 0) 527 return; 528 529 device->event_callback (device, event, data, device->event_userdata); 530 } 531 532 533 int 534 device_is_cancelled (dc_device_t *device) 535 { 536 if (device == NULL) 537 return 0; 538 539 if (device->cancel_callback == NULL) 540 return 0; 541 542 return device->cancel_callback (device->cancel_userdata); 543 }