I'm writing a small library to handle comunications with an rs232 device.
Most of the functions write a few bytes in the descriptor, wait for the reply, analise 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'm returning 0 on success, -1 on error. I need to define the values i should set in errno.
-Nack received: I couldn't find any error that suited this case.
-Unexpected answer: EBADMSG.
-Timeout: ETIME or ETIMEDOUT.
-Send failled: EIO ?
What error should i use for the NACK case, and are the error numbers in the other cases suitable? Or should i stay the hell out of errno and find another way for reporting the errors (like diferent return values)?