From 73ac869f262d423926f978e3e854959d9ec5735f Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 10:29:12 +0100 Subject: [PATCH 01/24] drop cmd stuff --- cube/Makefile | 111 +++++++----- cube/User/Inc/cmdHandler.h | 9 - cube/User/Inc/cmdHelper.h | 23 --- cube/User/Inc/eeprom.h | 1 - cube/User/Src/cmdHandler.c | 326 ----------------------------------- cube/User/Src/configCmds.c | 212 ----------------------- cube/User/Src/eeprom.c | 2 +- cube/User/Src/main2.c | 7 - cube/User/Src/regularCmds.c | 52 ------ tools/insertMyCode.sh | 38 ++-- tools/removeGeneratedCode.sh | 2 +- 11 files changed, 85 insertions(+), 698 deletions(-) delete mode 100644 cube/User/Inc/cmdHandler.h delete mode 100644 cube/User/Inc/cmdHelper.h delete mode 100644 cube/User/Src/cmdHandler.c delete mode 100644 cube/User/Src/configCmds.c delete mode 100644 cube/User/Src/regularCmds.c diff --git a/cube/Makefile b/cube/Makefile index 6da5d48..48e001f 100644 --- a/cube/Makefile +++ b/cube/Makefile @@ -1,8 +1,11 @@ -# Processed by ../tools/insertMyCode.sh +# Processed by ../tools/insertMyCode.sh ########################################################################################################################## # File automatically-generated by tool: [projectgenerator] version: [3.10.0-B14] date: [Sat Feb 13 18:28:59 CET 2021] ########################################################################################################################## +# FILE NOT LONGER UNDER CONTROL OF THE GENERATOR BUT MANUALLY MAINTAINED, 2020-02-16 # + + # ------------------------------------------------ # Generic Makefile (based on gcc) # @@ -11,6 +14,9 @@ # 2015-07-22 - first version # ------------------------------------------------ + +NETWORK = LAN + ###################################### # target ###################################### @@ -36,36 +42,47 @@ BUILD_DIR = build # source ###################################### # C sources -C_SOURCES = \ -User/Src/cmdHandler.c User/Src/config.c User/Src/configCmds.c User/Src/counter.c User/Src/eeprom.c User/Src/logger.c User/Src/main2.c User/Src/ports.c User/Src/regularCmds.c User/Src/ringbuffer.c User/Src/sha256.c User/Src/show.c User/Src/utils.c User/Src/wizHelper.c hottislib/PontCoopScheduler.c \ -Core/Src/main.c \ -Core/Src/gpio.c \ -Core/Src/iwdg.c \ -Core/Src/spi.c \ -Core/Src/tim.c \ -Core/Src/usart.c \ -Core/Src/stm32f1xx_it.c \ -Core/Src/stm32f1xx_hal_msp.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_iwdg.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_spi.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c \ +C_SOURCES = \ +User/Src/config.c \ +User/Src/counter.c \ +User/Src/eeprom.c \ +User/Src/logger.c \ +User/Src/main2.c \ +User/Src/ports.c \ +User/Src/ringbuffer.c \ +User/Src/sha256.c \ +User/Src/show.c \ +User/Src/utils.c \ +User/Src/wizHelper.c \ +hottislib/PontCoopScheduler.c \ +Core/Src/main.c \ +Core/Src/gpio.c \ +Core/Src/iwdg.c \ +Core/Src/spi.c \ +Core/Src/tim.c \ +Core/Src/usart.c \ +Core/Src/stm32f1xx_it.c \ +Core/Src/stm32f1xx_hal_msp.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_iwdg.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_spi.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c \ Core/Src/system_stm32f1xx.c # ASM sources -ASM_SOURCES = \ +ASM_SOURCES = \ startup_stm32f103xb.s @@ -109,8 +126,8 @@ MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI) AS_DEFS = # C defines -C_DEFS = \ --DUSE_HAL_DRIVER \ +C_DEFS = \ +-DUSE_HAL_DRIVER \ -DSTM32F103xB @@ -118,25 +135,25 @@ C_DEFS = \ AS_INCLUDES = # C includes -C_INCLUDES = \ --Ihottislib \ --IUser/Inc \ --IioLibrary_Driver/Internet/SNTP \ --IioLibrary_Driver/Internet/DNS \ --IioLibrary_Driver/Internet/DHCP \ --IioLibrary_Driver/Ethernet \ --ICore/Inc \ --IDrivers/STM32F1xx_HAL_Driver/Inc \ --IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \ --IDrivers/CMSIS/Device/ST/STM32F1xx/Include \ --IDrivers/CMSIS/Include \ +C_INCLUDES = \ +-Ihottislib \ +-IUser/Inc \ +-IioLibrary_Driver/Internet/SNTP \ +-IioLibrary_Driver/Internet/DNS \ +-IioLibrary_Driver/Internet/DHCP \ +-IioLibrary_Driver/Ethernet \ +-ICore/Inc \ +-IDrivers/STM32F1xx_HAL_Driver/Inc \ +-IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \ +-IDrivers/CMSIS/Device/ST/STM32F1xx/Include \ +-IDrivers/CMSIS/Include \ -IDrivers/CMSIS/Include # compile gcc flags ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -Werror -fdata-sections -ffunction-sections -CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -Werror -fdata-sections -ffunction-sections +CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -DNETWORK="\"$(NETWORK)\"" -Wall -Werror -fdata-sections -ffunction-sections ifeq ($(DEBUG), 1) CFLAGS += -g -gdwarf-2 @@ -168,14 +185,14 @@ all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET # list of objects OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o))) vpath %.c $(sort $(dir $(C_SOURCES))) -OBJECTS += $(addprefix $(BUILD_DIR)/,w5500.a) +OBJECTS += $(addprefix $(BUILD_DIR)/,w5500.a) # list of ASM program objects OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o))) vpath %.s $(sort $(dir $(ASM_SOURCES))) -$(BUILD_DIR)/w5500.a: - (cd ioLibrary_Driver && $(MAKE) && cp w5500.a ../$(BUILD_DIR) && cd ..) - +$(BUILD_DIR)/w5500.a: + (cd ioLibrary_Driver && $(MAKE) && cp w5500.a ../$(BUILD_DIR) && cd ..) + $(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@ diff --git a/cube/User/Inc/cmdHandler.h b/cube/User/Inc/cmdHandler.h deleted file mode 100644 index 41e14c0..0000000 --- a/cube/User/Inc/cmdHandler.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef _CMDHANDLER_H_ -#define _CMDHANDLER_H_ - -#include - -void cmdHandlerInit(); - - -#endif /* _CMDHANDLER_H_ */ diff --git a/cube/User/Inc/cmdHelper.h b/cube/User/Inc/cmdHelper.h deleted file mode 100644 index 9bfa723..0000000 --- a/cube/User/Inc/cmdHelper.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef _CMDHELPER_H_ -#define _CMDHELPER_H_ - -#include -#include - - -typedef bool (*cmdFunc_t)(uint8_t argc, char **args); - -typedef struct { - char name[24]; - 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_ */ diff --git a/cube/User/Inc/eeprom.h b/cube/User/Inc/eeprom.h index 3a6d249..fafcb7e 100644 --- a/cube/User/Inc/eeprom.h +++ b/cube/User/Inc/eeprom.h @@ -40,7 +40,6 @@ 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 eepromHourlyUpdateDeviceStats(void *handle); diff --git a/cube/User/Src/cmdHandler.c b/cube/User/Src/cmdHandler.c deleted file mode 100644 index f3db424..0000000 --- a/cube/User/Src/cmdHandler.c +++ /dev/null @@ -1,326 +0,0 @@ -#include -#include - -#include -#include -#include -#include -#include -#include - -#include - -#include -#include -#include -#include - - -extern const uint8_t CMD_SOCK; - -const uint16_t cmdPort = 23; - -typedef enum { - CH_INIT, - CH_LISTEN, - CH_WAITING, - CH_BANNER, - CH_PROMPT, - CH_RECEIVE, - CH_DISCONNECT, - CH_DISCONNECT_WAIT, - CH_ERROR -} chState_t; - -void sendString(const char *buf) { - send(CMD_SOCK, (uint8_t*)buf, strlen(buf)); -} - -bool sendFormatString(const char *format, ...) { - bool retCode = true; - 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; -} - -// returns 0 to continue waiting for input -// returns -1 to close the connection -// 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" \ - "config ............................... Enter configuration 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 UNKNOWN_COMMAND[] = "Unknown command\n\r"; - uint8_t *messageToSend = NULL; - - static bool configMode = false; - - if (resetSpecialModes) { - configMode = false; - } - - cmd_t const * commands = getRegularCommands(); - if (configMode) { - commands = getConfigCommands(); - } - - coloredMsg(LOG_YELLOW, "ch cec cmdLine is %s", cmdLine);; - - #define MAX_NUM_OF_ARGS 8 - char *args[MAX_NUM_OF_TASKS]; - uint8_t argc = 0; - char *inCmdLine = (char*)cmdLine; - do { - args[argc++] = strtok(inCmdLine, " "); - inCmdLine = NULL; - } while (args[argc - 1] != NULL); - if (argc > 0) { - argc--; - } - char *cmd = args[0]; - - int8_t retCode = 0; - coloredMsg(LOG_YELLOW, "ch cec cmd is %s, number of arguments %d", cmd, argc); - - if (0 == strcmp(cmd, "quit")) { - messageToSend = (uint8_t*)GOODBYE_MSG; - retCode = -1; - } else if (0 == strcmp(cmd, "help")) { - if (configMode) { - sendString(CONFIG_INTRO_MSG); - } - sendString(HELP_MSG); - uint8_t cmdIdx = 0; - while (true) { - cmd_t command = commands[cmdIdx]; - if (0 == strcmp("END_OF_CMDS", command.name)) { - break; - } - sendString(command.help); - cmdIdx++; - } - messageToSend = NULL; - } else if (0 == strcmp(cmd, "config")) { - coloredMsg(LOG_YELLOW, "ch cec enable config mode"); - configMode = true; - retCode = 2; - } else { - uint8_t cmdIdx = 0; - while (true) { - 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)) { - messageToSend = command.cmdFunc(argc, args) ? (uint8_t*)OK_MSG : (uint8_t*)FAILED_MSG; - break; - } - cmdIdx++; - } - } - - if (messageToSend) { - coloredMsg(LOG_YELLOW, "ch cec command finally returns %s", messageToSend); - send(CMD_SOCK, messageToSend, strlen((char*)messageToSend)); - } - - return retCode; -} - -static void cmdHandlerEngine(void *handle) { - static uint8_t receiveBuffer[256]; - - static chState_t state = CH_INIT; - static bool resetSpecialModes = false; - - static char banner[] = \ - "MBGW3\n\r" \ - "Type help for usage help\n\r" \ - "or quit to close the connection.\n\r"; - - static char *prompt; - static char defaultPrompt[] = " > "; - static char configPrompt[] = " (config) $ "; - - - int8_t res = 0; - uint8_t sockState; - int32_t resultSend; - int16_t receivedOctets; - int32_t resultRecv; - uint8_t resultDisconnect; - - - if (isNetworkAvailable()) { - switch (state) { - case CH_INIT: - coloredMsg(LOG_YELLOW, "ch che initializing socket"); - - res = socket(CMD_SOCK, Sn_MR_TCP, cmdPort, SF_IO_NONBLOCK); - coloredMsg(LOG_YELLOW, "ch che socket returns %d", res); - - if (res == CMD_SOCK) { - coloredMsg(LOG_YELLOW, "ch che socket is initialized"); - state = CH_LISTEN; - } else { - state = CH_ERROR; - } - break; - - case CH_LISTEN: - coloredMsg(LOG_YELLOW, "ch che listening"); - - res = listen(CMD_SOCK); - coloredMsg(LOG_YELLOW, "ch che listen returns %d", res); - - if (res == SOCK_OK) { - coloredMsg(LOG_YELLOW, "ch che ok, waiting for established"); - state = CH_WAITING; - } else { - state = CH_ERROR; - } - break; - - case CH_WAITING: - sockState = getSn_SR(CMD_SOCK); - if (sockState != SOCK_LISTEN) { - coloredMsg(LOG_YELLOW, "ch che socket state is 0x%02x", sockState); - state = CH_DISCONNECT; - } - - if (sockState == SOCK_ESTABLISHED) { - coloredMsg(LOG_YELLOW, "ch che connection is established"); - state = CH_BANNER; - } - break; - - case CH_BANNER: - coloredMsg(LOG_YELLOW, "ch che send banner"); - sockState = getSn_SR(CMD_SOCK); - if (sockState != SOCK_ESTABLISHED) { - coloredMsg(LOG_YELLOW, "ch che sockState is 0x%02x when trying to send banner", sockState); - state = CH_DISCONNECT; - } else { - resultSend = send(CMD_SOCK, (uint8_t*)banner, strlen(banner)); - coloredMsg(LOG_YELLOW, "ch che sent banner, send returns 0x%02x", resultSend); - prompt = defaultPrompt; - resetSpecialModes = true; - state = CH_PROMPT; - } - break; - - case CH_PROMPT: - coloredMsg(LOG_YELLOW, "ch che send prompt"); - sockState = getSn_SR(CMD_SOCK); - if (sockState != SOCK_ESTABLISHED) { - coloredMsg(LOG_YELLOW, "ch che sockState is 0x%02x when trying to send promt", sockState); - state = CH_DISCONNECT; - } else { - sendFormatString("%s %s", getConfig()->deviceName, prompt); - coloredMsg(LOG_YELLOW, "ch che sent prompt %s %s", getConfig()->deviceName, prompt); - state = CH_RECEIVE; - } - break; - - case CH_RECEIVE: - sockState = getSn_SR(CMD_SOCK); - if (sockState != SOCK_ESTABLISHED) { - coloredMsg(LOG_YELLOW, "ch che sockState is 0x%02x when trying to receive something", sockState); - state = CH_DISCONNECT; - } else { - receivedOctets = getSn_RX_RSR(CMD_SOCK); - - if (receivedOctets > 0) { - memset(receiveBuffer, 0, sizeof(receiveBuffer)); - resultRecv = recv(CMD_SOCK, receiveBuffer, sizeof(receiveBuffer)); - coloredMsg(LOG_YELLOW, "ch che recv returns 0x%02x", resultRecv); - if (resultRecv > 0) { - if ((receiveBuffer[strlen((char*)receiveBuffer) - 1] == 0x0a) || - (receiveBuffer[strlen((char*)receiveBuffer) - 1] == 0x0d)) { - receiveBuffer[strlen((char*)receiveBuffer) - 1] = 0; - } - if ((receiveBuffer[strlen((char*)receiveBuffer) - 1] == 0x0a) || - (receiveBuffer[strlen((char*)receiveBuffer) - 1] == 0x0d)) { - receiveBuffer[strlen((char*)receiveBuffer) - 1] = 0; - } - coloredMsg(LOG_YELLOW, "ch che received: %s", receiveBuffer); - int8_t resCEC = cmdExecuteCommand(receiveBuffer, resetSpecialModes); - resetSpecialModes = false; - switch (resCEC) { - case 0: - state = CH_PROMPT; - break; - case -1: - state = CH_DISCONNECT; - break; - case 2: - prompt = configPrompt; - state = CH_PROMPT; - break; - case 3: - prompt = defaultPrompt; - state = CH_PROMPT; - break; - default: - state = CH_DISCONNECT; - break; - } - } - } - } - break; - - case CH_DISCONNECT: - coloredMsg(LOG_YELLOW, "ch che close our end"); - resultDisconnect = disconnect(CMD_SOCK); - coloredMsg(LOG_YELLOW, "ch che disconnect returns 0x%02x", resultDisconnect); - state = CH_DISCONNECT_WAIT; - break; - - case CH_DISCONNECT_WAIT: - //coloredMsg(LOG_YELLOW, "ch che waiting after disconnect"); - sockState = getSn_SR(CMD_SOCK); - //coloredMsg(LOG_YELLOW, "ch che sockState is 0x%02x", sockState); - if (sockState == SOCK_CLOSED) { - coloredMsg(LOG_YELLOW, "ch che socket is closed now"); - state = CH_INIT; - } - break; - - - case CH_ERROR: - coloredMsg(LOG_YELLOW, "ch che error state, will stop here"); - schDel(cmdHandlerEngine, NULL); - state = CH_INIT; - schAdd(cmdHandlerEngine, NULL, 5000, 100); - coloredMsg(LOG_YELLOW, "ch che restart command handler engine in 5s"); - break; - } - } -} - -void cmdHandlerInit() { - schAdd(cmdHandlerEngine, NULL, 0, 100); -} \ No newline at end of file diff --git a/cube/User/Src/configCmds.c b/cube/User/Src/configCmds.c deleted file mode 100644 index d05c397..0000000 --- a/cube/User/Src/configCmds.c +++ /dev/null @@ -1,212 +0,0 @@ -#include -#include - -#include -#include - -#include -#include - - - -static bool showConfigCmd(uint8_t argc, char **args) { - bool retCode = true; - - t_configBlock configBlock; - eepromReadConfigBlock(&configBlock); - sendString("This is the saved configuration.\n\r"); - sendString("It is not necessarily the active configuration.\n\r"); - 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("NTP Server: %s\n\r", configBlock.ntpServer); - sendFormatString("deviceId: %s\n\r", configBlock.deviceId); - sendFormatString("sharedSecret: (will not be displayed)\n\r"); - sendFormatString("Sink Server: %s\n\r", configBlock.sinkServer); - - return retCode; -} - -static bool setStringParameterCmd(uint8_t argc, char **args, size_t offset, size_t length) { - bool retCode = true; - - t_configBlock configBlock; - char *parameterName = args[1]; - char *newParameterValue = args[2]; - if (strlen(newParameterValue) >= length) { - sendString("given new value for is too long\n\r"); - retCode = false; - } else { - sendFormatString("set %s to %s\n\r", parameterName, newParameterValue); - - eepromReadConfigBlock(&configBlock); - strcpy((((char*)&configBlock) + offset), newParameterValue); - eepromWriteConfigBlock(&configBlock); - } - - return retCode; -} - -/* -static bool setInt32ParameterCmd(uint8_t argc, char **args, size_t offset, int32_t minV, int32_t maxV) { - bool retCode = true; - - t_configBlock configBlock; - char *parameterName = args[1]; - char *newParameterValue = args[2]; - long int value = strtol(newParameterValue, NULL, 10); - if (value < minV) { - sendString("value is too small\n\r"); - retCode = false; - } else if (value > maxV) { - sendString("value is too large\n\r"); - } else { - int32_t v = (int32_t) value; - sendFormatString("set %s to %ld\n\r", parameterName, v); - - eepromReadConfigBlock(&configBlock); - *((int32_t*)(((uint8_t*)&configBlock) + offset)) = v; - eepromWriteConfigBlock(&configBlock); - } - - return retCode; -} -*/ - -static bool setDeviceNameCmd(uint8_t argc, char **args) { - return setStringParameterCmd(argc, args, - offsetof(t_configBlock, deviceName), - sizeof(((t_configBlock*)0)->deviceName)); -} - -static bool setNtpServerCmd(uint8_t argc, char **args) { - return setStringParameterCmd(argc, args, - offsetof(t_configBlock, ntpServer), - sizeof(((t_configBlock*)0)->ntpServer)); -} - -static bool setDeviceIdCmd(uint8_t argc, char **args) { - return setStringParameterCmd(argc, args, - offsetof(t_configBlock, deviceId), - sizeof(((t_configBlock*)0)->deviceId)); -} - -static bool setSharedSecretCmd(uint8_t argc, char **args) { - return setStringParameterCmd(argc, args, - offsetof(t_configBlock, sharedSecret), - sizeof(((t_configBlock*)0)->sharedSecret)); -} - -static bool setSinkServerCmd(uint8_t argc, char **args) { - return setStringParameterCmd(argc, args, - offsetof(t_configBlock, sinkServer), - sizeof(((t_configBlock*)0)->sinkServer)); -} - - -const static cmd_t SET_COMMANDS[] = { - { .name = "devicename", .cmdFunc = setDeviceNameCmd, - .help = \ - "devicename ........................... Name of this device\n\r" - }, - { .name = "ntpserver", .cmdFunc = setNtpServerCmd, - .help = \ - "ntpserver ............................ Name of the NTP server\n\r" - }, - { .name = "deviceid", .cmdFunc = setDeviceIdCmd, - .help = \ - "deviceid ............................. ID of this device\n\r" - }, - { .name = "sharedSecret", .cmdFunc = setSharedSecretCmd, - .help = \ - "sharedsecret ......................... Shared secret\n\r" - }, - { .name = "sinkserver", .cmdFunc = setSinkServerCmd, - .help = \ - "sinkserver ........................... Name of the sink server\n\r" - }, - { .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL } -}; - -const static char UNKNOWN_PARAMETER[] = "unknown parameter\n\r"; -const static char OK_MSG[] = "OK\n\r"; -const static char FAILED_MSG[] = "Failed\n\r"; - -static bool setCmd(uint8_t argc, char **args) { - bool retCode = false; - uint8_t *messageToSend = NULL; - - char *cmd = args[1]; - if (argc >= 2) { - if (0 == strcmp("help", cmd)) { - sendString("You can set the following parameters:\n\r"); - uint8_t cmdIdx = 0; - while (true) { - cmd_t command = SET_COMMANDS[cmdIdx]; - if (0 == strcmp("END_OF_CMDS", command.name)) { - break; - } - sendString(command.help); - cmdIdx++; - } - retCode = true; - } else { - uint8_t cmdIdx = 0; - while (true) { - cmd_t command = SET_COMMANDS[cmdIdx]; - if (0 == strcmp("END_OF_CMDS", command.name)) { - messageToSend = (uint8_t*) UNKNOWN_PARAMETER; - break; - } - if (0 == strcmp(cmd, command.name)) { - retCode = command.cmdFunc(argc, args); - messageToSend = retCode ? (uint8_t*)OK_MSG : (uint8_t*)FAILED_MSG; - sendString("Remember you need to restart to active this change.\n\r"); - break; - } - cmdIdx++; - } - sendString((const char*)messageToSend); - } - } - - return retCode; -} - -static bool restartCmd(uint8_t argc, char **args) { - HAL_NVIC_SystemReset(); - // you won't come here ... - return true; -} - - - -const cmd_t CONFIG_COMMANDS[] = { - { .name = "show", .cmdFunc = showConfigCmd, - .help = \ - "show ................................. Show the configuration\n\r" - }, - { .name = "set", .cmdFunc = setCmd, - .help = \ - "set .................................. Set configuration parameters\n\r" \ - " Argument help gives a list of \n\r" \ - " parameters\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; -} \ No newline at end of file diff --git a/cube/User/Src/eeprom.c b/cube/User/Src/eeprom.c index 7d04925..60d9494 100644 --- a/cube/User/Src/eeprom.c +++ b/cube/User/Src/eeprom.c @@ -130,7 +130,7 @@ void eepromReadConfigBlock(t_configBlock *destConfigBlock) { } } -void eepromWriteConfigBlock(t_configBlock *srcConfigBlock) { +static 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); diff --git a/cube/User/Src/main2.c b/cube/User/Src/main2.c index b743a4d..080d672 100644 --- a/cube/User/Src/main2.c +++ b/cube/User/Src/main2.c @@ -14,7 +14,6 @@ #include #include #include -#include #include #include @@ -40,12 +39,6 @@ void my_setup_2() { wizInit(); - if (HAL_GPIO_ReadPin(ADMIN_MODE_GPIO_Port, ADMIN_MODE_Pin)) { - show(LED_BLUE, BLINK); - coloredMsg(LED_RED, "STARTING ADMIN MODE"); - cmdHandlerInit(); - } - counterInit(); logMsg("Application running"); diff --git a/cube/User/Src/regularCmds.c b/cube/User/Src/regularCmds.c deleted file mode 100644 index 89a7044..0000000 --- a/cube/User/Src/regularCmds.c +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include -#include - -#include - - -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 watchdog resets: %ld\n\r" \ - "\n\r", - deviceStats->totalRunningHours, deviceStats->totalPowercycles, - deviceStats->totalWatchdogResets - ); - - return true; -} - -static bool resetStatsCmd(uint8_t argc, char **args) { - t_deviceStats *deviceStats = getGlobalDeviceStats(); - deviceStats->totalWatchdogResets = 0; - schAdd(eepromHourlyUpdateDeviceStats, NULL, 1, 0); - return true; -} - - -const cmd_t COMMANDS[] = { - { .name = "stats", .cmdFunc = globalStatsCmd, - .help = \ - "stats .......................... Show the device statistics\n\r" - }, - { .name = "resetStats", .cmdFunc = resetStatsCmd, - .help = \ - "resetStats ..................... Reset the device statistics\n\r" - }, - { .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL } -}; - -const cmd_t *getRegularCommands() { - return COMMANDS; -} \ No newline at end of file diff --git a/tools/insertMyCode.sh b/tools/insertMyCode.sh index 940970c..530f298 100755 --- a/tools/insertMyCode.sh +++ b/tools/insertMyCode.sh @@ -6,8 +6,8 @@ MAIN_C_BAK=${MAIN_C}-bak IT_C=./Core/Src/stm32f1xx_it.c IT_C_BAK=${IT_C}-bak -MAKEFILE=./Makefile -MAKEFILE_BAK=${MAKEFILE}-bak +# MAKEFILE=./Makefile +# MAKEFILE_BAK=${MAKEFILE}-bak PROCESSED="Processed by $0" @@ -31,7 +31,7 @@ checkFile () { checkFile $MAIN_C $MAIN_C_BAK -checkFile $MAKEFILE $MAKEFILE_BAK +# checkFile $MAKEFILE $MAKEFILE_BAK checkFile $IT_C $IT_C_BAK cp $MAIN_C $MAIN_C_BAK @@ -62,22 +62,22 @@ for I in hottislib/*.c; do SRC_EXT+="$I " done -cp $MAKEFILE $MAKEFILE_BAK -echo "# $PROCESSED" > $MAKEFILE -cat $MAKEFILE_BAK | \ - sed -e 's/\(-specs=nano.specs\)/\1 -u _printf_float/' \ - -e 's/\(-Wall\)/\1 -Werror/' \ - -e 's%\(# list of ASM program objects\)%OBJECTS += $(addprefix $(BUILD_DIR)/,w5500.a)\n\1%' \ - -e 's,\($(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)\),$(BUILD_DIR)/w5500.a:\n\t(cd ioLibrary_Driver \&\& $(MAKE) \&\& cp w5500.a ../$(BUILD_DIR) \&\& cd ..)\n\n\1,' \ - -e 's,\(C_SOURCES = \\\),\1\n'"$SRC_EXT"' \\,' \ - -e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Ethernet \\,' \ - -e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Internet/DHCP \\,' \ - -e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Internet/DNS \\,' \ - -e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Internet/SNTP \\,' \ - -e 's,\(C_INCLUDES = \\\),\1\n-IUser/Inc \\,' \ - -e 's,\(C_INCLUDES = \\\),\1\n-Ihottislib \\,' \ - >> $MAKEFILE - +# cp $MAKEFILE $MAKEFILE_BAK +# echo "# $PROCESSED" > $MAKEFILE +# cat $MAKEFILE_BAK | \ +# sed -e 's/\(-specs=nano.specs\)/\1 -u _printf_float/' \ +# -e 's/\(-Wall\)/\1 -Werror/' \ +# -e 's%\(# list of ASM program objects\)%OBJECTS += $(addprefix $(BUILD_DIR)/,w5500.a)\n\1%' \ +# -e 's,\($(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)\),$(BUILD_DIR)/w5500.a:\n\t(cd ioLibrary_Driver \&\& $(MAKE) \&\& cp w5500.a ../$(BUILD_DIR) \&\& cd ..)\n\n\1,' \ +# -e 's,\(C_SOURCES = \\\),\1\n'"$SRC_EXT"' \\,' \ +# -e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Ethernet \\,' \ +# -e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Internet/DHCP \\,' \ +# -e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Internet/DNS \\,' \ +# -e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Internet/SNTP \\,' \ +# -e 's,\(C_INCLUDES = \\\),\1\n-IUser/Inc \\,' \ +# -e 's,\(C_INCLUDES = \\\),\1\n-Ihottislib \\,' \ +# >> $MAKEFILE +cp Makefile.sav Makefile diff --git a/tools/removeGeneratedCode.sh b/tools/removeGeneratedCode.sh index b9d15f8..674937a 100755 --- a/tools/removeGeneratedCode.sh +++ b/tools/removeGeneratedCode.sh @@ -1,3 +1,3 @@ #!/bin/bash - +cp Makefile Makefile.sav rm -rf build/ Core/ Drivers/ Makefile Makefile-bak startup_stm32f103xb.s STM32F103C8Tx_FLASH.ld From e9b60d4058a631edbd76454a51c067b8761a4a7c Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 10:30:55 +0100 Subject: [PATCH 02/24] config write still required --- cube/User/Inc/eeprom.h | 1 + cube/User/Src/eeprom.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/cube/User/Inc/eeprom.h b/cube/User/Inc/eeprom.h index fafcb7e..3a6d249 100644 --- a/cube/User/Inc/eeprom.h +++ b/cube/User/Inc/eeprom.h @@ -40,6 +40,7 @@ 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 eepromHourlyUpdateDeviceStats(void *handle); diff --git a/cube/User/Src/eeprom.c b/cube/User/Src/eeprom.c index 60d9494..7d04925 100644 --- a/cube/User/Src/eeprom.c +++ b/cube/User/Src/eeprom.c @@ -130,7 +130,7 @@ void eepromReadConfigBlock(t_configBlock *destConfigBlock) { } } -static void eepromWriteConfigBlock(t_configBlock *srcConfigBlock) { +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); From bfbcc309df5953811e772e121d9fbd47bdae7177 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 10:37:09 +0100 Subject: [PATCH 03/24] add network abstraction layer --- cube/Makefile | 9 +++++++-- cube/User/Inc/networkAbstractionLayer.h | 9 +++++++++ cube/User/Src/main2.c | 7 +++++-- cube/User/Src/networkAbstractionLayer.c | 8 ++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 cube/User/Inc/networkAbstractionLayer.h create mode 100644 cube/User/Src/networkAbstractionLayer.c diff --git a/cube/Makefile b/cube/Makefile index 48e001f..be3632e 100644 --- a/cube/Makefile +++ b/cube/Makefile @@ -15,7 +15,11 @@ # ------------------------------------------------ -NETWORK = LAN +# LAN = 1 +# WiFi = 2 +# GSM = 3 +NETWORK = 1 + ###################################### # target @@ -54,6 +58,7 @@ User/Src/sha256.c \ User/Src/show.c \ User/Src/utils.c \ User/Src/wizHelper.c \ +User/Src/networkAbstractionLayer.c \ hottislib/PontCoopScheduler.c \ Core/Src/main.c \ Core/Src/gpio.c \ @@ -153,7 +158,7 @@ C_INCLUDES = \ # compile gcc flags ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -Werror -fdata-sections -ffunction-sections -CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -DNETWORK="\"$(NETWORK)\"" -Wall -Werror -fdata-sections -ffunction-sections +CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -DNETWORK=$(NETWORK) -Wall -Werror -fdata-sections -ffunction-sections ifeq ($(DEBUG), 1) CFLAGS += -g -gdwarf-2 diff --git a/cube/User/Inc/networkAbstractionLayer.h b/cube/User/Inc/networkAbstractionLayer.h new file mode 100644 index 0000000..e11e821 --- /dev/null +++ b/cube/User/Inc/networkAbstractionLayer.h @@ -0,0 +1,9 @@ +#ifndef _NETWORK_ABSTRACTION_LAYER_H_ +#define _NETWORK_ABSTRACTION_LAYER_H_ + + + +void networkInit(); + + +#endif /* _NETWORK_ABSTRACTION_LAYER_H_ */ diff --git a/cube/User/Src/main2.c b/cube/User/Src/main2.c index 080d672..69b5cb2 100644 --- a/cube/User/Src/main2.c +++ b/cube/User/Src/main2.c @@ -13,11 +13,14 @@ #include #include #include -#include +#include #include #include + + + void my_setup_1() { schInit(); logInit(); @@ -37,7 +40,7 @@ void my_setup_2() { configInit(); - wizInit(); + networkInit(); counterInit(); diff --git a/cube/User/Src/networkAbstractionLayer.c b/cube/User/Src/networkAbstractionLayer.c new file mode 100644 index 0000000..42bbe01 --- /dev/null +++ b/cube/User/Src/networkAbstractionLayer.c @@ -0,0 +1,8 @@ +#include + +#include + + +void networkInit() { + wizInit(); +} \ No newline at end of file From dfce60e01f86f9be918a786ca729d5fa1d8fd038 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 11:01:01 +0100 Subject: [PATCH 04/24] network abstraction, time handling --- cube/User/Inc/networkAbstractionLayer.h | 8 +++- cube/User/Inc/wizHelper.h | 8 +--- cube/User/Src/counter.c | 4 +- cube/User/Src/networkAbstractionLayer.c | 50 ++++++++++++++++++- cube/User/Src/wizHelper.c | 64 +++++-------------------- 5 files changed, 73 insertions(+), 61 deletions(-) diff --git a/cube/User/Inc/networkAbstractionLayer.h b/cube/User/Inc/networkAbstractionLayer.h index e11e821..fc5611b 100644 --- a/cube/User/Inc/networkAbstractionLayer.h +++ b/cube/User/Inc/networkAbstractionLayer.h @@ -1,9 +1,15 @@ #ifndef _NETWORK_ABSTRACTION_LAYER_H_ #define _NETWORK_ABSTRACTION_LAYER_H_ +#include +#include +typedef struct { + uint64_t seconds; + bool valid; +} t_seconds; void networkInit(); - +t_seconds* networkGetSeconds(); #endif /* _NETWORK_ABSTRACTION_LAYER_H_ */ diff --git a/cube/User/Inc/wizHelper.h b/cube/User/Inc/wizHelper.h index 5d3f4f6..80abe4f 100644 --- a/cube/User/Inc/wizHelper.h +++ b/cube/User/Inc/wizHelper.h @@ -5,18 +5,12 @@ #include -typedef struct { - uint64_t seconds; - uint32_t missedUpdates; - bool valid; -} t_seconds; int wizInit(); bool isNetworkAvailable(); uint8_t* wizGetIPAddress(); bool wizDnsQuery(char *name, uint8_t *ip); - -t_seconds* wizGetSeconds(); +uint64_t wizSntpQuery(); #endif // _WIZHELPER_H_ diff --git a/cube/User/Src/counter.c b/cube/User/Src/counter.c index c833d40..edcb875 100644 --- a/cube/User/Src/counter.c +++ b/cube/User/Src/counter.c @@ -13,6 +13,8 @@ #include #include #include +#include + const uint32_t COUNTER_FREQUENCY = 1.0e6; @@ -198,7 +200,7 @@ void mainsCntsInputCaptureCallback(TIM_HandleTypeDef *htim) { void counterInit() { deviceStats = getGlobalDeviceStats(); - seconds = wizGetSeconds(); + seconds = networkGetSeconds(); for (uint8_t i = 0; i < NUM_OF_MINUTE_BUFFERS; i++) { minuteBufferReady[i] = false; } diff --git a/cube/User/Src/networkAbstractionLayer.c b/cube/User/Src/networkAbstractionLayer.c index 42bbe01..0be10f4 100644 --- a/cube/User/Src/networkAbstractionLayer.c +++ b/cube/User/Src/networkAbstractionLayer.c @@ -1,8 +1,56 @@ #include +#include +#include +#include #include +static t_seconds seconds = { .seconds = 0, .valid = false }; + + +static void networkSecondsHandler(void *handle) { + static bool tryAgain = false; + + seconds.seconds += 1; + + if (! seconds.valid) { + coloredMsg(LOG_YELLOW, "nsh, initially querying time"); + uint64_t tmpSeconds = wizSntpQuery(); + if (tmpSeconds != 0) { + coloredMsg(LOG_YELLOW, "nsh, success, time is %lu", tmpSeconds); + seconds.seconds = tmpSeconds; + seconds.valid = true; + } else { + coloredMsg(LOG_YELLOW, "nsh, failed"); + } + } else if (tryAgain || ((seconds.seconds % 3600) == 0)) { + coloredMsg(LOG_YELLOW, "nsh, periodically querying time"); + uint64_t tmpSeconds = wizSntpQuery(); + if (tmpSeconds != 0) { + coloredMsg(LOG_YELLOW, "nsh, success, network time is %lu", tmpSeconds); + tryAgain = false; + if (seconds.seconds != tmpSeconds) { + coloredMsg(LOG_YELLOW, "nsh, local time updated"); + seconds.seconds = tmpSeconds; + } else { + coloredMsg(LOG_YELLOW, "nsh, local time still in sync"); + } + } else { + coloredMsg(LOG_YELLOW, "nsh, failed, trying again ..."); + tryAgain = true; + } + } + + HAL_IWDG_Refresh(&hiwdg); +} + +t_seconds* networkGetSeconds() { + return &seconds; +} + + void networkInit() { wizInit(); -} \ No newline at end of file + schAdd(networkSecondsHandler, NULL, 0, 1000); +} diff --git a/cube/User/Src/wizHelper.c b/cube/User/Src/wizHelper.c index a43438d..2face62 100644 --- a/cube/User/Src/wizHelper.c +++ b/cube/User/Src/wizHelper.c @@ -13,7 +13,6 @@ #include #include #include -#include static t_configBlock *config; @@ -28,7 +27,6 @@ static uint8_t dnsBuffer[DNS_BUFFER_SIZE]; static uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE]; -static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false }; const uint64_t UNIX_NTP_EPOCH_DIFF = 2208988800; @@ -135,11 +133,13 @@ bool wizDnsQuery(char *name, uint8_t *ip) { } -static void wizSNTPHandler(void *handle) { +uint64_t wizSntpQuery() { bool success = false; + uint64_t seconds = 0; + static uint32_t missedUpdates = 0; if (networkAvailable) { - coloredMsg(LOG_BLUE, "wizsh, about to call SNTP"); + coloredMsg(LOG_BLUE, "wizsq, about to call SNTP"); uint8_t ntpServer[4]; if (wizDnsQuery(config->ntpServer, ntpServer)) { @@ -148,48 +148,27 @@ static void wizSNTPHandler(void *handle) { for (uint8_t i = 0; i < 16; i++) { datetime curTime; int8_t res = SNTP_run(&curTime); - uint64_t receivedSeconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF; if (res == 1) { - if (seconds.seconds != receivedSeconds) { - coloredMsg(LOG_BLUE, "wizsh, time deviation: %lu %lu", - seconds.seconds, receivedSeconds); - } else { - coloredMsg(LOG_BLUE, "wizsh, time still matching"); - } - - seconds.seconds = receivedSeconds; - seconds.valid = true; - seconds.missedUpdates = 0; + seconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF; + missedUpdates = 0; updated = true; success = true; - coloredMsg(LOG_BLUE, "wizsh, curTime: %lu", seconds.seconds); - + coloredMsg(LOG_BLUE, "wizsq, curTime: %lu", seconds); break; } } if (! updated) { - coloredMsg(LOG_BLUE, "wizsh, time update failed"); - seconds.missedUpdates += 1; + coloredMsg(LOG_BLUE, "wizsq, time update failed"); + missedUpdates += 1; } } else { - seconds.missedUpdates += 1; - coloredMsg(LOG_BLUE, "wizsh, error when querying ntp server name"); + coloredMsg(LOG_BLUE, "wizsq, error when querying ntp server name"); + missedUpdates += 1; } } - - uint16_t tryAgainIn = (success) ? 3600 : 1; - schAdd(wizSNTPHandler, NULL, tryAgainIn * 1000, 0); - coloredMsg(LOG_BLUE, "wizsh, next sntp request in %d seconds", tryAgainIn); -} - -static void wizSecondsHandler(void *handle) { - seconds.seconds += 1; - HAL_IWDG_Refresh(&hiwdg); -} - -t_seconds* wizGetSeconds() { - return &seconds; + + return seconds; } static void wizPhyLinkHandler(void *handle) { @@ -198,7 +177,6 @@ static void wizPhyLinkHandler(void *handle) { static uint8_t lastStablePhyLink = 255; static bool dhcpInitialized = false; - static bool sntpInitialized = false; uint8_t phyLink = 0; int8_t res = ctlwizchip(CW_GET_PHYLINK, (void*) &phyLink); @@ -218,12 +196,6 @@ static void wizPhyLinkHandler(void *handle) { coloredMsg(LOG_BLUE, "wizplh, DHCP handler scheduled"); dhcpInitialized = true; - - - schAdd(wizSNTPHandler, NULL, 15, 0); - coloredMsg(LOG_BLUE, "wizplh, SNTP handler scheduled"); - - sntpInitialized = true; } else { networkAvailable = false; show(LED_GREEN, BLINK); @@ -239,14 +211,6 @@ static void wizPhyLinkHandler(void *handle) { dhcpInitialized = false; } - - if (sntpInitialized) { - schDel(wizSNTPHandler, NULL); - coloredMsg(LOG_BLUE, "wizplh, SNTP handler unscheduled"); - seconds.valid = false; - - sntpInitialized = false; - } } } } @@ -304,7 +268,5 @@ int wizInit() { schAdd(wizPhyLinkHandler, NULL, 0, 1000); coloredMsg(LOG_BLUE, "wizI, PhyLink handler scheduled"); - schAdd(wizSecondsHandler, NULL, 0, 1000); - coloredMsg(LOG_BLUE, "wizI, Seconds handler scheduled"); return 0; } From b3b19084806472a5aea9a26453bf0d8373ecb416 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 11:04:45 +0100 Subject: [PATCH 05/24] missedUpdates --- cube/User/Inc/networkAbstractionLayer.h | 1 + cube/User/Src/networkAbstractionLayer.c | 6 +++++- cube/User/Src/wizHelper.c | 4 ---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cube/User/Inc/networkAbstractionLayer.h b/cube/User/Inc/networkAbstractionLayer.h index fc5611b..762dc2b 100644 --- a/cube/User/Inc/networkAbstractionLayer.h +++ b/cube/User/Inc/networkAbstractionLayer.h @@ -6,6 +6,7 @@ typedef struct { uint64_t seconds; + uint32_t missedUpdates; bool valid; } t_seconds; diff --git a/cube/User/Src/networkAbstractionLayer.c b/cube/User/Src/networkAbstractionLayer.c index 0be10f4..79d3162 100644 --- a/cube/User/Src/networkAbstractionLayer.c +++ b/cube/User/Src/networkAbstractionLayer.c @@ -6,7 +6,7 @@ #include -static t_seconds seconds = { .seconds = 0, .valid = false }; +static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false }; static void networkSecondsHandler(void *handle) { @@ -20,15 +20,18 @@ static void networkSecondsHandler(void *handle) { if (tmpSeconds != 0) { coloredMsg(LOG_YELLOW, "nsh, success, time is %lu", tmpSeconds); seconds.seconds = tmpSeconds; + seconds.missedUpdates = 0; seconds.valid = true; } else { coloredMsg(LOG_YELLOW, "nsh, failed"); + seconds.missedUpdates += 1; } } else if (tryAgain || ((seconds.seconds % 3600) == 0)) { coloredMsg(LOG_YELLOW, "nsh, periodically querying time"); uint64_t tmpSeconds = wizSntpQuery(); if (tmpSeconds != 0) { coloredMsg(LOG_YELLOW, "nsh, success, network time is %lu", tmpSeconds); + seconds.missedUpdates = 0; tryAgain = false; if (seconds.seconds != tmpSeconds) { coloredMsg(LOG_YELLOW, "nsh, local time updated"); @@ -38,6 +41,7 @@ static void networkSecondsHandler(void *handle) { } } else { coloredMsg(LOG_YELLOW, "nsh, failed, trying again ..."); + seconds.missedUpdates += 1; tryAgain = true; } } diff --git a/cube/User/Src/wizHelper.c b/cube/User/Src/wizHelper.c index 2face62..bb2e0ca 100644 --- a/cube/User/Src/wizHelper.c +++ b/cube/User/Src/wizHelper.c @@ -136,7 +136,6 @@ bool wizDnsQuery(char *name, uint8_t *ip) { uint64_t wizSntpQuery() { bool success = false; uint64_t seconds = 0; - static uint32_t missedUpdates = 0; if (networkAvailable) { coloredMsg(LOG_BLUE, "wizsq, about to call SNTP"); @@ -150,7 +149,6 @@ uint64_t wizSntpQuery() { int8_t res = SNTP_run(&curTime); if (res == 1) { seconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF; - missedUpdates = 0; updated = true; success = true; coloredMsg(LOG_BLUE, "wizsq, curTime: %lu", seconds); @@ -160,11 +158,9 @@ uint64_t wizSntpQuery() { if (! updated) { coloredMsg(LOG_BLUE, "wizsq, time update failed"); - missedUpdates += 1; } } else { coloredMsg(LOG_BLUE, "wizsq, error when querying ntp server name"); - missedUpdates += 1; } } From 12626941ce10b645d8118f94f06c39aa672fb17c Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 11:06:45 +0100 Subject: [PATCH 06/24] fix --- cube/User/Src/wizHelper.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/cube/User/Src/wizHelper.c b/cube/User/Src/wizHelper.c index bb2e0ca..d671d91 100644 --- a/cube/User/Src/wizHelper.c +++ b/cube/User/Src/wizHelper.c @@ -134,7 +134,6 @@ bool wizDnsQuery(char *name, uint8_t *ip) { uint64_t wizSntpQuery() { - bool success = false; uint64_t seconds = 0; if (networkAvailable) { @@ -143,20 +142,17 @@ uint64_t wizSntpQuery() { uint8_t ntpServer[4]; if (wizDnsQuery(config->ntpServer, ntpServer)) { SNTP_init(SNTP_SOCK, ntpServer, 0, sntpBuffer); - bool updated = false; for (uint8_t i = 0; i < 16; i++) { datetime curTime; int8_t res = SNTP_run(&curTime); if (res == 1) { seconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF; - updated = true; - success = true; coloredMsg(LOG_BLUE, "wizsq, curTime: %lu", seconds); break; } } - if (! updated) { + if (seconds == 0) { coloredMsg(LOG_BLUE, "wizsq, time update failed"); } } else { From d749d2e7fcab92431ed96da21c178731fee8137c Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 11:18:41 +0100 Subject: [PATCH 07/24] send udp --- cube/User/Inc/networkAbstractionLayer.h | 4 +++ cube/User/Src/counter.c | 30 +------------------ cube/User/Src/networkAbstractionLayer.c | 39 +++++++++++++++++++++++++ cube/User/Src/ports.c | 5 ++-- 4 files changed, 46 insertions(+), 32 deletions(-) diff --git a/cube/User/Inc/networkAbstractionLayer.h b/cube/User/Inc/networkAbstractionLayer.h index 762dc2b..c6600f3 100644 --- a/cube/User/Inc/networkAbstractionLayer.h +++ b/cube/User/Inc/networkAbstractionLayer.h @@ -3,6 +3,8 @@ #include #include +#include + typedef struct { uint64_t seconds; @@ -12,5 +14,7 @@ typedef struct { void networkInit(); t_seconds* networkGetSeconds(); +int8_t networkSendMinuteBuffer(t_minuteBuffer *minuteBuffer); + #endif /* _NETWORK_ABSTRACTION_LAYER_H_ */ diff --git a/cube/User/Src/counter.c b/cube/User/Src/counter.c index edcb875..1805580 100644 --- a/cube/User/Src/counter.c +++ b/cube/User/Src/counter.c @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -38,33 +37,6 @@ uint8_t activeMinuteBuffer; static t_configBlock *config; -extern uint8_t SINK_SOCK; -const uint16_t SINK_PORT = 20169; - -int8_t counterSendMinuteBuffer(t_minuteBuffer *minuteBuffer) { - uint8_t sinkAddr[4]; - if (! wizDnsQuery(config->sinkServer, sinkAddr)) { - coloredMsg(LOG_BLUE, "csmb, failed to resolve sink server name"); - return -1; - } else { - coloredMsg(LOG_BLUE, "csmb, sink server at %d.%d.%d.%d", sinkAddr[0], sinkAddr[1], sinkAddr[2], sinkAddr[3]); - } - - - socket(SINK_SOCK, Sn_MR_UDP, SINK_PORT, 0); - uint8_t sockState = getSn_SR(SINK_SOCK); - if (sockState == SOCK_UDP) { - sendto(SINK_SOCK, minuteBuffer->b, sizeof(minuteBuffer->b), sinkAddr, SINK_PORT); - coloredMsg(LOG_BLUE, "csmb, sent"); - } else { - coloredMsg(LOG_BLUE, "csmb, socket in unexpected state: %d", sockState); - return -2; - } - - close(SINK_SOCK); - - return 1; -} void counterMinuteTick(void *handle) { for (uint8_t minuteBufferIdx = 0; minuteBufferIdx < NUM_OF_MINUTE_BUFFERS; minuteBufferIdx++) { @@ -122,7 +94,7 @@ void counterMinuteTick(void *handle) { minuteBuffer->s.hash[31] ); - int8_t res = counterSendMinuteBuffer(minuteBuffer); + int8_t res = networkSendMinuteBuffer(minuteBuffer); if (res == 1) { coloredMsg(LOG_BLUE, "cmt, successfully sent"); minuteBufferReady[minuteBufferIdx] = false; diff --git a/cube/User/Src/networkAbstractionLayer.c b/cube/User/Src/networkAbstractionLayer.c index 79d3162..9e0c16c 100644 --- a/cube/User/Src/networkAbstractionLayer.c +++ b/cube/User/Src/networkAbstractionLayer.c @@ -2,11 +2,15 @@ #include #include #include +#include +#include #include +#include static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false }; +static t_configBlock *config; static void networkSecondsHandler(void *handle) { @@ -54,7 +58,42 @@ t_seconds* networkGetSeconds() { } +extern uint8_t SINK_SOCK; +const uint16_t SINK_PORT = 20169; + +int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen) { + uint8_t sinkAddr[4]; + if (! wizDnsQuery(hostname, sinkAddr)) { + coloredMsg(LOG_BLUE, "nus, failed to resolve sink server name"); + return -1; + } else { + coloredMsg(LOG_BLUE, "nus, sink server at %d.%d.%d.%d", sinkAddr[0], sinkAddr[1], sinkAddr[2], sinkAddr[3]); + } + + socket(SINK_SOCK, Sn_MR_UDP, SINK_PORT, 0); + uint8_t sockState = getSn_SR(SINK_SOCK); + if (sockState == SOCK_UDP) { + sendto(SINK_SOCK, buf, bufLen, sinkAddr, SINK_PORT); + coloredMsg(LOG_BLUE, "nus, sent"); + } else { + coloredMsg(LOG_BLUE, "nus, socket in unexpected state: %d", sockState); + return -2; + } + + close(SINK_SOCK); + + return 1; +} + + +int8_t networkSendMinuteBuffer(t_minuteBuffer *minuteBuffer) { + return networkUdpSend(config->sinkServer, SINK_PORT, minuteBuffer->b, sizeof(minuteBuffer->b)); +} + void networkInit() { + static t_configBlock *config; + wizInit(); + schAdd(networkSecondsHandler, NULL, 0, 1000); } diff --git a/cube/User/Src/ports.c b/cube/User/Src/ports.c index 67a4972..4fcb7fc 100644 --- a/cube/User/Src/ports.c +++ b/cube/User/Src/ports.c @@ -6,6 +6,5 @@ const uint8_t DHCP_SOCK = 0; const uint8_t SNTP_SOCK = 1; -const uint8_t CMD_SOCK = 2; -const uint8_t DNS_SOCK = 3; -const uint8_t SINK_SOCK = 4; +const uint8_t DNS_SOCK = 2; +const uint8_t SINK_SOCK = 3; From 5eaa9aceed8ce0775c0b66c238249d6a4f81578a Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 11:19:29 +0100 Subject: [PATCH 08/24] send udp --- cube/User/Src/networkAbstractionLayer.c | 1 + 1 file changed, 1 insertion(+) diff --git a/cube/User/Src/networkAbstractionLayer.c b/cube/User/Src/networkAbstractionLayer.c index 9e0c16c..5bbe518 100644 --- a/cube/User/Src/networkAbstractionLayer.c +++ b/cube/User/Src/networkAbstractionLayer.c @@ -7,6 +7,7 @@ #include #include +#include static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false }; From af21143561e7ba00f5ec43b0af37f0ac26390550 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 11:20:37 +0100 Subject: [PATCH 09/24] send udp --- cube/User/Src/networkAbstractionLayer.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cube/User/Src/networkAbstractionLayer.c b/cube/User/Src/networkAbstractionLayer.c index 5bbe518..7eb350c 100644 --- a/cube/User/Src/networkAbstractionLayer.c +++ b/cube/User/Src/networkAbstractionLayer.c @@ -92,9 +92,7 @@ int8_t networkSendMinuteBuffer(t_minuteBuffer *minuteBuffer) { } void networkInit() { - static t_configBlock *config; - wizInit(); - + config = getConfig(); schAdd(networkSecondsHandler, NULL, 0, 1000); } From 37767dc81d95741a2b8ff507d00d0bcbe6a49335 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 11:26:49 +0100 Subject: [PATCH 10/24] network abstraction --- cube/User/Src/networkAbstractionLayer.c | 29 ++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/cube/User/Src/networkAbstractionLayer.c b/cube/User/Src/networkAbstractionLayer.c index 7eb350c..94289ab 100644 --- a/cube/User/Src/networkAbstractionLayer.c +++ b/cube/User/Src/networkAbstractionLayer.c @@ -5,15 +5,32 @@ #include #include + + +#define NETWORK_LAN 1 +#define NETWORK_WiFi 2 +#define NETWORK_GSM 3 + +#if NETWORK == NETWORK_LAN #include #include #include +#endif + +const uint16_t SINK_PORT = 20169; + static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false }; static t_configBlock *config; +#if NETWORK == NETWORK_LAN +static uint64_t networkSntpQuery() { + return wizSntpQuery(); +} +#endif + static void networkSecondsHandler(void *handle) { static bool tryAgain = false; @@ -21,7 +38,7 @@ static void networkSecondsHandler(void *handle) { if (! seconds.valid) { coloredMsg(LOG_YELLOW, "nsh, initially querying time"); - uint64_t tmpSeconds = wizSntpQuery(); + uint64_t tmpSeconds = networkSntpQuery(); if (tmpSeconds != 0) { coloredMsg(LOG_YELLOW, "nsh, success, time is %lu", tmpSeconds); seconds.seconds = tmpSeconds; @@ -33,7 +50,7 @@ static void networkSecondsHandler(void *handle) { } } else if (tryAgain || ((seconds.seconds % 3600) == 0)) { coloredMsg(LOG_YELLOW, "nsh, periodically querying time"); - uint64_t tmpSeconds = wizSntpQuery(); + uint64_t tmpSeconds = networkSntpQuery(); if (tmpSeconds != 0) { coloredMsg(LOG_YELLOW, "nsh, success, network time is %lu", tmpSeconds); seconds.missedUpdates = 0; @@ -59,10 +76,11 @@ t_seconds* networkGetSeconds() { } -extern uint8_t SINK_SOCK; -const uint16_t SINK_PORT = 20169; -int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen) { +#if NETWORK == NETWORK_LAN +extern uint8_t SINK_SOCK; + +static int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen) { uint8_t sinkAddr[4]; if (! wizDnsQuery(hostname, sinkAddr)) { coloredMsg(LOG_BLUE, "nus, failed to resolve sink server name"); @@ -85,6 +103,7 @@ int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufL return 1; } +#endif int8_t networkSendMinuteBuffer(t_minuteBuffer *minuteBuffer) { From fd914a94eab1801ce62f03a8f488380cb2c16453 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 11:31:19 +0100 Subject: [PATCH 11/24] tune makefile --- cube/Makefile | 9 +++++++-- cube/User/Src/networkAbstractionLayer.c | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cube/Makefile b/cube/Makefile index be3632e..d397de3 100644 --- a/cube/Makefile +++ b/cube/Makefile @@ -52,12 +52,10 @@ User/Src/counter.c \ User/Src/eeprom.c \ User/Src/logger.c \ User/Src/main2.c \ -User/Src/ports.c \ User/Src/ringbuffer.c \ User/Src/sha256.c \ User/Src/show.c \ User/Src/utils.c \ -User/Src/wizHelper.c \ User/Src/networkAbstractionLayer.c \ hottislib/PontCoopScheduler.c \ Core/Src/main.c \ @@ -86,6 +84,13 @@ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c \ Core/Src/system_stm32f1xx.c +ifeq ($(NETWORK), 1) +C_SOURCES += \ +User/Src/ports.c \ +User/Src/wizHelper.c +endif + + # ASM sources ASM_SOURCES = \ startup_stm32f103xb.s diff --git a/cube/User/Src/networkAbstractionLayer.c b/cube/User/Src/networkAbstractionLayer.c index 94289ab..c25b4aa 100644 --- a/cube/User/Src/networkAbstractionLayer.c +++ b/cube/User/Src/networkAbstractionLayer.c @@ -111,7 +111,10 @@ int8_t networkSendMinuteBuffer(t_minuteBuffer *minuteBuffer) { } void networkInit() { +#if NETWORK == NETWORK_LAN wizInit(); +#endif + config = getConfig(); schAdd(networkSecondsHandler, NULL, 0, 1000); } From 5677f3526000f0c55193a7a5ade96e5ace96ca77 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 11:32:37 +0100 Subject: [PATCH 12/24] tune makefile --- cube/Makefile | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cube/Makefile b/cube/Makefile index d397de3..837978f 100644 --- a/cube/Makefile +++ b/cube/Makefile @@ -148,10 +148,6 @@ AS_INCLUDES = C_INCLUDES = \ -Ihottislib \ -IUser/Inc \ --IioLibrary_Driver/Internet/SNTP \ --IioLibrary_Driver/Internet/DNS \ --IioLibrary_Driver/Internet/DHCP \ --IioLibrary_Driver/Ethernet \ -ICore/Inc \ -IDrivers/STM32F1xx_HAL_Driver/Inc \ -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \ @@ -160,6 +156,15 @@ C_INCLUDES = \ -IDrivers/CMSIS/Include +ifeq ($(NETWORK), 1) +C_INCLUDES += \ +-IioLibrary_Driver/Internet/SNTP \ +-IioLibrary_Driver/Internet/DNS \ +-IioLibrary_Driver/Internet/DHCP \ +-IioLibrary_Driver/Ethernet +endif + + # compile gcc flags ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -Werror -fdata-sections -ffunction-sections From 5b7248a8a50bce26a3b2bc5942d50cb755d752e4 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 11:34:26 +0100 Subject: [PATCH 13/24] tune makefile --- cube/Makefile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cube/Makefile b/cube/Makefile index 837978f..184c212 100644 --- a/cube/Makefile +++ b/cube/Makefile @@ -18,7 +18,7 @@ # LAN = 1 # WiFi = 2 # GSM = 3 -NETWORK = 1 +# NETWORK = 1 ###################################### @@ -200,13 +200,19 @@ all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET # list of objects OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o))) vpath %.c $(sort $(dir $(C_SOURCES))) + +ifeq ($(NETWORK), 1) OBJECTS += $(addprefix $(BUILD_DIR)/,w5500.a) +endif + # list of ASM program objects OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o))) vpath %.s $(sort $(dir $(ASM_SOURCES))) +ifeq ($(NETWORK), 1) $(BUILD_DIR)/w5500.a: (cd ioLibrary_Driver && $(MAKE) && cp w5500.a ../$(BUILD_DIR) && cd ..) +endif $(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@ From d29529bf8a2791aee17886db6cbd6b15aa62e2f1 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 11:44:51 +0100 Subject: [PATCH 14/24] nal --- cube/Makefile | 3 +- cube/User/Inc/networkAbstractionLayer_impl.h | 10 ++++ cube/User/Src/networkAbstractionLayer.c | 56 ++------------------ cube/User/Src/networkAbstractionLayer_lan.c | 43 +++++++++++++++ 4 files changed, 58 insertions(+), 54 deletions(-) create mode 100644 cube/User/Inc/networkAbstractionLayer_impl.h create mode 100644 cube/User/Src/networkAbstractionLayer_lan.c diff --git a/cube/Makefile b/cube/Makefile index 184c212..5b0fc21 100644 --- a/cube/Makefile +++ b/cube/Makefile @@ -87,7 +87,8 @@ Core/Src/system_stm32f1xx.c ifeq ($(NETWORK), 1) C_SOURCES += \ User/Src/ports.c \ -User/Src/wizHelper.c +User/Src/wizHelper.c \ +User/Src/networkAbstractionLayer_lan.c endif diff --git a/cube/User/Inc/networkAbstractionLayer_impl.h b/cube/User/Inc/networkAbstractionLayer_impl.h new file mode 100644 index 0000000..fed8522 --- /dev/null +++ b/cube/User/Inc/networkAbstractionLayer_impl.h @@ -0,0 +1,10 @@ +#ifndef _NETWORK_ABSTRACTION_LAYER_IMPL_H_ +#define _NETWORK_ABSTRACTION_LAYER_IMPL_H_ + +#include + +uint64_t networkSntpQuery(); +int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen); +void networkImplInit(); + +#endif /* _NETWORK_ABSTRACTION_LAYER_IMPL_H_ */ diff --git a/cube/User/Src/networkAbstractionLayer.c b/cube/User/Src/networkAbstractionLayer.c index c25b4aa..575967c 100644 --- a/cube/User/Src/networkAbstractionLayer.c +++ b/cube/User/Src/networkAbstractionLayer.c @@ -5,18 +5,7 @@ #include #include - - -#define NETWORK_LAN 1 -#define NETWORK_WiFi 2 -#define NETWORK_GSM 3 - -#if NETWORK == NETWORK_LAN -#include -#include -#include -#endif - +#include const uint16_t SINK_PORT = 20169; @@ -25,12 +14,6 @@ static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false }; static t_configBlock *config; -#if NETWORK == NETWORK_LAN -static uint64_t networkSntpQuery() { - return wizSntpQuery(); -} -#endif - static void networkSecondsHandler(void *handle) { static bool tryAgain = false; @@ -75,46 +58,13 @@ t_seconds* networkGetSeconds() { return &seconds; } - - -#if NETWORK == NETWORK_LAN -extern uint8_t SINK_SOCK; - -static int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen) { - uint8_t sinkAddr[4]; - if (! wizDnsQuery(hostname, sinkAddr)) { - coloredMsg(LOG_BLUE, "nus, failed to resolve sink server name"); - return -1; - } else { - coloredMsg(LOG_BLUE, "nus, sink server at %d.%d.%d.%d", sinkAddr[0], sinkAddr[1], sinkAddr[2], sinkAddr[3]); - } - - socket(SINK_SOCK, Sn_MR_UDP, SINK_PORT, 0); - uint8_t sockState = getSn_SR(SINK_SOCK); - if (sockState == SOCK_UDP) { - sendto(SINK_SOCK, buf, bufLen, sinkAddr, SINK_PORT); - coloredMsg(LOG_BLUE, "nus, sent"); - } else { - coloredMsg(LOG_BLUE, "nus, socket in unexpected state: %d", sockState); - return -2; - } - - close(SINK_SOCK); - - return 1; -} -#endif - - int8_t networkSendMinuteBuffer(t_minuteBuffer *minuteBuffer) { return networkUdpSend(config->sinkServer, SINK_PORT, minuteBuffer->b, sizeof(minuteBuffer->b)); } void networkInit() { -#if NETWORK == NETWORK_LAN - wizInit(); -#endif - + networkImplInit(); + config = getConfig(); schAdd(networkSecondsHandler, NULL, 0, 1000); } diff --git a/cube/User/Src/networkAbstractionLayer_lan.c b/cube/User/Src/networkAbstractionLayer_lan.c new file mode 100644 index 0000000..c1908b4 --- /dev/null +++ b/cube/User/Src/networkAbstractionLayer_lan.c @@ -0,0 +1,43 @@ +#include +#include + +#include +#include +#include + + + +uint64_t networkSntpQuery() { + return wizSntpQuery(); +} + + +extern uint8_t SINK_SOCK; + +int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen) { + uint8_t sinkAddr[4]; + if (! wizDnsQuery(hostname, sinkAddr)) { + coloredMsg(LOG_BLUE, "nus, failed to resolve sink server name"); + return -1; + } else { + coloredMsg(LOG_BLUE, "nus, sink server at %d.%d.%d.%d", sinkAddr[0], sinkAddr[1], sinkAddr[2], sinkAddr[3]); + } + + socket(SINK_SOCK, Sn_MR_UDP, port, 0); + uint8_t sockState = getSn_SR(SINK_SOCK); + if (sockState == SOCK_UDP) { + sendto(SINK_SOCK, buf, bufLen, sinkAddr, port); + coloredMsg(LOG_BLUE, "nus, sent"); + } else { + coloredMsg(LOG_BLUE, "nus, socket in unexpected state: %d", sockState); + return -2; + } + + close(SINK_SOCK); + + return 1; +} + +void networkImplInit() { + wizInit(); +} From 3334b5d9dac75b3a64a29b3a235217edf558e778 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 11:47:03 +0100 Subject: [PATCH 15/24] nal --- cube/Makefile | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/cube/Makefile b/cube/Makefile index 5b0fc21..64a0839 100644 --- a/cube/Makefile +++ b/cube/Makefile @@ -15,10 +15,12 @@ # ------------------------------------------------ -# LAN = 1 -# WiFi = 2 -# GSM = 3 -# NETWORK = 1 +# Network implementations, to be set on commandline +# LAN +# WiFi +# GSM + +# NETWORK = LAN ###################################### @@ -84,7 +86,7 @@ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c \ Core/Src/system_stm32f1xx.c -ifeq ($(NETWORK), 1) +ifeq ($(NETWORK), "LAN") C_SOURCES += \ User/Src/ports.c \ User/Src/wizHelper.c \ @@ -157,7 +159,7 @@ C_INCLUDES = \ -IDrivers/CMSIS/Include -ifeq ($(NETWORK), 1) +ifeq ($(NETWORK), "LAN") C_INCLUDES += \ -IioLibrary_Driver/Internet/SNTP \ -IioLibrary_Driver/Internet/DNS \ @@ -202,7 +204,7 @@ all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o))) vpath %.c $(sort $(dir $(C_SOURCES))) -ifeq ($(NETWORK), 1) +ifeq ($(NETWORK), "LAN") OBJECTS += $(addprefix $(BUILD_DIR)/,w5500.a) endif @@ -210,7 +212,7 @@ endif OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o))) vpath %.s $(sort $(dir $(ASM_SOURCES))) -ifeq ($(NETWORK), 1) +ifeq ($(NETWORK), "LAN") $(BUILD_DIR)/w5500.a: (cd ioLibrary_Driver && $(MAKE) && cp w5500.a ../$(BUILD_DIR) && cd ..) endif From 3e2da4e3853c443b3aa5644dbc3a33a390ac272a Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 11:51:43 +0100 Subject: [PATCH 16/24] nal --- cube/Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cube/Makefile b/cube/Makefile index 64a0839..86d0882 100644 --- a/cube/Makefile +++ b/cube/Makefile @@ -22,6 +22,9 @@ # NETWORK = LAN +ifndef NETWORK +$(error NETWORK is not set) +endif ###################################### # target From 7dd27d374ff8b1393b4ff1cc7a877234aae7b419 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 11:52:20 +0100 Subject: [PATCH 17/24] changes, fixes --- cube/Makefile | 10 +++++----- cube/User/Src/counter.c | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/cube/Makefile b/cube/Makefile index 64a0839..7b2041f 100644 --- a/cube/Makefile +++ b/cube/Makefile @@ -86,7 +86,7 @@ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c \ Core/Src/system_stm32f1xx.c -ifeq ($(NETWORK), "LAN") +ifeq ($(NETWORK), LAN) C_SOURCES += \ User/Src/ports.c \ User/Src/wizHelper.c \ @@ -159,7 +159,7 @@ C_INCLUDES = \ -IDrivers/CMSIS/Include -ifeq ($(NETWORK), "LAN") +ifeq ($(NETWORK), LAN) C_INCLUDES += \ -IioLibrary_Driver/Internet/SNTP \ -IioLibrary_Driver/Internet/DNS \ @@ -204,7 +204,7 @@ all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o))) vpath %.c $(sort $(dir $(C_SOURCES))) -ifeq ($(NETWORK), "LAN") +ifeq ($(NETWORK), LAN) OBJECTS += $(addprefix $(BUILD_DIR)/,w5500.a) endif @@ -212,7 +212,7 @@ endif OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o))) vpath %.s $(sort $(dir $(ASM_SOURCES))) -ifeq ($(NETWORK), "LAN") +ifeq ($(NETWORK), LAN) $(BUILD_DIR)/w5500.a: (cd ioLibrary_Driver && $(MAKE) && cp w5500.a ../$(BUILD_DIR) && cd ..) endif @@ -247,4 +247,4 @@ clean: ####################################### -include $(wildcard $(BUILD_DIR)/*.d) -# *** EOF *** \ No newline at end of file +# *** EOF *** diff --git a/cube/User/Src/counter.c b/cube/User/Src/counter.c index 1805580..5c90b76 100644 --- a/cube/User/Src/counter.c +++ b/cube/User/Src/counter.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include From 734343c4778c4fad32ed18e539178daf604002ca Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 12:19:50 +0100 Subject: [PATCH 18/24] config rearranged --- cube/User/Inc/config.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cube/User/Inc/config.h b/cube/User/Inc/config.h index c823f27..33a9a83 100644 --- a/cube/User/Inc/config.h +++ b/cube/User/Inc/config.h @@ -4,16 +4,16 @@ #include #include -#define CONFIG_MAGIC 0xdead0007 +#define CONFIG_MAGIC 0xdead0008 typedef struct __attribute__((__packed__)) s_configBlock { uint32_t configMagic; char deviceName[16]; - uint8_t macAddress[6]; char ntpServer[48]; char deviceId[16]; char sharedSecret[SHA256_BLOCK_SIZE]; char sinkServer[48]; + uint8_t macAddress[6]; uint8_t filler[22]; } t_configBlock; From a2a484fcc5fc57672dcf80a774f5040ca044f79b Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 12:27:12 +0100 Subject: [PATCH 19/24] network specific configuration --- cube/User/Inc/config.h | 4 +++- cube/User/Src/config.c | 12 ++++++------ cube/User/Src/wizHelper.c | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/cube/User/Inc/config.h b/cube/User/Inc/config.h index 33a9a83..51f1682 100644 --- a/cube/User/Inc/config.h +++ b/cube/User/Inc/config.h @@ -13,7 +13,9 @@ typedef struct __attribute__((__packed__)) s_configBlock { char deviceId[16]; char sharedSecret[SHA256_BLOCK_SIZE]; char sinkServer[48]; - uint8_t macAddress[6]; + union networkspecific { + uint8_t macAddress[6]; + }; uint8_t filler[22]; } t_configBlock; diff --git a/cube/User/Src/config.c b/cube/User/Src/config.c index 4395dff..e3fa426 100644 --- a/cube/User/Src/config.c +++ b/cube/User/Src/config.c @@ -44,12 +44,12 @@ void configInit() { } coloredMsg(LOG_BLUE, "cfg ci configMagic: %lx", mainConfigBlock.configMagic); coloredMsg(LOG_BLUE, "cfg ci deviceName: %s", mainConfigBlock.deviceName); - coloredMsg(LOG_BLUE, "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, "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, "cfg ci ntp server: %s", mainConfigBlock.ntpServer); coloredMsg(LOG_BLUE, "cfg ci deviceId: %s", mainConfigBlock.deviceId); coloredMsg(LOG_BLUE, "cfg ci sharedSecret: %s", mainConfigBlock.sharedSecret); diff --git a/cube/User/Src/wizHelper.c b/cube/User/Src/wizHelper.c index d671d91..2707344 100644 --- a/cube/User/Src/wizHelper.c +++ b/cube/User/Src/wizHelper.c @@ -211,7 +211,7 @@ static void wizPhyLinkHandler(void *handle) { int wizInit() { config = getConfig(); netInfo.dhcp = NETINFO_DHCP; - memcpy(netInfo.mac, config->macAddress, 6); + memcpy(netInfo.mac, config->networkspecific.macAddress, 6); coloredMsg(LOG_BLUE, "wizI, resetting Ethernet module"); wizReset(true); From 4ca37156f3e5cf44db8859858342309171cf3f37 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 12:28:31 +0100 Subject: [PATCH 20/24] network specific configuration --- cube/User/Inc/config.h | 4 ++-- cube/User/Src/config.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cube/User/Inc/config.h b/cube/User/Inc/config.h index 51f1682..08fdf9b 100644 --- a/cube/User/Inc/config.h +++ b/cube/User/Inc/config.h @@ -13,9 +13,9 @@ typedef struct __attribute__((__packed__)) s_configBlock { char deviceId[16]; char sharedSecret[SHA256_BLOCK_SIZE]; char sinkServer[48]; - union networkspecific { + union { uint8_t macAddress[6]; - }; + } networkspecific; uint8_t filler[22]; } t_configBlock; diff --git a/cube/User/Src/config.c b/cube/User/Src/config.c index e3fa426..e609698 100644 --- a/cube/User/Src/config.c +++ b/cube/User/Src/config.c @@ -10,7 +10,7 @@ t_configBlock defaultConfigBlock = { .configMagic = CONFIG_MAGIC, .deviceName = "MainsCnt", - .macAddress = { 0x00, 0xA0, 0x57, 0x05, 0x3E, 0x0F }, + .networkspecific.macAddress = { 0x00, 0xA0, 0x57, 0x05, 0x3E, 0x0F }, .ntpServer = "0.de.pool.ntp.org", .deviceId = "MainsCnt01", .sharedSecret = "sharedSecretGanzGeheim", From b3832a3d1b31da3a1b4e8ab5d95508f7d95d970f Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 12:31:45 +0100 Subject: [PATCH 21/24] network specific configuration --- cube/User/Inc/config.h | 4 +++- cube/User/Src/config.c | 2 +- cube/User/Src/wizHelper.c | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cube/User/Inc/config.h b/cube/User/Inc/config.h index 08fdf9b..85163a6 100644 --- a/cube/User/Inc/config.h +++ b/cube/User/Inc/config.h @@ -14,7 +14,9 @@ typedef struct __attribute__((__packed__)) s_configBlock { char sharedSecret[SHA256_BLOCK_SIZE]; char sinkServer[48]; union { - uint8_t macAddress[6]; + struct { + uint8_t macAddress[6]; + } lan; } networkspecific; uint8_t filler[22]; } t_configBlock; diff --git a/cube/User/Src/config.c b/cube/User/Src/config.c index e609698..c134cb8 100644 --- a/cube/User/Src/config.c +++ b/cube/User/Src/config.c @@ -10,7 +10,7 @@ t_configBlock defaultConfigBlock = { .configMagic = CONFIG_MAGIC, .deviceName = "MainsCnt", - .networkspecific.macAddress = { 0x00, 0xA0, 0x57, 0x05, 0x3E, 0x0F }, + .networkspecific.lan.macAddress = { 0x00, 0xA0, 0x57, 0x05, 0x3E, 0x0F }, .ntpServer = "0.de.pool.ntp.org", .deviceId = "MainsCnt01", .sharedSecret = "sharedSecretGanzGeheim", diff --git a/cube/User/Src/wizHelper.c b/cube/User/Src/wizHelper.c index 2707344..2e9cd52 100644 --- a/cube/User/Src/wizHelper.c +++ b/cube/User/Src/wizHelper.c @@ -211,7 +211,7 @@ static void wizPhyLinkHandler(void *handle) { int wizInit() { config = getConfig(); netInfo.dhcp = NETINFO_DHCP; - memcpy(netInfo.mac, config->networkspecific.macAddress, 6); + memcpy(netInfo.mac, config->networkspecific.lan.macAddress, 6); coloredMsg(LOG_BLUE, "wizI, resetting Ethernet module"); wizReset(true); From 9db73606236ef09c490b37e6a743cfc227e22dd4 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 13:00:10 +0100 Subject: [PATCH 22/24] prepare wifi --- cube/Makefile | 6 +++++- cube/User/Inc/config.h | 4 ++++ cube/User/Src/networkAbstractionLayer_wifi.c | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 cube/User/Src/networkAbstractionLayer_wifi.c diff --git a/cube/Makefile b/cube/Makefile index 9702f96..365e0ae 100644 --- a/cube/Makefile +++ b/cube/Makefile @@ -96,6 +96,10 @@ User/Src/wizHelper.c \ User/Src/networkAbstractionLayer_lan.c endif +ifeq ($(NETWORK), WiFi) +C_SOURCES += \ +User/Src/networkAbstractionLayer_wifi.c +endif # ASM sources ASM_SOURCES = \ @@ -250,4 +254,4 @@ clean: ####################################### -include $(wildcard $(BUILD_DIR)/*.d) -# *** EOF *** +# *** EOF *** diff --git a/cube/User/Inc/config.h b/cube/User/Inc/config.h index 85163a6..28ff8f7 100644 --- a/cube/User/Inc/config.h +++ b/cube/User/Inc/config.h @@ -17,6 +17,10 @@ typedef struct __attribute__((__packed__)) s_configBlock { struct { uint8_t macAddress[6]; } lan; + struct { + char ssid[48]; + char key[65]; // the actual key may have up to 64 chars and has to end with a \0 + } wifi; } networkspecific; uint8_t filler[22]; } t_configBlock; diff --git a/cube/User/Src/networkAbstractionLayer_wifi.c b/cube/User/Src/networkAbstractionLayer_wifi.c new file mode 100644 index 0000000..6f35a37 --- /dev/null +++ b/cube/User/Src/networkAbstractionLayer_wifi.c @@ -0,0 +1,19 @@ +#include +#include + +#include + + + +uint64_t networkSntpQuery() { + return 0; +} + + +int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen) { + return 0; +} + +void networkImplInit() { + +} From 482d60e903a07ae7b8ca39593e5c3db90d1a447e Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 13:02:13 +0100 Subject: [PATCH 23/24] prepare wifi --- cube/User/Inc/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cube/User/Inc/config.h b/cube/User/Inc/config.h index 28ff8f7..05a30cd 100644 --- a/cube/User/Inc/config.h +++ b/cube/User/Inc/config.h @@ -22,7 +22,7 @@ typedef struct __attribute__((__packed__)) s_configBlock { char key[65]; // the actual key may have up to 64 chars and has to end with a \0 } wifi; } networkspecific; - uint8_t filler[22]; + uint8_t filler[15]; } t_configBlock; From a10b9c1dfdae85a608848bf843a0d8a179ee23a5 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 13:05:44 +0100 Subject: [PATCH 24/24] fix config padding --- cube/User/Inc/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cube/User/Inc/config.h b/cube/User/Inc/config.h index 05a30cd..586b9b5 100644 --- a/cube/User/Inc/config.h +++ b/cube/User/Inc/config.h @@ -22,7 +22,7 @@ typedef struct __attribute__((__packed__)) s_configBlock { char key[65]; // the actual key may have up to 64 chars and has to end with a \0 } wifi; } networkspecific; - uint8_t filler[15]; + uint8_t filler[11]; } t_configBlock;