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.claudiu@ymail.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
53ccc4f43b
commit
0c8886ce70
@ -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);
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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)
|
||||
{
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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)
|
||||
{
|
||||
|
||||
@ -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)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user