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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 #include "libaltos_private.h"
19 #include "libaltos_posix.h"
22 #include <IOKit/usb/USBspec.h>
23 #include <sys/param.h>
28 /* Mac OS X don't have strndup even if _GNU_SOURCE is defined */
30 altos_strndup (const char *s, size_t n)
32 size_t len = strlen (s);
44 io_iterator_t iterator;
49 get_string(io_object_t object, CFStringRef entry, char *result, int result_len)
51 CFTypeRef entry_as_string;
54 entry_as_string = IORegistryEntrySearchCFProperty (object,
58 kIORegistryIterateRecursively);
59 if (entry_as_string) {
60 got_string = CFStringGetCString(entry_as_string,
62 kCFStringEncodingASCII);
64 CFRelease(entry_as_string);
72 get_number(io_object_t object, CFStringRef entry, int *result)
74 CFTypeRef entry_as_number;
77 entry_as_number = IORegistryEntrySearchCFProperty (object,
81 kIORegistryIterateRecursively);
82 if (entry_as_number) {
83 got_number = CFNumberGetValue(entry_as_number,
92 PUBLIC struct altos_list *
93 altos_list_start(void)
95 struct altos_list *list = calloc (sizeof (struct altos_list), 1);
96 CFMutableDictionaryRef matching_dictionary = IOServiceMatching("IOUSBDevice");
97 io_iterator_t tdIterator;
102 ret = IOServiceGetMatchingServices(kIOMasterPortDefault, matching_dictionary, &list->iterator);
103 if (ret != kIOReturnSuccess) {
111 PUBLIC struct altos_list *
112 altos_ftdi_list_start(void)
114 struct altos_list *list = altos_list_start();
122 altos_list_next(struct altos_list *list, struct altos_device *device)
125 char serial_string[128];
128 object = IOIteratorNext(list->iterator);
132 if (!get_number (object, CFSTR(kUSBVendorID), &device->vendor) ||
133 !get_number (object, CFSTR(kUSBProductID), &device->product))
135 if (get_string (object, CFSTR("IOCalloutDevice"), device->path, sizeof (device->path)) &&
136 get_string (object, CFSTR("USB Product Name"), device->name, sizeof (device->name)) &&
137 get_string (object, CFSTR("USB Serial Number"), serial_string, sizeof (serial_string))) {
138 device->serial = atoi(serial_string);
145 altos_list_finish(struct altos_list *list)
147 IOObjectRelease (list->iterator);
151 struct altos_bt_list {
158 #define INQUIRY_MAX_RSP 255
160 struct altos_bt_list *
161 altos_bt_list_start(int inquiry_time)
167 altos_bt_list_next(struct altos_bt_list *bt_list,
168 struct altos_bt_device *device)
174 altos_bt_list_finish(struct altos_bt_list *bt_list)
179 altos_bt_fill_in(char *name, char *addr, struct altos_bt_device *device)
181 strncpy(device->name, name, sizeof (device->name));
182 device->name[sizeof(device->name)-1] = '\0';
183 strncpy(device->addr, addr, sizeof (device->addr));
184 device->addr[sizeof(device->addr)-1] = '\0';
188 altos_bt_open(struct altos_bt_device *device)