diff --git a/epicardium/ble/ble.c b/epicardium/ble/ble.c index 4be880c3f1b49c6eaef9c5fd2812a37809dc4a51..f2f05fe886448ee2a94097df830cf92283e3fb14 100644 --- a/epicardium/ble/ble.c +++ b/epicardium/ble/ble.c @@ -1,3 +1,4 @@ +#define _POSIX_C_SOURCE 200809 #include "epicardium.h" #include "modules/log.h" @@ -13,6 +14,7 @@ #include "timers.h" #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <stdbool.h> @@ -170,28 +172,60 @@ void wsf_ble_signal_event(void) } /*************************************************************************************************/ #define BLEMAXCFGBYTES 100 -bool ble_shall_start(void) + +int ble_get_option(const char *name, char *buf, size_t buf_len) { + char *saveptr1, *saveptr2; + char *token, *str1, *name_parsed, *option; + char cfgBuf[BLEMAXCFGBYTES + 1]; + int bleConfigFile = epic_file_open("ble.txt", "r"); if (bleConfigFile < 0) { LOG_INFO("ble", "can not open ble.txt -> BLE is not started"); - epic_file_close(bleConfigFile); - return false; + return bleConfigFile; } - char cfgBuf[BLEMAXCFGBYTES + 1]; - int readNum = epic_file_read(bleConfigFile, cfgBuf, BLEMAXCFGBYTES); + int readNum = epic_file_read(bleConfigFile, cfgBuf, BLEMAXCFGBYTES - 1); epic_file_close(bleConfigFile); if (readNum < 0) { LOG_WARN("ble", "can not read ble.txt -> BLE is not started"); - return false; + return readNum; } cfgBuf[readNum] = '\0'; - char bleActiveStr[] = "active=true"; - cfgBuf[sizeof(bleActiveStr) - 1] = '\0'; + str1 = cfgBuf; + while (TRUE) { + token = strtok_r(str1, "\n", &saveptr1); + if (token == NULL) + break; + str1 = NULL; + + name_parsed = strtok_r(token, "=", &saveptr2); + if (name_parsed == NULL) + continue; + if (strcmp(token, name_parsed) != 0) + continue; + option = strtok_r(NULL, "=", &saveptr2); + if (option == NULL) + return -ENOENT; + + strncpy(buf, option, buf_len - 1); + buf[buf_len - 1] = '\0'; + return 0; + } + return -ENOENT; +} + +bool ble_shall_start(void) +{ + char buf[10]; + int ret; + + ret = ble_get_option("active", buf, sizeof(buf)); + if (ret) + return false; - if (strcmp(cfgBuf, "active=true") != 0) { + if (strcmp(buf, "true") != 0) { LOG_INFO("ble", "BLE is disabled."); return false; } else { diff --git a/epicardium/ble/filetransfer.c b/epicardium/ble/filetransfer.c index fa2cff4d3e8f884a6f382f469fab13c7126045cb..8f7c220ddad5220f4c136c9ee38f8d53fe6d8908 100644 --- a/epicardium/ble/filetransfer.c +++ b/epicardium/ble/filetransfer.c @@ -407,10 +407,23 @@ static attsGroup_t fileTransCfgGroup = { .endHandle = FILE_TRANS_END_HDL, }; +bool ble_fileTrans_start(void) +{ + char buf[10]; + int ret; + + /* if the option is not there activate it for now */ + ret = ble_get_option("fileTrans", buf, sizeof(buf)); + if (ret) + return true; + + return !strcmp("true", buf); +} /* * This registers and starts the BLE file transfer service. */ void bleFileTransfer_init(void) { - AttsAddGroup(&fileTransCfgGroup); + if (ble_fileTrans_start()) + AttsAddGroup(&fileTransCfgGroup); } diff --git a/epicardium/modules/modules.h b/epicardium/modules/modules.h index d6d045f2e8cc4e45a59e7a9a10495fa0373f9333..5a2ac6e7855825dbf20ce7ec58bb2eb0eeffb3fa 100644 --- a/epicardium/modules/modules.h +++ b/epicardium/modules/modules.h @@ -66,6 +66,7 @@ int pmic_read_amux(enum pmic_amux_signal sig, float *result); void vBleTask(void *pvParameters); bool ble_shall_start(void); void ble_uart_write(uint8_t *pValue, uint8_t len); +int ble_get_option(const char *name, char *buf, size_t buf_len); /* ---------- Hardware (Peripheral) Locks ---------------------------------- */ void hwlock_init(void);