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.
23 #define BLUETOOTH_PRODUCT_TELEBT "TeleBT"
30 return LIBALTOS_SUCCESS;
38 static struct altos_error last_error;
41 altos_set_last_error(int code, char *string)
43 last_error.code = code;
44 strncpy(last_error.string, string, sizeof (last_error.string) -1);
45 last_error.string[sizeof(last_error.string)-1] = '\0';
49 altos_get_last_error(struct altos_error *error)
58 /* Mac OS X don't have strndup even if _GNU_SOURCE is defined */
60 altos_strndup (const char *s, size_t n)
62 size_t len = strlen (s);
74 #define altos_strndup strndup
85 #define USB_BUF_SIZE 64
94 unsigned char out_data[USB_BUF_SIZE];
96 unsigned char in_data[USB_BUF_SIZE];
102 altos_set_last_posix_error(void)
104 altos_set_last_error(errno, strerror(errno));
107 PUBLIC struct altos_file *
108 altos_open(struct altos_device *device)
110 struct altos_file *file = calloc (sizeof (struct altos_file), 1);
115 altos_set_last_posix_error();
119 // altos_set_last_error(12, "yeah yeah, failed again");
123 file->fd = open(device->path, O_RDWR | O_NOCTTY);
125 altos_set_last_posix_error();
132 file->out_fd = open(device->path, O_RDWR | O_NOCTTY);
133 if (file->out_fd < 0) {
134 altos_set_last_posix_error();
140 ret = tcgetattr(file->fd, &term);
142 altos_set_last_posix_error();
153 term.c_cc[VTIME] = 0;
156 term.c_cc[VTIME] = 1;
158 ret = tcsetattr(file->fd, TCSAFLUSH, &term);
160 altos_set_last_posix_error();
172 altos_close(struct altos_file *file)
174 if (file->fd != -1) {
178 write(file->pipe[1], "\r", 1);
188 altos_free(struct altos_file *file)
195 altos_flush(struct altos_file *file)
197 if (file->out_used && 0) {
199 fwrite(file->out_data, 1, file->out_used, stdout);
202 while (file->out_used) {
208 ret = write (file->fd, file->out_data, file->out_used);
210 ret = write (file->out_fd, file->out_data, file->out_used);
213 altos_set_last_posix_error();
214 return -last_error.code;
217 memmove(file->out_data, file->out_data + ret,
218 file->out_used - ret);
219 file->out_used -= ret;
226 altos_putchar(struct altos_file *file, char c)
230 if (file->out_used == USB_BUF_SIZE) {
231 ret = altos_flush(file);
236 file->out_data[file->out_used++] = c;
238 if (file->out_used == USB_BUF_SIZE)
239 ret = altos_flush(file);
248 altos_fill(struct altos_file *file, int timeout)
257 while (file->in_read == file->in_used) {
259 return LIBALTOS_ERROR;
262 fd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
263 fd[1].fd = file->pipe[0];
264 fd[1].events = POLLIN;
265 ret = poll(fd, 2, timeout);
267 altos_set_last_posix_error();
268 return LIBALTOS_ERROR;
271 return LIBALTOS_TIMEOUT;
273 if (fd[0].revents & (POLLHUP|POLLERR|POLLNVAL))
274 return LIBALTOS_ERROR;
275 if (fd[0].revents & POLLIN)
278 ret = read(file->fd, file->in_data, USB_BUF_SIZE);
280 altos_set_last_posix_error();
281 return LIBALTOS_ERROR;
286 if (ret == 0 && timeout > 0)
287 return LIBALTOS_TIMEOUT;
291 if (file->in_used && 0) {
293 fwrite(file->in_data, 1, file->in_used, stdout);
300 altos_getchar(struct altos_file *file, int timeout)
303 while (file->in_read == file->in_used) {
305 return LIBALTOS_ERROR;
306 ret = altos_fill(file, timeout);
310 return file->in_data[file->in_read++];
313 #endif /* POSIX_TTY */
316 * Scan for Altus Metrum devices by looking through /sys
327 #include <bluetooth/bluetooth.h>
328 #include <bluetooth/hci.h>
329 #include <bluetooth/hci_lib.h>
330 #include <bluetooth/rfcomm.h>
333 cc_fullname (char *dir, char *file)
336 int dlen = strlen (dir);
337 int flen = strlen (file);
340 if (dir[dlen-1] != '/')
342 new = malloc (dlen + slen + flen + 1);
353 cc_basename(char *file)
357 b = strrchr(file, '/');
364 load_string(char *dir, char *file)
366 char *full = cc_fullname(dir, file);
372 f = fopen(full, "r");
376 r = fgets(line, sizeof (line), f);
381 if (r[rlen-1] == '\n')
387 load_hex(char *dir, char *file)
393 line = load_string(dir, file);
396 i = strtol(line, &end, 16);
404 load_dec(char *dir, char *file)
410 line = load_string(dir, file);
413 i = strtol(line, &end, 10);
421 dir_filter_tty_colon(const struct dirent *d)
423 return strncmp(d->d_name, "tty:", 4) == 0;
427 dir_filter_tty(const struct dirent *d)
429 return strncmp(d->d_name, "tty", 3) == 0;
432 struct altos_usbdev {
437 int serial; /* AltOS always uses simple integer serial numbers */
448 struct dirent **namelist;
451 char endpoint_base[20];
457 base = cc_basename(sys);
458 num_configs = load_hex(sys, "bNumConfigurations");
459 num_interfaces = load_hex(sys, "bNumInterfaces");
460 for (config = 1; config <= num_configs; config++) {
461 for (interface = 0; interface < num_interfaces; interface++) {
462 sprintf(endpoint_base, "%s:%d.%d",
463 base, config, interface);
464 endpoint_full = cc_fullname(sys, endpoint_base);
467 /* Check for tty:ttyACMx style names
469 ntty = scandir(endpoint_full, &namelist,
470 dir_filter_tty_colon,
474 tty = cc_fullname("/dev", namelist[0]->d_name + 4);
479 /* Check for ttyACMx style names
481 ntty = scandir(endpoint_full, &namelist,
486 tty = cc_fullname("/dev", namelist[0]->d_name);
491 /* Check for tty/ttyACMx style names
493 tty_dir = cc_fullname(endpoint_full, "tty");
495 ntty = scandir(tty_dir, &namelist,
500 tty = cc_fullname("/dev", namelist[0]->d_name);
510 static struct altos_usbdev *
511 usb_scan_device(char *sys)
513 struct altos_usbdev *usbdev;
519 usbdev = calloc(1, sizeof (struct altos_usbdev));
522 usbdev->sys = strdup(sys);
523 usbdev->manufacturer = load_string(sys, "manufacturer");
524 usbdev->product_name = load_string(sys, "product");
525 usbdev->serial = load_dec(sys, "serial");
526 usbdev->idProduct = load_hex(sys, "idProduct");
527 usbdev->idVendor = load_hex(sys, "idVendor");
533 usbdev_free(struct altos_usbdev *usbdev)
536 free(usbdev->manufacturer);
537 free(usbdev->product_name);
538 /* this can get used as a return value */
544 #define USB_DEVICES "/sys/bus/usb/devices"
547 dir_filter_dev(const struct dirent *d)
549 const char *n = d->d_name;
557 if (c == '.' && n != d->d_name + 1)
565 struct altos_usbdev **dev;
571 altos_list_start(void)
574 struct dirent **ents;
576 struct altos_usbdev *dev;
577 struct altos_list *devs;
580 devs = calloc(1, sizeof (struct altos_list));
584 n = scandir (USB_DEVICES, &ents,
589 for (e = 0; e < n; e++) {
590 dir = cc_fullname(USB_DEVICES, ents[e]->d_name);
591 dev = usb_scan_device(dir);
596 devs->dev = realloc(devs->dev,
597 (devs->ndev + 1) * sizeof (struct usbdev *));
599 devs->dev = malloc (sizeof (struct usbdev *));
600 devs->dev[devs->ndev++] = dev;
608 altos_list_next(struct altos_list *list, struct altos_device *device)
610 struct altos_usbdev *dev;
611 if (list->current >= list->ndev) {
614 dev = list->dev[list->current];
615 strcpy(device->name, dev->product_name);
616 device->vendor = dev->idVendor;
617 device->product = dev->idProduct;
618 strcpy(device->path, dev->tty);
619 device->serial = dev->serial;
625 altos_list_finish(struct altos_list *usbdevs)
631 for (i = 0; i < usbdevs->ndev; i++)
632 usbdev_free(usbdevs->dev[i]);
636 struct altos_bt_list {
644 #define INQUIRY_MAX_RSP 255
646 struct altos_bt_list *
647 altos_bt_list_start(int inquiry_time)
649 struct altos_bt_list *bt_list;
651 bt_list = calloc(1, sizeof (struct altos_bt_list));
655 bt_list->ii = calloc(INQUIRY_MAX_RSP, sizeof (inquiry_info));
658 bt_list->dev_id = hci_get_route(NULL);
659 if (bt_list->dev_id < 0)
662 bt_list->sock = hci_open_dev(bt_list->dev_id);
663 if (bt_list->sock < 0)
666 bt_list->num_rsp = hci_inquiry(bt_list->dev_id,
672 if (bt_list->num_rsp < 0)
679 close(bt_list->sock);
690 altos_bt_list_next(struct altos_bt_list *bt_list,
691 struct altos_bt_device *device)
695 if (bt_list->rsp >= bt_list->num_rsp)
698 ii = &bt_list->ii[bt_list->rsp];
699 ba2str(&ii->bdaddr, device->addr);
700 memset(&device->name, '\0', sizeof (device->name));
701 if (hci_read_remote_name(bt_list->sock, &ii->bdaddr,
702 sizeof (device->name),
703 device->name, 0) < 0) {
704 strcpy(device->name, "[unknown]");
711 altos_bt_list_finish(struct altos_bt_list *bt_list)
713 close(bt_list->sock);
719 altos_bt_fill_in(char *name, char *addr, struct altos_bt_device *device)
721 strncpy(device->name, name, sizeof (device->name));
722 device->name[sizeof(device->name)-1] = '\0';
723 strncpy(device->addr, addr, sizeof (device->addr));
724 device->addr[sizeof(device->addr)-1] = '\0';
728 altos_bt_open(struct altos_bt_device *device)
730 struct sockaddr_rc addr = { 0 };
732 struct altos_file *file;
734 file = calloc(1, sizeof (struct altos_file));
737 file->fd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
739 altos_set_last_posix_error();
743 addr.rc_family = AF_BLUETOOTH;
745 str2ba(device->addr, &addr.rc_bdaddr);
747 status = connect(file->fd,
748 (struct sockaddr *)&addr,
751 altos_set_last_posix_error();
759 file->out_fd = dup(file->fd);
774 #include <IOKitLib.h>
775 #include <IOKit/usb/USBspec.h>
776 #include <sys/param.h>
778 #include <CFNumber.h>
785 io_iterator_t iterator;
789 get_string(io_object_t object, CFStringRef entry, char *result, int result_len)
791 CFTypeRef entry_as_string;
794 entry_as_string = IORegistryEntrySearchCFProperty (object,
798 kIORegistryIterateRecursively);
799 if (entry_as_string) {
800 got_string = CFStringGetCString(entry_as_string,
802 kCFStringEncodingASCII);
804 CFRelease(entry_as_string);
812 get_number(io_object_t object, CFStringRef entry, int *result)
814 CFTypeRef entry_as_number;
817 entry_as_number = IORegistryEntrySearchCFProperty (object,
821 kIORegistryIterateRecursively);
822 if (entry_as_number) {
823 got_number = CFNumberGetValue(entry_as_number,
832 PUBLIC struct altos_list *
833 altos_list_start(void)
835 struct altos_list *list = calloc (sizeof (struct altos_list), 1);
836 CFMutableDictionaryRef matching_dictionary = IOServiceMatching("IOUSBDevice");
837 io_iterator_t tdIterator;
838 io_object_t tdObject;
842 ret = IOServiceGetMatchingServices(kIOMasterPortDefault, matching_dictionary, &list->iterator);
843 if (ret != kIOReturnSuccess)
849 altos_list_next(struct altos_list *list, struct altos_device *device)
852 char serial_string[128];
855 object = IOIteratorNext(list->iterator);
859 if (!get_number (object, CFSTR(kUSBVendorID), &device->vendor) ||
860 !get_number (object, CFSTR(kUSBProductID), &device->product))
862 if (device->vendor != 0xfffe)
864 if (device->product < 0x000a || 0x0013 < device->product)
866 if (get_string (object, CFSTR("IOCalloutDevice"), device->path, sizeof (device->path)) &&
867 get_string (object, CFSTR("USB Product Name"), device->name, sizeof (device->name)) &&
868 get_string (object, CFSTR("USB Serial Number"), serial_string, sizeof (serial_string))) {
869 device->serial = atoi(serial_string);
876 altos_list_finish(struct altos_list *list)
878 IOObjectRelease (list->iterator);
882 struct altos_bt_list {
889 #define INQUIRY_MAX_RSP 255
891 struct altos_bt_list *
892 altos_bt_list_start(int inquiry_time)
898 altos_bt_list_next(struct altos_bt_list *bt_list,
899 struct altos_bt_device *device)
905 altos_bt_list_finish(struct altos_bt_list *bt_list)
910 altos_bt_fill_in(char *name, char *addr, struct altos_bt_device *device)
912 strncpy(device->name, name, sizeof (device->name));
913 device->name[sizeof(device->name)-1] = '\0';
914 strncpy(device->addr, addr, sizeof (device->addr));
915 device->addr[sizeof(device->addr)-1] = '\0';
919 altos_bt_open(struct altos_bt_device *device)
931 #include <setupapi.h>
938 #define USB_BUF_SIZE 64
942 unsigned char out_data[USB_BUF_SIZE];
944 unsigned char in_data[USB_BUF_SIZE];
953 _altos_set_last_windows_error(char *file, int line)
955 DWORD error = GetLastError();
957 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
962 sizeof (message) / sizeof (TCHAR),
964 if (error != ERROR_SUCCESS)
965 printf ("%s:%d %s\n", file, line, message);
966 altos_set_last_error(error, message);
969 #define altos_set_last_windows_error() _altos_set_last_windows_error(__FILE__, __LINE__)
971 PUBLIC struct altos_list *
972 altos_list_start(void)
974 struct altos_list *list = calloc(1, sizeof (struct altos_list));
978 list->dev_info = SetupDiGetClassDevs(NULL, "USB", NULL,
979 DIGCF_ALLCLASSES|DIGCF_PRESENT);
980 if (list->dev_info == INVALID_HANDLE_VALUE) {
981 altos_set_last_windows_error();
990 altos_list_next(struct altos_list *list, struct altos_device *device)
992 SP_DEVINFO_DATA dev_info_data;
995 char friendlyname[256];
999 unsigned int vid, pid;
1002 DWORD friendlyname_type;
1003 DWORD friendlyname_len;
1005 dev_info_data.cbSize = sizeof (SP_DEVINFO_DATA);
1006 while(SetupDiEnumDeviceInfo(list->dev_info, list->index,
1011 dev_key = SetupDiOpenDevRegKey(list->dev_info, &dev_info_data,
1012 DICS_FLAG_GLOBAL, 0, DIREG_DEV,
1014 if (dev_key == INVALID_HANDLE_VALUE) {
1015 altos_set_last_windows_error();
1016 printf("cannot open device registry key\n");
1020 /* Fetch symbolic name for this device and parse out
1021 * the vid/pid/serial info */
1022 symbolic_len = sizeof(symbolic);
1023 result = RegQueryValueEx(dev_key, "SymbolicName", NULL, NULL,
1024 symbolic, &symbolic_len);
1026 altos_set_last_windows_error();
1027 printf("cannot find SymbolicName value\n");
1028 RegCloseKey(dev_key);
1031 vid = pid = serial = 0;
1032 sscanf((char *) symbolic + sizeof("\\??\\USB#VID_") - 1,
1034 sscanf((char *) symbolic + sizeof("\\??\\USB#VID_XXXX&PID_") - 1,
1036 sscanf((char *) symbolic + sizeof("\\??\\USB#VID_XXXX&PID_XXXX#") - 1,
1039 /* Fetch the com port name */
1040 port_len = sizeof (port);
1041 result = RegQueryValueEx(dev_key, "PortName", NULL, NULL,
1043 RegCloseKey(dev_key);
1045 altos_set_last_windows_error();
1046 printf("failed to get PortName\n");
1050 /* Fetch the device description which is the device name,
1051 * with firmware that has unique USB ids */
1052 friendlyname_len = sizeof (friendlyname);
1053 if(!SetupDiGetDeviceRegistryProperty(list->dev_info,
1057 (BYTE *)friendlyname,
1058 sizeof(friendlyname),
1061 altos_set_last_windows_error();
1062 printf("Failed to get friendlyname\n");
1065 device->vendor = vid;
1066 device->product = pid;
1067 device->serial = serial;
1068 strcpy(device->name, friendlyname);
1070 strcpy(device->path, (char *) port);
1073 result = GetLastError();
1074 if (result != ERROR_NO_MORE_ITEMS) {
1075 altos_set_last_windows_error();
1076 printf ("SetupDiEnumDeviceInfo failed error %d\n", (int) result);
1082 altos_list_finish(struct altos_list *list)
1084 SetupDiDestroyDeviceInfoList(list->dev_info);
1089 altos_queue_read(struct altos_file *file)
1092 if (file->pend_read)
1093 return LIBALTOS_SUCCESS;
1095 if (!ReadFile(file->handle, file->in_data, USB_BUF_SIZE, &got, &file->ov_read)) {
1096 if (GetLastError() != ERROR_IO_PENDING) {
1097 altos_set_last_windows_error();
1098 return LIBALTOS_ERROR;
1100 file->pend_read = TRUE;
1102 file->pend_read = FALSE;
1104 file->in_used = got;
1106 return LIBALTOS_SUCCESS;
1110 altos_wait_read(struct altos_file *file, int timeout)
1115 if (!file->pend_read)
1116 return LIBALTOS_SUCCESS;
1121 ret = WaitForSingleObject(file->ov_read.hEvent, timeout);
1124 if (!GetOverlappedResult(file->handle, &file->ov_read, &got, FALSE)) {
1125 altos_set_last_windows_error();
1126 return LIBALTOS_ERROR;
1128 file->pend_read = FALSE;
1130 file->in_used = got;
1133 return LIBALTOS_TIMEOUT;
1136 return LIBALTOS_ERROR;
1138 return LIBALTOS_SUCCESS;
1142 altos_fill(struct altos_file *file, int timeout)
1146 if (file->in_read < file->in_used)
1147 return LIBALTOS_SUCCESS;
1149 file->in_read = file->in_used = 0;
1151 ret = altos_queue_read(file);
1154 ret = altos_wait_read(file, timeout);
1158 return LIBALTOS_SUCCESS;
1162 altos_flush(struct altos_file *file)
1165 unsigned char *data = file->out_data;
1166 int used = file->out_used;
1170 if (!WriteFile(file->handle, data, used, &put, &file->ov_write)) {
1171 if (GetLastError() != ERROR_IO_PENDING) {
1172 altos_set_last_windows_error();
1173 printf ("\tflush write error\n");
1174 return LIBALTOS_ERROR;
1176 ret = WaitForSingleObject(file->ov_write.hEvent, INFINITE);
1179 if (!GetOverlappedResult(file->handle, &file->ov_write, &put, FALSE)) {
1180 altos_set_last_windows_error();
1181 printf ("\tflush result error\n");
1182 return LIBALTOS_ERROR;
1186 altos_set_last_windows_error();
1187 printf ("\tflush wait error\n");
1188 return LIBALTOS_ERROR;
1195 return LIBALTOS_SUCCESS;
1198 PUBLIC struct altos_file *
1199 altos_open(struct altos_device *device)
1201 struct altos_file *file = calloc (1, sizeof (struct altos_file));
1203 COMMTIMEOUTS timeouts;
1208 strcpy(full_name, "\\\\.\\");
1209 strcat(full_name, device->path);
1210 file->handle = CreateFile(full_name, GENERIC_READ|GENERIC_WRITE,
1211 0, NULL, OPEN_EXISTING,
1212 FILE_FLAG_OVERLAPPED, NULL);
1213 if (file->handle == INVALID_HANDLE_VALUE) {
1214 altos_set_last_windows_error();
1215 printf ("cannot open %s\n", full_name);
1219 file->ov_read.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1220 file->ov_write.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1222 timeouts.ReadIntervalTimeout = MAXDWORD;
1223 timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
1224 timeouts.ReadTotalTimeoutConstant = 1 << 30; /* almost forever */
1225 timeouts.WriteTotalTimeoutMultiplier = 0;
1226 timeouts.WriteTotalTimeoutConstant = 0;
1227 SetCommTimeouts(file->handle, &timeouts);
1233 altos_close(struct altos_file *file)
1235 if (file->handle != INVALID_HANDLE_VALUE) {
1236 CloseHandle(file->handle);
1237 file->handle = INVALID_HANDLE_VALUE;
1238 SetEvent(file->ov_read.hEvent);
1239 SetEvent(file->ov_write.hEvent);
1240 CloseHandle(file->ov_read.hEvent);
1241 CloseHandle(file->ov_write.hEvent);
1246 altos_free(struct altos_file *file)
1253 altos_putchar(struct altos_file *file, char c)
1257 if (file->out_used == USB_BUF_SIZE) {
1258 ret = altos_flush(file);
1262 file->out_data[file->out_used++] = c;
1263 if (file->out_used == USB_BUF_SIZE)
1264 return altos_flush(file);
1265 return LIBALTOS_SUCCESS;
1269 altos_getchar(struct altos_file *file, int timeout)
1272 while (file->in_read == file->in_used) {
1273 if (file->handle == INVALID_HANDLE_VALUE)
1274 return LIBALTOS_ERROR;
1275 ret = altos_fill(file, timeout);
1279 return file->in_data[file->in_read++];
1282 struct altos_bt_list *
1283 altos_bt_list_start(int inquiry_time)
1289 altos_bt_list_next(struct altos_bt_list *bt_list,
1290 struct altos_bt_device *device)
1296 altos_bt_list_finish(struct altos_bt_list *bt_list)
1302 altos_bt_fill_in(char *name, char *addr, struct altos_bt_device *device)
1304 strncpy(device->name, name, sizeof (device->name));
1305 device->name[sizeof(device->name)-1] = '\0';
1306 strncpy(device->addr, addr, sizeof (device->addr));
1307 device->addr[sizeof(device->addr)-1] = '\0';
1311 altos_bt_open(struct altos_bt_device *device)