From 96d845f8c6825a76b103a764c9023d2b78fce84b Mon Sep 17 00:00:00 2001 From: schneider <schneider@blinkenlichts.net> Date: Mon, 5 Oct 2020 01:22:49 +0200 Subject: [PATCH] change(ble): Use card10.cfg instead of ble.txt and mac.txt --- Documentation/card10-cfg.rst | 4 ++++ epicardium/ble/ble.c | 40 ++++++++++-------------------------- epicardium/ble/ble_main.c | 3 ++- preload/apps/ble/__init__.py | 37 +++++++++++++++------------------ 4 files changed, 34 insertions(+), 50 deletions(-) diff --git a/Documentation/card10-cfg.rst b/Documentation/card10-cfg.rst index 881c3bd1c..7a0173b3d 100644 --- a/Documentation/card10-cfg.rst +++ b/Documentation/card10-cfg.rst @@ -43,5 +43,9 @@ Option name Type Description ------------------ ---------- ----------- ``default_app`` String Full path to the exectutable file of the default application. If this option is not set,``apps/analog_clock/__init__.py`` is used. ------------------ ---------- ----------- +``ble_enable`` Boolean Activate the BLE interface. Turn off for more privacy or to conserve energy. +------------------ ---------- ----------- +``ble_mac`` Boolean MAC address used for BLE. Format: ``ca:4d:10:xx:xx:xx``. +------------------ ---------- ----------- ``ble_log_enable`` Boolean Activate HCI level logging of BLE data. Creates a new btsnoop compatible log file named ``ble.log`` in the ``logs`` folder after each boot if BLE is activated. Keeps the last 10 files. ================== ========== =========== diff --git a/epicardium/ble/ble.c b/epicardium/ble/ble.c index d1bd46d92..47ef03ebe 100644 --- a/epicardium/ble/ble.c +++ b/epicardium/ble/ble.c @@ -174,11 +174,11 @@ static void setAddress(void) uint8_t bdAddr[6] = { 0xCA, 0x4D, 0x10, 0x00, 0x00, 0x00 }; char buf[32]; - int result = fs_read_text_file("mac.txt", buf, sizeof(buf)); + int result = epic_config_get_string("ble_mac", buf, sizeof(buf)); if (result < 0) { - APP_TRACE_INFO0("mac.txt not found, generating random MAC"); - epic_trng_read(bdAddr + 3, 3); + APP_TRACE_INFO0("ble_mac not set. Generating random MAC"); + epic_csprng_read(bdAddr + 3, 3); sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n", bdAddr[0], @@ -187,9 +187,9 @@ static void setAddress(void) bdAddr[3], bdAddr[4], bdAddr[5]); - fs_write_file("mac.txt", buf, strlen(buf)); + epic_config_set_string("ble_mac", buf); } else { - APP_TRACE_INFO1("mac file contents: %s", buf); + APP_TRACE_INFO1("ble_mac: %s", buf); } int a, b, c, d, e, f; @@ -255,35 +255,17 @@ void RSV11_IRQHandler(void) notify(); } /*************************************************************************************************/ -#define BLEMAXCFGBYTES 100 bool ble_shall_start(void) { - 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; - } - - char cfgBuf[BLEMAXCFGBYTES + 1]; - int readNum = epic_file_read(bleConfigFile, cfgBuf, BLEMAXCFGBYTES); - epic_file_close(bleConfigFile); - if (readNum < 0) { - LOG_WARN("ble", "can not read ble.txt -> BLE is not started"); - return false; - } - cfgBuf[readNum] = '\0'; + bool ble_enabled = config_get_boolean_with_default("ble_enable", false); - char bleActiveStr[] = "active=true"; - cfgBuf[sizeof(bleActiveStr) - 1] = '\0'; - - if (strcmp(cfgBuf, "active=true") != 0) { - LOG_INFO("ble", "BLE is disabled."); - return false; - } else { + if (ble_enabled) { LOG_INFO("ble", "BLE is enabled."); - return true; + } else { + LOG_INFO("ble", "BLE is disabled."); } + + return ble_enabled; } /*************************************************************************************************/ static void scheduleTimer(void) diff --git a/epicardium/ble/ble_main.c b/epicardium/ble/ble_main.c index 35024f6aa..dc3d54669 100644 --- a/epicardium/ble/ble_main.c +++ b/epicardium/ble/ble_main.c @@ -464,7 +464,8 @@ static void bleSetup(bleMsg_t *pMsg) char buf[32]; char a, b, c, d, e, f, K; - if (fs_read_text_file("mac.txt", buf, sizeof(buf)) > 0) + int result = epic_config_get_string("ble_mac", buf, sizeof(buf)); + if (result == 0) { if (sscanf(buf, "%c%c:%c%c:%c%c:%c%c:%c%c:%c%c", &K,&K,&K,&K,&K,&K, &a, &b, &c, &d, &e, &f) == 12) { diff --git a/preload/apps/ble/__init__.py b/preload/apps/ble/__init__.py index d5b0b7163..eae9969d6 100644 --- a/preload/apps/ble/__init__.py +++ b/preload/apps/ble/__init__.py @@ -4,11 +4,8 @@ import time import buttons import sys_ble import interrupt +import config -CONFIG_NAME = "ble.txt" -MAC_NAME = "mac.txt" -ACTIVE_STRING = "active=true" -INACTIVE_STRING = "active=false" ble_event = None @@ -18,19 +15,16 @@ def ble_callback(_): def init(): - if CONFIG_NAME not in os.listdir("."): - with open(CONFIG_NAME, "w") as f: - f.write(INACTIVE_STRING) interrupt.set_callback(interrupt.BLE, ble_callback) interrupt.enable_callback(interrupt.BLE) sys_ble.set_bondable(True) def load_mac(): - if MAC_NAME in os.listdir("."): - with open(MAC_NAME) as f: - return f.read().strip() - + try: + return config.get_string("ble_mac") + except OSError: + return None def triangle(disp, x, y, left): yf = 1 if left else -1 @@ -41,9 +35,10 @@ def triangle(disp, x, y, left): def toggle(): - content = INACTIVE_STRING if is_active() else ACTIVE_STRING - with open(CONFIG_NAME, "w") as f: - f.write(content) + if is_active(): + config.set_string("ble_enable", "false") + else: + config.set_string("ble_enable", "true") disp.clear() disp.print("resetting", posy=0, fg=[0, 255, 255]) @@ -54,12 +49,14 @@ def toggle(): def is_active(): - with open(CONFIG_NAME, "r") as f: - state = f.readlines()[0] - if len(state) < len(ACTIVE_STRING): - return False - state = state[0 : len(ACTIVE_STRING)] - return state == ACTIVE_STRING + try: + active = config.get_string("ble_enable") + if active == "true" or active == "1": + return True + except OSError: + pass + + return False def headline(): -- GitLab