Skip to content

Commit

Permalink
os/board/rtl8730e: Add BLE client numeric comparison callback and aut…
Browse files Browse the repository at this point in the history
…o send pairing request

- added client numeric comparison callback ble_device_passkey_display_cb to app layer
- added start pairing API  ble_manager_start_bond to app layer
- added flag auto_sec_req to automatically send pairing request  when connected
- removed is_secured condition checking, which is previously used to check if pairing should take place after connection.
  • Loading branch information
yeetee179 committed Feb 19, 2025
1 parent 048fb0c commit dd65155
Show file tree
Hide file tree
Showing 20 changed files with 247 additions and 74 deletions.
23 changes: 20 additions & 3 deletions apps/examples/ble_rmc/ble_rmc_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ static void ble_operation_notification_cb(ble_client_ctx *ctx, ble_attr_handle a
return;
}

static void ble_device_passkey_display_cb(ble_client_ctx *ctx, uint32_t passkey, ble_conn_handle conn_handle)
{
printf("[######## %s : %d]passkey %ld, conn_handle %d\n", __FUNCTION__, __LINE__, passkey, conn_handle);
return;
}

void restart_server(void) {
ble_result_e ret = BLE_MANAGER_FAIL;
ble_data data[1] = { 0, };
Expand Down Expand Up @@ -282,6 +288,8 @@ static ble_client_callback_list client_config = {
ble_device_disconnected_cb,
ble_device_connected_cb,
ble_operation_notification_cb,
NULL,
ble_device_passkey_display_cb
};

static ble_server_init_config server_config = {
Expand Down Expand Up @@ -540,6 +548,13 @@ int ble_rmc_main(int argc, char *argv[])
RMC_LOG(RMC_CLIENT_TAG, "fail to delete bond dev[%d]\n", ret);
}
}
if (argc == 4) {
if (strncmp(argv[2], "start", 6) == 0) {
int conn_handle = atoi(argv[3]);
printf("[######## %s : %d]conn_handle %d\n", __FUNCTION__, __LINE__, conn_handle);
ret = ble_manager_start_bond(&conn_handle);
}
}
RMC_LOG(RMC_CLIENT_TAG, "bond command done.\n");
}

Expand Down Expand Up @@ -746,7 +761,7 @@ int ble_rmc_main(int argc, char *argv[])

if (strncmp(argv[1], "connect", 8) == 0) {
ble_client_ctx *ctx = NULL;

/*
1. scan
2. delete bond
Expand Down Expand Up @@ -931,8 +946,10 @@ int ble_rmc_main(int argc, char *argv[])
sec_param.bond_flag = atoi(argv[4]);
sec_param.mitm_flag = atoi(argv[5]);
sec_param.sec_pair_flag = atoi(argv[6]);
sec_param.use_fixed_key = atoi(argv[7]);
sec_param.fixed_key = atoi(argv[8]);
sec_param.sec_pair_only_flag = atoi(argv[7]);
sec_param.use_fixed_key = atoi(argv[8]);
sec_param.fixed_key = atoi(argv[9]);
sec_param.auto_sec_req = atoi(argv[10]);
ret = ble_manager_set_secure_param(&sec_param);
if (ret != BLE_MANAGER_SUCCESS) {
RMC_LOG(RMC_SERVER_TAG, "set secure param fail: [%d]\n", ret);
Expand Down
2 changes: 2 additions & 0 deletions framework/include/ble_manager/ble_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ typedef void(*ble_client_device_disconnected_cb)(ble_client_ctx *ctx);
typedef void(*ble_client_device_connected_cb)(ble_client_ctx *ctx, ble_device_connected* connected_device);
typedef void(*ble_client_operation_notification_cb)(ble_client_ctx *ctx, ble_attr_handle attr_handle, ble_data* read_result);
typedef void (*ble_client_operation_indication_cb)(ble_client_ctx *ctx, ble_attr_handle attr_handle, ble_data* read_result);
typedef void (*ble_client_passkey_display_cb)(ble_client_ctx *ctx, uint32_t passkey_display_cb, ble_conn_handle conn_handle);

typedef struct {
/* This is a set of callback function for BLE client */
ble_client_device_disconnected_cb disconnected_cb;
ble_client_device_connected_cb connected_cb;
ble_client_operation_notification_cb notification_cb;
ble_client_operation_indication_cb indication_cb;
ble_client_passkey_display_cb passkey_display_cb;
} ble_client_callback_list;

ble_client_ctx *ble_client_create_ctx(ble_client_callback_list *callbacks);
Expand Down
16 changes: 9 additions & 7 deletions framework/include/ble_manager/ble_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ typedef struct {
} ble_conn_param;

