custom.c (7974B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2017 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 <libdivecomputer/custom.h> 25 26 #include "iostream-private.h" 27 #include "common-private.h" 28 #include "context-private.h" 29 30 static dc_status_t dc_custom_set_timeout (dc_iostream_t *abstract, int timeout); 31 static dc_status_t dc_custom_set_break (dc_iostream_t *abstract, unsigned int value); 32 static dc_status_t dc_custom_set_dtr (dc_iostream_t *abstract, unsigned int value); 33 static dc_status_t dc_custom_set_rts (dc_iostream_t *abstract, unsigned int value); 34 static dc_status_t dc_custom_get_lines (dc_iostream_t *abstract, unsigned int *value); 35 static dc_status_t dc_custom_get_available (dc_iostream_t *abstract, size_t *value); 36 static dc_status_t dc_custom_configure (dc_iostream_t *abstract, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol); 37 static dc_status_t dc_custom_poll (dc_iostream_t *abstract, int timeout); 38 static dc_status_t dc_custom_read (dc_iostream_t *abstract, void *data, size_t size, size_t *actual); 39 static dc_status_t dc_custom_write (dc_iostream_t *abstract, const void *data, size_t size, size_t *actual); 40 static dc_status_t dc_custom_ioctl (dc_iostream_t *abstract, unsigned int request, void *data, size_t size); 41 static dc_status_t dc_custom_flush (dc_iostream_t *abstract); 42 static dc_status_t dc_custom_purge (dc_iostream_t *abstract, dc_direction_t direction); 43 static dc_status_t dc_custom_sleep (dc_iostream_t *abstract, unsigned int milliseconds); 44 static dc_status_t dc_custom_close (dc_iostream_t *abstract); 45 46 typedef struct dc_custom_t { 47 /* Base class. */ 48 dc_iostream_t base; 49 /* Internal state. */ 50 dc_custom_cbs_t callbacks; 51 void *userdata; 52 } dc_custom_t; 53 54 static const dc_iostream_vtable_t dc_custom_vtable = { 55 sizeof(dc_custom_t), 56 dc_custom_set_timeout, /* set_timeout */ 57 dc_custom_set_break, /* set_break */ 58 dc_custom_set_dtr, /* set_dtr */ 59 dc_custom_set_rts, /* set_rts */ 60 dc_custom_get_lines, /* get_lines */ 61 dc_custom_get_available, /* get_available */ 62 dc_custom_configure, /* configure */ 63 dc_custom_poll, /* poll */ 64 dc_custom_read, /* read */ 65 dc_custom_write, /* write */ 66 dc_custom_ioctl, /* ioctl */ 67 dc_custom_flush, /* flush */ 68 dc_custom_purge, /* purge */ 69 dc_custom_sleep, /* sleep */ 70 dc_custom_close, /* close */ 71 }; 72 73 dc_status_t 74 dc_custom_open (dc_iostream_t **out, dc_context_t *context, dc_transport_t transport, const dc_custom_cbs_t *callbacks, void *userdata) 75 { 76 dc_custom_t *custom = NULL; 77 78 if (out == NULL || callbacks == NULL) 79 return DC_STATUS_INVALIDARGS; 80 81 INFO (context, "Open: transport=%u", transport); 82 83 // Allocate memory. 84 custom = (dc_custom_t *) dc_iostream_allocate (context, &dc_custom_vtable, transport); 85 if (custom == NULL) { 86 ERROR (context, "Failed to allocate memory."); 87 return DC_STATUS_NOMEMORY; 88 } 89 90 custom->callbacks = *callbacks; 91 custom->userdata = userdata; 92 93 *out = (dc_iostream_t *) custom; 94 95 return DC_STATUS_SUCCESS; 96 } 97 98 static dc_status_t 99 dc_custom_set_timeout (dc_iostream_t *abstract, int timeout) 100 { 101 dc_custom_t *custom = (dc_custom_t *) abstract; 102 103 if (custom->callbacks.set_timeout == NULL) 104 return DC_STATUS_SUCCESS; 105 106 return custom->callbacks.set_timeout (custom->userdata, timeout); 107 } 108 109 static dc_status_t 110 dc_custom_set_break (dc_iostream_t *abstract, unsigned int value) 111 { 112 dc_custom_t *custom = (dc_custom_t *) abstract; 113 114 if (custom->callbacks.set_break == NULL) 115 return DC_STATUS_SUCCESS; 116 117 return custom->callbacks.set_break (custom->userdata, value); 118 } 119 120 static dc_status_t 121 dc_custom_set_dtr (dc_iostream_t *abstract, unsigned int value) 122 { 123 dc_custom_t *custom = (dc_custom_t *) abstract; 124 125 if (custom->callbacks.set_dtr == NULL) 126 return DC_STATUS_SUCCESS; 127 128 return custom->callbacks.set_dtr (custom->userdata, value); 129 } 130 131 static dc_status_t 132 dc_custom_set_rts (dc_iostream_t *abstract, unsigned int value) 133 { 134 dc_custom_t *custom = (dc_custom_t *) abstract; 135 136 if (custom->callbacks.set_rts == NULL) 137 return DC_STATUS_SUCCESS; 138 139 return custom->callbacks.set_rts (custom->userdata, value); 140 } 141 142 static dc_status_t 143 dc_custom_get_lines (dc_iostream_t *abstract, unsigned int *value) 144 { 145 dc_custom_t *custom = (dc_custom_t *) abstract; 146 147 if (custom->callbacks.get_lines == NULL) 148 return DC_STATUS_SUCCESS; 149 150 return custom->callbacks.get_lines (custom->userdata, value); 151 } 152 153 static dc_status_t 154 dc_custom_get_available (dc_iostream_t *abstract, size_t *value) 155 { 156 dc_custom_t *custom = (dc_custom_t *) abstract; 157 158 if (custom->callbacks.get_available == NULL) 159 return DC_STATUS_SUCCESS; 160 161 return custom->callbacks.get_available (custom->userdata, value); 162 } 163 164 static dc_status_t 165 dc_custom_configure (dc_iostream_t *abstract, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol) 166 { 167 dc_custom_t *custom = (dc_custom_t *) abstract; 168 169 if (custom->callbacks.configure == NULL) 170 return DC_STATUS_SUCCESS; 171 172 return custom->callbacks.configure (custom->userdata, baudrate, databits, parity, stopbits, flowcontrol); 173 } 174 175 static dc_status_t 176 dc_custom_poll (dc_iostream_t *abstract, int timeout) 177 { 178 dc_custom_t *custom = (dc_custom_t *) abstract; 179 180 if (custom->callbacks.poll == NULL) 181 return DC_STATUS_SUCCESS; 182 183 return custom->callbacks.poll (custom->userdata, timeout); 184 } 185 186 static dc_status_t 187 dc_custom_read (dc_iostream_t *abstract, void *data, size_t size, size_t *actual) 188 { 189 dc_custom_t *custom = (dc_custom_t *) abstract; 190 191 if (custom->callbacks.read == NULL) 192 return DC_STATUS_SUCCESS; 193 194 return custom->callbacks.read (custom->userdata, data, size, actual); 195 } 196 197 static dc_status_t 198 dc_custom_write (dc_iostream_t *abstract, const void *data, size_t size, size_t *actual) 199 { 200 dc_custom_t *custom = (dc_custom_t *) abstract; 201 202 if (custom->callbacks.write == NULL) 203 return DC_STATUS_SUCCESS; 204 205 return custom->callbacks.write (custom->userdata, data, size, actual); 206 } 207 208 static dc_status_t 209 dc_custom_ioctl (dc_iostream_t *abstract, unsigned int request, void *data, size_t size) 210 { 211 dc_custom_t *custom = (dc_custom_t *) abstract; 212 213 if (custom->callbacks.ioctl == NULL) 214 return DC_STATUS_SUCCESS; 215 216 return custom->callbacks.ioctl (custom->userdata, request, data, size); 217 } 218 219 static dc_status_t 220 dc_custom_flush (dc_iostream_t *abstract) 221 { 222 dc_custom_t *custom = (dc_custom_t *) abstract; 223 224 if (custom->callbacks.flush == NULL) 225 return DC_STATUS_SUCCESS; 226 227 return custom->callbacks.flush (custom->userdata); 228 } 229 230 static dc_status_t 231 dc_custom_purge (dc_iostream_t *abstract, dc_direction_t direction) 232 { 233 dc_custom_t *custom = (dc_custom_t *) abstract; 234 235 if (custom->callbacks.purge == NULL) 236 return DC_STATUS_SUCCESS; 237 238 return custom->callbacks.purge (custom->userdata, direction); 239 } 240 241 static dc_status_t 242 dc_custom_sleep (dc_iostream_t *abstract, unsigned int milliseconds) 243 { 244 dc_custom_t *custom = (dc_custom_t *) abstract; 245 246 if (custom->callbacks.sleep == NULL) 247 return DC_STATUS_SUCCESS; 248 249 return custom->callbacks.sleep (custom->userdata, milliseconds); 250 } 251 252 static dc_status_t 253 dc_custom_close (dc_iostream_t *abstract) 254 { 255 dc_custom_t *custom = (dc_custom_t *) abstract; 256 257 if (custom->callbacks.close == NULL) 258 return DC_STATUS_SUCCESS; 259 260 return custom->callbacks.close (custom->userdata); 261 }