iostream.c (8891B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2016 Jef Driesen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #include <stddef.h> 23 #include <stdlib.h> 24 #include <assert.h> 25 26 #include <libdivecomputer/ioctl.h> 27 28 #include "iostream-private.h" 29 #include "context-private.h" 30 #include "platform.h" 31 32 dc_iostream_t * 33 dc_iostream_allocate (dc_context_t *context, const dc_iostream_vtable_t *vtable, dc_transport_t transport) 34 { 35 dc_iostream_t *iostream = NULL; 36 37 assert(vtable != NULL); 38 assert(vtable->size >= sizeof(dc_iostream_t)); 39 40 // Allocate memory. 41 iostream = (dc_iostream_t *) malloc (vtable->size); 42 if (iostream == NULL) { 43 ERROR (context, "Failed to allocate memory."); 44 return iostream; 45 } 46 47 // Initialize the base class. 48 iostream->vtable = vtable; 49 iostream->context = context; 50 iostream->transport = transport; 51 52 return iostream; 53 } 54 55 void 56 dc_iostream_deallocate (dc_iostream_t *iostream) 57 { 58 free (iostream); 59 } 60 61 int 62 dc_iostream_isinstance (dc_iostream_t *iostream, const dc_iostream_vtable_t *vtable) 63 { 64 if (iostream == NULL) 65 return 0; 66 67 return iostream->vtable == vtable; 68 } 69 70 dc_transport_t 71 dc_iostream_get_transport (dc_iostream_t *iostream) 72 { 73 if (iostream == NULL) 74 return DC_TRANSPORT_NONE; 75 76 return iostream->transport; 77 } 78 79 dc_status_t 80 dc_iostream_set_timeout (dc_iostream_t *iostream, int timeout) 81 { 82 if (iostream == NULL || iostream->vtable->set_timeout == NULL) 83 return DC_STATUS_SUCCESS; 84 85 INFO (iostream->context, "Timeout: value=%i", timeout); 86 87 return iostream->vtable->set_timeout (iostream, timeout); 88 } 89 90 dc_status_t 91 dc_iostream_set_break (dc_iostream_t *iostream, unsigned int value) 92 { 93 if (iostream == NULL || iostream->vtable->set_break == NULL) 94 return DC_STATUS_SUCCESS; 95 96 INFO (iostream->context, "Break: value=%i", value); 97 98 return iostream->vtable->set_break (iostream, value); 99 } 100 101 dc_status_t 102 dc_iostream_set_dtr (dc_iostream_t *iostream, unsigned int value) 103 { 104 if (iostream == NULL || iostream->vtable->set_dtr == NULL) 105 return DC_STATUS_SUCCESS; 106 107 INFO (iostream->context, "DTR: value=%i", value); 108 109 return iostream->vtable->set_dtr (iostream, value); 110 } 111 112 dc_status_t 113 dc_iostream_set_rts (dc_iostream_t *iostream, unsigned int value) 114 { 115 if (iostream == NULL || iostream->vtable->set_rts == NULL) 116 return DC_STATUS_SUCCESS; 117 118 INFO (iostream->context, "RTS: value=%i", value); 119 120 return iostream->vtable->set_rts (iostream, value); 121 } 122 123 dc_status_t 124 dc_iostream_get_lines (dc_iostream_t *iostream, unsigned int *value) 125 { 126 dc_status_t status = DC_STATUS_SUCCESS; 127 unsigned int lines = 0; 128 129 if (iostream == NULL || iostream->vtable->get_lines == NULL) { 130 goto out; 131 } 132 133 status = iostream->vtable->get_lines (iostream, &lines); 134 135 INFO (iostream->context, "Lines: value=%u", lines); 136 137 out: 138 if (value) 139 *value = lines; 140 141 return status; 142 } 143 144 dc_status_t 145 dc_iostream_get_available (dc_iostream_t *iostream, size_t *value) 146 { 147 dc_status_t status = DC_STATUS_SUCCESS; 148 size_t available = 0; 149 150 if (iostream == NULL || iostream->vtable->get_available == NULL) { 151 goto out; 152 } 153 154 status = iostream->vtable->get_available (iostream, &available); 155 156 INFO (iostream->context, "Available: value=" DC_PRINTF_SIZE, available); 157 158 out: 159 if (value) 160 *value = available; 161 162 return status; 163 } 164 165 dc_status_t 166 dc_iostream_configure (dc_iostream_t *iostream, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol) 167 { 168 if (iostream == NULL || iostream->vtable->configure == NULL) 169 return DC_STATUS_SUCCESS; 170 171 INFO (iostream->context, "Configure: baudrate=%i, databits=%i, parity=%i, stopbits=%i, flowcontrol=%i", 172 baudrate, databits, parity, stopbits, flowcontrol); 173 174 return iostream->vtable->configure (iostream, baudrate, databits, parity, stopbits, flowcontrol); 175 } 176 177 dc_status_t 178 dc_iostream_poll (dc_iostream_t *iostream, int timeout) 179 { 180 if (iostream == NULL || iostream->vtable->poll == NULL) 181 return DC_STATUS_SUCCESS; 182 183 INFO (iostream->context, "Poll: value=%i", timeout); 184 185 return iostream->vtable->poll (iostream, timeout); 186 } 187 188 dc_status_t 189 dc_iostream_read (dc_iostream_t *iostream, void *data, size_t size, size_t *actual) 190 { 191 if (actual) 192 *actual = 0; 193 194 if (iostream == NULL || iostream->vtable->read == NULL) 195 return DC_STATUS_IO; 196 197 while (size) { 198 dc_status_t status; 199 size_t nbytes = 0; 200 201 status = iostream->vtable->read (iostream, data, size, &nbytes); 202 HEXDUMP (iostream->context, DC_LOGLEVEL_INFO, "Read", (unsigned char *) data, nbytes); 203 204 /* 205 * If the reader is able to handle partial results, 206 * return them as such. NOTE! No need to add up a 207 * total, we will go through this loop only once 208 * in this case. 209 */ 210 if (actual) { 211 *actual = nbytes; 212 return status; 213 } 214 215 if (status != DC_STATUS_SUCCESS) 216 return status; 217 218 /* 219 * Defensive check: if the read() function returned 220 * zero bytes, don't loop forever - give up with a 221 * timeout. Jef pointed out that the subsurface 222 * qt_serial_read() function can cause this badness.. 223 */ 224 if (!nbytes) 225 return DC_STATUS_TIMEOUT; 226 227 /* 228 * Continue reading to fill up the whole buffer, 229 * since the reader is not able to handle a 230 * partial result. 231 */ 232 data = (void *)(nbytes + (char *)data); 233 size -= nbytes; 234 } 235 236 return DC_STATUS_SUCCESS; 237 } 238 239 dc_status_t 240 dc_iostream_write (dc_iostream_t *iostream, const void *data, size_t size, size_t *actual) 241 { 242 if (actual) 243 *actual = 0; 244 245 if (iostream == NULL || iostream->vtable->write == NULL) 246 return DC_STATUS_IO; 247 248 while (size) { 249 dc_status_t status; 250 size_t nbytes = 0; 251 252 status = iostream->vtable->write (iostream, data, size, &nbytes); 253 HEXDUMP (iostream->context, DC_LOGLEVEL_INFO, "Write", (const unsigned char *) data, nbytes); 254 255 if (actual) { 256 *actual = nbytes; 257 return status; 258 } 259 260 if (status != DC_STATUS_SUCCESS) 261 return status; 262 263 if (!nbytes) 264 return DC_STATUS_IO; 265 266 data = (void *)(nbytes + (char *)data); 267 size -= nbytes; 268 } 269 270 return DC_STATUS_SUCCESS; 271 } 272 273 dc_status_t 274 dc_iostream_ioctl (dc_iostream_t *iostream, unsigned int request, void *data, size_t size) 275 { 276 dc_status_t status = DC_STATUS_SUCCESS; 277 278 if (iostream == NULL || iostream->vtable->ioctl == NULL) 279 return DC_STATUS_SUCCESS; 280 281 // The size should match the size encoded in the ioctl request, 282 // unless it's a variable size request. 283 if (size != DC_IOCTL_SIZE(request) && 284 !(DC_IOCTL_DIR(request) != DC_IOCTL_DIR_NONE && DC_IOCTL_SIZE(request) == 0)) { 285 ERROR (iostream->context, "Invalid size for ioctl request 0x%08x (" DC_PRINTF_SIZE ").", request, size); 286 return DC_STATUS_INVALIDARGS; 287 } 288 289 INFO (iostream->context, "Ioctl: request=0x%08x (dir=%u, type=%u, nr=%u, size=%u)", 290 request, 291 DC_IOCTL_DIR(request), DC_IOCTL_TYPE(request), 292 DC_IOCTL_NR(request), DC_IOCTL_SIZE(request)); 293 294 if (DC_IOCTL_DIR(request) & DC_IOCTL_DIR_WRITE) { 295 HEXDUMP (iostream->context, DC_LOGLEVEL_INFO, "Ioctl write", (unsigned char *) data, size); 296 } 297 298 status = iostream->vtable->ioctl (iostream, request, data, size); 299 300 if (DC_IOCTL_DIR(request) & DC_IOCTL_DIR_READ) { 301 HEXDUMP (iostream->context, DC_LOGLEVEL_INFO, "Ioctl read", (unsigned char *) data, size); 302 } 303 304 return status; 305 } 306 307 dc_status_t 308 dc_iostream_flush (dc_iostream_t *iostream) 309 { 310 if (iostream == NULL || iostream->vtable->flush == NULL) 311 return DC_STATUS_SUCCESS; 312 313 INFO (iostream->context, "Flush: none"); 314 315 return iostream->vtable->flush (iostream); 316 } 317 318 dc_status_t 319 dc_iostream_purge (dc_iostream_t *iostream, dc_direction_t direction) 320 { 321 if (iostream == NULL || iostream->vtable->purge == NULL) 322 return DC_STATUS_SUCCESS; 323 324 INFO (iostream->context, "Purge: direction=%u", direction); 325 326 return iostream->vtable->purge (iostream, direction); 327 } 328 329 dc_status_t 330 dc_iostream_sleep (dc_iostream_t *iostream, unsigned int milliseconds) 331 { 332 if (iostream == NULL || iostream->vtable->sleep == NULL) 333 return DC_STATUS_SUCCESS; 334 335 INFO (iostream->context, "Sleep: value=%u", milliseconds); 336 337 return iostream->vtable->sleep (iostream, milliseconds); 338 } 339 340 dc_status_t 341 dc_iostream_close (dc_iostream_t *iostream) 342 { 343 dc_status_t status = DC_STATUS_SUCCESS; 344 345 if (iostream == NULL) 346 return DC_STATUS_SUCCESS; 347 348 if (iostream->vtable->close) { 349 status = iostream->vtable->close (iostream); 350 } 351 352 dc_iostream_deallocate (iostream); 353 354 return status; 355 }