typedef struct {
uint8_t io_cap; /*!< IO capabilities */
uint8_t oob_data_flag; /*!< OOB data flag */
uint8_t bond_flag; /*!< Bonding flags */
uint8_t mitm_flag; /*!< MITM flag */
uint8_t sec_pair_flag; /*!< Secure connection pairing support flag */
uint8_t use_fixed_key; /*!< Pairing use fixed passkey */
uint32_t fixed_key; /*!< Fixed passkey value */
uint8_t io_cap; /*!< IO capabilities */
uint8_t oob_data_flag; /*!< OOB data flag */
uint8_t bond_flag; /*!< Bonding flags */
uint8_t mitm_flag; /*!< MITM flag */
uint8_t sec_pair_flag; /*!< Secure connection pairing support flag */
uint8_t sec_pair_only_flag; /*!< Only accept secure connection pairing when local sec_pair_flag is set */
uint8_t use_fixed_key; /*!< Pairing use fixed passkey */
uint32_t fixed_key; /*!< Fixed passkey value */
uint8_t auto_sec_req; /*!< Auto send security request when connected */
} ble_sec_param;

/**
Expand Down
7 changes: 7 additions & 0 deletions framework/src/ble_manager/ble_manager_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ ble_result_e ble_manager_set_secure_param(ble_sec_param *sec_param )
RETURN_RESULT(res, msg);
}

ble_result_e ble_manager_start_bond(ble_conn_handle *conn_handle)
{
blemgr_msg_s msg = {BLE_CMD_STA_BOND, BLE_MANAGER_FAIL, (void *)conn_handle, NULL};
int res = blemgr_post_message(&msg);
RETURN_RESULT(res, msg);
}

ble_result_e ble_manager_get_bonded_device(ble_bonded_device_list *device_list, uint16_t *device_count)
{
blemgr_msg_params param = {2, {(void *)device_list, (void *)device_count}};
Expand Down
2 changes: 2 additions & 0 deletions framework/src/ble_manager/ble_manager_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ typedef enum {
BLE_CMD_INIT,
BLE_CMD_DEINIT,
BLE_CMD_GET_MAC,
BLE_CMD_STA_BOND,
BLE_CMD_PASSKEY_CONFIRM,
BLE_CMD_SEC_PARAM_SET,
BLE_CMD_GET_BONDED_DEV,
Expand Down Expand Up @@ -97,6 +98,7 @@ typedef enum {
// Callback Event
BLE_EVT_CLIENT_CONNECT,
BLE_EVT_CLIENT_DISCONNECT,
BLE_EVT_CLIENT_DISPLAY_PASSKEY,
BLE_EVT_CLIENT_NOTI,
BLE_EVT_CLIENT_INDI,
BLE_EVT_SCAN_STATE,
Expand Down
10 changes: 10 additions & 0 deletions framework/src/ble_manager/ble_manager_lwnl.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ trble_result_e ble_drv_passkey_confirm(uint8_t *conn_handle, uint8_t *confirm)
return res;
}

trble_result_e ble_drv_start_bond(trble_conn_handle *conn_handle)
{
trble_result_e res = TRBLE_SUCCESS;
lwnl_msg msg = {BLE_INTF_NAME, {LWNL_REQ_BLE_START_BOND}, sizeof(trble_conn_handle), (void *)(conn_handle), (void *)&res};
if (_send_msg(&msg) < 0) {
res = TRBLE_FILE_ERROR;
}
return res;
}

trble_result_e ble_drv_get_bonded_device(trble_bonded_device_list_s *device_list, uint16_t *device_count)
{
trble_result_e res = TRBLE_SUCCESS;
Expand Down
3 changes: 3 additions & 0 deletions framework/src/ble_manager/ble_manager_lwnl_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ static void _lwnl_call_event(int fd, lwnl_cb_status status, int len)
case LWNL_EVT_BLE_CLIENT_DISCONNECT:
LWNL_SET_MSG(&g_msg, BLE_EVT_CLIENT_DISCONNECT, BLE_MANAGER_FAIL, param, NULL);
break;
case LWNL_EVT_BLE_CLIENT_DISPLAY_PASSKEY:
LWNL_SET_MSG(&g_msg, BLE_EVT_CLIENT_DISPLAY_PASSKEY, BLE_MANAGER_FAIL, param, NULL);
break;
case LWNL_EVT_BLE_CLIENT_NOTI:
LWNL_SET_MSG(&g_msg, BLE_EVT_CLIENT_NOTI, BLE_MANAGER_FAIL, param, NULL);
break;
Expand Down
32 changes: 32 additions & 0 deletions framework/src/ble_manager/ble_manager_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ static void _event_caller(int evt_pri, void *data) {
ble_client_device_disconnected_cb callback = msg->param[0];
callback(msg->param[1]);
} break;
case BLE_EVT_CLIENT_DISPLAY_PASSKEY: {
ble_client_passkey_display_cb callback = msg->param[0];
trble_conn_handle conn = *(trble_conn_handle *)(msg->param[2]);
uint32_t passkey = *(uint32_t *)(msg->param[2] + sizeof(trble_conn_handle));
callback(msg->param[1], passkey, conn);
} break;
case BLE_EVT_CLIENT_NOTI: {
ble_client_operation_notification_cb callback = msg->param[0];
ble_attr_handle attr_handle = *(ble_attr_handle *)(msg->param[2] + sizeof(ble_conn_handle));
Expand Down Expand Up @@ -270,6 +276,12 @@ ble_result_e blemgr_handle_request(blemgr_msg_s *msg)
ret = ble_drv_set_sec_param(sec_param);
} break;

case BLE_CMD_STA_BOND: {
BLE_STATE_CHECK;
trble_conn_handle *con_handle = (blemgr_msg_params *)msg->param;
ret = ble_drv_start_bond(con_handle);
} break;

case BLE_CMD_GET_BONDED_DEV: {
BLE_STATE_CHECK;

Expand Down Expand Up @@ -1223,6 +1235,26 @@ ble_result_e blemgr_handle_request(blemgr_msg_s *msg)
}
} break;

case BLE_EVT_CLIENT_DISPLAY_PASSKEY: {
if (msg->param == NULL) {
break;
}
int i;
ble_client_ctx_internal *ctx = NULL;
ble_conn_handle conn_handle = *(ble_conn_handle *)msg->param;

for (i = 0; i < BLE_MAX_CONNECTION_COUNT; i++) {
if (g_client_table[i].conn_handle == conn_handle) {
ctx = &g_client_table[i];
break;
}
}
if (ctx && ctx->callbacks.passkey_display_cb) {
memcpy(queue_msg.param, (void*[]){ctx->callbacks.passkey_display_cb, ctx, msg->param}, sizeof(void*) * queue_msg.count);
ble_queue_enque(BLE_QUEUE_EVT_PRI_HIGH, &queue_msg);
}
} break;

case BLE_EVT_CLIENT_NOTI: {
if (msg->param == NULL) {
break;
Expand Down
11 changes: 11 additions & 0 deletions os/board/rtl8730e/src/component/bluetooth/api/rtk_bt_le_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,17 @@ uint16_t rtk_bt_le_gap_privacy_init(bool whitelist)
}
#endif /* RTK_BLE_PRIVACY_SUPPORT */

