osx - kevent & USB serial ports -
i'm having trouble using kevent on mac usb serial console. i've narrowed down to:
#include <errno.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <sys/event.h> #include <sys/ioctl.h> #include <termios.h> #define device "/dev/cu.usbserial-0011111d" int main() { int kqueue_fd = kqueue(); if (kqueue_fd < 0) { printf("failed open kqueue\n"); return -1; } int device_fd = open(device, o_rdwr | o_nonblock | o_noctty); if (device_fd < 0) { printf("failed open device: %s\n", device); return -1; } printf("opened %d\n", device_fd); enum { max_events = 1 }; struct kevent events[max_events]; ev_set(&events[0], device_fd, evfilt_read, ev_add, 0, 0, null); int r = kevent(kqueue_fd, events, 1, null, 0, null); if (r < 0) { printf("kevent failed: %s\n", strerror(errno)); return -1; } struct timespec sleep_time; sleep_time.tv_sec = 5; sleep_time.tv_nsec = 0; int ready = kevent(kqueue_fd, null, 0, (struct kevent*) &events, max_events, &sleep_time); if (ready == 0) { printf("no event\n"); return 0; } (int = 0; < ready; i++) { printf(".ident %ld, .filter %d, .flags 0x%x, .fflags 0x%x, " ".data: %ld, .udata %p\n", events[i].ident, events[i].filter, events[i].flags, events[i].fflags, events[i].data, events[i].udata); int unread = 0; r = ioctl(events[i].ident, fionread, &unread); if (r < 0) { printf("ioctl failed: %d: %s\n", errno, strerror(errno)); } } }
when run , unplug usb device in middle of call kevent(), get:
opened 4 .ident 4, .filter -1, .flags 0x1, .fflags 0x0, .data: 6, .udata 0x0 ioctl failed: 6: device not configured
my understanding contents of event translates to:
fd 4, evfilt_read, ev_add, 6 bytes remaining on fd. ioctl() fails (since device removed), , errno 6, seems if event.data returning errno, not bytes remaining.
how can differentiate between normal read case , case device has been removed? filter, flags & fflags appear same in both cases.
additional information
if switch opening serial console pipe, , write single byte followed closing write end, get:
pipe() fd: 5 -> 4 .ident 4, .filter -1, .flags 0x1, .fflags 0x0, .data: 1, .udata 0x0 .ident 4, .filter -1, .flags 0x8001, .fflags 0x0, .data: 0, .udata 0x0
this expect, since 0x8000 ev_eof.
Comments
Post a Comment