diff --git a/Makefile b/Makefile index ce506a7..42e18f4 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ endif # Linker extra options here. ifeq ($(USE_LDOPT),) - USE_LDOPT = + USE_LDOPT = --print-memory-usage endif # Enable this if you want link time optimizations (LTO). @@ -121,7 +121,7 @@ include $(CHIBIOS)/os/various/lwip_bindings/lwip.mk #include $(CHIBIOS)/ext/STM32F4xx_StdPeriph_Driver/stcrypt.mk #include $(CHIBIOS)/ext/STM32_Cryptographic_Library/stcryptolib.mk #include $(CHIBIOS)/os/various/wolfssl_bindings/wolfssl.mk -#include $(CHIBIOS)/ext/mbedtls/mbedtls.mk +include $(CHIBIOS)/ext/mbedtls/mbedtls.mk # Define linker script file here LDSCRIPT= $(CONFDIR)/STM32F437xG.ld diff --git a/lwipopts.h b/lwipopts.h index 15bc2c0..728e82d 100644 --- a/lwipopts.h +++ b/lwipopts.h @@ -190,6 +190,8 @@ //#define MDNS_DEBUG LWIP_DBG_ON // MQTT //#define MQTT_DEBUG LWIP_DBG_ON +// HTTP Client +//#define HTTPC_DEBUG LWIP_DBG_ON // SNTP #define SNTP_SERVER_DNS 1 @@ -212,6 +214,16 @@ #define LWIP_NETIF_HOSTNAME 1 +// MBED TLS +/* +#define LWIP_ALTCP 1 +#define LWIP_ALTCP_TLS 1 +#define LWIP_ALTCP_TLS_MBEDTLS 1 +#define MBEDTLS_PLATFORM_MEMORY 1 +#define ALTCP_MBEDTLS_RNG_FN mbedtls_entropy_func +//#define MEMP_MEM_MALLOC 1 +*/ + /* * new SNTP_SET_SYSTEM_TIME function * diff --git a/main.c b/main.c index 53713be..c7444b4 100644 --- a/main.c +++ b/main.c @@ -100,15 +100,31 @@ char gprsSmsText[128] __attribute__((section(".ram4"))); #include "lwip/apps/sntp.h" #include "lwip/apps/smtp.h" #include "lwip/apps/mdns.h" + +#include "lwip/inet.h" +#include "lwip/apps/http_client.h" + #include "ohs_httpdhandler.h" // MQTT -#include "lwip/apps/mqtt_priv.h" // Needed to conf.mqtt +#include "lwip/apps/mqtt_priv.h" // Needed for conf.mqtt #include "lwip/apps/mqtt.h" #include "ohs_mqtt_functions.h" // Shell functions #include "ohs_shell.h" +// MBEDTLS +/* +#include "config.h" +#include "mbedtls/platform.h" +#include "mbedtls/net_sockets.h" +#include "mbedtls/ssl.h" +#include "mbedtls/entropy.h" +#include "mbedtls/ctr_drbg.h" +#include "mbedtls/error.h" +#include "mbedtls/certs.h" +*/ + // #ifdef OHS_WOLFSSL #include "crypto.h" @@ -164,6 +180,50 @@ static void mdns_example_report(struct netif* netif, u8_t result, s8_t service){ } #endif +/* +uint8_t httpcGo = 1; +err_t httpcHeaderDone (httpc_state_t *connection, void *arg, struct pbuf *hdr, u16_t hdr_len, u32_t content_len){ + LWIP_UNUSED_ARG(connection); + LWIP_UNUSED_ARG(arg); + + chprintf(console, ">httpcHeaderDone: %d;%d;%s\r\n", hdr_len, content_len, hdr->payload); + return ERR_OK; +} + +void httpcFinished (void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res, err_t err){ + LWIP_UNUSED_ARG(arg); + + chprintf(console, ">httpcFinished err: %d; httpc_result: %d, rx_content_len: %d, srv_res: %d\r\n", err, httpc_result,rx_content_len,srv_res); + httpcGo = 1; +} + +err_t httpcGetResult (void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err){ + LWIP_UNUSED_ARG(arg); + LWIP_UNUSED_ARG(conn); + + chprintf(console, ">httpcGetResult: %d;%s\r\n", err, p->payload); + + // Free pbuf + if (p != NULL) pbuf_free(p); + return ERR_OK; +} + +static void mydebug(void *ctx, int level, const char *file, int line, + const char *str) { + const char *p, *basename; + (void) ctx; + + // Extract basename from file + for(p = basename = file; *p != '\0'; p++) { + if(*p == '/' || *p == '\\') { + basename = p + 1; + } + } + + chprintf(console, "%s:%04d: |%d| %s", basename, line, level, str); +} +*/ + /* * Application entry point. */ @@ -203,7 +263,7 @@ int main(void) { struct lwipthread_opts lwip_opts = { &macAddr[0], 0, 0, 0, NET_ADDRESS_DHCP #if LWIP_NETIF_HOSTNAME - , OHS_NAME + , OHS_NAME "2" #endif ,NULL, NULL }; @@ -230,7 +290,7 @@ int main(void) { memset(&gprsSmsText[0], 0, sizeof(gprsSmsText)); memset(&gprsSystemInfo[0], 0, sizeof(gprsSystemInfo)); memset(&logText[0], 0, LOG_TEXT_LENGTH); - memset(&alertMsg[0], 0 , HTTP_ALERT_MSG_SIZE); // Empty alert message + memset(&httpAlertMsg[0], 0 , HTTP_ALERT_MSG_SIZE); // Empty alert message shellInit(); @@ -319,17 +379,19 @@ int main(void) { chprintf(console, "Size of conf: %u, group: %u\r\n", sizeof(conf), sizeof(group)); // Check if we have 1.3 -> 1.4 version update - if ((conf.versionMajor == 1) && (conf.versionMinor == 3) && (OHS_MINOR == 4)) { + if ((conf.versionMajor == 1) && (conf.versionMinor == 3) && (OHS_MINOR == 4)) { // Set new version conf struct changes // Save the changes conf.versionMajor = OHS_MAJOR; conf.versionMinor = OHS_MINOR; writeToBkpSRAM((uint8_t*)&conf, sizeof(config_t), 0); - } else if (OHS_MINOR != conf.versionMinor) { + } else if (OHS_MINOR != conf.versionMinor) { // Unknown version change, clear all setConfDefault(); // Save the changes + conf.versionMajor = OHS_MAJOR; + conf.versionMinor = OHS_MINOR; writeToBkpSRAM((uint8_t*)&conf, sizeof(config_t), 0); // Init and save runtime variables initRuntimeGroups(); // Initialize runtime variables @@ -351,6 +413,8 @@ int main(void) { UNLOCK_TCPIP_CORE(); // MQTT CLEAR_CONF_MQTT_ADDRESS_ERROR(conf.mqtt.setting); // Force resolve address on start + // Set HTTPD connection ID to be "unique", to disallow Id=NULL as vaild + authorizedConn.id = STM32_UUID[0] + rand(); // Start startTime = getTimeUnixSec(); @@ -366,6 +430,57 @@ int main(void) { wolfSSL_Init(); #endif + /* + mbedtls_entropy_context entropy; + mbedtls_ctr_drbg_context ctr_drbg; + mbedtls_ssl_context ssl; + mbedtls_x509_crt cacert; + mbedtls_ssl_config conf; + mbedtls_net_context server_fd; + + char buf[512]; + int ret, flags, len; + mbedtls_ssl_init(&ssl); + mbedtls_x509_crt_init(&cacert); + mbedtls_ctr_drbg_init(&ctr_drbg); + + mbedtls_ssl_config_init(&conf); + + mbedtls_entropy_init(&entropy); + if((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, + NULL, 0)) != 0) { + chprintf(console, "mbedtls_ctr_drbg_seed returned %d", ret); + } + */ + + // MBEDTLS client + /* + mbedtls_entropy_context entropy; + mbedtls_ctr_drbg_context ctr_drbg; + mbedtls_ssl_context ssl; + mbedtls_x509_crt cacert; + mbedtls_ssl_config conf; + int ret; + + mbedtls_ssl_init(&ssl); + mbedtls_ssl_config_init(&conf); + mbedtls_x509_crt_init(&cacert); + mbedtls_ctr_drbg_init(&ctr_drbg); + mbedtls_entropy_init(&entropy); + if((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, + NULL, 0)) != 0) { + chprintf(console, ">mbedtls_ctr_drbg_seed: %d", ret); + } + + httpc_connection_t httpClientConn; + httpClientConn.result_fn = httpcFinished; + httpClientConn.headers_done_fn = httpcHeaderDone; + httpClientConn.use_proxy = 0; + ip_addr_t httpcAddr;// = IPADDR4_INIT_BYTES(10,10,10,127); + httpc_state_t* http_state; + err_t err; + */ + //size_t n, total, largest; // Idle runner while (true) { @@ -378,7 +493,7 @@ int main(void) { chThdWait(shelltp); // Waiting termination. } - //chThdSleepMilliseconds(10000); + /* n = chHeapStatus(NULL, &total, &largest); chprintf(console, "core free memory : %u bytes" SHELL_NEWLINE_STR, chCoreGetStatusX()); @@ -390,5 +505,19 @@ int main(void) { //chThdSleepMilliseconds(10000); //int ret = wolfSSL_get_ciphers(&tclOutput[0], (int)sizeof(tclOutput)); //https_client(); + + /* + chThdSleepMilliseconds(20000); + err = inet_aton("10.10.10.127", &httpcAddr); + chprintf(console, ">inet_aton: %d\r\n", err); + + if (httpcGo) { + LOCK_TCPIP_CORE(); + err = httpc_get_file_dns("www.seznam.cz", 80, "/#/login", &httpClientConn, httpcGetResult, NULL, &http_state); + if (!err) httpcGo = 0; + chprintf(console, ">httpc_get_file: %d\r\n", err); + UNLOCK_TCPIP_CORE(); + } + */ } } diff --git a/ohs_conf.h b/ohs_conf.h index 5dbe3a6..86c0bce 100644 --- a/ohs_conf.h +++ b/ohs_conf.h @@ -21,8 +21,8 @@ #define OHS_NAME "OHS" #define OHS_MAJOR 1 -#define OHS_MINOR 3 -#define OHS_MOD 10 +#define OHS_MINOR 4 +#define OHS_MOD 0 #define BACKUP_SRAM_SIZE 0x1000 // 4kB SRAM size @@ -751,20 +751,20 @@ typedef struct { char type; //= 'K/S/I'; char function;//= ' '; uint8_t number; //= 0; - // |- MQTT publish - // ||- Free - // |||- Battery low flag, for battery type node - // |||||||- Group number - // |||||||- 0 .. 15 - // |||||||- - // |||||||- - // ||||||||- Enabled - // 76543210 - uint16_t setting;// = B00011110; // 2 bytes to store also zone setting - float value; // = 0; - time_t lastOK; // = 0; - uint8_t queue; // = DUMMY_NO_VALUE 255; // No queue - char name[NAME_LENGTH]; // = ""; + float value; // = 0; + time_t lastOK; // = 0; + void *queue; // + // |- MQTT publish + // ||- Free + // |||- Battery low flag, for battery type node + // |||||||- Group number + // |||||||- 0 .. 15 + // |||||||- + // |||||||- + // ||||||||- Enabled + // 76543210 + uint16_t setting;// = B00011110; // 2 bytes to store also zone setting + char name[NAME_LENGTH]; // = ""; } node_t; node_t node[NODE_SIZE] __attribute__((section(".ram4"))); @@ -778,7 +778,7 @@ void initRuntimeNodes(void){ node[i].lastOK = 0; node[i].name[0] = '\0'; node[i].number = 0; - node[i].queue = DUMMY_NO_VALUE; + node[i].queue = NULL; node[i].setting = 0b00011110; node[i].type = '\0'; node[i].value = 0; @@ -832,7 +832,7 @@ void initRuntimeZones(void){ /* * Write to backup SRAM */ -int16_t writeToBkpSRAM(uint8_t *data, uint16_t size, uint16_t offset){ +uint16_t writeToBkpSRAM(uint8_t *data, uint16_t size, uint16_t offset){ osalDbgAssert(((size + offset) < BACKUP_SRAM_SIZE), "BkpSRAM out of region"); uint16_t i = 0; uint8_t *baseAddress = (uint8_t *) BKPSRAM_BASE; @@ -844,7 +844,7 @@ int16_t writeToBkpSRAM(uint8_t *data, uint16_t size, uint16_t offset){ /* * Read from backup SRAM */ -int16_t readFromBkpSRAM(uint8_t *data, uint16_t size, uint16_t offset){ +uint16_t readFromBkpSRAM(uint8_t *data, uint16_t size, uint16_t offset){ osalDbgAssert(((size + offset) < BACKUP_SRAM_SIZE), "BkpSRAM out of region"); uint16_t i = 0; uint8_t *baseAddress = (uint8_t *) BKPSRAM_BASE; @@ -856,7 +856,7 @@ int16_t readFromBkpSRAM(uint8_t *data, uint16_t size, uint16_t offset){ /* * Write to backup RTC */ -int16_t writeToBkpRTC(uint8_t *data, uint8_t size, uint8_t offset){ +uint8_t writeToBkpRTC(uint8_t *data, uint8_t size, uint8_t offset){ osalDbgAssert(((size + offset) < STM32_RTC_STORAGE_SIZE), "BkpRTC out of region"); osalDbgAssert(!(offset % 4), "BkpRTC misaligned"); // Offset is not aligned to to unint32_t registers uint8_t i = 0; @@ -874,7 +874,7 @@ int16_t writeToBkpRTC(uint8_t *data, uint8_t size, uint8_t offset){ /* * Read from backup RTC */ -int16_t readFromBkpRTC(uint8_t *data, uint8_t size, uint8_t offset){ +uint8_t readFromBkpRTC(uint8_t *data, uint8_t size, uint8_t offset){ osalDbgAssert(((size + offset) < STM32_RTC_STORAGE_SIZE), "BkpRTC out of region"); osalDbgAssert(!(offset % 4), "BkpRTC misaligned"); // Offset is not aligned to to unint32_t registers uint8_t i = 0; diff --git a/ohs_functions.h b/ohs_functions.h index ba04fd2..e1579af 100644 --- a/ohs_functions.h +++ b/ohs_functions.h @@ -94,19 +94,21 @@ int8_t sendData(uint8_t address, const uint8_t *data, uint8_t length){ if (address <= RADIO_UNIT_OFFSET) { RS485Msg_t rs485Data; - chprintf(console, "RS485 Send data to address: %d\r\n", address); + chprintf(console, "RS485 data to: %d\r\n", address); rs485Data.address = address; rs485Data.length = length; memcpy(&rs485Data.data[0], data, length); + /* for(uint8_t ii = 0; ii < length; ii++) { chprintf(console, "%d-%x, ", ii, rs485Data.data[ii]); } chprintf(console, "\r\n"); + */ if (rs485SendMsgWithACK(&RS485D2, &rs485Data, 5) == MSG_OK) resp = 1; else resp = -1; } // Radio if (address >= RADIO_UNIT_OFFSET) { - chprintf(console, "Radio Send data to address: %d\r\n", address - RADIO_UNIT_OFFSET); + chprintf(console, "Radio data to: %d\r\n", address - RADIO_UNIT_OFFSET); resp = rfm69SendWithRetry(address - RADIO_UNIT_OFFSET, data, length, 5); } return resp; @@ -121,7 +123,7 @@ int8_t sendCmd(uint8_t address, uint8_t command) { if (address <= RADIO_UNIT_OFFSET) { RS485Cmd_t rs485Cmd; - chprintf(console, "RS485 send cmd: %d to address: %d\r\n", command, address); + chprintf(console, "RS485 cmd: %d to: %d\r\n", command, address); rs485Cmd.address = address; rs485Cmd.length = command; if (rs485SendCmdWithACK(&RS485D2, &rs485Cmd, 3) == MSG_OK) resp = 1; @@ -132,10 +134,10 @@ int8_t sendCmd(uint8_t address, uint8_t command) { char radioCmd[] = {'C', command}; if (address == RADIO_UNIT_OFFSET) { - chprintf(console, "Radio send cmd: %d to broadcast.\r\n", command); + chprintf(console, "Radio cmd: %d to broadcast.\r\n", command); resp = rfm69Send(255, radioCmd, sizeof(radioCmd), false); } else { - chprintf(console, "Radio send cmd: %d to address: %d\r\n", command, address - RADIO_UNIT_OFFSET); + chprintf(console, "Radio cmd: %d to: %d\r\n", command, address - RADIO_UNIT_OFFSET); resp = rfm69Send(address - RADIO_UNIT_OFFSET, radioCmd, sizeof(radioCmd), true); } } diff --git a/ohs_httpdhandler.h b/ohs_httpdhandler.h index 221707f..4244fa7 100644 --- a/ohs_httpdhandler.h +++ b/ohs_httpdhandler.h @@ -53,7 +53,7 @@ static void *currentConn; char current_uri[LWIP_HTTPD_MAX_REQUEST_URI_LEN] __attribute__((section(".ram4"))); char postData[HTTP_POST_DATA_SIZE] __attribute__((section(".ram4"))); -char alertMsg[HTTP_ALERT_MSG_SIZE] __attribute__((section(".ram4"))); +char httpAlertMsg[HTTP_ALERT_MSG_SIZE] __attribute__((section(".ram4"))); char setCookie[HTTP_SET_COOKIE_SIZE] __attribute__((section(".ram4"))); void *verifiedConn = NULL; typedef struct { @@ -175,11 +175,11 @@ int fs_open_custom(struct fs_file *file, const char *name){ // Main Body chprintf(chp, "