Compare commits
57 Commits
transmit_b
...
configurat
Author | SHA1 | Date | |
---|---|---|---|
e2384d01ec
|
|||
fe1b2e5b40
|
|||
765c3b077c
|
|||
f019f7eb77
|
|||
0ed710e652
|
|||
24f2d0a01d
|
|||
78d32a7cc9
|
|||
a14928b035
|
|||
11265fc979
|
|||
fe694e75d2
|
|||
bde9f148a0
|
|||
77141319ea
|
|||
b4170ec6dc
|
|||
6a2646b93c
|
|||
243c487546
|
|||
7d03900ada
|
|||
98c0a9d7a2
|
|||
f22c821ca3
|
|||
76ff0d8e24 | |||
518abe4e1c
|
|||
bc15e23f10
|
|||
2c392cce53
|
|||
a26a2878f4
|
|||
6e96a27141
|
|||
a43e42a99f
|
|||
d2c5943014
|
|||
f409d3fb22
|
|||
ba6ac21069
|
|||
39ca9d963b
|
|||
4e3f086fa0
|
|||
44523a4ed3
|
|||
9a7e8ee297
|
|||
eaf658de51
|
|||
9e99d5510d
|
|||
159750bcf6
|
|||
aaad534d21
|
|||
e0c3394725
|
|||
638171b494
|
|||
d4b00cf391
|
|||
544163f4a3
|
|||
6d7119c0e2
|
|||
cb5dd5f79a
|
|||
9223cbda73
|
|||
975ecf40f9
|
|||
0b1c6217c4
|
|||
3c25a7d9e1
|
|||
b4ef9d29f9
|
|||
7aded82145
|
|||
8894036679
|
|||
3e1a7946c2
|
|||
4e04ad6567
|
|||
cbc3ca6f20 | |||
73c1360978 | |||
56ac741079
|
|||
7a12bba4df
|
|||
397c3d2a56
|
|||
f5f8aa0660
|
@ -37,7 +37,7 @@ BUILD_DIR = build
|
||||
######################################
|
||||
# C sources
|
||||
C_SOURCES = \
|
||||
User/Src/oled.c User/Src/cmdHandler.c User/Src/eeprom.c User/Src/frontend.c User/Src/logger.c User/Src/loopCtrl.c User/Src/main2.c User/Src/mbusComm.c User/Src/mbusParserExt.c User/Src/mqttComm.c User/Src/ports.c User/Src/ringbuffer.c User/Src/show.c User/Src/utils.c User/Src/wizHelper.c hottislib/PontCoopScheduler.c \
|
||||
User/Src/regularCmds.c User/Src/adminCmds.c User/Src/configCmds.c User/Src/config.c User/Src/oled.c User/Src/cmdHandler.c User/Src/eeprom.c User/Src/frontend.c User/Src/logger.c User/Src/loopCtrl.c User/Src/main2.c User/Src/mbusComm.c User/Src/mbusParserExt.c User/Src/mqttComm.c User/Src/ports.c User/Src/ringbuffer.c User/Src/show.c User/Src/utils.c User/Src/wizHelper.c hottislib/PontCoopScheduler.c \
|
||||
libmbus/mbus/mbus-protocol.c \
|
||||
Core/Src/main.c \
|
||||
Core/Src/gpio.c \
|
||||
@ -125,6 +125,7 @@ C_INCLUDES = \
|
||||
-Ilibmbus \
|
||||
-IUser/Inc \
|
||||
-IioLibrary_Driver/Internet/DHCP \
|
||||
-IioLibrary_Driver/Internet/DNS \
|
||||
-IioLibrary_Driver/Ethernet \
|
||||
-ICore/Inc \
|
||||
-IDrivers/STM32F1xx_HAL_Driver/Inc \
|
||||
|
23
cube/User/Inc/cmdHelper.h
Normal file
23
cube/User/Inc/cmdHelper.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef _CMDHELPER_H_
|
||||
#define _CMDHELPER_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
typedef bool (*cmdFunc_t)(uint8_t argc, char **args);
|
||||
|
||||
typedef struct {
|
||||
char name[16];
|
||||
char help[512];
|
||||
cmdFunc_t cmdFunc;
|
||||
} cmd_t;
|
||||
|
||||
void sendString(const char *buf);
|
||||
bool sendFormatString(const char *format, ...);
|
||||
|
||||
const cmd_t *getRegularCommands();
|
||||
const cmd_t *getAdminCommands();
|
||||
const cmd_t *getConfigCommands();
|
||||
|
||||
#endif /* _CMDHELPER_H_ */
|
42
cube/User/Inc/config.h
Normal file
42
cube/User/Inc/config.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef _CONFIG_H_
|
||||
#define _CONFIG_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <spi.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define CONFIG_MAGIC 0xdead000a
|
||||
#define DEVICE_MAGIC 0xaffe0000
|
||||
|
||||
typedef struct __attribute__((__packed__)) s_configBlock {
|
||||
uint32_t configMagic;
|
||||
char deviceName[16];
|
||||
uint8_t macAddress[6];
|
||||
int32_t frontendThreshold;
|
||||
char brokerName[64];
|
||||
char watchdogTopic[64];
|
||||
char startupTopic[64];
|
||||
char statusTopic[64];
|
||||
char mbusDataTopic[64];
|
||||
char syslogServerName[64];
|
||||
uint8_t numOfDeviceBlocks;
|
||||
uint8_t filler[1];
|
||||
} t_configBlock;
|
||||
|
||||
|
||||
#define MBUSDEVICE_NAMELENGTH 16
|
||||
#define MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS 4
|
||||
|
||||
typedef struct __attribute__((__packed__)) s_deviceBlock {
|
||||
uint32_t deviceMagic;
|
||||
char deviceName[MBUSDEVICE_NAMELENGTH];
|
||||
uint8_t address;
|
||||
int8_t consideredField[MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS];
|
||||
int32_t period;
|
||||
uint8_t filler[3];
|
||||
} t_deviceBlock;
|
||||
|
||||
void configInit();
|
||||
t_configBlock* getConfig();
|
||||
|
||||
#endif /* _CONFIG_H_ */
|
@ -3,19 +3,48 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <spi.h>
|
||||
#include <config.h>
|
||||
#include <assert.h>
|
||||
|
||||
|
||||
|
||||
#define EEPROM_WRITE_BLOCK_SIZE 32
|
||||
#define EEPROM_AFTER_WRITE_DELAY 7
|
||||
|
||||
|
||||
typedef struct __attribute__((__packed__)) s_eepromHeader {
|
||||
uint32_t magic;
|
||||
uint32_t writeCounter;
|
||||
} t_eepromHeader;
|
||||
static_assert((sizeof(t_eepromHeader) <= EEPROM_WRITE_BLOCK_SIZE), "t_eepromHeader has illegal size, must be less than or equal 32");
|
||||
|
||||
typedef struct __attribute__((__packed__)) s_deviceStats {
|
||||
uint32_t totalRunningHours;
|
||||
uint32_t totalPowercycles;
|
||||
uint32_t totalRequests;
|
||||
uint32_t totalFailures;
|
||||
} t_deviceStats;
|
||||
static_assert((sizeof(t_deviceStats) <= EEPROM_WRITE_BLOCK_SIZE), "t_deviceStats has illegal size, must be less than or equal 32");
|
||||
|
||||
static_assert((sizeof(t_configBlock) % 32 == 0), "t_configBlock has illegal size, must be dividable by 32");
|
||||
static_assert((sizeof(t_deviceBlock) % 32 == 0), "t_deviceBlock has illegal size, must be dividable by 32");
|
||||
|
||||
|
||||
#define EEPROM_BASE_ADDR 0
|
||||
#define EEPROM_DEVICE_STATS_ADDR 32
|
||||
#define EEPROM_CONFIG_BLOCK_ADDR 64
|
||||
#define EEPROM_DEVICE_BLOCK_BASE_ADDR (EEPROM_CONFIG_BLOCK_ADDR + sizeof(t_configBlock))
|
||||
|
||||
|
||||
|
||||
void eepromInit();
|
||||
void eepromWrite(uint16_t addr, uint8_t *buf, uint8_t len);
|
||||
void eepromRead(uint16_t addr, uint8_t *buf, uint8_t len);
|
||||
void eepromSpiTxCpltCallback(SPI_HandleTypeDef *hspi);
|
||||
t_deviceStats* getGlobalDeviceStats();
|
||||
void eepromReadConfigBlock(t_configBlock *destConfigBlock);
|
||||
void eepromWriteConfigBlock(t_configBlock *srcConfigBlock);
|
||||
void eepromReadDeviceBlock(uint8_t blockNum, t_deviceBlock *destDeviceBlock);
|
||||
void eepromWriteDeviceBlock(uint8_t blockNum, t_deviceBlock *srcDeviceBlock);
|
||||
|
||||
#endif /* EEPROM_H_ */
|
||||
|
@ -9,6 +9,5 @@ void frontendInit();
|
||||
void frontendAdcCallback(ADC_HandleTypeDef* hadc);
|
||||
void frontendEnable();
|
||||
void frontendDisable();
|
||||
void frontendSetThreshold(int32_t threshold);
|
||||
|
||||
#endif // _FRONTEND_H_
|
@ -4,10 +4,9 @@
|
||||
#include <main.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <config.h>
|
||||
|
||||
|
||||
#define MBUSDEVICE_NAMELENGTH 24
|
||||
#define MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS 4
|
||||
typedef struct {
|
||||
char deviceName[MBUSDEVICE_NAMELENGTH];
|
||||
uint8_t address;
|
||||
@ -33,6 +32,7 @@ typedef struct {
|
||||
uint32_t uartOverrunCnt;
|
||||
uint32_t uartFramingErrCnt;
|
||||
uint32_t uartParityErrCnt;
|
||||
uint32_t uartNoiseErrCnt;
|
||||
} t_mbusCommStats;
|
||||
|
||||
// e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice);
|
||||
@ -44,5 +44,7 @@ void mbusCommRxCpltCallback(UART_HandleTypeDef *huart);
|
||||
void mbusCommErrorCallback(UART_HandleTypeDef *huart);
|
||||
void mbusCommSetStats(t_mbusCommStats stats);
|
||||
t_mbusCommStats *mbusCommGetStats();
|
||||
void mbusCommAddDevice(t_deviceBlock *deviceBlock);
|
||||
|
||||
|
||||
#endif // _MBUSCOMM_H_
|
||||
|
@ -4,9 +4,10 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
int wizInit();
|
||||
bool isNetworkAvailable();
|
||||
uint8_t* wizGetIPAddress();
|
||||
|
||||
bool wizDnsQuery(char *name, uint8_t *ip);
|
||||
|
||||
#endif // _WIZHELPER_H_
|
81
cube/User/Src/adminCmds.c
Normal file
81
cube/User/Src/adminCmds.c
Normal file
@ -0,0 +1,81 @@
|
||||
#include <cmdHelper.h>
|
||||
#include <logger.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <mbusComm.h>
|
||||
#include <loopCtrl.h>
|
||||
|
||||
|
||||
// clear statistics
|
||||
static bool clearCmd(uint8_t argc, char **args) {
|
||||
t_mbusCommStats zeroedStats = { .mbusRequestCnt = 0, .mbusErrorCnt = 0, .uartOctetCnt = 0, .uartOverrunCnt = 0, .uartFramingErrCnt = 0, .uartParityErrCnt = 0, .uartNoiseErrCnt = 0 };
|
||||
mbusCommSetStats(zeroedStats);
|
||||
coloredMsg(LOG_YELLOW, true, "ch cc global statistics cleared");
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool mbusCommEnableCmd(uint8_t argc, char **args) {
|
||||
bool retCode = true;
|
||||
if (argc == 2) {
|
||||
if (0 == strcmp("false", args[1])) {
|
||||
mbusCommEnable(false);
|
||||
coloredMsg(LOG_YELLOW, true, "ch mcec Meterbus communication disabled");
|
||||
} else if (0 == strcmp("true", args[1])) {
|
||||
mbusCommEnable(true);
|
||||
coloredMsg(LOG_YELLOW, true, "ch mcec Meterbus communication enabled");
|
||||
} else {
|
||||
retCode = false;
|
||||
}
|
||||
} else {
|
||||
retCode = false;
|
||||
}
|
||||
return retCode;
|
||||
}
|
||||
|
||||
static bool loopEnableCmd(uint8_t argc, char **args) {
|
||||
bool retCode = true;
|
||||
if (argc == 2) {
|
||||
if (0 == strcmp("false", args[1])) {
|
||||
loopDisable();
|
||||
coloredMsg(LOG_YELLOW, true, "ch lec loop disabled");
|
||||
} else if (0 == strcmp("true", args[1])) {
|
||||
loopEnable();
|
||||
coloredMsg(LOG_YELLOW, true, "ch lec loop enabled");
|
||||
} else {
|
||||
retCode = false;
|
||||
}
|
||||
} else {
|
||||
retCode = false;
|
||||
}
|
||||
return retCode;
|
||||
}
|
||||
|
||||
|
||||
|
||||
const cmd_t ADMIN_COMMANDS[] = {
|
||||
{ .name = "clear", .cmdFunc = clearCmd,
|
||||
.help = \
|
||||
"clear ................................ Clears the global Meterbus\n\r" \
|
||||
" statistics\n\r"
|
||||
},
|
||||
{ .name = "mbusCommEnable", .cmdFunc = mbusCommEnableCmd,
|
||||
.help = \
|
||||
"mbusCommEnable true|false ............ Enables or disables the Meterbus\n\r" \
|
||||
" communication\n\r"
|
||||
},
|
||||
{ .name = "loopEnable", .cmdFunc = loopEnableCmd,
|
||||
.help = \
|
||||
"loopEnable true|false ................ Enables or disables the loop.\n\r" \
|
||||
" Disable Meterbus communication\n\r" \
|
||||
" first if you want to disable the\n\r" \
|
||||
" for a longer time, otherwise the\n\r" \
|
||||
" request will enable it again\n\r"
|
||||
},
|
||||
{ .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL }
|
||||
};
|
||||
|
||||
|
||||
|
||||
const cmd_t *getAdminCommands() {
|
||||
return ADMIN_COMMANDS;
|
||||
}
|
@ -1,19 +1,18 @@
|
||||
#include <cmdHandler.h>
|
||||
#include <cmdHelper.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <socket.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include <PontCoopScheduler.h>
|
||||
#include <wizHelper.h>
|
||||
#include <mbusComm.h>
|
||||
#include <loopCtrl.h>
|
||||
|
||||
|
||||
extern const uint8_t CMD_SOCK;
|
||||
|
||||
@ -31,133 +30,65 @@ typedef enum {
|
||||
CH_ERROR
|
||||
} chState_t;
|
||||
|
||||
|
||||
typedef bool (*cmdFunc_t)(uint8_t argc, char **args);
|
||||
|
||||
typedef struct {
|
||||
bool requiredConfigMode;
|
||||
char name[16];
|
||||
char help[512];
|
||||
cmdFunc_t cmdFunc;
|
||||
} cmd_t;
|
||||
|
||||
|
||||
// clear statistics
|
||||
bool clearCmd(uint8_t argc, char **args) {
|
||||
t_mbusCommStats zeroedStats = { .mbusRequestCnt = 0, .mbusErrorCnt = 0, .uartOctetCnt = 0, .uartOverrunCnt = 0, .uartFramingErrCnt = 0, .uartParityErrCnt = 0 };
|
||||
mbusCommSetStats(zeroedStats);
|
||||
coloredMsg(LOG_YELLOW, true, "ch cc global statistics cleared");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool globalStatsCmd(uint8_t argc, char **args) {
|
||||
t_mbusCommStats *stats = mbusCommGetStats();
|
||||
char buf[256];
|
||||
sprintf(buf, \
|
||||
"Global statistics\n\r" \
|
||||
" Meterbus Requests: %ld\n\r" \
|
||||
" Meterbus Errors: %ld\n\r" \
|
||||
" UART Octets: %ld\n\r" \
|
||||
" UART Overruns: %ld\n\r" \
|
||||
" UART Framing Errs: %ld\n\r" \
|
||||
" UART Parity Errs: %ld\n\r",
|
||||
stats->mbusRequestCnt, stats->mbusErrorCnt,
|
||||
stats->uartOctetCnt, stats->uartOverrunCnt, stats->uartFramingErrCnt, stats->uartParityErrCnt
|
||||
);
|
||||
void sendString(const char *buf) {
|
||||
send(CMD_SOCK, (uint8_t*)buf, strlen(buf));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mbusCommEnableCmd(uint8_t argc, char **args) {
|
||||
bool sendFormatString(const char *format, ...) {
|
||||
bool retCode = true;
|
||||
if (argc == 2) {
|
||||
if (0 == strcmp("false", args[1])) {
|
||||
mbusCommEnable(false);
|
||||
coloredMsg(LOG_YELLOW, true, "ch mcec Meterbus communication disabled");
|
||||
} else if (0 == strcmp("true", args[1])) {
|
||||
mbusCommEnable(true);
|
||||
coloredMsg(LOG_YELLOW, true, "ch mcec Meterbus communication enabled");
|
||||
} else {
|
||||
retCode = false;
|
||||
}
|
||||
} else {
|
||||
retCode = false;
|
||||
}
|
||||
va_list vl;
|
||||
va_start(vl, format);
|
||||
char buf[4096];
|
||||
int vcnt = vsnprintf(buf, sizeof(buf), format, vl);
|
||||
retCode = (vcnt < sizeof(buf));
|
||||
va_end(vl);
|
||||
sendString(buf);
|
||||
return retCode;
|
||||
}
|
||||
|
||||
bool loopEnableCmd(uint8_t argc, char **args) {
|
||||
bool retCode = true;
|
||||
if (argc == 2) {
|
||||
if (0 == strcmp("false", args[1])) {
|
||||
loopDisable();
|
||||
coloredMsg(LOG_YELLOW, true, "ch lec loop disabled");
|
||||
} else if (0 == strcmp("true", args[1])) {
|
||||
loopEnable();
|
||||
coloredMsg(LOG_YELLOW, true, "ch lec loop enabled");
|
||||
} else {
|
||||
retCode = false;
|
||||
}
|
||||
} else {
|
||||
retCode = false;
|
||||
}
|
||||
return retCode;
|
||||
}
|
||||
|
||||
const static cmd_t COMMANDS[] = {
|
||||
{ .requiredConfigMode = true, .name = "clear", .cmdFunc = clearCmd,
|
||||
.help = \
|
||||
"clear ................................ Clears the global Meterbus\n\r" \
|
||||
" statistics\n\r" \
|
||||
" Required configuration mode\n\r"
|
||||
},
|
||||
{ .requiredConfigMode = true, .name = "mbusCommEnable", .cmdFunc = mbusCommEnableCmd,
|
||||
.help = \
|
||||
"mbusCommEnable true|false ............ Enables or disables the Meterbus\n\r" \
|
||||
" communication\n\r"
|
||||
},
|
||||
{ .requiredConfigMode = true, .name = "loopEnable", .cmdFunc = loopEnableCmd,
|
||||
.help = \
|
||||
"loopEnable true|false ................ Enables or disables the loop.\n\r" \
|
||||
" Disable Meterbus communication\n\r" \
|
||||
" first if you want to disable the\n\r" \
|
||||
" for a longer time, otherwise the\n\r" \
|
||||
" request will enable it again\n\r"
|
||||
},
|
||||
{ .requiredConfigMode = false, .name = "globalStats", .cmdFunc = globalStatsCmd,
|
||||
.help = \
|
||||
"globalStats .......................... Show the global statistics\n\r" \
|
||||
" counters requestCnt and errorCnt\n\r"
|
||||
},
|
||||
{ .requiredConfigMode = false, .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL }
|
||||
};
|
||||
|
||||
|
||||
// returns 0 to continue waiting for input
|
||||
// returns -1 to close the connection
|
||||
// returns 1 to toggle to config mode
|
||||
// returns 2 to toggle back to default mode
|
||||
int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetConfigMode) {
|
||||
// returns 1 to toggle to admin mode
|
||||
// returns 2 to toggle to config mode
|
||||
// returns 3 to toggle back to default mode
|
||||
static int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetSpecialModes) {
|
||||
const static char HELP_MSG[] = \
|
||||
"Usage\n\r" \
|
||||
"\n\r" \
|
||||
"help ................................. Show this help page\n\r" \
|
||||
"quit ................................. Terminate the console session\n\r" \
|
||||
"enable ............................... Enable configuration mode\n\r" \
|
||||
"disable .............................. Disable configuration mode\n\r" \
|
||||
"enable ............................... Enable admin mode\n\r" \
|
||||
"config ............................... Enter configuration mode\n\r" \
|
||||
"disable .............................. Disable admin/config mode\n\r" \
|
||||
;
|
||||
const static char CONFIG_INTRO_MSG[] = \
|
||||
"In configuration mode each command changing the configuration\n\r" \
|
||||
"will save changes directly to the EEPROM.\n\r" \
|
||||
"However, the system will only consider these changes after a\n\r" \
|
||||
"restart since only in this sitution the EEPROM is read.\n\r" \
|
||||
"\n\r" \
|
||||
;
|
||||
const static char GOODBYE_MSG[] = "Good bye\n\r";
|
||||
const static char OK_MSG[] = "OK\n\r";
|
||||
const static char FAILED_MSG[] = "Failed\n\r";
|
||||
const static char REQUIRES_CONFIG_MODE_MGS[] = "Not executed, requires config mode\n\r";
|
||||
const static char UNKNOWN_COMMAND[] = "Unknown command\n\r";
|
||||
uint8_t *messageToSend = NULL;
|
||||
|
||||
static bool adminMode = false;
|
||||
static bool configMode = false;
|
||||
|
||||
if (resetConfigMode) {
|
||||
if (resetSpecialModes) {
|
||||
adminMode = false;
|
||||
configMode = false;
|
||||
}
|
||||
|
||||
cmd_t const * commands = getRegularCommands();
|
||||
if (adminMode) {
|
||||
commands = getAdminCommands();
|
||||
} else if (configMode) {
|
||||
commands = getConfigCommands();
|
||||
}
|
||||
|
||||
coloredMsg(LOG_YELLOW, false, "ch cec cmdLine is %s", cmdLine);;
|
||||
|
||||
#define MAX_NUM_OF_ARGS 8
|
||||
@ -180,38 +111,44 @@ int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetConfigMode) {
|
||||
messageToSend = (uint8_t*)GOODBYE_MSG;
|
||||
retCode = -1;
|
||||
} else if (0 == strcmp(cmd, "help")) {
|
||||
send(CMD_SOCK, (uint8_t*)HELP_MSG, strlen(HELP_MSG));
|
||||
if (configMode) {
|
||||
sendString(CONFIG_INTRO_MSG);
|
||||
}
|
||||
sendString(HELP_MSG);
|
||||
uint8_t cmdIdx = 0;
|
||||
while (true) {
|
||||
cmd_t command = COMMANDS[cmdIdx];
|
||||
cmd_t command = commands[cmdIdx];
|
||||
if (0 == strcmp("END_OF_CMDS", command.name)) {
|
||||
break;
|
||||
}
|
||||
send(CMD_SOCK, (uint8_t*)command.help, strlen(command.help));
|
||||
sendString(command.help);
|
||||
cmdIdx++;
|
||||
}
|
||||
messageToSend = NULL;
|
||||
} else if (0 == strcmp(cmd, "enable")) {
|
||||
coloredMsg(LOG_YELLOW, true, "ch cec enable config mode");
|
||||
configMode = true;
|
||||
coloredMsg(LOG_YELLOW, true, "ch cec enable admin mode");
|
||||
adminMode = true;
|
||||
retCode = 1;
|
||||
} else if (0 == strcmp(cmd, "disable")) {
|
||||
coloredMsg(LOG_YELLOW, true, "ch cec disable config mode");
|
||||
coloredMsg(LOG_YELLOW, true, "ch cec disable admin mode");
|
||||
adminMode = false;
|
||||
configMode = false;
|
||||
retCode = 3;
|
||||
} else if (0 == strcmp(cmd, "config")) {
|
||||
coloredMsg(LOG_YELLOW, true, "ch cec enable config mode");
|
||||
configMode = true;
|
||||
retCode = 2;
|
||||
} else {
|
||||
uint8_t cmdIdx = 0;
|
||||
while (true) {
|
||||
cmd_t command = COMMANDS[cmdIdx];
|
||||
cmd_t command = commands[cmdIdx];
|
||||
if (0 == strcmp("END_OF_CMDS", command.name)) {
|
||||
messageToSend = (uint8_t*) UNKNOWN_COMMAND;
|
||||
break;
|
||||
}
|
||||
if (0 == strcmp(cmd, command.name)) {
|
||||
if (command.requiredConfigMode && !configMode) {
|
||||
messageToSend = (uint8_t*)REQUIRES_CONFIG_MODE_MGS;
|
||||
} else {
|
||||
messageToSend = command.cmdFunc(argc, args) ? (uint8_t*)OK_MSG : (uint8_t*)FAILED_MSG;
|
||||
}
|
||||
break;
|
||||
}
|
||||
cmdIdx++;
|
||||
}
|
||||
@ -224,11 +161,11 @@ int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetConfigMode) {
|
||||
return retCode;
|
||||
}
|
||||
|
||||
void cmdHandlerEngine(void *handle) {
|
||||
static void cmdHandlerEngine(void *handle) {
|
||||
static uint8_t receiveBuffer[256];
|
||||
|
||||
static chState_t state = CH_INIT;
|
||||
static bool resetConfigMode = false;
|
||||
static bool resetSpecialModes = false;
|
||||
|
||||
static char banner[] = \
|
||||
"MBGW3\n\r" \
|
||||
@ -236,8 +173,9 @@ void cmdHandlerEngine(void *handle) {
|
||||
"or quit to close the connection.\n\r";
|
||||
|
||||
static char *prompt;
|
||||
static char defaultPrompt[] = "MBGW3 # ";
|
||||
static char elevatedPrompt[] = "MBGW3 (admin) > ";
|
||||
static char defaultPrompt[] = "MBGW3 > ";
|
||||
static char adminPrompt[] = "MBGW3 (admin) # ";
|
||||
static char configPrompt[] = "MBGW3 (config) $ ";
|
||||
|
||||
|
||||
int8_t res = 0;
|
||||
@ -300,7 +238,7 @@ void cmdHandlerEngine(void *handle) {
|
||||
resultSend = send(CMD_SOCK, (uint8_t*)banner, strlen(banner));
|
||||
coloredMsg(LOG_YELLOW, false, "ch che sent banner, send returns 0x%02x", resultSend);
|
||||
prompt = defaultPrompt;
|
||||
resetConfigMode = true;
|
||||
resetSpecialModes = true;
|
||||
state = CH_PROMPT;
|
||||
}
|
||||
break;
|
||||
@ -340,8 +278,8 @@ void cmdHandlerEngine(void *handle) {
|
||||
receiveBuffer[strlen((char*)receiveBuffer) - 1] = 0;
|
||||
}
|
||||
coloredMsg(LOG_YELLOW, false, "ch che received: %s", receiveBuffer);
|
||||
int8_t resCEC = cmdExecuteCommand(receiveBuffer, resetConfigMode);
|
||||
resetConfigMode = false;
|
||||
int8_t resCEC = cmdExecuteCommand(receiveBuffer, resetSpecialModes);
|
||||
resetSpecialModes = false;
|
||||
switch (resCEC) {
|
||||
case 0:
|
||||
state = CH_PROMPT;
|
||||
@ -350,10 +288,14 @@ void cmdHandlerEngine(void *handle) {
|
||||
state = CH_DISCONNECT;
|
||||
break;
|
||||
case 1:
|
||||
prompt = elevatedPrompt;
|
||||
prompt = adminPrompt;
|
||||
state = CH_PROMPT;
|
||||
break;
|
||||
case 2:
|
||||
prompt = configPrompt;
|
||||
state = CH_PROMPT;
|
||||
break;
|
||||
case 3:
|
||||
prompt = defaultPrompt;
|
||||
state = CH_PROMPT;
|
||||
break;
|
||||
@ -371,9 +313,9 @@ void cmdHandlerEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case CH_DISCONNECT_WAIT:
|
||||
coloredMsg(LOG_YELLOW, false, "ch che waiting after disconnect");
|
||||
//coloredMsg(LOG_YELLOW, false, "ch che waiting after disconnect");
|
||||
sockState = getSn_SR(CMD_SOCK);
|
||||
coloredMsg(LOG_YELLOW, false, "ch che sockState is 0x%02x", sockState);
|
||||
//coloredMsg(LOG_YELLOW, false, "ch che sockState is 0x%02x", sockState);
|
||||
if (sockState == SOCK_CLOSED) {
|
||||
coloredMsg(LOG_YELLOW, true, "ch che socket is closed now");
|
||||
state = CH_INIT;
|
||||
|
158
cube/User/Src/config.c
Normal file
158
cube/User/Src/config.c
Normal file
@ -0,0 +1,158 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <config.h>
|
||||
#include <eeprom.h>
|
||||
#include <logger.h>
|
||||
#include <mbusComm.h>
|
||||
|
||||
|
||||
|
||||
#define NUM_OF_DEFAULT_DEVICES 8
|
||||
t_deviceBlock defaultDeviceBlock[] = {
|
||||
{
|
||||
.deviceMagic = DEVICE_MAGIC,
|
||||
.deviceName = "Total",
|
||||
.address = 80,
|
||||
.consideredField = { 0, 17, -1, -1 },
|
||||
.period = 10,
|
||||
.filler = { 0, 0, 0 }
|
||||
},
|
||||
{
|
||||
.deviceMagic = DEVICE_MAGIC,
|
||||
.deviceName = "Computer",
|
||||
.address = 85,
|
||||
.consideredField = { 0, 4, 2, 3 },
|
||||
.period = 10,
|
||||
.filler = { 0, 0, 0 }
|
||||
},
|
||||
{
|
||||
.deviceMagic = DEVICE_MAGIC,
|
||||
.deviceName = "Dryer",
|
||||
.address = 81,
|
||||
.consideredField = { 0, 4, 2, 3 },
|
||||
.period = 10,
|
||||
.filler = { 0, 0, 0 }
|
||||
},
|
||||
{
|
||||
.deviceMagic = DEVICE_MAGIC,
|
||||
.deviceName = "Laundry",
|
||||
.address = 82,
|
||||
.consideredField = { 0, 4, 2, 3 },
|
||||
.period = 10,
|
||||
.filler = { 0, 0, 0 }
|
||||
},
|
||||
{
|
||||
.deviceMagic = DEVICE_MAGIC,
|
||||
.deviceName = "Dishwasher",
|
||||
.address = 83,
|
||||
.consideredField = { 0, 4, 2, 3 },
|
||||
.period = 10,
|
||||
.filler = { 0, 0, 0 }
|
||||
},
|
||||
{
|
||||
.deviceMagic = DEVICE_MAGIC,
|
||||
.deviceName = "Light",
|
||||
.address = 84,
|
||||
.consideredField = { 0, 4, 2, 3 },
|
||||
.period = 10,
|
||||
.filler = { 0, 0, 0 }
|
||||
},
|
||||
{
|
||||
.deviceMagic = DEVICE_MAGIC,
|
||||
.deviceName = "Freezer",
|
||||
.address = 86,
|
||||
.consideredField = { 0, 4, 2, 3 },
|
||||
.period = 10,
|
||||
.filler = { 0, 0, 0 }
|
||||
},
|
||||
{
|
||||
.deviceMagic = DEVICE_MAGIC,
|
||||
.deviceName = "Fridge",
|
||||
.address = 87,
|
||||
.consideredField = { 0, 4, 2, 3 },
|
||||
.period = 10,
|
||||
.filler = { 0, 0, 0 }
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
t_configBlock defaultConfigBlock = {
|
||||
.configMagic = CONFIG_MAGIC,
|
||||
.deviceName = "MBGW3",
|
||||
.macAddress = { 0x00, 0xA0, 0x57, 0x05, 0x3E, 0x0D },
|
||||
.frontendThreshold = 240,
|
||||
.brokerName = "mqttbroker",
|
||||
.watchdogTopic = "IoT/Watchdog",
|
||||
.startupTopic = "IoT/MBGW3/Startup",
|
||||
.statusTopic = "IoT/MBGW3/Status",
|
||||
.mbusDataTopic = "IoT/MBGW3/Measurement",
|
||||
.syslogServerName = "syslogserver",
|
||||
.numOfDeviceBlocks = NUM_OF_DEFAULT_DEVICES,
|
||||
.filler = { 0 }
|
||||
};
|
||||
|
||||
|
||||
|
||||
t_configBlock mainConfigBlock;
|
||||
|
||||
|
||||
|
||||
t_configBlock* getConfig() {
|
||||
return &mainConfigBlock;
|
||||
}
|
||||
|
||||
|
||||
void configInit() {
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci Reading configuration block from eeprom");
|
||||
eepromReadConfigBlock(&mainConfigBlock);
|
||||
|
||||
if (mainConfigBlock.configMagic != CONFIG_MAGIC) {
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci Invalid configuration block read from eeprom");
|
||||
|
||||
eepromWriteConfigBlock(&defaultConfigBlock);
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci Default configuration block written to eeprom");
|
||||
|
||||
for (uint8_t i = 0; i < NUM_OF_DEFAULT_DEVICES; i++) {
|
||||
eepromWriteDeviceBlock(i, &defaultDeviceBlock[i]);
|
||||
}
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci Default device blocks written to eeprom");
|
||||
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci Reading configuration block from eeprom again");
|
||||
eepromReadConfigBlock(&mainConfigBlock);
|
||||
}
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci configMagic: %lx", mainConfigBlock.configMagic);
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci deviceName: %s", mainConfigBlock.deviceName);
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci MAC address: %02x:%02x:%02x:%02x:%02x:%02x", mainConfigBlock.macAddress[0],
|
||||
mainConfigBlock.macAddress[1],
|
||||
mainConfigBlock.macAddress[2],
|
||||
mainConfigBlock.macAddress[3],
|
||||
mainConfigBlock.macAddress[4],
|
||||
mainConfigBlock.macAddress[5]);
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci frontend threshold: %ld", mainConfigBlock.frontendThreshold);
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci broker: %s", mainConfigBlock.brokerName);
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci watchdogTopic: %s", mainConfigBlock.watchdogTopic);
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci startupTopic: %s", mainConfigBlock.startupTopic);
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci statusTopic: %s", mainConfigBlock.statusTopic);
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci mbusDataTopic: %s", mainConfigBlock.mbusDataTopic);
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci syslog server: %s", mainConfigBlock.syslogServerName);
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci device block cnt: %d", mainConfigBlock.numOfDeviceBlocks);
|
||||
|
||||
for (uint8_t i = 0; i < mainConfigBlock.numOfDeviceBlocks; i++) {
|
||||
t_deviceBlock tmpDeviceBlock;
|
||||
eepromReadDeviceBlock(i, &tmpDeviceBlock);
|
||||
if (tmpDeviceBlock.deviceMagic == DEVICE_MAGIC) {
|
||||
coloredMsg(LOG_BLUE, false, "cfg ci device %d: ", i);
|
||||
coloredMsg(LOG_BLUE, false, " Name: %s, Address: %d, Period: %d",
|
||||
tmpDeviceBlock.deviceName, tmpDeviceBlock.address, tmpDeviceBlock.period);
|
||||
coloredMsg(LOG_BLUE, false, " Considered Fields: %d %d %d %d",
|
||||
tmpDeviceBlock.consideredField[0],
|
||||
tmpDeviceBlock.consideredField[1],
|
||||
tmpDeviceBlock.consideredField[2],
|
||||
tmpDeviceBlock.consideredField[3]);
|
||||
mbusCommAddDevice(&tmpDeviceBlock);
|
||||
} else {
|
||||
coloredMsg(LOG_BLUE, false, "magic of device %d does not match, ignored", i);
|
||||
}
|
||||
}
|
||||
}
|
76
cube/User/Src/configCmds.c
Normal file
76
cube/User/Src/configCmds.c
Normal file
@ -0,0 +1,76 @@
|
||||
#include <cmdHelper.h>
|
||||
#include <logger.h>
|
||||
|
||||
#include <eeprom.h>
|
||||
#include <config.h>
|
||||
|
||||
|
||||
static bool showConfigCmd(uint8_t argc, char **args) {
|
||||
bool retCode = true;
|
||||
|
||||
t_configBlock configBlock;
|
||||
eepromReadConfigBlock(&configBlock);
|
||||
sendFormatString("configMagic: %lx\n\r", configBlock.configMagic);
|
||||
sendFormatString("deviceName: %s\n\r", configBlock.deviceName);
|
||||
sendFormatString("MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n\r", configBlock.macAddress[0],
|
||||
configBlock.macAddress[1],
|
||||
configBlock.macAddress[2],
|
||||
configBlock.macAddress[3],
|
||||
configBlock.macAddress[4],
|
||||
configBlock.macAddress[5]);
|
||||
sendFormatString("frontend threshold: %ld\n\r", configBlock.frontendThreshold);
|
||||
sendFormatString("broker: %s\n\r", configBlock.brokerName);
|
||||
sendFormatString("watchdogTopic: %s\n\r", configBlock.watchdogTopic);
|
||||
sendFormatString("startupTopic: %s\n\r", configBlock.startupTopic);
|
||||
sendFormatString("statusTopic: %s\n\r", configBlock.statusTopic);
|
||||
sendFormatString("mbusDataTopic: %s\n\r", configBlock.mbusDataTopic);
|
||||
sendFormatString("syslog server: %s\n\r", configBlock.syslogServerName);
|
||||
sendFormatString("device block cnt: %d\n\r", configBlock.numOfDeviceBlocks);
|
||||
|
||||
for (uint8_t i = 0; i < configBlock.numOfDeviceBlocks; i++) {
|
||||
t_deviceBlock tmpDeviceBlock;
|
||||
eepromReadDeviceBlock(i, &tmpDeviceBlock);
|
||||
if (tmpDeviceBlock.deviceMagic == DEVICE_MAGIC) {
|
||||
sendFormatString("device %d: \n\r", i);
|
||||
sendFormatString(" Name: %s, Address: %d, Period: %d\n\r",
|
||||
tmpDeviceBlock.deviceName, tmpDeviceBlock.address, tmpDeviceBlock.period);
|
||||
sendFormatString(" Considered Fields: %d %d %d %d\n\r",
|
||||
tmpDeviceBlock.consideredField[0],
|
||||
tmpDeviceBlock.consideredField[1],
|
||||
tmpDeviceBlock.consideredField[2],
|
||||
tmpDeviceBlock.consideredField[3]);
|
||||
if (tmpDeviceBlock.deviceMagic != DEVICE_MAGIC) {
|
||||
sendString(" DEVICE MAGIC DOES NOT MATCH\n\r");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return retCode;
|
||||
}
|
||||
|
||||
static bool restartCmd(uint8_t argc, char **args) {
|
||||
HAL_NVIC_SystemReset();
|
||||
// you want come here ...
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
const cmd_t CONFIG_COMMANDS[] = {
|
||||
{ .name = "show", .cmdFunc = showConfigCmd,
|
||||
.help = \
|
||||
"show ................................. Show the configuration\n\r"
|
||||
},
|
||||
{ .name = "restart", .cmdFunc = restartCmd,
|
||||
.help = \
|
||||
"restart .............................. Restart the system,\n\r" \
|
||||
" Required to reload config\n\r"
|
||||
},
|
||||
{ .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL }
|
||||
};
|
||||
|
||||
|
||||
|
||||
const cmd_t *getConfigCommands() {
|
||||
return CONFIG_COMMANDS;
|
||||
}
|
@ -7,6 +7,8 @@
|
||||
#include <PontCoopScheduler.h>
|
||||
#include <utils.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#define HIGH GPIO_PIN_SET
|
||||
#define LOW GPIO_PIN_RESET
|
||||
|
||||
@ -20,21 +22,18 @@ static const uint8_t EEPROM_WREN = 0x06;
|
||||
// static const uint8_t EEPROM_WRSR = 0x01;
|
||||
|
||||
|
||||
static const uint32_t EEPROM_MAGIC = 0xaffe0008;
|
||||
static const uint32_t EEPROM_MAGIC = 0xaffe0009;
|
||||
|
||||
|
||||
static const uint16_t EEPROM_HEADER_ADDR = EEPROM_BASE_ADDR;
|
||||
|
||||
|
||||
typedef union {
|
||||
struct __attribute__((__packed__)) s_eepromHeader {
|
||||
uint32_t magic;
|
||||
uint32_t writeCounter;
|
||||
uint8_t activeBlock;
|
||||
} s;
|
||||
t_eepromHeader s;
|
||||
uint8_t b[sizeof(struct s_eepromHeader)];
|
||||
} t_eepromHeader;
|
||||
|
||||
static const uint16_t EEPROM_HEADER_ADDR = 0;
|
||||
static t_eepromHeader eepromHeader;
|
||||
} t_eepromHeaderBlock;
|
||||
|
||||
static t_eepromHeaderBlock eepromHeader;
|
||||
|
||||
|
||||
typedef union {
|
||||
@ -42,11 +41,13 @@ typedef union {
|
||||
uint8_t b[sizeof(t_deviceStats)];
|
||||
} t_deviceStatsBlock;
|
||||
|
||||
static const uint16_t DEVICE_STATS_ADDR = 32;
|
||||
static const uint16_t DEVICE_STATS_ADDR = EEPROM_DEVICE_STATS_ADDR;
|
||||
static t_deviceStatsBlock deviceStats;
|
||||
|
||||
static const uint8_t NUM_OF_BLOCKS = 2;
|
||||
static const uint16_t BLOCK_ADDR[] = { 64, 4128 };
|
||||
static const uint16_t CONFIG_BLOCK_ADDR = EEPROM_CONFIG_BLOCK_ADDR;
|
||||
static const uint16_t DEVICE_BLOCK_ADDR = EEPROM_DEVICE_BLOCK_BASE_ADDR;
|
||||
|
||||
|
||||
|
||||
|
||||
typedef union {
|
||||
@ -58,7 +59,6 @@ typedef union {
|
||||
uint8_t b[sizeof(struct s_spiMsg)];
|
||||
} t_spiMsg;
|
||||
|
||||
|
||||
t_deviceStats* getGlobalDeviceStats() {
|
||||
return &(deviceStats.s);
|
||||
}
|
||||
@ -72,6 +72,11 @@ static uint16_t swap(uint16_t i) {
|
||||
}
|
||||
|
||||
|
||||
// active waiting, use only during initialization!
|
||||
static void eepromActiveDelay(uint8_t delay_ms) {
|
||||
activeDelay(delay_ms);
|
||||
}
|
||||
|
||||
void eepromWrite(uint16_t addr, uint8_t *buf, uint8_t len) {
|
||||
t_spiMsg msg = {
|
||||
.s.cmd = EEPROM_WRITE,
|
||||
@ -126,10 +131,41 @@ static void eepromHourlyUpdateDeviceStats(void *handle) {
|
||||
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
|
||||
}
|
||||
|
||||
// active waiting, use only during initialization!
|
||||
static void eepromActiveDelay(uint8_t delay_ms) {
|
||||
activeDelay(delay_ms);
|
||||
void eepromReadConfigBlock(t_configBlock *destConfigBlock) {
|
||||
// static_assert((sizeof(*destConfigBlock) % EEPROM_WRITE_BLOCK_SIZE == 0), "config block has illegal size, must be dividable by 32");
|
||||
|
||||
for (uint8_t i = 0; i < (sizeof(*destConfigBlock) / EEPROM_WRITE_BLOCK_SIZE); i++) {
|
||||
eepromRead(CONFIG_BLOCK_ADDR + (i * EEPROM_WRITE_BLOCK_SIZE), ((uint8_t*)destConfigBlock) + (i * EEPROM_WRITE_BLOCK_SIZE), EEPROM_WRITE_BLOCK_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
void eepromWriteConfigBlock(t_configBlock *srcConfigBlock) {
|
||||
for (uint8_t i = 0; i < (sizeof(*srcConfigBlock) / EEPROM_WRITE_BLOCK_SIZE); i++) {
|
||||
eepromWrite(CONFIG_BLOCK_ADDR + (i * EEPROM_WRITE_BLOCK_SIZE), ((uint8_t*)srcConfigBlock) + (i * EEPROM_WRITE_BLOCK_SIZE), EEPROM_WRITE_BLOCK_SIZE);
|
||||
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
void eepromReadDeviceBlock(uint8_t blockNum, t_deviceBlock *destDeviceBlock) {
|
||||
static_assert((sizeof(*destDeviceBlock) % EEPROM_WRITE_BLOCK_SIZE == 0), "device block has illegal size, must be dividable by 32");
|
||||
|
||||
for (uint8_t i = 0; i < (sizeof(*destDeviceBlock) / EEPROM_WRITE_BLOCK_SIZE); i++) {
|
||||
eepromRead(DEVICE_BLOCK_ADDR + (blockNum * sizeof(*destDeviceBlock)) + (i * EEPROM_WRITE_BLOCK_SIZE),
|
||||
((uint8_t*)destDeviceBlock) + (i * EEPROM_WRITE_BLOCK_SIZE),
|
||||
EEPROM_WRITE_BLOCK_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
void eepromWriteDeviceBlock(uint8_t blockNum, t_deviceBlock *srcDeviceBlock) {
|
||||
for (uint8_t i = 0; i < (sizeof(*srcDeviceBlock) / EEPROM_WRITE_BLOCK_SIZE); i++) {
|
||||
eepromWrite(DEVICE_BLOCK_ADDR + (blockNum * sizeof(*srcDeviceBlock)) + (i * EEPROM_WRITE_BLOCK_SIZE),
|
||||
((uint8_t*)srcDeviceBlock) + (i * EEPROM_WRITE_BLOCK_SIZE),
|
||||
EEPROM_WRITE_BLOCK_SIZE);
|
||||
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void eepromInit() {
|
||||
__EEPROM_CS(HIGH);
|
||||
@ -147,26 +183,19 @@ void eepromInit() {
|
||||
deviceStats.s.totalFailures = 0;
|
||||
logMsg("eeI, about to write device stats for the first time");
|
||||
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
|
||||
eepromActiveDelay(7);
|
||||
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
|
||||
|
||||
uint8_t emptyBlock[32];
|
||||
uint8_t emptyBlock[EEPROM_WRITE_BLOCK_SIZE];
|
||||
memset(emptyBlock, 0, sizeof(emptyBlock));
|
||||
for (uint8_t i = 0; i < NUM_OF_BLOCKS; i++) {
|
||||
for (uint8_t j = 0; j <= 127; j++) {
|
||||
uint16_t addr = BLOCK_ADDR[i] + sizeof(emptyBlock) * j;
|
||||
eepromWrite(addr, emptyBlock, sizeof(emptyBlock));
|
||||
eepromActiveDelay(7);
|
||||
}
|
||||
}
|
||||
logMsg("eeI, storage blocks initialized");
|
||||
|
||||
eepromWrite(CONFIG_BLOCK_ADDR, emptyBlock, EEPROM_WRITE_BLOCK_SIZE);
|
||||
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
|
||||
logMsg("eeI, config block initialized");
|
||||
|
||||
eepromHeader.s.magic = EEPROM_MAGIC;
|
||||
eepromHeader.s.activeBlock = 0;
|
||||
eepromHeader.s.writeCounter = 1;
|
||||
logMsg("eeI, about to write header for the first time");
|
||||
eepromWrite(EEPROM_HEADER_ADDR, eepromHeader.b, sizeof(eepromHeader));
|
||||
eepromActiveDelay(7);
|
||||
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
|
||||
logMsg("eeI, eeprom has been initialized");
|
||||
} else {
|
||||
logMsg("eeI, eeprom is initialized");
|
||||
@ -182,7 +211,7 @@ void eepromInit() {
|
||||
deviceStats.s.totalPowercycles += 1;
|
||||
logMsg("eeI, about to write device stats with updated power cycles counter");
|
||||
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
|
||||
eepromActiveDelay(7);
|
||||
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
|
||||
|
||||
schAdd(eepromHourlyUpdateDeviceStats, NULL, 0, 60 * 60 * 1000);
|
||||
logMsg("eeI, hourly device stats update scheduled");
|
||||
|
@ -6,9 +6,10 @@
|
||||
#include <frontend.h>
|
||||
#include <logger.h>
|
||||
#include <show.h>
|
||||
#include <config.h>
|
||||
|
||||
|
||||
|
||||
static t_configBlock *config;
|
||||
|
||||
|
||||
static volatile int32_t frontendAdcThreshold = 0;
|
||||
@ -16,16 +17,15 @@ static volatile bool frontendEnabled = false;
|
||||
|
||||
|
||||
void frontendInit() {
|
||||
config = getConfig();
|
||||
frontendAdcThreshold = config->frontendThreshold;
|
||||
|
||||
HAL_ADCEx_Calibration_Start(&frontendAdc);
|
||||
logMsg("frontendInit, calibration done");
|
||||
HAL_ADC_Start_IT(&frontendAdc);
|
||||
logMsg("frontendInit, adc started");
|
||||
}
|
||||
|
||||
void frontendSetThreshold(int32_t threshold) {
|
||||
frontendAdcThreshold = threshold;
|
||||
}
|
||||
|
||||
void frontendEnable() {
|
||||
frontendEnabled = true;
|
||||
}
|
||||
@ -37,8 +37,6 @@ void frontendDisable() {
|
||||
void frontendAdcCallback(ADC_HandleTypeDef* hadc) {
|
||||
static int32_t holdValue = 0;
|
||||
|
||||
// show(DEBUG_2, TOGGLE);
|
||||
|
||||
if (frontendEnabled) {
|
||||
int32_t currentValue = (int32_t) HAL_ADC_GetValue(hadc);
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include <ringbuffer.h>
|
||||
#include <wizHelper.h>
|
||||
#include <socket.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
@ -22,18 +23,21 @@
|
||||
#endif //LOGGER_OUTPUT_BY_INTERRUPT
|
||||
|
||||
|
||||
#define LOGBUFFER_SIZE 2048
|
||||
#define LOGBUFFER_SIZE 4096
|
||||
#define MSGBUFFER_SIZE 256
|
||||
|
||||
|
||||
static t_configBlock *config;
|
||||
|
||||
extern const uint8_t SYSLOG_SOCK;
|
||||
uint8_t syslogAddr[] = { 172, 16, 11, 15 };
|
||||
uint8_t syslogAddr[4];
|
||||
|
||||
uint8_t singleOctetTXBuffer;
|
||||
|
||||
static ringbuffer_t logBuffer;
|
||||
|
||||
void logInit() {
|
||||
config = getConfig();
|
||||
ringbufferInit(&logBuffer, LOGBUFFER_SIZE);
|
||||
}
|
||||
|
||||
@ -69,16 +73,31 @@ int logExec() {
|
||||
void syslog(char *msg) {
|
||||
static uint8_t state = 0;
|
||||
int8_t res8 = 0;
|
||||
int32_t res32 = 0;
|
||||
|
||||
if (isNetworkAvailable()) {
|
||||
switch (state) {
|
||||
case 0:
|
||||
res8 = socket(SYSLOG_SOCK, Sn_MR_UDP, 514, SF_IO_NONBLOCK);
|
||||
if (res8 == SYSLOG_SOCK) {
|
||||
state = 1;
|
||||
}
|
||||
if (res8 != SYSLOG_SOCK) {
|
||||
break;
|
||||
}
|
||||
state = 1;
|
||||
// no break
|
||||
case 1:
|
||||
sendto(SYSLOG_SOCK, (uint8_t*)msg, strlen(msg), syslogAddr, 514);
|
||||
if (! wizDnsQuery(config->syslogServerName, syslogAddr)) {
|
||||
disconnect(SYSLOG_SOCK);
|
||||
state = 0;
|
||||
break;
|
||||
}
|
||||
state = 2;
|
||||
// no break
|
||||
case 2:
|
||||
res32 = sendto(SYSLOG_SOCK, (uint8_t*)msg, strlen(msg), syslogAddr, 514);
|
||||
if (res32 != strlen(msg)) {
|
||||
disconnect(SYSLOG_SOCK);
|
||||
state = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include <mqttComm.h>
|
||||
#include <cmdHandler.h>
|
||||
#include <oled.h>
|
||||
#include <config.h>
|
||||
|
||||
|
||||
void my_setup_1() {
|
||||
@ -46,6 +47,9 @@ void my_setup_2() {
|
||||
eepromInit();
|
||||
oledPrint(OLED_SCREEN0, "eeprom init");
|
||||
|
||||
configInit();
|
||||
oledPrint(OLED_SCREEN0, "config init");
|
||||
|
||||
wizInit();
|
||||
oledPrint(OLED_SCREEN0, "network init");
|
||||
|
||||
@ -55,7 +59,6 @@ void my_setup_2() {
|
||||
oledPrint(OLED_SCREEN0, "cmdhandler init");
|
||||
|
||||
frontendInit();
|
||||
frontendSetThreshold(240);
|
||||
oledPrint(OLED_SCREEN0, "frontend init");
|
||||
|
||||
mbusCommInit();
|
||||
|
@ -17,13 +17,21 @@
|
||||
#include <mqttComm.h>
|
||||
#include <oled.h>
|
||||
#include <ringbuffer.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <mbus/mbus-protocol.h>
|
||||
|
||||
static const char MBUS_TOPIC[] = "IoT/MBGW3/Measurement";
|
||||
|
||||
static t_configBlock *config;
|
||||
|
||||
static const uint8_t MBUS_QUERY_CMD = 0x5b;
|
||||
|
||||
// Delay between end of last bit of sent request telegram
|
||||
// and enabling the frontend for receiving the response.
|
||||
// Standard says client must wait at least 11 bit times.
|
||||
// 1 / 2400 * 11 = 4.5e-3ms
|
||||
static const uint32_t DELAY_AFTER_SENDING = 4; // ms
|
||||
|
||||
typedef enum {
|
||||
MBCR_SUCCESS = 0,
|
||||
MBCR_ERROR_TIMEOUT,
|
||||
@ -47,6 +55,7 @@ typedef enum {
|
||||
MBCS_SEND_CONTINUED,
|
||||
MBCS_SENDING,
|
||||
MBCS_SENDING_DONE,
|
||||
MBCS_WAIT_AFTER_SENDING,
|
||||
MBCS_ENABLE_FRONTEND,
|
||||
MBCS_START1,
|
||||
MBCS_LENGTH1,
|
||||
@ -105,7 +114,7 @@ typedef struct {
|
||||
|
||||
static t_mbusCommHandle mbusCommHandle = { .requestId = 0, .state = MBCS_IDLE, .retryCnt = 0, .cmd = 0, .addr = 0, .startTime = 0, .receiveCnt = 0, .waitForOctet = false };
|
||||
|
||||
static t_mbusCommStats mbusCommStats = { .mbusRequestCnt = 0, .mbusErrorCnt = 0, .uartOctetCnt = 0, .uartOverrunCnt = 0, .uartFramingErrCnt = 0, .uartParityErrCnt = 0 };
|
||||
static t_mbusCommStats mbusCommStats = { .mbusRequestCnt = 0, .mbusErrorCnt = 0, .uartOctetCnt = 0, .uartOverrunCnt = 0, .uartFramingErrCnt = 0, .uartParityErrCnt = 0, .uartNoiseErrCnt = 0 };
|
||||
|
||||
static bool mbusCommEnabled = true;
|
||||
|
||||
@ -123,7 +132,7 @@ static void printError() {
|
||||
coloredMsg(LOG_YELLOW, true, "mbc pe [%d] Error ratio is %.2f",
|
||||
mbusCommHandle.requestId,
|
||||
errorRatio);
|
||||
mqttPublishf(MBUS_TOPIC, "{\"Status\":\"Error\", \"RequestId\":\"%d\", \"Device\":\"%s\", \"Errors\":\"%d\", \"Requests\":\"%d\", \"ErrorRatio\":\"%.2f\"}",
|
||||
mqttPublishf(config->mbusDataTopic, "{\"Status\":\"Error\", \"RequestId\":\"%d\", \"Device\":\"%s\", \"Errors\":\"%d\", \"Requests\":\"%d\", \"ErrorRatio\":\"%.2f\"}",
|
||||
mbusCommHandle.requestId, mbusCommHandle.device->deviceName,
|
||||
mbusCommHandle.device->failures, mbusCommHandle.device->requests, errorRatio);
|
||||
oledPrintf(OLED_SCREEN0, "Err:%d/%d %.2f", mbusCommHandle.device->failures, mbusCommHandle.device->requests, errorRatio);
|
||||
@ -224,25 +233,25 @@ static void parseAndPrintFrame() {
|
||||
mbusCommHandle.requestId,
|
||||
errorRatio);
|
||||
if (numOfConsideredFields == 1) {
|
||||
mqttPublishf(MBUS_TOPIC, "{\"Status\":\"Ok\", \"RequestId\":\"%d\", \"Device\":\"%s\", \"Errors\":\"%d\", \"Requests\":\"%d\", \"ErrorRatio\":\"%.2f\", " \
|
||||
mqttPublishf(config->mbusDataTopic, "{\"Status\":\"Ok\", \"RequestId\":\"%d\", \"Device\":\"%s\", \"Errors\":\"%d\", \"Requests\":\"%d\", \"ErrorRatio\":\"%.2f\", " \
|
||||
"\"Values\":{\"%s\":\"%.1f\"}}",
|
||||
mbusCommHandle.requestId, mbusCommHandle.device->deviceName,
|
||||
mbusCommHandle.device->failures, mbusCommHandle.device->requests, errorRatio,
|
||||
keys[0], values[0]);
|
||||
} else if (numOfConsideredFields == 2) {
|
||||
mqttPublishf(MBUS_TOPIC, "{\"Status\":\"Ok\", \"RequestId\":\"%d\", \"Device\":\"%s\", \"Errors\":\"%d\", \"Requests\":\"%d\", \"ErrorRatio\":\"%.2f\", " \
|
||||
mqttPublishf(config->mbusDataTopic, "{\"Status\":\"Ok\", \"RequestId\":\"%d\", \"Device\":\"%s\", \"Errors\":\"%d\", \"Requests\":\"%d\", \"ErrorRatio\":\"%.2f\", " \
|
||||
"\"Values\":{\"%s\":\"%.1f\", \"%s\":\"%.1f\"}}",
|
||||
mbusCommHandle.requestId, mbusCommHandle.device->deviceName,
|
||||
mbusCommHandle.device->failures, mbusCommHandle.device->requests, errorRatio,
|
||||
keys[0], values[0], keys[1], values[1]);
|
||||
} else if (numOfConsideredFields == 3) {
|
||||
mqttPublishf(MBUS_TOPIC, "{\"Status\":\"Ok\", \"RequestId\":\"%d\", \"Device\":\"%s\", \"Errors\":\"%d\", \"Requests\":\"%d\", \"ErrorRatio\":\"%.2f\", " \
|
||||
mqttPublishf(config->mbusDataTopic, "{\"Status\":\"Ok\", \"RequestId\":\"%d\", \"Device\":\"%s\", \"Errors\":\"%d\", \"Requests\":\"%d\", \"ErrorRatio\":\"%.2f\", " \
|
||||
"\"Values\":{\"%s\":\"%.1f\", \"%s\":\"%.1f\", \"%s\":\"%.1f\"}}",
|
||||
mbusCommHandle.requestId, mbusCommHandle.device->deviceName,
|
||||
mbusCommHandle.device->failures, mbusCommHandle.device->requests, errorRatio,
|
||||
keys[0], values[0], keys[1], values[1], keys[2], values[2]);
|
||||
} else if (numOfConsideredFields == 4) {
|
||||
mqttPublishf(MBUS_TOPIC, "{\"Status\":\"Ok\", \"RequestId\":\"%d\", \"Device\":\"%s\", \"Errors\":\"%d\", \"Requests\":\"%d\", \"ErrorRatio\":\"%.2f\", " \
|
||||
mqttPublishf(config->mbusDataTopic, "{\"Status\":\"Ok\", \"RequestId\":\"%d\", \"Device\":\"%s\", \"Errors\":\"%d\", \"Requests\":\"%d\", \"ErrorRatio\":\"%.2f\", " \
|
||||
"\"Values\":{\"%s\":\"%.1f\", \"%s\":\"%.1f\", \"%s\":\"%.1f\", \"%s\":\"%.1f\"}}",
|
||||
mbusCommHandle.requestId, mbusCommHandle.device->deviceName,
|
||||
mbusCommHandle.device->failures, mbusCommHandle.device->requests, errorRatio,
|
||||
@ -258,19 +267,16 @@ static void parseAndPrintFrame() {
|
||||
|
||||
|
||||
void mbusCommISR() {
|
||||
show(DEBUG_1, TOGGLE);
|
||||
|
||||
uint32_t isrflags = READ_REG(mbusUart.Instance->SR);
|
||||
uint32_t cr1its = READ_REG(mbusUart.Instance->CR1);
|
||||
|
||||
// RXNEIE doesn't need to be considered since it is always on and more over the
|
||||
// RXNE flag is cleared by reading the DR, which is done in any case
|
||||
if (((isrflags & USART_SR_RXNE) != RESET) || ((isrflags & (USART_SR_ORE | USART_SR_FE | USART_SR_PE)) != RESET)) {
|
||||
if (((isrflags & USART_SR_RXNE) != RESET) || ((isrflags & (USART_SR_ORE | USART_SR_FE | USART_SR_PE | USART_SR_NE)) != RESET)) {
|
||||
// Error flags are only valid together with the RX flag.
|
||||
// They will be cleared by reading SR (already done above) followed by reading DR (below).
|
||||
bool errorFound = false;
|
||||
if ((isrflags & USART_SR_ORE) != RESET) {
|
||||
show(DEBUG_2, TOGGLE);
|
||||
mbusCommStats.uartOverrunCnt += 1;
|
||||
errorFound = true;
|
||||
}
|
||||
@ -282,10 +288,13 @@ void mbusCommISR() {
|
||||
mbusCommStats.uartParityErrCnt += 1;
|
||||
errorFound = true;
|
||||
}
|
||||
if ((isrflags & USART_SR_NE) != RESET) {
|
||||
mbusCommStats.uartNoiseErrCnt += 1;
|
||||
errorFound = true;
|
||||
}
|
||||
mbusCommStats.uartOctetCnt += 1;
|
||||
// it is required to read the DR in any case here, not only when the buffer has space
|
||||
// otherwise the interrupt flag won't be disabled, particularly important in case of
|
||||
// ORE
|
||||
// otherwise the interrupt flag won't be reset, particularly important in case of ORE
|
||||
uint8_t data = (uint8_t)(mbusUart.Instance->DR & (uint8_t)0x00FF);
|
||||
if ((! errorFound) &&
|
||||
(mbusCommHandle.receiveBuffer.writeIdx < mbusCommHandle.receiveBuffer.size)) {
|
||||
@ -302,17 +311,24 @@ void mbusCommISR() {
|
||||
mbusCommHandle.sendBuffer.readIdx += 1;
|
||||
if (mbusCommHandle.sendBuffer.readIdx == mbusCommHandle.sendBuffer.writeIdx) {
|
||||
__HAL_UART_DISABLE_IT(&mbusUart, UART_IT_TXE);
|
||||
__HAL_UART_ENABLE_IT(&mbusUart, UART_IT_TC);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// advance the state for the engine only when the last octet is shifted out completely
|
||||
if (((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET)) {
|
||||
__HAL_UART_DISABLE_IT(&mbusUart, UART_IT_TC);
|
||||
mbusCommHandle.state = MBCS_SENDING_DONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void mbusCommExec() {
|
||||
static uint8_t userdataIdx = 0;
|
||||
static uint8_t calculatedChksum = 0;
|
||||
uint8_t receivedOctet = 0;
|
||||
static uint32_t delayAfterSendStartTime = 0;
|
||||
|
||||
if ((mbusCommHandle.startTime != 0) && ((mbusCommHandle.startTime + 2500) < HAL_GetTick())) {
|
||||
mbusCommHandle.state = MBCS_TIMEOUT;
|
||||
@ -364,7 +380,16 @@ void mbusCommExec() {
|
||||
|
||||
case MBCS_SENDING_DONE:
|
||||
//coloredMsg(LOG_YELLOW, false, "mbc hre [%d] state SENDING_DONE", mbusCommHandle.requestId);
|
||||
delayAfterSendStartTime = HAL_GetTick();
|
||||
mbusCommHandle.state = MBCS_WAIT_AFTER_SENDING;
|
||||
break;
|
||||
|
||||
case MBCS_WAIT_AFTER_SENDING:
|
||||
// Avoids switching on the frontend while there is still noise from sending on the loop
|
||||
// Make sure to wait not too long otherwise the beginning of the response is missed
|
||||
if ((delayAfterSendStartTime + DELAY_AFTER_SENDING) < HAL_GetTick()) {
|
||||
mbusCommHandle.state = MBCS_ENABLE_FRONTEND;
|
||||
}
|
||||
break;
|
||||
|
||||
case MBCS_ENABLE_FRONTEND:
|
||||
@ -507,6 +532,9 @@ void mbusCommExec() {
|
||||
// coloredMsg(LOG_RED, false, "mbc hre [%d] state ERROR", mbusCommHandle.requestId);
|
||||
coloredMsg(LOG_RED, true, "mbc hre [%d] error", mbusCommHandle.requestId);
|
||||
show(LED_RED, ON);
|
||||
// disable frontend immediately in case of error since no more data in relevant
|
||||
// this avoids strange noise on RX line/led
|
||||
frontendDisable();
|
||||
mbusCommHandle.state = MBCS_ERROR_CONTINUED;
|
||||
// no break
|
||||
|
||||
@ -579,10 +607,28 @@ static e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice) {
|
||||
|
||||
#define PERIOD 10
|
||||
|
||||
static uint8_t numOfDevices = 8;
|
||||
static uint8_t numOfDevices = 0;
|
||||
static t_mbusDevice *devices = NULL;
|
||||
|
||||
void mbusCommAddDevice(t_deviceBlock *deviceBlock) {
|
||||
devices = (t_mbusDevice*) realloc((void*) devices, (numOfDevices + 1) * sizeof(t_mbusDevice));
|
||||
strcpy(devices[numOfDevices].deviceName, deviceBlock->deviceName);
|
||||
devices[numOfDevices].address = deviceBlock->address;
|
||||
memcpy(devices[numOfDevices].consideredField, deviceBlock->consideredField, sizeof(deviceBlock->consideredField));
|
||||
devices[numOfDevices].period = deviceBlock->period;
|
||||
devices[numOfDevices].requests = 0;
|
||||
devices[numOfDevices].failures = 0;
|
||||
devices[numOfDevices].delay = 0;
|
||||
devices[numOfDevices].waiting = false;
|
||||
devices[numOfDevices].active = true;
|
||||
coloredMsg(LOG_YELLOW, true, "mbc mbcad device added %d %s %d", numOfDevices, devices[numOfDevices].deviceName, devices[numOfDevices].address);
|
||||
numOfDevices += 1;
|
||||
}
|
||||
|
||||
/*
|
||||
static t_mbusDevice devices[] = {
|
||||
{
|
||||
.deviceName = "TotalPower",
|
||||
.deviceName = "Total",
|
||||
.address = 80,
|
||||
.consideredField = { 0, 17, -1, -1 },
|
||||
.requests = 0,
|
||||
@ -593,7 +639,7 @@ static t_mbusDevice devices[] = {
|
||||
.active = true
|
||||
},
|
||||
{
|
||||
.deviceName = "ComputerPower",
|
||||
.deviceName = "Computer",
|
||||
.address = 85,
|
||||
.consideredField = { 0, 4, 2, 3 },
|
||||
.requests = 0,
|
||||
@ -604,7 +650,7 @@ static t_mbusDevice devices[] = {
|
||||
.active = true
|
||||
},
|
||||
{
|
||||
.deviceName = "DryerPower",
|
||||
.deviceName = "Dryer",
|
||||
.address = 81,
|
||||
.consideredField = { 0, 4, 2, 3 },
|
||||
.requests = 0,
|
||||
@ -615,7 +661,7 @@ static t_mbusDevice devices[] = {
|
||||
.active = true
|
||||
},
|
||||
{
|
||||
.deviceName = "LaundryPower",
|
||||
.deviceName = "Laundry",
|
||||
.address = 82,
|
||||
.consideredField = { 0, 4, 2, 3 },
|
||||
.requests = 0,
|
||||
@ -626,7 +672,7 @@ static t_mbusDevice devices[] = {
|
||||
.active = true
|
||||
},
|
||||
{
|
||||
.deviceName = "DishwasherPower",
|
||||
.deviceName = "Dishwasher",
|
||||
.address = 83,
|
||||
.consideredField = { 0, 4, 2, 3 },
|
||||
.requests = 0,
|
||||
@ -637,7 +683,7 @@ static t_mbusDevice devices[] = {
|
||||
.active = true
|
||||
},
|
||||
{
|
||||
.deviceName = "LightPower",
|
||||
.deviceName = "Light",
|
||||
.address = 84,
|
||||
.consideredField = { 0, 4, 2, 3 },
|
||||
.requests = 0,
|
||||
@ -648,7 +694,7 @@ static t_mbusDevice devices[] = {
|
||||
.active = true
|
||||
},
|
||||
{
|
||||
.deviceName = "FreezerPower",
|
||||
.deviceName = "Freezer",
|
||||
.address = 86,
|
||||
.consideredField = { 0, 4, 2, 3 },
|
||||
.requests = 0,
|
||||
@ -659,7 +705,7 @@ static t_mbusDevice devices[] = {
|
||||
.active = true
|
||||
},
|
||||
{
|
||||
.deviceName = "FridgePower",
|
||||
.deviceName = "Fridge",
|
||||
.address = 87,
|
||||
.consideredField = { 0, 4, 2, 3 },
|
||||
.requests = 0,
|
||||
@ -670,6 +716,9 @@ static t_mbusDevice devices[] = {
|
||||
.active = true
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
static void triggerMBusRequest(void *handle) {
|
||||
@ -749,6 +798,8 @@ static void mbusCommScheduler(void *handle) {
|
||||
}
|
||||
|
||||
void mbusCommInit() {
|
||||
config = getConfig();
|
||||
|
||||
coloredMsg(LOG_GREEN, true, "mbc mci initializing Meterbus communication");
|
||||
|
||||
// enable receive interrupts
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <mbusComm.h>
|
||||
#include <oled.h>
|
||||
#include <eeprom.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
@ -19,17 +20,14 @@
|
||||
|
||||
extern const uint8_t MQTT_SOCK;
|
||||
|
||||
static t_configBlock *config;
|
||||
|
||||
client_t client;
|
||||
mqttClient_t mqttClient;
|
||||
|
||||
uint8_t brokerAddress[] = { 172, 16, 2, 16 };
|
||||
uint8_t brokerAddress[4];
|
||||
uint16_t brokerPort = 1883;
|
||||
|
||||
const static char WatchdogTopic[] = "IoT/Watchdog";
|
||||
const static char StartupTopic[] = "IoT/MBGW3/Startup";
|
||||
const static char StatusTopic[] = "IoT/MBGW3/Status";
|
||||
|
||||
|
||||
static uint32_t watchdogCounter = 0;
|
||||
|
||||
void watchdogHandler(void *handle) {
|
||||
@ -44,7 +42,7 @@ void watchdogHandler(void *handle) {
|
||||
|
||||
// typedef void (*callback_t)(char*, uint8_t*, uint16_t);
|
||||
static void mqttCallback(char *topic, uint8_t *payload, uint16_t payloadLength) {
|
||||
if (0 == strcmp(topic, WatchdogTopic)) {
|
||||
if (0 == strcmp(topic, config->watchdogTopic)) {
|
||||
watchdogCounter++;
|
||||
} else {
|
||||
coloredMsg(LOG_GREEN, false, "mqcb: %s : %.*s", topic, payloadLength, payload);
|
||||
@ -58,10 +56,10 @@ static void mqttStatusPublisher(void *handle) {
|
||||
|
||||
char buf[256];
|
||||
uint32_t uptime = HAL_GetTick() / 1000;
|
||||
snprintf(buf, sizeof(buf), "{\"uptime\":\"%ld\", \"runningHours\":\"%ld\", \"powerCycles\":\"%ld\", \"tasks\":\"%d\", \"requests\":\"%ld\", \"errors\":\"%ld\", \"octets\":\"%ld\", \"overrun\":\"%ld\", \"framing\":\"%ld\", \"parity\":\"%ld\"}",
|
||||
snprintf(buf, sizeof(buf), "{\"uptime\":\"%ld\", \"runningHours\":\"%ld\", \"powerCycles\":\"%ld\", \"tasks\":\"%d\", \"requests\":\"%ld\", \"errors\":\"%ld\", \"octets\":\"%ld\", \"overrun\":\"%ld\", \"framing\":\"%ld\", \"parity\":\"%ld\", \"noise\":\"%ld\"}",
|
||||
uptime, globalDeviceStats->totalRunningHours, globalDeviceStats->totalPowercycles, schTaskCnt(), mbusCommStats->mbusRequestCnt, mbusCommStats->mbusErrorCnt,
|
||||
mbusCommStats->uartOctetCnt, mbusCommStats->uartOverrunCnt, mbusCommStats->uartFramingErrCnt, mbusCommStats->uartParityErrCnt);
|
||||
bool res = publish(&mqttClient, StatusTopic, (const uint8_t*)buf, strlen(buf), false);
|
||||
mbusCommStats->uartOctetCnt, mbusCommStats->uartOverrunCnt, mbusCommStats->uartFramingErrCnt, mbusCommStats->uartParityErrCnt, mbusCommStats->uartNoiseErrCnt);
|
||||
bool res = publish(&mqttClient, config->statusTopic, (const uint8_t*)buf, strlen(buf), false);
|
||||
coloredMsg(LOG_GREEN, false, "mqch, publish returned %d", res);
|
||||
|
||||
oledSetActiveScreen(OLED_SCREEN1);
|
||||
@ -73,7 +71,7 @@ static void mqttStatusPublisher(void *handle) {
|
||||
oledPrintf(OLED_SCREEN1, "PowerCycl:%d", globalDeviceStats->totalPowercycles);
|
||||
oledPrintf(OLED_SCREEN1, "Req:%ld", mbusCommStats->mbusRequestCnt);
|
||||
oledPrintf(OLED_SCREEN1, "Err:%ld", mbusCommStats->mbusErrorCnt);
|
||||
oledPrintf(OLED_SCREEN1, "Uart:%d", mbusCommStats->uartOverrunCnt + mbusCommStats->uartFramingErrCnt + mbusCommStats->uartParityErrCnt);
|
||||
oledPrintf(OLED_SCREEN1, "Uart:%d", mbusCommStats->uartOverrunCnt + mbusCommStats->uartFramingErrCnt + mbusCommStats->uartParityErrCnt + mbusCommStats->uartNoiseErrCnt);
|
||||
}
|
||||
|
||||
void mqttCommHandler(void *handle) {
|
||||
@ -92,6 +90,11 @@ void mqttCommHandler(void *handle) {
|
||||
break;
|
||||
|
||||
case 1:
|
||||
coloredMsg(LOG_GREEN, false, "mqch, resolving broker name");
|
||||
if (! wizDnsQuery(config->brokerName, brokerAddress)) {
|
||||
coloredMsg(LOG_GREEN, false, "mqch, query for broker address failed, going to error state");
|
||||
state = 255;
|
||||
}
|
||||
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_GREEN, false, "mqch, mqttConnect returns %d", res);
|
||||
@ -106,7 +109,7 @@ void mqttCommHandler(void *handle) {
|
||||
|
||||
case 2:
|
||||
coloredMsg(LOG_GREEN, false, "mqch, publish start-up");
|
||||
res = publish(&mqttClient, StartupTopic, (const uint8_t*)message, strlen((char*)message), false);
|
||||
res = publish(&mqttClient, config->startupTopic, (const uint8_t*)message, strlen((char*)message), false);
|
||||
coloredMsg(LOG_GREEN, false, "mqch, publish returned %d", res);
|
||||
schAdd(mqttStatusPublisher, NULL, 0, 60000);
|
||||
coloredMsg(LOG_GREEN, false, "mqch, status publisher scheduled");
|
||||
@ -115,7 +118,7 @@ void mqttCommHandler(void *handle) {
|
||||
|
||||
case 3:
|
||||
coloredMsg(LOG_GREEN, false, "mqch, subscribe watchdog");
|
||||
res = subscribe(&mqttClient, WatchdogTopic, MQTTQOS0);
|
||||
res = subscribe(&mqttClient, config->watchdogTopic, MQTTQOS0);
|
||||
coloredMsg(LOG_GREEN, false, "mqch, subscribe returned %d", res);
|
||||
schAdd(watchdogHandler, NULL, 60000, 60000);
|
||||
coloredMsg(LOG_GREEN, false, "mqch, watchdogHandler scheduled");
|
||||
@ -145,6 +148,7 @@ void mqttCommHandler(void *handle) {
|
||||
}
|
||||
|
||||
void mqttCommInit() {
|
||||
config = getConfig();
|
||||
schAdd(mqttCommHandler, NULL, 0, 100);
|
||||
}
|
||||
|
||||
|
@ -8,3 +8,4 @@ const uint8_t DHCP_SOCK = 0;
|
||||
const uint8_t MQTT_SOCK = 1;
|
||||
const uint8_t CMD_SOCK = 2;
|
||||
const uint8_t SYSLOG_SOCK = 3;
|
||||
const uint8_t DNS_SOCK = 4;
|
||||
|
57
cube/User/Src/regularCmds.c
Normal file
57
cube/User/Src/regularCmds.c
Normal file
@ -0,0 +1,57 @@
|
||||
#include <cmdHelper.h>
|
||||
#include <logger.h>
|
||||
|
||||
#include <mbusComm.h>
|
||||
#include <eeprom.h>
|
||||
|
||||
|
||||
static bool globalStatsCmd(uint8_t argc, char **args) {
|
||||
uint32_t uptime = HAL_GetTick() / 1000;
|
||||
sendFormatString(\
|
||||
"Current uptime: %ld\n\r" \
|
||||
"\n\r",
|
||||
uptime
|
||||
);
|
||||
|
||||
t_deviceStats *deviceStats = getGlobalDeviceStats();
|
||||
sendFormatString(\
|
||||
"Global Device statistics\n\r" \
|
||||
" Total running hours: %ld\n\r" \
|
||||
" Total power cycles: %ld\n\r" \
|
||||
" Total requests: %ld\n\r" \
|
||||
" Total failures: %ld\n\r" \
|
||||
"\n\r",
|
||||
deviceStats->totalRunningHours, deviceStats->totalPowercycles,
|
||||
deviceStats->totalRequests, deviceStats->totalFailures
|
||||
);
|
||||
|
||||
t_mbusCommStats *mbusStats = mbusCommGetStats();
|
||||
sendFormatString(\
|
||||
"Global Meterbus/UART statistics\n\r" \
|
||||
" Meterbus Requests: %ld\n\r" \
|
||||
" Meterbus Errors: %ld\n\r" \
|
||||
" UART Octets: %ld\n\r" \
|
||||
" UART Overruns: %ld\n\r" \
|
||||
" UART Framing Errs: %ld\n\r" \
|
||||
" UART Parity Errs: %ld\n\r" \
|
||||
" UART Noise Errs: %ld\n\r",
|
||||
mbusStats->mbusRequestCnt, mbusStats->mbusErrorCnt,
|
||||
mbusStats->uartOctetCnt, mbusStats->uartOverrunCnt, mbusStats->uartFramingErrCnt, mbusStats->uartParityErrCnt, mbusStats->uartNoiseErrCnt
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
const cmd_t COMMANDS[] = {
|
||||
{ .name = "globalStats", .cmdFunc = globalStatsCmd,
|
||||
.help = \
|
||||
"globalStats .......................... Show the global statistics\n\r" \
|
||||
" counters requestCnt and errorCnt\n\r"
|
||||
},
|
||||
{ .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL }
|
||||
};
|
||||
|
||||
const cmd_t *getRegularCommands() {
|
||||
return COMMANDS;
|
||||
}
|
@ -11,16 +11,23 @@
|
||||
#include <dhcp.h>
|
||||
#include <show.h>
|
||||
#include <oled.h>
|
||||
#include <dns.h>
|
||||
#include <config.h>
|
||||
|
||||
wiz_NetInfo netInfo = {
|
||||
.mac = { 0x00, 0xA0, 0x57, 0x05, 0x3E, 0x0D },
|
||||
.dhcp = NETINFO_DHCP
|
||||
};
|
||||
|
||||
|
||||
static t_configBlock *config;
|
||||
|
||||
wiz_NetInfo netInfo;
|
||||
|
||||
#define DHCP_BUFFER_SIZE 2048
|
||||
static uint8_t dhcpBuffer[DHCP_BUFFER_SIZE];
|
||||
|
||||
#define DNS_BUFFER_SIZE MAX_DNS_BUF_SIZE
|
||||
static uint8_t dnsBuffer[DNS_BUFFER_SIZE];
|
||||
|
||||
extern const uint8_t DHCP_SOCK;
|
||||
extern const uint8_t DNS_SOCK;
|
||||
|
||||
static bool networkAvailable = false;
|
||||
|
||||
@ -109,7 +116,22 @@ static void wizDHCPHandler(void *handle) {
|
||||
}
|
||||
}
|
||||
|
||||
bool wizDnsQuery(char *name, uint8_t *ip) {
|
||||
bool retCode = false;
|
||||
coloredMsg(LOG_BLUE, false, "wizdq, querying for %s", name);
|
||||
int8_t res = DNS_run(netInfo.dns, (uint8_t*) name, ip);
|
||||
coloredMsg(LOG_BLUE, false, "wizdq, DNS_run returns %d", res);
|
||||
retCode = (res == 1);
|
||||
if (retCode) {
|
||||
coloredMsg(LOG_BLUE, false, "wizdq, got address %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
|
||||
}
|
||||
return retCode;
|
||||
}
|
||||
|
||||
static void wizPhyLinkHandler(void *handle) {
|
||||
// this handler is anyhow called with a 1s period, so we reuse it for the DNS timer
|
||||
DNS_time_handler();
|
||||
|
||||
static uint8_t lastStablePhyLink = 255;
|
||||
static bool dhcpInitialized = false;
|
||||
|
||||
@ -153,7 +175,12 @@ static void wizPhyLinkHandler(void *handle) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int wizInit() {
|
||||
config = getConfig();
|
||||
netInfo.dhcp = NETINFO_DHCP;
|
||||
memcpy(netInfo.mac, config->macAddress, 6);
|
||||
|
||||
coloredMsg(LOG_BLUE, false, "wizI, resetting Ethernet module");
|
||||
wizReset(true);
|
||||
activeDelay(2);
|
||||
@ -196,6 +223,7 @@ int wizInit() {
|
||||
res = ctlwizchip(CW_GET_ID, (void*) buf);
|
||||
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]);
|
||||
|
||||
DNS_init(DNS_SOCK, dnsBuffer);
|
||||
|
||||
schAdd(wizPhyLinkHandler, NULL, 0, 1000);
|
||||
coloredMsg(LOG_BLUE, false, "wizI, PhyLink handler scheduled");
|
||||
|
@ -47,8 +47,11 @@ cat $MAIN_C_BAK | \
|
||||
cp $IT_C $IT_C_BAK
|
||||
echo "// $PROCESSED" > $IT_C
|
||||
cat $IT_C_BAK | \
|
||||
sed -e 's,\(/\* USER CODE BEGIN Includes \*/\),\1\n#include "main2.h"\n,' | \
|
||||
sed -e 's,\(/\* USER CODE BEGIN SysTick_IRQn 1 \*/\),\1\n SYSTICK_Callback();\n,' >> $IT_C
|
||||
sed -e 's,\(/\* USER CODE BEGIN Includes \*/\),\1\n#include "main2.h"\n,' \
|
||||
-e 's,\(/\* USER CODE BEGIN SysTick_IRQn 1 \*/\),\1\n SYSTICK_Callback();\n,' \
|
||||
-e 's,\(HAL_UART_IRQHandler(&huart5);\),// \1,' \
|
||||
-e 's,\(/\* USER CODE BEGIN UART5_IRQn 1 \*/\),\1\n mbusCommISR();\n,' \
|
||||
>> $IT_C
|
||||
|
||||
# mkdir w5500
|
||||
# pushd ioLibrary_Driver
|
||||
@ -81,6 +84,7 @@ cat $MAKEFILE_BAK | \
|
||||
sed -e 's,\(C_SOURCES = \\\),\1\n'"$SRC_EXT"' \\,' | \
|
||||
sed -e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Ethernet \\,' | \
|
||||
sed -e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Internet/DHCP \\,' | \
|
||||
sed -e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Internet/DNS \\,' | \
|
||||
sed -e 's,\(C_INCLUDES = \\\),\1\n-IUser/Inc \\,' | \
|
||||
sed -e 's,\(C_INCLUDES = \\\),\1\n-Ilibmbus \\,' | \
|
||||
sed -e 's,\(C_INCLUDES = \\\),\1\n-Ipubsubc/src \\,' | \
|
||||
|
Reference in New Issue
Block a user