This commit is contained in:
Wolfgang Hottgenroth 2020-11-17 12:01:15 +01:00
parent ccf6982b62
commit 23568db1b5
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
8 changed files with 137 additions and 165 deletions

View File

@ -3,8 +3,10 @@
#include <main.h> #include <main.h>
#include <stdbool.h>
typedef enum { typedef enum {
LOG_NORMAL,
LOG_HIGH, LOG_HIGH,
LOG_RED, LOG_RED,
LOG_GREEN, LOG_GREEN,
@ -22,10 +24,7 @@ void logFree();
// return value can be ignored, it is only used in test // return value can be ignored, it is only used in test
int logMsg(const char *format, ...); int logMsg(const char *format, ...);
// in red int coloredMsg(const t_logColor color, bool syslogToo, const char *format, ...);
int errMsg(const char *format, ...);
int coloredMsg(const t_logColor color, const char *format, ...);
void debugTxCpltCallback(UART_HandleTypeDef *huart); void debugTxCpltCallback(UART_HandleTypeDef *huart);

View File

@ -148,7 +148,7 @@ int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetConfigMode) {
configMode = false; 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 #define MAX_NUM_OF_ARGS 8
char *args[MAX_NUM_OF_TASKS]; char *args[MAX_NUM_OF_TASKS];
@ -164,7 +164,7 @@ int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetConfigMode) {
char *cmd = args[0]; char *cmd = args[0];
int8_t retCode = 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")) { if (0 == strcmp(cmd, "quit")) {
messageToSend = GOODBYE_MSG; messageToSend = GOODBYE_MSG;
@ -182,11 +182,11 @@ int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetConfigMode) {
} }
messageToSend = NULL; messageToSend = NULL;
} else if (0 == strcmp(cmd, "enable")) { } else if (0 == strcmp(cmd, "enable")) {
coloredMsg(LOG_YELLOW, "cec, enable config mode"); coloredMsg(LOG_YELLOW, true, "cec, enable config mode");
configMode = true; configMode = true;
retCode = 1; retCode = 1;
} else if (0 == strcmp(cmd, "disable")) { } else if (0 == strcmp(cmd, "disable")) {
coloredMsg(LOG_YELLOW, "cec, disable config mode"); coloredMsg(LOG_YELLOW, true, "cec, disable config mode");
configMode = false; configMode = false;
retCode = 2; retCode = 2;
} else { } else {
@ -241,13 +241,13 @@ void cmdHandlerEngine(void *handle) {
if (isNetworkAvailable()) { if (isNetworkAvailable()) {
switch (state) { switch (state) {
case CH_INIT: 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); 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) { if (res == CMD_SOCK) {
coloredMsg(LOG_YELLOW, "che, socket is initialized"); coloredMsg(LOG_YELLOW, false, "che, socket is initialized");
state = CH_LISTEN; state = CH_LISTEN;
} else { } else {
state = CH_ERROR; state = CH_ERROR;
@ -255,13 +255,13 @@ void cmdHandlerEngine(void *handle) {
break; break;
case CH_LISTEN: case CH_LISTEN:
coloredMsg(LOG_YELLOW, "che, listening"); coloredMsg(LOG_YELLOW, false, "che, listening");
res = listen(CMD_SOCK); 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) { if (res == SOCK_OK) {
coloredMsg(LOG_YELLOW, "che, ok, waiting for established"); coloredMsg(LOG_YELLOW, false, "che, ok, waiting for established");
state = CH_WAITING; state = CH_WAITING;
} else { } else {
state = CH_ERROR; state = CH_ERROR;
@ -269,28 +269,26 @@ void cmdHandlerEngine(void *handle) {
break; break;
case CH_WAITING: case CH_WAITING:
//coloredMsg(LOG_YELLOW, "che, waiting for established");
sockState = getSn_SR(CMD_SOCK); sockState = getSn_SR(CMD_SOCK);
if (sockState != SOCK_LISTEN) { 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) { if (sockState == SOCK_ESTABLISHED) {
coloredMsg(LOG_YELLOW, "che, connection is established"); coloredMsg(LOG_YELLOW, true, "che, connection is established");
state = CH_BANNER; state = CH_BANNER;
} }
break; break;
case CH_BANNER: case CH_BANNER:
coloredMsg(LOG_YELLOW, "che, send banner"); coloredMsg(LOG_YELLOW, false, "che, send banner");
sockState = getSn_SR(CMD_SOCK); sockState = getSn_SR(CMD_SOCK);
if (sockState != SOCK_ESTABLISHED) { 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; state = CH_DISCONNECT;
} else { } else {
resultSend = send(CMD_SOCK, banner, strlen(banner)); 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; prompt = defaultPrompt;
resetConfigMode = true; resetConfigMode = true;
state = CH_PROMPT; state = CH_PROMPT;
@ -298,14 +296,14 @@ void cmdHandlerEngine(void *handle) {
break; break;
case CH_PROMPT: case CH_PROMPT:
coloredMsg(LOG_YELLOW, "che send prompt"); coloredMsg(LOG_YELLOW, false, "che send prompt");
sockState = getSn_SR(CMD_SOCK); sockState = getSn_SR(CMD_SOCK);
if (sockState != SOCK_ESTABLISHED) { 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; state = CH_DISCONNECT;
} else { } else {
resultSend = send(CMD_SOCK, prompt, strlen(prompt)); 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; state = CH_RECEIVE;
} }
break; break;
@ -313,17 +311,15 @@ void cmdHandlerEngine(void *handle) {
case CH_RECEIVE: case CH_RECEIVE:
sockState = getSn_SR(CMD_SOCK); sockState = getSn_SR(CMD_SOCK);
if (sockState != SOCK_ESTABLISHED) { 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; state = CH_DISCONNECT;
} else { } else {
// coloredMsg(LOG_YELLOW, "che, now waiting for some input");
receivedOctets = getSn_RX_RSR(CMD_SOCK); receivedOctets = getSn_RX_RSR(CMD_SOCK);
// coloredMsg(LOG_YELLOW, "che, getSn_RxMAX returns %d", res16);
if (receivedOctets > 0) { if (receivedOctets > 0) {
memset(receiveBuffer, 0, sizeof(receiveBuffer)); memset(receiveBuffer, 0, sizeof(receiveBuffer));
resultRecv = recv(CMD_SOCK, receiveBuffer, 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 (resultRecv > 0) {
if ((receiveBuffer[strlen(receiveBuffer) - 1] == 0x0a) || if ((receiveBuffer[strlen(receiveBuffer) - 1] == 0x0a) ||
(receiveBuffer[strlen(receiveBuffer) - 1] == 0x0d)) { (receiveBuffer[strlen(receiveBuffer) - 1] == 0x0d)) {
@ -333,7 +329,7 @@ void cmdHandlerEngine(void *handle) {
(receiveBuffer[strlen(receiveBuffer) - 1] == 0x0d)) { (receiveBuffer[strlen(receiveBuffer) - 1] == 0x0d)) {
receiveBuffer[strlen(receiveBuffer) - 1] = 0; 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); int8_t resCEC = cmdExecuteCommand(receiveBuffer, resetConfigMode);
resetConfigMode = false; resetConfigMode = false;
switch (resCEC) { switch (resCEC) {
@ -358,25 +354,25 @@ void cmdHandlerEngine(void *handle) {
break; break;
case CH_DISCONNECT: case CH_DISCONNECT:
coloredMsg(LOG_YELLOW, "che, close our end"); coloredMsg(LOG_YELLOW, true, "che, close our end");
resultDisconnect = disconnect(CMD_SOCK); 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; state = CH_DISCONNECT_WAIT;
break; break;
case CH_DISCONNECT_WAIT: case CH_DISCONNECT_WAIT:
coloredMsg(LOG_YELLOW, "che, waiting after disconnect"); coloredMsg(LOG_YELLOW, false, "che, waiting after disconnect");
sockState = getSn_SR(CMD_SOCK); 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) { if (sockState == SOCK_CLOSED) {
coloredMsg(LOG_YELLOW, "che, socket is closed now"); coloredMsg(LOG_YELLOW, true, "che, socket is closed now");
state = CH_INIT; state = CH_INIT;
} }
break; break;
case CH_ERROR: 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); schDel(cmdHandlerEngine, NULL);
break; break;
} }

View File

@ -136,16 +136,16 @@ void eepromInit() {
logMsg("eeI, read header"); logMsg("eeI, read header");
eepromRead(EEPROM_HEADER_ADDR, eepromHeader.b, sizeof(eepromHeader)); 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) { if (eepromHeader.s.magic != EEPROM_MAGIC) {
coloredMsg(LOG_RED, "eeI, eeprom is uninitialized"); logMsg("eeI, eeprom is uninitialized");
deviceStats.s.totalPowercycles = 0; deviceStats.s.totalPowercycles = 0;
deviceStats.s.totalRunningHours = 0; deviceStats.s.totalRunningHours = 0;
deviceStats.s.totalRequests = 0; deviceStats.s.totalRequests = 0;
deviceStats.s.totalFailures = 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)); eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
eepromActiveDelay(7); eepromActiveDelay(7);
@ -158,35 +158,35 @@ void eepromInit() {
eepromActiveDelay(7); eepromActiveDelay(7);
} }
} }
coloredMsg(LOG_RED, "eeI, storage blocks initialized"); logMsg("eeI, storage blocks initialized");
eepromHeader.s.magic = EEPROM_MAGIC; eepromHeader.s.magic = EEPROM_MAGIC;
eepromHeader.s.activeBlock = 0; eepromHeader.s.activeBlock = 0;
eepromHeader.s.writeCounter = 1; 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)); eepromWrite(EEPROM_HEADER_ADDR, eepromHeader.b, sizeof(eepromHeader));
eepromActiveDelay(7); eepromActiveDelay(7);
coloredMsg(LOG_GREEN, "eeI, eeprom has been initialized"); logMsg("eeI, eeprom has been initialized");
} else { } 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)); eepromRead(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
coloredMsg(LOG_GREEN, "eeI, total powercycles so far: %d", deviceStats.s.totalPowercycles); logMsg("eeI, total powercycles so far: %d", deviceStats.s.totalPowercycles);
coloredMsg(LOG_GREEN, "eeI, total running hours so far: %d", deviceStats.s.totalRunningHours); logMsg("eeI, total running hours so far: %d", deviceStats.s.totalRunningHours);
coloredMsg(LOG_GREEN, "eeI, total requests so far: %d", deviceStats.s.totalRequests); logMsg("eeI, total requests so far: %d", deviceStats.s.totalRequests);
coloredMsg(LOG_GREEN, "eeI, total failures so far: %d", deviceStats.s.totalFailures); logMsg("eeI, total failures so far: %d", deviceStats.s.totalFailures);
t_mbusCommStats stats = { .requestCnt = deviceStats.s.totalRequests, .errorCnt = deviceStats.s.totalFailures}; t_mbusCommStats stats = { .requestCnt = deviceStats.s.totalRequests, .errorCnt = deviceStats.s.totalFailures};
mbusCommSetStats(stats); mbusCommSetStats(stats);
deviceStats.s.totalPowercycles += 1; 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)); eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
eepromActiveDelay(7); eepromActiveDelay(7);
schAdd(eepromHourlyUpdateDeviceStats, NULL, 0, 60 * 60 * 1000); schAdd(eepromHourlyUpdateDeviceStats, NULL, 0, 60 * 60 * 1000);
coloredMsg(LOG_GREEN, "eeI, hourly device stats update scheduled"); logMsg("eeI, hourly device stats update scheduled");
} }

View File

@ -118,15 +118,7 @@ int logMsg(const char *format, ...) {
return res; return res;
} }
int errMsg(const char *format, ...) { int coloredMsg(const t_logColor color, bool syslogToo, 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, ...) {
const static char POST[] = "\x1b[0m\r\n"; const static char POST[] = "\x1b[0m\r\n";
const static char HIGH[] = "\x1b[1m"; const static char HIGH[] = "\x1b[1m";
const static char RED[] = "\x1b[31;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_list vl;
va_start(vl, format); va_start(vl, format);
int res = innerLogMsg(pre, POST, false, format, vl); int res = innerLogMsg(pre, POST, syslogToo, format, vl);
va_end(vl); va_end(vl);
return res; return res;
} }

View File

@ -179,7 +179,7 @@ void scheduleMBusRequest(void *handle) {
if (devices[i].delay <= 0) { if (devices[i].delay <= 0) {
devices[i].delay = devices[i].period; devices[i].delay = devices[i].period;
devices[i].waiting = true; 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() { void my_setup_2() {
show(LED_RED, OFF); show(LED_RED, OFF);
show(LED_GREEN, ON); show(LED_GREEN, ON);
coloredMsg(LOG_BLUE, "Application starting"); logMsg("Application starting");
eepromInit(); eepromInit();

View File

@ -126,30 +126,30 @@ static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) {
int r = mbus_frame_data_parse(&reply, &frame_data); int r = mbus_frame_data_parse(&reply, &frame_data);
if (r == 0) { if (r == 0) {
mbus_data_variable *data_var = &(frame_data.data_var); 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)) { 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)) { 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)) { 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)) { 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)) { 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)) { 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)) { 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)) { 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; mbus_data_record *record;
int i; int i;
@ -159,7 +159,7 @@ static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) {
for (uint8_t j = 0; j < MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS; j++) { for (uint8_t j = 0; j < MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS; j++) {
if ((mbusCommHandle->device->consideredField[j].index == i) && if ((mbusCommHandle->device->consideredField[j].index == i) &&
(strlen(mbusCommHandle->device->consideredField[j].label) > 0)) { (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, i,
mbusCommHandle->device->consideredField[j].label, mbusCommHandle->device->consideredField[j].label,
mbus_data_record_unit(record), mbus_data_record_unit(record),
@ -169,14 +169,14 @@ static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) {
} }
mbus_data_record_free(data_var->record); mbus_data_record_free(data_var->record);
} else { } 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 handleRequestEngine(void *handle);
static void timeoutHandler(void *handle) { static void timeoutHandler(void *handle) {
errMsg("mbc timeout"); coloredMsg(LOG_RED, true, "mbc timeout");
t_mbusCommHandle *localMbusCommHandle = (t_mbusCommHandle*) handle; t_mbusCommHandle *localMbusCommHandle = (t_mbusCommHandle*) handle;
localMbusCommHandle->state = MBCS_TIMEOUT; localMbusCommHandle->state = MBCS_TIMEOUT;
localMbusCommHandle->receiving = false; localMbusCommHandle->receiving = false;
@ -196,11 +196,11 @@ static void handleRequestEngine(void *handle) {
switch (localMbusCommHandle->state) { switch (localMbusCommHandle->state) {
case MBCS_IDLE: case MBCS_IDLE:
logMsg("hre state IDLE"); coloredMsg(LOG_YELLOW, false, "hre state IDLE");
break; break;
case MBCS_SEND: case MBCS_SEND:
logMsg("hre state SEND"); coloredMsg(LOG_YELLOW, false, "hre state SEND");
localMbusCommHandle->sendBuf[0] = 0x10; localMbusCommHandle->sendBuf[0] = 0x10;
localMbusCommHandle->sendBuf[1] = localMbusCommHandle->cmd; localMbusCommHandle->sendBuf[1] = localMbusCommHandle->cmd;
localMbusCommHandle->sendBuf[2] = localMbusCommHandle->addr; localMbusCommHandle->sendBuf[2] = localMbusCommHandle->addr;
@ -210,13 +210,13 @@ static void handleRequestEngine(void *handle) {
// no break !! // no break !!
case MBCS_SEND_CONTINUED: case MBCS_SEND_CONTINUED:
logMsg("hre state SEND_CONTINUED"); coloredMsg(LOG_YELLOW, false, "hre state SEND_CONTINUED");
show(LED_RED, OFF); show(LED_RED, OFF);
if (! loopActive) { if (! loopActive) {
logMsg("hre enabling loop, try %d", localMbusCommHandle->retryCnt); coloredMsg(LOG_YELLOW, true, "hre enabling loop, try %d", localMbusCommHandle->retryCnt);
localMbusCommHandle->retryCnt++; localMbusCommHandle->retryCnt++;
loopEnable(); loopEnable();
schAdd(handleRequestEngine, handle, 10, 0); // give 10ms to settled the loop schAdd(handleRequestEngine, handle, 100, 0); // give 100ms to settled the loop
} else { } else {
localMbusCommHandle->retryCnt = 0; localMbusCommHandle->retryCnt = 0;
HAL_UART_Transmit_IT(&mbusUart, localMbusCommHandle->sendBuf, 5); HAL_UART_Transmit_IT(&mbusUart, localMbusCommHandle->sendBuf, 5);
@ -227,13 +227,13 @@ static void handleRequestEngine(void *handle) {
break; break;
case MBCS_SENDING_DONE: case MBCS_SENDING_DONE:
logMsg("hre state SENDING_DONE"); coloredMsg(LOG_YELLOW, false, "hre state SENDING_DONE");
localMbusCommHandle->state = MBCS_ENABLE_FRONTEND; localMbusCommHandle->state = MBCS_ENABLE_FRONTEND;
schAdd(handleRequestEngine, handle, 3, 0); schAdd(handleRequestEngine, handle, 3, 0);
break; break;
case MBCS_ENABLE_FRONTEND: case MBCS_ENABLE_FRONTEND:
logMsg("hre state ENABLE_FRONTEND"); coloredMsg(LOG_YELLOW, false, "hre state ENABLE_FRONTEND");
frontendEnable(); frontendEnable();
schAdd(timeoutHandler, handle, 2500, 0); schAdd(timeoutHandler, handle, 2500, 0);
calculatedChksum = 0; calculatedChksum = 0;
@ -243,12 +243,11 @@ static void handleRequestEngine(void *handle) {
break; break;
case MBCS_START1: case MBCS_START1:
//logMsg("hre state START1");
if (localMbusCommHandle->receivedOctet == 0x68) { if (localMbusCommHandle->receivedOctet == 0x68) {
localMbusCommHandle->frame.start1 = localMbusCommHandle->receivedOctet; localMbusCommHandle->frame.start1 = localMbusCommHandle->receivedOctet;
localMbusCommHandle->state = MBCS_LENGTH1; localMbusCommHandle->state = MBCS_LENGTH1;
} else { } 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->result = MBCR_ERROR_STATE_ENGINE__START1;
localMbusCommHandle->state = MBCS_ERROR; localMbusCommHandle->state = MBCS_ERROR;
} }
@ -256,16 +255,15 @@ static void handleRequestEngine(void *handle) {
break; break;
case MBCS_LENGTH1: case MBCS_LENGTH1:
//logMsg("hre state LENGTH1");
if (localMbusCommHandle->receivedOctet <= 3) { 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->result = MBCR_ERROR_STATE_ENGINE__LENGTH1;
localMbusCommHandle->state = MBCS_ERROR; localMbusCommHandle->state = MBCS_ERROR;
} else { } else {
localMbusCommHandle->frame.length1 = localMbusCommHandle->receivedOctet; localMbusCommHandle->frame.length1 = localMbusCommHandle->receivedOctet;
localMbusCommHandle->frame.userdata = (uint8_t*) malloc(localMbusCommHandle->frame.length1 - 3); localMbusCommHandle->frame.userdata = (uint8_t*) malloc(localMbusCommHandle->frame.length1 - 3);
if (! localMbusCommHandle->frame.userdata) { 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->result = MBCR_ERROR_OUT_OF_MEMORY__USERDATA;
localMbusCommHandle->state = MBCS_ERROR; localMbusCommHandle->state = MBCS_ERROR;
} else { } else {
@ -276,9 +274,8 @@ static void handleRequestEngine(void *handle) {
break; break;
case MBCS_LENGTH2: case MBCS_LENGTH2:
//logMsg("hre state LENGTH2");
if (localMbusCommHandle->frame.length1 != localMbusCommHandle->receivedOctet) { 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->result = MBCR_ERROR_STATE_ENGINE__LENGTH2;
localMbusCommHandle->state = MBCS_ERROR; localMbusCommHandle->state = MBCS_ERROR;
} else { } else {
@ -289,12 +286,11 @@ static void handleRequestEngine(void *handle) {
break; break;
case MBCS_START2: case MBCS_START2:
//logMsg("hre state START2");
if (localMbusCommHandle->receivedOctet == 0x68) { if (localMbusCommHandle->receivedOctet == 0x68) {
localMbusCommHandle->frame.start2 = localMbusCommHandle->receivedOctet; localMbusCommHandle->frame.start2 = localMbusCommHandle->receivedOctet;
localMbusCommHandle->state = MBCS_C_FIELD; localMbusCommHandle->state = MBCS_C_FIELD;
} else { } 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->result = MBCR_ERROR_STATE_ENGINE__START2;
localMbusCommHandle->state = MBCS_ERROR; localMbusCommHandle->state = MBCS_ERROR;
} }
@ -302,7 +298,6 @@ static void handleRequestEngine(void *handle) {
break; break;
case MBCS_C_FIELD: case MBCS_C_FIELD:
//logMsg("hre state C_FIELD");
localMbusCommHandle->frame.c = localMbusCommHandle->receivedOctet; localMbusCommHandle->frame.c = localMbusCommHandle->receivedOctet;
calculatedChksum += localMbusCommHandle->receivedOctet; calculatedChksum += localMbusCommHandle->receivedOctet;
localMbusCommHandle->state = MBCS_A_FIELD; localMbusCommHandle->state = MBCS_A_FIELD;
@ -310,7 +305,6 @@ static void handleRequestEngine(void *handle) {
break; break;
case MBCS_A_FIELD: case MBCS_A_FIELD:
//logMsg("hre state A_FIELD");
localMbusCommHandle->frame.a = localMbusCommHandle->receivedOctet; localMbusCommHandle->frame.a = localMbusCommHandle->receivedOctet;
calculatedChksum += localMbusCommHandle->receivedOctet; calculatedChksum += localMbusCommHandle->receivedOctet;
localMbusCommHandle->state = MBCS_CI_FIELD; localMbusCommHandle->state = MBCS_CI_FIELD;
@ -318,7 +312,6 @@ static void handleRequestEngine(void *handle) {
break; break;
case MBCS_CI_FIELD: case MBCS_CI_FIELD:
//logMsg("hre state CI_FIELD");
localMbusCommHandle->frame.ci = localMbusCommHandle->receivedOctet; localMbusCommHandle->frame.ci = localMbusCommHandle->receivedOctet;
calculatedChksum += localMbusCommHandle->receivedOctet; calculatedChksum += localMbusCommHandle->receivedOctet;
localMbusCommHandle->state = MBCS_USERDATA; localMbusCommHandle->state = MBCS_USERDATA;
@ -326,7 +319,6 @@ static void handleRequestEngine(void *handle) {
break; break;
case MBCS_USERDATA: case MBCS_USERDATA:
//logMsg("hre state USERDATA");
localMbusCommHandle->frame.userdata[userdataIdx] = localMbusCommHandle->receivedOctet; localMbusCommHandle->frame.userdata[userdataIdx] = localMbusCommHandle->receivedOctet;
calculatedChksum += localMbusCommHandle->receivedOctet; calculatedChksum += localMbusCommHandle->receivedOctet;
userdataIdx++; userdataIdx++;
@ -337,9 +329,8 @@ static void handleRequestEngine(void *handle) {
break; break;
case MBCS_CHKSUM: case MBCS_CHKSUM:
//logMsg("hre state CHKSUM");
if (localMbusCommHandle->receivedOctet != calculatedChksum) { 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->result = MBCR_ERROR_STATE_ENGINE__INVALID_CHKSUM;
localMbusCommHandle->state = MBCS_ERROR; localMbusCommHandle->state = MBCS_ERROR;
} else { } else {
@ -350,13 +341,12 @@ static void handleRequestEngine(void *handle) {
break; break;
case MBCS_STOP: case MBCS_STOP:
//logMsg("hre state STOP");
if (localMbusCommHandle->receivedOctet == 0x16) { if (localMbusCommHandle->receivedOctet == 0x16) {
localMbusCommHandle->frame.stop = localMbusCommHandle->receivedOctet; localMbusCommHandle->frame.stop = localMbusCommHandle->receivedOctet;
localMbusCommHandle->state = MBCS_DONE; localMbusCommHandle->state = MBCS_DONE;
schAdd(handleRequestEngine, handle, 0, 0); schAdd(handleRequestEngine, handle, 0, 0);
} else { } 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->result = MBCR_ERROR_STATE_ENGINE__STOP;
localMbusCommHandle->state = MBCS_ERROR; localMbusCommHandle->state = MBCS_ERROR;
receiveNext(localMbusCommHandle); receiveNext(localMbusCommHandle);
@ -364,7 +354,7 @@ static void handleRequestEngine(void *handle) {
break; break;
case MBCS_DONE: case MBCS_DONE:
logMsg("hre state DONE"); coloredMsg(LOG_YELLOW, false, "hre state DONE");
parseAndPrintFrame(localMbusCommHandle); parseAndPrintFrame(localMbusCommHandle);
if (localMbusCommHandle->frame.userdata != NULL) { if (localMbusCommHandle->frame.userdata != NULL) {
free(localMbusCommHandle->frame.userdata); free(localMbusCommHandle->frame.userdata);
@ -377,14 +367,14 @@ static void handleRequestEngine(void *handle) {
break; break;
case MBCS_ERROR: case MBCS_ERROR:
errMsg("hre state ERROR"); coloredMsg(LOG_RED, true, "hre state ERROR");
show(LED_RED, ON); 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); receiveNext(localMbusCommHandle);
break; break;
case MBCS_TIMEOUT: case MBCS_TIMEOUT:
errMsg("hre state TIMEOUT"); coloredMsg(LOG_RED, true, "hre state TIMEOUT");
mbusCommStats.errorCnt += 1; mbusCommStats.errorCnt += 1;
localMbusCommHandle->device->failures += 1; localMbusCommHandle->device->failures += 1;
localMbusCommHandle->receiving = false; localMbusCommHandle->receiving = false;
@ -396,11 +386,11 @@ static void handleRequestEngine(void *handle) {
uint8_t kitchenSink[16]; uint8_t kitchenSink[16];
memset(kitchenSink, 0, 16); memset(kitchenSink, 0, 16);
HAL_StatusTypeDef r = HAL_UART_Receive(&mbusUart, kitchenSink, 16, 100); 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 // no break
case MBCS_DISABLE_FRONTEND: case MBCS_DISABLE_FRONTEND:
logMsg("hre state DISABLE_FRONTEND"); coloredMsg(LOG_YELLOW, false, "hre state DISABLE_FRONTEND");
frontendDisable(); frontendDisable();
localMbusCommHandle->state = MBCS_IDLE; localMbusCommHandle->state = MBCS_IDLE;
break; break;
@ -421,7 +411,7 @@ void mbusCommRxCpltCallback(UART_HandleTypeDef *huart) {
schAdd(handleRequestEngine, (void*) &mbusCommHandle, 0, 0); schAdd(handleRequestEngine, (void*) &mbusCommHandle, 0, 0);
mbusCommHandle.receiving = false; mbusCommHandle.receiving = false;
} else { } 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 (mbusCommEnabled) {
if (mbusCommHandle.state == MBCS_IDLE) { 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->deviceName,
mbusDevice->requests, mbusDevice->requests,
mbusDevice->failures, mbusDevice->failures,
@ -455,7 +445,7 @@ e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice) {
} else { } else {
res = MBCRR_DISABLED; res = MBCRR_DISABLED;
} }
return res; return res;
} }

View File

@ -32,10 +32,10 @@ static uint32_t watchdogCounter = 0;
void watchdogHandler(void *handle) { void watchdogHandler(void *handle) {
if (watchdogCounter > 0) { if (watchdogCounter > 0) {
coloredMsg(LOG_GREEN, "Watchdog received in between"); coloredMsg(LOG_GREEN, false, "Watchdog received in between");
watchdogCounter = 0; watchdogCounter = 0;
} else { } 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 // boot the system
} }
} }
@ -45,18 +45,18 @@ static void mqttCallback(char *topic, uint8_t *payload, uint16_t payloadLength)
if (0 == strcmp(topic, WatchdogTopic)) { if (0 == strcmp(topic, WatchdogTopic)) {
watchdogCounter++; watchdogCounter++;
} else { } else {
logMsg("mqcb: %s : %.*s", topic, payloadLength, payload); coloredMsg(LOG_GREEN, false, "mqcb: %s : %.*s", topic, payloadLength, payload);
} }
} }
static void mqttStatusPublisher(void *handle) { static void mqttStatusPublisher(void *handle) {
logMsg("mqsp: publishing status"); coloredMsg(LOG_GREEN, false, "mqsp: publishing status");
t_mbusCommStats *mbusCommStats = mbusCommGetStats(); t_mbusCommStats *mbusCommStats = mbusCommGetStats();
char buf[64]; char buf[64];
snprintf(buf, 128, "{\"uptime\":\"%ld\", \"tasks\":\"%d\", \"requests\":\"%d\", \"errors\":\"%d\"}", snprintf(buf, 128, "{\"uptime\":\"%ld\", \"tasks\":\"%d\", \"requests\":\"%d\", \"errors\":\"%d\"}",
HAL_GetTick()/1000, schTaskCnt(), mbusCommStats->requestCnt, mbusCommStats->errorCnt); HAL_GetTick()/1000, schTaskCnt(), mbusCommStats->requestCnt, mbusCommStats->errorCnt);
bool res = publish(&mqttClient, StatusTopic, (const char*)buf, strlen(buf), false); 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()) { if (isNetworkAvailable()) {
switch (state) { switch (state) {
case 0: case 0:
coloredMsg(LOG_YELLOW, "mqch, initializing mqtt client"); coloredMsg(LOG_GREEN, false, "mqch, initializing mqtt client");
client.sockNum = MQTT_SOCK; client.sockNum = MQTT_SOCK;
mqttClientInit(&mqttClient, &client, mqttCallback); mqttClientInit(&mqttClient, &client, mqttCallback);
coloredMsg(LOG_YELLOW, "mqch: mqtt client initialized"); coloredMsg(LOG_GREEN, false, "mqch: mqtt client initialized");
state = 1; state = 1;
break; break;
case 1: 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); 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) { if (res) {
coloredMsg(LOG_YELLOW, "mqch, ok, connected"); coloredMsg(LOG_GREEN, false, "mqch, ok, connected");
state = 2; state = 2;
} else { } else {
state = 255; state = 255;
@ -89,40 +89,39 @@ void mqttCommHandler(void *handle) {
break; break;
case 2: 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); 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); schAdd(mqttStatusPublisher, NULL, 0, 60000);
coloredMsg(LOG_YELLOW, "mqch, status publisher scheduled"); coloredMsg(LOG_GREEN, false, "mqch, status publisher scheduled");
state = 3; state = 3;
break; break;
case 3: case 3:
coloredMsg(LOG_YELLOW, "mqch, subscribe watchdog"); coloredMsg(LOG_GREEN, false, "mqch, subscribe watchdog");
res = subscribe(&mqttClient, WatchdogTopic, MQTTQOS0); 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); schAdd(watchdogHandler, NULL, 60000, 60000);
coloredMsg(LOG_YELLOW, "mqch, watchdogHandler scheduled"); coloredMsg(LOG_GREEN, false, "mqch, watchdogHandler scheduled");
state = 4; state = 4;
break; break;
case 4: case 4:
coloredMsg(LOG_YELLOW, "mqch, now entering the loop"); coloredMsg(LOG_GREEN, false, "mqch, now entering the loop");
state = 5; state = 5;
break; break;
case 5: case 5:
// coloredMsg(LOG_YELLOW, "mqch, looping");
if (! mqttLoop(&mqttClient)) { if (! mqttLoop(&mqttClient)) {
state = 0; state = 0;
} }
break; break;
case 255: 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(mqttCommHandler, NULL);
schDel(watchdogHandler, 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); schAdd(mqttCommHandler, NULL, 60000, 100);
break; break;
} }
@ -136,8 +135,8 @@ void mqttCommInit() {
void mqttPublish(char *topic, char *message) { void mqttPublish(char *topic, char *message) {
bool res = publish(&mqttClient, topic, message, strlen(message), false); bool res = publish(&mqttClient, topic, message, strlen(message), false);
if (res) { if (res) {
coloredMsg(LOG_YELLOW, "mqp: %s -> %s successfully published", message, topic); coloredMsg(LOG_GREEN, false, "mqp: %s -> %s successfully published", message, topic);
} else { } else {
coloredMsg(LOG_RED, "mqp: %s -> %s failed to publish", message, topic); coloredMsg(LOG_RED, true, "mqp: %s -> %s failed to publish", message, topic);
} }
} }

View File

@ -39,23 +39,19 @@ static void wiz_cs_deselect(void) {
static uint8_t wiz_spi_readbyte(void) { static uint8_t wiz_spi_readbyte(void) {
uint8_t rbuf; uint8_t rbuf;
HAL_SPI_Receive(&etherSpi, &rbuf, 1, HAL_MAX_DELAY); HAL_SPI_Receive(&etherSpi, &rbuf, 1, HAL_MAX_DELAY);
// coloredMsg(LOG_YELLOW, "R: %02x", rbuf);
return rbuf; return rbuf;
} }
static void wiz_spi_writebyte(uint8_t wb) { static void wiz_spi_writebyte(uint8_t wb) {
// coloredMsg(LOG_YELLOW, "W: %02x", wb);
HAL_SPI_Transmit(&etherSpi, &wb, 1, HAL_MAX_DELAY); HAL_SPI_Transmit(&etherSpi, &wb, 1, HAL_MAX_DELAY);
} }
static void wiz_spi_readburst(uint8_t* pBuf, uint16_t len) { static void wiz_spi_readburst(uint8_t* pBuf, uint16_t len) {
HAL_SPI_Receive(&etherSpi, pBuf, len, HAL_MAX_DELAY); HAL_SPI_Receive(&etherSpi, 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) { static void wiz_spi_writeburst(uint8_t* pBuf, uint16_t len) {
HAL_SPI_Transmit(&etherSpi, pBuf, len, HAL_MAX_DELAY); HAL_SPI_Transmit(&etherSpi, 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) { static void wizReset(bool b) {
@ -64,36 +60,36 @@ static void wizReset(bool b) {
static void wizDHCPAssign() { static void wizDHCPAssign() {
coloredMsg(LOG_GREEN, "wizda"); coloredMsg(LOG_BLUE, false, "wizda");
getIPfromDHCP(netInfo.ip); 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); 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); 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); 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); wizchip_setnetinfo(&netInfo);
coloredMsg(LOG_GREEN, "wizda, set netinfo again"); coloredMsg(LOG_BLUE, false, "wizda, set netinfo again");
networkAvailable = true; networkAvailable = true;
coloredMsg(LOG_GREEN, "wizda, network is available"); coloredMsg(LOG_BLUE, false, "wizda, network is available");
} }
static void wizDHCPUpdate() { static void wizDHCPUpdate() {
coloredMsg(LOG_YELLOW, "wizdu"); coloredMsg(LOG_BLUE, false, "wizdu");
getIPfromDHCP(netInfo.ip); 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); 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); 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); 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); wizchip_setnetinfo(&netInfo);
coloredMsg(LOG_YELLOW, "wizdu, netinfo updated"); coloredMsg(LOG_BLUE, false, "wizdu, netinfo updated");
} }
static void wizDHCPHandler(void *handle) { static void wizDHCPHandler(void *handle) {
@ -102,7 +98,7 @@ static void wizDHCPHandler(void *handle) {
uint8_t res = DHCP_run(); uint8_t res = DHCP_run();
if (lastDhcpRes != res) { 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; lastDhcpRes = res;
} }
} }
@ -114,7 +110,7 @@ static void wizPhyLinkHandler(void *handle) {
uint8_t phyLink = 0; uint8_t phyLink = 0;
int8_t res = ctlwizchip(CW_GET_PHYLINK, (void*) &phyLink); int8_t res = ctlwizchip(CW_GET_PHYLINK, (void*) &phyLink);
if (lastStablePhyLink != 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; lastStablePhyLink = phyLink;
if (phyLink == PHY_LINK_ON) { if (phyLink == PHY_LINK_ON) {
@ -122,24 +118,24 @@ static void wizPhyLinkHandler(void *handle) {
memset(dhcpBuffer, 0, DHCP_BUFFER_SIZE); memset(dhcpBuffer, 0, DHCP_BUFFER_SIZE);
reg_dhcp_cbfunc(wizDHCPAssign, wizDHCPUpdate, NULL); reg_dhcp_cbfunc(wizDHCPAssign, wizDHCPUpdate, NULL);
DHCP_init(DHCP_SOCK, dhcpBuffer); 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 // run the dhcp handler the first time after 1s and then every 100ms
schAdd(wizDHCPHandler, NULL, 1000, 1000); schAdd(wizDHCPHandler, NULL, 1000, 1000);
coloredMsg(LOG_RED, "wizplh, DHCP handler scheduled"); coloredMsg(LOG_BLUE, false, "wizplh, DHCP handler scheduled");
dhcpInitialized = true; dhcpInitialized = true;
} else { } else {
networkAvailable = false; networkAvailable = false;
coloredMsg(LOG_RED, "wizplh, network is unavailable"); coloredMsg(LOG_BLUE, false, "wizplh, network is unavailable");
// stop DHCP handler // stop DHCP handler
if (dhcpInitialized) { if (dhcpInitialized) {
DHCP_stop(); DHCP_stop();
coloredMsg(LOG_RED, "wizplh, DHCP stopped"); coloredMsg(LOG_BLUE, false, "wizplh, DHCP stopped");
schDel(wizDHCPHandler, NULL); schDel(wizDHCPHandler, NULL);
coloredMsg(LOG_RED, "wizplh, DHCP handler unscheduled"); coloredMsg(LOG_BLUE, false, "wizplh, DHCP handler unscheduled");
dhcpInitialized = false; dhcpInitialized = false;
} }
@ -148,29 +144,29 @@ static void wizPhyLinkHandler(void *handle) {
} }
int wizInit() { int wizInit() {
coloredMsg(LOG_RED, "wizI, resetting Ethernet module"); coloredMsg(LOG_BLUE, false, "wizI, resetting Ethernet module");
wizReset(true); wizReset(true);
activeDelay(2); activeDelay(2);
wizReset(false); wizReset(false);
activeDelay(50); 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); 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); 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); 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 }; uint8_t bufSizes[] = { 2, 2, 2, 2, 2, 2, 2, 2 };
int8_t res = wizchip_init(bufSizes, bufSizes); 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(); wizphy_reset();
activeDelay(5); activeDelay(5);
coloredMsg(LOG_RED, "wizI, reset phy"); coloredMsg(LOG_BLUE, false, "wizI, reset phy");
wiz_PhyConf wpc; wiz_PhyConf wpc;
wpc.mode = PHY_MODE_MANUAL; wpc.mode = PHY_MODE_MANUAL;
@ -178,21 +174,21 @@ int wizInit() {
wpc.duplex = PHY_DUPLEX_FULL; wpc.duplex = PHY_DUPLEX_FULL;
wizphy_setphyconf(&wpc); wizphy_setphyconf(&wpc);
activeDelay(5); activeDelay(5);
coloredMsg(LOG_RED, "wizI, phy config set"); coloredMsg(LOG_BLUE, false, "wizI, phy config set");
wizchip_setnetinfo(&netInfo); 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 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]; uint8_t buf[6];
res = ctlwizchip(CW_GET_ID, (void*) buf); 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); schAdd(wizPhyLinkHandler, NULL, 0, 1000);
coloredMsg(LOG_RED, "wizI, PhyLink handler scheduled"); coloredMsg(LOG_BLUE, false, "wizI, PhyLink handler scheduled");
return 0; return 0;
} }