subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

hdlc.c (10341B)


      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 
     24 #include "hdlc.h"
     25 
     26 #include "iostream-private.h"
     27 #include "common-private.h"
     28 #include "context-private.h"
     29 
     30 #define END     0x7E
     31 #define ESC     0x7D
     32 #define ESC_BIT 0x20
     33 
     34 static dc_status_t dc_hdlc_set_timeout (dc_iostream_t *abstract, int timeout);
     35 static dc_status_t dc_hdlc_set_break (dc_iostream_t *abstract, unsigned int value);
     36 static dc_status_t dc_hdlc_set_dtr (dc_iostream_t *abstract, unsigned int value);
     37 static dc_status_t dc_hdlc_set_rts (dc_iostream_t *abstract, unsigned int value);
     38 static dc_status_t dc_hdlc_get_lines (dc_iostream_t *abstract, unsigned int *value);
     39 static dc_status_t dc_hdlc_configure (dc_iostream_t *abstract, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol);
     40 static dc_status_t dc_hdlc_poll (dc_iostream_t *abstract, int timeout);
     41 static dc_status_t dc_hdlc_read (dc_iostream_t *abstract, void *data, size_t size, size_t *actual);
     42 static dc_status_t dc_hdlc_write (dc_iostream_t *abstract, const void *data, size_t size, size_t *actual);
     43 static dc_status_t dc_hdlc_ioctl (dc_iostream_t *abstract, unsigned int request, void *data, size_t size);
     44 static dc_status_t dc_hdlc_flush (dc_iostream_t *abstract);
     45 static dc_status_t dc_hdlc_purge (dc_iostream_t *abstract, dc_direction_t direction);
     46 static dc_status_t dc_hdlc_sleep (dc_iostream_t *abstract, unsigned int milliseconds);
     47 static dc_status_t dc_hdlc_close (dc_iostream_t *abstract);
     48 
     49 typedef struct dc_hdlc_t {
     50 	/* Base class. */
     51 	dc_iostream_t base;
     52 	/* Internal state. */
     53 	dc_context_t *context;
     54 	dc_iostream_t *iostream;
     55 	unsigned char *rbuf;
     56 	unsigned char *wbuf;
     57 	size_t rbuf_size;
     58 	size_t rbuf_offset;
     59 	size_t rbuf_available;
     60 	size_t wbuf_size;
     61 	size_t wbuf_offset;
     62 } dc_hdlc_t;
     63 
     64 static const dc_iostream_vtable_t dc_hdlc_vtable = {
     65 	sizeof(dc_hdlc_t),
     66 	dc_hdlc_set_timeout, /* set_timeout */
     67 	dc_hdlc_set_break, /* set_break */
     68 	dc_hdlc_set_dtr, /* set_dtr */
     69 	dc_hdlc_set_rts, /* set_rts */
     70 	dc_hdlc_get_lines, /* get_lines */
     71 	NULL, /* get_available */
     72 	dc_hdlc_configure, /* configure */
     73 	dc_hdlc_poll, /* poll */
     74 	dc_hdlc_read, /* read */
     75 	dc_hdlc_write, /* write */
     76 	dc_hdlc_ioctl, /* ioctl */
     77 	dc_hdlc_flush, /* flush */
     78 	dc_hdlc_purge, /* purge */
     79 	dc_hdlc_sleep, /* sleep */
     80 	dc_hdlc_close, /* close */
     81 };
     82 
     83 dc_status_t
     84 dc_hdlc_open (dc_iostream_t **out, dc_context_t *context, dc_iostream_t *base, size_t isize, size_t osize)
     85 {
     86 	dc_status_t status = DC_STATUS_SUCCESS;
     87 	dc_hdlc_t *hdlc = NULL;
     88 
     89 	if (out == NULL)
     90 		return DC_STATUS_INVALIDARGS;
     91 
     92 	if (base == NULL || isize == 0 || osize == 0)
     93 		return DC_STATUS_INVALIDARGS;
     94 
     95 	dc_transport_t transport = dc_iostream_get_transport (base);
     96 
     97 	// Allocate memory.
     98 	hdlc = (dc_hdlc_t *) dc_iostream_allocate (NULL, &dc_hdlc_vtable, transport);
     99 	if (hdlc == NULL) {
    100 		ERROR (context, "Failed to allocate memory.");
    101 		status = DC_STATUS_NOMEMORY;
    102 		goto error_exit;
    103 	}
    104 
    105 	// Allocate the read buffer.
    106 	hdlc->rbuf = malloc (isize);
    107 	if (hdlc->rbuf == NULL) {
    108 		ERROR (context, "Failed to allocate memory.");
    109 		status = DC_STATUS_NOMEMORY;
    110 		goto error_free;
    111 	}
    112 
    113 	// Allocate the write buffer.
    114 	hdlc->wbuf = malloc (osize);
    115 	if (hdlc->wbuf == NULL) {
    116 		ERROR (context, "Failed to allocate memory.");
    117 		status = DC_STATUS_NOMEMORY;
    118 		goto error_free_rbuf;
    119 	}
    120 
    121 	hdlc->context = context;
    122 	hdlc->iostream = base;
    123 	hdlc->rbuf_size = isize;
    124 	hdlc->rbuf_offset = 0;
    125 	hdlc->rbuf_available = 0;
    126 	hdlc->wbuf_size = osize;
    127 	hdlc->wbuf_offset = 0;
    128 
    129 	*out = (dc_iostream_t *) hdlc;
    130 
    131 	return DC_STATUS_SUCCESS;
    132 
    133 error_free_rbuf:
    134 	free (hdlc->rbuf);
    135 error_free:
    136 	dc_iostream_deallocate ((dc_iostream_t *) hdlc);
    137 error_exit:
    138 	return status;
    139 }
    140 
    141 static dc_status_t
    142 dc_hdlc_set_timeout (dc_iostream_t *abstract, int timeout)
    143 {
    144 	dc_hdlc_t *hdlc = (dc_hdlc_t *) abstract;
    145 
    146 	return dc_iostream_set_timeout (hdlc->iostream, timeout);
    147 }
    148 
    149 static dc_status_t
    150 dc_hdlc_set_break (dc_iostream_t *abstract, unsigned int value)
    151 {
    152 	dc_hdlc_t *hdlc = (dc_hdlc_t *) abstract;
    153 
    154 	return dc_iostream_set_break (hdlc->iostream, value);
    155 }
    156 
    157 static dc_status_t
    158 dc_hdlc_set_dtr (dc_iostream_t *abstract, unsigned int value)
    159 {
    160 	dc_hdlc_t *hdlc = (dc_hdlc_t *) abstract;
    161 
    162 	return dc_iostream_set_dtr (hdlc->iostream, value);
    163 }
    164 
    165 static dc_status_t
    166 dc_hdlc_set_rts (dc_iostream_t *abstract, unsigned int value)
    167 {
    168 	dc_hdlc_t *hdlc = (dc_hdlc_t *) abstract;
    169 
    170 	return dc_iostream_set_rts (hdlc->iostream, value);
    171 }
    172 
    173 static dc_status_t
    174 dc_hdlc_get_lines (dc_iostream_t *abstract, unsigned int *value)
    175 {
    176 	dc_hdlc_t *hdlc = (dc_hdlc_t *) abstract;
    177 
    178 	return dc_iostream_get_lines (hdlc->iostream, value);
    179 }
    180 
    181 static dc_status_t
    182 dc_hdlc_configure (dc_iostream_t *abstract, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol)
    183 {
    184 	dc_hdlc_t *hdlc = (dc_hdlc_t *) abstract;
    185 
    186 	return dc_iostream_configure (hdlc->iostream, baudrate, databits, parity, stopbits, flowcontrol);
    187 }
    188 
    189 static dc_status_t
    190 dc_hdlc_poll (dc_iostream_t *abstract, int timeout)
    191 {
    192 	dc_hdlc_t *hdlc = (dc_hdlc_t *) abstract;
    193 
    194 	if (hdlc->rbuf_available) {
    195 		return DC_STATUS_SUCCESS;
    196 	}
    197 
    198 	return dc_iostream_poll (hdlc->iostream, timeout);
    199 }
    200 
    201 static dc_status_t
    202 dc_hdlc_read (dc_iostream_t *abstract, void *data, size_t size, size_t *actual)
    203 {
    204 	dc_status_t status = DC_STATUS_SUCCESS;
    205 	dc_hdlc_t *hdlc = (dc_hdlc_t *) abstract;
    206 	size_t nbytes = 0;
    207 
    208 	unsigned int initialized = 0;
    209 	unsigned int escaped = 0;
    210 
    211 	while (1) {
    212 		if (hdlc->rbuf_available == 0) {
    213 			// Read a packet into the cache.
    214 			size_t len = 0;
    215 			status = dc_iostream_read (hdlc->iostream, hdlc->rbuf, hdlc->rbuf_size, &len);
    216 			if (status != DC_STATUS_SUCCESS) {
    217 				goto out;
    218 			}
    219 
    220 			hdlc->rbuf_available = len;
    221 			hdlc->rbuf_offset = 0;
    222 		}
    223 
    224 		while (hdlc->rbuf_available) {
    225 			unsigned char c = hdlc->rbuf[hdlc->rbuf_offset];
    226 			hdlc->rbuf_offset++;
    227 			hdlc->rbuf_available--;
    228 
    229 			if (c == END) {
    230 				if (escaped) {
    231 					ERROR (hdlc->context, "HDLC frame escaped the special character %02x.", c);
    232 					status = DC_STATUS_IO;
    233 					goto out;
    234 				}
    235 
    236 				if (initialized) {
    237 					goto out;
    238 				}
    239 
    240 				initialized = 1;
    241 				continue;
    242 			}
    243 
    244 			if (!initialized) {
    245 				continue;
    246 			}
    247 
    248 			if (c == ESC) {
    249 				if (escaped) {
    250 					ERROR (hdlc->context, "HDLC frame escaped the special character %02x.", c);
    251 					status = DC_STATUS_IO;
    252 					goto out;
    253 				}
    254 				escaped = 1;
    255 				continue;
    256 			}
    257 
    258 			if (escaped) {
    259 				c ^= ESC_BIT;
    260 				escaped = 0;
    261 			}
    262 
    263 			if (nbytes < size)
    264 				((unsigned char *)data)[nbytes] = c;
    265 			nbytes++;
    266 		}
    267 	}
    268 
    269 out:
    270 	if (nbytes > size) {
    271 		ERROR (hdlc->context, "HDLC frame is too large (" DC_PRINTF_SIZE " " DC_PRINTF_SIZE ").", nbytes, size);
    272 		dc_status_set_error (&status, DC_STATUS_IO);
    273 		nbytes = size;
    274 	}
    275 
    276 	if (actual)
    277 		*actual = nbytes;
    278 
    279 	return status;
    280 }
    281 
    282 static dc_status_t
    283 dc_hdlc_write (dc_iostream_t *abstract, const void *data, size_t size, size_t *actual)
    284 {
    285 	dc_status_t status = DC_STATUS_SUCCESS;
    286 	dc_hdlc_t *hdlc = (dc_hdlc_t *) abstract;
    287 	size_t nbytes = 0;
    288 
    289 	// Clear the buffer.
    290 	hdlc->wbuf_offset = 0;
    291 
    292 	// Start of the packet.
    293 	hdlc->wbuf[hdlc->wbuf_offset++] = END;
    294 
    295 	// Flush the buffer if necessary.
    296 	if (hdlc->wbuf_offset >= hdlc->wbuf_size) {
    297 		status = dc_iostream_write (hdlc->iostream, hdlc->wbuf, hdlc->wbuf_offset, NULL);
    298 		if (status != DC_STATUS_SUCCESS) {
    299 			goto out;
    300 		}
    301 
    302 		hdlc->wbuf_offset = 0;
    303 	}
    304 
    305 	while (nbytes < size) {
    306 		unsigned char c = ((const unsigned char *) data)[nbytes];
    307 
    308 		if (c == END || c == ESC) {
    309 			// Append the escape character.
    310 			hdlc->wbuf[hdlc->wbuf_offset++] = ESC;
    311 
    312 			// Flush the buffer if necessary.
    313 			if (hdlc->wbuf_offset >= hdlc->wbuf_size) {
    314 				status = dc_iostream_write (hdlc->iostream, hdlc->wbuf, hdlc->wbuf_offset, NULL);
    315 				if (status != DC_STATUS_SUCCESS) {
    316 					goto out;
    317 				}
    318 
    319 				hdlc->wbuf_offset = 0;
    320 			}
    321 
    322 			// Escape the character.
    323 			c ^= ESC_BIT;
    324 		}
    325 
    326 		// Append the character.
    327 		hdlc->wbuf[hdlc->wbuf_offset++] = c;
    328 
    329 		// Flush the buffer if necessary.
    330 		if (hdlc->wbuf_offset >= hdlc->wbuf_size) {
    331 			status = dc_iostream_write (hdlc->iostream, hdlc->wbuf, hdlc->wbuf_offset, NULL);
    332 			if (status != DC_STATUS_SUCCESS) {
    333 				goto out;
    334 			}
    335 
    336 			hdlc->wbuf_offset = 0;
    337 		}
    338 
    339 		nbytes++;
    340 	}
    341 
    342 	// End of the packet.
    343 	hdlc->wbuf[hdlc->wbuf_offset++] = END;
    344 
    345 	// Flush the buffer.
    346 	status = dc_iostream_write (hdlc->iostream, hdlc->wbuf, hdlc->wbuf_offset, NULL);
    347 	if (status != DC_STATUS_SUCCESS) {
    348 		goto out;
    349 	}
    350 
    351 	hdlc->wbuf_offset = 0;
    352 
    353 out:
    354 	if (actual)
    355 		*actual = nbytes;
    356 
    357 	return status;
    358 }
    359 
    360 static dc_status_t
    361 dc_hdlc_ioctl (dc_iostream_t *abstract, unsigned int request, void *data, size_t size)
    362 {
    363 	dc_hdlc_t *hdlc = (dc_hdlc_t *) abstract;
    364 
    365 	return dc_iostream_ioctl (hdlc->iostream, request, data, size);
    366 }
    367 
    368 static dc_status_t
    369 dc_hdlc_flush (dc_iostream_t *abstract)
    370 {
    371 	dc_hdlc_t *hdlc = (dc_hdlc_t *) abstract;
    372 
    373 	return dc_iostream_flush (hdlc->iostream);
    374 }
    375 
    376 static dc_status_t
    377 dc_hdlc_purge (dc_iostream_t *abstract, dc_direction_t direction)
    378 {
    379 	dc_hdlc_t *hdlc = (dc_hdlc_t *) abstract;
    380 
    381 	if (direction & DC_DIRECTION_INPUT) {
    382 		hdlc->rbuf_available = 0;
    383 		hdlc->rbuf_offset = 0;
    384 	}
    385 
    386 	return dc_iostream_purge (hdlc->iostream, direction);
    387 }
    388 
    389 static dc_status_t
    390 dc_hdlc_sleep (dc_iostream_t *abstract, unsigned int milliseconds)
    391 {
    392 	dc_hdlc_t *hdlc = (dc_hdlc_t *) abstract;
    393 
    394 	return dc_iostream_sleep (hdlc->iostream, milliseconds);
    395 }
    396 
    397 static dc_status_t
    398 dc_hdlc_close (dc_iostream_t *abstract)
    399 {
    400 	dc_hdlc_t *hdlc = (dc_hdlc_t *) abstract;
    401 
    402 	free (hdlc->wbuf);
    403 	free (hdlc->rbuf);
    404 
    405 	return DC_STATUS_SUCCESS;
    406 }