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

iostream.h (9862B)


      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 #ifndef DC_IOSTREAM_H
     23 #define DC_IOSTREAM_H
     24 
     25 #include <stddef.h>
     26 #include <libdivecomputer/common.h>
     27 #include <libdivecomputer/context.h>
     28 
     29 #ifdef __cplusplus
     30 extern "C" {
     31 #endif /* __cplusplus */
     32 
     33 /**
     34  * Opaque object representing a I/O stream.
     35  */
     36 typedef struct dc_iostream_t dc_iostream_t;
     37 
     38 /**
     39  * The parity checking scheme.
     40  */
     41 typedef enum dc_parity_t {
     42 	DC_PARITY_NONE, /**< No parity */
     43 	DC_PARITY_ODD,  /**< Odd parity */
     44 	DC_PARITY_EVEN, /**< Even parity */
     45 	DC_PARITY_MARK, /**< Mark parity (always 1) */
     46 	DC_PARITY_SPACE /**< Space parity (alwasy 0) */
     47 } dc_parity_t;
     48 
     49 /**
     50  * The number of stop bits.
     51  */
     52 typedef enum dc_stopbits_t {
     53 	DC_STOPBITS_ONE,          /**< 1 stop bit */
     54 	DC_STOPBITS_ONEPOINTFIVE, /**< 1.5 stop bits*/
     55 	DC_STOPBITS_TWO           /**< 2 stop bits */
     56 } dc_stopbits_t;
     57 
     58 /**
     59  * The flow control.
     60  */
     61 typedef enum dc_flowcontrol_t {
     62 	DC_FLOWCONTROL_NONE,     /**< No flow control */
     63 	DC_FLOWCONTROL_HARDWARE, /**< Hardware (RTS/CTS) flow control */
     64 	DC_FLOWCONTROL_SOFTWARE  /**< Software (XON/XOFF) flow control */
     65 } dc_flowcontrol_t;
     66 
     67 /**
     68  * The direction of the data transmission.
     69  */
     70 typedef enum dc_direction_t {
     71 	DC_DIRECTION_INPUT = 0x01,  /**< Input direction */
     72 	DC_DIRECTION_OUTPUT = 0x02, /**< Output direction */
     73 	DC_DIRECTION_ALL = DC_DIRECTION_INPUT | DC_DIRECTION_OUTPUT /**< All directions */
     74 } dc_direction_t;
     75 
     76 /**
     77  * The line signals.
     78  */
     79 typedef enum dc_line_t {
     80 	DC_LINE_DCD = 0x01, /**< Data carrier detect */
     81 	DC_LINE_CTS = 0x02, /**< Clear to send */
     82 	DC_LINE_DSR = 0x04, /**< Data set ready */
     83 	DC_LINE_RNG = 0x08, /**< Ring indicator */
     84 } dc_line_t;
     85 
     86 /**
     87  * Get the transport type.
     88  *
     89  * @param[in]  iostream  A valid I/O stream.
     90  * @returns The transport type of the I/O stream.
     91  */
     92 dc_transport_t
     93 dc_iostream_get_transport (dc_iostream_t *iostream);
     94 
     95 /**
     96  * Set the read timeout.
     97  *
     98  * There are three distinct modes available:
     99  *
    100  *  1. Blocking (timeout < 0):
    101  *
    102  *     The read operation is blocked until all the requested bytes have
    103  *     been received. If the requested number of bytes does not arrive,
    104  *     the operation will block forever.
    105  *
    106  *  2. Non-blocking (timeout == 0):
    107  *
    108  *     The read operation returns immediately with the bytes that have
    109  *     already been received, even if no bytes have been received.
    110  *
    111  *  3. Timeout (timeout > 0):
    112  *
    113  *     The read operation is blocked until all the requested bytes have
    114  *     been received. If the requested number of bytes does not arrive
    115  *     within the specified amount of time, the operation will return
    116  *     with the bytes that have already been received.
    117  *
    118  * @param[in]  iostream  A valid I/O stream.
    119  * @param[in]  timeout   The timeout in milliseconds.
    120  * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
    121  * on failure.
    122  */
    123 dc_status_t
    124 dc_iostream_set_timeout (dc_iostream_t *iostream, int timeout);
    125 
    126 /**
    127  * Set the state of the break condition.
    128  *
    129  * @param[in]  iostream  A valid I/O stream.
    130  * @param[in]  value     The break condition state.
    131  * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
    132  * on failure.
    133  */
    134 dc_status_t
    135 dc_iostream_set_break (dc_iostream_t *iostream, unsigned int value);
    136 
    137 /**
    138  * Set the state of the DTR line.
    139  *
    140  * @param[in]  iostream  A valid I/O stream.
    141  * @param[in]  value     The DTR line state.
    142  * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
    143  * on failure.
    144  */
    145 dc_status_t
    146 dc_iostream_set_dtr (dc_iostream_t *iostream, unsigned int value);
    147 
    148 /**
    149  * Set the state of the RTS line.
    150  *
    151  * @param[in]  iostream  A valid I/O stream.
    152  * @param[in]  value     The RTS line state.
    153  * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
    154  * on failure.
    155  */
    156 dc_status_t
    157 dc_iostream_set_rts (dc_iostream_t *iostream, unsigned int value);
    158 
    159 /**
    160  * Query the state of the line signals.
    161  *
    162  * @param[in]   iostream  A valid I/O stream.
    163  * @param[out]  value     A location to store the bitmap with the state
    164  *                        of the line signals.
    165  * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
    166  * on failure.
    167  */
    168 dc_status_t
    169 dc_iostream_get_lines (dc_iostream_t *iostream, unsigned int *value);
    170 
    171 /**
    172  * Query the number of available bytes in the input buffer.
    173  *
    174  * @param[in]   iostream  A valid I/O stream.
    175  * @param[out]  value     A location to store the number of bytes in the
    176  *                        input buffer.
    177  * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
    178  * on failure.
    179  */
    180 dc_status_t
    181 dc_iostream_get_available (dc_iostream_t *iostream, size_t *value);
    182 
    183 /**
    184  * Configure the line settings.
    185  *
    186  * @param[in]  iostream     A valid I/O stream.
    187  * @param[in]  baudrate     The baud rate setting.
    188  * @param[in]  databits     The number of data bits.
    189  * @param[in]  parity       The parity setting.
    190  * @param[in]  stopbits     The number of stop bits.
    191  * @param[in]  flowcontrol  The flow control setting.
    192  * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
    193  * on failure.
    194  */
    195 dc_status_t
    196 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);
    197 
    198 /**
    199  * Poll the I/O stream for available data.
    200  *
    201  * There are three distinct modes available:
    202  *
    203  *  1. Blocking (timeout < 0):
    204  *
    205  *     The poll operation is blocked until one or more bytes have been
    206  *     received. If no bytes are received, the operation will block
    207  *     forever.
    208  *
    209  *  2. Non-blocking (timeout == 0):
    210  *
    211  *     The poll operation returns immediately, even if no bytes have
    212  *     been received.
    213  *
    214  *  3. Timeout (timeout > 0):
    215  *
    216  *     The poll operation is blocked until one or more bytes have been
    217  *     received. If no bytes are received within the specified amount of
    218  *     time, the operation will return with a timeout.
    219  *
    220  * @param[in]  iostream  A valid I/O stream.
    221  * @param[in]  timeout   The timeout in milliseconds.
    222  * @returns #DC_STATUS_SUCCESS on success, #DC_STATUS_TIMEOUT on
    223  * timeout, or another #dc_status_t code on failure.
    224  */
    225 dc_status_t
    226 dc_iostream_poll (dc_iostream_t *iostream, int timeout);
    227 
    228 /**
    229  * Read data from the I/O stream.
    230  *
    231  * @param[in]  iostream  A valid I/O stream.
    232  * @param[out] data      The memory buffer to read the data into.
    233  * @param[in]  size      The number of bytes to read.
    234  * @param[out] actual    An (optional) location to store the actual
    235  *                       number of bytes transferred.
    236  * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
    237  * on failure.
    238  */
    239 dc_status_t
    240 dc_iostream_read (dc_iostream_t *iostream, void *data, size_t size, size_t *actual);
    241 
    242 /**
    243  * Write data to the I/O stream.
    244  *
    245  * @param[in]  iostream  A valid I/O stream.
    246  * @param[in]  data      The memory buffer to write the data from.
    247  * @param[in]  size      The number of bytes to write.
    248  * @param[out] actual    An (optional) location to store the actual
    249  *                       number of bytes transferred.
    250  * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
    251  * on failure.
    252  */
    253 dc_status_t
    254 dc_iostream_write (dc_iostream_t *iostream, const void *data, size_t size, size_t *actual);
    255 
    256 /**
    257  * Perform an I/O stream specific request.
    258  *
    259  * @param[in]  iostream  A valid I/O stream.
    260  * @param[in]  request   The request to perform.
    261  * @param[in,out]  data  The request specific data.
    262  * @param[in]  size      The size of the request specific data.
    263  * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
    264  * on failure.
    265  */
    266 dc_status_t
    267 dc_iostream_ioctl (dc_iostream_t *iostream, unsigned int request, void *data, size_t size);
    268 
    269 /**
    270  * Flush the internal output buffer and wait until the data has been
    271  * transmitted.
    272  *
    273  * @param[in]  iostream  A valid I/O stream.
    274  * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
    275  * on failure.
    276  */
    277 dc_status_t
    278 dc_iostream_flush (dc_iostream_t *iostream);
    279 
    280 /**
    281  * Discards all data from the internal buffers.
    282  *
    283  * @param[in]  iostream   A valid I/O stream.
    284  * @param[in]  direction  The direction of the buffer(s).
    285  * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
    286  * on failure.
    287  */
    288 dc_status_t
    289 dc_iostream_purge (dc_iostream_t *iostream, dc_direction_t direction);
    290 
    291 /**
    292  * Suspend execution of the current thread for the specified amount of
    293  * time.
    294  *
    295  * @param[in]  iostream      A valid I/O stream.
    296  * @param[in]  milliseconds  The number of milliseconds to sleep.
    297  * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
    298  * on failure.
    299  */
    300 dc_status_t
    301 dc_iostream_sleep (dc_iostream_t *iostream, unsigned int milliseconds);
    302 
    303 /**
    304  * Close the I/O stream and free all resources.
    305  *
    306  * @param[in]  iostream  A valid I/O stream.
    307  * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code
    308  * on failure.
    309  */
    310 dc_status_t
    311 dc_iostream_close (dc_iostream_t *iostream);
    312 
    313 dc_status_t
    314 dc_usb_storage_open (dc_iostream_t **out, dc_context_t *context, const char *name);
    315 
    316 #ifdef __cplusplus
    317 }
    318 #endif /* __cplusplus */
    319 #endif /* DC_IOSTREAM_H */