uint16_t rtk_bt_le_sm_set_pairing_mode(rtk_bt_le_pairing_mode_t pairing_mode)
{
uint16_t ret = 0;
if (pairing_mode > RTK_PAIRING_MODE_PAIRABLE) {
return RTK_BT_ERR_PARAM_INVALID;
}
ret = rtk_bt_send_cmd(RTK_BT_LE_GP_GAP, RTK_BT_LE_GAP_ACT_SET_PAIRING_MODE,
&pairing_mode, sizeof(pairing_mode));
return ret;
}

uint16_t rtk_bt_le_sm_set_security_param(rtk_bt_le_security_param_t *p_sec_param)
{
uint16_t ret = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3919,42 +3919,56 @@ static uint16_t bt_stack_le_privacy_init(void *param)
}
#endif

static uint16_t bt_stack_le_sm_set_security_param(void *param)
static uint16_t bt_stack_le_sm_set_pairing_mode(void *param)
{
T_GAP_CAUSE cause;
rtk_bt_le_security_param_t *p_sec_param = (rtk_bt_le_security_param_t *)param;
uint16_t auth_flags = 0;
uint8_t auth_pair_mode = 1;
uint16_t auth_sec_req_flags = 0;
rtk_bt_le_pairing_mode_t pairing_mode = *(rtk_bt_le_pairing_mode_t *)param;

cause = gap_set_param(GAP_PARAM_BOND_PAIRING_MODE, sizeof(uint8_t), &pairing_mode);
if (cause) {
return RTK_BT_ERR_LOWER_STACK_API;
}

cause = gap_set_param(GAP_PARAM_BOND_PAIRING_MODE, sizeof(uint8_t),
&auth_pair_mode);
cause = gap_set_pairable_mode();
if (cause) {
return RTK_BT_ERR_LOWER_STACK_API;
}
return 0;
}

