Hi, These patches create a generic way to represent any type of serial communication. They are used to create a custom serial implementation for HW_OSTC3 and SHEARWATER families.
Claudiu
From 05d830371f2bce35820bebb7419ed27946b3a658 Mon Sep 17 00:00:00 2001 From: Claudiu Olteanu <olteanu.clau...@ymail.com> Date: Sat, 27 Jun 2015 15:11:15 +0300 Subject: [PATCH 1/7] Extend the transport enum descriptor for serial communication Add a new transport type which can be used to identify Bluetooth serial communication. Signed-off-by Claudiu Oleanu <olteanu.clau...@ymail.com> --- include/libdivecomputer/descriptor.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/libdivecomputer/descriptor.h b/include/libdivecomputer/descriptor.h index 6f9735d..f1c815d 100644 --- a/include/libdivecomputer/descriptor.h +++ b/include/libdivecomputer/descriptor.h @@ -33,7 +33,8 @@ typedef enum dc_transport_t { DC_TRANSPORT_NONE, DC_TRANSPORT_SERIAL, DC_TRANSPORT_USB, - DC_TRANSPORT_IRDA + DC_TRANSPORT_IRDA, + DC_TRANSPORT_BLUETOOTH } dc_transport_t; typedef struct dc_descriptor_t dc_descriptor_t; -- 2.1.4
From b4092f3b4b04f9b31097f6c79b2e1cdafcaf5a06 Mon Sep 17 00:00:00 2001 From: Claudiu Olteanu <olteanu.clau...@ymail.com> Date: Sat, 27 Jun 2015 15:14:16 +0300 Subject: [PATCH 2/7] Create a generic way to represent any type of serial communication Add a structure which holds references to basic operations on a serial communication. This can be used to pass a set of function pointer callbacks in order to create a custom implementation for serial communication. Add a generic structure to represent the needed information for a serial communication. Implement the initialization method where the user can pass a set of function pointer callbacks and set some custom data for the serial device. Create open method for the native serial implementation. Signed-off-by: Claudiu Olteanu <olteanu.clau...@ymail.com> --- include/libdivecomputer/Makefile.am | 3 +- include/libdivecomputer/custom_serial.h | 63 ++++++++++++++++++++++++++ src/Makefile.am | 3 +- src/custom_serial.c | 79 +++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 include/libdivecomputer/custom_serial.h create mode 100644 src/custom_serial.c diff --git a/include/libdivecomputer/Makefile.am b/include/libdivecomputer/Makefile.am index 71887d5..a0ed196 100644 --- a/include/libdivecomputer/Makefile.am +++ b/include/libdivecomputer/Makefile.am @@ -53,4 +53,5 @@ libdivecomputer_HEADERS = \ citizen.h \ citizen_aqualand.h \ divesystem.h \ - divesystem_idive.h + divesystem_idive.h \ + custom_serial.h diff --git a/include/libdivecomputer/custom_serial.h b/include/libdivecomputer/custom_serial.h new file mode 100644 index 0000000..a52d49b --- /dev/null +++ b/include/libdivecomputer/custom_serial.h @@ -0,0 +1,63 @@ +/* + * libdivecomputer + * + * Copyright (C) 2015 Claudiu Olteanu + * base on code that is Copyright (C) 2008 Jef Driesen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#ifndef CUSTOM_SERIAL_H +#define CUSTOM_SERIAL_H + +#include <string.h> +#include <stdlib.h> + +#include "context.h" +#include "descriptor.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef struct serial_t serial_t; + +typedef struct dc_serial_operations_t +{ + int (*open) (serial_t **device, dc_context_t *context, const char *name); + int (*close) (serial_t *device); + int (*read) (serial_t *device, void* data, unsigned int size); + int (*write) (serial_t *device, const void* data, unsigned int size); + int (*flush) (serial_t *device, int queue); + int (*get_received) (serial_t *device); + int (*get_transmitted) (serial_t *device); +} dc_serial_operations_t; + +typedef struct dc_serial_t { + serial_t *port; //serial device port + dc_transport_t type; //the type of the transport (USB, SERIAL, IRDA, BLUETOOTH) + void *data; //specific data for serial device + const dc_serial_operations_t *ops; //reference to a custom set of operations +} dc_serial_t; + +void dc_serial_init(dc_serial_t *device, void *data, const dc_serial_operations_t *ops); + +dc_status_t dc_serial_native_open(dc_serial_t **serial, dc_context_t *context, const char *devname); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* CUSTOM_SERIAL_H */ diff --git a/src/Makefile.am b/src/Makefile.am index abbaa48..0c60d02 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -59,7 +59,8 @@ libdivecomputer_la_SOURCES = \ ringbuffer.h ringbuffer.c \ checksum.h checksum.c \ array.h array.c \ - buffer.c + buffer.c \ + custom_serial.c if OS_WIN32 libdivecomputer_la_SOURCES += serial.h serial_win32.c diff --git a/src/custom_serial.c b/src/custom_serial.c new file mode 100644 index 0000000..6e024b2 --- /dev/null +++ b/src/custom_serial.c @@ -0,0 +1,79 @@ +/* + * libdivecomputer + * + * Copyright (C) 2015 Claudiu Olteanu + * base on code that is Copyright (C) 2008 Jef Driesen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#include <libdivecomputer/custom_serial.h> +#include <serial.h> + +#include "context-private.h" + +const dc_serial_operations_t native_serial_ops = { + .open = serial_open, + .close = serial_close, + .read = serial_read, + .write = serial_write, + .flush = serial_flush, + .get_received = serial_get_received, + .get_transmitted = serial_get_transmitted +}; + + +void +dc_serial_init(dc_serial_t *device, void *data, const dc_serial_operations_t *ops) +{ + memset(device, 0, sizeof (*device)); + device->data = data; + device->ops = ops; +} + + +dc_status_t +dc_serial_native_open(dc_serial_t **out, dc_context_t *context, const char *devname) +{ + if (out == NULL) + return DC_STATUS_INVALIDARGS; + + // Allocate memory. + dc_serial_t *serial_device = (dc_serial_t *) malloc (sizeof (dc_serial_t)); + + if (serial_device == NULL) { + ERROR (context, "Failed to allocate memory."); + return DC_STATUS_NOMEMORY; + } + + // Initialize data and function pointers + dc_serial_init(serial_device, NULL, &native_serial_ops); + + // Open the serial device. + int rc = serial_open (&serial_device->port, context, devname); + if (rc == -1) { + ERROR (context, "Failed to open the serial port."); + free (serial_device); + return DC_STATUS_IO; + } + + // Set the type of the device + serial_device->type = DC_TRANSPORT_SERIAL; + + *out = serial_device; + + return DC_STATUS_SUCCESS; +} -- 2.1.4
From 803984baaf0f91020b4344bcdd5f801d1dad3e0f Mon Sep 17 00:00:00 2001 From: Claudiu Olteanu <olteanu.clau...@ymail.com> Date: Sat, 27 Jun 2015 15:18:01 +0300 Subject: [PATCH 3/7] Use the dc_serial_t structure in HW OSTC family 3 Open a native serial device and use it in the HW OSTC3 implementation. This patch replaces the old serial structure with the new one, which can be used for custom serial implementations. Signed-off-by: Claudiu Olteanu <olteanu.clau...@ymail.com> --- include/libdivecomputer/hw_ostc3.h | 2 ++ src/hw_ostc3.c | 41 +++++++++++++++++++------------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/include/libdivecomputer/hw_ostc3.h b/include/libdivecomputer/hw_ostc3.h index c60dc63..454d45c 100644 --- a/include/libdivecomputer/hw_ostc3.h +++ b/include/libdivecomputer/hw_ostc3.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 @@ -26,6 +27,7 @@ #include "device.h" #include "parser.h" #include "buffer.h" +#include "custom_serial.h" #ifdef __cplusplus extern "C" { diff --git a/src/hw_ostc3.c b/src/hw_ostc3.c index a221fb2..a55c5cc 100644 --- a/src/hw_ostc3.c +++ b/src/hw_ostc3.c @@ -3,6 +3,7 @@ * * Copyright (C) 2013 Jef Driesen * Copyright (C) 2014 Anton Lundin + * 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 @@ -88,7 +89,7 @@ typedef enum hw_ostc3_state_t { typedef struct hw_ostc3_device_t { dc_device_t base; - serial_t *port; + dc_serial_t *serial; unsigned char fingerprint[5]; hw_ostc3_state_t state; } hw_ostc3_device_t; @@ -163,7 +164,7 @@ hw_ostc3_transfer (hw_ostc3_device_t *device, // Send the command. unsigned char command[1] = {cmd}; - int n = serial_write (device->port, command, sizeof (command)); + int n = device->serial->ops->write (device->serial->port, command, sizeof (command)); if (n != sizeof (command)) { ERROR (abstract->context, "Failed to send the command."); return EXITCODE (n); @@ -171,7 +172,7 @@ hw_ostc3_transfer (hw_ostc3_device_t *device, // Read the echo. unsigned char echo[1] = {0}; - n = serial_read (device->port, echo, sizeof (echo)); + n = device->serial->ops->read (device->serial->port, echo, sizeof (echo)); if (n != sizeof (echo)) { ERROR (abstract->context, "Failed to receive the echo."); return EXITCODE (n); @@ -190,7 +191,7 @@ hw_ostc3_transfer (hw_ostc3_device_t *device, if (input) { // Send the input data packet. - n = serial_write (device->port, input, isize); + n = device->serial->ops->write (device->serial->port, input, isize); if (n != isize) { ERROR (abstract->context, "Failed to send the data packet."); return EXITCODE (n); @@ -204,7 +205,7 @@ hw_ostc3_transfer (hw_ostc3_device_t *device, unsigned int len = 1024; // Increase the packet size if more data is immediately available. - int available = serial_get_received (device->port); + int available = device->serial->ops->get_received (device->serial->port); if (available > len) len = available; @@ -213,7 +214,7 @@ hw_ostc3_transfer (hw_ostc3_device_t *device, len = osize - nbytes; // Read the packet. - n = serial_read (device->port, output + nbytes, len); + n = device->serial->ops->read (device->serial->port, output + nbytes, len); if (n != len) { ERROR (abstract->context, "Failed to receive the answer."); return EXITCODE (n); @@ -232,7 +233,7 @@ hw_ostc3_transfer (hw_ostc3_device_t *device, if (cmd != EXIT) { // Read the ready byte. unsigned char answer[1] = {0}; - n = serial_read (device->port, answer, sizeof (answer)); + n = device->serial->ops->read (device->serial->port, answer, sizeof (answer)); if (n != sizeof (answer)) { ERROR (abstract->context, "Failed to receive the ready byte."); return EXITCODE (n); @@ -266,11 +267,11 @@ hw_ostc3_device_open (dc_device_t **out, dc_context_t *context, const char *name device_init (&device->base, context, &hw_ostc3_device_vtable); // Set the default values. - device->port = NULL; + device->serial = NULL; memset (device->fingerprint, 0, sizeof (device->fingerprint)); // Open the device. - int rc = serial_open (&device->port, context, name); + int rc = dc_serial_native_open (&device->serial, context, name); if (rc == -1) { ERROR (context, "Failed to open the serial port."); free (device); @@ -278,25 +279,25 @@ hw_ostc3_device_open (dc_device_t **out, dc_context_t *context, const char *name } // Set the serial communication protocol (115200 8N1). - rc = serial_configure (device->port, 115200, 8, SERIAL_PARITY_NONE, 1, SERIAL_FLOWCONTROL_NONE); + 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."); - serial_close (device->port); + device->serial->ops->close (device->serial->port); free (device); return DC_STATUS_IO; } // Set the timeout for receiving data (3000ms). - if (serial_set_timeout (device->port, 3000) == -1) { + if (serial_set_timeout (device->serial->port, 3000) == -1) { ERROR (context, "Failed to set the timeout."); - serial_close (device->port); + device->serial->ops->close (device->serial->port); free (device); return DC_STATUS_IO; } // Make sure everything is in a sane state. - serial_sleep (device->port, 300); - serial_flush (device->port, SERIAL_QUEUE_BOTH); + serial_sleep (device->serial->port, 300); + device->serial->ops->flush (device->serial->port, SERIAL_QUEUE_BOTH); device->state = OPEN; @@ -336,17 +337,17 @@ hw_ostc3_device_init_service (hw_ostc3_device_t *device) int n = 0; // We cant use hw_ostc3_transfer here, due to the different echos - n = serial_write (device->port, command, sizeof (command)); + n = device->serial->ops->write (device->serial->port, command, sizeof (command)); if (n != sizeof (command)) { ERROR (context, "Failed to send the command."); return EXITCODE (n); } // Give the device some time to enter service mode - serial_sleep (device->port, 100); + serial_sleep (device->serial->port, 100); // Read the response - n = serial_read (device->port, output, sizeof (output)); + n = device->serial->ops->read (device->serial->port, output, sizeof (output)); if (n != sizeof (output)) { ERROR (context, "Failed to receive the echo."); return EXITCODE (n); @@ -408,14 +409,14 @@ hw_ostc3_device_close (dc_device_t *abstract) rc = hw_ostc3_transfer (device, NULL, EXIT, NULL, 0, NULL, 0); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to send the command."); - serial_close (device->port); + device->serial->ops->close (device->serial->port); free (device); return rc; } } // Close the device. - if (serial_close (device->port) == -1) { + if (device->serial->ops->close (device->serial->port) == -1) { free (device); return DC_STATUS_IO; } -- 2.1.4
From c9dfb26aca258672ceac788ceecb809f183db93c Mon Sep 17 00:00:00 2001 From: Claudiu Olteanu <olteanu.clau...@ymail.com> Date: Sat, 27 Jun 2015 15:21:19 +0300 Subject: [PATCH 4/7] Implement custom open method for HW OSTC 3 family Create a custom open method for HW OSTC3 family. This method can be used to pass a reference to a dc_serial_t structure. In this way the applications can implement their own implementation for a serial communication and set their callbacks for the basic serial functions. Signed-off-by: Claudiu Olteanu <olteanu.clau...@ymail.com> --- include/libdivecomputer/hw_ostc3.h | 3 ++ src/hw_ostc3.c | 57 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/include/libdivecomputer/hw_ostc3.h b/include/libdivecomputer/hw_ostc3.h index 454d45c..8463cb2 100644 --- a/include/libdivecomputer/hw_ostc3.h +++ b/include/libdivecomputer/hw_ostc3.h @@ -40,6 +40,9 @@ dc_status_t hw_ostc3_device_open (dc_device_t **device, dc_context_t *context, const char *name); dc_status_t +hw_ostc3_device_custom_open (dc_device_t **device, dc_context_t *context, dc_serial_t *serial); + +dc_status_t hw_ostc3_device_version (dc_device_t *device, unsigned char data[], unsigned int size); dc_status_t diff --git a/src/hw_ostc3.c b/src/hw_ostc3.c index a55c5cc..2d975b7 100644 --- a/src/hw_ostc3.c +++ b/src/hw_ostc3.c @@ -272,10 +272,10 @@ hw_ostc3_device_open (dc_device_t **out, dc_context_t *context, const char *name // Open the device. int rc = dc_serial_native_open (&device->serial, context, name); - if (rc == -1) { + if (rc != DC_STATUS_SUCCESS) { ERROR (context, "Failed to open the serial port."); free (device); - return DC_STATUS_IO; + return rc; } // Set the serial communication protocol (115200 8N1). @@ -307,6 +307,59 @@ hw_ostc3_device_open (dc_device_t **out, dc_context_t *context, const char *name } +dc_status_t +hw_ostc3_device_custom_open (dc_device_t **out, dc_context_t *context, dc_serial_t *serial) +{ + if (out == NULL || serial == NULL || serial->port == NULL) + return DC_STATUS_INVALIDARGS; + + // Allocate memory. + hw_ostc3_device_t *device = (hw_ostc3_device_t *) malloc (sizeof (hw_ostc3_device_t)); + if (device == NULL) { + ERROR (context, "Failed to allocate memory."); + return DC_STATUS_NOMEMORY; + } + + // Initialize the base class. + device_init (&device->base, context, &hw_ostc3_device_vtable); + + // Set the default values. + memset (device->fingerprint, 0, sizeof (device->fingerprint)); + + // 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); + free (device); + 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); + free (device); + 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); + + device->state = OPEN; + + *out = (dc_device_t *) device; + + return DC_STATUS_SUCCESS; +} + + static dc_status_t hw_ostc3_device_init_download (hw_ostc3_device_t *device) { -- 2.1.4
From e8a132a921f8aac846c9e21cd7032e174f441ee8 Mon Sep 17 00:00:00 2001 From: Claudiu Olteanu <olteanu.clau...@ymail.com> Date: Sat, 27 Jun 2015 15:22:04 +0300 Subject: [PATCH 5/7] Implement custom device open method This method can be used by external applications to open a device and to pass their custom implementation for the serial communication. Signed-off-by: Claudiu Olteanu <olteanu.clau...@ymail.com> --- include/libdivecomputer/device.h | 5 +++++ src/device.c | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/libdivecomputer/device.h b/include/libdivecomputer/device.h index 7ba4bd6..60590b5 100644 --- a/include/libdivecomputer/device.h +++ b/include/libdivecomputer/device.h @@ -2,6 +2,7 @@ * libdivecomputer * * Copyright (C) 2008 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 @@ -27,6 +28,7 @@ #include "descriptor.h" #include "buffer.h" #include "datetime.h" +#include "custom_serial.h" #ifdef __cplusplus extern "C" { @@ -72,6 +74,9 @@ typedef int (*dc_dive_callback_t) (const unsigned char *data, unsigned int size, dc_status_t dc_device_open (dc_device_t **out, dc_context_t *context, dc_descriptor_t *descriptor, const char *name); +dc_status_t +dc_device_custom_open (dc_device_t **out, dc_context_t *context, dc_descriptor_t *descriptor, dc_serial_t *serial); + dc_family_t dc_device_get_type (dc_device_t *device); diff --git a/src/device.c b/src/device.c index d95585d..257dc75 100644 --- a/src/device.c +++ b/src/device.c @@ -2,6 +2,7 @@ * libdivecomputer * * Copyright (C) 2008 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 @@ -174,6 +175,27 @@ dc_device_open (dc_device_t **out, dc_context_t *context, dc_descriptor_t *descr return rc; } +dc_status_t +dc_device_custom_open (dc_device_t **out, dc_context_t *context, dc_descriptor_t *descriptor, dc_serial_t *serial) +{ + dc_status_t rc = DC_STATUS_SUCCESS; + dc_device_t *device = NULL; + + if (out == NULL || descriptor == NULL || serial == NULL) + return DC_STATUS_INVALIDARGS; + + switch (dc_descriptor_get_type (descriptor)) { + case DC_FAMILY_HW_OSTC3: + rc = hw_ostc3_device_custom_open (&device, context, serial); + break; + default: + return DC_STATUS_INVALIDARGS; + } + + *out = device; + + return rc; +} int dc_device_isinstance (dc_device_t *device, const dc_device_vtable_t *vtable) -- 2.1.4
From e22c14994f1a58d2d258463163ceceb961b94b4b Mon Sep 17 00:00:00 2001 From: Claudiu Olteanu <olteanu.clau...@ymail.com> Date: Sun, 28 Jun 2015 23:46:56 +0300 Subject: [PATCH 6/7] Use the dc_serial_t structure in SHEARWATER family Use the new structure in the SHEARWATER family implementation. This patch opens a native serial device and use it for the serial communication. Also the patch uses the set of callback functions saved in the dc_serial_t structure. Signed-off-by: Claudiu Olteanu <olteanu.clau...@ymail.com> --- src/shearwater_common.c | 29 +++++++++++++++-------------- src/shearwater_common.h | 4 +++- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/shearwater_common.c b/src/shearwater_common.c index b809a3c..a4e94de 100644 --- a/src/shearwater_common.c +++ b/src/shearwater_common.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 @@ -41,30 +42,30 @@ dc_status_t shearwater_common_open (shearwater_common_device_t *device, dc_context_t *context, const char *name) { // Open the device. - int rc = serial_open (&device->port, context, name); + int rc = dc_serial_native_open (&device->serial, context, name); if (rc == -1) { ERROR (context, "Failed to open the serial port."); - return DC_STATUS_IO; + return rc; } // Set the serial communication protocol (115200 8N1). - rc = serial_configure (device->port, 115200, 8, SERIAL_PARITY_NONE, 1, SERIAL_FLOWCONTROL_NONE); + 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."); - serial_close (device->port); + device->serial->ops->close (device->serial->port); return DC_STATUS_IO; } // Set the timeout for receiving data (3000ms). - if (serial_set_timeout (device->port, 3000) == -1) { + if (serial_set_timeout (device->serial->port, 3000) == -1) { ERROR (context, "Failed to set the timeout."); - serial_close (device->port); + device->serial->ops->close (device->serial->port); return DC_STATUS_IO; } // Make sure everything is in a sane state. - serial_sleep (device->port, 300); - serial_flush (device->port, SERIAL_QUEUE_BOTH); + serial_sleep (device->serial->port, 300); + device->serial->ops->flush (device->serial->port, SERIAL_QUEUE_BOTH); return DC_STATUS_SUCCESS; } @@ -74,7 +75,7 @@ dc_status_t shearwater_common_close (shearwater_common_device_t *device) { // Close the device. - if (serial_close (device->port) == -1) { + if (device->serial->ops->close (device->serial->port) == -1) { return DC_STATUS_IO; } @@ -154,7 +155,7 @@ shearwater_common_slip_write (shearwater_common_device_t *device, const unsigned #if 0 // Send an initial END character to flush out any data that may have // accumulated in the receiver due to line noise. - n = serial_write (device->port, end, sizeof (end)); + n = device->serial->ops->write (device->serial->port, end, sizeof (end)); if (n != sizeof (end)) { return EXITCODE(n); } @@ -183,7 +184,7 @@ shearwater_common_slip_write (shearwater_common_device_t *device, const unsigned // Flush the buffer if necessary. if (nbytes + len + sizeof(end) > sizeof(buffer)) { - n = serial_write (device->port, buffer, nbytes); + n = device->serial->ops->write (device->serial->port, buffer, nbytes); if (n != nbytes) { return EXITCODE(n); } @@ -201,7 +202,7 @@ shearwater_common_slip_write (shearwater_common_device_t *device, const unsigned nbytes += sizeof(end); // Flush the buffer. - n = serial_write (device->port, buffer, nbytes); + n = device->serial->ops->write (device->serial->port, buffer, nbytes); if (n != nbytes) { return EXITCODE(n); } @@ -224,7 +225,7 @@ shearwater_common_slip_read (shearwater_common_device_t *device, unsigned char d int n = 0; // Get a single character to process. - n = serial_read (device->port, &c, 1); + n = device->serial->ops->read (device->serial->port, &c, 1); if (n != 1) { return EXITCODE(n); } @@ -243,7 +244,7 @@ shearwater_common_slip_read (shearwater_common_device_t *device, unsigned char d case ESC: // If it's an ESC character, get another character and then // figure out what to store in the packet based on that. - n = serial_read (device->port, &c, 1); + n = device->serial->ops->read (device->serial->port, &c, 1); if (n != 1) { return EXITCODE(n); } diff --git a/src/shearwater_common.h b/src/shearwater_common.h index a07de14..b930fb9 100644 --- a/src/shearwater_common.h +++ b/src/shearwater_common.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 @@ -24,6 +25,7 @@ #include "device-private.h" #include "serial.h" +#include "libdivecomputer/custom_serial.h" #ifdef __cplusplus extern "C" { @@ -34,7 +36,7 @@ extern "C" { typedef struct shearwater_common_device_t { dc_device_t base; - serial_t *port; + dc_serial_t *serial; } shearwater_common_device_t; dc_status_t -- 2.1.4
From b50db86e3f0da31aedfe3d2afbddcd94ba2c8a39 Mon Sep 17 00:00:00 2001 From: Claudiu Olteanu <olteanu.clau...@ymail.com> Date: Mon, 29 Jun 2015 00:08:20 +0300 Subject: [PATCH 7/7] Implement custom open device method for SHEARWATER family Create a custom open method for SHEARWATER family. This method can be used to pass a reference to a dc_serial_t structure. In this way the applications can implement their own implementation for a serial communication and set their callbacks for the basic serial functions. Signed-off-by: Claudiu Olteanu <olteanu.clau...@ymail.com> --- include/libdivecomputer/shearwater_petrel.h | 4 +++ include/libdivecomputer/shearwater_predator.h | 4 +++ src/device.c | 6 +++++ src/shearwater_common.c | 33 ++++++++++++++++++++++++- src/shearwater_common.h | 3 +++ src/shearwater_petrel.c | 35 +++++++++++++++++++++++++++ src/shearwater_predator.c | 35 +++++++++++++++++++++++++++ 7 files changed, 119 insertions(+), 1 deletion(-) 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 @@ -34,6 +35,9 @@ 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); #ifdef __cplusplus 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 @@ -34,6 +35,9 @@ 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); dc_status_t 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). @@ -72,6 +72,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) { // Close the 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 @@ -43,6 +43,9 @@ 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); dc_status_t 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) { -- 2.1.4
_______________________________________________ subsurface mailing list subsurface@subsurface-divelog.org http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface