irda.c (8703B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2008 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 #ifdef HAVE_CONFIG_H 23 #include "config.h" 24 #endif 25 26 #include <stdlib.h> // malloc, free 27 #include <string.h> 28 29 #include "socket.h" 30 31 #ifdef _WIN32 32 #ifdef HAVE_AF_IRDA_H 33 #define IRDA 34 #include <af_irda.h> 35 #endif 36 #else 37 #ifdef HAVE_LINUX_IRDA_H 38 #define IRDA 39 #include <linux/types.h> 40 #include <linux/irda.h> 41 #endif 42 #endif 43 44 #include <libdivecomputer/irda.h> 45 46 #include "common-private.h" 47 #include "context-private.h" 48 #include "iostream-private.h" 49 #include "iterator-private.h" 50 #include "array.h" 51 #include "platform.h" 52 53 #define ISINSTANCE(device) dc_iostream_isinstance((device), &dc_irda_vtable) 54 55 #define DISCOVER_MAX_DEVICES 16 // Maximum number of devices. 56 #define DISCOVER_MAX_RETRIES 4 // Maximum number of retries. 57 58 #ifdef _WIN32 59 #define DISCOVER_BUFSIZE sizeof (DEVICELIST) + \ 60 sizeof (IRDA_DEVICE_INFO) * (DISCOVER_MAX_DEVICES - 1) 61 #else 62 #define DISCOVER_BUFSIZE sizeof (struct irda_device_list) + \ 63 sizeof (struct irda_device_info) * (DISCOVER_MAX_DEVICES - 1) 64 #endif 65 66 struct dc_irda_device_t { 67 unsigned int address; 68 unsigned int charset; 69 unsigned int hints; 70 char name[22]; 71 }; 72 73 #ifdef IRDA 74 static dc_status_t dc_irda_iterator_next (dc_iterator_t *iterator, void *item); 75 76 typedef struct dc_irda_iterator_t { 77 dc_iterator_t base; 78 dc_irda_device_t items[DISCOVER_MAX_DEVICES]; 79 size_t count; 80 size_t current; 81 } dc_irda_iterator_t; 82 83 static const dc_iterator_vtable_t dc_irda_iterator_vtable = { 84 sizeof(dc_irda_iterator_t), 85 dc_irda_iterator_next, 86 NULL, 87 }; 88 89 static const dc_iostream_vtable_t dc_irda_vtable = { 90 sizeof(dc_socket_t), 91 dc_socket_set_timeout, /* set_timeout */ 92 NULL, /* set_break */ 93 NULL, /* set_dtr */ 94 NULL, /* set_rts */ 95 NULL, /* get_lines */ 96 dc_socket_get_available, /* get_available */ 97 NULL, /* configure */ 98 dc_socket_poll, /* poll */ 99 dc_socket_read, /* read */ 100 dc_socket_write, /* write */ 101 dc_socket_ioctl, /* ioctl */ 102 NULL, /* flush */ 103 NULL, /* purge */ 104 dc_socket_sleep, /* sleep */ 105 dc_socket_close, /* close */ 106 }; 107 #endif 108 109 unsigned int 110 dc_irda_device_get_address (dc_irda_device_t *device) 111 { 112 if (device == NULL) 113 return 0; 114 115 return device->address; 116 } 117 118 const char * 119 dc_irda_device_get_name (dc_irda_device_t *device) 120 { 121 if (device == NULL || device->name[0] == '\0') 122 return NULL; 123 124 return device->name; 125 } 126 127 void 128 dc_irda_device_free (dc_irda_device_t *device) 129 { 130 free (device); 131 } 132 133 dc_status_t 134 dc_irda_iterator_new (dc_iterator_t **out, dc_context_t *context, dc_descriptor_t *descriptor) 135 { 136 #ifdef IRDA 137 dc_status_t status = DC_STATUS_SUCCESS; 138 dc_irda_iterator_t *iterator = NULL; 139 140 if (out == NULL) 141 return DC_STATUS_INVALIDARGS; 142 143 iterator = (dc_irda_iterator_t *) dc_iterator_allocate (context, &dc_irda_iterator_vtable); 144 if (iterator == NULL) { 145 SYSERROR (context, S_ENOMEM); 146 return DC_STATUS_NOMEMORY; 147 } 148 149 // Initialize the socket library. 150 status = dc_socket_init (context); 151 if (status != DC_STATUS_SUCCESS) { 152 goto error_free; 153 } 154 155 // Open the socket. 156 s_socket_t fd = socket (AF_IRDA, SOCK_STREAM, 0); 157 if (fd == S_INVALID) { 158 s_errcode_t errcode = S_ERRNO; 159 SYSERROR (context, errcode); 160 status = dc_socket_syserror(errcode); 161 goto error_socket_exit; 162 } 163 164 unsigned char data[DISCOVER_BUFSIZE] = {0}; 165 #ifdef _WIN32 166 DEVICELIST *list = (DEVICELIST *) data; 167 #else 168 struct irda_device_list *list = (struct irda_device_list *) data; 169 #endif 170 s_socklen_t size = sizeof (data); 171 172 int rc = 0; 173 unsigned int nretries = 0; 174 while ((rc = getsockopt (fd, SOL_IRLMP, IRLMP_ENUMDEVICES, (char *) data, &size)) != 0 || 175 #ifdef _WIN32 176 list->numDevice == 0) 177 #else 178 list->len == 0) 179 #endif 180 { 181 // Automatically retry the discovery when no devices were found. 182 // On Linux, getsockopt fails with EAGAIN when no devices are 183 // discovered, while on Windows it succeeds and sets the number 184 // of devices to zero. Both situations are handled the same here. 185 if (rc != 0) { 186 s_errcode_t errcode = S_ERRNO; 187 if (errcode != S_EAGAIN) { 188 SYSERROR (context, errcode); 189 status = dc_socket_syserror(errcode); 190 goto error_socket_close; 191 } 192 } 193 194 // Abort if the maximum number of retries is reached. 195 if (nretries++ >= DISCOVER_MAX_RETRIES) { 196 break; 197 } 198 199 // Restore the size parameter in case it was 200 // modified by the previous getsockopt call. 201 size = sizeof (data); 202 203 dc_platform_sleep (1000); 204 } 205 206 S_CLOSE (fd); 207 dc_socket_exit (context); 208 209 unsigned int count = 0; 210 #ifdef _WIN32 211 for (size_t i = 0; i < list->numDevice; ++i) { 212 const char *name = list->Device[i].irdaDeviceName; 213 unsigned int address = array_uint32_le (list->Device[i].irdaDeviceID); 214 unsigned int charset = list->Device[i].irdaCharSet; 215 unsigned int hints = (list->Device[i].irdaDeviceHints1 << 8) + 216 list->Device[i].irdaDeviceHints2; 217 #else 218 for (size_t i = 0; i < list->len; ++i) { 219 const char *name = list->dev[i].info; 220 unsigned int address = list->dev[i].daddr; 221 unsigned int charset = list->dev[i].charset; 222 unsigned int hints = array_uint16_be (list->dev[i].hints); 223 #endif 224 225 INFO (context, "Discover: address=%08x, name=%s, charset=%02x, hints=%04x", 226 address, name, charset, hints); 227 228 if (!dc_descriptor_filter (descriptor, DC_TRANSPORT_IRDA, name)) { 229 continue; 230 } 231 232 strncpy(iterator->items[count].name, name, sizeof(iterator->items[count].name) - 1); 233 iterator->items[count].name[sizeof(iterator->items[count].name) - 1] = '\0'; 234 iterator->items[count].address = address; 235 iterator->items[count].charset = charset; 236 iterator->items[count].hints = hints; 237 count++; 238 } 239 240 iterator->current = 0; 241 iterator->count = count; 242 243 *out = (dc_iterator_t *) iterator; 244 245 return DC_STATUS_SUCCESS; 246 247 error_socket_close: 248 S_CLOSE (fd); 249 error_socket_exit: 250 dc_socket_exit (context); 251 error_free: 252 dc_iterator_deallocate ((dc_iterator_t *) iterator); 253 return status; 254 #else 255 return DC_STATUS_UNSUPPORTED; 256 #endif 257 } 258 259 #ifdef IRDA 260 static dc_status_t 261 dc_irda_iterator_next (dc_iterator_t *abstract, void *out) 262 { 263 dc_irda_iterator_t *iterator = (dc_irda_iterator_t *) abstract; 264 dc_irda_device_t *device = NULL; 265 266 if (iterator->current >= iterator->count) 267 return DC_STATUS_DONE; 268 269 device = (dc_irda_device_t *) malloc (sizeof(dc_irda_device_t)); 270 if (device == NULL) { 271 SYSERROR (abstract->context, S_ENOMEM); 272 return DC_STATUS_NOMEMORY; 273 } 274 275 *device = iterator->items[iterator->current++]; 276 277 *(dc_irda_device_t **) out = device; 278 279 return DC_STATUS_SUCCESS; 280 } 281 #endif 282 283 dc_status_t 284 dc_irda_open (dc_iostream_t **out, dc_context_t *context, unsigned int address, unsigned int lsap) 285 { 286 #ifdef IRDA 287 dc_status_t status = DC_STATUS_SUCCESS; 288 dc_socket_t *device = NULL; 289 290 if (out == NULL) 291 return DC_STATUS_INVALIDARGS; 292 293 INFO (context, "Open: address=%08x, lsap=%u", address, lsap); 294 295 // Allocate memory. 296 device = (dc_socket_t *) dc_iostream_allocate (context, &dc_irda_vtable, DC_TRANSPORT_IRDA); 297 if (device == NULL) { 298 SYSERROR (context, S_ENOMEM); 299 return DC_STATUS_NOMEMORY; 300 } 301 302 // Open the socket. 303 status = dc_socket_open (&device->base, AF_IRDA, SOCK_STREAM, 0); 304 if (status != DC_STATUS_SUCCESS) { 305 goto error_free; 306 } 307 308 #ifdef _WIN32 309 SOCKADDR_IRDA peer; 310 peer.irdaAddressFamily = AF_IRDA; 311 peer.irdaDeviceID[0] = (address ) & 0xFF; 312 peer.irdaDeviceID[1] = (address >> 8) & 0xFF; 313 peer.irdaDeviceID[2] = (address >> 16) & 0xFF; 314 peer.irdaDeviceID[3] = (address >> 24) & 0xFF; 315 dc_platform_snprintf (peer.irdaServiceName, sizeof(peer.irdaServiceName), "LSAP-SEL%u", lsap); 316 #else 317 struct sockaddr_irda peer; 318 peer.sir_family = AF_IRDA; 319 peer.sir_addr = address; 320 peer.sir_lsap_sel = lsap; 321 memset (peer.sir_name, 0x00, sizeof(peer.sir_name)); 322 #endif 323 324 status = dc_socket_connect (&device->base, (struct sockaddr *) &peer, sizeof (peer)); 325 if (status != DC_STATUS_SUCCESS) { 326 goto error_close; 327 } 328 329 *out = (dc_iostream_t *) device; 330 331 return DC_STATUS_SUCCESS; 332 333 error_close: 334 dc_socket_close (&device->base); 335 error_free: 336 dc_iostream_deallocate ((dc_iostream_t *) device); 337 return status; 338 #else 339 return DC_STATUS_UNSUPPORTED; 340 #endif 341 }