2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include "libaltos_private.h"
21 #include "libaltos_posix.h"
25 #include <bluetooth/bluetooth.h>
26 #include <bluetooth/hci.h>
27 #include <bluetooth/hci_lib.h>
28 #include <bluetooth/rfcomm.h>
31 cc_fullname (char *dir, char *file)
34 int dlen = strlen (dir);
35 int flen = strlen (file);
38 if (dir[dlen-1] != '/')
40 new = malloc (dlen + slen + flen + 1);
51 cc_basename(char *file)
55 b = strrchr(file, '/');
62 load_string(char *dir, char *file)
64 char *full = cc_fullname(dir, file);
74 r = fgets(line, sizeof (line), f);
79 if (r[rlen-1] == '\n')
85 load_hex(char *dir, char *file)
91 line = load_string(dir, file);
94 i = strtol(line, &end, 16);
102 load_dec(char *dir, char *file)
108 line = load_string(dir, file);
111 i = strtol(line, &end, 10);
119 dir_filter_tty_colon(const struct dirent *d)
121 return strncmp(d->d_name, "tty:", 4) == 0;
125 dir_filter_tty(const struct dirent *d)
127 return strncmp(d->d_name, "tty", 3) == 0;
130 struct altos_usbdev {
135 int serial; /* AltOS always uses simple integer serial numbers */
146 struct dirent **namelist;
149 char endpoint_base[20];
155 base = cc_basename(sys);
156 num_configs = load_hex(sys, "bNumConfigurations");
157 num_interfaces = load_hex(sys, "bNumInterfaces");
158 for (config = 1; config <= num_configs; config++) {
159 for (interface = 0; interface < num_interfaces; interface++) {
160 sprintf(endpoint_base, "%s:%d.%d",
161 base, config, interface);
162 endpoint_full = cc_fullname(sys, endpoint_base);
165 /* Check for tty:ttyACMx style names
167 ntty = scandir(endpoint_full, &namelist,
168 dir_filter_tty_colon,
172 tty = cc_fullname("/dev", namelist[0]->d_name + 4);
177 /* Check for tty/ttyACMx style names
179 tty_dir = cc_fullname(endpoint_full, "tty");
180 ntty = scandir(tty_dir, &namelist,
185 tty = cc_fullname("/dev", namelist[0]->d_name);
191 /* Check for ttyACMx style names
193 ntty = scandir(endpoint_full, &namelist,
198 tty = cc_fullname("/dev", namelist[0]->d_name);
208 static struct altos_usbdev *
209 usb_scan_device(char *sys)
211 struct altos_usbdev *usbdev;
217 usbdev = calloc(1, sizeof (struct altos_usbdev));
220 usbdev->sys = strdup(sys);
221 usbdev->manufacturer = load_string(sys, "manufacturer");
222 usbdev->product_name = load_string(sys, "product");
223 usbdev->serial = load_dec(sys, "serial");
224 usbdev->idProduct = load_hex(sys, "idProduct");
225 usbdev->idVendor = load_hex(sys, "idVendor");
231 usbdev_free(struct altos_usbdev *usbdev)
234 free(usbdev->manufacturer);
235 free(usbdev->product_name);
236 /* this can get used as a return value */
242 #define USB_DEVICES "/sys/bus/usb/devices"
245 dir_filter_dev(const struct dirent *d)
247 const char *n = d->d_name;
255 if (c == '.' && n != d->d_name + 1)
263 struct altos_usbdev **dev;
269 altos_list_start(void)
272 struct dirent **ents;
274 struct altos_usbdev *dev;
275 struct altos_list *devs;
278 devs = calloc(1, sizeof (struct altos_list));
282 n = scandir (USB_DEVICES, &ents,
287 for (e = 0; e < n; e++) {
288 dir = cc_fullname(USB_DEVICES, ents[e]->d_name);
289 dev = usb_scan_device(dir);
294 devs->dev = realloc(devs->dev,
295 (devs->ndev + 1) * sizeof (struct usbdev *));
297 devs->dev = malloc (sizeof (struct usbdev *));
298 devs->dev[devs->ndev++] = dev;
305 PUBLIC struct altos_list *
306 altos_ftdi_list_start(void)
308 return altos_list_start();
312 altos_list_next(struct altos_list *list, struct altos_device *device)
314 struct altos_usbdev *dev;
315 if (list->current >= list->ndev) {
318 dev = list->dev[list->current];
319 strcpy(device->name, dev->product_name);
320 device->vendor = dev->idVendor;
321 device->product = dev->idProduct;
322 strcpy(device->path, dev->tty);
323 device->serial = dev->serial;
329 altos_list_finish(struct altos_list *usbdevs)
335 for (i = 0; i < usbdevs->ndev; i++)
336 usbdev_free(usbdevs->dev[i]);
343 static int bt_initialized;
345 static int init_bt(void) {
346 if (!bt_initialized) {
348 libbt = dlopen("libbluetooth.so.3", RTLD_LAZY);
350 printf("failed to find bluetooth library\n");
352 return libbt != NULL;
355 #define join(a,b) a ## b
356 #define bt_func(name, ret, fail, formals, actuals) \
357 static ret join(altos_, name) formals { \
358 static ret (*name) formals; \
359 if (!init_bt()) return fail; \
360 name = dlsym(libbt, #name); \
361 if (!name) return fail; \
362 return name actuals; \
365 bt_func(ba2str, int, -1, (const bdaddr_t *ba, char *str), (ba, str))
366 #define ba2str altos_ba2str
368 bt_func(str2ba, int, -1, (const char *str, bdaddr_t *ba), (str, ba))
369 #define str2ba altos_str2ba
371 bt_func(hci_read_remote_name, int, -1, (int sock, const bdaddr_t *ba, int len, char *name, int timeout), (sock, ba, len, name, timeout))
372 #define hci_read_remote_name altos_hci_read_remote_name
374 bt_func(hci_open_dev, int, -1, (int dev_id), (dev_id))
375 #define hci_open_dev altos_hci_open_dev
377 bt_func(hci_get_route, int, -1, (bdaddr_t *bdaddr), (bdaddr))
378 #define hci_get_route altos_hci_get_route
380 bt_func(hci_inquiry, int, -1, (int adapter_id, int len, int max_rsp, const uint8_t *lap, inquiry_info **devs, long flags), (adapter_id, len, max_rsp, lap, devs, flags))
381 #define hci_inquiry altos_hci_inquiry
383 struct altos_bt_list {
391 #define INQUIRY_MAX_RSP 255
393 struct altos_bt_list *
394 altos_bt_list_start(int inquiry_time)
396 struct altos_bt_list *bt_list;
398 bt_list = calloc(1, sizeof (struct altos_bt_list));
402 bt_list->ii = calloc(INQUIRY_MAX_RSP, sizeof (inquiry_info));
405 bt_list->dev_id = hci_get_route(NULL);
406 if (bt_list->dev_id < 0)
409 bt_list->sock = hci_open_dev(bt_list->dev_id);
410 if (bt_list->sock < 0)
413 bt_list->num_rsp = hci_inquiry(bt_list->dev_id,
419 if (bt_list->num_rsp < 0)
426 close(bt_list->sock);
437 altos_bt_list_next(struct altos_bt_list *bt_list,
438 struct altos_bt_device *device)
442 if (bt_list->rsp >= bt_list->num_rsp)
445 ii = &bt_list->ii[bt_list->rsp];
446 if (ba2str(&ii->bdaddr, device->addr) < 0)
448 memset(&device->name, '\0', sizeof (device->name));
449 if (hci_read_remote_name(bt_list->sock, &ii->bdaddr,
450 sizeof (device->name),
451 device->name, 0) < 0) {
452 strcpy(device->name, "[unknown]");
459 altos_bt_list_finish(struct altos_bt_list *bt_list)
461 close(bt_list->sock);
467 altos_bt_fill_in(char *name, char *addr, struct altos_bt_device *device)
469 strncpy(device->name, name, sizeof (device->name));
470 device->name[sizeof(device->name)-1] = '\0';
471 strncpy(device->addr, addr, sizeof (device->addr));
472 device->addr[sizeof(device->addr)-1] = '\0';
476 altos_bt_open(struct altos_bt_device *device)
478 struct sockaddr_rc addr = { 0 };
480 struct altos_file_posix *file;
482 file = calloc(1, sizeof (struct altos_file_posix));
485 altos_set_last_posix_error();
488 addr.rc_family = AF_BLUETOOTH;
490 if (str2ba(device->addr, &addr.rc_bdaddr) < 0) {
491 altos_set_last_posix_error();
495 for (i = 0; i < 5; i++) {
496 file->fd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
498 altos_set_last_posix_error();
502 status = connect(file->fd,
503 (struct sockaddr *)&addr,
505 if (status >= 0 || errno != EBUSY)
511 altos_set_last_posix_error();
519 file->out_fd = dup(file->fd);