packet.c (9118B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2023 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 <stdlib.h> // malloc, free 23 #include <string.h> 24 25 #include "packet.h" 26 27 #include "iostream-private.h" 28 #include "common-private.h" 29 #include "context-private.h" 30 31 static dc_status_t dc_packet_set_timeout (dc_iostream_t *abstract, int timeout); 32 static dc_status_t dc_packet_set_break (dc_iostream_t *abstract, unsigned int value); 33 static dc_status_t dc_packet_set_dtr (dc_iostream_t *abstract, unsigned int value); 34 static dc_status_t dc_packet_set_rts (dc_iostream_t *abstract, unsigned int value); 35 static dc_status_t dc_packet_get_lines (dc_iostream_t *abstract, unsigned int *value); 36 static dc_status_t dc_packet_get_available (dc_iostream_t *abstract, size_t *value); 37 static dc_status_t dc_packet_configure (dc_iostream_t *abstract, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol); 38 static dc_status_t dc_packet_poll (dc_iostream_t *abstract, int timeout); 39 static dc_status_t dc_packet_read (dc_iostream_t *abstract, void *data, size_t size, size_t *actual); 40 static dc_status_t dc_packet_write (dc_iostream_t *abstract, const void *data, size_t size, size_t *actual); 41 static dc_status_t dc_packet_ioctl (dc_iostream_t *abstract, unsigned int request, void *data, size_t size); 42 static dc_status_t dc_packet_flush (dc_iostream_t *abstract); 43 static dc_status_t dc_packet_purge (dc_iostream_t *abstract, dc_direction_t direction); 44 static dc_status_t dc_packet_sleep (dc_iostream_t *abstract, unsigned int milliseconds); 45 static dc_status_t dc_packet_close (dc_iostream_t *abstract); 46 47 typedef struct dc_packet_t { 48 /* Base class. */ 49 dc_iostream_t base; 50 /* Internal state. */ 51 dc_iostream_t *iostream; 52 unsigned char *cache; 53 size_t available; 54 size_t offset; 55 size_t isize; 56 size_t osize; 57 } dc_packet_t; 58 59 static const dc_iostream_vtable_t dc_packet_vtable = { 60 sizeof(dc_packet_t), 61 dc_packet_set_timeout, /* set_timeout */ 62 dc_packet_set_break, /* set_break */ 63 dc_packet_set_dtr, /* set_dtr */ 64 dc_packet_set_rts, /* set_rts */ 65 dc_packet_get_lines, /* get_lines */ 66 dc_packet_get_available, /* get_available */ 67 dc_packet_configure, /* configure */ 68 dc_packet_poll, /* poll */ 69 dc_packet_read, /* read */ 70 dc_packet_write, /* write */ 71 dc_packet_ioctl, /* ioctl */ 72 dc_packet_flush, /* flush */ 73 dc_packet_purge, /* purge */ 74 dc_packet_sleep, /* sleep */ 75 dc_packet_close, /* close */ 76 }; 77 78 dc_status_t 79 dc_packet_open (dc_iostream_t **out, dc_context_t *context, dc_iostream_t *base, size_t isize, size_t osize) 80 { 81 dc_status_t status = DC_STATUS_SUCCESS; 82 dc_packet_t *packet = NULL; 83 unsigned char *buffer = NULL; 84 85 if (out == NULL || base == NULL) 86 return DC_STATUS_INVALIDARGS; 87 88 // Allocate memory. 89 packet = (dc_packet_t *) dc_iostream_allocate (NULL, &dc_packet_vtable, dc_iostream_get_transport(base)); 90 if (packet == NULL) { 91 ERROR (context, "Failed to allocate memory."); 92 status = DC_STATUS_NOMEMORY; 93 goto error_exit; 94 } 95 96 // Allocate the read buffer. 97 if (isize) { 98 buffer = (unsigned char *) malloc (isize); 99 if (buffer == NULL) { 100 ERROR (context, "Failed to allocate memory."); 101 status = DC_STATUS_NOMEMORY; 102 goto error_free; 103 } 104 } 105 106 packet->iostream = base; 107 packet->cache = buffer; 108 packet->available = 0; 109 packet->offset = 0; 110 packet->isize = isize; 111 packet->osize = osize; 112 113 *out = (dc_iostream_t *) packet; 114 115 return DC_STATUS_SUCCESS; 116 117 error_free: 118 dc_iostream_deallocate ((dc_iostream_t *) packet); 119 error_exit: 120 return status; 121 } 122 123 static dc_status_t 124 dc_packet_set_timeout (dc_iostream_t *abstract, int timeout) 125 { 126 dc_packet_t *packet = (dc_packet_t *) abstract; 127 128 return dc_iostream_set_timeout (packet->iostream, timeout); 129 } 130 131 static dc_status_t 132 dc_packet_set_break (dc_iostream_t *abstract, unsigned int value) 133 { 134 dc_packet_t *packet = (dc_packet_t *) abstract; 135 136 return dc_iostream_set_break (packet->iostream, value); 137 } 138 139 static dc_status_t 140 dc_packet_set_dtr (dc_iostream_t *abstract, unsigned int value) 141 { 142 dc_packet_t *packet = (dc_packet_t *) abstract; 143 144 return dc_iostream_set_dtr (packet->iostream, value); 145 } 146 147 static dc_status_t 148 dc_packet_set_rts (dc_iostream_t *abstract, unsigned int value) 149 { 150 dc_packet_t *packet = (dc_packet_t *) abstract; 151 152 return dc_iostream_set_rts (packet->iostream, value); 153 } 154 155 static dc_status_t 156 dc_packet_get_lines (dc_iostream_t *abstract, unsigned int *value) 157 { 158 dc_packet_t *packet = (dc_packet_t *) abstract; 159 160 return dc_iostream_get_lines (packet->iostream, value); 161 } 162 163 static dc_status_t 164 dc_packet_get_available (dc_iostream_t *abstract, size_t *value) 165 { 166 dc_packet_t *packet = (dc_packet_t *) abstract; 167 168 if (packet->isize && packet->available) { 169 if (value) 170 *value = packet->available; 171 return DC_STATUS_SUCCESS; 172 } 173 174 return dc_iostream_get_available (packet->iostream, value); 175 } 176 177 static dc_status_t 178 dc_packet_configure (dc_iostream_t *abstract, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol) 179 { 180 dc_packet_t *packet = (dc_packet_t *) abstract; 181 182 return dc_iostream_configure (packet->iostream, baudrate, databits, parity, stopbits, flowcontrol); 183 } 184 185 static dc_status_t 186 dc_packet_poll (dc_iostream_t *abstract, int timeout) 187 { 188 dc_packet_t *packet = (dc_packet_t *) abstract; 189 190 if (packet->isize && packet->available) 191 return DC_STATUS_SUCCESS; 192 193 return dc_iostream_poll (packet->iostream, timeout); 194 } 195 196 static dc_status_t 197 dc_packet_read (dc_iostream_t *abstract, void *data, size_t size, size_t *actual) 198 { 199 dc_status_t status = DC_STATUS_SUCCESS; 200 dc_packet_t *packet = (dc_packet_t *) abstract; 201 size_t nbytes = 0; 202 203 while (nbytes < size) { 204 // Get the remaining size. 205 size_t length = size - nbytes; 206 207 if (packet->isize) { 208 if (packet->available == 0) { 209 // Read a packet into the cache. 210 size_t len = 0; 211 status = dc_iostream_read (packet->iostream, packet->cache, packet->isize, &len); 212 if (status != DC_STATUS_SUCCESS) 213 break; 214 215 packet->available = len; 216 packet->offset = 0; 217 } 218 219 // Limit to the maximum packet size. 220 if (length > packet->available) 221 length = packet->available; 222 223 // Copy the data from the cached packet. 224 memcpy ((unsigned char *) data + nbytes, packet->cache + packet->offset, length); 225 packet->available -= length; 226 packet->offset += length; 227 } else { 228 // Read the packet. 229 status = dc_iostream_read (packet->iostream, (unsigned char *) data + nbytes, length, &length); 230 if (status != DC_STATUS_SUCCESS) 231 break; 232 } 233 234 // Update the total number of bytes. 235 nbytes += length; 236 } 237 238 if (actual) 239 *actual = nbytes; 240 241 return status; 242 } 243 244 static dc_status_t 245 dc_packet_write (dc_iostream_t *abstract, const void *data, size_t size, size_t *actual) 246 { 247 dc_status_t status = DC_STATUS_SUCCESS; 248 dc_packet_t *packet = (dc_packet_t *) abstract; 249 size_t nbytes = 0; 250 251 while (nbytes < size) { 252 // Get the remaining size. 253 size_t length = size - nbytes; 254 255 // Limit to the maximum packet size. 256 if (packet->osize) { 257 if (length > packet->osize) 258 length = packet->osize; 259 } 260 261 // Write the packet. 262 status = dc_iostream_write (packet->iostream, (const unsigned char *) data + nbytes, length, &length); 263 if (status != DC_STATUS_SUCCESS) 264 break; 265 266 // Update the total number of bytes. 267 nbytes += length; 268 } 269 270 if (actual) 271 *actual = nbytes; 272 273 return status; 274 } 275 276 static dc_status_t 277 dc_packet_ioctl (dc_iostream_t *abstract, unsigned int request, void *data, size_t size) 278 { 279 dc_packet_t *packet = (dc_packet_t *) abstract; 280 281 return dc_iostream_ioctl (packet->iostream, request, data, size); 282 } 283 284 static dc_status_t 285 dc_packet_flush (dc_iostream_t *abstract) 286 { 287 dc_packet_t *packet = (dc_packet_t *) abstract; 288 289 return dc_iostream_flush (packet->iostream); 290 } 291 292 static dc_status_t 293 dc_packet_purge (dc_iostream_t *abstract, dc_direction_t direction) 294 { 295 dc_packet_t *packet = (dc_packet_t *) abstract; 296 297 if (direction & DC_DIRECTION_INPUT) { 298 packet->available = 0; 299 packet->offset = 0; 300 } 301 302 return dc_iostream_purge (packet->iostream, direction); 303 } 304 305 static dc_status_t 306 dc_packet_sleep (dc_iostream_t *abstract, unsigned int milliseconds) 307 { 308 dc_packet_t *packet = (dc_packet_t *) abstract; 309 310 return dc_iostream_sleep (packet->iostream, milliseconds); 311 } 312 313 static dc_status_t 314 dc_packet_close (dc_iostream_t *abstract) 315 { 316 dc_packet_t *packet = (dc_packet_t *) abstract; 317 318 free (packet->cache); 319 320 return DC_STATUS_SUCCESS; 321 }