more error codes
This commit is contained in:
14
src/mbusgw.c
14
src/mbusgw.c
@ -228,7 +228,7 @@ void closeSerial(int fd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint8_t request(int fd, uint8_t cmd, uint8_t addr, t_longframe **retFrame) {
|
uint8_t request(int fd, uint8_t cmd, uint8_t addr, t_longframe **retFrame) {
|
||||||
uint8_t retCode = 0;
|
uint8_t retCode = SUCCESS;
|
||||||
|
|
||||||
t_longframe *frame = (t_longframe*) malloc(sizeof(t_longframe));
|
t_longframe *frame = (t_longframe*) malloc(sizeof(t_longframe));
|
||||||
if (! frame) {
|
if (! frame) {
|
||||||
@ -292,18 +292,21 @@ uint8_t request(int fd, uint8_t cmd, uint8_t addr, t_longframe **retFrame) {
|
|||||||
state = e_LENGTH1;
|
state = e_LENGTH1;
|
||||||
} else {
|
} else {
|
||||||
errlog("invalid start1 symbol %02x\n", c);
|
errlog("invalid start1 symbol %02x\n", c);
|
||||||
|
retCode = ERROR_STATE_ENGINE__START1;
|
||||||
state = e_ERROR;
|
state = e_ERROR;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case e_LENGTH1:
|
case e_LENGTH1:
|
||||||
if (c <= 3) {
|
if (c <= 3) {
|
||||||
errlog("length to small %02x\n", c);
|
errlog("length to small %02x\n", c);
|
||||||
|
retCode = ERROR_STATE_ENGINE__LENGTH1;
|
||||||
state = e_ERROR;
|
state = e_ERROR;
|
||||||
} else {
|
} else {
|
||||||
frame->length1 = c;
|
frame->length1 = c;
|
||||||
frame->userdata = (uint8_t*) malloc(frame->length1 - 3);
|
frame->userdata = (uint8_t*) malloc(frame->length1 - 3);
|
||||||
if (! frame->userdata) {
|
if (! frame->userdata) {
|
||||||
errlog("unable to allocate memory for userdata\n");
|
errlog("unable to allocate memory for userdata\n");
|
||||||
|
retCode = ERROR_OUT_OF_MEMORY__USERDATA;
|
||||||
state = e_ERROR;
|
state = e_ERROR;
|
||||||
}
|
}
|
||||||
state = e_LENGTH2;
|
state = e_LENGTH2;
|
||||||
@ -312,6 +315,7 @@ uint8_t request(int fd, uint8_t cmd, uint8_t addr, t_longframe **retFrame) {
|
|||||||
case e_LENGTH2:
|
case e_LENGTH2:
|
||||||
if (frame->length1 != c) {
|
if (frame->length1 != c) {
|
||||||
errlog("invalid length2 %02x vs. %02x\n", frame->length1, c);
|
errlog("invalid length2 %02x vs. %02x\n", frame->length1, c);
|
||||||
|
retCode = ERROR_STATE_ENGINE__LENGTH2;
|
||||||
state = e_ERROR;
|
state = e_ERROR;
|
||||||
} else {
|
} else {
|
||||||
frame->length2 = c;
|
frame->length2 = c;
|
||||||
@ -324,6 +328,7 @@ uint8_t request(int fd, uint8_t cmd, uint8_t addr, t_longframe **retFrame) {
|
|||||||
state = e_C_FIELD;
|
state = e_C_FIELD;
|
||||||
} else {
|
} else {
|
||||||
errlog("invalid start2 symbol %02x\n", c);
|
errlog("invalid start2 symbol %02x\n", c);
|
||||||
|
retCode = ERROR_STATE_ENGINE__START2;
|
||||||
state = e_ERROR;
|
state = e_ERROR;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -353,6 +358,7 @@ uint8_t request(int fd, uint8_t cmd, uint8_t addr, t_longframe **retFrame) {
|
|||||||
case e_CHKSUM:
|
case e_CHKSUM:
|
||||||
if (c != calculatedChksum) {
|
if (c != calculatedChksum) {
|
||||||
errlog("invalid checksum %02x vs %02x\n", calculatedChksum, c);
|
errlog("invalid checksum %02x vs %02x\n", calculatedChksum, c);
|
||||||
|
retCode = ERROR_STATE_ENGINE__INVALID_CHKSUM;
|
||||||
state = e_ERROR;
|
state = e_ERROR;
|
||||||
} else {
|
} else {
|
||||||
frame->chksum = c;
|
frame->chksum = c;
|
||||||
@ -365,11 +371,13 @@ uint8_t request(int fd, uint8_t cmd, uint8_t addr, t_longframe **retFrame) {
|
|||||||
state = e_DONE;
|
state = e_DONE;
|
||||||
} else {
|
} else {
|
||||||
errlog("invalid stop symbol %02x\n", c);
|
errlog("invalid stop symbol %02x\n", c);
|
||||||
|
retCode = ERROR_STATE_ENGINE__STOP;
|
||||||
state = e_ERROR;
|
state = e_ERROR;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
errlog("illegal state %d\n", state);
|
errlog("illegal state %d\n", state);
|
||||||
|
retCode = ERROR_STATE_ENGINE__ILLEGAL_STATE;
|
||||||
state = e_ERROR;
|
state = e_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -377,7 +385,9 @@ uint8_t request(int fd, uint8_t cmd, uint8_t addr, t_longframe **retFrame) {
|
|||||||
|
|
||||||
if ((state == e_ERROR) || (state == e_TIMEOUT)) {
|
if ((state == e_ERROR) || (state == e_TIMEOUT)) {
|
||||||
if (state == e_ERROR) {
|
if (state == e_ERROR) {
|
||||||
retCode = ERROR_STATE_ENGINE;
|
if (retCode == SUCCESS) {
|
||||||
|
retCode = ERROR_STATE_ENGINE__UNKNOWN;
|
||||||
|
}
|
||||||
} else if (state == e_TIMEOUT) {
|
} else if (state == e_TIMEOUT) {
|
||||||
retCode = ERROR_TIMEOUT;
|
retCode = ERROR_TIMEOUT;
|
||||||
}
|
}
|
||||||
|
16
src/mbusgw.h
16
src/mbusgw.h
@ -19,10 +19,18 @@
|
|||||||
|
|
||||||
#define SUCCESS 0
|
#define SUCCESS 0
|
||||||
#define ERROR_TIMEOUT 1
|
#define ERROR_TIMEOUT 1
|
||||||
#define ERROR_STATE_ENGINE 2
|
#define ERROR_LOOP_FAILURE 2
|
||||||
#define ERROR_LOOP_FAILURE 3
|
#define ERROR_TX_REG_UNACCESSIBLE 3
|
||||||
#define ERROR_TX_REG_UNACCESSIBLE 4
|
#define ERROR_OUT_OF_MEMORY__FRAME 4
|
||||||
#define ERROR_OUT_OF_MEMORY 5
|
#define ERROR_OUT_OF_MEMORY__USERDATA 5
|
||||||
|
#define ERROR_STATE_ENGINE__START1 10
|
||||||
|
#define ERROR_STATE_ENGINE__LENGTH1 11
|
||||||
|
#define ERROR_STATE_ENGINE__LENGTH2 12
|
||||||
|
#define ERROR_STATE_ENGINE__START2 13
|
||||||
|
#define ERROR_STATE_ENGINE__INVALID_CHKSUM 14
|
||||||
|
#define ERROR_STATE_ENGINE__STOP 15
|
||||||
|
#define ERROR_STATE_ENGINE__ILLEGAL_STATE 16
|
||||||
|
#define ERROR_STATE_ENGINE__UNKNOWN 17
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
Reference in New Issue
Block a user