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);
453 #define USB_BUF_SIZE 64
458 unsigned char out_data[USB_BUF_SIZE];
460 unsigned char in_data[USB_BUF_SIZE];
463 pthread_mutex_t putc_mutex;
464 pthread_mutex_t getc_mutex;
468 altos_open(struct altos_device *device)
470 struct altos_file *file = calloc (sizeof (struct altos_file), 1);
478 file->fd = open(device->path, O_RDWR | O_NOCTTY);
480 perror(device->path);
484 ret = tcgetattr(file->fd, &term);
493 term.c_cc[VTIME] = 0;
494 ret = tcsetattr(file->fd, TCSAFLUSH, &term);
501 pthread_mutex_init(&file->putc_mutex,NULL);
502 pthread_mutex_init(&file->getc_mutex,NULL);
507 altos_close(struct altos_file *file)
509 if (file->fd != -1) {
512 write(file->pipe[1], "\r", 1);
518 altos_free(struct altos_file *file)
525 _altos_flush(struct altos_file *file)
527 while (file->out_used) {
533 ret = write (file->fd, file->out_data, file->out_used);
537 memmove(file->out_data, file->out_data + ret,
538 file->out_used - ret);
539 file->out_used -= ret;
545 altos_putchar(struct altos_file *file, char c)
549 pthread_mutex_lock(&file->putc_mutex);
550 if (file->out_used == USB_BUF_SIZE) {
551 ret = _altos_flush(file);
553 pthread_mutex_unlock(&file->putc_mutex);
557 file->out_data[file->out_used++] = c;
559 if (file->out_used == USB_BUF_SIZE)
560 ret = _altos_flush(file);
561 pthread_mutex_unlock(&file->putc_mutex);
566 altos_flush(struct altos_file *file)
569 pthread_mutex_lock(&file->putc_mutex);
570 ret = _altos_flush(file);
571 pthread_mutex_unlock(&file->putc_mutex);
579 altos_getchar(struct altos_file *file, int timeout)
586 pthread_mutex_lock(&file->getc_mutex);
588 fd[0].events = POLLIN;
589 fd[1].fd = file->pipe[0];
590 fd[1].events = POLLIN;
591 while (file->in_read == file->in_used) {
593 pthread_mutex_unlock(&file->getc_mutex);
594 return LIBALTOS_ERROR;
598 ret = poll(fd, 2, timeout);
600 perror("altos_getchar");
601 pthread_mutex_unlock(&file->getc_mutex);
602 return LIBALTOS_ERROR;
605 pthread_mutex_unlock(&file->getc_mutex);
606 return LIBALTOS_TIMEOUT;
608 if (fd[0].revents & POLLIN) {
609 ret = read(file->fd, file->in_data, USB_BUF_SIZE);
611 perror("altos_getchar");
612 pthread_mutex_unlock(&file->getc_mutex);
613 return LIBALTOS_ERROR;
619 ret = file->in_data[file->in_read++];
620 pthread_mutex_unlock(&file->getc_mutex);
624 #endif /* POSIX_TTY */
629 #include <setupapi.h>
636 #define USB_BUF_SIZE 64
640 unsigned char out_data[USB_BUF_SIZE];
642 unsigned char in_data[USB_BUF_SIZE];
648 PUBLIC struct altos_list *
649 altos_list_start(void)
651 struct altos_list *list = calloc(1, sizeof (struct altos_list));
655 list->dev_info = SetupDiGetClassDevs(NULL, "USB", NULL,
656 DIGCF_ALLCLASSES|DIGCF_PRESENT);
657 if (list->dev_info == INVALID_HANDLE_VALUE) {
658 printf("SetupDiGetClassDevs failed %d\n", GetLastError());
667 altos_list_next(struct altos_list *list, struct altos_device *device)
669 SP_DEVINFO_DATA dev_info_data;
682 dev_info_data.cbSize = sizeof (SP_DEVINFO_DATA);
683 while(SetupDiEnumDeviceInfo(list->dev_info, list->index,
688 dev_key = SetupDiOpenDevRegKey(list->dev_info, &dev_info_data,
689 DICS_FLAG_GLOBAL, 0, DIREG_DEV,
691 if (dev_key == INVALID_HANDLE_VALUE) {
692 printf("cannot open device registry key\n");
696 /* Fetch symbolic name for this device and parse out
697 * the vid/pid/serial info */
698 symbolic_len = sizeof(symbolic);
699 result = RegQueryValueEx(dev_key, "SymbolicName", NULL, NULL,
700 symbolic, &symbolic_len);
702 printf("cannot find SymbolicName value\n");
703 RegCloseKey(dev_key);
706 vid = pid = serial = 0;
707 sscanf(symbolic + sizeof("\\??\\USB#VID_") - 1,
709 sscanf(symbolic + sizeof("\\??\\USB#VID_XXXX&PID_") - 1,
711 sscanf(symbolic + sizeof("\\??\\USB#VID_XXXX&PID_XXXX#") - 1,
713 if (!USB_IS_ALTUSMETRUM(vid, pid)) {
714 printf("Not Altus Metrum symbolic name: %s\n",
716 RegCloseKey(dev_key);
720 /* Fetch the com port name */
721 port_len = sizeof (port);
722 result = RegQueryValueEx(dev_key, "PortName", NULL, NULL,
724 RegCloseKey(dev_key);
726 printf("failed to get PortName\n");
730 /* Fetch the 'location information' which is the device name,
732 location_len = sizeof (location);
733 if(!SetupDiGetDeviceRegistryProperty(list->dev_info,
735 SPDRP_LOCATION_INFORMATION,
741 printf("Failed to get location\n");
744 device->vendor = vid;
745 device->product = pid;
746 device->serial = serial;
748 if (strcasestr(location, "tele"))
749 strcpy(device->name, location);
751 strcpy(device->name, "");
753 strcpy(device->path, port);
754 printf ("product: %04x:%04x (%s) path: %s serial %d\n",
755 device->vendor, device->product, device->name,
756 device->path, device->serial);
759 result = GetLastError();
760 if (result != ERROR_NO_MORE_ITEMS)
761 printf ("SetupDiEnumDeviceInfo failed error %d\n", result);
766 altos_list_finish(struct altos_list *list)
768 SetupDiDestroyDeviceInfoList(list->dev_info);
773 altos_fill(struct altos_file *file, int timeout)
777 COMMTIMEOUTS timeouts;
779 if (file->in_read < file->in_used)
780 return LIBALTOS_SUCCESS;
781 file->in_read = file->in_used = 0;
784 timeouts.ReadIntervalTimeout = MAXDWORD;
785 timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
786 timeouts.ReadTotalTimeoutConstant = timeout;
788 timeouts.ReadIntervalTimeout = 0;
789 timeouts.ReadTotalTimeoutMultiplier = 0;
790 timeouts.ReadTotalTimeoutConstant = 0;
792 timeouts.WriteTotalTimeoutMultiplier = 0;
793 timeouts.WriteTotalTimeoutConstant = 0;
795 if (!SetCommTimeouts(file->handle, &timeouts)) {
796 printf("SetCommTimeouts failed %d\n", GetLastError());
799 if (!ReadFile(file->handle, file->in_data, USB_BUF_SIZE, &got, NULL)) {
800 result = GetLastError();
801 printf ("read failed %d\n", result);
802 return LIBALTOS_ERROR;
806 return LIBALTOS_SUCCESS;
807 return LIBALTOS_TIMEOUT;
811 altos_flush(struct altos_file *file)
814 char *data = file->out_data;
815 char used = file->out_used;
819 if (!WriteFile(file->handle, data, used, &put, NULL)) {
820 result = GetLastError();
821 printf ("write failed %d\n", result);
822 return LIBALTOS_ERROR;
828 return LIBALTOS_SUCCESS;
831 PUBLIC struct altos_file *
832 altos_open(struct altos_device *device)
834 struct altos_file *file = calloc (sizeof (struct altos_file), 1);
840 strcpy(full_name, "\\\\.\\");
841 strcat(full_name, device->path);
842 file->handle = CreateFile(full_name, GENERIC_READ|GENERIC_WRITE,
843 0, NULL, OPEN_EXISTING,
844 FILE_ATTRIBUTE_NORMAL, NULL);
845 if (file->handle == INVALID_HANDLE_VALUE) {
850 timeouts.ReadIntervalTimeout = MAXDWORD;
851 timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
852 timeouts.ReadTotalTimeoutConstant = 100;
853 timeouts.WriteTotalTimeoutMultiplier = 0;
854 timeouts.WriteTotalTimeoutConstant = 10000;
855 if (!SetCommTimeouts(file->handle, &timeouts)) {
856 printf("SetCommTimeouts failed %d\n", GetLastError());
863 altos_close(struct altos_file *file)
865 if (file->handle != INVALID_HANDLE_VALUE) {
866 CloseHandle(file->handle);
867 file->handle = INVALID_HANDLE_VALUE;
872 altos_free(struct altos_file *file)
879 altos_putchar(struct altos_file *file, char c)
883 if (file->out_used == USB_BUF_SIZE) {
884 ret = altos_flush(file);
888 file->out_data[file->out_used++] = c;
889 if (file->out_used == USB_BUF_SIZE)
890 return altos_flush(file);
891 return LIBALTOS_SUCCESS;
895 altos_getchar(struct altos_file *file, int timeout)
898 while (file->in_read == file->in_used) {
899 ret = altos_flush(file);
902 if (file->handle == INVALID_HANDLE_VALUE)
903 return LIBALTOS_ERROR;
904 ret = altos_fill(file, timeout);
908 return file->in_data[file->in_read++];