From 77d19af5527e10fea4c5da3750f5c75ab9d400d7 Mon Sep 17 00:00:00 2001
From: Hauke Mehrtens <hauke@hauke-m.de>
Date: Fri, 23 Aug 2019 12:38:13 +0200
Subject: [PATCH] BLE: FileTrans: Allow to deactivate File transfer

This allows to deactivate the File Transfer in the ble.txt

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
---
 epicardium/ble/ble.c          | 52 +++++++++++++++++++++++++++++------
 epicardium/ble/filetransfer.c | 15 +++++++++-
 epicardium/modules/modules.h  |  1 +
 3 files changed, 58 insertions(+), 10 deletions(-)

diff --git a/epicardium/ble/ble.c b/epicardium/ble/ble.c
index 4be880c3..f2f05fe8 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 fa2cff4d..8f7c220d 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 d6d045f2..5a2ac6e7 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);
-- 
GitLab