From 99998e7da7f66843d7bf6a48b3ac7aaa59521efc Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Sun, 31 Jan 2021 19:55:16 +0100
Subject: [PATCH] change(ble): Go back to standard adv set when resetting
 hardware

---
 epicardium/ble/ble_adv.c      | 28 ++++++++++++++++++----------
 epicardium/modules/hardware.c |  6 ++++++
 2 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/epicardium/ble/ble_adv.c b/epicardium/ble/ble_adv.c
index 51ff30918..6b129c50a 100644
--- a/epicardium/ble/ble_adv.c
+++ b/epicardium/ble/ble_adv.c
@@ -137,11 +137,6 @@ void ble_adv_proc_msg(bleMsg_t *pMsg)
 }
 
 void ble_adv_init(void)
-{
-	pAppAdvCfg = (appAdvCfg_t *)&bleAdvCfg;
-}
-
-void ble_adv_setup(void)
 {
 	char buf[32];
 	char a, b, c, d, e, f, K;
@@ -163,6 +158,11 @@ void ble_adv_setup(void)
 	}
 	/* clang-format on */
 
+	pAppAdvCfg = (appAdvCfg_t *)&bleAdvCfg;
+}
+
+void ble_adv_setup(void)
+{
 	/* set advertising and scan response data for discoverable mode */
 	if (config_get_boolean_with_default("ble_hid_enable", false)) {
 		AppAdvSetData(
@@ -202,7 +202,7 @@ void ble_adv_stop(void)
 	}
 }
 
-void ble_adv_start(uint8_t mode)
+static void adv_start(uint8_t mode)
 {
 	if (advertising_mode != APP_MODE_NONE) {
 		/* We need to stop advertising in between or the
@@ -219,21 +219,29 @@ void ble_adv_start(uint8_t mode)
 		advertising_mode_target = mode;
 		AppAdvStart(advertising_mode);
 	}
+	tainted = false;
+}
+
+void ble_adv_start(uint8_t mode)
+{
+	adv_start(mode);
+	tainted = true;
 }
 
 void ble_adv_discoverable(bool discoverable)
 {
 	if (discoverable) {
-		if (advertising_mode != APP_MODE_DISCOVERABLE) {
+		if (advertising_mode != APP_MODE_DISCOVERABLE || tainted) {
 			LOG_INFO("ble", "Making bondable and discoverable");
-			ble_adv_start(APP_MODE_DISCOVERABLE);
+			adv_start(APP_MODE_DISCOVERABLE);
 		}
 	} else {
 		/* TODO: This does way more than the function name indicates */
 		if (AppDbCheckBonded()) {
-			if (advertising_mode != APP_MODE_CONNECTABLE) {
+			if (advertising_mode != APP_MODE_CONNECTABLE ||
+			    tainted) {
 				LOG_INFO("ble", "Bonded. Making connectable");
-				ble_adv_start(APP_MODE_CONNECTABLE);
+				adv_start(APP_MODE_CONNECTABLE);
 			}
 		} else {
 			LOG_INFO("ble", "Not bonded. Stop advertising");
diff --git a/epicardium/modules/hardware.c b/epicardium/modules/hardware.c
index fb413958e..de0d9d215 100644
--- a/epicardium/modules/hardware.c
+++ b/epicardium/modules/hardware.c
@@ -14,6 +14,7 @@
 #include "pmic.h"
 #include "portexpander.h"
 #include "max86150.h"
+#include "ble/ble_api.h"
 
 #include "gpio.h"
 #include "i2c.h"
@@ -288,6 +289,11 @@ int hardware_reset(void)
 	/*
 	 * BLE
 	 */
+
+	/* Reset advertisement data */
+	ble_adv_setup();
+
+	/* Start advertising again if needed */
 	epic_ble_set_mode(false, false);
 
 	return 0;
-- 
GitLab