syslog
This commit is contained in:
parent
46549cbc9f
commit
dc91b5317a
@ -72,6 +72,7 @@ typedef struct {
|
||||
} t_longframe;
|
||||
|
||||
typedef struct {
|
||||
uint32_t requestId;
|
||||
e_mbusCommState state;
|
||||
uint8_t retryCnt;
|
||||
uint8_t cmd;
|
||||
@ -86,7 +87,7 @@ typedef struct {
|
||||
} t_mbusCommHandle;
|
||||
|
||||
|
||||
static t_mbusCommHandle mbusCommHandle = { .state = MBCS_IDLE, .retryCnt = 0, .cmd = 0, .addr = 0, .receiveCnt = 0, .receivedOctet = 0, .receiving = false };
|
||||
static t_mbusCommHandle mbusCommHandle = { .requestId = 0, .state = MBCS_IDLE, .retryCnt = 0, .cmd = 0, .addr = 0, .receiveCnt = 0, .receivedOctet = 0, .receiving = false };
|
||||
|
||||
static t_mbusCommStats mbusCommStats = { .requestCnt = 0, .errorCnt = 0 };
|
||||
|
||||
@ -100,8 +101,8 @@ t_mbusCommStats *mbusCommGetStats() {
|
||||
return &mbusCommStats;
|
||||
}
|
||||
|
||||
static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) {
|
||||
t_longframe *frame = &(mbusCommHandle->frame);
|
||||
static void parseAndPrintFrame(t_mbusCommHandle *localMbusCommHandle) {
|
||||
t_longframe *frame = &(localMbusCommHandle->frame);
|
||||
|
||||
mbus_frame reply;
|
||||
memset(&reply, 0, sizeof(reply));
|
||||
@ -126,30 +127,30 @@ static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) {
|
||||
int r = mbus_frame_data_parse(&reply, &frame_data);
|
||||
if (r == 0) {
|
||||
mbus_data_variable *data_var = &(frame_data.data_var);
|
||||
coloredMsg(LOG_YELLOW, false, "papf sts: %02x", data_var->header.status);
|
||||
coloredMsg(LOG_YELLOW, false, "papf [%d] sts: %02x", localMbusCommHandle->requestId, data_var->header.status);
|
||||
if ((data_var->header.status & 0x01)) {
|
||||
coloredMsg(LOG_RED, true, "papf sts: Application Busy");
|
||||
coloredMsg(LOG_RED, true, "papf [%d] sts: Application Busy", localMbusCommHandle->requestId);
|
||||
}
|
||||
if ((data_var->header.status & 0x02)) {
|
||||
coloredMsg(LOG_RED, true, "papf sts: Any Application Error");
|
||||
coloredMsg(LOG_RED, true, "papf [%d] sts: Any Application Error", localMbusCommHandle->requestId);
|
||||
}
|
||||
if ((data_var->header.status & 0x04)) {
|
||||
coloredMsg(LOG_RED, true, "papf sts: Power Low");
|
||||
coloredMsg(LOG_RED, true, "papf [%d] sts: Power Low", localMbusCommHandle->requestId);
|
||||
}
|
||||
if ((data_var->header.status & 0x08)) {
|
||||
coloredMsg(LOG_RED, true, "papf sts: Permanent Error");
|
||||
coloredMsg(LOG_RED, true, "papf [%d] sts: Permanent Error", localMbusCommHandle->requestId);
|
||||
}
|
||||
if ((data_var->header.status & 0x10)) {
|
||||
coloredMsg(LOG_RED, true, "papf sts: Temporary Error");
|
||||
coloredMsg(LOG_RED, true, "papf [%d] sts: Temporary Error", localMbusCommHandle->requestId);
|
||||
}
|
||||
if ((data_var->header.status & 0x20)) {
|
||||
coloredMsg(LOG_RED, true, "papf sts: Specific to manufacturer Error 1");
|
||||
coloredMsg(LOG_RED, true, "papf [%d] sts: Specific to manufacturer Error 1", localMbusCommHandle->requestId);
|
||||
}
|
||||
if ((data_var->header.status & 0x40)) {
|
||||
coloredMsg(LOG_RED, true, "papf sts: Specific to manufacturer Error 2");
|
||||
coloredMsg(LOG_RED, true, "papf [%d] sts: Specific to manufacturer Error 2", localMbusCommHandle->requestId);
|
||||
}
|
||||
if ((data_var->header.status & 0x80)) {
|
||||
coloredMsg(LOG_RED, true, "papf sts: Specific to manufacturer Error 3");
|
||||
coloredMsg(LOG_RED, true, "papf [%d] sts: Specific to manufacturer Error 3", localMbusCommHandle->requestId);
|
||||
}
|
||||
mbus_data_record *record;
|
||||
int i;
|
||||
@ -157,11 +158,12 @@ static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) {
|
||||
record;
|
||||
record = record->next, i++) {
|
||||
for (uint8_t j = 0; j < MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS; j++) {
|
||||
if ((mbusCommHandle->device->consideredField[j].index == i) &&
|
||||
(strlen(mbusCommHandle->device->consideredField[j].label) > 0)) {
|
||||
coloredMsg(LOG_YELLOW, true, "papf txt: I:%d, L:%s, U:%s V:%s",
|
||||
if ((localMbusCommHandle->device->consideredField[j].index == i) &&
|
||||
(strlen(localMbusCommHandle->device->consideredField[j].label) > 0)) {
|
||||
coloredMsg(LOG_YELLOW, true, "papf [%d] txt: I:%d, L:%s, U:%s V:%s",
|
||||
localMbusCommHandle->requestId,
|
||||
i,
|
||||
mbusCommHandle->device->consideredField[j].label,
|
||||
localMbusCommHandle->device->consideredField[j].label,
|
||||
mbus_data_record_unit(record),
|
||||
mbus_data_record_value(record));
|
||||
}
|
||||
@ -169,15 +171,15 @@ static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) {
|
||||
}
|
||||
mbus_data_record_free(data_var->record);
|
||||
} else {
|
||||
coloredMsg(LOG_RED, true, "papf err: unable to parse frame");
|
||||
coloredMsg(LOG_RED, true, "papf [%d] err: unable to parse frame", localMbusCommHandle->requestId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void handleRequestEngine(void *handle);
|
||||
static void timeoutHandler(void *handle) {
|
||||
coloredMsg(LOG_RED, true, "mbc timeout");
|
||||
t_mbusCommHandle *localMbusCommHandle = (t_mbusCommHandle*) handle;
|
||||
coloredMsg(LOG_RED, true, "mbcth [%d] timeout", localMbusCommHandle->requestId);
|
||||
localMbusCommHandle->state = MBCS_TIMEOUT;
|
||||
localMbusCommHandle->receiving = false;
|
||||
handleRequestEngine(handle);
|
||||
@ -196,11 +198,11 @@ static void handleRequestEngine(void *handle) {
|
||||
|
||||
switch (localMbusCommHandle->state) {
|
||||
case MBCS_IDLE:
|
||||
coloredMsg(LOG_YELLOW, false, "hre state IDLE");
|
||||
coloredMsg(LOG_YELLOW, false, "hre [%d] state IDLE", localMbusCommHandle->requestId);
|
||||
break;
|
||||
|
||||
case MBCS_SEND:
|
||||
coloredMsg(LOG_YELLOW, false, "hre state SEND");
|
||||
coloredMsg(LOG_YELLOW, false, "hre [%d] state SEND", localMbusCommHandle->requestId);
|
||||
localMbusCommHandle->sendBuf[0] = 0x10;
|
||||
localMbusCommHandle->sendBuf[1] = localMbusCommHandle->cmd;
|
||||
localMbusCommHandle->sendBuf[2] = localMbusCommHandle->addr;
|
||||
@ -210,10 +212,10 @@ static void handleRequestEngine(void *handle) {
|
||||
// no break !!
|
||||
|
||||
case MBCS_SEND_CONTINUED:
|
||||
coloredMsg(LOG_YELLOW, false, "hre state SEND_CONTINUED");
|
||||
coloredMsg(LOG_YELLOW, false, "hre [%d] state SEND_CONTINUED", localMbusCommHandle->requestId);
|
||||
show(LED_RED, OFF);
|
||||
if (! loopActive) {
|
||||
coloredMsg(LOG_YELLOW, true, "hre enabling loop, try %d", localMbusCommHandle->retryCnt);
|
||||
coloredMsg(LOG_YELLOW, true, "hre [%d] enabling loop, try %d", localMbusCommHandle->requestId, localMbusCommHandle->retryCnt);
|
||||
localMbusCommHandle->retryCnt++;
|
||||
loopEnable();
|
||||
schAdd(handleRequestEngine, handle, 100, 0); // give 100ms to settled the loop
|
||||
@ -227,13 +229,13 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_SENDING_DONE:
|
||||
coloredMsg(LOG_YELLOW, false, "hre state SENDING_DONE");
|
||||
coloredMsg(LOG_YELLOW, false, "hre [%d] state SENDING_DONE", localMbusCommHandle->requestId);
|
||||
localMbusCommHandle->state = MBCS_ENABLE_FRONTEND;
|
||||
schAdd(handleRequestEngine, handle, 3, 0);
|
||||
break;
|
||||
|
||||
case MBCS_ENABLE_FRONTEND:
|
||||
coloredMsg(LOG_YELLOW, false, "hre state ENABLE_FRONTEND");
|
||||
coloredMsg(LOG_YELLOW, false, "hre [%d] state ENABLE_FRONTEND", localMbusCommHandle->requestId);
|
||||
frontendEnable();
|
||||
schAdd(timeoutHandler, handle, 2500, 0);
|
||||
calculatedChksum = 0;
|
||||
@ -247,7 +249,7 @@ static void handleRequestEngine(void *handle) {
|
||||
localMbusCommHandle->frame.start1 = localMbusCommHandle->receivedOctet;
|
||||
localMbusCommHandle->state = MBCS_LENGTH1;
|
||||
} else {
|
||||
coloredMsg(LOG_RED, true, "hre err: invalid start1 symbol %02x", localMbusCommHandle->receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "hre [%d] err: invalid start1 symbol %02x", localMbusCommHandle->requestId, localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__START1;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
}
|
||||
@ -256,14 +258,14 @@ static void handleRequestEngine(void *handle) {
|
||||
|
||||
case MBCS_LENGTH1:
|
||||
if (localMbusCommHandle->receivedOctet <= 3) {
|
||||
coloredMsg(LOG_RED, true, "hre err: length to small %02x", localMbusCommHandle->receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "hre [%d] err: length to small %02x", localMbusCommHandle->requestId, localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__LENGTH1;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
} else {
|
||||
localMbusCommHandle->frame.length1 = localMbusCommHandle->receivedOctet;
|
||||
localMbusCommHandle->frame.userdata = (uint8_t*) malloc(localMbusCommHandle->frame.length1 - 3);
|
||||
if (! localMbusCommHandle->frame.userdata) {
|
||||
coloredMsg(LOG_RED, true, "hre err: unable to allocate memory for userdata");
|
||||
coloredMsg(LOG_RED, true, "hre [%d] err: unable to allocate memory for userdata", localMbusCommHandle->requestId);
|
||||
localMbusCommHandle->result = MBCR_ERROR_OUT_OF_MEMORY__USERDATA;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
} else {
|
||||
@ -275,7 +277,8 @@ static void handleRequestEngine(void *handle) {
|
||||
|
||||
case MBCS_LENGTH2:
|
||||
if (localMbusCommHandle->frame.length1 != localMbusCommHandle->receivedOctet) {
|
||||
coloredMsg(LOG_RED, true, "hre err: invalid length2 %02x vs. %02x", localMbusCommHandle->frame.length1, localMbusCommHandle->receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "hre [%d] err: invalid length2 %02x vs. %02x",
|
||||
localMbusCommHandle->requestId, localMbusCommHandle->frame.length1, localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__LENGTH2;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
} else {
|
||||
@ -290,7 +293,8 @@ static void handleRequestEngine(void *handle) {
|
||||
localMbusCommHandle->frame.start2 = localMbusCommHandle->receivedOctet;
|
||||
localMbusCommHandle->state = MBCS_C_FIELD;
|
||||
} else {
|
||||
coloredMsg(LOG_RED, true, "hre err: invalid start2 symbol %02x", localMbusCommHandle->receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "hre [%d] err: invalid start2 symbol %02x",
|
||||
localMbusCommHandle->requestId, localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__START2;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
}
|
||||
@ -330,7 +334,8 @@ static void handleRequestEngine(void *handle) {
|
||||
|
||||
case MBCS_CHKSUM:
|
||||
if (localMbusCommHandle->receivedOctet != calculatedChksum) {
|
||||
coloredMsg(LOG_RED, true, "hre err: invalid checksum %02x vs %02x", calculatedChksum, localMbusCommHandle->receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "hre [%d] err: invalid checksum %02x vs %02x",
|
||||
localMbusCommHandle->requestId, calculatedChksum, localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__INVALID_CHKSUM;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
} else {
|
||||
@ -346,7 +351,8 @@ static void handleRequestEngine(void *handle) {
|
||||
localMbusCommHandle->state = MBCS_DONE;
|
||||
schAdd(handleRequestEngine, handle, 0, 0);
|
||||
} else {
|
||||
coloredMsg(LOG_RED, true, "hre err: invalid stop symbol %02x", localMbusCommHandle->receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "hre [%d] err: invalid stop symbol %02x",
|
||||
localMbusCommHandle->requestId, localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__STOP;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
receiveNext(localMbusCommHandle);
|
||||
@ -354,7 +360,7 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_DONE:
|
||||
coloredMsg(LOG_YELLOW, false, "hre state DONE");
|
||||
coloredMsg(LOG_YELLOW, false, "hre [%d] state DONE", localMbusCommHandle->requestId);
|
||||
parseAndPrintFrame(localMbusCommHandle);
|
||||
if (localMbusCommHandle->frame.userdata != NULL) {
|
||||
free(localMbusCommHandle->frame.userdata);
|
||||
@ -367,14 +373,15 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_ERROR:
|
||||
coloredMsg(LOG_RED, true, "hre state ERROR");
|
||||
coloredMsg(LOG_RED, true, "hre [%d] state ERROR", localMbusCommHandle->requestId);
|
||||
show(LED_RED, ON);
|
||||
coloredMsg(LOG_RED, true, "hre err: already error, read the rest (now: %02x) until timeout", localMbusCommHandle->receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "hre [%d] err: already error, read the rest (now: %02x) until timeout",
|
||||
localMbusCommHandle->requestId, localMbusCommHandle->receivedOctet);
|
||||
receiveNext(localMbusCommHandle);
|
||||
break;
|
||||
|
||||
case MBCS_TIMEOUT:
|
||||
coloredMsg(LOG_RED, true, "hre state TIMEOUT");
|
||||
coloredMsg(LOG_RED, true, "hre [%d] state TIMEOUT", localMbusCommHandle->requestId);
|
||||
mbusCommStats.errorCnt += 1;
|
||||
localMbusCommHandle->device->failures += 1;
|
||||
localMbusCommHandle->receiving = false;
|
||||
@ -386,11 +393,11 @@ static void handleRequestEngine(void *handle) {
|
||||
uint8_t kitchenSink[16];
|
||||
memset(kitchenSink, 0, 16);
|
||||
HAL_StatusTypeDef r = HAL_UART_Receive(&mbusUart, kitchenSink, 16, 100);
|
||||
coloredMsg(LOG_RED, true, "hre abort, last receive result: %02x", r);
|
||||
coloredMsg(LOG_RED, true, "hre [%d] abort, last receive result: %02x", localMbusCommHandle->requestId, r);
|
||||
// no break
|
||||
|
||||
case MBCS_DISABLE_FRONTEND:
|
||||
coloredMsg(LOG_YELLOW, false, "hre state DISABLE_FRONTEND");
|
||||
coloredMsg(LOG_YELLOW, false, "hre [%d] state DISABLE_FRONTEND", localMbusCommHandle->requestId);
|
||||
frontendDisable();
|
||||
localMbusCommHandle->state = MBCS_IDLE;
|
||||
break;
|
||||
@ -411,7 +418,8 @@ void mbusCommRxCpltCallback(UART_HandleTypeDef *huart) {
|
||||
schAdd(handleRequestEngine, (void*) &mbusCommHandle, 0, 0);
|
||||
mbusCommHandle.receiving = false;
|
||||
} else {
|
||||
coloredMsg(LOG_RED, true, "mcrx: received 0x%02x but not expected", mbusCommHandle.receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "mcrx [%d] received 0x%02x but not expected",
|
||||
mbusCommHandle.requestId, mbusCommHandle.receivedOctet);
|
||||
}
|
||||
}
|
||||
|
||||
@ -424,19 +432,22 @@ e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice) {
|
||||
|
||||
if (mbusCommEnabled) {
|
||||
if (mbusCommHandle.state == MBCS_IDLE) {
|
||||
coloredMsg(LOG_YELLOW, true, "*** NEW REQUEST %s R:%d F:%d GRC:%d GEC:%d ***",
|
||||
mbusDevice->deviceName,
|
||||
mbusDevice->requests,
|
||||
mbusDevice->failures,
|
||||
mbusCommStats.requestCnt,
|
||||
mbusCommStats.errorCnt);
|
||||
|
||||
mbusCommHandle.requestId += 1;
|
||||
mbusCommHandle.state = MBCS_SEND;
|
||||
mbusCommHandle.retryCnt = 0;
|
||||
mbusCommHandle.cmd = MBUS_QUERY_CMD;
|
||||
mbusCommHandle.addr = mbusDevice->address;
|
||||
mbusCommHandle.device = mbusDevice;
|
||||
mbusDevice->requests += 1;
|
||||
|
||||
coloredMsg(LOG_YELLOW, true, "mcr [%d] new request %s R:%d F:%d GRC:%d GEC:%d",
|
||||
mbusCommHandle.requestId,
|
||||
mbusDevice->deviceName,
|
||||
mbusDevice->requests,
|
||||
mbusDevice->failures,
|
||||
mbusCommStats.requestCnt,
|
||||
mbusCommStats.errorCnt);
|
||||
|
||||
schAdd(handleRequestEngine, (void*) &mbusCommHandle, 0, 0);
|
||||
res = MBCRR_TRIGGERED;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user