syslog
This commit is contained in:
parent
ccf6982b62
commit
23568db1b5
@ -3,8 +3,10 @@
|
||||
|
||||
#include <main.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef enum {
|
||||
LOG_NORMAL,
|
||||
LOG_HIGH,
|
||||
LOG_RED,
|
||||
LOG_GREEN,
|
||||
@ -22,10 +24,7 @@ void logFree();
|
||||
// return value can be ignored, it is only used in test
|
||||
int logMsg(const char *format, ...);
|
||||
|
||||
// in red
|
||||
int errMsg(const char *format, ...);
|
||||
|
||||
int coloredMsg(const t_logColor color, const char *format, ...);
|
||||
int coloredMsg(const t_logColor color, bool syslogToo, const char *format, ...);
|
||||
|
||||
void debugTxCpltCallback(UART_HandleTypeDef *huart);
|
||||
|
||||
|
@ -148,7 +148,7 @@ int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetConfigMode) {
|
||||
configMode = false;
|
||||
}
|
||||
|
||||
coloredMsg(LOG_YELLOW, "cec, cmdLine is %s", cmdLine);;
|
||||
coloredMsg(LOG_YELLOW, false, "cec, cmdLine is %s", cmdLine);;
|
||||
|
||||
#define MAX_NUM_OF_ARGS 8
|
||||
char *args[MAX_NUM_OF_TASKS];
|
||||
@ -164,7 +164,7 @@ int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetConfigMode) {
|
||||
char *cmd = args[0];
|
||||
|
||||
int8_t retCode = 0;
|
||||
coloredMsg(LOG_YELLOW, "cec, cmd is %s, number of arguments %d", cmd, argc);
|
||||
coloredMsg(LOG_YELLOW, false, "cec, cmd is %s, number of arguments %d", cmd, argc);
|
||||
|
||||
if (0 == strcmp(cmd, "quit")) {
|
||||
messageToSend = GOODBYE_MSG;
|
||||
@ -182,11 +182,11 @@ int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetConfigMode) {
|
||||
}
|
||||
messageToSend = NULL;
|
||||
} else if (0 == strcmp(cmd, "enable")) {
|
||||
coloredMsg(LOG_YELLOW, "cec, enable config mode");
|
||||
coloredMsg(LOG_YELLOW, true, "cec, enable config mode");
|
||||
configMode = true;
|
||||
retCode = 1;
|
||||
} else if (0 == strcmp(cmd, "disable")) {
|
||||
coloredMsg(LOG_YELLOW, "cec, disable config mode");
|
||||
coloredMsg(LOG_YELLOW, true, "cec, disable config mode");
|
||||
configMode = false;
|
||||
retCode = 2;
|
||||
} else {
|
||||
@ -241,13 +241,13 @@ void cmdHandlerEngine(void *handle) {
|
||||
if (isNetworkAvailable()) {
|
||||
switch (state) {
|
||||
case CH_INIT:
|
||||
coloredMsg(LOG_YELLOW, "che, initializing socket");
|
||||
coloredMsg(LOG_YELLOW, false, "che, initializing socket");
|
||||
|
||||
res = socket(CMD_SOCK, Sn_MR_TCP, cmdPort, SF_IO_NONBLOCK);
|
||||
coloredMsg(LOG_YELLOW, "che, socket returns %d", res);
|
||||
coloredMsg(LOG_YELLOW, false, "che, socket returns %d", res);
|
||||
|
||||
if (res == CMD_SOCK) {
|
||||
coloredMsg(LOG_YELLOW, "che, socket is initialized");
|
||||
coloredMsg(LOG_YELLOW, false, "che, socket is initialized");
|
||||
state = CH_LISTEN;
|
||||
} else {
|
||||
state = CH_ERROR;
|
||||
@ -255,13 +255,13 @@ void cmdHandlerEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case CH_LISTEN:
|
||||
coloredMsg(LOG_YELLOW, "che, listening");
|
||||
coloredMsg(LOG_YELLOW, false, "che, listening");
|
||||
|
||||
res = listen(CMD_SOCK);
|
||||
coloredMsg(LOG_YELLOW, "che, listen returns %d", res);
|
||||
coloredMsg(LOG_YELLOW, false, "che, listen returns %d", res);
|
||||
|
||||
if (res == SOCK_OK) {
|
||||
coloredMsg(LOG_YELLOW, "che, ok, waiting for established");
|
||||
coloredMsg(LOG_YELLOW, false, "che, ok, waiting for established");
|
||||
state = CH_WAITING;
|
||||
} else {
|
||||
state = CH_ERROR;
|
||||
@ -269,28 +269,26 @@ void cmdHandlerEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case CH_WAITING:
|
||||
//coloredMsg(LOG_YELLOW, "che, waiting for established");
|
||||
|
||||
sockState = getSn_SR(CMD_SOCK);
|
||||
if (sockState != SOCK_LISTEN) {
|
||||
coloredMsg(LOG_YELLOW, "che, socket state is 0x%02x", sockState);
|
||||
coloredMsg(LOG_YELLOW, false, "che, socket state is 0x%02x", sockState);
|
||||
}
|
||||
|
||||
if (sockState == SOCK_ESTABLISHED) {
|
||||
coloredMsg(LOG_YELLOW, "che, connection is established");
|
||||
coloredMsg(LOG_YELLOW, true, "che, connection is established");
|
||||
state = CH_BANNER;
|
||||
}
|
||||
break;
|
||||
|
||||
case CH_BANNER:
|
||||
coloredMsg(LOG_YELLOW, "che, send banner");
|
||||
coloredMsg(LOG_YELLOW, false, "che, send banner");
|
||||
sockState = getSn_SR(CMD_SOCK);
|
||||
if (sockState != SOCK_ESTABLISHED) {
|
||||
coloredMsg(LOG_YELLOW, "che sockState is 0x%02x when trying to send banner", sockState);
|
||||
coloredMsg(LOG_YELLOW, true, "che sockState is 0x%02x when trying to send banner", sockState);
|
||||
state = CH_DISCONNECT;
|
||||
} else {
|
||||
resultSend = send(CMD_SOCK, banner, strlen(banner));
|
||||
coloredMsg(LOG_YELLOW, "che, sent banner, send returns 0x%02x", resultSend);
|
||||
coloredMsg(LOG_YELLOW, false, "che, sent banner, send returns 0x%02x", resultSend);
|
||||
prompt = defaultPrompt;
|
||||
resetConfigMode = true;
|
||||
state = CH_PROMPT;
|
||||
@ -298,14 +296,14 @@ void cmdHandlerEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case CH_PROMPT:
|
||||
coloredMsg(LOG_YELLOW, "che send prompt");
|
||||
coloredMsg(LOG_YELLOW, false, "che send prompt");
|
||||
sockState = getSn_SR(CMD_SOCK);
|
||||
if (sockState != SOCK_ESTABLISHED) {
|
||||
coloredMsg(LOG_YELLOW, "che sockState is 0x%02x when trying to send promt", sockState);
|
||||
coloredMsg(LOG_YELLOW, true, "che sockState is 0x%02x when trying to send promt", sockState);
|
||||
state = CH_DISCONNECT;
|
||||
} else {
|
||||
resultSend = send(CMD_SOCK, prompt, strlen(prompt));
|
||||
coloredMsg(LOG_YELLOW, "che, sent prompt %s, send returns 0x%02x", prompt, resultSend);
|
||||
coloredMsg(LOG_YELLOW, false, "che, sent prompt %s, send returns 0x%02x", prompt, resultSend);
|
||||
state = CH_RECEIVE;
|
||||
}
|
||||
break;
|
||||
@ -313,17 +311,15 @@ void cmdHandlerEngine(void *handle) {
|
||||
case CH_RECEIVE:
|
||||
sockState = getSn_SR(CMD_SOCK);
|
||||
if (sockState != SOCK_ESTABLISHED) {
|
||||
coloredMsg(LOG_YELLOW, "che sockState is 0x%02x when trying to receive something", sockState);
|
||||
coloredMsg(LOG_YELLOW, true, "che sockState is 0x%02x when trying to receive something", sockState);
|
||||
state = CH_DISCONNECT;
|
||||
} else {
|
||||
// coloredMsg(LOG_YELLOW, "che, now waiting for some input");
|
||||
receivedOctets = getSn_RX_RSR(CMD_SOCK);
|
||||
// coloredMsg(LOG_YELLOW, "che, getSn_RxMAX returns %d", res16);
|
||||
|
||||
if (receivedOctets > 0) {
|
||||
memset(receiveBuffer, 0, sizeof(receiveBuffer));
|
||||
resultRecv = recv(CMD_SOCK, receiveBuffer, sizeof(receiveBuffer));
|
||||
coloredMsg(LOG_YELLOW, "che, recv returns 0x%02x", resultRecv);
|
||||
coloredMsg(LOG_YELLOW, false, "che, recv returns 0x%02x", resultRecv);
|
||||
if (resultRecv > 0) {
|
||||
if ((receiveBuffer[strlen(receiveBuffer) - 1] == 0x0a) ||
|
||||
(receiveBuffer[strlen(receiveBuffer) - 1] == 0x0d)) {
|
||||
@ -333,7 +329,7 @@ void cmdHandlerEngine(void *handle) {
|
||||
(receiveBuffer[strlen(receiveBuffer) - 1] == 0x0d)) {
|
||||
receiveBuffer[strlen(receiveBuffer) - 1] = 0;
|
||||
}
|
||||
coloredMsg(LOG_YELLOW, "che, received: %s", receiveBuffer);
|
||||
coloredMsg(LOG_YELLOW, false, "che, received: %s", receiveBuffer);
|
||||
int8_t resCEC = cmdExecuteCommand(receiveBuffer, resetConfigMode);
|
||||
resetConfigMode = false;
|
||||
switch (resCEC) {
|
||||
@ -358,25 +354,25 @@ void cmdHandlerEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case CH_DISCONNECT:
|
||||
coloredMsg(LOG_YELLOW, "che, close our end");
|
||||
coloredMsg(LOG_YELLOW, true, "che, close our end");
|
||||
resultDisconnect = disconnect(CMD_SOCK);
|
||||
coloredMsg(LOG_YELLOW, "che, disconnect returns 0x%02x", resultDisconnect);
|
||||
coloredMsg(LOG_YELLOW, true, "che, disconnect returns 0x%02x", resultDisconnect);
|
||||
state = CH_DISCONNECT_WAIT;
|
||||
break;
|
||||
|
||||
case CH_DISCONNECT_WAIT:
|
||||
coloredMsg(LOG_YELLOW, "che, waiting after disconnect");
|
||||
coloredMsg(LOG_YELLOW, false, "che, waiting after disconnect");
|
||||
sockState = getSn_SR(CMD_SOCK);
|
||||
coloredMsg(LOG_YELLOW, "che, sockState is 0x%02x", sockState);
|
||||
coloredMsg(LOG_YELLOW, false, "che, sockState is 0x%02x", sockState);
|
||||
if (sockState == SOCK_CLOSED) {
|
||||
coloredMsg(LOG_YELLOW, "che, socket is closed now");
|
||||
coloredMsg(LOG_YELLOW, true, "che, socket is closed now");
|
||||
state = CH_INIT;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case CH_ERROR:
|
||||
coloredMsg(LOG_YELLOW, "che, error state, will stop here");
|
||||
coloredMsg(LOG_YELLOW, true, "che, error state, will stop here");
|
||||
schDel(cmdHandlerEngine, NULL);
|
||||
break;
|
||||
}
|
||||
|
@ -136,16 +136,16 @@ void eepromInit() {
|
||||
|
||||
logMsg("eeI, read header");
|
||||
eepromRead(EEPROM_HEADER_ADDR, eepromHeader.b, sizeof(eepromHeader));
|
||||
coloredMsg(LOG_RED, "eeI, magic: %08x", eepromHeader.s.magic);
|
||||
logMsg("eeI, magic: %08x", eepromHeader.s.magic);
|
||||
|
||||
if (eepromHeader.s.magic != EEPROM_MAGIC) {
|
||||
coloredMsg(LOG_RED, "eeI, eeprom is uninitialized");
|
||||
logMsg("eeI, eeprom is uninitialized");
|
||||
|
||||
deviceStats.s.totalPowercycles = 0;
|
||||
deviceStats.s.totalRunningHours = 0;
|
||||
deviceStats.s.totalRequests = 0;
|
||||
deviceStats.s.totalFailures = 0;
|
||||
coloredMsg(LOG_RED, "eeI, about to write device stats for the first time");
|
||||
logMsg("eeI, about to write device stats for the first time");
|
||||
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
|
||||
eepromActiveDelay(7);
|
||||
|
||||
@ -158,35 +158,35 @@ void eepromInit() {
|
||||
eepromActiveDelay(7);
|
||||
}
|
||||
}
|
||||
coloredMsg(LOG_RED, "eeI, storage blocks initialized");
|
||||
logMsg("eeI, storage blocks initialized");
|
||||
|
||||
|
||||
eepromHeader.s.magic = EEPROM_MAGIC;
|
||||
eepromHeader.s.activeBlock = 0;
|
||||
eepromHeader.s.writeCounter = 1;
|
||||
coloredMsg(LOG_RED, "eeI, about to write header for the first time");
|
||||
logMsg("eeI, about to write header for the first time");
|
||||
eepromWrite(EEPROM_HEADER_ADDR, eepromHeader.b, sizeof(eepromHeader));
|
||||
eepromActiveDelay(7);
|
||||
coloredMsg(LOG_GREEN, "eeI, eeprom has been initialized");
|
||||
logMsg("eeI, eeprom has been initialized");
|
||||
} else {
|
||||
coloredMsg(LOG_GREEN, "eeI, eeprom is initialized");
|
||||
logMsg("eeI, eeprom is initialized");
|
||||
}
|
||||
|
||||
coloredMsg(LOG_GREEN, "eeI, about to read device stats");
|
||||
logMsg("eeI, about to read device stats");
|
||||
eepromRead(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
|
||||
coloredMsg(LOG_GREEN, "eeI, total powercycles so far: %d", deviceStats.s.totalPowercycles);
|
||||
coloredMsg(LOG_GREEN, "eeI, total running hours so far: %d", deviceStats.s.totalRunningHours);
|
||||
coloredMsg(LOG_GREEN, "eeI, total requests so far: %d", deviceStats.s.totalRequests);
|
||||
coloredMsg(LOG_GREEN, "eeI, total failures so far: %d", deviceStats.s.totalFailures);
|
||||
logMsg("eeI, total powercycles so far: %d", deviceStats.s.totalPowercycles);
|
||||
logMsg("eeI, total running hours so far: %d", deviceStats.s.totalRunningHours);
|
||||
logMsg("eeI, total requests so far: %d", deviceStats.s.totalRequests);
|
||||
logMsg("eeI, total failures so far: %d", deviceStats.s.totalFailures);
|
||||
|
||||
t_mbusCommStats stats = { .requestCnt = deviceStats.s.totalRequests, .errorCnt = deviceStats.s.totalFailures};
|
||||
mbusCommSetStats(stats);
|
||||
|
||||
deviceStats.s.totalPowercycles += 1;
|
||||
coloredMsg(LOG_GREEN, "eeI, about to write device stats with updated power cycles counter");
|
||||
logMsg("eeI, about to write device stats with updated power cycles counter");
|
||||
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
|
||||
eepromActiveDelay(7);
|
||||
|
||||
schAdd(eepromHourlyUpdateDeviceStats, NULL, 0, 60 * 60 * 1000);
|
||||
coloredMsg(LOG_GREEN, "eeI, hourly device stats update scheduled");
|
||||
logMsg("eeI, hourly device stats update scheduled");
|
||||
}
|
||||
|
@ -118,15 +118,7 @@ int logMsg(const char *format, ...) {
|
||||
return res;
|
||||
}
|
||||
|
||||
int errMsg(const char *format, ...) {
|
||||
va_list vl;
|
||||
va_start(vl, format);
|
||||
int res = innerLogMsg("\x1b[31;1m", "\x1b[0m\r\n", true, format, vl);
|
||||
va_end(vl);
|
||||
return res;
|
||||
}
|
||||
|
||||
int coloredMsg(const t_logColor color, const char *format, ...) {
|
||||
int coloredMsg(const t_logColor color, bool syslogToo, const char *format, ...) {
|
||||
const static char POST[] = "\x1b[0m\r\n";
|
||||
const static char HIGH[] = "\x1b[1m";
|
||||
const static char RED[] = "\x1b[31;1m";
|
||||
@ -153,7 +145,7 @@ int coloredMsg(const t_logColor color, const char *format, ...) {
|
||||
}
|
||||
va_list vl;
|
||||
va_start(vl, format);
|
||||
int res = innerLogMsg(pre, POST, false, format, vl);
|
||||
int res = innerLogMsg(pre, POST, syslogToo, format, vl);
|
||||
va_end(vl);
|
||||
return res;
|
||||
}
|
||||
|
@ -179,7 +179,7 @@ void scheduleMBusRequest(void *handle) {
|
||||
if (devices[i].delay <= 0) {
|
||||
devices[i].delay = devices[i].period;
|
||||
devices[i].waiting = true;
|
||||
coloredMsg(LOG_GREEN, "*** Scheduled: %s", devices[i].deviceName);
|
||||
coloredMsg(LOG_GREEN, false, "*** Scheduled: %s", devices[i].deviceName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -187,7 +187,7 @@ void scheduleMBusRequest(void *handle) {
|
||||
void my_setup_2() {
|
||||
show(LED_RED, OFF);
|
||||
show(LED_GREEN, ON);
|
||||
coloredMsg(LOG_BLUE, "Application starting");
|
||||
logMsg("Application starting");
|
||||
|
||||
eepromInit();
|
||||
|
||||
|
@ -126,30 +126,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);
|
||||
logMsg("papf sts: %02x", data_var->header.status);
|
||||
coloredMsg(LOG_YELLOW, false, "papf sts: %02x", data_var->header.status);
|
||||
if ((data_var->header.status & 0x01)) {
|
||||
errMsg("papf sts: Application Busy");
|
||||
coloredMsg(LOG_RED, true, "papf sts: Application Busy");
|
||||
}
|
||||
if ((data_var->header.status & 0x02)) {
|
||||
errMsg("papf sts: Any Application Error");
|
||||
coloredMsg(LOG_RED, true, "papf sts: Any Application Error");
|
||||
}
|
||||
if ((data_var->header.status & 0x04)) {
|
||||
errMsg("papf sts: Power Low");
|
||||
coloredMsg(LOG_RED, true, "papf sts: Power Low");
|
||||
}
|
||||
if ((data_var->header.status & 0x08)) {
|
||||
errMsg("papf sts: Permanent Error");
|
||||
coloredMsg(LOG_RED, true, "papf sts: Permanent Error");
|
||||
}
|
||||
if ((data_var->header.status & 0x10)) {
|
||||
errMsg("papf sts: Temporary Error");
|
||||
coloredMsg(LOG_RED, true, "papf sts: Temporary Error");
|
||||
}
|
||||
if ((data_var->header.status & 0x20)) {
|
||||
errMsg("papf sts: Specific to manufacturer Error 1");
|
||||
coloredMsg(LOG_RED, true, "papf sts: Specific to manufacturer Error 1");
|
||||
}
|
||||
if ((data_var->header.status & 0x40)) {
|
||||
errMsg("papf sts: Specific to manufacturer Error 2");
|
||||
coloredMsg(LOG_RED, true, "papf sts: Specific to manufacturer Error 2");
|
||||
}
|
||||
if ((data_var->header.status & 0x80)) {
|
||||
errMsg("papf sts: Specific to manufacturer Error 3");
|
||||
coloredMsg(LOG_RED, true, "papf sts: Specific to manufacturer Error 3");
|
||||
}
|
||||
mbus_data_record *record;
|
||||
int i;
|
||||
@ -159,7 +159,7 @@ static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) {
|
||||
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, "papf txt: I:%d, L:%s, U:%s V:%s",
|
||||
coloredMsg(LOG_YELLOW, true, "papf txt: I:%d, L:%s, U:%s V:%s",
|
||||
i,
|
||||
mbusCommHandle->device->consideredField[j].label,
|
||||
mbus_data_record_unit(record),
|
||||
@ -169,14 +169,14 @@ static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) {
|
||||
}
|
||||
mbus_data_record_free(data_var->record);
|
||||
} else {
|
||||
errMsg("papf err: unable to parse frame");
|
||||
coloredMsg(LOG_RED, true, "papf err: unable to parse frame");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void handleRequestEngine(void *handle);
|
||||
static void timeoutHandler(void *handle) {
|
||||
errMsg("mbc timeout");
|
||||
coloredMsg(LOG_RED, true, "mbc timeout");
|
||||
t_mbusCommHandle *localMbusCommHandle = (t_mbusCommHandle*) handle;
|
||||
localMbusCommHandle->state = MBCS_TIMEOUT;
|
||||
localMbusCommHandle->receiving = false;
|
||||
@ -196,11 +196,11 @@ static void handleRequestEngine(void *handle) {
|
||||
|
||||
switch (localMbusCommHandle->state) {
|
||||
case MBCS_IDLE:
|
||||
logMsg("hre state IDLE");
|
||||
coloredMsg(LOG_YELLOW, false, "hre state IDLE");
|
||||
break;
|
||||
|
||||
case MBCS_SEND:
|
||||
logMsg("hre state SEND");
|
||||
coloredMsg(LOG_YELLOW, false, "hre state SEND");
|
||||
localMbusCommHandle->sendBuf[0] = 0x10;
|
||||
localMbusCommHandle->sendBuf[1] = localMbusCommHandle->cmd;
|
||||
localMbusCommHandle->sendBuf[2] = localMbusCommHandle->addr;
|
||||
@ -210,13 +210,13 @@ static void handleRequestEngine(void *handle) {
|
||||
// no break !!
|
||||
|
||||
case MBCS_SEND_CONTINUED:
|
||||
logMsg("hre state SEND_CONTINUED");
|
||||
coloredMsg(LOG_YELLOW, false, "hre state SEND_CONTINUED");
|
||||
show(LED_RED, OFF);
|
||||
if (! loopActive) {
|
||||
logMsg("hre enabling loop, try %d", localMbusCommHandle->retryCnt);
|
||||
coloredMsg(LOG_YELLOW, true, "hre enabling loop, try %d", localMbusCommHandle->retryCnt);
|
||||
localMbusCommHandle->retryCnt++;
|
||||
loopEnable();
|
||||
schAdd(handleRequestEngine, handle, 10, 0); // give 10ms to settled the loop
|
||||
schAdd(handleRequestEngine, handle, 100, 0); // give 100ms to settled the loop
|
||||
} else {
|
||||
localMbusCommHandle->retryCnt = 0;
|
||||
HAL_UART_Transmit_IT(&mbusUart, localMbusCommHandle->sendBuf, 5);
|
||||
@ -227,13 +227,13 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_SENDING_DONE:
|
||||
logMsg("hre state SENDING_DONE");
|
||||
coloredMsg(LOG_YELLOW, false, "hre state SENDING_DONE");
|
||||
localMbusCommHandle->state = MBCS_ENABLE_FRONTEND;
|
||||
schAdd(handleRequestEngine, handle, 3, 0);
|
||||
break;
|
||||
|
||||
case MBCS_ENABLE_FRONTEND:
|
||||
logMsg("hre state ENABLE_FRONTEND");
|
||||
coloredMsg(LOG_YELLOW, false, "hre state ENABLE_FRONTEND");
|
||||
frontendEnable();
|
||||
schAdd(timeoutHandler, handle, 2500, 0);
|
||||
calculatedChksum = 0;
|
||||
@ -243,12 +243,11 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_START1:
|
||||
//logMsg("hre state START1");
|
||||
if (localMbusCommHandle->receivedOctet == 0x68) {
|
||||
localMbusCommHandle->frame.start1 = localMbusCommHandle->receivedOctet;
|
||||
localMbusCommHandle->state = MBCS_LENGTH1;
|
||||
} else {
|
||||
errMsg("hre err: invalid start1 symbol %02x", localMbusCommHandle->receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "hre err: invalid start1 symbol %02x", localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__START1;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
}
|
||||
@ -256,16 +255,15 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_LENGTH1:
|
||||
//logMsg("hre state LENGTH1");
|
||||
if (localMbusCommHandle->receivedOctet <= 3) {
|
||||
errMsg("hre err: length to small %02x", localMbusCommHandle->receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "hre err: length to small %02x", 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) {
|
||||
errMsg("hre err: unable to allocate memory for userdata");
|
||||
coloredMsg(LOG_RED, true, "hre err: unable to allocate memory for userdata");
|
||||
localMbusCommHandle->result = MBCR_ERROR_OUT_OF_MEMORY__USERDATA;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
} else {
|
||||
@ -276,9 +274,8 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_LENGTH2:
|
||||
//logMsg("hre state LENGTH2");
|
||||
if (localMbusCommHandle->frame.length1 != localMbusCommHandle->receivedOctet) {
|
||||
errMsg("hre err: invalid length2 %02x vs. %02x", localMbusCommHandle->frame.length1, localMbusCommHandle->receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "hre err: invalid length2 %02x vs. %02x", localMbusCommHandle->frame.length1, localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__LENGTH2;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
} else {
|
||||
@ -289,12 +286,11 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_START2:
|
||||
//logMsg("hre state START2");
|
||||
if (localMbusCommHandle->receivedOctet == 0x68) {
|
||||
localMbusCommHandle->frame.start2 = localMbusCommHandle->receivedOctet;
|
||||
localMbusCommHandle->state = MBCS_C_FIELD;
|
||||
} else {
|
||||
errMsg("hre err: invalid start2 symbol %02x", localMbusCommHandle->receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "hre err: invalid start2 symbol %02x", localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__START2;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
}
|
||||
@ -302,7 +298,6 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_C_FIELD:
|
||||
//logMsg("hre state C_FIELD");
|
||||
localMbusCommHandle->frame.c = localMbusCommHandle->receivedOctet;
|
||||
calculatedChksum += localMbusCommHandle->receivedOctet;
|
||||
localMbusCommHandle->state = MBCS_A_FIELD;
|
||||
@ -310,7 +305,6 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_A_FIELD:
|
||||
//logMsg("hre state A_FIELD");
|
||||
localMbusCommHandle->frame.a = localMbusCommHandle->receivedOctet;
|
||||
calculatedChksum += localMbusCommHandle->receivedOctet;
|
||||
localMbusCommHandle->state = MBCS_CI_FIELD;
|
||||
@ -318,7 +312,6 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_CI_FIELD:
|
||||
//logMsg("hre state CI_FIELD");
|
||||
localMbusCommHandle->frame.ci = localMbusCommHandle->receivedOctet;
|
||||
calculatedChksum += localMbusCommHandle->receivedOctet;
|
||||
localMbusCommHandle->state = MBCS_USERDATA;
|
||||
@ -326,7 +319,6 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_USERDATA:
|
||||
//logMsg("hre state USERDATA");
|
||||
localMbusCommHandle->frame.userdata[userdataIdx] = localMbusCommHandle->receivedOctet;
|
||||
calculatedChksum += localMbusCommHandle->receivedOctet;
|
||||
userdataIdx++;
|
||||
@ -337,9 +329,8 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_CHKSUM:
|
||||
//logMsg("hre state CHKSUM");
|
||||
if (localMbusCommHandle->receivedOctet != calculatedChksum) {
|
||||
errMsg("hre err: invalid checksum %02x vs %02x", calculatedChksum, localMbusCommHandle->receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "hre err: invalid checksum %02x vs %02x", calculatedChksum, localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__INVALID_CHKSUM;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
} else {
|
||||
@ -350,13 +341,12 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_STOP:
|
||||
//logMsg("hre state STOP");
|
||||
if (localMbusCommHandle->receivedOctet == 0x16) {
|
||||
localMbusCommHandle->frame.stop = localMbusCommHandle->receivedOctet;
|
||||
localMbusCommHandle->state = MBCS_DONE;
|
||||
schAdd(handleRequestEngine, handle, 0, 0);
|
||||
} else {
|
||||
errMsg("hre err: invalid stop symbol %02x", localMbusCommHandle->receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "hre err: invalid stop symbol %02x", localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__STOP;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
receiveNext(localMbusCommHandle);
|
||||
@ -364,7 +354,7 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_DONE:
|
||||
logMsg("hre state DONE");
|
||||
coloredMsg(LOG_YELLOW, false, "hre state DONE");
|
||||
parseAndPrintFrame(localMbusCommHandle);
|
||||
if (localMbusCommHandle->frame.userdata != NULL) {
|
||||
free(localMbusCommHandle->frame.userdata);
|
||||
@ -377,14 +367,14 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_ERROR:
|
||||
errMsg("hre state ERROR");
|
||||
coloredMsg(LOG_RED, true, "hre state ERROR");
|
||||
show(LED_RED, ON);
|
||||
errMsg("hre err: already error, read the rest (now: %02x) until timeout", localMbusCommHandle->receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "hre err: already error, read the rest (now: %02x) until timeout", localMbusCommHandle->receivedOctet);
|
||||
receiveNext(localMbusCommHandle);
|
||||
break;
|
||||
|
||||
case MBCS_TIMEOUT:
|
||||
errMsg("hre state TIMEOUT");
|
||||
coloredMsg(LOG_RED, true, "hre state TIMEOUT");
|
||||
mbusCommStats.errorCnt += 1;
|
||||
localMbusCommHandle->device->failures += 1;
|
||||
localMbusCommHandle->receiving = false;
|
||||
@ -396,11 +386,11 @@ static void handleRequestEngine(void *handle) {
|
||||
uint8_t kitchenSink[16];
|
||||
memset(kitchenSink, 0, 16);
|
||||
HAL_StatusTypeDef r = HAL_UART_Receive(&mbusUart, kitchenSink, 16, 100);
|
||||
errMsg("hre abort, last receive result: %02x", r);
|
||||
coloredMsg(LOG_RED, true, "hre abort, last receive result: %02x", r);
|
||||
// no break
|
||||
|
||||
case MBCS_DISABLE_FRONTEND:
|
||||
logMsg("hre state DISABLE_FRONTEND");
|
||||
coloredMsg(LOG_YELLOW, false, "hre state DISABLE_FRONTEND");
|
||||
frontendDisable();
|
||||
localMbusCommHandle->state = MBCS_IDLE;
|
||||
break;
|
||||
@ -421,7 +411,7 @@ void mbusCommRxCpltCallback(UART_HandleTypeDef *huart) {
|
||||
schAdd(handleRequestEngine, (void*) &mbusCommHandle, 0, 0);
|
||||
mbusCommHandle.receiving = false;
|
||||
} else {
|
||||
errMsg("mcrx: received 0x%02x but not expected", mbusCommHandle.receivedOctet);
|
||||
coloredMsg(LOG_RED, true, "mcrx: received 0x%02x but not expected", mbusCommHandle.receivedOctet);
|
||||
}
|
||||
}
|
||||
|
||||
@ -434,7 +424,7 @@ e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice) {
|
||||
|
||||
if (mbusCommEnabled) {
|
||||
if (mbusCommHandle.state == MBCS_IDLE) {
|
||||
coloredMsg(LOG_BLUE, "*** NEW REQUEST %s R:%d F:%d GRC:%d GEC:%d ***",
|
||||
coloredMsg(LOG_YELLOW, true, "*** NEW REQUEST %s R:%d F:%d GRC:%d GEC:%d ***",
|
||||
mbusDevice->deviceName,
|
||||
mbusDevice->requests,
|
||||
mbusDevice->failures,
|
||||
@ -455,7 +445,7 @@ e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice) {
|
||||
} else {
|
||||
res = MBCRR_DISABLED;
|
||||
}
|
||||
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
@ -32,10 +32,10 @@ static uint32_t watchdogCounter = 0;
|
||||
|
||||
void watchdogHandler(void *handle) {
|
||||
if (watchdogCounter > 0) {
|
||||
coloredMsg(LOG_GREEN, "Watchdog received in between");
|
||||
coloredMsg(LOG_GREEN, false, "Watchdog received in between");
|
||||
watchdogCounter = 0;
|
||||
} else {
|
||||
coloredMsg(LOG_RED, "No watchdog received in between, booting the system ...");
|
||||
coloredMsg(LOG_RED, true, "No watchdog received in between, booting the system ...");
|
||||
// boot the system
|
||||
}
|
||||
}
|
||||
@ -45,18 +45,18 @@ static void mqttCallback(char *topic, uint8_t *payload, uint16_t payloadLength)
|
||||
if (0 == strcmp(topic, WatchdogTopic)) {
|
||||
watchdogCounter++;
|
||||
} else {
|
||||
logMsg("mqcb: %s : %.*s", topic, payloadLength, payload);
|
||||
coloredMsg(LOG_GREEN, false, "mqcb: %s : %.*s", topic, payloadLength, payload);
|
||||
}
|
||||
}
|
||||
|
||||
static void mqttStatusPublisher(void *handle) {
|
||||
logMsg("mqsp: publishing status");
|
||||
coloredMsg(LOG_GREEN, false, "mqsp: publishing status");
|
||||
t_mbusCommStats *mbusCommStats = mbusCommGetStats();
|
||||
char buf[64];
|
||||
snprintf(buf, 128, "{\"uptime\":\"%ld\", \"tasks\":\"%d\", \"requests\":\"%d\", \"errors\":\"%d\"}",
|
||||
HAL_GetTick()/1000, schTaskCnt(), mbusCommStats->requestCnt, mbusCommStats->errorCnt);
|
||||
bool res = publish(&mqttClient, StatusTopic, (const char*)buf, strlen(buf), false);
|
||||
coloredMsg(LOG_YELLOW, "mqch, publish returned %d", res);
|
||||
coloredMsg(LOG_GREEN, false, "mqch, publish returned %d", res);
|
||||
|
||||
}
|
||||
|
||||
@ -67,21 +67,21 @@ void mqttCommHandler(void *handle) {
|
||||
if (isNetworkAvailable()) {
|
||||
switch (state) {
|
||||
case 0:
|
||||
coloredMsg(LOG_YELLOW, "mqch, initializing mqtt client");
|
||||
coloredMsg(LOG_GREEN, false, "mqch, initializing mqtt client");
|
||||
client.sockNum = MQTT_SOCK;
|
||||
mqttClientInit(&mqttClient, &client, mqttCallback);
|
||||
coloredMsg(LOG_YELLOW, "mqch: mqtt client initialized");
|
||||
coloredMsg(LOG_GREEN, false, "mqch: mqtt client initialized");
|
||||
|
||||
state = 1;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
coloredMsg(LOG_YELLOW, "mqch, connecting to broker ");
|
||||
coloredMsg(LOG_GREEN, false, "mqch, connecting to broker ");
|
||||
bool res = mqttConnect(&mqttClient, brokerAddress, 1883, "mbv3gw-client", NULL, NULL, NULL, 0, false, NULL, false);
|
||||
coloredMsg(LOG_YELLOW, "mqch, mqttConnect returns %d", res);
|
||||
coloredMsg(LOG_GREEN, false, "mqch, mqttConnect returns %d", res);
|
||||
|
||||
if (res) {
|
||||
coloredMsg(LOG_YELLOW, "mqch, ok, connected");
|
||||
coloredMsg(LOG_GREEN, false, "mqch, ok, connected");
|
||||
state = 2;
|
||||
} else {
|
||||
state = 255;
|
||||
@ -89,40 +89,39 @@ void mqttCommHandler(void *handle) {
|
||||
break;
|
||||
|
||||
case 2:
|
||||
coloredMsg(LOG_YELLOW, "mqch, publish start-up");
|
||||
coloredMsg(LOG_GREEN, false, "mqch, publish start-up");
|
||||
res = publish(&mqttClient, StartupTopic, (const char*)message, strlen(message), false);
|
||||
coloredMsg(LOG_YELLOW, "mqch, publish returned %d", res);
|
||||
coloredMsg(LOG_GREEN, false, "mqch, publish returned %d", res);
|
||||
schAdd(mqttStatusPublisher, NULL, 0, 60000);
|
||||
coloredMsg(LOG_YELLOW, "mqch, status publisher scheduled");
|
||||
coloredMsg(LOG_GREEN, false, "mqch, status publisher scheduled");
|
||||
state = 3;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
coloredMsg(LOG_YELLOW, "mqch, subscribe watchdog");
|
||||
coloredMsg(LOG_GREEN, false, "mqch, subscribe watchdog");
|
||||
res = subscribe(&mqttClient, WatchdogTopic, MQTTQOS0);
|
||||
coloredMsg(LOG_YELLOW, "mqch, subscribe returned %d", res);
|
||||
coloredMsg(LOG_GREEN, false, "mqch, subscribe returned %d", res);
|
||||
schAdd(watchdogHandler, NULL, 60000, 60000);
|
||||
coloredMsg(LOG_YELLOW, "mqch, watchdogHandler scheduled");
|
||||
coloredMsg(LOG_GREEN, false, "mqch, watchdogHandler scheduled");
|
||||
state = 4;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
coloredMsg(LOG_YELLOW, "mqch, now entering the loop");
|
||||
coloredMsg(LOG_GREEN, false, "mqch, now entering the loop");
|
||||
state = 5;
|
||||
break;
|
||||
|
||||
case 5:
|
||||
// coloredMsg(LOG_YELLOW, "mqch, looping");
|
||||
if (! mqttLoop(&mqttClient)) {
|
||||
state = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case 255:
|
||||
coloredMsg(LOG_YELLOW, "mqch, error state, will stop here");
|
||||
coloredMsg(LOG_RED, true, "mqch, error state, will stop here");
|
||||
schDel(mqttCommHandler, NULL);
|
||||
schDel(watchdogHandler, NULL);
|
||||
coloredMsg(LOG_YELLOW, "mqch, trying again in one minute");
|
||||
coloredMsg(LOG_RED, true, "mqch, trying again in one minute");
|
||||
schAdd(mqttCommHandler, NULL, 60000, 100);
|
||||
break;
|
||||
}
|
||||
@ -136,8 +135,8 @@ void mqttCommInit() {
|
||||
void mqttPublish(char *topic, char *message) {
|
||||
bool res = publish(&mqttClient, topic, message, strlen(message), false);
|
||||
if (res) {
|
||||
coloredMsg(LOG_YELLOW, "mqp: %s -> %s successfully published", message, topic);
|
||||
coloredMsg(LOG_GREEN, false, "mqp: %s -> %s successfully published", message, topic);
|
||||
} else {
|
||||
coloredMsg(LOG_RED, "mqp: %s -> %s failed to publish", message, topic);
|
||||
coloredMsg(LOG_RED, true, "mqp: %s -> %s failed to publish", message, topic);
|
||||
}
|
||||
}
|
@ -39,23 +39,19 @@ static void wiz_cs_deselect(void) {
|
||||
static uint8_t wiz_spi_readbyte(void) {
|
||||
uint8_t rbuf;
|
||||
HAL_SPI_Receive(ðerSpi, &rbuf, 1, HAL_MAX_DELAY);
|
||||
// coloredMsg(LOG_YELLOW, "R: %02x", rbuf);
|
||||
return rbuf;
|
||||
}
|
||||
|
||||
static void wiz_spi_writebyte(uint8_t wb) {
|
||||
// coloredMsg(LOG_YELLOW, "W: %02x", wb);
|
||||
HAL_SPI_Transmit(ðerSpi, &wb, 1, HAL_MAX_DELAY);
|
||||
}
|
||||
|
||||
static void wiz_spi_readburst(uint8_t* pBuf, uint16_t len) {
|
||||
HAL_SPI_Receive(ðerSpi, pBuf, len, HAL_MAX_DELAY);
|
||||
// coloredMsg(LOG_YELLOW, "RB: %d %02x %02x %02x", len, pBuf[0], pBuf[1], pBuf[2]);
|
||||
}
|
||||
|
||||
static void wiz_spi_writeburst(uint8_t* pBuf, uint16_t len) {
|
||||
HAL_SPI_Transmit(ðerSpi, pBuf, len, HAL_MAX_DELAY);
|
||||
// coloredMsg(LOG_YELLOW, "WB: %d %02x %02x %02x", len, pBuf[0], pBuf[1], pBuf[2]);
|
||||
}
|
||||
|
||||
static void wizReset(bool b) {
|
||||
@ -64,36 +60,36 @@ static void wizReset(bool b) {
|
||||
|
||||
|
||||
static void wizDHCPAssign() {
|
||||
coloredMsg(LOG_GREEN, "wizda");
|
||||
coloredMsg(LOG_BLUE, false, "wizda");
|
||||
getIPfromDHCP(netInfo.ip);
|
||||
coloredMsg(LOG_GREEN, "wizda, IP: %d.%d.%d.%d", netInfo.ip[0], netInfo.ip[1], netInfo.ip[2], netInfo.ip[3]);
|
||||
coloredMsg(LOG_BLUE, false, "wizda, IP: %d.%d.%d.%d", netInfo.ip[0], netInfo.ip[1], netInfo.ip[2], netInfo.ip[3]);
|
||||
getGWfromDHCP(netInfo.gw);
|
||||
coloredMsg(LOG_GREEN, "wizda, GW: %d.%d.%d.%d", netInfo.gw[0], netInfo.gw[1], netInfo.gw[2], netInfo.gw[3]);
|
||||
coloredMsg(LOG_BLUE, false, "wizda, GW: %d.%d.%d.%d", netInfo.gw[0], netInfo.gw[1], netInfo.gw[2], netInfo.gw[3]);
|
||||
getSNfromDHCP(netInfo.sn);
|
||||
coloredMsg(LOG_GREEN, "wizda, SN: %d.%d.%d.%d", netInfo.sn[0], netInfo.sn[1], netInfo.sn[2], netInfo.sn[3]);
|
||||
coloredMsg(LOG_BLUE, false, "wizda, SN: %d.%d.%d.%d", netInfo.sn[0], netInfo.sn[1], netInfo.sn[2], netInfo.sn[3]);
|
||||
getDNSfromDHCP(netInfo.dns);
|
||||
coloredMsg(LOG_GREEN, "wizda, DNS: %d.%d.%d.%d", netInfo.dns[0], netInfo.dns[1], netInfo.dns[2], netInfo.dns[3]);
|
||||
coloredMsg(LOG_BLUE, false, "wizda, DNS: %d.%d.%d.%d", netInfo.dns[0], netInfo.dns[1], netInfo.dns[2], netInfo.dns[3]);
|
||||
|
||||
wizchip_setnetinfo(&netInfo);
|
||||
coloredMsg(LOG_GREEN, "wizda, set netinfo again");
|
||||
coloredMsg(LOG_BLUE, false, "wizda, set netinfo again");
|
||||
|
||||
networkAvailable = true;
|
||||
coloredMsg(LOG_GREEN, "wizda, network is available");
|
||||
coloredMsg(LOG_BLUE, false, "wizda, network is available");
|
||||
}
|
||||
|
||||
static void wizDHCPUpdate() {
|
||||
coloredMsg(LOG_YELLOW, "wizdu");
|
||||
coloredMsg(LOG_BLUE, false, "wizdu");
|
||||
getIPfromDHCP(netInfo.ip);
|
||||
coloredMsg(LOG_YELLOW, "wizdu, IP: %d.%d.%d.%d", netInfo.ip[0], netInfo.ip[1], netInfo.ip[2], netInfo.ip[3]);
|
||||
coloredMsg(LOG_BLUE, false, "wizdu, IP: %d.%d.%d.%d", netInfo.ip[0], netInfo.ip[1], netInfo.ip[2], netInfo.ip[3]);
|
||||
getGWfromDHCP(netInfo.gw);
|
||||
coloredMsg(LOG_YELLOW, "wizdu, GW: %d.%d.%d.%d", netInfo.gw[0], netInfo.gw[1], netInfo.gw[2], netInfo.gw[3]);
|
||||
coloredMsg(LOG_BLUE, false, "wizdu, GW: %d.%d.%d.%d", netInfo.gw[0], netInfo.gw[1], netInfo.gw[2], netInfo.gw[3]);
|
||||
getSNfromDHCP(netInfo.sn);
|
||||
coloredMsg(LOG_YELLOW, "wizdu, SN: %d.%d.%d.%d", netInfo.sn[0], netInfo.sn[1], netInfo.sn[2], netInfo.sn[3]);
|
||||
coloredMsg(LOG_BLUE, false, "wizdu, SN: %d.%d.%d.%d", netInfo.sn[0], netInfo.sn[1], netInfo.sn[2], netInfo.sn[3]);
|
||||
getDNSfromDHCP(netInfo.dns);
|
||||
coloredMsg(LOG_YELLOW, "wizdu, DNS: %d.%d.%d.%d", netInfo.dns[0], netInfo.dns[1], netInfo.dns[2], netInfo.dns[3]);
|
||||
coloredMsg(LOG_BLUE, false, "wizdu, DNS: %d.%d.%d.%d", netInfo.dns[0], netInfo.dns[1], netInfo.dns[2], netInfo.dns[3]);
|
||||
|
||||
wizchip_setnetinfo(&netInfo);
|
||||
coloredMsg(LOG_YELLOW, "wizdu, netinfo updated");
|
||||
coloredMsg(LOG_BLUE, false, "wizdu, netinfo updated");
|
||||
}
|
||||
|
||||
static void wizDHCPHandler(void *handle) {
|
||||
@ -102,7 +98,7 @@ static void wizDHCPHandler(void *handle) {
|
||||
uint8_t res = DHCP_run();
|
||||
|
||||
if (lastDhcpRes != res) {
|
||||
coloredMsg(LOG_RED, "wizdh, dhcp state has changed: %d", res);
|
||||
coloredMsg(LOG_BLUE, false, "wizdh, dhcp state has changed: %d", res);
|
||||
lastDhcpRes = res;
|
||||
}
|
||||
}
|
||||
@ -114,7 +110,7 @@ static void wizPhyLinkHandler(void *handle) {
|
||||
uint8_t phyLink = 0;
|
||||
int8_t res = ctlwizchip(CW_GET_PHYLINK, (void*) &phyLink);
|
||||
if (lastStablePhyLink != phyLink) {
|
||||
coloredMsg(LOG_RED, "wizplh, ctlwizchip returns %d, phy link changed to %d", res, phyLink);
|
||||
coloredMsg(LOG_BLUE, false, "wizplh, ctlwizchip returns %d, phy link changed to %d", res, phyLink);
|
||||
lastStablePhyLink = phyLink;
|
||||
|
||||
if (phyLink == PHY_LINK_ON) {
|
||||
@ -122,24 +118,24 @@ static void wizPhyLinkHandler(void *handle) {
|
||||
memset(dhcpBuffer, 0, DHCP_BUFFER_SIZE);
|
||||
reg_dhcp_cbfunc(wizDHCPAssign, wizDHCPUpdate, NULL);
|
||||
DHCP_init(DHCP_SOCK, dhcpBuffer);
|
||||
coloredMsg(LOG_RED, "wizplh, DHCP initialized");
|
||||
coloredMsg(LOG_BLUE, false, "wizplh, DHCP initialized");
|
||||
|
||||
// run the dhcp handler the first time after 1s and then every 100ms
|
||||
schAdd(wizDHCPHandler, NULL, 1000, 1000);
|
||||
coloredMsg(LOG_RED, "wizplh, DHCP handler scheduled");
|
||||
coloredMsg(LOG_BLUE, false, "wizplh, DHCP handler scheduled");
|
||||
|
||||
dhcpInitialized = true;
|
||||
} else {
|
||||
networkAvailable = false;
|
||||
coloredMsg(LOG_RED, "wizplh, network is unavailable");
|
||||
coloredMsg(LOG_BLUE, false, "wizplh, network is unavailable");
|
||||
|
||||
// stop DHCP handler
|
||||
if (dhcpInitialized) {
|
||||
DHCP_stop();
|
||||
coloredMsg(LOG_RED, "wizplh, DHCP stopped");
|
||||
coloredMsg(LOG_BLUE, false, "wizplh, DHCP stopped");
|
||||
|
||||
schDel(wizDHCPHandler, NULL);
|
||||
coloredMsg(LOG_RED, "wizplh, DHCP handler unscheduled");
|
||||
coloredMsg(LOG_BLUE, false, "wizplh, DHCP handler unscheduled");
|
||||
|
||||
dhcpInitialized = false;
|
||||
}
|
||||
@ -148,29 +144,29 @@ static void wizPhyLinkHandler(void *handle) {
|
||||
}
|
||||
|
||||
int wizInit() {
|
||||
coloredMsg(LOG_RED, "wizI, resetting Ethernet module");
|
||||
coloredMsg(LOG_BLUE, false, "wizI, resetting Ethernet module");
|
||||
wizReset(true);
|
||||
activeDelay(2);
|
||||
wizReset(false);
|
||||
activeDelay(50);
|
||||
|
||||
coloredMsg(LOG_RED, "wizI, registering callbacks");
|
||||
coloredMsg(LOG_BLUE, false, "wizI, registering callbacks");
|
||||
reg_wizchip_cs_cbfunc(wiz_cs_select, wiz_cs_deselect);
|
||||
coloredMsg(LOG_GREEN, "wizI, cs funcs registed");
|
||||
coloredMsg(LOG_BLUE, false, "wizI, cs funcs registed");
|
||||
reg_wizchip_spi_cbfunc(wiz_spi_readbyte, wiz_spi_writebyte);
|
||||
coloredMsg(LOG_GREEN, "wizI, spi funcs registed");
|
||||
coloredMsg(LOG_BLUE, false, "wizI, spi funcs registed");
|
||||
reg_wizchip_spiburst_cbfunc(wiz_spi_readburst, wiz_spi_writeburst);
|
||||
coloredMsg(LOG_GREEN, "wizI, spi burst funcs registed");
|
||||
coloredMsg(LOG_BLUE, false, "wizI, spi burst funcs registed");
|
||||
|
||||
|
||||
coloredMsg(LOG_RED, "wizI, initializing Ethernet module");
|
||||
coloredMsg(LOG_BLUE, false, "wizI, initializing Ethernet module");
|
||||
uint8_t bufSizes[] = { 2, 2, 2, 2, 2, 2, 2, 2 };
|
||||
int8_t res = wizchip_init(bufSizes, bufSizes);
|
||||
coloredMsg(LOG_RED, "wizI, module driver returned %d", res);
|
||||
coloredMsg(LOG_BLUE, false, "wizI, module driver returned %d", res);
|
||||
|
||||
wizphy_reset();
|
||||
activeDelay(5);
|
||||
coloredMsg(LOG_RED, "wizI, reset phy");
|
||||
coloredMsg(LOG_BLUE, false, "wizI, reset phy");
|
||||
|
||||
wiz_PhyConf wpc;
|
||||
wpc.mode = PHY_MODE_MANUAL;
|
||||
@ -178,21 +174,21 @@ int wizInit() {
|
||||
wpc.duplex = PHY_DUPLEX_FULL;
|
||||
wizphy_setphyconf(&wpc);
|
||||
activeDelay(5);
|
||||
coloredMsg(LOG_RED, "wizI, phy config set");
|
||||
coloredMsg(LOG_BLUE, false, "wizI, phy config set");
|
||||
|
||||
wizchip_setnetinfo(&netInfo);
|
||||
coloredMsg(LOG_RED, "wizI, netinfo set to Ethernet module");
|
||||
coloredMsg(LOG_BLUE, false, "wizI, netinfo set to Ethernet module");
|
||||
|
||||
res = wizchip_setnetmode(NM_FORCEARP); // and not NM_PINGBLOCK
|
||||
coloredMsg(LOG_RED, "wizI, set netmode, result is %d", res);
|
||||
coloredMsg(LOG_BLUE, false, "wizI, set netmode, result is %d", res);
|
||||
|
||||
uint8_t buf[6];
|
||||
res = ctlwizchip(CW_GET_ID, (void*) buf);
|
||||
coloredMsg(LOG_RED, "wizI, CW_GET_ID: %d %02x %02x %02x %02x %02x %02x", res, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
|
||||
coloredMsg(LOG_BLUE, false, "wizI, CW_GET_ID: %d %02x %02x %02x %02x %02x %02x", res, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
|
||||
|
||||
|
||||
schAdd(wizPhyLinkHandler, NULL, 0, 1000);
|
||||
coloredMsg(LOG_RED, "wizI, PhyLink handler scheduled");
|
||||
coloredMsg(LOG_BLUE, false, "wizI, PhyLink handler scheduled");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user