Port the serial code to the new I/O interface
This commit is contained in:
parent
3ca27995e1
commit
7cd1656d1d
@ -34,7 +34,7 @@
|
||||
|
||||
typedef struct citizen_aqualand_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned char fingerprint[8];
|
||||
} citizen_aqualand_device_t;
|
||||
|
||||
@ -73,40 +73,40 @@ citizen_aqualand_device_open (dc_device_t **out, dc_context_t *context, const ch
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
memset (device->fingerprint, 0, sizeof (device->fingerprint));
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (4800 8N1).
|
||||
status = dc_serial_configure (device->port, 4800, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 4800, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (1000ms).
|
||||
status = dc_serial_set_timeout (device->port, 1000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 1000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_sleep (device->port, 300);
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_sleep (device->iostream, 300);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
*out = (dc_device_t *) device;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -121,7 +121,7 @@ citizen_aqualand_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -159,21 +159,21 @@ citizen_aqualand_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
return DC_STATUS_NOMEMORY;
|
||||
}
|
||||
|
||||
dc_serial_set_dtr (device->port, 1);
|
||||
dc_iostream_set_dtr (device->iostream, 1);
|
||||
|
||||
// Send the init byte.
|
||||
const unsigned char init[] = {0x7F};
|
||||
status = dc_serial_write (device->port, init, sizeof (init), NULL);
|
||||
status = dc_iostream_write (device->iostream, init, sizeof (init), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
}
|
||||
|
||||
dc_serial_sleep(device->port, 1200);
|
||||
dc_iostream_sleep(device->iostream, 1200);
|
||||
|
||||
// Send the command.
|
||||
const unsigned char command[] = {0xFF};
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -182,7 +182,7 @@ citizen_aqualand_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
while (1) {
|
||||
// Receive the response packet.
|
||||
unsigned char answer[32] = {0};
|
||||
status = dc_serial_read (device->port, answer, sizeof (answer), NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, sizeof (answer), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -191,7 +191,7 @@ citizen_aqualand_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
dc_buffer_append(buffer, answer, sizeof (answer));
|
||||
|
||||
// Send the command.
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -201,7 +201,7 @@ citizen_aqualand_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
break;
|
||||
}
|
||||
|
||||
dc_serial_set_dtr (device->port, 0);
|
||||
dc_iostream_set_dtr (device->iostream, 0);
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
@ -94,7 +94,7 @@ typedef struct cochran_device_layout_t {
|
||||
|
||||
typedef struct cochran_commander_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
const cochran_device_layout_t *layout;
|
||||
unsigned char id[67];
|
||||
unsigned char fingerprint[6];
|
||||
@ -305,30 +305,30 @@ cochran_commander_serial_setup (cochran_commander_device_t *device)
|
||||
dc_status_t status = DC_STATUS_SUCCESS;
|
||||
|
||||
// Set the serial communication protocol (9600 8N2, no FC).
|
||||
status = dc_serial_configure (device->port, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_TWO, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_TWO, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (device->base.context, "Failed to set the terminal attributes.");
|
||||
return status;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (5000 ms).
|
||||
status = dc_serial_set_timeout (device->port, 5000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 5000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (device->base.context, "Failed to set the timeout.");
|
||||
return status;
|
||||
}
|
||||
|
||||
// Wake up DC and trigger heartbeat
|
||||
dc_serial_set_break(device->port, 1);
|
||||
dc_serial_sleep(device->port, 16);
|
||||
dc_serial_set_break(device->port, 0);
|
||||
dc_iostream_set_break(device->iostream, 1);
|
||||
dc_iostream_sleep(device->iostream, 16);
|
||||
dc_iostream_set_break(device->iostream, 0);
|
||||
|
||||
// Clear old heartbeats
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
// Wait for heartbeat byte before send
|
||||
unsigned char answer = 0;
|
||||
status = dc_serial_read(device->port, &answer, 1, NULL);
|
||||
status = dc_iostream_read(device->iostream, &answer, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (device->base.context, "Failed to receive device heartbeat.");
|
||||
return status;
|
||||
@ -359,9 +359,9 @@ cochran_commander_packet (cochran_commander_device_t *device, dc_event_progress_
|
||||
// has no buffering.
|
||||
for (unsigned int i = 0; i < csize; i++) {
|
||||
// Give the DC time to read the character.
|
||||
if (i) dc_serial_sleep(device->port, 16); // 16 ms
|
||||
if (i) dc_iostream_sleep(device->iostream, 16); // 16 ms
|
||||
|
||||
status = dc_serial_write(device->port, command + i, 1, NULL);
|
||||
status = dc_iostream_write(device->iostream, command + i, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -370,10 +370,10 @@ cochran_commander_packet (cochran_commander_device_t *device, dc_event_progress_
|
||||
|
||||
if (high_speed && device->layout->baudrate != 9600) {
|
||||
// Give the DC time to process the command.
|
||||
dc_serial_sleep(device->port, 45);
|
||||
dc_iostream_sleep(device->iostream, 45);
|
||||
|
||||
// Rates are odd, like 850400 for the EMC, 115200 for commander
|
||||
status = dc_serial_configure(device->port, device->layout->baudrate, 8, DC_PARITY_NONE, DC_STOPBITS_TWO, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure(device->iostream, device->layout->baudrate, 8, DC_PARITY_NONE, DC_STOPBITS_TWO, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to set the high baud rate.");
|
||||
return status;
|
||||
@ -388,7 +388,7 @@ cochran_commander_packet (cochran_commander_device_t *device, dc_event_progress_
|
||||
if (len > 1024)
|
||||
len = 1024;
|
||||
|
||||
status = dc_serial_read (device->port, answer + nbytes, len, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer + nbytes, len, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive data.");
|
||||
return status;
|
||||
@ -523,7 +523,7 @@ cochran_commander_read (cochran_commander_device_t *device, dc_event_progress_t
|
||||
return DC_STATUS_UNSUPPORTED;
|
||||
}
|
||||
|
||||
dc_serial_sleep(device->port, 550);
|
||||
dc_iostream_sleep(device->iostream, 550);
|
||||
|
||||
// set back to 9600 baud
|
||||
rc = cochran_commander_serial_setup(device);
|
||||
@ -732,11 +732,11 @@ cochran_commander_device_open (dc_device_t **out, dc_context_t *context, const c
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
cochran_commander_device_set_fingerprint((dc_device_t *) device, NULL, 0);
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, device->base.context, name);
|
||||
status = dc_serial_open (&device->iostream, device->base.context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (device->base.context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
@ -785,7 +785,7 @@ cochran_commander_device_open (dc_device_t **out, dc_context_t *context, const c
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -799,7 +799,7 @@ cochran_commander_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
|
||||
@ -55,7 +55,7 @@ typedef struct cressi_edy_layout_t {
|
||||
|
||||
typedef struct cressi_edy_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
const cressi_edy_layout_t *layout;
|
||||
unsigned char fingerprint[SZ_PAGE / 2];
|
||||
unsigned int model;
|
||||
@ -112,7 +112,7 @@ cressi_edy_packet (cressi_edy_device_t *device, const unsigned char command[], u
|
||||
|
||||
for (unsigned int i = 0; i < csize; ++i) {
|
||||
// Send the command to the device.
|
||||
status = dc_serial_write (device->port, command + i, 1, NULL);
|
||||
status = dc_iostream_write (device->iostream, command + i, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -120,7 +120,7 @@ cressi_edy_packet (cressi_edy_device_t *device, const unsigned char command[], u
|
||||
|
||||
// Receive the echo.
|
||||
unsigned char echo = 0;
|
||||
status = dc_serial_read (device->port, &echo, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, &echo, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the echo.");
|
||||
return status;
|
||||
@ -135,7 +135,7 @@ cressi_edy_packet (cressi_edy_device_t *device, const unsigned char command[], u
|
||||
|
||||
if (asize) {
|
||||
// Receive the answer of the device.
|
||||
status = dc_serial_read (device->port, answer, asize, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, asize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -165,8 +165,8 @@ cressi_edy_transfer (cressi_edy_device_t *device, const unsigned char command[],
|
||||
return rc;
|
||||
|
||||
// Delay the next attempt.
|
||||
dc_serial_sleep (device->port, 300);
|
||||
dc_serial_purge (device->port, DC_DIRECTION_INPUT);
|
||||
dc_iostream_sleep (device->iostream, 300);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT);
|
||||
}
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
@ -234,49 +234,49 @@ cressi_edy_device_open (dc_device_t **out, dc_context_t *context, const char *na
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
device->layout = NULL;
|
||||
device->model = 0;
|
||||
memset (device->fingerprint, 0, sizeof (device->fingerprint));
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (1200 8N1).
|
||||
status = dc_serial_configure (device->port, 1200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 1200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (1000 ms).
|
||||
status = dc_serial_set_timeout (device->port, 1000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 1000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the DTR line.
|
||||
status = dc_serial_set_dtr (device->port, 1);
|
||||
status = dc_iostream_set_dtr (device->iostream, 1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the DTR line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Clear the RTS line.
|
||||
status = dc_serial_set_rts (device->port, 0);
|
||||
status = dc_iostream_set_rts (device->iostream, 0);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to clear the RTS line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_sleep(device->port, 300);
|
||||
dc_serial_purge(device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_sleep(device->iostream, 300);
|
||||
dc_iostream_purge(device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
// Send the init commands.
|
||||
cressi_edy_init1 (device);
|
||||
@ -290,22 +290,22 @@ cressi_edy_device_open (dc_device_t **out, dc_context_t *context, const char *na
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (4800 8N1).
|
||||
status = dc_serial_configure (device->port, 4800, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 4800, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_sleep(device->port, 300);
|
||||
dc_serial_purge(device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_sleep(device->iostream, 300);
|
||||
dc_iostream_purge(device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
*out = (dc_device_t*) device;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -326,7 +326,7 @@ cressi_edy_device_close (dc_device_t *abstract)
|
||||
}
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
|
||||
@ -49,7 +49,7 @@
|
||||
|
||||
typedef struct cressi_leonardo_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned char fingerprint[5];
|
||||
} cressi_leonardo_device_t;
|
||||
|
||||
@ -106,14 +106,14 @@ cressi_leonardo_packet (cressi_leonardo_device_t *device, const unsigned char co
|
||||
return DC_STATUS_CANCELLED;
|
||||
|
||||
// Send the command to the device.
|
||||
status = dc_serial_write (device->port, command, csize, NULL);
|
||||
status = dc_iostream_write (device->iostream, command, csize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
}
|
||||
|
||||
// Receive the answer of the device.
|
||||
status = dc_serial_read (device->port, answer, asize, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, asize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -156,8 +156,8 @@ cressi_leonardo_transfer (cressi_leonardo_device_t *device, const unsigned char
|
||||
return rc;
|
||||
|
||||
// Discard any garbage bytes.
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_serial_purge (device->port, DC_DIRECTION_INPUT);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT);
|
||||
}
|
||||
|
||||
return rc;
|
||||
@ -180,62 +180,62 @@ cressi_leonardo_device_open (dc_device_t **out, dc_context_t *context, const cha
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
memset (device->fingerprint, 0, sizeof (device->fingerprint));
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (115200 8N1).
|
||||
status = dc_serial_configure (device->port, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (1000 ms).
|
||||
status = dc_serial_set_timeout (device->port, 1000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 1000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the RTS line.
|
||||
status = dc_serial_set_rts (device->port, 1);
|
||||
status = dc_iostream_set_rts (device->iostream, 1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the RTS line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the DTR line.
|
||||
status = dc_serial_set_dtr (device->port, 1);
|
||||
status = dc_iostream_set_dtr (device->iostream, 1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the DTR line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
dc_serial_sleep (device->port, 200);
|
||||
dc_iostream_sleep (device->iostream, 200);
|
||||
|
||||
// Clear the DTR line.
|
||||
status = dc_serial_set_dtr (device->port, 0);
|
||||
status = dc_iostream_set_dtr (device->iostream, 0);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to clear the DTR line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
*out = (dc_device_t *) device;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -249,7 +249,7 @@ cressi_leonardo_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -334,7 +334,7 @@ cressi_leonardo_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Send the command header to the dive computer.
|
||||
const unsigned char command[] = {0x7B, 0x31, 0x32, 0x33, 0x44, 0x42, 0x41, 0x7d};
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -342,7 +342,7 @@ cressi_leonardo_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Receive the header packet.
|
||||
unsigned char header[7] = {0};
|
||||
status = dc_serial_read (device->port, header, sizeof (header), NULL);
|
||||
status = dc_iostream_read (device->iostream, header, sizeof (header), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -364,7 +364,7 @@ cressi_leonardo_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Increase the packet size if more data is immediately available.
|
||||
size_t available = 0;
|
||||
status = dc_serial_get_available (device->port, &available);
|
||||
status = dc_iostream_get_available (device->iostream, &available);
|
||||
if (status == DC_STATUS_SUCCESS && available > len)
|
||||
len = available;
|
||||
|
||||
@ -373,7 +373,7 @@ cressi_leonardo_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
len = SZ_MEMORY - nbytes;
|
||||
|
||||
// Read the packet.
|
||||
status = dc_serial_read (device->port, data + nbytes, len, NULL);
|
||||
status = dc_iostream_read (device->iostream, data + nbytes, len, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -388,7 +388,7 @@ cressi_leonardo_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Receive the trailer packet.
|
||||
unsigned char trailer[4] = {0};
|
||||
status = dc_serial_read (device->port, trailer, sizeof (trailer), NULL);
|
||||
status = dc_iostream_read (device->iostream, trailer, sizeof (trailer), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
|
||||
@ -51,7 +51,7 @@
|
||||
|
||||
typedef struct diverite_nitekq_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned char version[32];
|
||||
unsigned char fingerprint[SZ_LOGBOOK];
|
||||
} diverite_nitekq_device_t;
|
||||
@ -87,7 +87,7 @@ diverite_nitekq_send (diverite_nitekq_device_t *device, unsigned char cmd)
|
||||
|
||||
// Send the command.
|
||||
unsigned char command[] = {cmd};
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -104,7 +104,7 @@ diverite_nitekq_receive (diverite_nitekq_device_t *device, unsigned char data[],
|
||||
dc_device_t *abstract = (dc_device_t *) device;
|
||||
|
||||
// Read the answer.
|
||||
status = dc_serial_read (device->port, data, size, NULL);
|
||||
status = dc_iostream_read (device->iostream, data, size, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -112,7 +112,7 @@ diverite_nitekq_receive (diverite_nitekq_device_t *device, unsigned char data[],
|
||||
|
||||
// Read the checksum.
|
||||
unsigned char checksum[2] = {0};
|
||||
status = dc_serial_read (device->port, checksum, sizeof (checksum), NULL);
|
||||
status = dc_iostream_read (device->iostream, checksum, sizeof (checksum), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the checksum.");
|
||||
return status;
|
||||
@ -130,14 +130,14 @@ diverite_nitekq_handshake (diverite_nitekq_device_t *device)
|
||||
|
||||
// Send the command.
|
||||
unsigned char command[] = {HANDSHAKE};
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
}
|
||||
|
||||
// Read the answer.
|
||||
status = dc_serial_read (device->port, device->version, sizeof (device->version), NULL);
|
||||
status = dc_iostream_read (device->iostream, device->version, sizeof (device->version), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -164,33 +164,33 @@ diverite_nitekq_device_open (dc_device_t **out, dc_context_t *context, const cha
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
memset (device->fingerprint, 0, sizeof (device->fingerprint));
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (9600 8N1).
|
||||
status = dc_serial_configure (device->port, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (1000ms).
|
||||
status = dc_serial_set_timeout (device->port, 1000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 1000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
// Perform the handshaking.
|
||||
status = diverite_nitekq_handshake (device);
|
||||
@ -204,7 +204,7 @@ diverite_nitekq_device_open (dc_device_t **out, dc_context_t *context, const cha
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -225,7 +225,7 @@ diverite_nitekq_device_close (dc_device_t *abstract)
|
||||
}
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
|
||||
@ -69,7 +69,7 @@ typedef struct divesystem_idive_commands_t {
|
||||
|
||||
typedef struct divesystem_idive_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned char fingerprint[4];
|
||||
unsigned int model;
|
||||
} divesystem_idive_device_t;
|
||||
@ -131,41 +131,41 @@ divesystem_idive_device_open (dc_device_t **out, dc_context_t *context, const ch
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
memset (device->fingerprint, 0, sizeof (device->fingerprint));
|
||||
device->model = model;
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (115200 8N1).
|
||||
status = dc_serial_configure (device->port, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (1000ms).
|
||||
status = dc_serial_set_timeout (device->port, 1000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 1000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_sleep (device->port, 300);
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_sleep (device->iostream, 300);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
*out = (dc_device_t *) device;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -180,7 +180,7 @@ divesystem_idive_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -229,7 +229,7 @@ divesystem_idive_send (divesystem_idive_device_t *device, const unsigned char co
|
||||
packet[csize + 3] = (crc ) & 0xFF;
|
||||
|
||||
// Send the data packet.
|
||||
status = dc_serial_write (device->port, packet, csize + 4, NULL);
|
||||
status = dc_iostream_write (device->iostream, packet, csize + 4, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -253,7 +253,7 @@ divesystem_idive_receive (divesystem_idive_device_t *device, unsigned char answe
|
||||
|
||||
// Read the packet start byte.
|
||||
while (1) {
|
||||
status = dc_serial_read (device->port, packet + 0, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, packet + 0, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the packet start byte.");
|
||||
return status;
|
||||
@ -264,7 +264,7 @@ divesystem_idive_receive (divesystem_idive_device_t *device, unsigned char answe
|
||||
}
|
||||
|
||||
// Read the packet length.
|
||||
status = dc_serial_read (device->port, packet + 1, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, packet + 1, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the packet length.");
|
||||
return status;
|
||||
@ -277,7 +277,7 @@ divesystem_idive_receive (divesystem_idive_device_t *device, unsigned char answe
|
||||
}
|
||||
|
||||
// Read the packet payload and checksum.
|
||||
status = dc_serial_read (device->port, packet + 2, len + 2, NULL);
|
||||
status = dc_iostream_read (device->iostream, packet + 2, len + 2, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the packet payload and checksum.");
|
||||
return status;
|
||||
@ -383,7 +383,7 @@ divesystem_idive_transfer (divesystem_idive_device_t *device, const unsigned cha
|
||||
break;
|
||||
|
||||
// Delay the next attempt.
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
}
|
||||
|
||||
if (errorcode) {
|
||||
|
||||
@ -55,7 +55,7 @@
|
||||
|
||||
typedef struct hw_frog_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned char fingerprint[5];
|
||||
} hw_frog_device_t;
|
||||
|
||||
@ -114,7 +114,7 @@ hw_frog_transfer (hw_frog_device_t *device,
|
||||
|
||||
// Send the command.
|
||||
unsigned char command[1] = {cmd};
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -123,7 +123,7 @@ hw_frog_transfer (hw_frog_device_t *device,
|
||||
if (cmd != INIT && cmd != HEADER) {
|
||||
// Read the echo.
|
||||
unsigned char answer[1] = {0};
|
||||
status = dc_serial_read (device->port, answer, sizeof (answer), NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, sizeof (answer), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the echo.");
|
||||
return status;
|
||||
@ -138,7 +138,7 @@ hw_frog_transfer (hw_frog_device_t *device,
|
||||
|
||||
if (input) {
|
||||
// Send the input data packet.
|
||||
status = dc_serial_write (device->port, input, isize, NULL);
|
||||
status = dc_iostream_write (device->iostream, input, isize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the data packet.");
|
||||
return status;
|
||||
@ -153,7 +153,7 @@ hw_frog_transfer (hw_frog_device_t *device,
|
||||
|
||||
// Increase the packet size if more data is immediately available.
|
||||
size_t available = 0;
|
||||
status = dc_serial_get_available (device->port, &available);
|
||||
status = dc_iostream_get_available (device->iostream, &available);
|
||||
if (status == DC_STATUS_SUCCESS && available > len)
|
||||
len = available;
|
||||
|
||||
@ -162,7 +162,7 @@ hw_frog_transfer (hw_frog_device_t *device,
|
||||
len = osize - nbytes;
|
||||
|
||||
// Read the packet.
|
||||
status = dc_serial_read (device->port, output + nbytes, len, NULL);
|
||||
status = dc_iostream_read (device->iostream, output + nbytes, len, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -181,7 +181,7 @@ hw_frog_transfer (hw_frog_device_t *device,
|
||||
if (cmd != EXIT) {
|
||||
// Read the ready byte.
|
||||
unsigned char answer[1] = {0};
|
||||
status = dc_serial_read (device->port, answer, sizeof (answer), NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, sizeof (answer), NULL);
|
||||
if (status != sizeof (answer)) {
|
||||
ERROR (abstract->context, "Failed to receive the ready byte.");
|
||||
return status;
|
||||
@ -215,33 +215,33 @@ hw_frog_device_open (dc_device_t **out, dc_context_t *context, const char *name)
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
memset (device->fingerprint, 0, sizeof (device->fingerprint));
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (115200 8N1).
|
||||
status = dc_serial_configure (device->port, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (3000ms).
|
||||
status = dc_serial_set_timeout (device->port, 3000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 3000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_sleep (device->port, 300);
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_sleep (device->iostream, 300);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
// Send the init command.
|
||||
status = hw_frog_transfer (device, NULL, INIT, NULL, 0, NULL, 0);
|
||||
@ -255,7 +255,7 @@ hw_frog_device_open (dc_device_t **out, dc_context_t *context, const char *name)
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -277,7 +277,7 @@ hw_frog_device_close (dc_device_t *abstract)
|
||||
}
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
|
||||
@ -58,7 +58,7 @@
|
||||
|
||||
typedef struct hw_ostc_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned char fingerprint[5];
|
||||
} hw_ostc_device_t;
|
||||
|
||||
@ -96,7 +96,7 @@ hw_ostc_send (hw_ostc_device_t *device, unsigned char cmd, unsigned int echo)
|
||||
|
||||
// Send the command.
|
||||
unsigned char command[1] = {cmd};
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -105,7 +105,7 @@ hw_ostc_send (hw_ostc_device_t *device, unsigned char cmd, unsigned int echo)
|
||||
if (echo) {
|
||||
// Read the echo.
|
||||
unsigned char answer[1] = {0};
|
||||
status = dc_serial_read (device->port, answer, sizeof (answer), NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, sizeof (answer), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the echo.");
|
||||
return status;
|
||||
@ -139,40 +139,40 @@ hw_ostc_device_open (dc_device_t **out, dc_context_t *context, const char *name)
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
memset (device->fingerprint, 0, sizeof (device->fingerprint));
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (115200 8N1).
|
||||
status = dc_serial_configure (device->port, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data.
|
||||
status = dc_serial_set_timeout (device->port, 4000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 4000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
*out = (dc_device_t*) device;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -187,7 +187,7 @@ hw_ostc_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -232,7 +232,7 @@ hw_ostc_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Send the command.
|
||||
unsigned char command[1] = {'a'};
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -240,7 +240,7 @@ hw_ostc_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Read the header.
|
||||
unsigned char header[SZ_HEADER] = {0};
|
||||
status = dc_serial_read (device->port, header, sizeof (header), NULL);
|
||||
status = dc_iostream_read (device->iostream, header, sizeof (header), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the header.");
|
||||
return status;
|
||||
@ -286,7 +286,7 @@ hw_ostc_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Increase the packet size if more data is immediately available.
|
||||
size_t available = 0;
|
||||
status = dc_serial_get_available (device->port, &available);
|
||||
status = dc_iostream_get_available (device->iostream, &available);
|
||||
if (status == DC_STATUS_SUCCESS && available > len)
|
||||
len = available;
|
||||
|
||||
@ -295,7 +295,7 @@ hw_ostc_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
len = size - nbytes;
|
||||
|
||||
// Read the packet.
|
||||
status = dc_serial_read (device->port, data + nbytes, len, NULL);
|
||||
status = dc_iostream_read (device->iostream, data + nbytes, len, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -369,7 +369,7 @@ hw_ostc_device_md2hash (dc_device_t *abstract, unsigned char data[], unsigned in
|
||||
return rc;
|
||||
|
||||
// Read the answer.
|
||||
status = dc_serial_read (device->port, data, SZ_MD2HASH, NULL);
|
||||
status = dc_iostream_read (device->iostream, data, SZ_MD2HASH, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -399,7 +399,7 @@ hw_ostc_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime)
|
||||
unsigned char packet[6] = {
|
||||
datetime->hour, datetime->minute, datetime->second,
|
||||
datetime->month, datetime->day, datetime->year - 2000};
|
||||
status = dc_serial_write (device->port, packet, sizeof (packet), NULL);
|
||||
status = dc_iostream_write (device->iostream, packet, sizeof (packet), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the data packet.");
|
||||
return status;
|
||||
@ -435,7 +435,7 @@ hw_ostc_device_eeprom_read (dc_device_t *abstract, unsigned int bank, unsigned c
|
||||
return rc;
|
||||
|
||||
// Read the answer.
|
||||
status = dc_serial_read (device->port, data, SZ_EEPROM, NULL);
|
||||
status = dc_iostream_read (device->iostream, data, SZ_EEPROM, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -556,7 +556,7 @@ hw_ostc_device_screenshot (dc_device_t *abstract, dc_buffer_t *buffer, hw_ostc_f
|
||||
unsigned int npixels = 0;
|
||||
while (npixels < WIDTH * HEIGHT) {
|
||||
unsigned char raw[3] = {0};
|
||||
status = dc_serial_read (device->port, raw, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, raw, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the packet.");
|
||||
return status;
|
||||
@ -574,7 +574,7 @@ hw_ostc_device_screenshot (dc_device_t *abstract, dc_buffer_t *buffer, hw_ostc_f
|
||||
count &= 0x3F;
|
||||
} else {
|
||||
// Color pixel.
|
||||
status = dc_serial_read (device->port, raw + 1, 2, NULL);
|
||||
status = dc_iostream_read (device->iostream, raw + 1, 2, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the packet.");
|
||||
return status;
|
||||
@ -774,7 +774,7 @@ hw_ostc_firmware_setup_internal (hw_ostc_device_t *device)
|
||||
|
||||
// Send the command.
|
||||
unsigned char command[1] = {0xC1};
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -782,7 +782,7 @@ hw_ostc_firmware_setup_internal (hw_ostc_device_t *device)
|
||||
|
||||
// Read the response.
|
||||
unsigned char answer[2] = {0};
|
||||
status = dc_serial_read (device->port, answer, sizeof (answer), NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, sizeof (answer), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the response.");
|
||||
return status;
|
||||
@ -825,7 +825,7 @@ hw_ostc_firmware_write_internal (hw_ostc_device_t *device, unsigned char *data,
|
||||
dc_device_t *abstract = (dc_device_t *) device;
|
||||
|
||||
// Send the packet.
|
||||
status = dc_serial_write (device->port, data, size, NULL);
|
||||
status = dc_iostream_write (device->iostream, data, size, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the packet.");
|
||||
return status;
|
||||
@ -833,7 +833,7 @@ hw_ostc_firmware_write_internal (hw_ostc_device_t *device, unsigned char *data,
|
||||
|
||||
// Read the response.
|
||||
unsigned char answer[1] = {0};
|
||||
status = dc_serial_read (device->port, answer, sizeof (answer), NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, sizeof (answer), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the response.");
|
||||
return status;
|
||||
@ -905,13 +905,13 @@ hw_ostc_device_fwupdate (dc_device_t *abstract, const char *filename)
|
||||
// bootloader needs to be send repeatedly, until the response packet is
|
||||
// received. Thus the time between each two attempts is directly controlled
|
||||
// by the timeout value.
|
||||
dc_serial_set_timeout (device->port, 300);
|
||||
dc_iostream_set_timeout (device->iostream, 300);
|
||||
|
||||
// Setup the bootloader.
|
||||
const unsigned int baudrates[] = {19200, 115200};
|
||||
for (unsigned int i = 0; i < C_ARRAY_SIZE(baudrates); ++i) {
|
||||
// Adjust the baudrate.
|
||||
rc = dc_serial_configure (device->port, baudrates[i], 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
rc = dc_iostream_configure (device->iostream, baudrates[i], 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to set the terminal attributes.");
|
||||
free (firmware);
|
||||
@ -931,7 +931,7 @@ hw_ostc_device_fwupdate (dc_device_t *abstract, const char *filename)
|
||||
}
|
||||
|
||||
// Increase the timeout again.
|
||||
dc_serial_set_timeout (device->port, 1000);
|
||||
dc_iostream_set_timeout (device->iostream, 1000);
|
||||
|
||||
// Enable progress notifications.
|
||||
dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
|
||||
|
||||
@ -91,7 +91,7 @@ typedef enum hw_ostc3_state_t {
|
||||
|
||||
typedef struct hw_ostc3_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned int hardware;
|
||||
unsigned int feature;
|
||||
unsigned int model;
|
||||
@ -197,7 +197,7 @@ hw_ostc3_transfer (hw_ostc3_device_t *device,
|
||||
|
||||
// Send the command.
|
||||
unsigned char command[1] = {cmd};
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -205,7 +205,7 @@ hw_ostc3_transfer (hw_ostc3_device_t *device,
|
||||
|
||||
// Read the echo.
|
||||
unsigned char echo[1] = {0};
|
||||
status = dc_serial_read (device->port, echo, sizeof (echo), NULL);
|
||||
status = dc_iostream_read (device->iostream, echo, sizeof (echo), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the echo.");
|
||||
return status;
|
||||
@ -234,7 +234,7 @@ hw_ostc3_transfer (hw_ostc3_device_t *device,
|
||||
len = isize - nbytes;
|
||||
|
||||
// Write the packet.
|
||||
status = dc_serial_write (device->port, input + nbytes, len, NULL);
|
||||
status = dc_iostream_write (device->iostream, input + nbytes, len, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the data packet.");
|
||||
return status;
|
||||
@ -258,7 +258,7 @@ hw_ostc3_transfer (hw_ostc3_device_t *device,
|
||||
|
||||
// Increase the packet size if more data is immediately available.
|
||||
size_t available = 0;
|
||||
status = dc_serial_get_available (device->port, &available);
|
||||
status = dc_iostream_get_available (device->iostream, &available);
|
||||
if (status == DC_STATUS_SUCCESS && available > len)
|
||||
len = available;
|
||||
|
||||
@ -267,7 +267,7 @@ hw_ostc3_transfer (hw_ostc3_device_t *device,
|
||||
len = osize - nbytes;
|
||||
|
||||
// Read the packet.
|
||||
status = dc_serial_read (device->port, output + nbytes, len, NULL);
|
||||
status = dc_iostream_read (device->iostream, output + nbytes, len, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -287,18 +287,18 @@ hw_ostc3_transfer (hw_ostc3_device_t *device,
|
||||
unsigned int count = delay / 100;
|
||||
for (unsigned int i = 0; i < count; ++i) {
|
||||
size_t available = 0;
|
||||
status = dc_serial_get_available (device->port, &available);
|
||||
status = dc_iostream_get_available (device->iostream, &available);
|
||||
if (status == DC_STATUS_SUCCESS && available > 0)
|
||||
break;
|
||||
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
}
|
||||
}
|
||||
|
||||
if (cmd != EXIT) {
|
||||
// Read the ready byte.
|
||||
unsigned char answer[1] = {0};
|
||||
status = dc_serial_read (device->port, answer, sizeof (answer), NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, sizeof (answer), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the ready byte.");
|
||||
return status;
|
||||
@ -332,36 +332,36 @@ hw_ostc3_device_open (dc_device_t **out, dc_context_t *context, const char *name
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
device->hardware = INVALID;
|
||||
device->feature = 0;
|
||||
device->model = 0;
|
||||
memset (device->fingerprint, 0, sizeof (device->fingerprint));
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (115200 8N1).
|
||||
status = dc_serial_configure (device->port, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (3000ms).
|
||||
status = dc_serial_set_timeout (device->port, 3000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 3000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_sleep (device->port, 300);
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_sleep (device->iostream, 300);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
device->state = OPEN;
|
||||
|
||||
@ -370,7 +370,7 @@ hw_ostc3_device_open (dc_device_t **out, dc_context_t *context, const char *name
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -434,17 +434,17 @@ hw_ostc3_device_init_service (hw_ostc3_device_t *device)
|
||||
unsigned char output[5];
|
||||
|
||||
// We cant use hw_ostc3_transfer here, due to the different echos
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to send the command.");
|
||||
return status;
|
||||
}
|
||||
|
||||
// Give the device some time to enter service mode
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
|
||||
// Read the response
|
||||
status = dc_serial_read (device->port, output, sizeof (output), NULL);
|
||||
status = dc_iostream_read (device->iostream, output, sizeof (output), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to receive the echo.");
|
||||
return status;
|
||||
@ -532,7 +532,7 @@ hw_ostc3_device_close (dc_device_t *abstract)
|
||||
}
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
|
||||
@ -52,7 +52,7 @@ mares_common_device_init (mares_common_device_t *device)
|
||||
assert (device != NULL);
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
device->echo = 0;
|
||||
device->delay = 0;
|
||||
}
|
||||
@ -88,11 +88,11 @@ mares_common_packet (mares_common_device_t *device, const unsigned char command[
|
||||
return DC_STATUS_CANCELLED;
|
||||
|
||||
if (device->delay) {
|
||||
dc_serial_sleep (device->port, device->delay);
|
||||
dc_iostream_sleep (device->iostream, device->delay);
|
||||
}
|
||||
|
||||
// Send the command to the device.
|
||||
status = dc_serial_write (device->port, command, csize, NULL);
|
||||
status = dc_iostream_write (device->iostream, command, csize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -101,7 +101,7 @@ mares_common_packet (mares_common_device_t *device, const unsigned char command[
|
||||
if (device->echo) {
|
||||
// Receive the echo of the command.
|
||||
unsigned char echo[PACKETSIZE] = {0};
|
||||
status = dc_serial_read (device->port, echo, csize, NULL);
|
||||
status = dc_iostream_read (device->iostream, echo, csize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the echo.");
|
||||
return status;
|
||||
@ -114,7 +114,7 @@ mares_common_packet (mares_common_device_t *device, const unsigned char command[
|
||||
}
|
||||
|
||||
// Receive the answer of the device.
|
||||
status = dc_serial_read (device->port, answer, asize, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, asize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -155,8 +155,8 @@ mares_common_transfer (mares_common_device_t *device, const unsigned char comman
|
||||
return rc;
|
||||
|
||||
// Discard any garbage bytes.
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_serial_purge (device->port, DC_DIRECTION_INPUT);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT);
|
||||
}
|
||||
|
||||
return rc;
|
||||
|
||||
@ -41,7 +41,7 @@ typedef struct mares_common_layout_t {
|
||||
|
||||
typedef struct mares_common_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned int echo;
|
||||
unsigned int delay;
|
||||
} mares_common_device_t;
|
||||
|
||||
@ -125,43 +125,43 @@ mares_darwin_device_open (dc_device_t **out, dc_context_t *context, const char *
|
||||
device->layout = &mares_darwin_layout;
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->base.port, context, name);
|
||||
status = dc_serial_open (&device->base.iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (9600 8N1).
|
||||
status = dc_serial_configure (device->base.port, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->base.iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (1000 ms).
|
||||
status = dc_serial_set_timeout (device->base.port, 1000);
|
||||
status = dc_iostream_set_timeout (device->base.iostream, 1000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the DTR line.
|
||||
status = dc_serial_set_dtr (device->base.port, 1);
|
||||
status = dc_iostream_set_dtr (device->base.iostream, 1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the DTR line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the RTS line.
|
||||
status = dc_serial_set_rts (device->base.port, 1);
|
||||
status = dc_iostream_set_rts (device->base.iostream, 1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the RTS line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_sleep (device->base.port, 100);
|
||||
dc_serial_purge (device->base.port, DC_DIRECTION_ALL);
|
||||
dc_iostream_sleep (device->base.iostream, 100);
|
||||
dc_iostream_purge (device->base.iostream, DC_DIRECTION_ALL);
|
||||
|
||||
// Override the base class values.
|
||||
device->base.echo = 1;
|
||||
@ -172,7 +172,7 @@ mares_darwin_device_open (dc_device_t **out, dc_context_t *context, const char *
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->base.port);
|
||||
dc_iostream_close (device->base.iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -186,7 +186,7 @@ mares_darwin_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->base.port);
|
||||
rc = dc_iostream_close (device->base.iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
|
||||
@ -65,7 +65,7 @@ typedef struct mares_iconhd_model_t {
|
||||
|
||||
typedef struct mares_iconhd_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
const mares_iconhd_layout_t *layout;
|
||||
unsigned char fingerprint[10];
|
||||
unsigned char version[140];
|
||||
@ -157,7 +157,7 @@ mares_iconhd_transfer (mares_iconhd_device_t *device,
|
||||
return DC_STATUS_CANCELLED;
|
||||
|
||||
// Send the command header to the dive computer.
|
||||
status = dc_serial_write (device->port, command, 2, NULL);
|
||||
status = dc_iostream_write (device->iostream, command, 2, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -165,7 +165,7 @@ mares_iconhd_transfer (mares_iconhd_device_t *device,
|
||||
|
||||
// Receive the header byte.
|
||||
unsigned char header[1] = {0};
|
||||
status = dc_serial_read (device->port, header, sizeof (header), NULL);
|
||||
status = dc_iostream_read (device->iostream, header, sizeof (header), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -179,7 +179,7 @@ mares_iconhd_transfer (mares_iconhd_device_t *device,
|
||||
|
||||
// Send the command payload to the dive computer.
|
||||
if (csize > 2) {
|
||||
status = dc_serial_write (device->port, command + 2, csize - 2, NULL);
|
||||
status = dc_iostream_write (device->iostream, command + 2, csize - 2, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -187,7 +187,7 @@ mares_iconhd_transfer (mares_iconhd_device_t *device,
|
||||
}
|
||||
|
||||
// Read the packet.
|
||||
status = dc_serial_read (device->port, answer, asize, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, asize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -195,7 +195,7 @@ mares_iconhd_transfer (mares_iconhd_device_t *device,
|
||||
|
||||
// Receive the trailer byte.
|
||||
unsigned char trailer[1] = {0};
|
||||
status = dc_serial_read (device->port, trailer, sizeof (trailer), NULL);
|
||||
status = dc_iostream_read (device->iostream, trailer, sizeof (trailer), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -228,7 +228,7 @@ mares_iconhd_device_open (dc_device_t **out, dc_context_t *context, const char *
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
device->layout = NULL;
|
||||
memset (device->fingerprint, 0, sizeof (device->fingerprint));
|
||||
memset (device->version, 0, sizeof (device->version));
|
||||
@ -236,42 +236,42 @@ mares_iconhd_device_open (dc_device_t **out, dc_context_t *context, const char *
|
||||
device->packetsize = 0;
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (115200 8E1).
|
||||
status = dc_serial_configure (device->port, 115200, 8, DC_PARITY_EVEN, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 115200, 8, DC_PARITY_EVEN, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (1000 ms).
|
||||
status = dc_serial_set_timeout (device->port, 1000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 1000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Clear the DTR line.
|
||||
status = dc_serial_set_dtr (device->port, 0);
|
||||
status = dc_iostream_set_dtr (device->iostream, 0);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to clear the DTR line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Clear the RTS line.
|
||||
status = dc_serial_set_rts (device->port, 0);
|
||||
status = dc_iostream_set_rts (device->iostream, 0);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to clear the RTS line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
// Send the version command.
|
||||
unsigned char command[] = {0xC2, 0x67};
|
||||
@ -315,7 +315,7 @@ mares_iconhd_device_open (dc_device_t **out, dc_context_t *context, const char *
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -330,7 +330,7 @@ mares_iconhd_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
|
||||
@ -45,7 +45,7 @@
|
||||
|
||||
typedef struct mares_nemo_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned char fingerprint[5];
|
||||
} mares_nemo_device_t;
|
||||
|
||||
@ -100,53 +100,53 @@ mares_nemo_device_open (dc_device_t **out, dc_context_t *context, const char *na
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
memset (device->fingerprint, 0, sizeof (device->fingerprint));
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (9600 8N1).
|
||||
status = dc_serial_configure (device->port, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (1000 ms).
|
||||
status = dc_serial_set_timeout (device->port, 1000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 1000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the DTR line.
|
||||
status = dc_serial_set_dtr (device->port, 1);
|
||||
status = dc_iostream_set_dtr (device->iostream, 1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the DTR line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the RTS line.
|
||||
status = dc_serial_set_rts (device->port, 1);
|
||||
status = dc_iostream_set_rts (device->iostream, 1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the RTS line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
*out = (dc_device_t*) device;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -161,7 +161,7 @@ mares_nemo_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -207,18 +207,18 @@ mares_nemo_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Wait until some data arrives.
|
||||
size_t available = 0;
|
||||
while (dc_serial_get_available (device->port, &available) == DC_STATUS_SUCCESS && available == 0) {
|
||||
while (dc_iostream_get_available (device->iostream, &available) == DC_STATUS_SUCCESS && available == 0) {
|
||||
if (device_is_cancelled (abstract))
|
||||
return DC_STATUS_CANCELLED;
|
||||
|
||||
device_event_emit (abstract, DC_EVENT_WAITING, NULL);
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
}
|
||||
|
||||
// Receive the header of the package.
|
||||
unsigned char header = 0x00;
|
||||
for (unsigned int i = 0; i < 20;) {
|
||||
status = dc_serial_read (device->port, &header, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, &header, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the header.");
|
||||
return status;
|
||||
@ -238,7 +238,7 @@ mares_nemo_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
while (nbytes < MEMORYSIZE) {
|
||||
// Read the packet.
|
||||
unsigned char packet[(PACKETSIZE + 1) * 2] = {0};
|
||||
status = dc_serial_read (device->port, packet, sizeof (packet), NULL);
|
||||
status = dc_iostream_read (device->iostream, packet, sizeof (packet), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
|
||||
@ -110,42 +110,42 @@ mares_puck_device_open (dc_device_t **out, dc_context_t *context, const char *na
|
||||
memset (device->fingerprint, 0, sizeof (device->fingerprint));
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->base.port, context, name);
|
||||
status = dc_serial_open (&device->base.iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (38400 8N1).
|
||||
status = dc_serial_configure (device->base.port, 38400, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->base.iostream, 38400, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (1000 ms).
|
||||
status = dc_serial_set_timeout (device->base.port, 1000);
|
||||
status = dc_iostream_set_timeout (device->base.iostream, 1000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Clear the DTR line.
|
||||
status = dc_serial_set_dtr (device->base.port, 0);
|
||||
status = dc_iostream_set_dtr (device->base.iostream, 0);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to clear the DTR line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Clear the RTS line.
|
||||
status = dc_serial_set_rts (device->base.port, 0);
|
||||
status = dc_iostream_set_rts (device->base.iostream, 0);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to clear the RTS line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_purge (device->base.port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->base.iostream, DC_DIRECTION_ALL);
|
||||
|
||||
// Identify the model number.
|
||||
unsigned char header[PACKETSIZE] = {0};
|
||||
@ -176,7 +176,7 @@ mares_puck_device_open (dc_device_t **out, dc_context_t *context, const char *na
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->base.port);
|
||||
dc_iostream_close (device->base.iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -191,7 +191,7 @@ mares_puck_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->base.port);
|
||||
rc = dc_iostream_close (device->base.iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
|
||||
@ -54,7 +54,7 @@
|
||||
|
||||
typedef struct oceanic_atom2_device_t {
|
||||
oceanic_common_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned int delay;
|
||||
unsigned int bigpage;
|
||||
unsigned char cache[256];
|
||||
@ -471,11 +471,11 @@ oceanic_atom2_packet (oceanic_atom2_device_t *device, const unsigned char comman
|
||||
return DC_STATUS_CANCELLED;
|
||||
|
||||
if (device->delay) {
|
||||
dc_serial_sleep (device->port, device->delay);
|
||||
dc_iostream_sleep (device->iostream, device->delay);
|
||||
}
|
||||
|
||||
// Send the command to the dive computer.
|
||||
status = dc_serial_write (device->port, command, csize, NULL);
|
||||
status = dc_iostream_write (device->iostream, command, csize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -489,7 +489,7 @@ oceanic_atom2_packet (oceanic_atom2_device_t *device, const unsigned char comman
|
||||
|
||||
// Receive the response (ACK/NAK) of the dive computer.
|
||||
unsigned char response = 0;
|
||||
status = dc_serial_read (device->port, &response, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, &response, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -503,7 +503,7 @@ oceanic_atom2_packet (oceanic_atom2_device_t *device, const unsigned char comman
|
||||
|
||||
if (asize) {
|
||||
// Receive the answer of the dive computer.
|
||||
status = dc_serial_read (device->port, answer, asize, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, asize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -552,8 +552,8 @@ oceanic_atom2_transfer (oceanic_atom2_device_t *device, const unsigned char comm
|
||||
device->delay++;
|
||||
|
||||
// Delay the next attempt.
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_serial_purge (device->port, DC_DIRECTION_INPUT);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT);
|
||||
}
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
@ -593,14 +593,14 @@ oceanic_atom2_device_open (dc_device_t **out, dc_context_t *context, const char
|
||||
oceanic_common_device_init (&device->base);
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
device->delay = 0;
|
||||
device->bigpage = 1; // no big pages
|
||||
device->cached = INVALID;
|
||||
memset(device->cache, 0, sizeof(device->cache));
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
@ -613,28 +613,28 @@ oceanic_atom2_device_open (dc_device_t **out, dc_context_t *context, const char
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (38400 8N1).
|
||||
status = dc_serial_configure (device->port, baudrate, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, baudrate, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (1000 ms).
|
||||
status = dc_serial_set_timeout (device->port, 1000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 1000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Give the interface 100 ms to settle and draw power up.
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
|
||||
// Set the DTR/RTS lines.
|
||||
dc_serial_set_dtr(device->port, 1);
|
||||
dc_serial_set_rts(device->port, 1);
|
||||
dc_iostream_set_dtr(device->iostream, 1);
|
||||
dc_iostream_set_rts(device->iostream, 1);
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
// Switch the device from surface mode into download mode. Before sending
|
||||
// this command, the device needs to be in PC mode (automatically activated
|
||||
@ -712,7 +712,7 @@ oceanic_atom2_device_open (dc_device_t **out, dc_context_t *context, const char
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -733,7 +733,7 @@ oceanic_atom2_device_close (dc_device_t *abstract)
|
||||
}
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@
|
||||
|
||||
typedef struct oceanic_veo250_device_t {
|
||||
oceanic_common_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned int last;
|
||||
} oceanic_veo250_device_t;
|
||||
|
||||
@ -99,10 +99,10 @@ oceanic_veo250_send (oceanic_veo250_device_t *device, const unsigned char comman
|
||||
return DC_STATUS_CANCELLED;
|
||||
|
||||
// Discard garbage bytes.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_INPUT);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT);
|
||||
|
||||
// Send the command to the dive computer.
|
||||
status = dc_serial_write (device->port, command, csize, NULL);
|
||||
status = dc_iostream_write (device->iostream, command, csize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -110,7 +110,7 @@ oceanic_veo250_send (oceanic_veo250_device_t *device, const unsigned char comman
|
||||
|
||||
// Receive the response (ACK/NAK) of the dive computer.
|
||||
unsigned char response = NAK;
|
||||
status = dc_serial_read (device->port, &response, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, &response, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -149,11 +149,11 @@ oceanic_veo250_transfer (oceanic_veo250_device_t *device, const unsigned char co
|
||||
return rc;
|
||||
|
||||
// Delay the next attempt.
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
}
|
||||
|
||||
// Receive the answer of the dive computer.
|
||||
status = dc_serial_read (device->port, answer, asize, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, asize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -177,7 +177,7 @@ oceanic_veo250_init (oceanic_veo250_device_t *device)
|
||||
|
||||
// Send the command to the dive computer.
|
||||
unsigned char command[2] = {0x55, 0x00};
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -186,7 +186,7 @@ oceanic_veo250_init (oceanic_veo250_device_t *device)
|
||||
// Receive the answer of the dive computer.
|
||||
size_t n = 0;
|
||||
unsigned char answer[13] = {0};
|
||||
status = dc_serial_read (device->port, answer, sizeof (answer), &n);
|
||||
status = dc_iostream_read (device->iostream, answer, sizeof (answer), &n);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
if (n == 0)
|
||||
@ -215,7 +215,7 @@ oceanic_veo250_quit (oceanic_veo250_device_t *device)
|
||||
|
||||
// Send the command to the dive computer.
|
||||
unsigned char command[2] = {0x98, 0x00};
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -248,49 +248,49 @@ oceanic_veo250_device_open (dc_device_t **out, dc_context_t *context, const char
|
||||
device->base.multipage = MULTIPAGE;
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
device->last = 0;
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (9600 8N1).
|
||||
status = dc_serial_configure (device->port, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (3000 ms).
|
||||
status = dc_serial_set_timeout (device->port, 3000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 3000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the DTR line.
|
||||
status = dc_serial_set_dtr (device->port, 1);
|
||||
status = dc_iostream_set_dtr (device->iostream, 1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the DTR line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the RTS line.
|
||||
status = dc_serial_set_rts (device->port, 1);
|
||||
status = dc_iostream_set_rts (device->iostream, 1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the RTS line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Give the interface 100 ms to settle and draw power up.
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
// Initialize the data cable (PPS mode).
|
||||
status = oceanic_veo250_init (device);
|
||||
@ -299,7 +299,7 @@ oceanic_veo250_device_open (dc_device_t **out, dc_context_t *context, const char
|
||||
}
|
||||
|
||||
// Delay the sending of the version command.
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
|
||||
// Switch the device from surface mode into download mode. Before sending
|
||||
// this command, the device needs to be in PC mode (manually activated by
|
||||
@ -322,7 +322,7 @@ oceanic_veo250_device_open (dc_device_t **out, dc_context_t *context, const char
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -343,7 +343,7 @@ oceanic_veo250_device_close (dc_device_t *abstract)
|
||||
}
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ typedef enum oceanic_vtpro_protocol_t {
|
||||
|
||||
typedef struct oceanic_vtpro_device_t {
|
||||
oceanic_common_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned int model;
|
||||
oceanic_vtpro_protocol_t protocol;
|
||||
} oceanic_vtpro_device_t;
|
||||
@ -140,7 +140,7 @@ oceanic_vtpro_send (oceanic_vtpro_device_t *device, const unsigned char command[
|
||||
return DC_STATUS_CANCELLED;
|
||||
|
||||
// Send the command to the dive computer.
|
||||
status = dc_serial_write (device->port, command, csize, NULL);
|
||||
status = dc_iostream_write (device->iostream, command, csize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -148,7 +148,7 @@ oceanic_vtpro_send (oceanic_vtpro_device_t *device, const unsigned char command[
|
||||
|
||||
// Receive the response (ACK/NAK) of the dive computer.
|
||||
unsigned char response = NAK;
|
||||
status = dc_serial_read (device->port, &response, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, &response, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -189,7 +189,7 @@ oceanic_vtpro_transfer (oceanic_vtpro_device_t *device, const unsigned char comm
|
||||
|
||||
if (asize) {
|
||||
// Receive the answer of the dive computer.
|
||||
status = dc_serial_read (device->port, answer, asize, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, asize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -210,7 +210,7 @@ oceanic_vtpro_init (oceanic_vtpro_device_t *device)
|
||||
unsigned char command[2][2] = {
|
||||
{0xAA, 0x00},
|
||||
{0x20, 0x00}};
|
||||
status = dc_serial_write (device->port, command[device->protocol], sizeof (command[device->protocol]), NULL);
|
||||
status = dc_iostream_write (device->iostream, command[device->protocol], sizeof (command[device->protocol]), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -218,7 +218,7 @@ oceanic_vtpro_init (oceanic_vtpro_device_t *device)
|
||||
|
||||
// Receive the answer of the dive computer.
|
||||
unsigned char answer[13] = {0};
|
||||
status = dc_serial_read (device->port, answer, sizeof (answer), NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, sizeof (answer), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -269,9 +269,9 @@ oceanic_vtpro_calibrate (oceanic_vtpro_device_t *device)
|
||||
// device needs approximately 6 seconds to respond.
|
||||
unsigned char answer[2] = {0};
|
||||
unsigned char command[2] = {0x18, 0x00};
|
||||
dc_serial_set_timeout (device->port, 9000);
|
||||
dc_iostream_set_timeout (device->iostream, 9000);
|
||||
dc_status_t rc = oceanic_vtpro_transfer (device, command, sizeof (command), answer, sizeof (answer));
|
||||
dc_serial_set_timeout (device->port, 3000);
|
||||
dc_iostream_set_timeout (device->iostream, 3000);
|
||||
if (rc != DC_STATUS_SUCCESS)
|
||||
return rc;
|
||||
|
||||
@ -337,7 +337,7 @@ oceanic_aeris500ai_device_logbook (dc_device_t *abstract, dc_event_progress_t *p
|
||||
for (unsigned int i = 0; i < last + 1; ++i) {
|
||||
// Receive the answer of the dive computer.
|
||||
unsigned char answer[PAGESIZE / 2 + 1] = {0};
|
||||
rc = dc_serial_read (device->port, answer, sizeof(answer), NULL);
|
||||
rc = dc_iostream_read (device->iostream, answer, sizeof(answer), NULL);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return rc;
|
||||
@ -407,7 +407,7 @@ oceanic_vtpro_device_open (dc_device_t **out, dc_context_t *context, const char
|
||||
device->base.multipage = MULTIPAGE;
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
device->model = model;
|
||||
if (model == AERIS500AI) {
|
||||
device->protocol = INTR;
|
||||
@ -416,45 +416,45 @@ oceanic_vtpro_device_open (dc_device_t **out, dc_context_t *context, const char
|
||||
}
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (9600 8N1).
|
||||
status = dc_serial_configure (device->port, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (3000 ms).
|
||||
status = dc_serial_set_timeout (device->port, 3000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 3000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the DTR line.
|
||||
status = dc_serial_set_dtr (device->port, 1);
|
||||
status = dc_iostream_set_dtr (device->iostream, 1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the DTR line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the RTS line.
|
||||
status = dc_serial_set_rts (device->port, 1);
|
||||
status = dc_iostream_set_rts (device->iostream, 1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the RTS line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Give the interface 100 ms to settle and draw power up.
|
||||
dc_serial_sleep (device->port, device->protocol == MOD ? 100 : 1000);
|
||||
dc_iostream_sleep (device->iostream, device->protocol == MOD ? 100 : 1000);
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
// Initialize the data cable (MOD mode).
|
||||
status = oceanic_vtpro_init (device);
|
||||
@ -495,7 +495,7 @@ oceanic_vtpro_device_open (dc_device_t **out, dc_context_t *context, const char
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -516,7 +516,7 @@ oceanic_vtpro_device_close (dc_device_t *abstract)
|
||||
}
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
|
||||
@ -36,7 +36,7 @@
|
||||
|
||||
typedef struct reefnet_sensus_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned char handshake[SZ_HANDSHAKE];
|
||||
unsigned int waiting;
|
||||
unsigned int timestamp;
|
||||
@ -72,7 +72,7 @@ reefnet_sensus_cancel (reefnet_sensus_device_t *device)
|
||||
|
||||
// Send the command to the device.
|
||||
unsigned char command = 0x00;
|
||||
status = dc_serial_write (device->port, &command, 1, NULL);
|
||||
status = dc_iostream_write (device->iostream, &command, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -102,7 +102,7 @@ reefnet_sensus_device_open (dc_device_t **out, dc_context_t *context, const char
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
device->waiting = 0;
|
||||
device->timestamp = 0;
|
||||
device->systime = (dc_ticks_t) -1;
|
||||
@ -110,35 +110,35 @@ reefnet_sensus_device_open (dc_device_t **out, dc_context_t *context, const char
|
||||
memset (device->handshake, 0, sizeof (device->handshake));
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (19200 8N1).
|
||||
status = dc_serial_configure (device->port, 19200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 19200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (3000 ms).
|
||||
status = dc_serial_set_timeout (device->port, 3000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 3000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
*out = (dc_device_t*) device;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -162,7 +162,7 @@ reefnet_sensus_device_close (dc_device_t *abstract)
|
||||
}
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -215,7 +215,7 @@ reefnet_sensus_handshake (reefnet_sensus_device_t *device)
|
||||
|
||||
// Send the command to the device.
|
||||
unsigned char command = 0x0A;
|
||||
status = dc_serial_write (device->port, &command, 1, NULL);
|
||||
status = dc_iostream_write (device->iostream, &command, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -223,7 +223,7 @@ reefnet_sensus_handshake (reefnet_sensus_device_t *device)
|
||||
|
||||
// Receive the answer from the device.
|
||||
unsigned char handshake[SZ_HANDSHAKE + 2] = {0};
|
||||
status = dc_serial_read (device->port, handshake, sizeof (handshake), NULL);
|
||||
status = dc_iostream_read (device->iostream, handshake, sizeof (handshake), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the handshake.");
|
||||
return status;
|
||||
@ -267,7 +267,7 @@ reefnet_sensus_handshake (reefnet_sensus_device_t *device)
|
||||
// Wait at least 10 ms to ensures the data line is
|
||||
// clear before transmission from the host begins.
|
||||
|
||||
dc_serial_sleep (device->port, 10);
|
||||
dc_iostream_sleep (device->iostream, 10);
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
@ -298,7 +298,7 @@ reefnet_sensus_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Send the command to the device.
|
||||
unsigned char command = 0x40;
|
||||
status = dc_serial_write (device->port, &command, 1, NULL);
|
||||
status = dc_iostream_write (device->iostream, &command, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -315,7 +315,7 @@ reefnet_sensus_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
if (len > 128)
|
||||
len = 128;
|
||||
|
||||
status = dc_serial_read (device->port, answer + nbytes, len, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer + nbytes, len, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
|
||||
@ -36,7 +36,7 @@
|
||||
|
||||
typedef struct reefnet_sensuspro_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned char handshake[SZ_HANDSHAKE];
|
||||
unsigned int timestamp;
|
||||
unsigned int devtime;
|
||||
@ -80,42 +80,42 @@ reefnet_sensuspro_device_open (dc_device_t **out, dc_context_t *context, const c
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
device->timestamp = 0;
|
||||
device->systime = (dc_ticks_t) -1;
|
||||
device->devtime = 0;
|
||||
memset (device->handshake, 0, sizeof (device->handshake));
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (19200 8N1).
|
||||
status = dc_serial_configure (device->port, 19200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 19200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (3000ms).
|
||||
status = dc_serial_set_timeout (device->port, 3000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 3000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
*out = (dc_device_t*) device;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -130,7 +130,7 @@ reefnet_sensuspro_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -182,18 +182,18 @@ reefnet_sensuspro_handshake (reefnet_sensuspro_device_t *device)
|
||||
dc_device_t *abstract = (dc_device_t *) device;
|
||||
|
||||
// Assert a break condition.
|
||||
dc_serial_set_break (device->port, 1);
|
||||
dc_iostream_set_break (device->iostream, 1);
|
||||
|
||||
// Receive the handshake from the dive computer.
|
||||
unsigned char handshake[SZ_HANDSHAKE + 2] = {0};
|
||||
status = dc_serial_read (device->port, handshake, sizeof (handshake), NULL);
|
||||
status = dc_iostream_read (device->iostream, handshake, sizeof (handshake), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the handshake.");
|
||||
return status;
|
||||
}
|
||||
|
||||
// Clear the break condition again.
|
||||
dc_serial_set_break (device->port, 0);
|
||||
dc_iostream_set_break (device->iostream, 0);
|
||||
|
||||
// Verify the checksum of the handshake packet.
|
||||
unsigned short crc = array_uint16_le (handshake + SZ_HANDSHAKE);
|
||||
@ -229,7 +229,7 @@ reefnet_sensuspro_handshake (reefnet_sensuspro_device_t *device)
|
||||
vendor.size = sizeof (device->handshake);
|
||||
device_event_emit (abstract, DC_EVENT_VENDOR, &vendor);
|
||||
|
||||
dc_serial_sleep (device->port, 10);
|
||||
dc_iostream_sleep (device->iostream, 10);
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
@ -247,7 +247,7 @@ reefnet_sensuspro_send (reefnet_sensuspro_device_t *device, unsigned char comman
|
||||
return rc;
|
||||
|
||||
// Send the instruction code to the device.
|
||||
status = dc_serial_write (device->port, &command, 1, NULL);
|
||||
status = dc_iostream_write (device->iostream, &command, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -287,7 +287,7 @@ reefnet_sensuspro_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
if (len > 256)
|
||||
len = 256;
|
||||
|
||||
status = dc_serial_read (device->port, answer + nbytes, len, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer + nbytes, len, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -352,9 +352,9 @@ reefnet_sensuspro_device_write_interval (dc_device_t *abstract, unsigned char in
|
||||
if (rc != DC_STATUS_SUCCESS)
|
||||
return rc;
|
||||
|
||||
dc_serial_sleep (device->port, 10);
|
||||
dc_iostream_sleep (device->iostream, 10);
|
||||
|
||||
status = dc_serial_write (device->port, &interval, 1, NULL);
|
||||
status = dc_iostream_write (device->iostream, &interval, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the data packet.");
|
||||
return status;
|
||||
|
||||
@ -45,7 +45,7 @@
|
||||
|
||||
typedef struct reefnet_sensusultra_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned char handshake[SZ_HANDSHAKE];
|
||||
unsigned int timestamp;
|
||||
unsigned int devtime;
|
||||
@ -87,42 +87,42 @@ reefnet_sensusultra_device_open (dc_device_t **out, dc_context_t *context, const
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
device->timestamp = 0;
|
||||
device->systime = (dc_ticks_t) -1;
|
||||
device->devtime = 0;
|
||||
memset (device->handshake, 0, sizeof (device->handshake));
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (115200 8N1).
|
||||
status = dc_serial_configure (device->port, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (3000ms).
|
||||
status = dc_serial_set_timeout (device->port, 3000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 3000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
*out = (dc_device_t*) device;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -137,7 +137,7 @@ reefnet_sensusultra_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -190,7 +190,7 @@ reefnet_sensusultra_send_uchar (reefnet_sensusultra_device_t *device, unsigned c
|
||||
|
||||
// Wait for the prompt byte.
|
||||
unsigned char prompt = 0;
|
||||
status = dc_serial_read (device->port, &prompt, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, &prompt, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the prompt byte");
|
||||
return status;
|
||||
@ -203,7 +203,7 @@ reefnet_sensusultra_send_uchar (reefnet_sensusultra_device_t *device, unsigned c
|
||||
}
|
||||
|
||||
// Send the value to the device.
|
||||
status = dc_serial_write (device->port, &value, 1, NULL);
|
||||
status = dc_iostream_write (device->iostream, &value, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the value.");
|
||||
return status;
|
||||
@ -244,7 +244,7 @@ reefnet_sensusultra_packet (reefnet_sensusultra_device_t *device, unsigned char
|
||||
return DC_STATUS_CANCELLED;
|
||||
|
||||
// Receive the data packet.
|
||||
status = dc_serial_read (device->port, data, size, NULL);
|
||||
status = dc_iostream_read (device->iostream, data, size, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the packet.");
|
||||
return status;
|
||||
@ -346,7 +346,7 @@ static dc_status_t
|
||||
reefnet_sensusultra_send (reefnet_sensusultra_device_t *device, unsigned short command)
|
||||
{
|
||||
// Flush the input and output buffers.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
// Wake-up the device and send the instruction code.
|
||||
unsigned int nretries = 0;
|
||||
@ -366,8 +366,8 @@ reefnet_sensusultra_send (reefnet_sensusultra_device_t *device, unsigned short c
|
||||
// not accidentally buffered by the host and (mis)interpreted as part
|
||||
// of the next packet.
|
||||
|
||||
dc_serial_sleep (device->port, 250);
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_sleep (device->iostream, 250);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
}
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
260
src/serial.h
260
src/serial.h
@ -24,64 +24,12 @@
|
||||
|
||||
#include <libdivecomputer/common.h>
|
||||
#include <libdivecomputer/context.h>
|
||||
#include <libdivecomputer/iostream.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
/**
|
||||
* Opaque object representing a serial connection.
|
||||
*/
|
||||
typedef struct dc_serial_t dc_serial_t;
|
||||
|
||||
/**
|
||||
* The parity checking scheme.
|
||||
*/
|
||||
typedef enum dc_parity_t {
|
||||
DC_PARITY_NONE, /**< No parity */
|
||||
DC_PARITY_ODD, /**< Odd parity */
|
||||
DC_PARITY_EVEN, /**< Even parity */
|
||||
DC_PARITY_MARK, /**< Mark parity (always 1) */
|
||||
DC_PARITY_SPACE /**< Space parity (alwasy 0) */
|
||||
} dc_parity_t;
|
||||
|
||||
/**
|
||||
* The number of stop bits.
|
||||
*/
|
||||
typedef enum dc_stopbits_t {
|
||||
DC_STOPBITS_ONE, /**< 1 stop bit */
|
||||
DC_STOPBITS_ONEPOINTFIVE, /**< 1.5 stop bits*/
|
||||
DC_STOPBITS_TWO /**< 2 stop bits */
|
||||
} dc_stopbits_t;
|
||||
|
||||
/**
|
||||
* The flow control.
|
||||
*/
|
||||
typedef enum dc_flowcontrol_t {
|
||||
DC_FLOWCONTROL_NONE, /**< No flow control */
|
||||
DC_FLOWCONTROL_HARDWARE, /**< Hardware (RTS/CTS) flow control */
|
||||
DC_FLOWCONTROL_SOFTWARE /**< Software (XON/XOFF) flow control */
|
||||
} dc_flowcontrol_t;
|
||||
|
||||
/**
|
||||
* The direction of the data transmission.
|
||||
*/
|
||||
typedef enum dc_direction_t {
|
||||
DC_DIRECTION_INPUT = 0x01, /**< Input direction */
|
||||
DC_DIRECTION_OUTPUT = 0x02, /**< Output direction */
|
||||
DC_DIRECTION_ALL = DC_DIRECTION_INPUT | DC_DIRECTION_OUTPUT /**< All directions */
|
||||
} dc_direction_t;
|
||||
|
||||
/**
|
||||
* The serial line signals.
|
||||
*/
|
||||
typedef enum dc_line_t {
|
||||
DC_LINE_DCD = 0x01, /**< Data carrier detect */
|
||||
DC_LINE_CTS = 0x02, /**< Clear to send */
|
||||
DC_LINE_DSR = 0x04, /**< Data set ready */
|
||||
DC_LINE_RNG = 0x08, /**< Ring indicator */
|
||||
} dc_line_t;
|
||||
|
||||
/**
|
||||
* Serial enumeration callback.
|
||||
*
|
||||
@ -104,216 +52,14 @@ dc_serial_enumerate (dc_serial_callback_t callback, void *userdata);
|
||||
/**
|
||||
* Open a serial connection.
|
||||
*
|
||||
* @param[out] serial A location to store the serial connection.
|
||||
* @param[out] iostream A location to store the serial connection.
|
||||
* @param[in] context A valid context object.
|
||||
* @param[in] name The name of the device node.
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_open (dc_serial_t **serial, dc_context_t *context, const char *name);
|
||||
|
||||
/**
|
||||
* Close the serial connection and free all resources.
|
||||
*
|
||||
* @param[in] serial A valid serial connection.
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_close (dc_serial_t *serial);
|
||||
|
||||
/**
|
||||
* Configure the serial line settings of the connection.
|
||||
*
|
||||
* @param[in] serial A valid serial connection.
|
||||
* @param[in] baudrate The baud rate setting.
|
||||
* @param[in] databits The number of data bits.
|
||||
* @param[in] parity The parity setting.
|
||||
* @param[in] stopbits The number of stop bits.
|
||||
* @param[in] flowcontrol The flow control setting.
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_configure (dc_serial_t *serial, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol);
|
||||
|
||||
/**
|
||||
* Set the read timeout.
|
||||
*
|
||||
* There are three distinct modes available:
|
||||
*
|
||||
* 1. Blocking (timeout < 0):
|
||||
*
|
||||
* The read operation is blocked until all the requested bytes have
|
||||
* been received. If the requested number of bytes does not arrive,
|
||||
* the operation will block forever.
|
||||
*
|
||||
* 2. Non-blocking (timeout == 0):
|
||||
*
|
||||
* The read operation returns immediately with the bytes that have
|
||||
* already been received, even if no bytes have been received.
|
||||
*
|
||||
* 3. Timeout (timeout > 0):
|
||||
*
|
||||
* The read operation is blocked until all the requested bytes have
|
||||
* been received. If the requested number of bytes does not arrive
|
||||
* within the specified amount of time, the operation will return
|
||||
* with the bytes that have already been received.
|
||||
*
|
||||
* @param[in] serial A valid serial connection.
|
||||
* @param[in] timeout The timeout in milliseconds.
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_set_timeout (dc_serial_t *serial, int timeout);
|
||||
|
||||
/**
|
||||
* Set the state of the half duplex emulation.
|
||||
*
|
||||
* @param[in] serial A valid serial connection.
|
||||
* @param[in] value The half duplex state.
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_set_halfduplex (dc_serial_t *serial, unsigned int value);
|
||||
|
||||
/**
|
||||
* Set the receive latency.
|
||||
*
|
||||
* The effect of this setting is highly platform and driver specific. On
|
||||
* Windows it does nothing at all, on Linux it controls the low latency
|
||||
* flag (e.g. only zero vs non-zero latency), and on Mac OS X it sets
|
||||
* the receive latency as requested.
|
||||
*
|
||||
* @param[in] serial A valid serial connection.
|
||||
* @param[in] value The latency in milliseconds.
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_set_latency (dc_serial_t *serial, unsigned int value);
|
||||
|
||||
/**
|
||||
* Read data from the serial connection.
|
||||
*
|
||||
* @param[in] serial A valid serial connection.
|
||||
* @param[out] data The memory buffer to read the data into.
|
||||
* @param[in] size The number of bytes to read.
|
||||
* @param[out] actual An (optional) location to store the actual
|
||||
* number of bytes transferred.
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_read (dc_serial_t *serial, void *data, size_t size, size_t *actual);
|
||||
|
||||
/**
|
||||
* Write data to the serial connection.
|
||||
*
|
||||
* @param[in] serial A valid serial connection.
|
||||
* @param[in] data The memory buffer to write the data from.
|
||||
* @param[in] size The number of bytes to write.
|
||||
* @param[out] actual An (optional) location to store the actual
|
||||
* number of bytes transferred.
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_write (dc_serial_t *serial, const void *data, size_t size, size_t *actual);
|
||||
|
||||
/**
|
||||
* Flush the internal output buffer and wait until the data has been
|
||||
* transmitted.
|
||||
*
|
||||
* @param[in] serial A valid serial connection.
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_flush (dc_serial_t *serial);
|
||||
|
||||
/**
|
||||
* Discards all data from the internal buffers.
|
||||
*
|
||||
* @param[in] serial A valid serial connection.
|
||||
* @param[in] direction The direction of the buffer(s).
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_purge (dc_serial_t *serial, dc_direction_t direction);
|
||||
|
||||
/**
|
||||
* Set the state of the break condition.
|
||||
*
|
||||
* @param[in] serial A valid serial connection.
|
||||
* @param[in] value The break condition state.
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_set_break (dc_serial_t *serial, unsigned int value);
|
||||
|
||||
/**
|
||||
* Set the state of the DTR line.
|
||||
*
|
||||
* @param[in] serial A valid serial connection.
|
||||
* @param[in] value The DTR line state.
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_set_dtr (dc_serial_t *serial, unsigned int value);
|
||||
|
||||
/**
|
||||
* Set the state of the RTS line.
|
||||
*
|
||||
* @param[in] serial A valid serial connection.
|
||||
* @param[in] value The RTS line state.
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_set_rts (dc_serial_t *serial, unsigned int value);
|
||||
|
||||
/**
|
||||
* Query the number of available bytes in the input buffer.
|
||||
*
|
||||
* @param[in] serial A valid serial connection.
|
||||
* @param[out] value A location to store the number of bytes in the
|
||||
* input buffer.
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_get_available (dc_serial_t *serial, size_t *value);
|
||||
|
||||
/**
|
||||
* Query the state of the line signals.
|
||||
*
|
||||
* @param[in] serial A valid serial connection.
|
||||
* @param[out] value A location to store the bitmap with the state of
|
||||
* the line signals.
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_get_lines (dc_serial_t *serial, unsigned int *value);
|
||||
|
||||
/**
|
||||
* Suspend execution of the current thread for the specified amount of
|
||||
* time.
|
||||
*
|
||||
* @param[in] serial A valid serial connection.
|
||||
* @param[in] milliseconds The number of milliseconds to sleep.
|
||||
* @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
|
||||
* on failure.
|
||||
*/
|
||||
dc_status_t
|
||||
dc_serial_sleep (dc_serial_t *serial, unsigned int milliseconds);
|
||||
dc_serial_open (dc_iostream_t **iostream, dc_context_t *context, const char *name);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@ -54,12 +54,29 @@
|
||||
#endif
|
||||
|
||||
#include "serial.h"
|
||||
|
||||
#include "common-private.h"
|
||||
#include "context-private.h"
|
||||
#include "iostream-private.h"
|
||||
|
||||
struct dc_serial_t {
|
||||
/* Library context. */
|
||||
dc_context_t *context;
|
||||
static dc_status_t dc_serial_set_timeout (dc_iostream_t *iostream, int timeout);
|
||||
static dc_status_t dc_serial_set_latency (dc_iostream_t *iostream, unsigned int value);
|
||||
static dc_status_t dc_serial_set_halfduplex (dc_iostream_t *iostream, unsigned int value);
|
||||
static dc_status_t dc_serial_set_break (dc_iostream_t *iostream, unsigned int value);
|
||||
static dc_status_t dc_serial_set_dtr (dc_iostream_t *iostream, unsigned int value);
|
||||
static dc_status_t dc_serial_set_rts (dc_iostream_t *iostream, unsigned int value);
|
||||
static dc_status_t dc_serial_get_lines (dc_iostream_t *iostream, unsigned int *value);
|
||||
static dc_status_t dc_serial_get_available (dc_iostream_t *iostream, size_t *value);
|
||||
static dc_status_t dc_serial_configure (dc_iostream_t *iostream, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol);
|
||||
static dc_status_t dc_serial_read (dc_iostream_t *iostream, void *data, size_t size, size_t *actual);
|
||||
static dc_status_t dc_serial_write (dc_iostream_t *iostream, const void *data, size_t size, size_t *actual);
|
||||
static dc_status_t dc_serial_flush (dc_iostream_t *iostream);
|
||||
static dc_status_t dc_serial_purge (dc_iostream_t *iostream, dc_direction_t direction);
|
||||
static dc_status_t dc_serial_sleep (dc_iostream_t *iostream, unsigned int milliseconds);
|
||||
static dc_status_t dc_serial_close (dc_iostream_t *iostream);
|
||||
|
||||
typedef struct dc_serial_t {
|
||||
dc_iostream_t base;
|
||||
/*
|
||||
* The file descriptor corresponding to the serial port.
|
||||
*/
|
||||
@ -75,6 +92,25 @@ struct dc_serial_t {
|
||||
int halfduplex;
|
||||
unsigned int baudrate;
|
||||
unsigned int nbits;
|
||||
} dc_serial_t;
|
||||
|
||||
static const dc_iostream_vtable_t dc_serial_vtable = {
|
||||
sizeof(dc_serial_t),
|
||||
dc_serial_set_timeout, /* set_timeout */
|
||||
dc_serial_set_latency, /* set_latency */
|
||||
dc_serial_set_halfduplex, /* set_halfduplex */
|
||||
dc_serial_set_break, /* set_break */
|
||||
dc_serial_set_dtr, /* set_dtr */
|
||||
dc_serial_set_rts, /* set_rts */
|
||||
dc_serial_get_lines, /* get_lines */
|
||||
dc_serial_get_available, /* get_received */
|
||||
dc_serial_configure, /* configure */
|
||||
dc_serial_read, /* read */
|
||||
dc_serial_write, /* write */
|
||||
dc_serial_flush, /* flush */
|
||||
dc_serial_purge, /* purge */
|
||||
dc_serial_sleep, /* sleep */
|
||||
dc_serial_close, /* close */
|
||||
};
|
||||
|
||||
static dc_status_t
|
||||
@ -140,9 +176,10 @@ dc_serial_enumerate (dc_serial_callback_t callback, void *userdata)
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_open (dc_serial_t **out, dc_context_t *context, const char *name)
|
||||
dc_serial_open (dc_iostream_t **out, dc_context_t *context, const char *name)
|
||||
{
|
||||
dc_status_t status = DC_STATUS_SUCCESS;
|
||||
dc_serial_t *device = NULL;
|
||||
|
||||
if (out == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
@ -150,15 +187,12 @@ dc_serial_open (dc_serial_t **out, dc_context_t *context, const char *name)
|
||||
INFO (context, "Open: name=%s", name ? name : "");
|
||||
|
||||
// Allocate memory.
|
||||
dc_serial_t *device = (dc_serial_t *) malloc (sizeof (dc_serial_t));
|
||||
device = (dc_serial_t *) dc_iostream_allocate (context, &dc_serial_vtable);
|
||||
if (device == NULL) {
|
||||
SYSERROR (context, ENOMEM);
|
||||
return DC_STATUS_NOMEMORY;
|
||||
}
|
||||
|
||||
// Library context.
|
||||
device->context = context;
|
||||
|
||||
// Default to blocking reads.
|
||||
device->timeout = -1;
|
||||
|
||||
@ -198,29 +232,27 @@ dc_serial_open (dc_serial_t **out, dc_context_t *context, const char *name)
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
*out = device;
|
||||
*out = (dc_iostream_t *) device;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
close (device->fd);
|
||||
error_free:
|
||||
free (device);
|
||||
dc_iostream_deallocate ((dc_iostream_t *) device);
|
||||
return status;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_close (dc_serial_t *device)
|
||||
static dc_status_t
|
||||
dc_serial_close (dc_iostream_t *abstract)
|
||||
{
|
||||
dc_status_t status = DC_STATUS_SUCCESS;
|
||||
|
||||
if (device == NULL)
|
||||
return DC_STATUS_SUCCESS;
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
// Restore the initial terminal attributes.
|
||||
if (tcsetattr (device->fd, TCSANOW, &device->tty) != 0) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
dc_status_set_error(&status, syserror (errcode));
|
||||
}
|
||||
|
||||
@ -232,31 +264,24 @@ dc_serial_close (dc_serial_t *device)
|
||||
// Close the device.
|
||||
if (close (device->fd) != 0) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
dc_status_set_error(&status, syserror (errcode));
|
||||
}
|
||||
|
||||
// Free memory.
|
||||
free (device);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_configure (dc_serial_t *device, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol)
|
||||
static dc_status_t
|
||||
dc_serial_configure (dc_iostream_t *abstract, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "Configure: baudrate=%i, databits=%i, parity=%i, stopbits=%i, flowcontrol=%i",
|
||||
baudrate, databits, parity, stopbits, flowcontrol);
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
// Retrieve the current settings.
|
||||
struct termios tty;
|
||||
memset (&tty, 0, sizeof (tty));
|
||||
if (tcgetattr (device->fd, &tty) != 0) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
@ -353,7 +378,7 @@ dc_serial_configure (dc_serial_t *device, unsigned int baudrate, unsigned int da
|
||||
if (cfsetispeed (&tty, baud) != 0 ||
|
||||
cfsetospeed (&tty, baud) != 0) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
@ -450,7 +475,7 @@ dc_serial_configure (dc_serial_t *device, unsigned int baudrate, unsigned int da
|
||||
// Apply the new settings.
|
||||
if (tcsetattr (device->fd, TCSANOW, &tty) != 0 && NOPTY) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
@ -461,7 +486,7 @@ dc_serial_configure (dc_serial_t *device, unsigned int baudrate, unsigned int da
|
||||
struct serial_struct ss;
|
||||
if (ioctl (device->fd, TIOCGSERIAL, &ss) != 0 && NOPTY) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
@ -473,14 +498,14 @@ dc_serial_configure (dc_serial_t *device, unsigned int baudrate, unsigned int da
|
||||
// Apply the new settings.
|
||||
if (ioctl (device->fd, TIOCSSERIAL, &ss) != 0 && NOPTY) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
#elif defined(IOSSIOSPEED)
|
||||
speed_t speed = baudrate;
|
||||
if (ioctl (device->fd, IOSSIOSPEED, &speed) != 0 && NOPTY) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
#else
|
||||
@ -495,42 +520,37 @@ dc_serial_configure (dc_serial_t *device, unsigned int baudrate, unsigned int da
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_set_timeout (dc_serial_t *device, int timeout)
|
||||
static dc_status_t
|
||||
dc_serial_set_timeout (dc_iostream_t *abstract, int timeout)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "Timeout: value=%i", timeout);
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
device->timeout = timeout;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_set_halfduplex (dc_serial_t *device, unsigned int value)
|
||||
static dc_status_t
|
||||
dc_serial_set_halfduplex (dc_iostream_t *abstract, unsigned int value)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
device->halfduplex = value;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_set_latency (dc_serial_t *device, unsigned int milliseconds)
|
||||
static dc_status_t
|
||||
dc_serial_set_latency (dc_iostream_t *abstract, unsigned int milliseconds)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
#if defined(TIOCGSERIAL) && defined(TIOCSSERIAL) && !defined(__ANDROID__)
|
||||
// Get the current settings.
|
||||
struct serial_struct ss;
|
||||
if (ioctl (device->fd, TIOCGSERIAL, &ss) != 0 && NOPTY) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
@ -544,7 +564,7 @@ dc_serial_set_latency (dc_serial_t *device, unsigned int milliseconds)
|
||||
// Apply the new settings.
|
||||
if (ioctl (device->fd, TIOCSSERIAL, &ss) != 0 && NOPTY) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
#elif defined(IOSSDATALAT)
|
||||
@ -554,7 +574,7 @@ dc_serial_set_latency (dc_serial_t *device, unsigned int milliseconds)
|
||||
unsigned long usec = (milliseconds == 0 ? 1 : milliseconds * 1000);
|
||||
if (ioctl (device->fd, IOSSDATALAT, &usec) != 0 && NOPTY) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
#endif
|
||||
@ -562,17 +582,13 @@ dc_serial_set_latency (dc_serial_t *device, unsigned int milliseconds)
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_read (dc_serial_t *device, void *data, size_t size, size_t *actual)
|
||||
static dc_status_t
|
||||
dc_serial_read (dc_iostream_t *abstract, void *data, size_t size, size_t *actual)
|
||||
{
|
||||
dc_status_t status = DC_STATUS_SUCCESS;
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
size_t nbytes = 0;
|
||||
|
||||
if (device == NULL) {
|
||||
status = DC_STATUS_INVALIDARGS;
|
||||
goto out_invalidargs;
|
||||
}
|
||||
|
||||
// The total timeout.
|
||||
int timeout = device->timeout;
|
||||
|
||||
@ -590,7 +606,7 @@ dc_serial_read (dc_serial_t *device, void *data, size_t size, size_t *actual)
|
||||
struct timeval now;
|
||||
if (gettimeofday (&now, NULL) != 0) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
status = syserror (errcode);
|
||||
goto out;
|
||||
}
|
||||
@ -618,7 +634,7 @@ dc_serial_read (dc_serial_t *device, void *data, size_t size, size_t *actual)
|
||||
int errcode = errno;
|
||||
if (errcode == EINTR)
|
||||
continue; // Retry.
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
status = syserror (errcode);
|
||||
goto out;
|
||||
} else if (rc == 0) {
|
||||
@ -630,7 +646,7 @@ dc_serial_read (dc_serial_t *device, void *data, size_t size, size_t *actual)
|
||||
int errcode = errno;
|
||||
if (errcode == EINTR || errcode == EAGAIN)
|
||||
continue; // Retry.
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
status = syserror (errcode);
|
||||
goto out;
|
||||
} else if (n == 0) {
|
||||
@ -645,32 +661,25 @@ dc_serial_read (dc_serial_t *device, void *data, size_t size, size_t *actual)
|
||||
}
|
||||
|
||||
out:
|
||||
HEXDUMP (device->context, DC_LOGLEVEL_INFO, "Read", (unsigned char *) data, nbytes);
|
||||
|
||||
out_invalidargs:
|
||||
if (actual)
|
||||
*actual = nbytes;
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_write (dc_serial_t *device, const void *data, size_t size, size_t *actual)
|
||||
static dc_status_t
|
||||
dc_serial_write (dc_iostream_t *abstract, const void *data, size_t size, size_t *actual)
|
||||
{
|
||||
dc_status_t status = DC_STATUS_SUCCESS;
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
size_t nbytes = 0;
|
||||
|
||||
if (device == NULL) {
|
||||
status = DC_STATUS_INVALIDARGS;
|
||||
goto out_invalidargs;
|
||||
}
|
||||
|
||||
struct timeval tve, tvb;
|
||||
if (device->halfduplex) {
|
||||
// Get the current time.
|
||||
if (gettimeofday (&tvb, NULL) != 0) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
status = syserror (errcode);
|
||||
goto out;
|
||||
}
|
||||
@ -686,7 +695,7 @@ dc_serial_write (dc_serial_t *device, const void *data, size_t size, size_t *act
|
||||
int errcode = errno;
|
||||
if (errcode == EINTR)
|
||||
continue; // Retry.
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
status = syserror (errcode);
|
||||
goto out;
|
||||
} else if (rc == 0) {
|
||||
@ -698,7 +707,7 @@ dc_serial_write (dc_serial_t *device, const void *data, size_t size, size_t *act
|
||||
int errcode = errno;
|
||||
if (errcode == EINTR || errcode == EAGAIN)
|
||||
continue; // Retry.
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
status = syserror (errcode);
|
||||
goto out;
|
||||
} else if (n == 0) {
|
||||
@ -717,7 +726,7 @@ dc_serial_write (dc_serial_t *device, const void *data, size_t size, size_t *act
|
||||
#endif
|
||||
int errcode = errno;
|
||||
if (errcode != EINTR ) {
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
status = syserror (errcode);
|
||||
goto out;
|
||||
}
|
||||
@ -727,7 +736,7 @@ dc_serial_write (dc_serial_t *device, const void *data, size_t size, size_t *act
|
||||
// Get the current time.
|
||||
if (gettimeofday (&tve, NULL) != 0) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
status = syserror (errcode);
|
||||
goto out;
|
||||
}
|
||||
@ -748,27 +757,21 @@ dc_serial_write (dc_serial_t *device, const void *data, size_t size, size_t *act
|
||||
// The remaining time is rounded up to the nearest millisecond to
|
||||
// match the Windows implementation. The higher resolution is
|
||||
// pointless anyway, since we already added a fudge factor above.
|
||||
dc_serial_sleep (device, (remaining + 999) / 1000);
|
||||
dc_serial_sleep (abstract, (remaining + 999) / 1000);
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
HEXDUMP (device->context, DC_LOGLEVEL_INFO, "Write", (const unsigned char *) data, nbytes);
|
||||
|
||||
out_invalidargs:
|
||||
if (actual)
|
||||
*actual = nbytes;
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_purge (dc_serial_t *device, dc_direction_t direction)
|
||||
static dc_status_t
|
||||
dc_serial_purge (dc_iostream_t *abstract, dc_direction_t direction)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "Purge: direction=%u", direction);
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
int flags = 0;
|
||||
|
||||
@ -788,93 +791,78 @@ dc_serial_purge (dc_serial_t *device, dc_direction_t direction)
|
||||
|
||||
if (tcflush (device->fd, flags) != 0) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_flush (dc_serial_t *device)
|
||||
static dc_status_t
|
||||
dc_serial_flush (dc_iostream_t *abstract)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "Flush: none");
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_set_break (dc_serial_t *device, unsigned int level)
|
||||
static dc_status_t
|
||||
dc_serial_set_break (dc_iostream_t *abstract, unsigned int level)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "Break: value=%i", level);
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
unsigned long action = (level ? TIOCSBRK : TIOCCBRK);
|
||||
|
||||
if (ioctl (device->fd, action, NULL) != 0 && NOPTY) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_set_dtr (dc_serial_t *device, unsigned int level)
|
||||
static dc_status_t
|
||||
dc_serial_set_dtr (dc_iostream_t *abstract, unsigned int level)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "DTR: value=%i", level);
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
unsigned long action = (level ? TIOCMBIS : TIOCMBIC);
|
||||
|
||||
int value = TIOCM_DTR;
|
||||
if (ioctl (device->fd, action, &value) != 0 && NOPTY) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_set_rts (dc_serial_t *device, unsigned int level)
|
||||
static dc_status_t
|
||||
dc_serial_set_rts (dc_iostream_t *abstract, unsigned int level)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "RTS: value=%i", level);
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
unsigned long action = (level ? TIOCMBIS : TIOCMBIC);
|
||||
|
||||
int value = TIOCM_RTS;
|
||||
if (ioctl (device->fd, action, &value) != 0 && NOPTY) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_get_available (dc_serial_t *device, size_t *value)
|
||||
static dc_status_t
|
||||
dc_serial_get_available (dc_iostream_t *abstract, size_t *value)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
int bytes = 0;
|
||||
if (ioctl (device->fd, TIOCINQ, &bytes) != 0) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
@ -884,18 +872,16 @@ dc_serial_get_available (dc_serial_t *device, size_t *value)
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_get_lines (dc_serial_t *device, unsigned int *value)
|
||||
static dc_status_t
|
||||
dc_serial_get_lines (dc_iostream_t *abstract, unsigned int *value)
|
||||
{
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
unsigned int lines = 0;
|
||||
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
int status = 0;
|
||||
if (ioctl (device->fd, TIOCMGET, &status) != 0) {
|
||||
int errcode = errno;
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
@ -914,14 +900,9 @@ dc_serial_get_lines (dc_serial_t *device, unsigned int *value)
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_sleep (dc_serial_t *device, unsigned int timeout)
|
||||
static dc_status_t
|
||||
dc_serial_sleep (dc_iostream_t *abstract, unsigned int timeout)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "Sleep: value=%u", timeout);
|
||||
|
||||
struct timespec ts;
|
||||
ts.tv_sec = (timeout / 1000);
|
||||
ts.tv_nsec = (timeout % 1000) * 1000000;
|
||||
@ -929,7 +910,7 @@ dc_serial_sleep (dc_serial_t *device, unsigned int timeout)
|
||||
while (nanosleep (&ts, &ts) != 0) {
|
||||
int errcode = errno;
|
||||
if (errcode != EINTR ) {
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,12 +25,29 @@
|
||||
#include <windows.h>
|
||||
|
||||
#include "serial.h"
|
||||
|
||||
#include "common-private.h"
|
||||
#include "context-private.h"
|
||||
#include "iostream-private.h"
|
||||
|
||||
struct dc_serial_t {
|
||||
/* Library context. */
|
||||
dc_context_t *context;
|
||||
static dc_status_t dc_serial_set_timeout (dc_iostream_t *iostream, int timeout);
|
||||
static dc_status_t dc_serial_set_latency (dc_iostream_t *iostream, unsigned int value);
|
||||
static dc_status_t dc_serial_set_halfduplex (dc_iostream_t *iostream, unsigned int value);
|
||||
static dc_status_t dc_serial_set_break (dc_iostream_t *iostream, unsigned int value);
|
||||
static dc_status_t dc_serial_set_dtr (dc_iostream_t *iostream, unsigned int value);
|
||||
static dc_status_t dc_serial_set_rts (dc_iostream_t *iostream, unsigned int value);
|
||||
static dc_status_t dc_serial_get_lines (dc_iostream_t *iostream, unsigned int *value);
|
||||
static dc_status_t dc_serial_get_available (dc_iostream_t *iostream, size_t *value);
|
||||
static dc_status_t dc_serial_configure (dc_iostream_t *iostream, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol);
|
||||
static dc_status_t dc_serial_read (dc_iostream_t *iostream, void *data, size_t size, size_t *actual);
|
||||
static dc_status_t dc_serial_write (dc_iostream_t *iostream, const void *data, size_t size, size_t *actual);
|
||||
static dc_status_t dc_serial_flush (dc_iostream_t *iostream);
|
||||
static dc_status_t dc_serial_purge (dc_iostream_t *iostream, dc_direction_t direction);
|
||||
static dc_status_t dc_serial_sleep (dc_iostream_t *iostream, unsigned int milliseconds);
|
||||
static dc_status_t dc_serial_close (dc_iostream_t *iostream);
|
||||
|
||||
typedef struct dc_serial_t {
|
||||
dc_iostream_t base;
|
||||
/*
|
||||
* The file descriptor corresponding to the serial port.
|
||||
*/
|
||||
@ -46,6 +63,25 @@ struct dc_serial_t {
|
||||
int halfduplex;
|
||||
unsigned int baudrate;
|
||||
unsigned int nbits;
|
||||
} dc_serial_t;
|
||||
|
||||
static const dc_iostream_vtable_t dc_serial_vtable = {
|
||||
sizeof(dc_serial_t),
|
||||
dc_serial_set_timeout, /* set_timeout */
|
||||
dc_serial_set_latency, /* set_latency */
|
||||
dc_serial_set_halfduplex, /* set_halfduplex */
|
||||
dc_serial_set_break, /* set_break */
|
||||
dc_serial_set_dtr, /* set_dtr */
|
||||
dc_serial_set_rts, /* set_rts */
|
||||
dc_serial_get_lines, /* get_lines */
|
||||
dc_serial_get_available, /* get_received */
|
||||
dc_serial_configure, /* configure */
|
||||
dc_serial_read, /* read */
|
||||
dc_serial_write, /* write */
|
||||
dc_serial_flush, /* flush */
|
||||
dc_serial_purge, /* purge */
|
||||
dc_serial_sleep, /* sleep */
|
||||
dc_serial_close, /* close */
|
||||
};
|
||||
|
||||
static dc_status_t
|
||||
@ -120,9 +156,10 @@ dc_serial_enumerate (dc_serial_callback_t callback, void *userdata)
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_open (dc_serial_t **out, dc_context_t *context, const char *name)
|
||||
dc_serial_open (dc_iostream_t **out, dc_context_t *context, const char *name)
|
||||
{
|
||||
dc_status_t status = DC_STATUS_SUCCESS;
|
||||
dc_serial_t *device = NULL;
|
||||
|
||||
if (out == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
@ -143,15 +180,12 @@ dc_serial_open (dc_serial_t **out, dc_context_t *context, const char *name)
|
||||
}
|
||||
|
||||
// Allocate memory.
|
||||
dc_serial_t *device = (dc_serial_t *) malloc (sizeof (dc_serial_t));
|
||||
device = (dc_serial_t *) dc_iostream_allocate (context, &dc_serial_vtable);
|
||||
if (device == NULL) {
|
||||
SYSERROR (context, ERROR_OUTOFMEMORY);
|
||||
return DC_STATUS_NOMEMORY;
|
||||
}
|
||||
|
||||
// Library context.
|
||||
device->context = context;
|
||||
|
||||
// Default to full-duplex.
|
||||
device->halfduplex = 0;
|
||||
device->baudrate = 0;
|
||||
@ -183,60 +217,51 @@ dc_serial_open (dc_serial_t **out, dc_context_t *context, const char *name)
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
*out = device;
|
||||
*out = (dc_iostream_t *) device;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
CloseHandle (device->hFile);
|
||||
error_free:
|
||||
free (device);
|
||||
dc_iostream_deallocate ((dc_iostream_t *) device);
|
||||
return status;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_close (dc_serial_t *device)
|
||||
static dc_status_t
|
||||
dc_serial_close (dc_iostream_t *abstract)
|
||||
{
|
||||
dc_status_t status = DC_STATUS_SUCCESS;
|
||||
|
||||
if (device == NULL)
|
||||
return DC_STATUS_SUCCESS;
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
// Restore the initial communication settings and timeouts.
|
||||
if (!SetCommState (device->hFile, &device->dcb) ||
|
||||
!SetCommTimeouts (device->hFile, &device->timeouts)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
dc_status_set_error(&status, syserror (errcode));
|
||||
}
|
||||
|
||||
// Close the device.
|
||||
if (!CloseHandle (device->hFile)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
dc_status_set_error(&status, syserror (errcode));
|
||||
}
|
||||
|
||||
// Free memory.
|
||||
free (device);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_configure (dc_serial_t *device, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol)
|
||||
static dc_status_t
|
||||
dc_serial_configure (dc_iostream_t *abstract, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "Configure: baudrate=%i, databits=%i, parity=%i, stopbits=%i, flowcontrol=%i",
|
||||
baudrate, databits, parity, stopbits, flowcontrol);
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
// Retrieve the current settings.
|
||||
DCB dcb;
|
||||
if (!GetCommState (device->hFile, &dcb)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
@ -326,7 +351,7 @@ dc_serial_configure (dc_serial_t *device, unsigned int baudrate, unsigned int da
|
||||
// Apply the new settings.
|
||||
if (!SetCommState (device->hFile, &dcb)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
@ -336,19 +361,16 @@ dc_serial_configure (dc_serial_t *device, unsigned int baudrate, unsigned int da
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_set_timeout (dc_serial_t *device, int timeout)
|
||||
static dc_status_t
|
||||
dc_serial_set_timeout (dc_iostream_t *abstract, int timeout)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "Timeout: value=%i", timeout);
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
// Retrieve the current timeouts.
|
||||
COMMTIMEOUTS timeouts;
|
||||
if (!GetCommTimeouts (device->hFile, &timeouts)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
@ -379,47 +401,39 @@ dc_serial_set_timeout (dc_serial_t *device, int timeout)
|
||||
// Activate the new timeouts.
|
||||
if (!SetCommTimeouts (device->hFile, &timeouts)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_set_halfduplex (dc_serial_t *device, unsigned int value)
|
||||
static dc_status_t
|
||||
dc_serial_set_halfduplex (dc_iostream_t *abstract, unsigned int value)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
device->halfduplex = value;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_set_latency (dc_serial_t *device, unsigned int value)
|
||||
static dc_status_t
|
||||
dc_serial_set_latency (dc_iostream_t *abstract, unsigned int value)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_read (dc_serial_t *device, void *data, size_t size, size_t *actual)
|
||||
static dc_status_t
|
||||
dc_serial_read (dc_iostream_t *abstract, void *data, size_t size, size_t *actual)
|
||||
{
|
||||
dc_status_t status = DC_STATUS_SUCCESS;
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
DWORD dwRead = 0;
|
||||
|
||||
if (device == NULL) {
|
||||
status = DC_STATUS_INVALIDARGS;
|
||||
goto out_invalidargs;
|
||||
}
|
||||
|
||||
if (!ReadFile (device->hFile, data, size, &dwRead, NULL)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
status = syserror (errcode);
|
||||
goto out;
|
||||
}
|
||||
@ -429,33 +443,26 @@ dc_serial_read (dc_serial_t *device, void *data, size_t size, size_t *actual)
|
||||
}
|
||||
|
||||
out:
|
||||
HEXDUMP (device->context, DC_LOGLEVEL_INFO, "Read", (unsigned char *) data, dwRead);
|
||||
|
||||
out_invalidargs:
|
||||
if (actual)
|
||||
*actual = dwRead;
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_write (dc_serial_t *device, const void *data, size_t size, size_t *actual)
|
||||
static dc_status_t
|
||||
dc_serial_write (dc_iostream_t *abstract, const void *data, size_t size, size_t *actual)
|
||||
{
|
||||
dc_status_t status = DC_STATUS_SUCCESS;
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
DWORD dwWritten = 0;
|
||||
|
||||
if (device == NULL) {
|
||||
status = DC_STATUS_INVALIDARGS;
|
||||
goto out_invalidargs;
|
||||
}
|
||||
|
||||
LARGE_INTEGER begin, end, freq;
|
||||
if (device->halfduplex) {
|
||||
// Get the current time.
|
||||
if (!QueryPerformanceFrequency(&freq) ||
|
||||
!QueryPerformanceCounter(&begin)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
status = syserror (errcode);
|
||||
goto out;
|
||||
}
|
||||
@ -463,7 +470,7 @@ dc_serial_write (dc_serial_t *device, const void *data, size_t size, size_t *act
|
||||
|
||||
if (!WriteFile (device->hFile, data, size, &dwWritten, NULL)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
status = syserror (errcode);
|
||||
goto out;
|
||||
}
|
||||
@ -472,7 +479,7 @@ dc_serial_write (dc_serial_t *device, const void *data, size_t size, size_t *act
|
||||
// Get the current time.
|
||||
if (!QueryPerformanceCounter(&end)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
status = syserror (errcode);
|
||||
goto out;
|
||||
}
|
||||
@ -491,7 +498,7 @@ dc_serial_write (dc_serial_t *device, const void *data, size_t size, size_t *act
|
||||
// The remaining time is rounded up to the nearest millisecond
|
||||
// because the Windows Sleep() function doesn't have a higher
|
||||
// resolution.
|
||||
dc_serial_sleep (device, (remaining + 999) / 1000);
|
||||
dc_serial_sleep (abstract, (remaining + 999) / 1000);
|
||||
}
|
||||
}
|
||||
|
||||
@ -500,22 +507,16 @@ dc_serial_write (dc_serial_t *device, const void *data, size_t size, size_t *act
|
||||
}
|
||||
|
||||
out:
|
||||
HEXDUMP (device->context, DC_LOGLEVEL_INFO, "Write", (const unsigned char *) data, dwWritten);
|
||||
|
||||
out_invalidargs:
|
||||
if (actual)
|
||||
*actual = dwWritten;
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_purge (dc_serial_t *device, dc_direction_t direction)
|
||||
static dc_status_t
|
||||
dc_serial_purge (dc_iostream_t *abstract, dc_direction_t direction)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "Purge: direction=%u", direction);
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
DWORD flags = 0;
|
||||
|
||||
@ -535,48 +536,42 @@ dc_serial_purge (dc_serial_t *device, dc_direction_t direction)
|
||||
|
||||
if (!PurgeComm (device->hFile, flags)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_flush (dc_serial_t *device)
|
||||
static dc_status_t
|
||||
dc_serial_flush (dc_iostream_t *abstract)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "Flush: none");
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
if (!FlushFileBuffers (device->hFile)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_set_break (dc_serial_t *device, unsigned int level)
|
||||
static dc_status_t
|
||||
dc_serial_set_break (dc_iostream_t *abstract, unsigned int level)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "Break: value=%i", level);
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
if (level) {
|
||||
if (!SetCommBreak (device->hFile)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
} else {
|
||||
if (!ClearCommBreak (device->hFile)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
}
|
||||
@ -584,55 +579,48 @@ dc_serial_set_break (dc_serial_t *device, unsigned int level)
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_set_dtr (dc_serial_t *device, unsigned int level)
|
||||
static dc_status_t
|
||||
dc_serial_set_dtr (dc_iostream_t *abstract, unsigned int level)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "DTR: value=%i", level);
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
int status = (level ? SETDTR : CLRDTR);
|
||||
|
||||
if (!EscapeCommFunction (device->hFile, status)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_set_rts (dc_serial_t *device, unsigned int level)
|
||||
static dc_status_t
|
||||
dc_serial_set_rts (dc_iostream_t *abstract, unsigned int level)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "RTS: value=%i", level);
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
int status = (level ? SETRTS : CLRRTS);
|
||||
|
||||
if (!EscapeCommFunction (device->hFile, status)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_get_available (dc_serial_t *device, size_t *value)
|
||||
static dc_status_t
|
||||
dc_serial_get_available (dc_iostream_t *abstract, size_t *value)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
|
||||
COMSTAT stats;
|
||||
|
||||
if (!ClearCommError (device->hFile, NULL, &stats)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
@ -642,18 +630,16 @@ dc_serial_get_available (dc_serial_t *device, size_t *value)
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_get_lines (dc_serial_t *device, unsigned int *value)
|
||||
static dc_status_t
|
||||
dc_serial_get_lines (dc_iostream_t *abstract, unsigned int *value)
|
||||
{
|
||||
dc_serial_t *device = (dc_serial_t *) abstract;
|
||||
unsigned int lines = 0;
|
||||
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
DWORD stats = 0;
|
||||
if (!GetCommModemStatus (device->hFile, &stats)) {
|
||||
DWORD errcode = GetLastError ();
|
||||
SYSERROR (device->context, errcode);
|
||||
SYSERROR (abstract->context, errcode);
|
||||
return syserror (errcode);
|
||||
}
|
||||
|
||||
@ -672,14 +658,9 @@ dc_serial_get_lines (dc_serial_t *device, unsigned int *value)
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
dc_status_t
|
||||
dc_serial_sleep (dc_serial_t *device, unsigned int timeout)
|
||||
static dc_status_t
|
||||
dc_serial_sleep (dc_iostream_t *abstract, unsigned int timeout)
|
||||
{
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
INFO (device->context, "Sleep: value=%u", timeout);
|
||||
|
||||
Sleep (timeout);
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
@ -41,34 +41,34 @@ shearwater_common_open (shearwater_common_device_t *device, dc_context_t *contex
|
||||
dc_status_t status = DC_STATUS_SUCCESS;
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
return status;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (115200 8N1).
|
||||
status = dc_serial_configure (device->port, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (3000ms).
|
||||
status = dc_serial_set_timeout (device->port, 3000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 3000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_sleep (device->port, 300);
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_sleep (device->iostream, 300);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
return status;
|
||||
}
|
||||
|
||||
@ -77,7 +77,7 @@ dc_status_t
|
||||
shearwater_common_close (shearwater_common_device_t *device)
|
||||
{
|
||||
// Close the device.
|
||||
return dc_serial_close (device->port);
|
||||
return dc_iostream_close (device->iostream);
|
||||
}
|
||||
|
||||
|
||||
@ -153,7 +153,7 @@ shearwater_common_slip_write (shearwater_common_device_t *device, const unsigned
|
||||
#if 0
|
||||
// Send an initial END character to flush out any data that may have
|
||||
// accumulated in the receiver due to line noise.
|
||||
status = dc_serial_write (device->port, end, sizeof (end), NULL);
|
||||
status = dc_iostream_write (device->iostream, end, sizeof (end), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
@ -182,7 +182,7 @@ shearwater_common_slip_write (shearwater_common_device_t *device, const unsigned
|
||||
|
||||
// Flush the buffer if necessary.
|
||||
if (nbytes + len + sizeof(end) > sizeof(buffer)) {
|
||||
status = dc_serial_write (device->port, buffer, nbytes, NULL);
|
||||
status = dc_iostream_write (device->iostream, buffer, nbytes, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
@ -200,7 +200,7 @@ shearwater_common_slip_write (shearwater_common_device_t *device, const unsigned
|
||||
nbytes += sizeof(end);
|
||||
|
||||
// Flush the buffer.
|
||||
status = dc_serial_write (device->port, buffer, nbytes, NULL);
|
||||
status = dc_iostream_write (device->iostream, buffer, nbytes, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
@ -223,7 +223,7 @@ shearwater_common_slip_read (shearwater_common_device_t *device, unsigned char d
|
||||
unsigned char c = 0;
|
||||
|
||||
// Get a single character to process.
|
||||
status = dc_serial_read (device->port, &c, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, &c, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
@ -242,7 +242,7 @@ shearwater_common_slip_read (shearwater_common_device_t *device, unsigned char d
|
||||
case ESC:
|
||||
// If it's an ESC character, get another character and then
|
||||
// figure out what to store in the packet based on that.
|
||||
status = dc_serial_read (device->port, &c, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, &c, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
@ -44,7 +44,7 @@ extern "C" {
|
||||
|
||||
typedef struct shearwater_common_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
} shearwater_common_device_t;
|
||||
|
||||
dc_status_t
|
||||
|
||||
@ -44,7 +44,7 @@
|
||||
|
||||
typedef struct suunto_d9_device_t {
|
||||
suunto_common2_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
} suunto_d9_device_t;
|
||||
|
||||
static dc_status_t suunto_d9_device_packet (dc_device_t *abstract, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size);
|
||||
@ -111,7 +111,7 @@ suunto_d9_device_autodetect (suunto_d9_device_t *device, unsigned int model)
|
||||
unsigned int idx = (hint + i) % C_ARRAY_SIZE(baudrates);
|
||||
|
||||
// Adjust the baudrate.
|
||||
status = dc_serial_configure (device->port, baudrates[idx], 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, baudrates[idx], 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to set the terminal attributes.");
|
||||
return status;
|
||||
@ -147,41 +147,41 @@ suunto_d9_device_open (dc_device_t **out, dc_context_t *context, const char *nam
|
||||
suunto_common2_device_init (&device->base);
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (9600 8N1).
|
||||
status = dc_serial_configure (device->port, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (3000 ms).
|
||||
status = dc_serial_set_timeout (device->port, 3000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 3000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the DTR line (power supply for the interface).
|
||||
status = dc_serial_set_dtr (device->port, 1);
|
||||
status = dc_iostream_set_dtr (device->iostream, 1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the DTR line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Give the interface 100 ms to settle and draw power up.
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
// Try to autodetect the protocol variant.
|
||||
status = suunto_d9_device_autodetect (device, model);
|
||||
@ -206,7 +206,7 @@ suunto_d9_device_open (dc_device_t **out, dc_context_t *context, const char *nam
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -221,7 +221,7 @@ suunto_d9_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -240,10 +240,10 @@ suunto_d9_device_packet (dc_device_t *abstract, const unsigned char command[], u
|
||||
return DC_STATUS_CANCELLED;
|
||||
|
||||
// Clear RTS to send the command.
|
||||
dc_serial_set_rts (device->port, 0);
|
||||
dc_iostream_set_rts (device->iostream, 0);
|
||||
|
||||
// Send the command to the dive computer.
|
||||
status = dc_serial_write (device->port, command, csize, NULL);
|
||||
status = dc_iostream_write (device->iostream, command, csize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -252,7 +252,7 @@ suunto_d9_device_packet (dc_device_t *abstract, const unsigned char command[], u
|
||||
// Receive the echo.
|
||||
unsigned char echo[128] = {0};
|
||||
assert (sizeof (echo) >= csize);
|
||||
status = dc_serial_read (device->port, echo, csize, NULL);
|
||||
status = dc_iostream_read (device->iostream, echo, csize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the echo.");
|
||||
return status;
|
||||
@ -265,10 +265,10 @@ suunto_d9_device_packet (dc_device_t *abstract, const unsigned char command[], u
|
||||
}
|
||||
|
||||
// Set RTS to receive the reply.
|
||||
dc_serial_set_rts (device->port, 1);
|
||||
dc_iostream_set_rts (device->iostream, 1);
|
||||
|
||||
// Receive the answer of the dive computer.
|
||||
status = dc_serial_read (device->port, answer, asize, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, asize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
|
||||
@ -36,7 +36,7 @@
|
||||
|
||||
typedef struct suunto_eon_device_t {
|
||||
suunto_common_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
} suunto_eon_device_t;
|
||||
|
||||
static dc_status_t suunto_eon_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
|
||||
@ -84,31 +84,31 @@ suunto_eon_device_open (dc_device_t **out, dc_context_t *context, const char *na
|
||||
suunto_common_device_init (&device->base);
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (1200 8N2).
|
||||
status = dc_serial_configure (device->port, 1200, 8, DC_PARITY_NONE, DC_STOPBITS_TWO, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 1200, 8, DC_PARITY_NONE, DC_STOPBITS_TWO, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (1000ms).
|
||||
status = dc_serial_set_timeout (device->port, 1000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 1000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Clear the RTS line.
|
||||
status = dc_serial_set_rts (device->port, 0);
|
||||
status = dc_iostream_set_rts (device->iostream, 0);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the DTR/RTS line.");
|
||||
goto error_close;
|
||||
@ -119,7 +119,7 @@ suunto_eon_device_open (dc_device_t **out, dc_context_t *context, const char *na
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -134,7 +134,7 @@ suunto_eon_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -163,7 +163,7 @@ suunto_eon_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Send the command.
|
||||
unsigned char command[1] = {'P'};
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -178,7 +178,7 @@ suunto_eon_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Increase the packet size if more data is immediately available.
|
||||
size_t available = 0;
|
||||
status = dc_serial_get_available (device->port, &available);
|
||||
status = dc_iostream_get_available (device->iostream, &available);
|
||||
if (status == DC_STATUS_SUCCESS && available > len)
|
||||
len = available;
|
||||
|
||||
@ -187,7 +187,7 @@ suunto_eon_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
len = sizeof(answer) - nbytes;
|
||||
|
||||
// Read the packet.
|
||||
status = dc_serial_read (device->port, answer + nbytes, len, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer + nbytes, len, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -264,7 +264,7 @@ suunto_eon_device_write_name (dc_device_t *abstract, unsigned char data[], unsig
|
||||
// Send the command.
|
||||
unsigned char command[21] = {'N'};
|
||||
memcpy (command + 1, data, size);
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -285,7 +285,7 @@ suunto_eon_device_write_interval (dc_device_t *abstract, unsigned char interval)
|
||||
|
||||
// Send the command.
|
||||
unsigned char command[2] = {'T', interval};
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
|
||||
@ -39,7 +39,7 @@
|
||||
|
||||
typedef struct suunto_solution_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
} suunto_solution_device_t;
|
||||
|
||||
static dc_status_t suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
|
||||
@ -78,31 +78,31 @@ suunto_solution_device_open (dc_device_t **out, dc_context_t *context, const cha
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (1200 8N2).
|
||||
status = dc_serial_configure (device->port, 1200, 8, DC_PARITY_NONE, DC_STOPBITS_TWO, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 1200, 8, DC_PARITY_NONE, DC_STOPBITS_TWO, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (1000ms).
|
||||
status = dc_serial_set_timeout (device->port, 1000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 1000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Clear the RTS line.
|
||||
status = dc_serial_set_rts (device->port, 0);
|
||||
status = dc_iostream_set_rts (device->iostream, 0);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the DTR/RTS line.");
|
||||
goto error_close;
|
||||
@ -113,7 +113,7 @@ suunto_solution_device_open (dc_device_t **out, dc_context_t *context, const cha
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -128,7 +128,7 @@ suunto_solution_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -161,14 +161,14 @@ suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
unsigned char answer[3] = {0};
|
||||
|
||||
// Assert DTR
|
||||
dc_serial_set_dtr (device->port, 1);
|
||||
dc_iostream_set_dtr (device->iostream, 1);
|
||||
|
||||
// Send: 0xFF
|
||||
command[0] = 0xFF;
|
||||
dc_serial_write (device->port, command, 1, NULL);
|
||||
dc_iostream_write (device->iostream, command, 1, NULL);
|
||||
|
||||
// Receive: 0x3F
|
||||
status = dc_serial_read (device->port, answer, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS)
|
||||
return status;
|
||||
if (answer[0] != 0x3F)
|
||||
@ -178,7 +178,7 @@ suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
command[0] = 0x4D;
|
||||
command[1] = 0x01;
|
||||
command[2] = 0x01;
|
||||
dc_serial_write (device->port, command, 3, NULL);
|
||||
dc_iostream_write (device->iostream, command, 3, NULL);
|
||||
|
||||
// Update and emit a progress event.
|
||||
progress.current += 1;
|
||||
@ -187,7 +187,7 @@ suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
data[0] = 0x00;
|
||||
for (unsigned int i = 1; i < SZ_MEMORY; ++i) {
|
||||
// Receive: 0x01, i, data[i]
|
||||
status = dc_serial_read (device->port, answer, 3, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, 3, NULL);
|
||||
if (status != DC_STATUS_SUCCESS)
|
||||
return status;
|
||||
if (answer[0] != 0x01 || answer[1] != i)
|
||||
@ -195,10 +195,10 @@ suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Send: i
|
||||
command[0] = i;
|
||||
dc_serial_write (device->port, command, 1, NULL);
|
||||
dc_iostream_write (device->iostream, command, 1, NULL);
|
||||
|
||||
// Receive: data[i]
|
||||
status = dc_serial_read (device->port, data + i, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, data + i, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS)
|
||||
return status;
|
||||
if (data[i] != answer[2])
|
||||
@ -206,7 +206,7 @@ suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Send: 0x0D
|
||||
command[0] = 0x0D;
|
||||
dc_serial_write (device->port, command, 1, NULL);
|
||||
dc_iostream_write (device->iostream, command, 1, NULL);
|
||||
|
||||
// Update and emit a progress event.
|
||||
progress.current += 1;
|
||||
@ -214,7 +214,7 @@ suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
}
|
||||
|
||||
// Receive: 0x02, 0x00, 0x80
|
||||
status = dc_serial_read (device->port, answer, 3, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, 3, NULL);
|
||||
if (status != DC_STATUS_SUCCESS)
|
||||
return status;
|
||||
if (answer[0] != 0x02 || answer[1] != 0x00 || answer[2] != 0x80)
|
||||
@ -222,10 +222,10 @@ suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Send: 0x80
|
||||
command[0] = 0x80;
|
||||
dc_serial_write (device->port, command, 1, NULL);
|
||||
dc_iostream_write (device->iostream, command, 1, NULL);
|
||||
|
||||
// Receive: 0x80
|
||||
status = dc_serial_read (device->port, answer, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS)
|
||||
return status;
|
||||
if (answer[0] != 0x80)
|
||||
@ -233,10 +233,10 @@ suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Send: 0x20
|
||||
command[0] = 0x20;
|
||||
dc_serial_write (device->port, command, 1, NULL);
|
||||
dc_iostream_write (device->iostream, command, 1, NULL);
|
||||
|
||||
// Receive: 0x3F
|
||||
status = dc_serial_read (device->port, answer, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS)
|
||||
return status;
|
||||
if (answer[0] != 0x3F)
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
typedef struct suunto_vyper_device_t {
|
||||
suunto_common_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
} suunto_vyper_device_t;
|
||||
|
||||
static dc_status_t suunto_vyper_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size);
|
||||
@ -104,48 +104,48 @@ suunto_vyper_device_open (dc_device_t **out, dc_context_t *context, const char *
|
||||
suunto_common_device_init (&device->base);
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (2400 8O1).
|
||||
status = dc_serial_configure (device->port, 2400, 8, DC_PARITY_ODD, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 2400, 8, DC_PARITY_ODD, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (1000 ms).
|
||||
status = dc_serial_set_timeout (device->port, 1000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 1000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the DTR line (power supply for the interface).
|
||||
status = dc_serial_set_dtr (device->port, 1);
|
||||
status = dc_iostream_set_dtr (device->iostream, 1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the DTR line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Give the interface 100 ms to settle and draw power up.
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
*out = (dc_device_t*) device;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -160,7 +160,7 @@ suunto_vyper_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -175,13 +175,13 @@ suunto_vyper_send (suunto_vyper_device_t *device, const unsigned char command[],
|
||||
dc_status_t status = DC_STATUS_SUCCESS;
|
||||
dc_device_t *abstract = (dc_device_t *) device;
|
||||
|
||||
dc_serial_sleep (device->port, 500);
|
||||
dc_iostream_sleep (device->iostream, 500);
|
||||
|
||||
// Set RTS to send the command.
|
||||
dc_serial_set_rts (device->port, 1);
|
||||
dc_iostream_set_rts (device->iostream, 1);
|
||||
|
||||
// Send the command to the dive computer.
|
||||
status = dc_serial_write (device->port, command, csize, NULL);
|
||||
status = dc_iostream_write (device->iostream, command, csize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -198,11 +198,11 @@ suunto_vyper_send (suunto_vyper_device_t *device, const unsigned char command[],
|
||||
// receive the reply before RTS is cleared. We have to wait some time
|
||||
// before clearing RTS (around 30ms). But if we wait too long (> 500ms),
|
||||
// the reply disappears again.
|
||||
dc_serial_sleep (device->port, 200);
|
||||
dc_serial_purge (device->port, DC_DIRECTION_INPUT);
|
||||
dc_iostream_sleep (device->iostream, 200);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT);
|
||||
|
||||
// Clear RTS to receive the reply.
|
||||
dc_serial_set_rts (device->port, 0);
|
||||
dc_iostream_set_rts (device->iostream, 0);
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
@ -227,7 +227,7 @@ suunto_vyper_transfer (suunto_vyper_device_t *device, const unsigned char comman
|
||||
}
|
||||
|
||||
// Receive the answer of the dive computer.
|
||||
status = dc_serial_read (device->port, answer, asize, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, asize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -352,7 +352,7 @@ suunto_vyper_read_dive (dc_device_t *abstract, dc_buffer_t *buffer, int init, dc
|
||||
// Receive the header of the package.
|
||||
size_t n = 0;
|
||||
unsigned char answer[SZ_PACKET + 3] = {0};
|
||||
status = dc_serial_read (device->port, answer, 2, &n);
|
||||
status = dc_iostream_read (device->iostream, answer, 2, &n);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
// If no data is received because a timeout occured, we assume
|
||||
// the last package was already received and the transmission
|
||||
@ -377,7 +377,7 @@ suunto_vyper_read_dive (dc_device_t *abstract, dc_buffer_t *buffer, int init, dc
|
||||
|
||||
// Receive the remaining part of the package.
|
||||
unsigned char len = answer[1];
|
||||
status = dc_serial_read (device->port, answer + 2, len + 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer + 2, len + 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
|
||||
@ -35,7 +35,7 @@
|
||||
|
||||
typedef struct suunto_vyper2_device_t {
|
||||
suunto_common2_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
} suunto_vyper2_device_t;
|
||||
|
||||
static dc_status_t suunto_vyper2_device_packet (dc_device_t *abstract, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size);
|
||||
@ -93,44 +93,44 @@ suunto_vyper2_device_open (dc_device_t **out, dc_context_t *context, const char
|
||||
suunto_common2_device_init (&device->base);
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (9600 8N1).
|
||||
status = dc_serial_configure (device->port, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (3000 ms).
|
||||
status = dc_serial_set_timeout (device->port, 3000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 3000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the DTR line (power supply for the interface).
|
||||
status = dc_serial_set_dtr (device->port, 1);
|
||||
status = dc_iostream_set_dtr (device->iostream, 1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the DTR line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Give the interface 100 ms to settle and draw power up.
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
// Enable half-duplex emulation.
|
||||
dc_serial_set_halfduplex (device->port, 1);
|
||||
dc_iostream_set_halfduplex (device->iostream, 1);
|
||||
|
||||
// Read the version info.
|
||||
status = suunto_common2_device_version ((dc_device_t *) device, device->base.version, sizeof (device->base.version));
|
||||
@ -151,7 +151,7 @@ suunto_vyper2_device_open (dc_device_t **out, dc_context_t *context, const char
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -166,7 +166,7 @@ suunto_vyper2_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -184,23 +184,23 @@ suunto_vyper2_device_packet (dc_device_t *abstract, const unsigned char command[
|
||||
if (device_is_cancelled (abstract))
|
||||
return DC_STATUS_CANCELLED;
|
||||
|
||||
dc_serial_sleep (device->port, 600);
|
||||
dc_iostream_sleep (device->iostream, 600);
|
||||
|
||||
// Set RTS to send the command.
|
||||
dc_serial_set_rts (device->port, 1);
|
||||
dc_iostream_set_rts (device->iostream, 1);
|
||||
|
||||
// Send the command to the dive computer.
|
||||
status = dc_serial_write (device->port, command, csize, NULL);
|
||||
status = dc_iostream_write (device->iostream, command, csize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
}
|
||||
|
||||
// Clear RTS to receive the reply.
|
||||
dc_serial_set_rts (device->port, 0);
|
||||
dc_iostream_set_rts (device->iostream, 0);
|
||||
|
||||
// Receive the answer of the dive computer.
|
||||
status = dc_serial_read (device->port, answer, asize, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, asize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
|
||||
@ -43,7 +43,7 @@
|
||||
|
||||
typedef struct uwatec_aladin_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned int timestamp;
|
||||
unsigned int devtime;
|
||||
dc_ticks_t systime;
|
||||
@ -86,41 +86,41 @@ uwatec_aladin_device_open (dc_device_t **out, dc_context_t *context, const char
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
device->timestamp = 0;
|
||||
device->systime = (dc_ticks_t) -1;
|
||||
device->devtime = 0;
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (19200 8N1).
|
||||
status = dc_serial_configure (device->port, 19200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 19200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (INFINITE).
|
||||
status = dc_serial_set_timeout (device->port, -1);
|
||||
status = dc_iostream_set_timeout (device->iostream, -1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the DTR line.
|
||||
status = dc_serial_set_dtr (device->port, 1);
|
||||
status = dc_iostream_set_dtr (device->iostream, 1);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the DTR line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Clear the RTS line.
|
||||
status = dc_serial_set_rts (device->port, 0);
|
||||
status = dc_iostream_set_rts (device->iostream, 0);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to clear the RTS line.");
|
||||
goto error_close;
|
||||
@ -131,7 +131,7 @@ uwatec_aladin_device_open (dc_device_t **out, dc_context_t *context, const char
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -146,7 +146,7 @@ uwatec_aladin_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -197,7 +197,7 @@ uwatec_aladin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
if (device_is_cancelled (abstract))
|
||||
return DC_STATUS_CANCELLED;
|
||||
|
||||
status = dc_serial_read (device->port, answer + i, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer + i, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -218,7 +218,7 @@ uwatec_aladin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
|
||||
|
||||
// Receive the remaining part of the package.
|
||||
status = dc_serial_read (device->port, answer + 4, sizeof (answer) - 4, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer + 4, sizeof (answer) - 4, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Unexpected EOF in answer.");
|
||||
return status;
|
||||
|
||||
@ -39,7 +39,7 @@
|
||||
|
||||
typedef struct uwatec_memomouse_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned int timestamp;
|
||||
unsigned int devtime;
|
||||
dc_ticks_t systime;
|
||||
@ -82,55 +82,55 @@ uwatec_memomouse_device_open (dc_device_t **out, dc_context_t *context, const ch
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
device->timestamp = 0;
|
||||
device->systime = (dc_ticks_t) -1;
|
||||
device->devtime = 0;
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (9600 8N1).
|
||||
status = dc_serial_configure (device->port, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (1000 ms).
|
||||
status = dc_serial_set_timeout (device->port, 1000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 1000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Clear the DTR line.
|
||||
status = dc_serial_set_dtr (device->port, 0);
|
||||
status = dc_iostream_set_dtr (device->iostream, 0);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to clear the DTR line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Clear the RTS line.
|
||||
status = dc_serial_set_rts (device->port, 0);
|
||||
status = dc_iostream_set_rts (device->iostream, 0);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to clear the RTS line.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
*out = (dc_device_t*) device;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -145,7 +145,7 @@ uwatec_memomouse_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -180,7 +180,7 @@ uwatec_memomouse_read_packet (uwatec_memomouse_device_t *device, unsigned char d
|
||||
assert (result != NULL);
|
||||
|
||||
// Receive the header of the package.
|
||||
status = dc_serial_read (device->port, data, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, data, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -197,7 +197,7 @@ uwatec_memomouse_read_packet (uwatec_memomouse_device_t *device, unsigned char d
|
||||
}
|
||||
|
||||
// Receive the remaining part of the package.
|
||||
status = dc_serial_read (device->port, data + 1, len + 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, data + 1, len + 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -234,11 +234,11 @@ uwatec_memomouse_read_packet_outer (uwatec_memomouse_device_t *device, unsigned
|
||||
return rc;
|
||||
|
||||
// Flush the input buffer.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_INPUT);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT);
|
||||
|
||||
// Reject the packet.
|
||||
unsigned char value = NAK;
|
||||
status = dc_serial_write (device->port, &value, 1, NULL);
|
||||
status = dc_iostream_write (device->iostream, &value, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to reject the packet.");
|
||||
return status;
|
||||
@ -277,7 +277,7 @@ uwatec_memomouse_read_packet_inner (uwatec_memomouse_device_t *device, dc_buffer
|
||||
|
||||
// Accept the packet.
|
||||
unsigned char value = ACK;
|
||||
status = dc_serial_write (device->port, &value, 1, NULL);
|
||||
status = dc_iostream_write (device->iostream, &value, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to accept the packet.");
|
||||
return status;
|
||||
@ -344,22 +344,22 @@ uwatec_memomouse_dump_internal (uwatec_memomouse_device_t *device, dc_buffer_t *
|
||||
device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress);
|
||||
|
||||
// Waiting for greeting message.
|
||||
while (dc_serial_get_available (device->port, &available) == DC_STATUS_SUCCESS && available == 0) {
|
||||
while (dc_iostream_get_available (device->iostream, &available) == DC_STATUS_SUCCESS && available == 0) {
|
||||
if (device_is_cancelled (abstract))
|
||||
return DC_STATUS_CANCELLED;
|
||||
|
||||
// Flush the input buffer.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_INPUT);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT);
|
||||
|
||||
// Reject the packet.
|
||||
unsigned char value = NAK;
|
||||
status = dc_serial_write (device->port, &value, 1, NULL);
|
||||
status = dc_iostream_write (device->iostream, &value, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to reject the packet.");
|
||||
return status;
|
||||
}
|
||||
|
||||
dc_serial_sleep (device->port, 300);
|
||||
dc_iostream_sleep (device->iostream, 300);
|
||||
}
|
||||
|
||||
// Read the ID string.
|
||||
@ -382,24 +382,24 @@ uwatec_memomouse_dump_internal (uwatec_memomouse_device_t *device, dc_buffer_t *
|
||||
|
||||
// Wait a small amount of time before sending the command.
|
||||
// Without this delay, the transfer will fail most of the time.
|
||||
dc_serial_sleep (device->port, 50);
|
||||
dc_iostream_sleep (device->iostream, 50);
|
||||
|
||||
// Keep send the command to the device,
|
||||
// until the ACK answer is received.
|
||||
unsigned char answer = NAK;
|
||||
while (answer == NAK) {
|
||||
// Flush the input buffer.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_INPUT);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT);
|
||||
|
||||
// Send the command to the device.
|
||||
status = dc_serial_write (device->port, command, sizeof (command), NULL);
|
||||
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
}
|
||||
|
||||
// Wait for the answer (ACK).
|
||||
status = dc_serial_read (device->port, &answer, 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, &answer, 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -413,12 +413,12 @@ uwatec_memomouse_dump_internal (uwatec_memomouse_device_t *device, dc_buffer_t *
|
||||
}
|
||||
|
||||
// Wait for the data packet.
|
||||
while (dc_serial_get_available (device->port, &available) == DC_STATUS_SUCCESS && available == 0) {
|
||||
while (dc_iostream_get_available (device->iostream, &available) == DC_STATUS_SUCCESS && available == 0) {
|
||||
if (device_is_cancelled (abstract))
|
||||
return DC_STATUS_CANCELLED;
|
||||
|
||||
device_event_emit (&device->base, DC_EVENT_WAITING, NULL);
|
||||
dc_serial_sleep (device->port, 100);
|
||||
dc_iostream_sleep (device->iostream, 100);
|
||||
}
|
||||
|
||||
// Fetch the current system time.
|
||||
@ -458,10 +458,10 @@ uwatec_memomouse_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Give the interface some time to notice the DTR
|
||||
// line change from a previous transfer (if any).
|
||||
dc_serial_sleep (device->port, 500);
|
||||
dc_iostream_sleep (device->iostream, 500);
|
||||
|
||||
// Set the DTR line.
|
||||
rc = dc_serial_set_dtr (device->port, 1);
|
||||
rc = dc_iostream_set_dtr (device->iostream, 1);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to set the RTS line.");
|
||||
return rc;
|
||||
@ -471,7 +471,7 @@ uwatec_memomouse_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
status = uwatec_memomouse_dump_internal (device, buffer);
|
||||
|
||||
// Clear the DTR line again.
|
||||
rc = dc_serial_set_dtr (device->port, 0);
|
||||
rc = dc_iostream_set_dtr (device->iostream, 0);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to set the RTS line.");
|
||||
return rc;
|
||||
|
||||
@ -37,7 +37,7 @@
|
||||
|
||||
typedef struct uwatec_meridian_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned int timestamp;
|
||||
unsigned int devtime;
|
||||
dc_ticks_t systime;
|
||||
@ -83,7 +83,7 @@ uwatec_meridian_transfer (uwatec_meridian_device_t *device, const unsigned char
|
||||
packet[11 + csize] = checksum_xor_uint8 (packet + 7, csize + 4, 0x00);
|
||||
|
||||
// Send the packet.
|
||||
status = dc_serial_write (device->port, packet, csize + 12, NULL);
|
||||
status = dc_iostream_write (device->iostream, packet, csize + 12, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
@ -91,7 +91,7 @@ uwatec_meridian_transfer (uwatec_meridian_device_t *device, const unsigned char
|
||||
|
||||
// Read the echo.
|
||||
unsigned char echo[sizeof(packet)];
|
||||
status = dc_serial_read (device->port, echo, csize + 12, NULL);
|
||||
status = dc_iostream_read (device->iostream, echo, csize + 12, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the echo.");
|
||||
return status;
|
||||
@ -105,7 +105,7 @@ uwatec_meridian_transfer (uwatec_meridian_device_t *device, const unsigned char
|
||||
|
||||
// Read the header.
|
||||
unsigned char header[6];
|
||||
status = dc_serial_read (device->port, header, sizeof (header), NULL);
|
||||
status = dc_iostream_read (device->iostream, header, sizeof (header), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the header.");
|
||||
return status;
|
||||
@ -118,7 +118,7 @@ uwatec_meridian_transfer (uwatec_meridian_device_t *device, const unsigned char
|
||||
}
|
||||
|
||||
// Read the packet.
|
||||
status = dc_serial_read (device->port, answer, asize, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, asize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the packet.");
|
||||
return status;
|
||||
@ -126,7 +126,7 @@ uwatec_meridian_transfer (uwatec_meridian_device_t *device, const unsigned char
|
||||
|
||||
// Read the checksum.
|
||||
unsigned char csum = 0x00;
|
||||
status = dc_serial_read (device->port, &csum, sizeof (csum), NULL);
|
||||
status = dc_iostream_read (device->iostream, &csum, sizeof (csum), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the checksum.");
|
||||
return status;
|
||||
@ -199,34 +199,34 @@ uwatec_meridian_device_open (dc_device_t **out, dc_context_t *context, const cha
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
device->timestamp = 0;
|
||||
device->systime = (dc_ticks_t) -1;
|
||||
device->devtime = 0;
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (57600 8N1).
|
||||
status = dc_serial_configure (device->port, 57600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 57600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (3000ms).
|
||||
status = dc_serial_set_timeout (device->port, 3000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 3000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
// Perform the handshaking.
|
||||
status = uwatec_meridian_handshake (device);
|
||||
@ -240,7 +240,7 @@ uwatec_meridian_device_open (dc_device_t **out, dc_context_t *context, const cha
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -255,7 +255,7 @@ uwatec_meridian_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
@ -398,7 +398,7 @@ uwatec_meridian_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Read the header.
|
||||
unsigned char header[5];
|
||||
status = dc_serial_read (device->port, header, sizeof (header), NULL);
|
||||
status = dc_iostream_read (device->iostream, header, sizeof (header), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the header.");
|
||||
return status;
|
||||
@ -412,7 +412,7 @@ uwatec_meridian_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
}
|
||||
|
||||
// Read the packet data.
|
||||
status = dc_serial_read (device->port, data + nbytes, packetsize - 1, NULL);
|
||||
status = dc_iostream_read (device->iostream, data + nbytes, packetsize - 1, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the packet.");
|
||||
return status;
|
||||
@ -420,7 +420,7 @@ uwatec_meridian_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
|
||||
|
||||
// Read the checksum.
|
||||
unsigned char csum = 0x00;
|
||||
status = dc_serial_read (device->port, &csum, sizeof (csum), NULL);
|
||||
status = dc_iostream_read (device->iostream, &csum, sizeof (csum), NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the checksum.");
|
||||
return status;
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
typedef struct zeagle_n2ition3_device_t {
|
||||
dc_device_t base;
|
||||
dc_serial_t *port;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned char fingerprint[16];
|
||||
} zeagle_n2ition3_device_t;
|
||||
|
||||
@ -81,14 +81,14 @@ zeagle_n2ition3_packet (zeagle_n2ition3_device_t *device, const unsigned char co
|
||||
return DC_STATUS_CANCELLED;
|
||||
|
||||
// Send the command to the device.
|
||||
status = dc_serial_write (device->port, command, csize, NULL);
|
||||
status = dc_iostream_write (device->iostream, command, csize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to send the command.");
|
||||
return status;
|
||||
}
|
||||
|
||||
// Receive the answer of the device.
|
||||
status = dc_serial_read (device->port, answer, asize, NULL);
|
||||
status = dc_iostream_read (device->iostream, answer, asize, NULL);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (abstract->context, "Failed to receive the answer.");
|
||||
return status;
|
||||
@ -149,32 +149,32 @@ zeagle_n2ition3_device_open (dc_device_t **out, dc_context_t *context, const cha
|
||||
}
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->iostream = NULL;
|
||||
memset (device->fingerprint, 0, sizeof (device->fingerprint));
|
||||
|
||||
// Open the device.
|
||||
status = dc_serial_open (&device->port, context, name);
|
||||
status = dc_serial_open (&device->iostream, context, name);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to open the serial port.");
|
||||
goto error_free;
|
||||
}
|
||||
|
||||
// Set the serial communication protocol (4800 8N1).
|
||||
status = dc_serial_configure (device->port, 4800, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
status = dc_iostream_configure (device->iostream, 4800, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the terminal attributes.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Set the timeout for receiving data (1000 ms).
|
||||
status = dc_serial_set_timeout (device->port, 1000);
|
||||
status = dc_iostream_set_timeout (device->iostream, 1000);
|
||||
if (status != DC_STATUS_SUCCESS) {
|
||||
ERROR (context, "Failed to set the timeout.");
|
||||
goto error_close;
|
||||
}
|
||||
|
||||
// Make sure everything is in a sane state.
|
||||
dc_serial_purge (device->port, DC_DIRECTION_ALL);
|
||||
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
|
||||
|
||||
// Send the init commands.
|
||||
zeagle_n2ition3_init (device);
|
||||
@ -184,7 +184,7 @@ zeagle_n2ition3_device_open (dc_device_t **out, dc_context_t *context, const cha
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
error_close:
|
||||
dc_serial_close (device->port);
|
||||
dc_iostream_close (device->iostream);
|
||||
error_free:
|
||||
dc_device_deallocate ((dc_device_t *) device);
|
||||
return status;
|
||||
@ -199,7 +199,7 @@ zeagle_n2ition3_device_close (dc_device_t *abstract)
|
||||
dc_status_t rc = DC_STATUS_SUCCESS;
|
||||
|
||||
// Close the device.
|
||||
rc = dc_serial_close (device->port);
|
||||
rc = dc_iostream_close (device->iostream);
|
||||
if (rc != DC_STATUS_SUCCESS) {
|
||||
dc_status_set_error(&status, rc);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user