471 lines
14 KiB
C
471 lines
14 KiB
C
/**
|
|
****************************************************************************************
|
|
* @file xc_gap_api.c
|
|
* @brief GAP APIs source code
|
|
* Copyright (c) 2022 - 2025, XinChip
|
|
* All rights reserved.
|
|
****************************************************************************************
|
|
*/
|
|
#include "rwip_config.h"
|
|
#if BLE_APP_PRESENT
|
|
#include "xc_gap_api.h"
|
|
|
|
void xc_ble_gapm_reset(void)
|
|
{
|
|
// Reset the stack
|
|
struct gapm_reset_cmd *p_cmd =
|
|
KE_MSG_ALLOC(GAPM_RESET_CMD, TASK_GAPM, TASK_APP, gapm_reset_cmd);
|
|
|
|
p_cmd->operation = GAPM_RESET;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_set_dev_config(struct gapm_set_dev_config_cmd *cfg_param)
|
|
{
|
|
struct gapm_set_dev_config_cmd *p_cmd = KE_MSG_ALLOC(
|
|
GAPM_SET_DEV_CONFIG_CMD, TASK_GAPM, TASK_APP, gapm_set_dev_config_cmd);
|
|
|
|
memcpy(p_cmd, cfg_param, sizeof(struct gapm_set_dev_config_cmd));
|
|
p_cmd->operation = GAPM_SET_DEV_CONFIG;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_get_dev_info(struct gapm_get_dev_info_cmd *cmd)
|
|
{
|
|
struct gapm_get_dev_info_cmd *p_cmd = KE_MSG_ALLOC(
|
|
GAPM_GET_DEV_INFO_CMD, TASK_GAPM, TASK_APP, gapm_get_dev_info_cmd);
|
|
|
|
p_cmd->operation = cmd->operation;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_resolve_addr(uint8_t nb_key, gap_addr_t *addr, uint8_t *irk)
|
|
{
|
|
struct gapm_resolv_addr_cmd *p_cmd = KE_MSG_ALLOC_DYN(
|
|
GAPM_RESOLV_ADDR_CMD, TASK_GAPM, TASK_APP, gapm_resolv_addr_cmd,
|
|
(nb_key * sizeof(struct gap_sec_key)));
|
|
|
|
memcpy(&p_cmd->addr.addr[0], &addr->addr[0], GAP_BD_ADDR_LEN);
|
|
memcpy(&p_cmd->irk[0].key[0], &irk[0], GAP_KEY_LEN * nb_key);
|
|
p_cmd->nb_key = nb_key;
|
|
|
|
p_cmd->operation = GAPM_RESOLV_ADDR;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_gen_random_addr(struct gapm_gen_rand_addr_cmd *p_param)
|
|
{
|
|
struct gapm_gen_rand_addr_cmd *p_cmd = KE_MSG_ALLOC(
|
|
GAPM_GEN_RAND_ADDR_CMD, TASK_GAPM, TASK_APP, gapm_gen_rand_addr_cmd);
|
|
|
|
memcpy(p_cmd, p_param, sizeof(struct gapm_gen_rand_addr_cmd));
|
|
p_cmd->operation = GAPM_GEN_RAND_ADDR;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_get_pub_key(void)
|
|
{
|
|
struct gapm_get_pub_key_cmd *p_cmd = KE_MSG_ALLOC(
|
|
GAPM_GET_PUB_KEY_CMD, TASK_GAPM, TASK_APP, gapm_get_pub_key_cmd);
|
|
|
|
p_cmd->operation = GAPM_GET_PUB_KEY;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_gen_random_nb(void)
|
|
{
|
|
struct gapm_gen_rand_nb_cmd *p_cmd = KE_MSG_ALLOC(
|
|
GAPM_GEN_RAND_NB_CMD, TASK_GAPM, TASK_APP, gapm_gen_rand_nb_cmd);
|
|
|
|
p_cmd->operation = GAPM_GEN_RAND_NB;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_set_irk(gap_sec_key_t *irk)
|
|
{
|
|
struct gapm_set_irk_cmd *p_cmd =
|
|
KE_MSG_ALLOC(GAPM_SET_IRK_CMD, TASK_GAPM, TASK_APP, gapm_set_irk_cmd);
|
|
|
|
p_cmd->operation = GAPM_SET_IRK;
|
|
memcpy(p_cmd->irk.key, irk->key, GAP_KEY_LEN);
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_get_ral_addr(struct gapm_get_ral_addr_cmd *p_param)
|
|
{
|
|
struct gapm_get_ral_addr_cmd *p_cmd = KE_MSG_ALLOC(
|
|
GAPM_GET_RAL_ADDR_CMD, TASK_GAPM, TASK_APP, gapm_get_ral_addr_cmd);
|
|
|
|
memcpy(p_cmd, p_param, sizeof(struct gapm_get_ral_addr_cmd));
|
|
p_cmd->operation = p_param->operation;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_set_ral_list(uint8_t size, struct gap_ral_dev_info *ral_info)
|
|
{
|
|
struct gapm_list_set_ral_cmd *p_cmd = KE_MSG_ALLOC_DYN(
|
|
GAPM_LIST_SET_CMD, TASK_GAPM, TASK_APP, gapm_list_set_ral_cmd,
|
|
(size * sizeof(struct gap_ral_dev_info)));
|
|
|
|
p_cmd->size = size;
|
|
memcpy(&p_cmd->ral_info, ral_info, sizeof(struct gap_ral_dev_info) * size);
|
|
p_cmd->operation = GAPM_SET_RAL;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_set_white_list(uint8_t size, gap_bdaddr_t *wl_info)
|
|
{
|
|
struct gapm_list_set_wl_cmd *p_cmd = KE_MSG_ALLOC_DYN(
|
|
GAPM_LIST_SET_CMD, TASK_GAPM, TASK_APP, gapm_list_set_wl_cmd,
|
|
(size * sizeof(struct gap_bdaddr)));
|
|
|
|
p_cmd->size = size;
|
|
memcpy(&p_cmd->wl_info, wl_info, sizeof(struct gap_bdaddr) * size);
|
|
p_cmd->operation = GAPM_SET_WL;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_advertise_create(
|
|
struct gapm_activity_create_adv_cmd *adv_creat_param)
|
|
{
|
|
struct gapm_activity_create_adv_cmd *p_cmd =
|
|
KE_MSG_ALLOC(GAPM_ACTIVITY_CREATE_CMD, TASK_GAPM, TASK_APP,
|
|
gapm_activity_create_adv_cmd);
|
|
|
|
memcpy(p_cmd, adv_creat_param, sizeof(struct gapm_activity_create_adv_cmd));
|
|
p_cmd->operation = GAPM_CREATE_ADV_ACTIVITY;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_set_adv_data(uint8_t actv_idx, uint16_t adv_data_length,
|
|
uint8_t *p_adv_data)
|
|
{
|
|
struct gapm_set_adv_data_cmd *p_cmd =
|
|
KE_MSG_ALLOC_DYN(GAPM_SET_ADV_DATA_CMD, TASK_GAPM, TASK_APP,
|
|
gapm_set_adv_data_cmd, adv_data_length);
|
|
|
|
p_cmd->operation = GAPM_SET_ADV_DATA;
|
|
p_cmd->actv_idx = actv_idx;
|
|
p_cmd->length = adv_data_length;
|
|
memcpy(&p_cmd->data[0], p_adv_data, adv_data_length);
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_set_scan_rsp_data(uint8_t actv_idx, uint16_t scan_rsp_data_length,
|
|
uint8_t *p_scan_rsp_data)
|
|
{
|
|
struct gapm_set_adv_data_cmd *p_cmd =
|
|
KE_MSG_ALLOC_DYN(GAPM_SET_ADV_DATA_CMD, TASK_GAPM, TASK_APP,
|
|
gapm_set_adv_data_cmd, scan_rsp_data_length);
|
|
|
|
p_cmd->operation = GAPM_SET_SCAN_RSP_DATA;
|
|
p_cmd->actv_idx = actv_idx;
|
|
p_cmd->length = scan_rsp_data_length;
|
|
memcpy(&p_cmd->data[0], p_scan_rsp_data, scan_rsp_data_length);
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_advertise_start(struct gapm_activity_start_cmd *adv_start_param)
|
|
{
|
|
struct gapm_activity_start_cmd *p_cmd = KE_MSG_ALLOC(
|
|
GAPM_ACTIVITY_START_CMD, TASK_GAPM, TASK_APP, gapm_activity_start_cmd);
|
|
|
|
memcpy(p_cmd, adv_start_param, sizeof(struct gapm_activity_start_cmd));
|
|
p_cmd->operation = GAPM_START_ACTIVITY;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_scan_create(struct gapm_activity_create_cmd *scan_creat_param)
|
|
{
|
|
struct gapm_activity_create_cmd *p_cmd =
|
|
KE_MSG_ALLOC(GAPM_ACTIVITY_CREATE_CMD, TASK_GAPM, TASK_APP,
|
|
gapm_activity_create_cmd);
|
|
|
|
memcpy(p_cmd, scan_creat_param, sizeof(struct gapm_activity_create_cmd));
|
|
p_cmd->operation = GAPM_CREATE_SCAN_ACTIVITY;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_scan_start(struct gapm_activity_start_cmd *scan_start_param)
|
|
{
|
|
struct gapm_activity_start_cmd *p_cmd = KE_MSG_ALLOC(
|
|
GAPM_ACTIVITY_START_CMD, TASK_GAPM, TASK_APP, gapm_activity_start_cmd);
|
|
|
|
memcpy(p_cmd, scan_start_param, sizeof(struct gapm_activity_start_cmd));
|
|
p_cmd->operation = GAPM_START_ACTIVITY;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_init_create(struct gapm_activity_create_cmd *init_creat_param)
|
|
{
|
|
struct gapm_activity_create_cmd *p_cmd =
|
|
KE_MSG_ALLOC(GAPM_ACTIVITY_CREATE_CMD, TASK_GAPM, TASK_APP,
|
|
gapm_activity_create_cmd);
|
|
|
|
memcpy(p_cmd, init_creat_param, sizeof(struct gapm_activity_create_cmd));
|
|
p_cmd->operation = GAPM_CREATE_INIT_ACTIVITY;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_init_start(struct gapm_activity_start_cmd *init_start_param)
|
|
{
|
|
struct gapm_activity_start_cmd *p_cmd = KE_MSG_ALLOC(
|
|
GAPM_ACTIVITY_START_CMD, TASK_GAPM, TASK_APP, gapm_activity_start_cmd);
|
|
|
|
memcpy(p_cmd, init_start_param, sizeof(struct gapm_activity_start_cmd));
|
|
p_cmd->operation = GAPM_START_ACTIVITY;
|
|
p_cmd->u_param.init_param.conn_param_1m.ce_len_min = CE_LEN_MIN;
|
|
p_cmd->u_param.init_param.conn_param_1m.ce_len_max = CE_LEN_MAX;
|
|
p_cmd->u_param.init_param.conn_param_2m.ce_len_min = CE_LEN_MIN;
|
|
p_cmd->u_param.init_param.conn_param_2m.ce_len_max = CE_LEN_MAX;
|
|
p_cmd->u_param.init_param.conn_param_coded.ce_len_min = CE_LEN_MIN;
|
|
p_cmd->u_param.init_param.conn_param_coded.ce_len_max = CE_LEN_MAX;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_activity_stop(int actv_idx)
|
|
{
|
|
struct gapm_activity_stop_cmd *p_cmd = KE_MSG_ALLOC(
|
|
GAPM_ACTIVITY_STOP_CMD, TASK_GAPM, TASK_APP, gapm_activity_stop_cmd);
|
|
|
|
if (actv_idx >= 0) {
|
|
p_cmd->operation = GAPM_STOP_ACTIVITY;
|
|
} else {
|
|
p_cmd->operation = GAPM_STOP_ALL_ACTIVITIES;
|
|
}
|
|
p_cmd->actv_idx = actv_idx;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_activity_delete(int actv_idx)
|
|
{
|
|
struct gapm_activity_delete_cmd *p_cmd =
|
|
KE_MSG_ALLOC(GAPM_ACTIVITY_DELETE_CMD, TASK_GAPM, TASK_APP,
|
|
gapm_activity_delete_cmd);
|
|
|
|
if (actv_idx >= 0) {
|
|
p_cmd->operation = GAPM_DELETE_ACTIVITY;
|
|
} else {
|
|
p_cmd->operation = GAPM_DELETE_ALL_ACTIVITIES;
|
|
}
|
|
p_cmd->actv_idx = actv_idx;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_disconnect(int conidx, uint32_t reason)
|
|
{
|
|
struct gapc_disconnect_cmd *p_cmd =
|
|
KE_MSG_ALLOC(GAPC_DISCONNECT_CMD, KE_BUILD_ID(TASK_GAPC, conidx),
|
|
TASK_APP, gapc_disconnect_cmd);
|
|
|
|
p_cmd->operation = GAPC_DISCONNECT;
|
|
p_cmd->reason = reason;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_update_param(int conidx, uint16_t intv_min, uint16_t intv_max,
|
|
uint16_t latency, uint16_t time_out)
|
|
{
|
|
struct gapc_param_update_cmd *p_cmd =
|
|
KE_MSG_ALLOC(GAPC_PARAM_UPDATE_CMD, KE_BUILD_ID(TASK_GAPC, conidx),
|
|
TASK_APP, gapc_param_update_cmd);
|
|
|
|
p_cmd->operation = GAPC_UPDATE_PARAMS;
|
|
p_cmd->intv_min = intv_min;
|
|
p_cmd->intv_max = intv_max;
|
|
p_cmd->latency = latency;
|
|
p_cmd->time_out = time_out;
|
|
// not used by a slave device
|
|
p_cmd->ce_len_min = CE_LEN_MIN;
|
|
// p_cmd->ce_len_min = 0xFFFF;
|
|
p_cmd->ce_len_max = CE_LEN_MAX;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_conn_cfm(int conidx, struct gapc_connection_cfm *p_cfm)
|
|
{
|
|
struct gapc_connection_cfm *p_cmd =
|
|
KE_MSG_ALLOC(GAPC_CONNECTION_CFM, KE_BUILD_ID(TASK_GAPC, conidx),
|
|
TASK_APP, gapc_connection_cfm);
|
|
|
|
memcpy(p_cmd, p_cfm, sizeof(struct gapc_connection_cfm));
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_get_peer_info(int conidx, struct gapc_get_info_cmd *get_info)
|
|
{
|
|
struct gapc_get_info_cmd *p_cmd =
|
|
KE_MSG_ALLOC(GAPC_GET_INFO_CMD, KE_BUILD_ID(TASK_GAPC, conidx),
|
|
TASK_APP, gapc_get_info_cmd);
|
|
|
|
p_cmd->operation = get_info->operation;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_param_update_cfm(int conidx,
|
|
struct gapc_param_update_cfm *update_cfm)
|
|
{
|
|
struct gapc_param_update_cfm *p_cmd =
|
|
KE_MSG_ALLOC(GAPC_PARAM_UPDATE_CFM, KE_BUILD_ID(TASK_GAPC, conidx),
|
|
TASK_APP, gapc_param_update_cfm);
|
|
|
|
memcpy(p_cmd, update_cfm, sizeof(struct gapc_param_update_cfm));
|
|
p_cmd->ce_len_min = CE_LEN_MIN;
|
|
p_cmd->ce_len_max = CE_LEN_MAX;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_bond(int conidx, struct gapc_bond_cmd *p_param)
|
|
{
|
|
struct gapc_bond_cmd *p_cmd = KE_MSG_ALLOC(
|
|
GAPC_BOND_CMD, KE_BUILD_ID(TASK_GAPC, conidx), TASK_APP, gapc_bond_cmd);
|
|
|
|
memcpy(p_cmd, p_param, sizeof(struct gapc_bond_cmd));
|
|
p_cmd->operation = GAPC_BOND;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_bond_cfm(int conidx, struct gapc_bond_cfm *p_param)
|
|
{
|
|
struct gapc_bond_cfm *p_cmd = KE_MSG_ALLOC(
|
|
GAPC_BOND_CFM, KE_BUILD_ID(TASK_GAPC, conidx), TASK_APP, gapc_bond_cfm);
|
|
|
|
memcpy(p_cmd, p_param, sizeof(struct gapc_bond_cfm));
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_get_dev_info_cfm(int conidx, struct gapc_get_dev_info_cfm *p_param)
|
|
{
|
|
if ((p_param->req == GAPC_DEV_NAME) ||
|
|
(p_param->req == GAPC_DEV_APPEARANCE) ||
|
|
(p_param->req == GAPC_DEV_SLV_PREF_PARAMS)) {
|
|
struct gapc_get_dev_info_cfm *p_cmd = KE_MSG_ALLOC_DYN(
|
|
GAPC_GET_DEV_INFO_CFM, KE_BUILD_ID(TASK_GAPC, conidx), TASK_APP,
|
|
gapc_get_dev_info_cfm, DEVICE_NAME_MAX_LEN);
|
|
|
|
memcpy(p_cmd, p_param, sizeof(struct gapc_get_dev_info_cfm));
|
|
|
|
ke_msg_send(p_cmd);
|
|
} else {
|
|
// do nothing;
|
|
}
|
|
}
|
|
|
|
void xc_ble_set_dev_info_cfm(int conidx, struct gapc_set_dev_info_cfm *p_param)
|
|
{
|
|
struct gapc_set_dev_info_cfm *p_cmd =
|
|
KE_MSG_ALLOC(GAPC_SET_DEV_INFO_CFM, KE_BUILD_ID(TASK_GAPC, conidx),
|
|
TASK_APP, gapc_set_dev_info_cfm);
|
|
|
|
memcpy(p_cmd, p_param, sizeof(struct gapc_set_dev_info_cfm));
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_encrypt(int conidx, struct gapc_encrypt_cmd *p_param)
|
|
{
|
|
struct gapc_encrypt_cmd *p_cmd =
|
|
KE_MSG_ALLOC(GAPC_ENCRYPT_CMD, KE_BUILD_ID(TASK_GAPC, conidx), TASK_APP,
|
|
gapc_encrypt_cmd);
|
|
|
|
memcpy(p_cmd, p_param, sizeof(struct gapc_encrypt_cmd));
|
|
p_cmd->operation = GAPC_ENCRYPT;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
/// issue from master
|
|
void xc_ble_encrypt_cfm(int conidx, struct gapc_encrypt_cfm *p_param)
|
|
{
|
|
struct gapc_encrypt_cfm *p_cmd =
|
|
KE_MSG_ALLOC(GAPC_ENCRYPT_CFM, KE_BUILD_ID(TASK_GAPC, conidx), TASK_APP,
|
|
gapc_encrypt_cfm);
|
|
|
|
memcpy(p_cmd, p_param, sizeof(struct gapc_encrypt_cfm));
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
/// issue from slave
|
|
void xc_ble_req_security(int conidx, uint8_t sec_auth)
|
|
{
|
|
struct gapc_security_cmd *p_cmd =
|
|
KE_MSG_ALLOC(GAPC_SECURITY_CMD, KE_BUILD_ID(TASK_GAPC, conidx),
|
|
TASK_APP, gapc_security_cmd);
|
|
|
|
p_cmd->operation = GAPC_SECURITY_REQ;
|
|
p_cmd->auth = sec_auth;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_set_pkt_size(int conidx, uint16_t tx_octets, uint16_t tx_time)
|
|
{
|
|
struct gapc_set_le_pkt_size_cmd *p_cmd =
|
|
KE_MSG_ALLOC(GAPC_SET_LE_PKT_SIZE_CMD, KE_BUILD_ID(TASK_GAPC, conidx),
|
|
TASK_APP, gapc_set_le_pkt_size_cmd);
|
|
|
|
p_cmd->operation = GAPC_SET_LE_PKT_SIZE;
|
|
p_cmd->tx_octets = tx_octets;
|
|
p_cmd->tx_time = tx_time;
|
|
|
|
// Send the message
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_set_rx_pkt_size(int conidx, uint16_t rx_octets, uint16_t rx_time)
|
|
{
|
|
struct gapc_set_max_rx_size_and_time_cmd *p_cmd = KE_MSG_ALLOC(
|
|
GAPC_SET_MAX_RX_SIZE_AND_TIME_CMD, KE_BUILD_ID(TASK_GAPC, conidx),
|
|
TASK_APP, gapc_set_max_rx_size_and_time_cmd);
|
|
|
|
p_cmd->operation = GAPC_SET_MAX_RX_SIZE_AND_TIME;
|
|
p_cmd->rx_octets = rx_octets;
|
|
p_cmd->rx_time = rx_time;
|
|
|
|
// Send the message
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
void xc_ble_set_channel_map(struct gapm_set_channel_map_cmd *p_param)
|
|
{
|
|
struct gapm_set_channel_map_cmd *p_cmd =
|
|
KE_MSG_ALLOC(GAPM_SET_CHANNEL_MAP_CMD, TASK_GAPM, TASK_APP,
|
|
gapm_set_channel_map_cmd);
|
|
|
|
memcpy(p_cmd, p_param, sizeof(struct gapm_set_channel_map_cmd));
|
|
p_cmd->operation = GAPM_SET_CHANNEL_MAP;
|
|
|
|
ke_msg_send(p_cmd);
|
|
}
|
|
|
|
#endif // (BLE_APP_PRESENT)
|