diff --git a/include/libdivecomputer/shearwater_petrel.h b/include/libdivecomputer/shearwater_petrel.h index 18a4bce..97a8dc2 100644 --- a/include/libdivecomputer/shearwater_petrel.h +++ b/include/libdivecomputer/shearwater_petrel.h @@ -2,6 +2,7 @@ * libdivecomputer * * Copyright (C) 2013 Jef Driesen + * Copyright (C) 2015 Claudiu Olteanu * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -33,6 +34,9 @@ extern "C" { dc_status_t shearwater_petrel_device_open (dc_device_t **device, dc_context_t *context, const char *name); +dc_status_t +shearwater_petrel_device_custom_open (dc_device_t **out, dc_context_t *context, dc_serial_t *serial); + dc_status_t shearwater_petrel_parser_create (dc_parser_t **parser, dc_context_t *context, unsigned int serial); diff --git a/include/libdivecomputer/shearwater_predator.h b/include/libdivecomputer/shearwater_predator.h index 28163e2..9a37bcf 100644 --- a/include/libdivecomputer/shearwater_predator.h +++ b/include/libdivecomputer/shearwater_predator.h @@ -2,6 +2,7 @@ * libdivecomputer * * Copyright (C) 2012 Jef Driesen + * Copyright (C) 2015 Claudiu Olteanu * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -33,6 +34,9 @@ extern "C" { dc_status_t shearwater_predator_device_open (dc_device_t **device, dc_context_t *context, const char *name); +dc_status_t +shearwater_predator_device_custom_open (dc_device_t **device, dc_context_t *context, dc_serial_t *serial); + dc_status_t shearwater_predator_extract_dives (dc_device_t *device, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata); diff --git a/src/device.c b/src/device.c index 257dc75..fd6fe9b 100644 --- a/src/device.c +++ b/src/device.c @@ -188,6 +188,12 @@ dc_device_custom_open (dc_device_t **out, dc_context_t *context, dc_descriptor_t case DC_FAMILY_HW_OSTC3: rc = hw_ostc3_device_custom_open (&device, context, serial); break; + case DC_FAMILY_SHEARWATER_PREDATOR: + rc = shearwater_predator_device_custom_open (&device, context, serial); + break; + case DC_FAMILY_SHEARWATER_PETREL: + rc = shearwater_petrel_device_custom_open (&device, context, serial); + break; default: return DC_STATUS_INVALIDARGS; } diff --git a/src/shearwater_common.c b/src/shearwater_common.c index a4e94de..b12bf9b 100644 --- a/src/shearwater_common.c +++ b/src/shearwater_common.c @@ -45,7 +45,7 @@ shearwater_common_open (shearwater_common_device_t *device, dc_context_t *contex int rc = dc_serial_native_open (&device->serial, context, name); if (rc == -1) { ERROR (context, "Failed to open the serial port."); - return rc; + return DC_STATUS_IO; } // Set the serial communication protocol (115200 8N1). @@ -71,6 +71,37 @@ shearwater_common_open (shearwater_common_device_t *device, dc_context_t *contex } +dc_status_t +shearwater_common_custom_open (shearwater_common_device_t *device, dc_context_t *context, dc_serial_t *serial) +{ + // Set the serial reference + device->serial = serial; + + if (serial->type == DC_TRANSPORT_SERIAL) { + // Set the serial communication protocol (115200 8N1). + int rc = serial_configure (device->serial->port, 115200, 8, SERIAL_PARITY_NONE, 1, SERIAL_FLOWCONTROL_NONE); + if (rc == -1) { + ERROR (context, "Failed to set the terminal attributes."); + device->serial->ops->close (device->serial->port); + return DC_STATUS_IO; + } + } + + // Set the timeout for receiving data (3000ms). + if (serial_set_timeout (device->serial->port, 3000) == -1) { + ERROR (context, "Failed to set the timeout."); + device->serial->ops->close (device->serial->port); + return DC_STATUS_IO; + } + + // Make sure everything is in a sane state. + serial_sleep (device->serial->port, 300); + device->serial->ops->flush (device->serial->port, SERIAL_QUEUE_BOTH); + + return DC_STATUS_SUCCESS; +} + + dc_status_t shearwater_common_close (shearwater_common_device_t *device) { diff --git a/src/shearwater_common.h b/src/shearwater_common.h index b930fb9..8e4112f 100644 --- a/src/shearwater_common.h +++ b/src/shearwater_common.h @@ -42,6 +42,9 @@ typedef struct shearwater_common_device_t { dc_status_t shearwater_common_open (shearwater_common_device_t *device, dc_context_t *context, const char *name); +dc_status_t +shearwater_common_custom_open (shearwater_common_device_t *device, dc_context_t *context, dc_serial_t *serial); + dc_status_t shearwater_common_close (shearwater_common_device_t *device); diff --git a/src/shearwater_petrel.c b/src/shearwater_petrel.c index 3534a26..e33dc8f 100644 --- a/src/shearwater_petrel.c +++ b/src/shearwater_petrel.c @@ -2,6 +2,7 @@ * libdivecomputer * * Copyright (C) 2013 Jef Driesen + * Copyright (C) 2015 Claudiu Olteanu * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -110,6 +111,40 @@ shearwater_petrel_device_open (dc_device_t **out, dc_context_t *context, const c } +dc_status_t +shearwater_petrel_device_custom_open (dc_device_t **out, dc_context_t *context, dc_serial_t *serial) +{ + dc_status_t rc = DC_STATUS_SUCCESS; + + if (out == NULL || serial == NULL || serial->port == NULL) + return DC_STATUS_INVALIDARGS; + + // Allocate memory. + shearwater_petrel_device_t *device = (shearwater_petrel_device_t *) malloc (sizeof (shearwater_petrel_device_t)); + if (device == NULL) { + ERROR (context, "Failed to allocate memory."); + return DC_STATUS_NOMEMORY; + } + + // Initialize the base class. + device_init (&device->base.base, context, &shearwater_petrel_device_vtable); + + // Set the default values. + memset (device->fingerprint, 0, sizeof (device->fingerprint)); + + // Open the device. + rc = shearwater_common_custom_open (&device->base, context, serial); + if (rc != DC_STATUS_SUCCESS) { + free (device); + return rc; + } + + *out = (dc_device_t *) device; + + return DC_STATUS_SUCCESS; +} + + static dc_status_t shearwater_petrel_device_close (dc_device_t *abstract) { diff --git a/src/shearwater_predator.c b/src/shearwater_predator.c index e8a44ac..4901787 100644 --- a/src/shearwater_predator.c +++ b/src/shearwater_predator.c @@ -2,6 +2,7 @@ * libdivecomputer * * Copyright (C) 2012 Jef Driesen + * Copyright (C) 2015 Claudiu Olteanu * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -96,6 +97,40 @@ shearwater_predator_device_open (dc_device_t **out, dc_context_t *context, const } +dc_status_t +shearwater_predator_device_custom_open (dc_device_t **out, dc_context_t *context, dc_serial_t *serial) +{ + dc_status_t rc = DC_STATUS_SUCCESS; + + if (out == NULL || serial == NULL || serial->port == NULL) + return DC_STATUS_INVALIDARGS; + + // Allocate memory. + shearwater_predator_device_t *device = (shearwater_predator_device_t *) malloc (sizeof (shearwater_predator_device_t)); + if (device == NULL) { + ERROR (context, "Failed to allocate memory."); + return DC_STATUS_NOMEMORY; + } + + // Initialize the base class. + device_init (&device->base.base, context, &shearwater_predator_device_vtable); + + // Set the default values. + memset (device->fingerprint, 0, sizeof (device->fingerprint)); + + // Open the device. + rc = shearwater_common_custom_open (&device->base, context, serial); + if (rc != DC_STATUS_SUCCESS) { + free (device); + return rc; + } + + *out = (dc_device_t *) device; + + return DC_STATUS_SUCCESS; +} + + static dc_status_t shearwater_predator_device_close (dc_device_t *abstract) {