2 * Copyright © 2010 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.
27 return LIBALTOS_SUCCESS;
36 /* Mac OS X don't have strndup even if _GNU_SOURCE is defined */
38 altos_strndup (const char *s, size_t n)
40 size_t len = strlen (s);
52 #define altos_strndup strndup
56 * Scan for Altus Metrum devices by looking through /sys
69 cc_fullname (char *dir, char *file)
72 int dlen = strlen (dir);
73 int flen = strlen (file);
76 if (dir[dlen-1] != '/')
78 new = malloc (dlen + slen + flen + 1);
89 cc_basename(char *file)
93 b = strrchr(file, '/');
100 load_string(char *dir, char *file)
102 char *full = cc_fullname(dir, file);
108 f = fopen(full, "r");
112 r = fgets(line, sizeof (line), f);
117 if (r[rlen-1] == '\n')
123 load_hex(char *dir, char *file)
129 line = load_string(dir, file);
132 i = strtol(line, &end, 16);
140 load_dec(char *dir, char *file)
146 line = load_string(dir, file);
149 i = strtol(line, &end, 10);
157 dir_filter_tty_colon(const struct dirent *d)
159 return strncmp(d->d_name, "tty:", 4) == 0;
163 dir_filter_tty(const struct dirent *d)
165 return strncmp(d->d_name, "tty", 3) == 0;
168 struct altos_usbdev {
173 int serial; /* AltOS always uses simple integer serial numbers */
184 struct dirent **namelist;
187 char endpoint_base[20];
193 base = cc_basename(sys);
194 num_configs = load_hex(sys, "bNumConfigurations");
195 num_interfaces = load_hex(sys, "bNumInterfaces");
196 for (config = 1; config <= num_configs; config++) {
197 for (interface = 0; interface < num_interfaces; interface++) {
198 sprintf(endpoint_base, "%s:%d.%d",
199 base, config, interface);
200 endpoint_full = cc_fullname(sys, endpoint_base);
202 /* Check for tty:ttyACMx style names
204 ntty = scandir(endpoint_full, &namelist,
205 dir_filter_tty_colon,
209 tty = cc_fullname("/dev", namelist[0]->d_name + 4);
214 /* Check for tty/ttyACMx style names
216 tty_dir = cc_fullname(endpoint_full, "tty");
218 ntty = scandir(tty_dir, &namelist,
223 tty = cc_fullname("/dev", namelist[0]->d_name);
232 static struct altos_usbdev *
233 usb_scan_device(char *sys)
235 struct altos_usbdev *usbdev;
237 usbdev = calloc(1, sizeof (struct altos_usbdev));
240 usbdev->sys = strdup(sys);
241 usbdev->manufacturer = load_string(sys, "manufacturer");
242 usbdev->product_name = load_string(sys, "product");
243 usbdev->serial = load_dec(sys, "serial");
244 usbdev->idProduct = load_hex(sys, "idProduct");
245 usbdev->idVendor = load_hex(sys, "idVendor");
246 usbdev->tty = usb_tty(sys);
251 usbdev_free(struct altos_usbdev *usbdev)
254 free(usbdev->manufacturer);
255 free(usbdev->product_name);
256 /* this can get used as a return value */
262 #define USB_DEVICES "/sys/bus/usb/devices"
265 dir_filter_dev(const struct dirent *d)
267 const char *n = d->d_name;
275 if (c == '.' && n != d->d_name + 1)
283 struct altos_usbdev **dev;
289 altos_list_start(void)
292 struct dirent **ents;
294 struct altos_usbdev *dev;
295 struct altos_list *devs;
298 devs = calloc(1, sizeof (struct altos_list));
302 n = scandir (USB_DEVICES, &ents,
307 for (e = 0; e < n; e++) {
308 dir = cc_fullname(USB_DEVICES, ents[e]->d_name);
309 dev = usb_scan_device(dir);
311 if (USB_IS_ALTUSMETRUM(dev->idVendor, dev->idProduct)) {
313 devs->dev = realloc(devs->dev,
314 devs->ndev + 1 * sizeof (struct usbdev *));
316 devs->dev = malloc (sizeof (struct usbdev *));
317 devs->dev[devs->ndev++] = dev;
326 altos_list_next(struct altos_list *list, struct altos_device *device)
328 struct altos_usbdev *dev;
329 if (list->current >= list->ndev)
331 dev = list->dev[list->current];
332 strcpy(device->name, dev->product_name);
333 device->vendor = dev->idVendor;
334 device->product = dev->idProduct;
335 strcpy(device->path, dev->tty);
336 device->serial = dev->serial;
342 altos_list_finish(struct altos_list *usbdevs)
348 for (i = 0; i < usbdevs->ndev; i++)
349 usbdev_free(usbdevs->dev[i]);
357 #include <IOKitLib.h>
358 #include <IOKit/usb/USBspec.h>
359 #include <sys/param.h>
361 #include <CFNumber.h>
368 io_iterator_t iterator;
372 get_string(io_object_t object, CFStringRef entry, char *result, int result_len)
374 CFTypeRef entry_as_string;
377 entry_as_string = IORegistryEntrySearchCFProperty (object,
381 kIORegistryIterateRecursively);
382 if (entry_as_string) {
383 got_string = CFStringGetCString(entry_as_string,
385 kCFStringEncodingASCII);
387 CFRelease(entry_as_string);
395 altos_list_start(void)
397 struct altos_list *list = calloc (sizeof (struct altos_list), 1);
398 CFMutableDictionaryRef matching_dictionary = IOServiceMatching("IOUSBDevice");
399 UInt32 vendor = 0xfffe, product = 0x000a;
400 CFNumberRef vendor_ref = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vendor);
401 CFNumberRef product_ref = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &product);
402 io_iterator_t tdIterator;
403 io_object_t tdObject;
405 CFDictionaryAddValue(matching_dictionary, CFSTR(kUSBVendorID), vendor_ref);
406 CFDictionaryAddValue(matching_dictionary, CFSTR(kUSBProductID), product_ref);
408 IOServiceGetMatchingServices(kIOMasterPortDefault, matching_dictionary, &list->iterator);
410 CFRelease(vendor_ref);
411 CFRelease(product_ref);
416 altos_list_next(struct altos_list *list, struct altos_device *device)
419 char serial_string[128];
422 object = IOIteratorNext(list->iterator);
426 if (get_string (object, CFSTR("IOCalloutDevice"), device->path, sizeof (device->path)) &&
427 get_string (object, CFSTR("USB Product Name"), device->product, sizeof (device->product)) &&
428 get_string (object, CFSTR("USB Serial Number"), serial_string, sizeof (serial_string))) {
429 device->serial = atoi(serial_string);
436 altos_list_finish(struct altos_list *list)
438 IOObjectRelease (list->iterator);
452 #define USB_BUF_SIZE 64
456 unsigned char out_data[USB_BUF_SIZE];
458 unsigned char in_data[USB_BUF_SIZE];
464 altos_open(struct altos_device *device)
466 struct altos_file *file = calloc (sizeof (struct altos_file), 1);
473 file->fd = open(device->path, O_RDWR | O_NOCTTY);
475 perror(device->path);
479 ret = tcgetattr(file->fd, &term);
488 term.c_cc[VTIME] = 1;
489 ret = tcsetattr(file->fd, TCSAFLUSH, &term);
500 altos_close(struct altos_file *file)
502 if (file->fd != -1) {
509 altos_free(struct altos_file *file)
516 altos_putchar(struct altos_file *file, char c)
520 if (file->out_used == USB_BUF_SIZE) {
521 ret = altos_flush(file);
525 file->out_data[file->out_used++] = c;
526 if (file->out_used == USB_BUF_SIZE)
527 return altos_flush(file);
532 altos_flush(struct altos_file *file)
534 while (file->out_used) {
539 ret = write (file->fd, file->out_data, file->out_used);
543 memmove(file->out_data, file->out_data + ret,
544 file->out_used - ret);
545 file->out_used -= ret;
553 altos_getchar(struct altos_file *file, int timeout)
555 while (file->in_read == file->in_used) {
566 ret = poll(&fd, 1, timeout);
568 return LIBALTOS_TIMEOUT;
570 ret = read(file->fd, file->in_data, USB_BUF_SIZE);
572 return LIBALTOS_ERROR;
576 return file->in_data[file->in_read++];
579 #endif /* POSIX_TTY */
584 #include <setupapi.h>
591 #define USB_BUF_SIZE 64
595 unsigned char out_data[USB_BUF_SIZE];
597 unsigned char in_data[USB_BUF_SIZE];
603 PUBLIC struct altos_list *
604 altos_list_start(void)
606 struct altos_list *list = calloc(1, sizeof (struct altos_list));
610 list->dev_info = SetupDiGetClassDevs(NULL, "USB", NULL,
611 DIGCF_ALLCLASSES|DIGCF_PRESENT);
612 if (list->dev_info == INVALID_HANDLE_VALUE) {
613 printf("SetupDiGetClassDevs failed %d\n", GetLastError());
622 altos_list_next(struct altos_list *list, struct altos_device *device)
624 SP_DEVINFO_DATA dev_info_data;
637 dev_info_data.cbSize = sizeof (SP_DEVINFO_DATA);
638 while(SetupDiEnumDeviceInfo(list->dev_info, list->index,
643 dev_key = SetupDiOpenDevRegKey(list->dev_info, &dev_info_data,
644 DICS_FLAG_GLOBAL, 0, DIREG_DEV,
646 if (dev_key == INVALID_HANDLE_VALUE) {
647 printf("cannot open device registry key\n");
651 /* Fetch symbolic name for this device and parse out
652 * the vid/pid/serial info */
653 symbolic_len = sizeof(symbolic);
654 result = RegQueryValueEx(dev_key, "SymbolicName", NULL, NULL,
655 symbolic, &symbolic_len);
657 printf("cannot find SymbolicName value\n");
658 RegCloseKey(dev_key);
661 vid = pid = serial = 0;
662 sscanf(symbolic + sizeof("\\??\\USB#VID_") - 1,
664 sscanf(symbolic + sizeof("\\??\\USB#VID_XXXX&PID_") - 1,
666 sscanf(symbolic + sizeof("\\??\\USB#VID_XXXX&PID_XXXX#") - 1,
668 if (!USB_IS_ALTUSMETRUM(vid, pid)) {
669 printf("Not Altus Metrum symbolic name: %s\n",
671 RegCloseKey(dev_key);
675 /* Fetch the com port name */
676 port_len = sizeof (port);
677 result = RegQueryValueEx(dev_key, "PortName", NULL, NULL,
679 RegCloseKey(dev_key);
681 printf("failed to get PortName\n");
685 /* Fetch the 'location information' which is the device name,
687 location_len = sizeof (location);
688 if(!SetupDiGetDeviceRegistryProperty(list->dev_info,
690 SPDRP_LOCATION_INFORMATION,
696 printf("Failed to get location\n");
699 device->vendor = vid;
700 device->product = pid;
701 device->serial = serial;
703 if (strcasestr(location, "tele"))
704 strcpy(device->name, location);
706 strcpy(device->name, "");
708 strcpy(device->path, port);
709 printf ("product: %04x:%04x (%s) path: %s serial %d\n",
710 device->vendor, device->product, device->name,
711 device->path, device->serial);
714 result = GetLastError();
715 if (result != ERROR_NO_MORE_ITEMS)
716 printf ("SetupDiEnumDeviceInfo failed error %d\n", result);
721 altos_list_finish(struct altos_list *list)
723 SetupDiDestroyDeviceInfoList(list->dev_info);
728 altos_fill(struct altos_file *file, int timeout)
732 COMMTIMEOUTS timeouts;
734 if (file->in_read < file->in_used)
735 return LIBALTOS_SUCCESS;
736 file->in_read = file->in_used = 0;
739 timeouts.ReadIntervalTimeout = MAXDWORD;
740 timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
741 timeouts.ReadTotalTimeoutConstant = timeout;
743 timeouts.ReadIntervalTimeout = 0;
744 timeouts.ReadTotalTimeoutMultiplier = 0;
745 timeouts.ReadTotalTimeoutConstant = 0;
747 timeouts.WriteTotalTimeoutMultiplier = 0;
748 timeouts.WriteTotalTimeoutConstant = 0;
750 if (!SetCommTimeouts(file->handle, &timeouts)) {
751 printf("SetCommTimeouts failed %d\n", GetLastError());
754 if (!ReadFile(file->handle, file->in_data, USB_BUF_SIZE, &got, NULL)) {
755 result = GetLastError();
756 printf ("read failed %d\n", result);
757 return LIBALTOS_ERROR;
761 return LIBALTOS_SUCCESS;
762 return LIBALTOS_TIMEOUT;
766 altos_flush(struct altos_file *file)
769 char *data = file->out_data;
770 char used = file->out_used;
774 if (!WriteFile(file->handle, data, used, &put, NULL)) {
775 result = GetLastError();
776 printf ("write failed %d\n", result);
777 return LIBALTOS_ERROR;
783 return LIBALTOS_SUCCESS;
786 PUBLIC struct altos_file *
787 altos_open(struct altos_device *device)
789 struct altos_file *file = calloc (sizeof (struct altos_file), 1);
795 strcpy(full_name, "\\\\.\\");
796 strcat(full_name, device->path);
797 file->handle = CreateFile(full_name, GENERIC_READ|GENERIC_WRITE,
798 0, NULL, OPEN_EXISTING,
799 FILE_ATTRIBUTE_NORMAL, NULL);
800 if (file->handle == INVALID_HANDLE_VALUE) {
805 timeouts.ReadIntervalTimeout = MAXDWORD;
806 timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
807 timeouts.ReadTotalTimeoutConstant = 100;
808 timeouts.WriteTotalTimeoutMultiplier = 0;
809 timeouts.WriteTotalTimeoutConstant = 10000;
810 if (!SetCommTimeouts(file->handle, &timeouts)) {
811 printf("SetCommTimeouts failed %d\n", GetLastError());
818 altos_close(struct altos_file *file)
820 if (file->handle != INVALID_HANDLE_VALUE) {
821 CloseHandle(file->handle);
822 file->handle = INVALID_HANDLE_VALUE;
827 altos_free(struct altos_file *file)
834 altos_putchar(struct altos_file *file, char c)
838 if (file->out_used == USB_BUF_SIZE) {
839 ret = altos_flush(file);
843 file->out_data[file->out_used++] = c;
844 if (file->out_used == USB_BUF_SIZE)
845 return altos_flush(file);
846 return LIBALTOS_SUCCESS;
850 altos_getchar(struct altos_file *file, int timeout)
853 while (file->in_read == file->in_used) {
854 ret = altos_flush(file);
857 if (file->handle == INVALID_HANDLE_VALUE)
858 return LIBALTOS_ERROR;
859 ret = altos_fill(file, timeout);
863 return file->in_data[file->in_read++];