From 6e46ea7530fb517f9d14345735a124b4ca4a92f4 Mon Sep 17 00:00:00 2001 From: yeetee179 Date: Fri, 17 Jan 2025 16:02:37 +0800 Subject: [PATCH] os/board/rtl8730e: added BLE secure parameter setting, passkey display callback and passkey confirmation API - ble_manager_set_secure_param API is added to set BLE secure parameter - ble_server_passkey_display_cb is added inform app layer passkey - ble_manager_passkey_confirm is added to for passkey confirmation --- apps/examples/ble_rmc/ble_rmc_main.c | 42 +++++++++++++++++++ apps/examples/ble_tester/ble_tester_main.c | 1 + framework/include/ble_manager/ble_common.h | 10 +++++ framework/include/ble_manager/ble_server.h | 2 + framework/src/ble_manager/ble_manager_api.c | 14 +++++++ framework/src/ble_manager/ble_manager_event.h | 2 + framework/src/ble_manager/ble_manager_lwnl.c | 21 ++++++++++ framework/src/ble_manager/ble_manager_state.c | 14 +++++++ .../src/component/os/tizenrt/rtk_blemgr.c | 2 + .../api/rtk_stack/rtk_stack_le_gap.c | 12 ++++++ .../ble_peripheral/ble_tizenrt_server.c | 25 +++++++++++ .../ble_scatternet/ble_tizenrt_combo.c | 1 + .../example/ble_scatternet/scatternet.c | 20 +++++---- .../platform/amebad2/src/platform_utils.c | 5 ++- .../src/component/os/tizenrt/rtk_blemgr.c | 22 ++++++++++ .../fwlib/ram_common/ameba_rom_patch.c | 24 +++++++++++ os/include/tinyara/net/if/ble.h | 18 ++++++++ os/net/blemgr/bledev.c | 30 +++++++++++++ os/net/blemgr/bledev_mgr_server.c | 6 +-- 19 files changed, 256 insertions(+), 15 deletions(-) diff --git a/apps/examples/ble_rmc/ble_rmc_main.c b/apps/examples/ble_rmc/ble_rmc_main.c index cee5e8228d..7e41753cb8 100644 --- a/apps/examples/ble_rmc/ble_rmc_main.c +++ b/apps/examples/ble_rmc/ble_rmc_main.c @@ -219,6 +219,12 @@ static void ble_server_mtu_update_cb(ble_conn_handle con_handle, uint16_t mtu_si return; } +static void ble_server_passkey_display_cb(uint32_t passkey, ble_conn_handle conn_handle) +{ + printf("[%s : %d] passkey %ld, con_handle %d\n", __FUNCTION__, __LINE__, passkey, conn_handle); + return; +} + static void utc_cb_charact_a_1(ble_server_attr_cb_type_e type, ble_conn_handle conn_handle, ble_attr_handle attr_handle, void *arg) { char *arg_str = "None"; @@ -282,6 +288,7 @@ static ble_server_init_config server_config = { ble_server_connected_cb, ble_server_disconnected_cb, ble_server_mtu_update_cb, + ble_server_passkey_display_cb, true, gatt_profile, sizeof(gatt_profile) / sizeof(ble_server_gatt_t)}; @@ -898,6 +905,41 @@ int ble_rmc_main(int argc, char *argv[]) } } + if (strncmp(argv[1], "passkeycfm", 11) == 0) { + uint8_t conn_handle = 0; + uint8_t confirm = 0; + if (argc >= 4) { + conn_handle = atoi(argv[2]); + confirm = atoi(argv[3]); + } + ret = ble_manager_passkey_confirm(conn_handle, confirm); + if (ret != BLE_MANAGER_SUCCESS) { + RMC_LOG(RMC_SERVER_TAG, "Passkey confirm fail: [%d]\n", ret); + } else { + RMC_LOG(RMC_SERVER_TAG, "Passkey confirm OK\n"); + } + } + if (strncmp(argv[1], "secureparam", 12) == 0) { + ble_sec_param sec_param; + // RTK_IO_CAP_DISPALY_ONLY = 0x00, /*!< 0x00 DisplayOnly */ + // RTK_IO_CAP_DISPLAY_YES_NO = 0x01, /*!< 0x01 DisplayYesNo */ + // RTK_IO_CAP_KEYBOARD_ONLY = 0x02, /*!< 0x02 KeyboardOnly */ + // RTK_IO_CAP_NO_IN_NO_OUT = 0x03, /*!< 0x03 NoInputNoOutput */ + // RTK_IO_CAP_KEYBOARD_DISPALY = 0x04, /*!< 0x04 KeyboardDisplay */ + sec_param.io_cap = atoi(argv[2]); + sec_param.oob_data_flag = atoi(argv[3]); + 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]); + 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); + } else { + RMC_LOG(RMC_SERVER_TAG, "set secure param OK\n"); + } + } /* Server Test */ if (strncmp(argv[1], "server", 7) == 0) { diff --git a/apps/examples/ble_tester/ble_tester_main.c b/apps/examples/ble_tester/ble_tester_main.c index 94fcebf16f..c01a27b634 100644 --- a/apps/examples/ble_tester/ble_tester_main.c +++ b/apps/examples/ble_tester/ble_tester_main.c @@ -388,6 +388,7 @@ static ble_server_init_config server_config = { ble_server_connected_cb, ble_server_disconnected_cb, ble_server_mtu_update_cb, + NULL, true, gatt_profile, sizeof(gatt_profile) / sizeof(ble_server_gatt_t) diff --git a/framework/include/ble_manager/ble_common.h b/framework/include/ble_manager/ble_common.h index 19ce643fc7..51a8bbf994 100644 --- a/framework/include/ble_manager/ble_common.h +++ b/framework/include/ble_manager/ble_common.h @@ -83,6 +83,16 @@ typedef struct { ble_conn_param_role role; } 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 */ +} ble_sec_param; + /** * @brief Result types of BLE Manager APIs such as FAIL, SUCCESS, or INVALID ARGS */ diff --git a/framework/include/ble_manager/ble_server.h b/framework/include/ble_manager/ble_server.h index 9c61b1f8b6..b0db317049 100644 --- a/framework/include/ble_manager/ble_server.h +++ b/framework/include/ble_manager/ble_server.h @@ -113,11 +113,13 @@ typedef struct { typedef void (*ble_server_connected_t)(ble_conn_handle con_handle, ble_server_connection_type_e conn_type, uint8_t mac[BLE_BD_ADDR_MAX_LEN]); typedef void (*ble_server_disconnected_t)(ble_conn_handle con_handle, uint16_t cause); typedef void (*ble_server_mtu_update_t)(ble_conn_handle con_handle, uint16_t mtu_size); +typedef void (*ble_server_passkey_display_t)(uint32_t passkey, ble_conn_handle conn_handle); typedef struct { ble_server_connected_t connected_cb; ble_server_disconnected_t disconnected_cb; ble_server_mtu_update_t mtu_update_cb; + ble_server_passkey_display_t passkey_display_cb; // true : Secure Manager is enabled. Bondable. // false : Secure Manager is disabled. Requesting Pairing will be rejected. Non-Bondable. bool is_secured_connect_allowed; diff --git a/framework/src/ble_manager/ble_manager_api.c b/framework/src/ble_manager/ble_manager_api.c index 89c0f648f2..646eda4456 100644 --- a/framework/src/ble_manager/ble_manager_api.c +++ b/framework/src/ble_manager/ble_manager_api.c @@ -56,6 +56,20 @@ ble_result_e ble_manager_get_mac_addr(uint8_t mac[BLE_BD_ADDR_MAX_LEN]) RETURN_RESULT(res, msg); } +ble_result_e ble_manager_passkey_confirm(uint8_t conn_handle, uint8_t confirm) +{ + blemgr_msg_params param = {3, {(void *)&conn_handle, (void *)&confirm}}; + blemgr_msg_s msg = {BLE_CMD_PASSKEY_CONFIRM, BLE_MANAGER_FAIL, (void *)(¶m), NULL}; + int res = blemgr_post_message(&msg); + RETURN_RESULT(res, msg); +} +ble_result_e ble_manager_set_secure_param(ble_sec_param *sec_param ) +{ + blemgr_msg_s msg = {BLE_CMD_SEC_PARAM_SET, BLE_MANAGER_FAIL, (void *)(sec_param), 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}}; diff --git a/framework/src/ble_manager/ble_manager_event.h b/framework/src/ble_manager/ble_manager_event.h index 3943cca5f1..1e678da138 100644 --- a/framework/src/ble_manager/ble_manager_event.h +++ b/framework/src/ble_manager/ble_manager_event.h @@ -23,6 +23,8 @@ typedef enum { BLE_CMD_INIT, BLE_CMD_DEINIT, BLE_CMD_GET_MAC, + BLE_CMD_PASSKEY_CONFIRM, + BLE_CMD_SEC_PARAM_SET, BLE_CMD_GET_BONDED_DEV, BLE_CMD_DEL_BOND, BLE_CMD_DEL_BOND_ALL, diff --git a/framework/src/ble_manager/ble_manager_lwnl.c b/framework/src/ble_manager/ble_manager_lwnl.c index 1db5fdc610..841f6e8af2 100644 --- a/framework/src/ble_manager/ble_manager_lwnl.c +++ b/framework/src/ble_manager/ble_manager_lwnl.c @@ -84,6 +84,17 @@ trble_result_e ble_drv_get_mac_addr(uint8_t mac[TRBLE_BD_ADDR_MAX_LEN]) return res; } +trble_result_e ble_drv_passkey_confirm(uint8_t *conn_handle, uint8_t *confirm) +{ + trble_result_e res = TRBLE_SUCCESS; + lwnl_msg_params msg_data = { 2, {(void *)conn_handle, (void *)confirm} }; + lwnl_msg msg = {BLE_INTF_NAME, {LWNL_REQ_BLE_PASSKEY_CONFIRM}, sizeof(msg_data), (void *)&msg_data, (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; @@ -95,6 +106,16 @@ trble_result_e ble_drv_get_bonded_device(trble_bonded_device_list_s *device_list return res; } +trble_result_e ble_drv_set_sec_param(trble_sec_param *sec_param) +{ + trble_result_e res = TRBLE_SUCCESS; + lwnl_msg msg = {BLE_INTF_NAME, {LWNL_REQ_BLE_SEC_PARAM_SET}, sizeof(trble_sec_param), (void *)sec_param, (void *)&res}; + if (_send_msg(&msg) < 0) { + res = TRBLE_FILE_ERROR; + } + return res; +} + trble_result_e ble_drv_delete_bonded(trble_addr *addr) { trble_result_e res = TRBLE_SUCCESS; diff --git a/framework/src/ble_manager/ble_manager_state.c b/framework/src/ble_manager/ble_manager_state.c index 5a31507d60..21816021da 100644 --- a/framework/src/ble_manager/ble_manager_state.c +++ b/framework/src/ble_manager/ble_manager_state.c @@ -256,6 +256,20 @@ ble_result_e blemgr_handle_request(blemgr_msg_s *msg) ret = ble_drv_get_mac_addr(mac); } break; + case BLE_CMD_PASSKEY_CONFIRM: { + BLE_STATE_CHECK; + blemgr_msg_params *param = (blemgr_msg_params *)msg->param; + uint8_t *conn_handle = (uint8_t *)param->param[0]; + uint8_t *confirm = (uint8_t *)param->param[1]; + ret = ble_drv_passkey_confirm(conn_handle, confirm); + } break; + + case BLE_CMD_SEC_PARAM_SET: { + BLE_STATE_CHECK; + trble_sec_param *sec_param = (trble_sec_param *)msg->param; + ret = ble_drv_set_sec_param(sec_param); + } break; + case BLE_CMD_GET_BONDED_DEV: { BLE_STATE_CHECK; diff --git a/os/board/rtl8721csm/src/component/os/tizenrt/rtk_blemgr.c b/os/board/rtl8721csm/src/component/os/tizenrt/rtk_blemgr.c index c2eb944c5a..02fa922c99 100644 --- a/os/board/rtl8721csm/src/component/os/tizenrt/rtk_blemgr.c +++ b/os/board/rtl8721csm/src/component/os/tizenrt/rtk_blemgr.c @@ -114,6 +114,8 @@ struct trble_ops g_trble_drv_ops = { trble_netmgr_init, trble_netmgr_deinit, trble_netmgr_get_mac_addr, + NULL, + NULL, trble_netmgr_get_bonded_device, trble_netmgr_delete_bond, trble_netmgr_delete_bond_all, diff --git a/os/board/rtl8730e/src/component/bluetooth/api/rtk_stack/rtk_stack_le_gap.c b/os/board/rtl8730e/src/component/bluetooth/api/rtk_stack/rtk_stack_le_gap.c index f989797943..28d2f3b123 100755 --- a/os/board/rtl8730e/src/component/bluetooth/api/rtk_stack/rtk_stack_le_gap.c +++ b/os/board/rtl8730e/src/component/bluetooth/api/rtk_stack/rtk_stack_le_gap.c @@ -4054,6 +4054,18 @@ static uint16_t bt_stack_le_sm_passkey_confirm(void *param) return RTK_BT_ERR_PARAM_INVALID; } + if (key_cfm->confirm == 1) + { + confirm = GAP_CFM_CAUSE_ACCEPT; + } + else if (key_cfm->confirm == 0) + { + confirm = GAP_CFM_CAUSE_REJECT; + } + else{ + return RTK_BT_ERR_PARAM_INVALID; + } + cause = le_bond_user_confirm(conn_id, confirm); if (cause) { return RTK_BT_ERR_LOWER_STACK_API; diff --git a/os/board/rtl8730e/src/component/bluetooth/example/ble_peripheral/ble_tizenrt_server.c b/os/board/rtl8730e/src/component/bluetooth/example/ble_peripheral/ble_tizenrt_server.c index f5ef89539b..539238eb45 100644 --- a/os/board/rtl8730e/src/component/bluetooth/example/ble_peripheral/ble_tizenrt_server.c +++ b/os/board/rtl8730e/src/component/bluetooth/example/ble_peripheral/ble_tizenrt_server.c @@ -97,6 +97,31 @@ 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) +{ + rtk_bt_le_security_param_t sec_param_input; + memcpy(&sec_param_input, &sec_param, sizeof(rtk_bt_le_security_param_t)); + if(RTK_BT_OK != rtk_bt_le_sm_set_security_param(&sec_param_input)) + { + debug_print("secure param set fail \n"); + return TRBLE_FAIL; + } + return TRBLE_SUCCESS; +} + +trble_result_e rtw_ble_pairing_passkey_confirm(uint8_t *conn_handle, uint8_t *confirm) +{ + rtk_bt_le_auth_key_confirm_t pair_cfm_param = {0}; + pair_cfm_param.conn_handle = *conn_handle; + pair_cfm_param.confirm = *confirm; + if(RTK_BT_OK != rtk_bt_le_sm_passkey_confirm(&pair_cfm_param)) + { + debug_print("passkey confirm fail \n"); + return TRBLE_FAIL; + } + return TRBLE_SUCCESS; +} + /* set data pointer of attribute value */ trble_result_e rtw_ble_server_att_set_data_ptr(trble_attr_handle attr_handle, uint8_t *new_data_ptr) { diff --git a/os/board/rtl8730e/src/component/bluetooth/example/ble_scatternet/ble_tizenrt_combo.c b/os/board/rtl8730e/src/component/bluetooth/example/ble_scatternet/ble_tizenrt_combo.c index 5d98206723..24d74a4fc8 100755 --- a/os/board/rtl8730e/src/component/bluetooth/example/ble_scatternet/ble_tizenrt_combo.c +++ b/os/board/rtl8730e/src/component/bluetooth/example/ble_scatternet/ble_tizenrt_combo.c @@ -93,6 +93,7 @@ trble_result_e rtw_ble_combo_init(trble_client_init_config* init_client, trble_s server_init_parm.connected_cb = init_server->connected_cb; server_init_parm.disconnected_cb = init_server->disconnected_cb; server_init_parm.mtu_update_cb = init_server->mtu_update_cb; + server_init_parm.passkey_display_cb = init_server->passkey_display_cb; server_init_parm.is_secured_connect_allowed = init_server->is_secured_connect_allowed; ble_tizenrt_scatternet_main(1); diff --git a/os/board/rtl8730e/src/component/bluetooth/example/ble_scatternet/scatternet.c b/os/board/rtl8730e/src/component/bluetooth/example/ble_scatternet/scatternet.c index eccf643f7b..02239b824c 100644 --- a/os/board/rtl8730e/src/component/bluetooth/example/ble_scatternet/scatternet.c +++ b/os/board/rtl8730e/src/component/bluetooth/example/ble_scatternet/scatternet.c @@ -129,15 +129,15 @@ static uint8_t scan_rsp_data[] = { .duplicate_opt = 0, }; -//static rtk_bt_le_security_param_t sec_param = { -// .io_cap = RTK_IO_CAP_NO_IN_NO_OUT, -// .oob_data_flag = 0, -// .bond_flag = 1, -// .mitm_flag = 0, -// .sec_pair_flag = 0, -// .use_fixed_key = 0, -// .fixed_key = 000000, -//}; +static rtk_bt_le_security_param_t sec_param = { + .io_cap = RTK_IO_CAP_NO_IN_NO_OUT, + .oob_data_flag = 0, + .bond_flag = 1, + .mitm_flag = 0, + .sec_pair_flag = 0, + .use_fixed_key = 0, + .fixed_key = 000000, +}; #if RTK_BLE_PRIVACY_SUPPORT static bool privacy_enable = false; @@ -519,6 +519,7 @@ static rtk_bt_evt_cb_ret_t ble_tizenrt_scatternet_gap_app_callback(uint8_t evt_c case RTK_BT_LE_GAP_EVT_AUTH_PASSKEY_CONFIRM_IND: { rtk_bt_le_auth_key_cfm_ind_t *key_cfm_ind = (rtk_bt_le_auth_key_cfm_ind_t *)param; + server_init_parm.passkey_display_cb(key_cfm_ind->passkey, key_cfm_ind->conn_handle); APP_PROMOTE("[APP] Auth passkey confirm: %ld, conn_handle: %d. " \ "Please comfirm if the passkeys are equal!\r\n", key_cfm_ind->passkey, @@ -877,6 +878,7 @@ int ble_tizenrt_scatternet_main(uint8_t enable) memcpy(name,(const uint8_t*)RTK_BT_DEV_NAME,strlen((const char *)RTK_BT_DEV_NAME)); BT_APP_PROCESS(rtk_bt_le_gap_set_device_name((uint8_t *)name)); BT_APP_PROCESS(rtk_bt_le_gap_set_appearance(RTK_BT_LE_GAP_APPEARANCE_HEART_RATE_BELT)); + BT_APP_PROCESS(rtk_bt_le_sm_set_security_param(&sec_param)); #if (RTK_BLE_5_0_AE_ADV_SUPPORT==0) BT_APP_PROCESS(rtk_bt_le_gap_set_adv_data(adv_data,sizeof(adv_data))); BT_APP_PROCESS(rtk_bt_le_gap_set_scan_rsp_data(scan_rsp_data,sizeof(scan_rsp_data))); diff --git a/os/board/rtl8730e/src/component/bluetooth/rtk_stack/platform/amebad2/src/platform_utils.c b/os/board/rtl8730e/src/component/bluetooth/rtk_stack/platform/amebad2/src/platform_utils.c index 47d663ed6e..7479f1eac8 100755 --- a/os/board/rtl8730e/src/component/bluetooth/rtk_stack/platform/amebad2/src/platform_utils.c +++ b/os/board/rtl8730e/src/component/bluetooth/rtk_stack/platform/amebad2/src/platform_utils.c @@ -2,7 +2,10 @@ #include #include +extern int TRNG_get_random_bytes(void *dst, uint32_t size); uint32_t platform_random(uint32_t max) { - return rand() % max; + uint32_t val; + TRNG_get_random_bytes(&val, sizeof(uint32_t)); + return (val % max); } diff --git a/os/board/rtl8730e/src/component/os/tizenrt/rtk_blemgr.c b/os/board/rtl8730e/src/component/os/tizenrt/rtk_blemgr.c index 75f993eb09..fd86148a62 100644 --- a/os/board/rtl8730e/src/component/os/tizenrt/rtk_blemgr.c +++ b/os/board/rtl8730e/src/component/os/tizenrt/rtk_blemgr.c @@ -66,7 +66,9 @@ static bool _check_mac_empty(uint8_t mac[TRBLE_BD_ADDR_MAX_LEN]) trble_result_e trble_netmgr_init(struct bledev *dev, trble_client_init_config *client, trble_server_init_config *server); trble_result_e trble_netmgr_deinit(struct bledev *dev); trble_result_e trble_netmgr_get_mac_addr(struct bledev *dev, uint8_t mac[TRBLE_BD_ADDR_MAX_LEN]); +trble_result_e trble_netmgr_set_sec_param(struct bledev *dev, trble_sec_param *sec_param); trble_result_e trble_netmgr_get_bonded_device(struct bledev *dev, trble_bonded_device_list_s *device_list, uint16_t *device_count); +trble_result_e trble_netmgr_passkey_confirm(struct bledev *dev, uint8_t *conn_handle, uint8_t *confirm); trble_result_e trble_netmgr_delete_bond(struct bledev *dev, trble_addr *addr); trble_result_e trble_netmgr_delete_bond_all(struct bledev *dev); trble_result_e trble_netmgr_conn_is_active(struct bledev *dev, trble_conn_handle con_handle, bool *is_active); @@ -131,6 +133,8 @@ struct trble_ops g_trble_drv_ops = { trble_netmgr_init, trble_netmgr_deinit, trble_netmgr_get_mac_addr, + trble_netmgr_set_sec_param, + trble_netmgr_passkey_confirm, trble_netmgr_get_bonded_device, trble_netmgr_delete_bond, trble_netmgr_delete_bond_all, @@ -270,6 +274,24 @@ trble_result_e trble_netmgr_get_bonded_device(struct bledev *dev, trble_bonded_d return ret; } +trble_result_e trble_netmgr_set_sec_param(struct bledev *dev, trble_sec_param *sec_param) +{ + trble_sec_param sec_param_input; + sec_param_input.io_cap = sec_param->io_cap; + sec_param_input.oob_data_flag = sec_param->oob_data_flag; + sec_param_input.bond_flag = sec_param->bond_flag; + sec_param_input.mitm_flag = sec_param->mitm_flag; + sec_param_input.sec_pair_flag = sec_param->sec_pair_flag; + sec_param_input.use_fixed_key = sec_param->use_fixed_key; + sec_param_input.fixed_key = sec_param->fixed_key; + return rtw_ble_sm_set_security_param(sec_param_input); +} + +trble_result_e trble_netmgr_passkey_confirm(struct bledev *dev, uint8_t *conn_handle, uint8_t *confirm) +{ + return rtw_ble_pairing_passkey_confirm(conn_handle, confirm); +} + trble_result_e trble_netmgr_delete_bond(struct bledev *dev, trble_addr *addr) { trble_addr t_addr[1] = { 0, }; diff --git a/os/board/rtl8730e/src/component/soc/amebad2/fwlib/ram_common/ameba_rom_patch.c b/os/board/rtl8730e/src/component/soc/amebad2/fwlib/ram_common/ameba_rom_patch.c index 77801f9611..1562a3958e 100644 --- a/os/board/rtl8730e/src/component/soc/amebad2/fwlib/ram_common/ameba_rom_patch.c +++ b/os/board/rtl8730e/src/component/soc/amebad2/fwlib/ram_common/ameba_rom_patch.c @@ -1288,4 +1288,28 @@ int SBOOT_Validate_Algorithm(u8 *AuthAlg, u8 *HashAlg, u8 ManiAuth, u8 ManiHash) } } #endif + +int TRNG_get_random_bytes(void *dst, u32 size) +{ + unsigned int ranbuf; + unsigned int *lp; + int i, count; + count = size / sizeof(unsigned int); + lp = (unsigned int *) dst; + for (i = 0; i < count; i ++) { + lp[i] = _rand(); + size -= sizeof(unsigned int); + } + if (size > 0) { + ranbuf = _rand(); + _memcpy(&lp[i], &ranbuf, size); + } + return 0; +} + +int TRNG_get_random_bytes_f_rng(void *p_rng, unsigned char *output, size_t output_size) +{ + UNUSED(p_rng); + return TRNG_get_random_bytes(output, output_size); +} /******************* (C) COPYRIGHT 2022 Realtek Semiconductor *****END OF FILE****/ diff --git a/os/include/tinyara/net/if/ble.h b/os/include/tinyara/net/if/ble.h index 95a1a2c612..f99dfc501c 100644 --- a/os/include/tinyara/net/if/ble.h +++ b/os/include/tinyara/net/if/ble.h @@ -68,13 +68,25 @@ typedef struct { trble_conn_param_role role; } trble_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 */ +} trble_sec_param; + typedef enum { // Common LWNL_REQ_BLE_INIT, LWNL_REQ_BLE_DEINIT, LWNL_REQ_BLE_GET_MAC, + LWNL_REQ_BLE_SEC_PARAM_SET, LWNL_REQ_BLE_GET_BONDED_DEV, LWNL_REQ_BLE_DEL_BOND, + LWNL_REQ_BLE_PASSKEY_CONFIRM, LWNL_REQ_BLE_DEL_BOND_ALL, LWNL_REQ_BLE_CONN_IS_ACTIVE, LWNL_REQ_BLE_CONN_IS_ANY_ACTIVE, @@ -319,11 +331,13 @@ typedef void (*trble_server_connected_t)(trble_conn_handle con_handle, trble_ser typedef void (*trble_server_disconnected_t)(trble_conn_handle con_handle, uint16_t cause); typedef void (*trble_server_mtu_update_t)(trble_conn_handle con_handle, uint16_t mtu_size); typedef void (*trble_server_oneshot_adv_t)(uint16_t adv_ret); +typedef void (*trble_server_passkey_display_t)(uint32_t passkey, trble_conn_handle con_handle); typedef struct { trble_server_connected_t connected_cb; trble_server_disconnected_t disconnected_cb; trble_server_mtu_update_t mtu_update_cb; + trble_server_passkey_display_t passkey_display_cb; // true : Secure Manager is enabled. Bondable. // false : Secure Manager is disabled. Requesting Pairing will be rejected. Non-Bondable. bool is_secured_connect_allowed; @@ -344,6 +358,8 @@ typedef trble_result_e (*trble_init)(struct bledev *dev, trble_client_init_confi typedef trble_result_e (*trble_deinit)(struct bledev *dev); typedef trble_result_e (*trble_get_mac_addr)(struct bledev *dev, uint8_t mac[TRBLE_BD_ADDR_MAX_LEN]); // trble_disconnect can be used in both of server & client. +typedef trble_result_e (*trble_set_sec_param)(struct bledev *dev, trble_sec_param *sec_param); +typedef trble_result_e (*trble_passkey_confirm)(struct bledev *dev, uint8_t *conn_handle, uint8_t *confirm); typedef trble_result_e (*trble_get_bonded_device)(struct bledev *dev, trble_bonded_device_list_s *device_list, uint16_t *device_count); typedef trble_result_e (*trble_delete_bond)(struct bledev *dev, trble_addr *addr); typedef trble_result_e (*trble_delete_bond_all)(struct bledev *dev); @@ -414,6 +430,8 @@ struct trble_ops { trble_init init; trble_deinit deinit; trble_get_mac_addr get_mac; + trble_set_sec_param set_sec_param; + trble_passkey_confirm passkey_confirm; trble_get_bonded_device get_bonded_dev; trble_delete_bond del_bond; trble_delete_bond_all del_bond_all; diff --git a/os/net/blemgr/bledev.c b/os/net/blemgr/bledev.c index 164dabeb62..b5ff5d755d 100644 --- a/os/net/blemgr/bledev.c +++ b/os/net/blemgr/bledev.c @@ -120,6 +120,36 @@ int bledev_handle(struct bledev *dev, lwnl_req cmd, void *data, uint32_t data_le } } break; + case LWNL_REQ_BLE_SEC_PARAM_SET: + { + trble_sec_param *sec_param = (trble_sec_param *)data; + if (data != NULL) { + memcpy(sec_param, data, data_len); + } else { + return TRBLE_INVALID_ARGS; + } + if (sec_param != NULL) { + TRBLE_DRV_CALL(ret, dev, set_sec_param, (dev, sec_param)); + } else { + ret = TRBLE_INVALID_ARGS; + } + } + break; + case LWNL_REQ_BLE_PASSKEY_CONFIRM: + { + uint8_t *conn_handle = 0; + uint8_t *confirm = 0; + lwnl_msg_params param = { 0, }; + if (data != NULL) { + memcpy(¶m, data, data_len); + } else { + return TRBLE_INVALID_ARGS; + } + conn_handle = (uint8_t *)param.param[0]; + confirm = (uint8_t *)param.param[1]; + TRBLE_DRV_CALL(ret, dev, passkey_confirm, (dev, conn_handle, confirm)); + } + break; case LWNL_REQ_BLE_GET_BONDED_DEV: { trble_bonded_device_list_s *device_list = NULL; diff --git a/os/net/blemgr/bledev_mgr_server.c b/os/net/blemgr/bledev_mgr_server.c index d0b927a80c..3ee136a338 100644 --- a/os/net/blemgr/bledev_mgr_server.c +++ b/os/net/blemgr/bledev_mgr_server.c @@ -38,11 +38,6 @@ static void ble_server_mtu_update_null_cb(trble_conn_handle con_handle, uint16_t return; } -static void ble_server_oneshot_adv_cb(uint16_t adv_result) -{ - return; -} - static void ble_server_null_cb(trble_attr_cb_type_e type, trble_conn_handle linkindex, trble_attr_handle handle, void *arg, uint16_t result, uint16_t pending) { return; @@ -83,6 +78,7 @@ static trble_server_init_config g_server_null_config = { ble_server_connected_null_cb, ble_server_disconnected_null_cb, ble_server_mtu_update_null_cb, + NULL, true, gatt_null_profile, sizeof(gatt_null_profile) / sizeof(trble_gatt_t)