Skip to content

Commit

Permalink
1.4.0., ATC for RFM69, RTC battery, various changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vysocan committed Feb 8, 2022
1 parent 9566792 commit fa9f1bd
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 66 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*
Expand Down
141 changes: 135 additions & 6 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
};
Expand All @@ -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();

Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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) {
Expand All @@ -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());
Expand All @@ -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();
}
*/
}
}
42 changes: 21 additions & 21 deletions ohs_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")));

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Loading

0 comments on commit fa9f1bd

Please sign in to comment.