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.claudiu@ymail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Claudiu Olteanu 2015-06-28 23:46:56 +03:00 committed by Dirk Hohndel
parent 8c3e44aa0c
commit 53ccc4f43b
2 changed files with 18 additions and 15 deletions

View File

@ -2,6 +2,7 @@
* libdivecomputer * libdivecomputer
* *
* Copyright (C) 2013 Jef Driesen * Copyright (C) 2013 Jef Driesen
* Copyright (C) 2015 Claudiu Olteanu
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * 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) shearwater_common_open (shearwater_common_device_t *device, dc_context_t *context, const char *name)
{ {
// Open the device. // Open the device.
int rc = serial_open (&device->port, context, name); int rc = dc_serial_native_open (&device->serial, context, name);
if (rc == -1) { if (rc == -1) {
ERROR (context, "Failed to open the serial port."); ERROR (context, "Failed to open the serial port.");
return DC_STATUS_IO; return rc;
} }
// Set the serial communication protocol (115200 8N1). // 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) { if (rc == -1) {
ERROR (context, "Failed to set the terminal attributes."); ERROR (context, "Failed to set the terminal attributes.");
serial_close (device->port); device->serial->ops->close (device->serial->port);
return DC_STATUS_IO; return DC_STATUS_IO;
} }
// Set the timeout for receiving data (3000ms). // 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."); ERROR (context, "Failed to set the timeout.");
serial_close (device->port); device->serial->ops->close (device->serial->port);
return DC_STATUS_IO; return DC_STATUS_IO;
} }
// Make sure everything is in a sane state. // Make sure everything is in a sane state.
serial_sleep (device->port, 300); serial_sleep (device->serial->port, 300);
serial_flush (device->port, SERIAL_QUEUE_BOTH); device->serial->ops->flush (device->serial->port, SERIAL_QUEUE_BOTH);
return DC_STATUS_SUCCESS; return DC_STATUS_SUCCESS;
} }
@ -74,7 +75,7 @@ dc_status_t
shearwater_common_close (shearwater_common_device_t *device) shearwater_common_close (shearwater_common_device_t *device)
{ {
// Close the device. // Close the device.
if (serial_close (device->port) == -1) { if (device->serial->ops->close (device->serial->port) == -1) {
return DC_STATUS_IO; return DC_STATUS_IO;
} }
@ -154,7 +155,7 @@ shearwater_common_slip_write (shearwater_common_device_t *device, const unsigned
#if 0 #if 0
// Send an initial END character to flush out any data that may have // Send an initial END character to flush out any data that may have
// accumulated in the receiver due to line noise. // 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)) { if (n != sizeof (end)) {
return EXITCODE(n); return EXITCODE(n);
} }
@ -183,7 +184,7 @@ shearwater_common_slip_write (shearwater_common_device_t *device, const unsigned
// Flush the buffer if necessary. // Flush the buffer if necessary.
if (nbytes + len + sizeof(end) > sizeof(buffer)) { 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) { if (n != nbytes) {
return EXITCODE(n); return EXITCODE(n);
} }
@ -201,7 +202,7 @@ shearwater_common_slip_write (shearwater_common_device_t *device, const unsigned
nbytes += sizeof(end); nbytes += sizeof(end);
// Flush the buffer. // Flush the buffer.
n = serial_write (device->port, buffer, nbytes); n = device->serial->ops->write (device->serial->port, buffer, nbytes);
if (n != nbytes) { if (n != nbytes) {
return EXITCODE(n); return EXITCODE(n);
} }
@ -224,7 +225,7 @@ shearwater_common_slip_read (shearwater_common_device_t *device, unsigned char d
int n = 0; int n = 0;
// Get a single character to process. // 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) { if (n != 1) {
return EXITCODE(n); return EXITCODE(n);
} }
@ -243,7 +244,7 @@ shearwater_common_slip_read (shearwater_common_device_t *device, unsigned char d
case ESC: case ESC:
// If it's an ESC character, get another character and then // If it's an ESC character, get another character and then
// figure out what to store in the packet based on that. // 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) { if (n != 1) {
return EXITCODE(n); return EXITCODE(n);
} }

View File

@ -2,6 +2,7 @@
* libdivecomputer * libdivecomputer
* *
* Copyright (C) 2013 Jef Driesen * Copyright (C) 2013 Jef Driesen
* Copyright (C) 2015 Claudiu Olteanu
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -24,6 +25,7 @@
#include "device-private.h" #include "device-private.h"
#include "serial.h" #include "serial.h"
#include "libdivecomputer/custom_serial.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -34,7 +36,7 @@ extern "C" {
typedef struct shearwater_common_device_t { typedef struct shearwater_common_device_t {
dc_device_t base; dc_device_t base;
serial_t *port; dc_serial_t *serial;
} shearwater_common_device_t; } shearwater_common_device_t;
dc_status_t dc_status_t