2017-04-27 18:24:53 +00:00
|
|
|
// SPDX-License-Identifier: LGPL-2.1+
|
2015-08-20 22:19:41 +00:00
|
|
|
/*
|
|
|
|
* libdivecomputer
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Jef Driesen
|
2015-08-20 22:19:44 +00:00
|
|
|
* Copyright (C) 2014 Venkatesh Shukla
|
2016-09-17 15:27:56 +00:00
|
|
|
* Copyright (C) 2015-2016 Anton Lundin
|
2015-08-20 22:19:41 +00:00
|
|
|
*
|
|
|
|
* 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 <stdlib.h> // malloc, free
|
|
|
|
#include <string.h> // strerror
|
|
|
|
#include <errno.h> // errno
|
|
|
|
#include <sys/time.h> // gettimeofday
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <libusb.h>
|
|
|
|
#include <ftdi.h>
|
|
|
|
|
2018-09-12 00:27:05 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h> // Sleep
|
|
|
|
#else
|
|
|
|
#include <time.h> // nanosleep
|
|
|
|
#endif
|
|
|
|
|
2015-08-20 22:19:44 +00:00
|
|
|
#ifndef __ANDROID__
|
|
|
|
#define INFO(context, fmt, ...) fprintf(stderr, "INFO: " fmt "\n", ##__VA_ARGS__)
|
|
|
|
#define ERROR(context, fmt, ...) fprintf(stderr, "ERROR: " fmt "\n", ##__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#include <android/log.h>
|
|
|
|
#define INFO(context, fmt, ...) __android_log_print(ANDROID_LOG_DEBUG, __FILE__, "INFO: " fmt "\n", ##__VA_ARGS__)
|
|
|
|
#define ERROR(context, fmt, ...) __android_log_print(ANDROID_LOG_DEBUG, __FILE__, "ERROR: " fmt "\n", ##__VA_ARGS__)
|
|
|
|
#endif
|
|
|
|
//#define SYSERROR(context, errcode) ERROR(__FILE__ ":" __LINE__ ": %s", strerror(errcode))
|
|
|
|
#define SYSERROR(context, errcode) ;
|
|
|
|
|
2017-12-13 00:14:14 +00:00
|
|
|
#include "libdivecomputer.h"
|
2016-09-17 15:27:56 +00:00
|
|
|
#include <libdivecomputer/context.h>
|
2018-04-17 01:14:59 +00:00
|
|
|
#include <libdivecomputer/custom.h>
|
2015-08-20 22:19:41 +00:00
|
|
|
|
|
|
|
#define VID 0x0403 // Vendor ID of FTDI
|
|
|
|
|
2016-09-17 15:27:56 +00:00
|
|
|
typedef struct ftdi_serial_t {
|
2015-08-20 22:19:44 +00:00
|
|
|
/* Library context. */
|
2015-08-20 22:19:43 +00:00
|
|
|
dc_context_t *context;
|
|
|
|
/*
|
|
|
|
* The file descriptor corresponding to the serial port.
|
|
|
|
* Also a libftdi_ftdi_ctx could be used?
|
|
|
|
*/
|
|
|
|
struct ftdi_context *ftdi_ctx;
|
|
|
|
long timeout;
|
|
|
|
/*
|
|
|
|
* Serial port settings are saved into this variable immediately
|
|
|
|
* after the port is opened. These settings are restored when the
|
|
|
|
* serial port is closed.
|
2015-08-20 22:19:41 +00:00
|
|
|
* Saving this using libftdi context or libusb. Search further.
|
|
|
|
* Custom implementation using libftdi functions could be done.
|
2015-08-20 22:19:43 +00:00
|
|
|
*/
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
unsigned int baudrate;
|
|
|
|
unsigned int nbits;
|
2017-09-05 21:07:57 +00:00
|
|
|
unsigned int databits;
|
|
|
|
unsigned int stopbits;
|
|
|
|
unsigned int parity;
|
2016-09-17 15:27:56 +00:00
|
|
|
} ftdi_serial_t;
|
2015-08-20 22:19:44 +00:00
|
|
|
|
2018-04-17 01:14:59 +00:00
|
|
|
static dc_status_t serial_ftdi_get_received (void *io, size_t *value)
|
2015-08-20 22:19:44 +00:00
|
|
|
{
|
2018-04-17 01:14:59 +00:00
|
|
|
ftdi_serial_t *device = io;
|
2016-09-17 15:27:56 +00:00
|
|
|
|
2015-08-20 22:19:44 +00:00
|
|
|
if (device == NULL)
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_INVALIDARGS;
|
2015-08-20 22:19:44 +00:00
|
|
|
|
|
|
|
// Direct access is not encouraged. But function implementation
|
|
|
|
// is not available. The return quantity might be anything.
|
|
|
|
// Find out further about its possible values and correct way of
|
|
|
|
// access.
|
2016-09-17 15:27:56 +00:00
|
|
|
*value = device->ftdi_ctx->readbuffer_remaining;
|
2015-08-20 22:19:44 +00:00
|
|
|
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_SUCCESS;
|
2015-08-20 22:19:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 15:27:56 +00:00
|
|
|
static dc_status_t serial_ftdi_get_transmitted (ftdi_serial_t *device)
|
2015-08-20 22:19:44 +00:00
|
|
|
{
|
|
|
|
if (device == NULL)
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_INVALIDARGS;
|
2015-08-20 22:19:44 +00:00
|
|
|
|
|
|
|
// This is not possible using libftdi. Look further into it.
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_UNSUPPORTED;
|
2015-08-20 22:19:44 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 19:41:21 +00:00
|
|
|
/*
|
|
|
|
* Get an msec value on some random base
|
|
|
|
*/
|
|
|
|
static unsigned int serial_ftdi_get_msec(void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return GetTickCount();
|
|
|
|
#else
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
Make sure our libdivecomputer custom IO interfaces have sleep functions
When I switched over from our own custom IO implementation to the new
upstream custom IO model in libdivecomputer, I completely missed the
fact that the libdivecomputer custom IO model also does a custom _sleep_
function.
I'm not entirely sure what the point was, and it broke things even in
libdivecopmputer itself when some of the new sleep functions were
broken.
Anyway, we didn't export any sleep functions at all for the bluetooth,
BLE and FTDI cases, the the libdivecomputer code didn't fall back to any
sane default sleep implementation either, so the end result was no
sleeping at all.
Which didn't matter for most divecomputers.
But it seems like at least some OSTC dive computers did care, at least
in certain situations, and both Miika and Anton had trouble downloading
with their OSTC Sport dive computers. Using the serial line protocol
and the legacy /dev/rfcomm model worked fine, because then it used the
sleeping functions in the POSIX serial code inside libdivecomputer.
This just adds trivial sleeping functions for the affected download
protocols. Maybe I should have just made libdivecomputer have a sane
default instead, but this wasn't hard either (the hard part was trying
to figure out why the downloads worked for some people and not for
others).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-07-13 16:49:42 +00:00
|
|
|
static dc_status_t serial_ftdi_sleep (void *io, unsigned int timeout)
|
2015-08-20 22:19:44 +00:00
|
|
|
{
|
Make sure our libdivecomputer custom IO interfaces have sleep functions
When I switched over from our own custom IO implementation to the new
upstream custom IO model in libdivecomputer, I completely missed the
fact that the libdivecomputer custom IO model also does a custom _sleep_
function.
I'm not entirely sure what the point was, and it broke things even in
libdivecopmputer itself when some of the new sleep functions were
broken.
Anyway, we didn't export any sleep functions at all for the bluetooth,
BLE and FTDI cases, the the libdivecomputer code didn't fall back to any
sane default sleep implementation either, so the end result was no
sleeping at all.
Which didn't matter for most divecomputers.
But it seems like at least some OSTC dive computers did care, at least
in certain situations, and both Miika and Anton had trouble downloading
with their OSTC Sport dive computers. Using the serial line protocol
and the legacy /dev/rfcomm model worked fine, because then it used the
sleeping functions in the POSIX serial code inside libdivecomputer.
This just adds trivial sleeping functions for the affected download
protocols. Maybe I should have just made libdivecomputer have a sane
default instead, but this wasn't hard either (the hard part was trying
to figure out why the downloads worked for some people and not for
others).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-07-13 16:49:42 +00:00
|
|
|
ftdi_serial_t *device = io;
|
|
|
|
|
2015-08-20 22:19:44 +00:00
|
|
|
if (device == NULL)
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_INVALIDARGS;
|
2015-08-20 22:19:44 +00:00
|
|
|
|
Make sure our libdivecomputer custom IO interfaces have sleep functions
When I switched over from our own custom IO implementation to the new
upstream custom IO model in libdivecomputer, I completely missed the
fact that the libdivecomputer custom IO model also does a custom _sleep_
function.
I'm not entirely sure what the point was, and it broke things even in
libdivecopmputer itself when some of the new sleep functions were
broken.
Anyway, we didn't export any sleep functions at all for the bluetooth,
BLE and FTDI cases, the the libdivecomputer code didn't fall back to any
sane default sleep implementation either, so the end result was no
sleeping at all.
Which didn't matter for most divecomputers.
But it seems like at least some OSTC dive computers did care, at least
in certain situations, and both Miika and Anton had trouble downloading
with their OSTC Sport dive computers. Using the serial line protocol
and the legacy /dev/rfcomm model worked fine, because then it used the
sleeping functions in the POSIX serial code inside libdivecomputer.
This just adds trivial sleeping functions for the affected download
protocols. Maybe I should have just made libdivecomputer have a sane
default instead, but this wasn't hard either (the hard part was trying
to figure out why the downloads worked for some people and not for
others).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-07-13 16:49:42 +00:00
|
|
|
INFO (device->context, "Sleep: value=%u", timeout);
|
2015-08-20 22:19:44 +00:00
|
|
|
|
2018-09-12 00:27:05 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
Sleep((DWORD)timeout);
|
|
|
|
#else
|
2015-08-20 22:19:44 +00:00
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = (timeout / 1000);
|
|
|
|
ts.tv_nsec = (timeout % 1000) * 1000000;
|
|
|
|
|
|
|
|
while (nanosleep (&ts, &ts) != 0) {
|
|
|
|
if (errno != EINTR ) {
|
|
|
|
SYSERROR (device->context, errno);
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_IO;
|
2015-08-20 22:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 00:27:05 +00:00
|
|
|
#endif
|
2015-08-20 22:19:44 +00:00
|
|
|
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_SUCCESS;
|
2015-08-20 22:19:44 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 22:19:41 +00:00
|
|
|
|
|
|
|
// Used internally for opening ftdi devices
|
2015-08-20 22:19:44 +00:00
|
|
|
static int serial_ftdi_open_device (struct ftdi_context *ftdi_ctx)
|
2015-08-20 22:19:41 +00:00
|
|
|
{
|
2017-05-30 03:56:42 +00:00
|
|
|
INFO(0, "serial_ftdi_open_device called");
|
2018-08-11 14:21:10 +00:00
|
|
|
int accepted_pids[] = {
|
2018-08-11 19:20:21 +00:00
|
|
|
0x6001, 0x6010, 0x6011, // Suunto (Smart Interface), Heinrichs Weikamp
|
|
|
|
0x6015, // possibly Aqualung
|
|
|
|
0xF460, // Oceanic
|
|
|
|
0xF680, // Suunto
|
|
|
|
0x87D0, // Cressi (Leonardo)
|
2015-08-20 22:19:41 +00:00
|
|
|
};
|
|
|
|
int num_accepted_pids = 6;
|
|
|
|
int i, pid, ret;
|
|
|
|
for (i = 0; i < num_accepted_pids; i++) {
|
|
|
|
pid = accepted_pids[i];
|
|
|
|
ret = ftdi_usb_open (ftdi_ctx, VID, pid);
|
2017-05-30 03:56:42 +00:00
|
|
|
INFO(0, "FTDI tried VID %04x pid %04x ret %d\n", VID, pid, ret);
|
2015-08-20 22:19:41 +00:00
|
|
|
if (ret == -3) // Device not found
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
// No supported devices are attached.
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Open the serial port.
|
|
|
|
// Initialise ftdi_context and use it to open the device
|
2018-04-17 01:14:59 +00:00
|
|
|
static dc_status_t serial_ftdi_open (void **io, dc_context_t *context)
|
2015-08-20 22:19:41 +00:00
|
|
|
{
|
2017-05-30 03:56:42 +00:00
|
|
|
INFO(0, "serial_ftdi_open called");
|
2015-08-20 22:19:43 +00:00
|
|
|
// Allocate memory.
|
2016-09-17 15:27:56 +00:00
|
|
|
ftdi_serial_t *device = (ftdi_serial_t *) malloc (sizeof (ftdi_serial_t));
|
2015-08-20 22:19:43 +00:00
|
|
|
if (device == NULL) {
|
2018-07-01 01:14:01 +00:00
|
|
|
INFO(0, "couldn't allocate memory");
|
2015-08-20 22:19:43 +00:00
|
|
|
SYSERROR (context, errno);
|
2015-08-20 22:19:44 +00:00
|
|
|
return DC_STATUS_NOMEMORY;
|
2015-08-20 22:19:43 +00:00
|
|
|
}
|
2017-05-30 03:56:42 +00:00
|
|
|
INFO(0, "setting up ftdi_ctx");
|
2015-08-20 22:19:41 +00:00
|
|
|
struct ftdi_context *ftdi_ctx = ftdi_new();
|
|
|
|
if (ftdi_ctx == NULL) {
|
2018-07-01 01:14:01 +00:00
|
|
|
INFO(0, "failed ftdi_new()");
|
2015-09-10 04:19:04 +00:00
|
|
|
free(device);
|
2015-08-20 22:19:41 +00:00
|
|
|
SYSERROR (context, errno);
|
2015-08-20 22:19:44 +00:00
|
|
|
return DC_STATUS_NOMEMORY;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
// Library context.
|
2016-09-17 15:27:56 +00:00
|
|
|
//device->context = context;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
// Default to blocking reads.
|
|
|
|
device->timeout = -1;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
// Default to full-duplex.
|
|
|
|
device->baudrate = 0;
|
|
|
|
device->nbits = 0;
|
2017-09-05 21:07:57 +00:00
|
|
|
device->databits = 0;
|
|
|
|
device->stopbits = 0;
|
|
|
|
device->parity = 0;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
|
|
|
// Initialize device ftdi context
|
2017-05-30 03:56:42 +00:00
|
|
|
INFO(0, "initialize ftdi_ctx");
|
2015-08-20 22:19:43 +00:00
|
|
|
ftdi_init(ftdi_ctx);
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
if (ftdi_set_interface(ftdi_ctx,INTERFACE_ANY)) {
|
2015-09-10 04:19:04 +00:00
|
|
|
free(device);
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (context, "%s", ftdi_get_error_string(ftdi_ctx));
|
|
|
|
return DC_STATUS_IO;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 01:14:01 +00:00
|
|
|
INFO(0, "call serial_ftdi_open_device");
|
2015-08-20 22:19:44 +00:00
|
|
|
if (serial_ftdi_open_device(ftdi_ctx) < 0) {
|
2015-09-10 04:19:04 +00:00
|
|
|
free(device);
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (context, "%s", ftdi_get_error_string(ftdi_ctx));
|
|
|
|
return DC_STATUS_IO;
|
2015-08-20 22:19:43 +00:00
|
|
|
}
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
if (ftdi_usb_reset(ftdi_ctx)) {
|
2015-12-27 22:33:17 +00:00
|
|
|
free(device);
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (context, "%s", ftdi_get_error_string(ftdi_ctx));
|
|
|
|
return DC_STATUS_IO;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
if (ftdi_usb_purge_buffers(ftdi_ctx)) {
|
2015-09-10 04:19:04 +00:00
|
|
|
free(device);
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (context, "%s", ftdi_get_error_string(ftdi_ctx));
|
|
|
|
return DC_STATUS_IO;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
device->ftdi_ctx = ftdi_ctx;
|
2015-08-20 22:19:44 +00:00
|
|
|
|
2018-04-17 01:14:59 +00:00
|
|
|
*io = device;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:44 +00:00
|
|
|
return DC_STATUS_SUCCESS;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Close the serial port.
|
|
|
|
//
|
2018-04-17 01:14:59 +00:00
|
|
|
static dc_status_t serial_ftdi_close (void *io)
|
2015-08-20 22:19:41 +00:00
|
|
|
{
|
2018-04-17 01:14:59 +00:00
|
|
|
ftdi_serial_t *device = io;
|
2016-09-17 15:27:56 +00:00
|
|
|
|
2015-08-20 22:19:41 +00:00
|
|
|
if (device == NULL)
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_SUCCESS;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
// Restore the initial terminal attributes.
|
2015-08-20 22:19:41 +00:00
|
|
|
// See if it is possible using libusb or libftdi
|
|
|
|
|
|
|
|
int ret = ftdi_usb_close(device->ftdi_ctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
ERROR (device->context, "Unable to close the ftdi device : %d (%s)\n",
|
2015-08-20 22:19:43 +00:00
|
|
|
ret, ftdi_get_error_string(device->ftdi_ctx));
|
2015-08-20 22:19:41 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ftdi_free(device->ftdi_ctx);
|
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
// Free memory.
|
|
|
|
free (device);
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_SUCCESS;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Configure the serial port (baudrate, databits, parity, stopbits and flowcontrol).
|
|
|
|
//
|
2018-04-17 01:14:59 +00:00
|
|
|
static dc_status_t serial_ftdi_configure (void *io, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol)
|
2015-08-20 22:19:41 +00:00
|
|
|
{
|
2018-04-17 01:14:59 +00:00
|
|
|
ftdi_serial_t *device = io;
|
2016-09-17 15:27:56 +00:00
|
|
|
|
2015-08-20 22:19:41 +00:00
|
|
|
if (device == NULL)
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_INVALIDARGS;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
INFO (device->context, "Configure: baudrate=%i, databits=%i, parity=%i, stopbits=%i, flowcontrol=%i",
|
|
|
|
baudrate, databits, parity, stopbits, flowcontrol);
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
enum ftdi_bits_type ft_bits;
|
|
|
|
enum ftdi_stopbits_type ft_stopbits;
|
|
|
|
enum ftdi_parity_type ft_parity;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
if (ftdi_set_baudrate(device->ftdi_ctx, baudrate) < 0) {
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_IO;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
// Set the character size.
|
|
|
|
switch (databits) {
|
|
|
|
case 7:
|
|
|
|
ft_bits = BITS_7;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
ft_bits = BITS_8;
|
|
|
|
break;
|
|
|
|
default:
|
2015-08-20 22:19:44 +00:00
|
|
|
return DC_STATUS_INVALIDARGS;
|
2015-08-20 22:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the parity type.
|
|
|
|
switch (parity) {
|
2016-09-17 15:27:56 +00:00
|
|
|
case DC_PARITY_NONE: /**< No parity */
|
2015-08-20 22:19:43 +00:00
|
|
|
ft_parity = NONE;
|
|
|
|
break;
|
2016-09-17 15:27:56 +00:00
|
|
|
case DC_PARITY_EVEN: /**< Even parity */
|
2015-08-20 22:19:43 +00:00
|
|
|
ft_parity = EVEN;
|
|
|
|
break;
|
2016-09-17 15:27:56 +00:00
|
|
|
case DC_PARITY_ODD: /**< Odd parity */
|
2015-08-20 22:19:43 +00:00
|
|
|
ft_parity = ODD;
|
|
|
|
break;
|
2016-09-17 15:27:56 +00:00
|
|
|
case DC_PARITY_MARK: /**< Mark parity (always 1) */
|
|
|
|
case DC_PARITY_SPACE: /**< Space parity (alwasy 0) */
|
2015-08-20 22:19:43 +00:00
|
|
|
default:
|
2015-08-20 22:19:44 +00:00
|
|
|
return DC_STATUS_INVALIDARGS;
|
2015-08-20 22:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the number of stop bits.
|
|
|
|
switch (stopbits) {
|
2016-09-17 15:27:56 +00:00
|
|
|
case DC_STOPBITS_ONE: /**< 1 stop bit */
|
2015-08-20 22:19:43 +00:00
|
|
|
ft_stopbits = STOP_BIT_1;
|
|
|
|
break;
|
2016-09-17 15:27:56 +00:00
|
|
|
case DC_STOPBITS_TWO: /**< 2 stop bits */
|
2015-08-20 22:19:43 +00:00
|
|
|
ft_stopbits = STOP_BIT_2;
|
|
|
|
break;
|
2016-09-17 15:27:56 +00:00
|
|
|
case DC_STOPBITS_ONEPOINTFIVE: /**< 1.5 stop bits*/
|
2015-08-20 22:19:43 +00:00
|
|
|
default:
|
2015-08-20 22:19:44 +00:00
|
|
|
return DC_STATUS_INVALIDARGS;
|
2015-08-20 22:19:43 +00:00
|
|
|
}
|
2015-08-20 22:19:41 +00:00
|
|
|
|
|
|
|
// Set the attributes
|
2015-08-20 22:19:43 +00:00
|
|
|
if (ftdi_set_line_property(device->ftdi_ctx, ft_bits, ft_stopbits, ft_parity)) {
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
|
|
|
|
return DC_STATUS_IO;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
// Set the flow control.
|
|
|
|
switch (flowcontrol) {
|
2016-09-17 15:27:56 +00:00
|
|
|
case DC_FLOWCONTROL_NONE: /**< No flow control */
|
2015-08-20 22:19:43 +00:00
|
|
|
if (ftdi_setflowctrl(device->ftdi_ctx, SIO_DISABLE_FLOW_CTRL) < 0) {
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
|
|
|
|
return DC_STATUS_IO;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
2015-08-20 22:19:43 +00:00
|
|
|
break;
|
2016-09-17 15:27:56 +00:00
|
|
|
case DC_FLOWCONTROL_HARDWARE: /**< Hardware (RTS/CTS) flow control */
|
2015-08-20 22:19:43 +00:00
|
|
|
if (ftdi_setflowctrl(device->ftdi_ctx, SIO_RTS_CTS_HS) < 0) {
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
|
|
|
|
return DC_STATUS_IO;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-09-17 15:27:56 +00:00
|
|
|
case DC_FLOWCONTROL_SOFTWARE: /**< Software (XON/XOFF) flow control */
|
2015-08-20 22:19:43 +00:00
|
|
|
if (ftdi_setflowctrl(device->ftdi_ctx, SIO_XON_XOFF_HS) < 0) {
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
|
|
|
|
return DC_STATUS_IO;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
2015-08-20 22:19:43 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-08-20 22:19:44 +00:00
|
|
|
return DC_STATUS_INVALIDARGS;
|
2015-08-20 22:19:43 +00:00
|
|
|
}
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
device->baudrate = baudrate;
|
|
|
|
device->nbits = 1 + databits + stopbits + (parity ? 1 : 0);
|
2017-09-05 21:07:57 +00:00
|
|
|
device->databits = databits;
|
|
|
|
device->stopbits = stopbits;
|
|
|
|
device->parity = parity;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:44 +00:00
|
|
|
return DC_STATUS_SUCCESS;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Configure the serial port (timeouts).
|
|
|
|
//
|
2018-04-17 01:14:59 +00:00
|
|
|
static dc_status_t serial_ftdi_set_timeout (void *io, int timeout)
|
2015-08-20 22:19:41 +00:00
|
|
|
{
|
2018-04-17 01:14:59 +00:00
|
|
|
ftdi_serial_t *device = io;
|
2016-09-17 15:27:56 +00:00
|
|
|
|
2015-08-20 22:19:41 +00:00
|
|
|
if (device == NULL)
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_INVALIDARGS;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2018-04-17 01:14:59 +00:00
|
|
|
INFO (device->context, "Timeout: value=%i", timeout);
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
device->timeout = timeout;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_SUCCESS;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 01:14:59 +00:00
|
|
|
static dc_status_t serial_ftdi_read (void *io, void *data, size_t size, size_t *actual)
|
2015-08-20 22:19:41 +00:00
|
|
|
{
|
2018-04-17 01:14:59 +00:00
|
|
|
ftdi_serial_t *device = io;
|
2016-09-17 15:27:56 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
if (device == NULL)
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_INVALIDARGS;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
// The total timeout.
|
|
|
|
long timeout = device->timeout;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2016-09-17 15:27:56 +00:00
|
|
|
// Simulate blocking read as 10s timeout
|
|
|
|
if (timeout == -1)
|
|
|
|
timeout = 10000;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2018-10-07 19:41:21 +00:00
|
|
|
unsigned int start_time = serial_ftdi_get_msec();
|
2015-08-20 22:19:43 +00:00
|
|
|
unsigned int nbytes = 0;
|
|
|
|
while (nbytes < size) {
|
2018-04-17 01:14:59 +00:00
|
|
|
int n = ftdi_read_data (device->ftdi_ctx, (unsigned char *) data + nbytes, size - nbytes);
|
2015-08-20 22:19:41 +00:00
|
|
|
if (n < 0) {
|
2015-08-20 22:19:43 +00:00
|
|
|
if (n == LIBUSB_ERROR_INTERRUPTED)
|
|
|
|
continue; //Retry.
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_IO; //Error during read call.
|
2015-08-20 22:19:43 +00:00
|
|
|
} else if (n == 0) {
|
2018-10-07 19:41:21 +00:00
|
|
|
if (serial_ftdi_get_msec() - start_time > timeout) {
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR(device->context, "%s", "FTDI read timed out.");
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_TIMEOUT;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
2018-10-07 19:41:21 +00:00
|
|
|
serial_ftdi_sleep (device, 1);
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
nbytes += n;
|
|
|
|
}
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:44 +00:00
|
|
|
INFO (device->context, "Read %d bytes", nbytes);
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2016-09-17 15:27:56 +00:00
|
|
|
if (actual)
|
|
|
|
*actual = nbytes;
|
|
|
|
|
|
|
|
return DC_STATUS_SUCCESS;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 01:14:59 +00:00
|
|
|
static dc_status_t serial_ftdi_write (void *io, const void *data, size_t size, size_t *actual)
|
2015-08-20 22:19:41 +00:00
|
|
|
{
|
2018-04-17 01:14:59 +00:00
|
|
|
ftdi_serial_t *device = io;
|
2016-09-17 15:27:56 +00:00
|
|
|
|
2015-08-20 22:19:41 +00:00
|
|
|
if (device == NULL)
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_INVALIDARGS;
|
2015-08-20 22:19:43 +00:00
|
|
|
|
|
|
|
unsigned int nbytes = 0;
|
|
|
|
while (nbytes < size) {
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2018-04-17 01:14:59 +00:00
|
|
|
int n = ftdi_write_data (device->ftdi_ctx, (unsigned char *) data + nbytes, size - nbytes);
|
2015-08-20 22:19:43 +00:00
|
|
|
if (n < 0) {
|
|
|
|
if (n == LIBUSB_ERROR_INTERRUPTED)
|
|
|
|
continue; // Retry.
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_IO; // Error during write call.
|
2015-08-20 22:19:43 +00:00
|
|
|
} else if (n == 0) {
|
|
|
|
break; // EOF.
|
|
|
|
}
|
|
|
|
|
|
|
|
nbytes += n;
|
|
|
|
}
|
|
|
|
|
2015-08-20 22:19:44 +00:00
|
|
|
INFO (device->context, "Wrote %d bytes", nbytes);
|
2015-08-20 22:19:43 +00:00
|
|
|
|
2016-09-17 15:27:56 +00:00
|
|
|
if (actual)
|
|
|
|
*actual = nbytes;
|
|
|
|
|
|
|
|
return DC_STATUS_SUCCESS;
|
2015-08-20 22:19:43 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 01:14:59 +00:00
|
|
|
static dc_status_t serial_ftdi_purge (void *io, dc_direction_t queue)
|
2015-08-20 22:19:41 +00:00
|
|
|
{
|
2018-04-17 01:14:59 +00:00
|
|
|
ftdi_serial_t *device = io;
|
2016-09-17 15:27:56 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
if (device == NULL)
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_INVALIDARGS;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2016-09-17 15:27:56 +00:00
|
|
|
size_t input;
|
2017-06-28 04:04:53 +00:00
|
|
|
serial_ftdi_get_received (io, &input);
|
2016-09-17 15:27:56 +00:00
|
|
|
INFO (device->context, "Flush: queue=%u, input=%lu, output=%i", queue, input,
|
2015-08-20 22:19:44 +00:00
|
|
|
serial_ftdi_get_transmitted (device));
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
switch (queue) {
|
2016-09-17 15:27:56 +00:00
|
|
|
case DC_DIRECTION_INPUT: /**< Input direction */
|
2015-08-20 22:19:43 +00:00
|
|
|
if (ftdi_usb_purge_tx_buffer(device->ftdi_ctx)) {
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_IO;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
2015-08-20 22:19:43 +00:00
|
|
|
break;
|
2016-09-17 15:27:56 +00:00
|
|
|
case DC_DIRECTION_OUTPUT: /**< Output direction */
|
2015-08-20 22:19:43 +00:00
|
|
|
if (ftdi_usb_purge_rx_buffer(device->ftdi_ctx)) {
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_IO;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
2015-08-20 22:19:43 +00:00
|
|
|
break;
|
2016-09-17 15:27:56 +00:00
|
|
|
case DC_DIRECTION_ALL: /**< All directions */
|
2015-08-20 22:19:43 +00:00
|
|
|
default:
|
2017-09-05 21:07:56 +00:00
|
|
|
if (ftdi_usb_reset(device->ftdi_ctx)) {
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_IO;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
2015-08-20 22:19:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_SUCCESS;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 01:14:59 +00:00
|
|
|
static dc_status_t serial_ftdi_set_break (void *io, unsigned int level)
|
2015-08-20 22:19:41 +00:00
|
|
|
{
|
2018-04-17 01:14:59 +00:00
|
|
|
ftdi_serial_t *device = io;
|
2016-09-17 15:27:56 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
if (device == NULL)
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_INVALIDARGS;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
INFO (device->context, "Break: value=%i", level);
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2017-09-05 21:07:57 +00:00
|
|
|
if (ftdi_set_line_property2(device->ftdi_ctx, device->databits, device->stopbits, device->parity, level)) {
|
|
|
|
ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
|
|
|
|
return DC_STATUS_IO;
|
|
|
|
}
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_UNSUPPORTED;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 01:14:59 +00:00
|
|
|
static dc_status_t serial_ftdi_set_dtr (void *io, unsigned int value)
|
2015-08-20 22:19:41 +00:00
|
|
|
{
|
2018-04-17 01:14:59 +00:00
|
|
|
ftdi_serial_t *device = io;
|
2016-09-17 15:27:56 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
if (device == NULL)
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_INVALIDARGS;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2018-04-17 01:14:59 +00:00
|
|
|
INFO (device->context, "DTR: value=%u", value);
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2018-04-17 01:14:59 +00:00
|
|
|
if (ftdi_setdtr(device->ftdi_ctx, value)) {
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_IO;
|
2015-08-20 22:19:43 +00:00
|
|
|
}
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_SUCCESS;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 01:14:59 +00:00
|
|
|
static dc_status_t serial_ftdi_set_rts (void *io, unsigned int level)
|
2015-08-20 22:19:41 +00:00
|
|
|
{
|
2018-04-17 01:14:59 +00:00
|
|
|
ftdi_serial_t *device = io;
|
2016-09-17 15:27:56 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
if (device == NULL)
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_INVALIDARGS;
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2018-04-17 01:14:59 +00:00
|
|
|
INFO (device->context, "RTS: value=%u", level);
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2015-08-20 22:19:43 +00:00
|
|
|
if (ftdi_setrts(device->ftdi_ctx, level)) {
|
2015-08-20 22:19:44 +00:00
|
|
|
ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_IO;
|
2015-08-20 22:19:43 +00:00
|
|
|
}
|
2015-08-20 22:19:41 +00:00
|
|
|
|
2016-09-17 15:27:56 +00:00
|
|
|
return DC_STATUS_SUCCESS;
|
2015-08-20 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 01:14:59 +00:00
|
|
|
dc_status_t ftdi_open(dc_iostream_t **iostream, dc_context_t *context)
|
|
|
|
{
|
|
|
|
dc_status_t rc = DC_STATUS_SUCCESS;
|
|
|
|
void *io = NULL;
|
|
|
|
|
|
|
|
static const dc_custom_cbs_t callbacks = {
|
|
|
|
serial_ftdi_set_timeout, /* set_timeout */
|
|
|
|
NULL, /* set_latency */
|
|
|
|
serial_ftdi_set_break, /* set_break */
|
|
|
|
serial_ftdi_set_dtr, /* set_dtr */
|
|
|
|
serial_ftdi_set_rts, /* set_rts */
|
|
|
|
NULL, /* get_lines */
|
|
|
|
serial_ftdi_get_received, /* get_received */
|
|
|
|
serial_ftdi_configure, /* configure */
|
|
|
|
serial_ftdi_read, /* read */
|
|
|
|
serial_ftdi_write, /* write */
|
|
|
|
NULL, /* flush */
|
|
|
|
serial_ftdi_purge, /* purge */
|
Make sure our libdivecomputer custom IO interfaces have sleep functions
When I switched over from our own custom IO implementation to the new
upstream custom IO model in libdivecomputer, I completely missed the
fact that the libdivecomputer custom IO model also does a custom _sleep_
function.
I'm not entirely sure what the point was, and it broke things even in
libdivecopmputer itself when some of the new sleep functions were
broken.
Anyway, we didn't export any sleep functions at all for the bluetooth,
BLE and FTDI cases, the the libdivecomputer code didn't fall back to any
sane default sleep implementation either, so the end result was no
sleeping at all.
Which didn't matter for most divecomputers.
But it seems like at least some OSTC dive computers did care, at least
in certain situations, and both Miika and Anton had trouble downloading
with their OSTC Sport dive computers. Using the serial line protocol
and the legacy /dev/rfcomm model worked fine, because then it used the
sleeping functions in the POSIX serial code inside libdivecomputer.
This just adds trivial sleeping functions for the affected download
protocols. Maybe I should have just made libdivecomputer have a sane
default instead, but this wasn't hard either (the hard part was trying
to figure out why the downloads worked for some people and not for
others).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-07-13 16:49:42 +00:00
|
|
|
serial_ftdi_sleep, /* sleep */
|
2018-04-17 01:14:59 +00:00
|
|
|
serial_ftdi_close, /* close */
|
|
|
|
};
|
|
|
|
|
2018-07-01 01:14:01 +00:00
|
|
|
INFO(device->contxt, "%s", "in ftdi_open");
|
2018-04-17 01:14:59 +00:00
|
|
|
rc = serial_ftdi_open(&io, context);
|
2018-07-01 01:14:01 +00:00
|
|
|
if (rc != DC_STATUS_SUCCESS) {
|
|
|
|
INFO(device->contxt, "%s", "serial_ftdi_open() failed");
|
2018-04-17 01:14:59 +00:00
|
|
|
return rc;
|
2018-07-01 01:14:01 +00:00
|
|
|
}
|
|
|
|
INFO(device->contxt, "%s", "calling dc_custom_open())");
|
2018-04-17 01:14:59 +00:00
|
|
|
return dc_custom_open(iostream, context, DC_TRANSPORT_SERIAL, &callbacks, io);
|
|
|
|
}
|