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