From 43bca5af70989e4c25c0aee6b00103d5b110a2ff Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Sun, 24 Jan 2021 23:59:40 +0100
Subject: [PATCH] feat(mp-ble): Allow to close current connection

---
 epicardium/ble/epic_ble_api.c           | 11 +++++++++++
 epicardium/epicardium.h                 |  5 ++++-
 pycardium/modules/modbluetooth_card10.c | 10 ++++++----
 3 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/epicardium/ble/epic_ble_api.c b/epicardium/ble/epic_ble_api.c
index b37ca2aa8..726d26a94 100644
--- a/epicardium/ble/epic_ble_api.c
+++ b/epicardium/ble/epic_ble_api.c
@@ -5,6 +5,7 @@
 #include "api/interrupt-sender.h"
 
 #include "wsf_buf.h"
+#include "app_api.h"
 #include "svc_core.h"
 
 #include "FreeRTOS.h"
@@ -92,6 +93,16 @@ void ble_epic_dm_api_event(dmEvt_t *dm_event)
 	}
 }
 
+void epic_ble_close_connection(uint8_t connId)
+{
+	AppConnClose(connId);
+}
+
+int epic_ble_is_connection_open(void)
+{
+	return AppConnIsOpen();
+}
+
 int epic_ble_set_device_name(const uint8_t *buf, uint16_t len)
 {
 	return epic_ble_atts_set_attr(GAP_DN_HDL, buf, len);
diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h
index 12f7b86fb..f8bb96cc4 100644
--- a/epicardium/epicardium.h
+++ b/epicardium/epicardium.h
@@ -177,6 +177,8 @@ typedef _Bool bool;
 #define API_BLE_GET_ATT_WRITE                 0x1BD
 #define API_BLE_FREE_ATT_WRITE                0x1BE
 #define API_BLE_ATTS_DYN_ADD_CHARACTERISTIC             0x1BF
+#define API_BLE_CLOSE_CONNECTION              0x170
+#define API_BLE_IS_CONNECTION_OPEN            0x171
 #define API_BLE_SET_DEVICE_NAME               0x172
 #define API_BLE_GET_DEVICE_NAME               0x173
 #define API_BLE_GET_ADDRESS                   0x174
@@ -2607,7 +2609,8 @@ API(API_BLE_ATTS_HANDLE_VALUE_IND, int epic_ble_atts_handle_value_ind(uint8_t co
 API(API_BLE_ATTS_SET_BUFFER, int epic_ble_atts_set_buffer(uint16_t value_handle, size_t len, bool append));
 
 API(API_BLE_FREE_EVENT, int epic_ble_free_event(struct epic_ble_event *e));
-
+API(API_BLE_CLOSE_CONNECTION, void epic_ble_close_connection(uint8_t connId));
+API(API_BLE_IS_CONNECTION_OPEN, int epic_ble_is_connection_open(void));
 API(API_BLE_SET_DEVICE_NAME, int epic_ble_set_device_name(const uint8_t *buf, uint16_t len));
 API(API_BLE_GET_DEVICE_NAME, int epic_ble_get_device_name(uint8_t **buf, uint16_t *len));
 API(API_BLE_GET_ADDRESS, void epic_ble_get_address(uint8_t *addr));
diff --git a/pycardium/modules/modbluetooth_card10.c b/pycardium/modules/modbluetooth_card10.c
index f64eab4cb..adcc21efe 100644
--- a/pycardium/modules/modbluetooth_card10.c
+++ b/pycardium/modules/modbluetooth_card10.c
@@ -352,10 +352,12 @@ int mp_bluetooth_gap_advertise_start(
 	const uint8_t *sr_data,
 	size_t sr_data_len
 ) {
-	// Temporarily going to scanner drops the connection
+	// Dropping any current connection starts advertising on the card10
 	// TODO: modify the advertising data
-	epic_ble_set_mode(false, true);
-	epic_ble_set_mode(false, false);
+	int connection = epic_ble_is_connection_open();
+	if (connection > 0) {
+		epic_ble_close_connection(connection);
+	}
 	return 0;
 }
 
@@ -631,7 +633,7 @@ int mp_bluetooth_gatts_set_buffer(uint16_t value_handle, size_t len, bool append
 // Disconnect from a central or peripheral.
 int mp_bluetooth_gap_disconnect(uint16_t conn_handle)
 {
-	raise();
+	epic_ble_close_connection(conn_handle);
 	return 0;
 }
 
-- 
GitLab