Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Correction: Serial port arbitration
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Correction: Serial port arbitration



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
References: 
 >Re: Serial port arbitration (UUCP device locking protocol?) (From: Mike Smith <email@hidden>)
 >Re: Serial port arbitration (UUCP device locking protocol?) (From: Dan Bernstein <email@hidden>)
 >Re: Serial port arbitration (UUCP device locking protocol?) (From: Godfrey van der Linden <email@hidden>)
 >Re: Serial port arbitration (UUCP device locking protocol?) (From: Dan Bernstein <email@hidden>)
 >Re: Serial port arbitration (UUCP device locking protocol?) (From: Godfrey van der Linden <email@hidden>)
 >Re: Serial port arbitration (UUCP device locking protocol?) (From: Dan Bernstein <email@hidden>)
 >Correction: Serial port arbitration (From: Godfrey van der Linden <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.