Re: Correction: Serial port arbitration
site_archiver@lists.apple.com Delivered-To: darwin-dev@lists.apple.com Godfrey On Feb 15, , at 10:30, Godfrey van der Linden wrote: #include <IOKit/serial/ioss.h> #include <IOKit/serial/IOSerialKeys.h> int fd; int preempt; tryAgain: for (;;) { do { fd = open(<tty.dialindevice>, O_NONBLOCK); if (-1 == fd) if (EBUSY == errno) continue; else exit(1); // Fatal error // clear O_NONBLOCK flag if (-1 == fcntl(fd, F_SETFL, 0)) exit(1); // fatal error if (-1 == ioctl(fd, TIOCEXCL, 0)) if (EBUSY == errno) { close(fd); fd = -1; continue; } else exit(1); // Fatal error } while(false); if (-1 != fd) break; // We must have had an EBUSY error to be here so wait for idle if (kIOReturnSuccess != IORegistryEntrySetProperty(portre, CFSTR(kIOTTYWaitForIdleKey), kCFBooleanTrue) exit(1); // Fatal error }; // At this point you have the port exclusively and non-preemtibly // so now is a good time to program up the modem or do any other // equipment initialisation you desire. preempt = 1;` if (-1 == ioctl(fd, IOSSPREEMPT, &preempt)) // Enable preemption exit(1); // Fatal error // Now the port is in the preemptible - blocking state // time to issue a blocking read or a select as needed // waiting for whatever event you are want. if (-1 == read(fd, buf, sizeof(buf)) // Pseudo code if (EBUSY == errno) { close(fd); // We have been preempted. goto tryAgain; } else exit(1); // probably fatal // We have a connection and it is time to say we are no longer // preemptible preempt = 0; if (-1 == ioctl(fd, IOSSPREEMPT, &preempt)) // Disable preemption exit(1); // Fatal error _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-dev mailing list (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/gvdl%40apple.com This email sent to gvdl@apple.com _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-dev mailing list (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/site_archiver%40lists.appl... And another correction, the error returned is not EIO but EBUSY when you are preempted, again see below. (I knew I shouldn't have written the code without testing ;-) Dan you are right, I got the logic inverted. The procedure should read (Same caveats as last time). // iterate over I/O registry to find desired node and keep your // registry entry node (See DTS docs for a write up on how to do this) io_registry_entry_t portre = <do the search> // That's it while we are running non-preemtibly/exclusive further // attempts to open the port on either cu.* or tty.* will fail with EBUSY This email sent to site_archiver@lists.apple.com
participants (1)
-
Godfrey van der Linden