Commit c090b391 authored by schneider's avatar schneider
Browse files

feat(ble): Show the name of a successful pairing

parent 6b91b1a7
......@@ -224,6 +224,7 @@ wsfHandlerId_t bleHandlerId;
static dmConnId_t pair_connId = DM_CONN_ID_NONE;
static uint32_t pair_confirm_value;
static appDbHdl_t last_pairing = NULL;
static void BleHandler(wsfEventMask_t event, wsfMsgHdr_t *pMsg);
......@@ -578,6 +579,15 @@ uint32_t epic_ble_get_compare_value(void)
return pair_confirm_value;
}
int epic_ble_get_last_pairing_name(char *buf, size_t buf_size)
{
if(last_pairing == NULL) {
return -ENOENT;
}
return AppDbGetPairingName(last_pairing, buf, buf_size);
}
void epic_ble_compare_response(bool confirmed)
{
if(!active) {
......@@ -765,7 +775,8 @@ static void bleProcMsg(bleMsg_t *pMsg)
pMsg->dm.pairCmpl.auth);
DmSecGenerateEccKeyReq();
AppDbNvmStoreBond(AppDbGetHdl((dmConnId_t) pMsg->hdr.param));
last_pairing = AppDbGetHdl((dmConnId_t) pMsg->hdr.param);
AppDbNvmStoreBond(last_pairing);
pair_connId = DM_CONN_ID_NONE;
trigger_event(BLE_EVENT_PAIRING_COMPLETE);
......
......@@ -930,3 +930,9 @@ void AppDbNvmStoreBond(appDbHdl_t hdl)
}
}
/* clang-format on */
int AppDbGetPairingName(appDbHdl_t hdl, char *buf, size_t buf_size)
{
appDbRec_t *pRec = (appDbRec_t *)hdl;
return record_to_filename(pRec, buf, buf_size);
}
......@@ -155,6 +155,7 @@ typedef _Bool bool;
#define API_BLE_SET_MODE 0x142
#define API_BLE_GET_EVENT 0x143
#define API_BLE_GET_SCAN_REPORT 0x144
#define API_BLE_GET_LAST_PAIRING_NAME 0x145
/* clang-format on */
......@@ -2236,6 +2237,18 @@ API(API_BLE_GET_EVENT, enum ble_event_type epic_ble_get_event(void));
*/
API(API_BLE_GET_COMPARE_VALUE, uint32_t epic_ble_get_compare_value(void));
/**
* Retrieve the (file) name of the last pairing which was successful.
*
* :return: `0` on success or a negative value if an error occured. Possible
* errors:
*
* - ``-ENOENT``: There was no successful pairing yet.
*
* .. versionadded:: 1.16
*/
API(API_BLE_GET_LAST_PAIRING_NAME, int epic_ble_get_last_pairing_name(char *buf, size_t buf_size));
/**
* Indicate wether the user confirmed the compare value.
*
......
......@@ -21,6 +21,7 @@
#include "wsf_os.h"
#include "dm_api.h"
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
......@@ -417,6 +418,19 @@ void AppDbNvmStoreCccTbl(appDbHdl_t hdl);
/*************************************************************************************************/
void AppDbNvmStoreBond(appDbHdl_t hdl);
/*************************************************************************************************/
/*!
* \brief Get a human readable (file) name of a pairing.
*
* \param hdl Database record handle.
* \param buf Buffer to put the name into.
* \param buf_size Size of the buffer in bytes.
*
* \return Length of name. -1 if the buffer is too small.
*/
/*************************************************************************************************/
int AppDbGetPairingName(appDbHdl_t hdl, char *buf, size_t buf_size);
/**@}*/
/*! \} */ /*! APP_FRAMEWORK_DB_API */
......
......@@ -137,9 +137,12 @@ while True:
state = 6
elif ble_event == sys_ble.EVENT_PAIRING_COMPLETE:
ble_event = None
pairing_name = sys_ble.get_last_pairing_name().split("/")[-1].split(".")[0]
disp.clear()
disp.print("BLE Pairing", posy=0, fg=[0, 0, 255])
disp.print(" Success", posy=40, fg=[0, 255, 0])
disp.print(" Success", posy=20, fg=[0, 255, 0])
disp.print("Name:", posy=40, fg=[255, 255, 255])
disp.print("%11s" % pairing_name, posy=60, fg=[255, 255, 255])
disp.update()
time.sleep(5)
os.exec("main.py")
......
......@@ -199,6 +199,7 @@ Q(get_string)
Q(BLE)
Q(ble)
Q(get_compare_value)
Q(get_last_pairing_name)
Q(get_scan_report)
Q(confirm_compare_value)
Q(set_bondable)
......
......@@ -5,6 +5,7 @@
#include "py/runtime.h"
#include <stdint.h>
#include <string.h>
static mp_obj_t mp_ble_confirm_compare_value(mp_obj_t confirmed_obj)
{
......@@ -24,6 +25,23 @@ static MP_DEFINE_CONST_FUN_OBJ_0(
ble_get_compare_value_obj, mp_ble_get_compare_value
);
static mp_obj_t mp_ble_get_last_pairing_name(void)
{
char pairing_str[32];
int status = epic_ble_get_last_pairing_name(
pairing_str, sizeof(pairing_str)
);
if (status < 0) {
mp_raise_OSError(-status);
}
mp_obj_t ret = mp_obj_new_str(pairing_str, strlen(pairing_str));
return ret;
}
static MP_DEFINE_CONST_FUN_OBJ_0(
ble_get_last_pairing_name_obj, mp_ble_get_last_pairing_name
);
static mp_obj_t mp_ble_get_scan_report(void)
{
struct epic_scan_report scan_report;
......@@ -74,6 +92,8 @@ static const mp_rom_map_elem_t ble_module_globals_table[] = {
MP_ROM_PTR(&ble_confirm_compare_value_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_compare_value),
MP_ROM_PTR(&ble_get_compare_value_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_last_pairing_name),
MP_ROM_PTR(&ble_get_last_pairing_name_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_scan_report),
MP_ROM_PTR(&ble_get_scan_report_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_event), MP_ROM_PTR(&ble_get_event_obj) },
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment