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.claudiu@ymail.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
b9e3f40d59
commit
a50a1e0688
@ -53,4 +53,5 @@ libdivecomputer_HEADERS = \
|
||||
citizen.h \
|
||||
citizen_aqualand.h \
|
||||
divesystem.h \
|
||||
divesystem_idive.h
|
||||
divesystem_idive.h \
|
||||
custom_serial.h
|
||||
|
||||
63
include/libdivecomputer/custom_serial.h
Normal file
63
include/libdivecomputer/custom_serial.h
Normal file
@ -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 */
|
||||
@ -63,7 +63,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
|
||||
|
||||
79
src/custom_serial.c
Normal file
79
src/custom_serial.c
Normal file
@ -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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user