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.claudiu@ymail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Claudiu Olteanu 2015-06-27 15:21:19 +03:00 committed by Dirk Hohndel
parent 69bd993233
commit 5dbaa7a053
2 changed files with 58 additions and 2 deletions

View File

@ -39,6 +39,9 @@ extern "C" {
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);

View File

@ -295,10 +295,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).
@ -330,6 +330,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)
{