static uint16_t bt_stack_le_sm_set_security_param(void *param)
{
T_GAP_CAUSE cause;
rtk_bt_le_security_param_t *p_sec_param = (rtk_bt_le_security_param_t *)param;
uint16_t auth_flags = 0;
uint16_t auth_sec_req_flags = 0;
cause = gap_set_param(GAP_PARAM_BOND_IO_CAPABILITIES, sizeof(uint8_t),
&p_sec_param->io_cap);
if (cause) {
return RTK_BT_ERR_LOWER_STACK_API;
}

#if defined(F_BT_LE_SMP_OOB_SUPPORT) &&F_BT_LE_SMP_OOB_SUPPORT
#if defined(F_BT_LE_SMP_OOB_SUPPORT) && F_BT_LE_SMP_OOB_SUPPORT
cause = gap_set_param(GAP_PARAM_BOND_OOB_ENABLED, sizeof(uint8_t),
&p_sec_param->oob_data_flag);
if (cause) {
return RTK_BT_ERR_LOWER_STACK_API;
}
#endif

auth_flags = p_sec_param->bond_flag | p_sec_param->mitm_flag << 2 |
p_sec_param->sec_pair_flag << 3;
auth_flags = (!!p_sec_param->bond_flag) | (!!p_sec_param->mitm_flag) << 2 |
(!!p_sec_param->sec_pair_flag) << 3 | (!!p_sec_param->sec_pair_only_flag) << 9;
cause = gap_set_param(GAP_PARAM_BOND_AUTHEN_REQUIREMENTS_FLAGS, sizeof(uint16_t),
&auth_flags);
if (cause) {
return RTK_BT_ERR_LOWER_STACK_API;
}

cause = le_bond_set_param(GAP_PARAM_BOND_SEC_REQ_ENABLE, sizeof(uint8_t),
&p_sec_param->auto_sec_req);
if (cause) {
return RTK_BT_ERR_LOWER_STACK_API;
}
auth_sec_req_flags = auth_flags;
cause = le_bond_set_param(GAP_PARAM_BOND_SEC_REQ_REQUIREMENT, sizeof(uint16_t),
&auth_sec_req_flags);
Expand Down Expand Up @@ -4546,6 +4560,10 @@ uint16_t bt_stack_le_gap_act_handle(rtk_bt_cmd_t *p_cmd)
break;
#endif

case RTK_BT_LE_GAP_ACT_SET_PAIRING_MODE:
ret = bt_stack_le_sm_set_pairing_mode(p_cmd->param);
break;

case RTK_BT_LE_GAP_ACT_SET_SEC_PARAM:
ret = bt_stack_le_sm_set_security_param(p_cmd->param);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ trble_result_e rtw_ble_client_connect(trble_conn_info* conn_info, bool is_secure
return TRBLE_SUCCESS;
}

trble_result_e rtw_ble_client_bond(trble_conn_handle conn_handle)
{
uint16_t ret = rtk_bt_le_sm_start_security(conn_handle);
return ret;
}

trble_result_e rtw_ble_client_conn_param_update(trble_conn_handle *conn_handle, trble_conn_param *conn_param)
{
rtk_bt_le_update_conn_param_t param;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,21 @@ trble_result_e rtw_ble_server_get_mac_address(uint8_t mac[TRBLE_BD_ADDR_MAX_LEN]
return TRBLE_SUCCESS;
}

trble_result_e rtw_ble_sm_set_security_param(trble_sec_param sec_param)
trble_result_e rtw_ble_sm_set_security_param(rtk_bt_le_security_param_t sec_param)
{
rtk_bt_le_security_param_t sec_param_input;
memcpy(&sec_param_input, &sec_param, sizeof(rtk_bt_le_security_param_t));

uint8_t pairing_mode = RTK_PAIRING_MODE_PAIRABLE;
if(RTK_BT_OK != rtk_bt_le_sm_set_pairing_mode(pairing_mode))
{
dbg("secure param set fail \n");
return TRBLE_FAIL;
}

if(RTK_BT_OK != rtk_bt_le_sm_set_security_param(&sec_param_input))
{
debug_print("secure param set fail \n");
dbg("secure param set fail \n");
return TRBLE_FAIL;
}
return TRBLE_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ trble_result_e rtw_ble_combo_init(trble_client_init_config* init_client, trble_s
client_init_parm->trble_device_disconnected_cb = init_client->trble_device_disconnected_cb;
client_init_parm->trble_operation_notification_cb = init_client->trble_operation_notification_cb;
client_init_parm->trble_operation_indication_cb = init_client->trble_operation_indication_cb;
client_init_parm->trble_device_passkey_display_cb = init_client->trble_device_passkey_display_cb;
client_init_parm->mtu = init_client->mtu;

//init server
Expand Down
Loading

0 comments on commit dd65155

Please sign in to comment.