Defining errno values ​​for library functions

I am writing a small library to work with rs232 devices.

Most functions write a few bytes in a descriptor, wait for a response, parse it, and return:

int demo_function(int fd)
{
    if (sendcommand(command)==0)
    {
        if (getreply(buffer)==0)
        {
            if (buffer == ACK)
                return 0;           //Success, returns 0
            else if (buffer == NACK)
                return -1;          //Error, Nack received
            else
                return -1;          //Error, unexpected answer
        }
        else
            return -1;              //Error, Timeout
    }
    else
        return -1;                  //Error, send failled.
}

      

I wanted to follow the standard as much as possible, so I return 0 on success, -1 on error. I need to define the values ​​that I have to set to errno.

-Type received: I could not find an error that fits this case.

- Expected response: EBADMSG.

-Timeout: ETIME or ETIMEDOUT.

-Send Verified: EIO?

What error should I use for the NACK case, and the error numbers are appropriate in other cases? Or should I stay black from errno and find another way to report errors (e.g. different return values)?

+1


source to share


2 answers


See the standard for the errno

s list . Personally, I would stay away from errno

and use seperate options, etc.



In your case, the function does not return anything useful so that you can use the return value as an indicator of success (0) or an error type (! 0).

+1


source


You should probably differentiate the return value between expected device responses (like ACK and NACK) and general unexpected system crashes (like timeouts, failed sends, etc.). I would recommend returning 0 on success, 1 on NACK, and -1 + errno on system crashes.



+1


source







All Articles