error handling

This commit is contained in:
2020-09-04 18:26:18 +02:00
parent d4ce143298
commit 275b29e6a4
2 changed files with 19 additions and 17 deletions

View File

@ -227,13 +227,13 @@ void closeSerial(int fd) {
close(fd); close(fd);
} }
t_longframe *request(int fd, uint8_t cmd, uint8_t addr) { uint8_t request(int fd, uint8_t cmd, uint8_t addr, t_longframe **retFrame) {
errno = 0; uint8_t retCode = 0;
t_longframe *frame = (t_longframe*) malloc(sizeof(t_longframe)); t_longframe *frame = (t_longframe*) malloc(sizeof(t_longframe));
if (! frame) { if (! frame) {
errlog("unable to allocate memory for frame\n"); errlog("unable to allocate memory for frame\n");
return NULL; return ERROR_OUT_OF_MEMORY;
} }
frame->userdata = NULL; frame->userdata = NULL;
@ -254,8 +254,9 @@ t_longframe *request(int fd, uint8_t cmd, uint8_t addr) {
if (ioctl(fd, TIOCSERGETLSR, &r) == -1) { if (ioctl(fd, TIOCSERGETLSR, &r) == -1) {
errlog("error %d getting TIOCSERGETLSR for fd %d: %s\n", errlog("error %d getting TIOCSERGETLSR for fd %d: %s\n",
errno, fd, strerror(errno)); errno, fd, strerror(errno));
errno = ERROR_APP_SPECIFIC_ERROR_FLAG | ERROR_TX_REG_UNACCESSIBLE; free(frame);
return NULL; frame = NULL;
return ERROR_TX_REG_UNACCESSIBLE;
} }
if (r & TIOCSER_TEMT) { if (r & TIOCSER_TEMT) {
break; break;
@ -376,9 +377,9 @@ t_longframe *request(int fd, uint8_t cmd, uint8_t addr) {
if ((state == e_ERROR) || (state == e_TIMEOUT)) { if ((state == e_ERROR) || (state == e_TIMEOUT)) {
if (state == e_ERROR) { if (state == e_ERROR) {
errno = ERROR_TX_REG_UNACCESSIBLE | ERROR_STATE_ENGINE; retCode = ERROR_STATE_ENGINE;
} else if (state == e_TIMEOUT) { } else if (state == e_TIMEOUT) {
errno = ERROR_TX_REG_UNACCESSIBLE | ERROR_TIMEOUT; retCode = ERROR_TIMEOUT;
} }
if (frame->userdata) { if (frame->userdata) {
free(frame->userdata); free(frame->userdata);
@ -388,7 +389,8 @@ t_longframe *request(int fd, uint8_t cmd, uint8_t addr) {
frame = NULL; frame = NULL;
} }
return frame; *retFrame = frame;
return retCode;
} }
void printFrame(bool hexOut, t_longframe *frame) { void printFrame(bool hexOut, t_longframe *frame) {
@ -493,14 +495,15 @@ int main(int argc, char *argv[]) {
infolog("sending request %02x %02x\n", cmd, addr); infolog("sending request %02x %02x\n", cmd, addr);
t_longframe *frame = NULL; t_longframe *frame = NULL;
uint8_t requestReturnCode = 0;
if (loopActiveFlag) { if (loopActiveFlag) {
ledRed(false); ledRed(false);
frame = request(fd, cmd, addr); requestReturnCode = request(fd, cmd, addr, &frame);
} else { } else {
errlog("loop is currently inactive, no need to try\n"); errlog("loop is currently inactive, no need to try\n");
} }
if (frame) { if (requestReturnCode == SUCCESS) {
infolog("received a valid frame\n"); infolog("received a valid frame\n");
printFrame(hexOut, frame); printFrame(hexOut, frame);
free(frame->userdata); free(frame->userdata);
@ -510,11 +513,11 @@ int main(int argc, char *argv[]) {
} else { } else {
ledRed(true); ledRed(true);
if (! loopActiveFlag) { if (! loopActiveFlag) {
errno = ERROR_APP_SPECIFIC_ERROR_FLAG | ERROR_LOOP_FAILURE; requestReturnCode = ERROR_LOOP_FAILURE;
} }
errlog("error %04x occured\n", errno); errlog("error %04x occured\n", errno);
if (! hexOut) { if (! hexOut) {
uint8_t maskedError = (uint8_t)(errno & ~ERROR_APP_SPECIFIC_ERROR_FLAG); uint8_t maskedError = requestReturnCode;
fprintf(stdout, "%c%c", maskedError, 0); fprintf(stdout, "%c%c", maskedError, 0);
fflush(stdout); fflush(stdout);
} }

View File

@ -17,13 +17,12 @@
#define DEFAULT_SERIAL_DEVICE "/dev/ttyAMA0" #define DEFAULT_SERIAL_DEVICE "/dev/ttyAMA0"
#define SUCCESS 0
#define ERROR_TIMEOUT 1 #define ERROR_TIMEOUT 1
#define ERROR_STATE_ENGINE 2 #define ERROR_STATE_ENGINE 2
#define ERROR_LOOP_FAILURE 3 #define ERROR_LOOP_FAILURE 3
#define ERROR_TX_REG_UNACCESSIBLE 4 #define ERROR_TX_REG_UNACCESSIBLE 4
#define ERROR_OUT_OF_MEMORY 5
#define ERROR_APP_SPECIFIC_ERROR_FLAG 0x4000
typedef struct { typedef struct {