Skip to content
Snippets Groups Projects
Commit 1308ae25 authored by schneider's avatar schneider
Browse files

feat(ble): Show peer device name in BLE app

parent 7a73f992
No related branches found
No related tags found
No related merge requests found
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include "bas/bas_api.h" #include "bas/bas_api.h"
#include "hrps/hrps_api.h" #include "hrps/hrps_api.h"
#include "rscp/rscp_api.h" #include "rscp/rscp_api.h"
#include "profiles/gap_api.h"
#include "cccd.h" #include "cccd.h"
#include "ble_api.h" #include "ble_api.h"
...@@ -462,6 +463,7 @@ static void bleClose(bleMsg_t *pMsg) ...@@ -462,6 +463,7 @@ static void bleClose(bleMsg_t *pMsg)
{ {
/* stop battery measurement */ /* stop battery measurement */
BasMeasBattStop((dmConnId_t) pMsg->hdr.param); BasMeasBattStop((dmConnId_t) pMsg->hdr.param);
GapClearDeviceName();
} }
/*************************************************************************************************/ /*************************************************************************************************/
...@@ -593,6 +595,15 @@ uint32_t epic_ble_get_compare_value(void) ...@@ -593,6 +595,15 @@ uint32_t epic_ble_get_compare_value(void)
return pair_confirm_value; return pair_confirm_value;
} }
int epic_ble_get_peer_device_name(char *buf, size_t buf_size)
{
if (AppConnIsOpen() != DM_CONN_ID_NONE) {
return GapGetDeviceName(buf, buf_size);
} else {
return -ENOENT;
}
}
int epic_ble_get_last_pairing_name(char *buf, size_t buf_size) int epic_ble_get_last_pairing_name(char *buf, size_t buf_size)
{ {
if(last_pairing == NULL) { if(last_pairing == NULL) {
......
...@@ -45,6 +45,9 @@ enum ...@@ -45,6 +45,9 @@ enum
Function Declarations Function Declarations
**************************************************************************************************/ **************************************************************************************************/
int GapGetDeviceName(char *buf, size_t buf_size);
void GapClearDeviceName(void);
/*************************************************************************************************/ /*************************************************************************************************/
/*! /*!
* \brief Perform service and characteristic discovery for GAP service. Note that pHdlList * \brief Perform service and characteristic discovery for GAP service. Note that pHdlList
......
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
#include "app_api.h" #include "app_api.h"
#include "gap_api.h" #include "gap_api.h"
#include <string.h>
/* card10: /* card10:
* copied from lib/sdk/Libraries/BTLE/stack/ble-profiles/sources/profiles/gap/gap_main.c * copied from lib/sdk/Libraries/BTLE/stack/ble-profiles/sources/profiles/gap/gap_main.c
*/ */
...@@ -33,6 +35,7 @@ ...@@ -33,6 +35,7 @@
/************************************************************************************************** /**************************************************************************************************
Local Variables Local Variables
**************************************************************************************************/ **************************************************************************************************/
static char dn_value[ATT_DEFAULT_PAYLOAD_LEN + 1];
/*! GAP service characteristics for discovery */ /*! GAP service characteristics for discovery */
static const attcDiscChar_t gapDn = static const attcDiscChar_t gapDn =
...@@ -66,6 +69,18 @@ static const attcDiscChar_t *gapDiscCharList[] = ...@@ -66,6 +69,18 @@ static const attcDiscChar_t *gapDiscCharList[] =
/* sanity check: make sure handle list length matches characteristic list length */ /* sanity check: make sure handle list length matches characteristic list length */
WSF_CT_ASSERT(GAP_HDL_LIST_LEN == ((sizeof(gapDiscCharList) / sizeof(attcDiscChar_t *)))); WSF_CT_ASSERT(GAP_HDL_LIST_LEN == ((sizeof(gapDiscCharList) / sizeof(attcDiscChar_t *))));
int GapGetDeviceName(char *buf, size_t buf_size)
{
memset(buf, 0, buf_size);
strncpy(buf, dn_value, buf_size - 1);
return 0;
}
void GapClearDeviceName(void)
{
dn_value[0] = 0;
}
/*************************************************************************************************/ /*************************************************************************************************/
/*! /*!
* \brief Perform service and characteristic discovery for GAP service. Note that pHdlList * \brief Perform service and characteristic discovery for GAP service. Note that pHdlList
...@@ -105,6 +120,9 @@ uint8_t GapValueUpdate(uint16_t *pHdlList, attEvt_t *pMsg) ...@@ -105,6 +120,9 @@ uint8_t GapValueUpdate(uint16_t *pHdlList, attEvt_t *pMsg)
/* device name string */ /* device name string */
if (pMsg->handle == pHdlList[GAP_DN_HDL_IDX]) if (pMsg->handle == pHdlList[GAP_DN_HDL_IDX])
{ {
int len = (pMsg->valueLen < (sizeof(dn_value) - 1)) ? pMsg->valueLen : (sizeof(dn_value) - 1);
memcpy(dn_value, pMsg->pValue, len);
dn_value[len] = '\0';
} }
/* Central Address Resolution */ /* Central Address Resolution */
else if (pMsg->handle == pHdlList[GAP_CAR_HDL_IDX]) else if (pMsg->handle == pHdlList[GAP_CAR_HDL_IDX])
......
...@@ -156,6 +156,8 @@ typedef _Bool bool; ...@@ -156,6 +156,8 @@ typedef _Bool bool;
#define API_BLE_GET_EVENT 0x143 #define API_BLE_GET_EVENT 0x143
#define API_BLE_GET_SCAN_REPORT 0x144 #define API_BLE_GET_SCAN_REPORT 0x144
#define API_BLE_GET_LAST_PAIRING_NAME 0x145 #define API_BLE_GET_LAST_PAIRING_NAME 0x145
#define API_BLE_GET_PEER_DEVICE_NAME 0x146
/* clang-format on */ /* clang-format on */
...@@ -2249,6 +2251,21 @@ API(API_BLE_GET_COMPARE_VALUE, uint32_t epic_ble_get_compare_value(void)); ...@@ -2249,6 +2251,21 @@ API(API_BLE_GET_COMPARE_VALUE, uint32_t epic_ble_get_compare_value(void));
*/ */
API(API_BLE_GET_LAST_PAIRING_NAME, int epic_ble_get_last_pairing_name(char *buf, size_t buf_size)); API(API_BLE_GET_LAST_PAIRING_NAME, int epic_ble_get_last_pairing_name(char *buf, size_t buf_size));
/**
* Retrieve the name of the peer to which we are connected
*
* The name might be empty if the peer device does not expose it or
* if it has not yet been read from it.
*
* :return: `0` on success or a negative value if an error occured. Possible
* errors:
*
* - ``-ENOENT``: There is no active connection at the moment.
*
* .. versionadded:: 1.16
*/
API(API_BLE_GET_PEER_DEVICE_NAME, int epic_ble_get_peer_device_name(char *buf, size_t buf_size));
/** /**
* Indicate wether the user confirmed the compare value. * Indicate wether the user confirmed the compare value.
* *
......
...@@ -7,6 +7,12 @@ import interrupt ...@@ -7,6 +7,12 @@ import interrupt
import config import config
ble_event = None ble_event = None
is_active = False
STATE_IDLE = 1
STATE_NUMERIC_COMPARISON = 2
STATE_WAIT_FOR_COMPLETION = 3
STATE_FAIL = 4
def ble_callback(_): def ble_callback(_):
...@@ -36,7 +42,7 @@ def triangle(disp, x, y, left): ...@@ -36,7 +42,7 @@ def triangle(disp, x, y, left):
def toggle(): def toggle():
if is_active(): if is_active:
config.set_string("ble_enable", "false") config.set_string("ble_enable", "false")
else: else:
config.set_string("ble_enable", "true") config.set_string("ble_enable", "true")
...@@ -49,26 +55,21 @@ def toggle(): ...@@ -49,26 +55,21 @@ def toggle():
os.reset() os.reset()
def is_active(): def headline():
disp.print("BLE", posy=0, fg=[0, 255, 255])
if is_active:
try: try:
active = config.get_string("ble_enable") dn = sys_ble.get_peer_device_name()
if active == "true" or active == "1": disp.print("Active", posy=0, posx=50, fg=[0, 255, 0])
return True disp.print(dn[:11], posy=20, posx=0, fg=[0, 255, 0])
except OSError: except OSError:
pass disp.print("Ready", posy=0, posx=50, fg=[0, 255, 0])
return False
def headline():
disp.print("BLE", posy=0, fg=[0, 255, 255])
if is_active():
disp.print("active", posy=20, fg=[0, 255, 0])
mac = load_mac() mac = load_mac()
if mac is not None: if mac is not None:
disp.print(mac[9:], posy=60, fg=[0, 0, 255]) disp.print(mac[9:], posy=60, fg=[0, 0, 255])
else: else:
disp.print("inactive", posy=20, fg=[255, 0, 0]) disp.print("Off", posy=0, posx=50, fg=[255, 0, 0])
def selector(): def selector():
...@@ -76,9 +77,17 @@ def selector(): ...@@ -76,9 +77,17 @@ def selector():
disp.print("toggle", posx=25, posy=40, fg=[255, 255, 255]) disp.print("toggle", posx=25, posy=40, fg=[255, 255, 255])
try:
active = config.get_string("ble_enable")
if active.lower() == "true" or active == "1":
is_active = True
except OSError:
pass
init() init()
disp = display.open() disp = display.open()
state = 1 state = STATE_IDLE
v_old = buttons.read() v_old = buttons.read()
while True: while True:
...@@ -86,55 +95,57 @@ while True: ...@@ -86,55 +95,57 @@ while True:
v = ~v_old & v_new v = ~v_old & v_new
v_old = v_new v_old = v_new
if state == 1: if state == STATE_IDLE:
# print config screen # print config screen
disp.clear() disp.clear()
headline() headline()
selector() selector()
disp.update() disp.update()
state = 2
elif state == 2:
# wait for button press or ble_event # wait for button press or ble_event
if ble_event == sys_ble.EVENT_HANDLE_NUMERIC_COMPARISON: if ble_event == sys_ble.EVENT_HANDLE_NUMERIC_COMPARISON:
ble_event = None ble_event = None
state = 3 state = STATE_NUMERIC_COMPARISON
if v & buttons.TOP_RIGHT: if v & buttons.TOP_RIGHT:
toggle() toggle()
state = 1
elif state == 3: elif state == STATE_NUMERIC_COMPARISON:
# print confirmation value # print confirmation value
compare_value = sys_ble.get_compare_value() compare_value = sys_ble.get_compare_value()
disp.clear() disp.clear()
disp.print("BLE: Pair?", posy=0, fg=[0, 0, 255])
disp.print("Code:", posy=20, fg=[0, 255, 255]) try:
dn = sys_ble.get_peer_device_name()
disp.print(dn[:11], posy=0, fg=[0, 0, 255])
except OSError:
pass
disp.print("Pair Code:", posy=20, fg=[0, 255, 255])
disp.print(" %06d" % compare_value, posy=40, fg=[255, 255, 255]) disp.print(" %06d" % compare_value, posy=40, fg=[255, 255, 255])
disp.print("Yes", posy=60, fg=[0, 255, 0]) disp.print("Yes", posy=60, fg=[0, 255, 0])
disp.print("No", posx=120, posy=60, fg=[255, 0, 0]) disp.print("No", posx=120, posy=60, fg=[255, 0, 0])
disp.update() disp.update()
state = 4
elif state == 4:
# wait for button press or ble_event # wait for button press or ble_event
if ble_event == sys_ble.EVENT_PAIRING_FAILED: if ble_event == sys_ble.EVENT_PAIRING_FAILED:
ble_event = None ble_event = None
state = 6 state = STATE_FAIL
if v & buttons.BOTTOM_LEFT: if v & buttons.BOTTOM_LEFT:
sys_ble.confirm_compare_value(True) sys_ble.confirm_compare_value(True)
disp.clear() disp.clear()
disp.print("BLE Pairing", posy=0, fg=[0, 0, 255]) disp.print("BLE Pairing", posy=0, fg=[0, 0, 255])
disp.print("Please Wait", posy=40, fg=[255, 255, 255]) disp.print("Please Wait", posy=40, fg=[255, 255, 255])
disp.update() disp.update()
state = 5 state = STATE_WAIT_FOR_COMPLETION
elif v & (buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT): elif v & (buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT):
sys_ble.confirm_compare_value(False) sys_ble.confirm_compare_value(False)
state = 6 state = STATE_FAIL
elif state == 5: elif state == STATE_WAIT_FOR_COMPLETION:
# Wait for pairing to complete # Wait for pairing to complete
if ble_event == sys_ble.EVENT_PAIRING_FAILED: if ble_event == sys_ble.EVENT_PAIRING_FAILED:
ble_event = None ble_event = None
state = 6 state = STATE_FAIL
elif ble_event == sys_ble.EVENT_PAIRING_COMPLETE: elif ble_event == sys_ble.EVENT_PAIRING_COMPLETE:
ble_event = None ble_event = None
pairing_name = sys_ble.get_last_pairing_name().split("/")[-1].split(".")[0] pairing_name = sys_ble.get_last_pairing_name().split("/")[-1].split(".")[0]
...@@ -147,13 +158,13 @@ while True: ...@@ -147,13 +158,13 @@ while True:
time.sleep(5) time.sleep(5)
os.exec("main.py") os.exec("main.py")
elif state == 6: elif state == STATE_FAIL:
# display fail screen and wait 5 seconds # display fail screen and wait 5 seconds
disp.clear() disp.clear()
disp.print("BLE Pairing", posy=0, fg=[0, 0, 255]) disp.print("BLE Pairing", posy=0, fg=[0, 0, 255])
disp.print(" Fail", posy=40, fg=[255, 0, 0]) disp.print(" Fail", posy=40, fg=[255, 0, 0])
disp.update() disp.update()
time.sleep(5) time.sleep(5)
state = 1 state = STATE_IDLE
time.sleep(0.1) time.sleep(0.1)
...@@ -200,6 +200,7 @@ Q(BLE) ...@@ -200,6 +200,7 @@ Q(BLE)
Q(ble) Q(ble)
Q(get_compare_value) Q(get_compare_value)
Q(get_last_pairing_name) Q(get_last_pairing_name)
Q(get_peer_device_name)
Q(get_scan_report) Q(get_scan_report)
Q(confirm_compare_value) Q(confirm_compare_value)
Q(set_bondable) Q(set_bondable)
......
...@@ -42,6 +42,23 @@ static MP_DEFINE_CONST_FUN_OBJ_0( ...@@ -42,6 +42,23 @@ static MP_DEFINE_CONST_FUN_OBJ_0(
ble_get_last_pairing_name_obj, mp_ble_get_last_pairing_name ble_get_last_pairing_name_obj, mp_ble_get_last_pairing_name
); );
static mp_obj_t mp_ble_get_peer_device_name(void)
{
char device_name_str[32];
int status = epic_ble_get_peer_device_name(
device_name_str, sizeof(device_name_str)
);
if (status < 0) {
mp_raise_OSError(-status);
}
mp_obj_t ret = mp_obj_new_str(device_name_str, strlen(device_name_str));
return ret;
}
static MP_DEFINE_CONST_FUN_OBJ_0(
ble_get_peer_device_name_obj, mp_ble_get_peer_device_name
);
static mp_obj_t mp_ble_get_scan_report(void) static mp_obj_t mp_ble_get_scan_report(void)
{ {
struct epic_scan_report scan_report; struct epic_scan_report scan_report;
...@@ -94,6 +111,8 @@ static const mp_rom_map_elem_t ble_module_globals_table[] = { ...@@ -94,6 +111,8 @@ static const mp_rom_map_elem_t ble_module_globals_table[] = {
MP_ROM_PTR(&ble_get_compare_value_obj) }, MP_ROM_PTR(&ble_get_compare_value_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_last_pairing_name), { MP_ROM_QSTR(MP_QSTR_get_last_pairing_name),
MP_ROM_PTR(&ble_get_last_pairing_name_obj) }, MP_ROM_PTR(&ble_get_last_pairing_name_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_peer_device_name),
MP_ROM_PTR(&ble_get_peer_device_name_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_scan_report), { MP_ROM_QSTR(MP_QSTR_get_scan_report),
MP_ROM_PTR(&ble_get_scan_report_obj) }, MP_ROM_PTR(&ble_get_scan_report_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_event), MP_ROM_PTR(&ble_get_event_obj) }, { MP_ROM_QSTR(MP_QSTR_get_event), MP_ROM_PTR(&ble_get_event_obj) },
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment