Re: Correction: Serial port arbitration
Re: Correction: Serial port arbitration
- Subject: Re: Correction: Serial port arbitration
- From: Godfrey van der Linden <email@hidden>
- Date: Tue, 15 Feb 2005 10:40:29 -0800
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 ;-)
Godfrey
On Feb 15, , at 10:30, Godfrey van der Linden wrote:
Dan you are right, I got the logic inverted. The procedure should
read (Same caveats as last time).
#include <IOKit/serial/ioss.h>
#include <IOKit/serial/IOSerialKeys.h>
// 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>
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
// 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
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden