component/bt : gatt server

1.add gatt server demo for user
2. some function didn't be tested
This commit is contained in:
Tian Hao 2016-11-18 00:30:35 +08:00
parent a35e2d42f0
commit 37365cef95
10 changed files with 382 additions and 73 deletions

View File

@ -30,7 +30,7 @@ esp_err_t esp_ble_gap_register_callback(esp_profile_cb_t callback)
esp_err_t esp_ble_gap_config_adv_data(esp_ble_adv_data_t *adv_data) esp_err_t esp_ble_gap_config_adv_data(esp_ble_adv_data_t *adv_data)
{ {
btc_msg_t msg; btc_msg_t msg;
esp_ble_gap_args_t arg; btc_ble_gap_args_t arg;
if (adv_data == NULL) { if (adv_data == NULL) {
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
@ -41,7 +41,7 @@ esp_err_t esp_ble_gap_config_adv_data(esp_ble_adv_data_t *adv_data)
msg.act = BTC_GAP_BLE_ACT_CFG_ADV_DATA; msg.act = BTC_GAP_BLE_ACT_CFG_ADV_DATA;
memcpy(&arg.adv_data, adv_data, sizeof(esp_ble_adv_data_t)); memcpy(&arg.adv_data, adv_data, sizeof(esp_ble_adv_data_t));
return (btc_transfer_context(&msg, &arg, sizeof(esp_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), btc_gap_ble_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
} }
@ -49,7 +49,7 @@ esp_err_t esp_ble_gap_config_adv_data(esp_ble_adv_data_t *adv_data)
esp_err_t esp_ble_gap_set_scan_params(esp_ble_scan_params_t *scan_params) esp_err_t esp_ble_gap_set_scan_params(esp_ble_scan_params_t *scan_params)
{ {
btc_msg_t msg; btc_msg_t msg;
esp_ble_gap_args_t arg; btc_ble_gap_args_t arg;
if (scan_params == NULL) { if (scan_params == NULL) {
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
@ -60,33 +60,33 @@ esp_err_t esp_ble_gap_set_scan_params(esp_ble_scan_params_t *scan_params)
msg.act = BTC_GAP_BLE_ACT_SET_SCAN_PARAM; msg.act = BTC_GAP_BLE_ACT_SET_SCAN_PARAM;
memcpy(&arg.scan_params, scan_params, sizeof(esp_ble_scan_params_t)); memcpy(&arg.scan_params, scan_params, sizeof(esp_ble_scan_params_t));
return (btc_transfer_context(&msg, &arg, sizeof(esp_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
} }
esp_err_t esp_ble_gap_start_scanning(uint32_t duration) esp_err_t esp_ble_gap_start_scanning(uint32_t duration)
{ {
btc_msg_t msg; btc_msg_t msg;
esp_ble_gap_args_t arg; btc_ble_gap_args_t arg;
msg.sig = BTC_SIG_API_CALL; msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BLE; msg.pid = BTC_PID_GAP_BLE;
msg.act = BTC_GAP_BLE_ACT_SET_SCAN_PARAM; msg.act = BTC_GAP_BLE_ACT_SET_SCAN_PARAM;
arg.duration = duration; arg.duration = duration;
return (btc_transfer_context(&msg, &arg, sizeof(esp_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
} }
esp_err_t esp_ble_gap_start_advertising(esp_ble_adv_params_t *adv_params) esp_err_t esp_ble_gap_start_advertising(esp_ble_adv_params_t *adv_params)
{ {
btc_msg_t msg; btc_msg_t msg;
esp_ble_gap_args_t arg; btc_ble_gap_args_t arg;
msg.sig = BTC_SIG_API_CALL; msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BLE; msg.pid = BTC_PID_GAP_BLE;
msg.act = BTC_GAP_BLE_ACT_START_ADV; msg.act = BTC_GAP_BLE_ACT_START_ADV;
memcpy(&arg.adv_params, adv_params, sizeof(esp_ble_adv_params_t)); memcpy(&arg.adv_params, adv_params, sizeof(esp_ble_adv_params_t));
return (btc_transfer_context(&msg, &arg, sizeof(esp_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
} }
esp_err_t esp_ble_gap_stop_advertising(void) esp_err_t esp_ble_gap_stop_advertising(void)
@ -104,20 +104,20 @@ esp_err_t esp_ble_gap_stop_advertising(void)
esp_err_t esp_ble_gap_update_conn_params(esp_ble_conn_update_params_t *params) esp_err_t esp_ble_gap_update_conn_params(esp_ble_conn_update_params_t *params)
{ {
btc_msg_t msg; btc_msg_t msg;
esp_ble_gap_args_t arg; btc_ble_gap_args_t arg;
msg.sig = BTC_SIG_API_CALL; msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BLE; msg.pid = BTC_PID_GAP_BLE;
msg.act = BTC_GAP_BLE_ACT_UPDATE_CONN_PARAM; msg.act = BTC_GAP_BLE_ACT_UPDATE_CONN_PARAM;
memcpy(&arg.conn_params, params, sizeof(esp_ble_conn_update_params_t)); memcpy(&arg.conn_params, params, sizeof(esp_ble_conn_update_params_t));
return (btc_transfer_context(&msg, &arg, sizeof(esp_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
} }
esp_err_t esp_ble_gap_set_pkt_data_len(esp_bd_addr_t remote_device, uint16_t tx_data_length) esp_err_t esp_ble_gap_set_pkt_data_len(esp_bd_addr_t remote_device, uint16_t tx_data_length)
{ {
btc_msg_t msg; btc_msg_t msg;
esp_ble_gap_args_t arg; btc_ble_gap_args_t arg;
msg.sig = BTC_SIG_API_CALL; msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BLE; msg.pid = BTC_PID_GAP_BLE;
@ -125,41 +125,41 @@ esp_err_t esp_ble_gap_set_pkt_data_len(esp_bd_addr_t remote_device, uint16_t tx_
arg.tx_data_length = tx_data_length; arg.tx_data_length = tx_data_length;
memcpy(arg.remote_device, remote_device, ESP_BD_ADDR_LEN); memcpy(arg.remote_device, remote_device, ESP_BD_ADDR_LEN);
return (btc_transfer_context(&msg, &arg, sizeof(esp_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
} }
esp_err_t esp_ble_gap_set_rand_addr(esp_bd_addr_t rand_addr) esp_err_t esp_ble_gap_set_rand_addr(esp_bd_addr_t rand_addr)
{ {
btc_msg_t msg; btc_msg_t msg;
esp_ble_gap_args_t arg; btc_ble_gap_args_t arg;
msg.sig = BTC_SIG_API_CALL; msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BLE; msg.pid = BTC_PID_GAP_BLE;
msg.act = BTC_GAP_BLE_ACT_SET_RAND_ADDRESS; msg.act = BTC_GAP_BLE_ACT_SET_RAND_ADDRESS;
memcpy(arg.rand_addr, rand_addr, ESP_BD_ADDR_LEN); memcpy(arg.rand_addr, rand_addr, ESP_BD_ADDR_LEN);
return (btc_transfer_context(&msg, &arg, sizeof(esp_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
} }
esp_err_t esp_ble_gap_config_local_privacy (bool privacy_enable) esp_err_t esp_ble_gap_config_local_privacy (bool privacy_enable)
{ {
btc_msg_t msg; btc_msg_t msg;
esp_ble_gap_args_t arg; btc_ble_gap_args_t arg;
msg.sig = BTC_SIG_API_CALL; msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BLE; msg.pid = BTC_PID_GAP_BLE;
msg.act = BTC_GAP_BLE_ACT_CONFIG_LOCAL_PRIVACY; msg.act = BTC_GAP_BLE_ACT_CONFIG_LOCAL_PRIVACY;
arg.privacy_enable = privacy_enable; arg.privacy_enable = privacy_enable;
return (btc_transfer_context(&msg, &arg, sizeof(esp_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
} }
esp_err_t esp_ble_gap_set_device_name(char *name) esp_err_t esp_ble_gap_set_device_name(char *name)
{ {
btc_msg_t msg; btc_msg_t msg;
esp_ble_gap_args_t arg; btc_ble_gap_args_t arg;
if (strlen(name) > ESP_GAP_DEVICE_NAME_MAX) { if (strlen(name) > ESP_GAP_DEVICE_NAME_MAX) {
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
@ -170,6 +170,6 @@ esp_err_t esp_ble_gap_set_device_name(char *name)
msg.act = BTC_GAP_BLE_ACT_SET_DEV_NAME; msg.act = BTC_GAP_BLE_ACT_SET_DEV_NAME;
strcpy(arg.device_name, name); strcpy(arg.device_name, name);
return (btc_transfer_context(&msg, &arg, sizeof(esp_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
} }

View File

@ -16,14 +16,11 @@ typedef uint32_t esp_gap_ble_event_t;
/// Advertising mode /// Advertising mode
typedef enum { typedef enum {
/// Mode in non-discoverable ADV_TYPE_IND = 0x00,
ADV_TYPE_NON_DISCOVERABLE = 0, ADV_TYPE_DIRECT_IND_HIGH = 0x01,
/// Mode in general discoverable ADV_TYPE_SCAN_IND = 0x02,
ADV_TYPE_GEN_DISCOVERABLE, ADV_TYPE_NONCONN_IND = 0x03,
/// Mode in limited discoverable ADV_TYPE_DIRECT_IND_LOW = 0x04,
ADV_TYPE_LIM_DISCOVERABLE,
/// Broadcaster mode which is a non discoverable and non connectable mode.
ADV_TYPE_BROADCASTER_MODE,
} esp_ble_adv_type_t; } esp_ble_adv_type_t;
typedef enum { typedef enum {
@ -50,7 +47,6 @@ typedef struct {
uint16_t adv_int_min; uint16_t adv_int_min;
uint16_t adv_int_max; uint16_t adv_int_max;
esp_ble_adv_type_t adv_type; esp_ble_adv_type_t adv_type;
esp_bd_addr_t own_addr;
esp_ble_addr_type_t own_addr_type; esp_ble_addr_type_t own_addr_type;
esp_bd_addr_t peer_addr; esp_bd_addr_t peer_addr;
esp_ble_addr_type_t peer_addr_type; esp_ble_addr_type_t peer_addr_type;
@ -105,21 +101,6 @@ typedef struct {
typedef void (*esp_gap_ble_cb_t)(esp_gap_ble_event_t event, void *param); typedef void (*esp_gap_ble_cb_t)(esp_gap_ble_event_t event, void *param);
/* esp_ble_gap_args_t */
typedef struct {
esp_ble_adv_data_t adv_data;
esp_ble_adv_params_t adv_params;
esp_ble_scan_params_t scan_params;
esp_ble_conn_update_params_t conn_params;
esp_bd_addr_t remote_device;
esp_bd_addr_t rand_addr;
uint32_t duration;
uint16_t tx_data_length;
bool privacy_enable;
#define ESP_GAP_DEVICE_NAME_MAX (32)
char device_name[ESP_GAP_DEVICE_NAME_MAX+1];
} esp_ble_gap_args_t;
typedef enum { typedef enum {
/* Search callback events */ /* Search callback events */
ESP_GAP_SEARCH_INQ_RES_EVT = 0, /* Inquiry result for a peer device. */ ESP_GAP_SEARCH_INQ_RES_EVT = 0, /* Inquiry result for a peer device. */

View File

@ -99,6 +99,7 @@ bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg
memcpy(&lmsg, msg, sizeof(btc_msg_t)); memcpy(&lmsg, msg, sizeof(btc_msg_t));
if (arg) { if (arg) {
lmsg.arg = (void *)GKI_getbuf(arg_len); lmsg.arg = (void *)GKI_getbuf(arg_len);
memset(lmsg.arg, 0x00, arg_len); //important, avoid arg which have no length
if (lmsg.arg == NULL) { if (lmsg.arg == NULL) {
return BT_STATUS_NOMEM; return BT_STATUS_NOMEM;
} }

View File

@ -115,8 +115,10 @@ static void btc_to_bta_adv_data(esp_ble_adv_data_t *p_adv_data, tBTA_BLE_ADV_DAT
mask = BTM_BLE_AD_BIT_FLAGS; mask = BTM_BLE_AD_BIT_FLAGS;
} }
if (p_adv_data->include_name) if (p_adv_data->include_name) {
LOG_ERROR("include dev name\n");
mask |= BTM_BLE_AD_BIT_DEV_NAME; mask |= BTM_BLE_AD_BIT_DEV_NAME;
}
if (p_adv_data->include_txpower) if (p_adv_data->include_txpower)
mask |= BTM_BLE_AD_BIT_TX_PWR; mask |= BTM_BLE_AD_BIT_TX_PWR;
@ -348,7 +350,6 @@ static void btc_set_scan_param_callback(tGATT_IF client_if, tBTA_STATUS status )
static void btc_ble_set_adv_data(esp_ble_adv_data_t *adv_data, static void btc_ble_set_adv_data(esp_ble_adv_data_t *adv_data,
tBTA_SET_ADV_DATA_CMPL_CBACK p_adv_data_cback) tBTA_SET_ADV_DATA_CMPL_CBACK p_adv_data_cback)
{ {
tBTA_BLE_ADV_DATA bta_adv_data; //TODO:must be global, not stack
tBTA_BLE_AD_MASK data_mask = 0; tBTA_BLE_AD_MASK data_mask = 0;
btc_to_bta_adv_data(adv_data, &gl_bta_adv_data, &data_mask); btc_to_bta_adv_data(adv_data, &gl_bta_adv_data, &data_mask);
@ -373,10 +374,26 @@ static void btc_ble_set_scan_param(esp_ble_scan_params_t *ble_scan_params,
//BTA_DmBleSetScanRsp(data_mask, &gl_bta_scan_rsp_data, p_scan_rsp_data_cback); //BTA_DmBleSetScanRsp(data_mask, &gl_bta_scan_rsp_data, p_scan_rsp_data_cback);
} }
static void btc_ble_start_advertising(esp_ble_adv_params_t *ble_adv_params) void btc_ble_start_advertising (esp_ble_adv_params_t *ble_adv_params)
{ {
tBLE_BD_ADDR bd_addr; tBTA_DM_DISC disc_mode = 0;
tBTA_DM_CONN conn_mode = 0;
tBLE_BD_ADDR peer_addr;
if (ble_adv_params->adv_type == ADV_TYPE_NONCONN_IND){
conn_mode = BTA_DM_BLE_NON_CONNECTABLE;
}else {
conn_mode = BTA_DM_BLE_CONNECTABLE;
}
if (ble_adv_params->adv_filter_policy == ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY){
disc_mode = BTA_DM_BLE_GENERAL_DISCOVERABLE;
}else if (ble_adv_params->adv_filter_policy == ADV_FILTER_ALLOW_SCAN_WLST_CON_ANY
|| ble_adv_params->adv_filter_policy == ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST){
disc_mode = BTA_DM_BLE_LIMITED_DISCOVERABLE;
}else if (ble_adv_params->adv_filter_policy == ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST){
disc_mode = BTA_DM_BLE_NON_DISCOVERABLE;
}
if (!API_BLE_ISVALID_PARAM(ble_adv_params->adv_int_min, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX) || if (!API_BLE_ISVALID_PARAM(ble_adv_params->adv_int_min, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX) ||
!API_BLE_ISVALID_PARAM(ble_adv_params->adv_int_max, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX)) !API_BLE_ISVALID_PARAM(ble_adv_params->adv_int_max, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX))
@ -385,8 +402,8 @@ static void btc_ble_start_advertising(esp_ble_adv_params_t *ble_adv_params)
return ; return ;
} }
if ((ble_adv_params->adv_type < ADV_TYPE_NON_DISCOVERABLE) && if ((ble_adv_params->adv_type < ADV_TYPE_IND) &&
(ble_adv_params->adv_type > ADV_TYPE_BROADCASTER_MODE) ) (ble_adv_params->adv_type > ADV_TYPE_DIRECT_IND_LOW) )
{ {
LOG_ERROR("Invalid advertisting type parameters.\n"); LOG_ERROR("Invalid advertisting type parameters.\n");
return; return;
@ -398,19 +415,22 @@ static void btc_ble_start_advertising(esp_ble_adv_params_t *ble_adv_params)
LOG_ERROR("Invalid advertisting type parameters.\n"); LOG_ERROR("Invalid advertisting type parameters.\n");
return; return;
} }
LOG_ERROR("API_Ble_AppStartAdvertising\n"); LOG_ERROR("API_Ble_AppStartAdvertising\n");
bd_addr.type = ble_adv_params->peer_addr_type;
memcpy(&bd_addr.bda, ble_adv_params->peer_addr, sizeof(BD_ADDR));
/// ///
memcpy(peer_addr.bda, ble_adv_params->peer_addr, ESP_BD_ADDR_LEN);
peer_addr.type = ble_adv_params->peer_addr_type;
BTA_DmSetBleAdvParamsAll(ble_adv_params->adv_int_min, BTA_DmSetBleAdvParamsAll(ble_adv_params->adv_int_min,
ble_adv_params->adv_int_max, ble_adv_params->adv_int_max,
ble_adv_params->adv_type, ble_adv_params->adv_type,
ble_adv_params->own_addr_type, ble_adv_params->own_addr_type,
ble_adv_params->channel_map, ble_adv_params->channel_map,
ble_adv_params->adv_filter_policy, ble_adv_params->adv_filter_policy,
&bd_addr); &peer_addr);
/*set connectable,discoverable, pairable and paired only modes of local device*/
BTA_DmSetVisibility(disc_mode, conn_mode, (UINT8)BTA_DM_NON_PAIRABLE, (UINT8)BTA_DM_CONN_ALL);
} }
@ -567,10 +587,63 @@ void btc_gap_ble_cb_handler(btc_msg_t *msg)
} }
void btc_gap_ble_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
{
switch (msg->act)
{
case BTC_GAP_BLE_ACT_CFG_ADV_DATA: {
esp_ble_adv_data_t *src = (esp_ble_adv_data_t *)p_src;
esp_ble_adv_data_t *dst = (esp_ble_adv_data_t*) p_dest;
if (src->p_manufacturer_data) {
dst->p_manufacturer_data = GKI_getbuf(src->manufacturer_len);
memcpy(dst->p_manufacturer_data, src->p_manufacturer_data,
src->manufacturer_len);
}
if (src->p_service_data) {
dst->p_service_data = GKI_getbuf(src->service_data_len);
memcpy(dst->p_service_data, src->p_service_data, src->service_data_len);
}
if (src->p_service_uuid) {
dst->p_service_uuid = GKI_getbuf(src->service_uuid_len);
memcpy(dst->p_service_uuid, src->p_service_uuid, src->service_uuid_len);
}
break;
}
default:
LOG_ERROR("Unhandled deep copy\n", msg->act);
break;
}
}
static void btc_gap_ble_arg_deep_free(btc_msg_t *msg)
{
switch (msg->act) {
case BTC_GAP_BLE_ACT_CFG_ADV_DATA: {
esp_ble_adv_data_t *adv = (esp_ble_adv_data_t *)msg->arg;
if (adv->p_service_data)
GKI_freebuf(adv->p_service_data);
if (adv->p_service_uuid)
GKI_freebuf(adv->p_service_uuid);
if (adv->p_manufacturer_data)
GKI_freebuf(adv->p_manufacturer_data);
break;
}
default:
LOG_ERROR("Unhandled deep free\n", msg->act);
break;
}
}
void btc_gap_ble_call_handler(btc_msg_t *msg) void btc_gap_ble_call_handler(btc_msg_t *msg)
{ {
esp_ble_gap_args_t *arg = (esp_ble_gap_args_t *)msg->arg; btc_ble_gap_args_t *arg = (btc_ble_gap_args_t *)msg->arg;
LOG_ERROR("%s act %d\n", __FUNCTION__, msg->act);
switch (msg->act) { switch (msg->act) {
case BTC_GAP_BLE_ACT_CFG_ADV_DATA: case BTC_GAP_BLE_ACT_CFG_ADV_DATA:
@ -610,8 +683,11 @@ void btc_gap_ble_call_handler(btc_msg_t *msg)
btc_ble_config_local_privacy(arg->privacy_enable); btc_ble_config_local_privacy(arg->privacy_enable);
break; break;
case BTC_GAP_BLE_ACT_SET_DEV_NAME: case BTC_GAP_BLE_ACT_SET_DEV_NAME:
BTA_DmSetDeviceName(arg->device_name);
break; break;
default: default:
break; break;
} }
btc_gap_ble_arg_deep_free(msg);
} }

View File

@ -128,7 +128,7 @@ void btc_gatts_call_handler(btc_msg_t *msg)
BTA_GATTS_DeleteService(arg->service_handle); BTA_GATTS_DeleteService(arg->service_handle);
break; break;
case BTC_GATTS_ACT_START_SERVICE: case BTC_GATTS_ACT_START_SERVICE:
BTA_GATTS_StartService(arg->service_handle, GATT_TRANSPORT_LE); BTA_GATTS_StartService(arg->service_handle, BTA_GATT_TRANSPORT_LE);
break; break;
case BTC_GATTS_ACT_STOP_SERVICE: case BTC_GATTS_ACT_STOP_SERVICE:
BTA_GATTS_StopService(arg->service_handle); BTA_GATTS_StopService(arg->service_handle);

View File

@ -1,6 +1,9 @@
#ifndef __BTC_GAP_BLE_H__ #ifndef __BTC_GAP_BLE_H__
#define __BTC_GAP_BLE_H__ #define __BTC_GAP_BLE_H__
#include "esp_bt_defs.h"
#include "esp_gap_ble_api.h"
typedef enum { typedef enum {
BTC_GAP_BLE_ACT_CFG_ADV_DATA = 0, BTC_GAP_BLE_ACT_CFG_ADV_DATA = 0,
BTC_GAP_BLE_ACT_SET_SCAN_PARAM, BTC_GAP_BLE_ACT_SET_SCAN_PARAM,
@ -15,7 +18,24 @@ typedef enum {
BTC_GAP_BLE_ACT_SET_DEV_NAME, BTC_GAP_BLE_ACT_SET_DEV_NAME,
} btc_gap_ble_act_t; } btc_gap_ble_act_t;
/* btc_ble_gap_args_t */
typedef struct {
esp_ble_adv_data_t adv_data;
esp_ble_adv_params_t adv_params;
esp_ble_scan_params_t scan_params;
esp_ble_conn_update_params_t conn_params;
esp_bd_addr_t remote_device;
esp_bd_addr_t rand_addr;
uint32_t duration;
uint16_t tx_data_length;
bool privacy_enable;
#define ESP_GAP_DEVICE_NAME_MAX (32)
char device_name[ESP_GAP_DEVICE_NAME_MAX+1];
} btc_ble_gap_args_t;
void btc_gap_ble_call_handler(btc_msg_t *msg); void btc_gap_ble_call_handler(btc_msg_t *msg);
void btc_gap_ble_cb_handler(btc_msg_t *msg); void btc_gap_ble_cb_handler(btc_msg_t *msg);
void btc_gap_ble_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src);
#endif /* __BTC_GAP_BLE_H__ */ #endif /* __BTC_GAP_BLE_H__ */

View File

@ -0,0 +1,11 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
PROJECT_NAME := bluedroid_demos
COMPONENT_ADD_INCLUDEDIRS := components/include
include $(IDF_PATH)/make/project.mk

View File

@ -0,0 +1,5 @@
ESP-IDF GATT SERVER demo
=======================
This is the demo for user to use ESP_APIs to create a GATT Server.

View File

@ -0,0 +1,10 @@
#
# Main Makefile. This is basically the same as a component makefile.
#
# This Makefile should, at the very least, just include $(SDK_PATH)/make/component_common.mk. By default,
# this will take the sources in the src/ directory, compile them and link them into
# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#
include $(IDF_PATH)/make/component_common.mk

View File

@ -0,0 +1,205 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "bt.h"
#include "bta_api.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_bt_defs.h"
#include "esp_bt_main.h"
#include "esp_bt_main.h"
#define GATTS_SERVICE_UUID_TEST 0xFFFF
#define GATTS_CHAR_UUID_TEST 0xFF01
#define GATTS_DESCR_UUID_TEST 0x3333
#define APP_ID_TEST 0x18
#define GATTS_NUM_HANDLE_TEST 4
#define TEST_DEVICE_NAME "snakeNB"
#define TEST_MANUFACTURER_DATA_LEN 8
static uint16_t test_service_uuid = GATTS_NUM_HANDLE_TEST;
static uint8_t test_manufacturer[TEST_MANUFACTURER_DATA_LEN] = {0x1, 0x2, 0x1, 0x2, 0x1, 0x2, 0x1, 0x2};
static esp_ble_adv_data_t test_adv_data = {
.set_scan_rsp = false,
.include_name = true,
.include_txpower = true,
.min_interval = 0x20,
.max_interval = 0x40,
.appearance = 0,
.manufacturer_len = TEST_MANUFACTURER_DATA_LEN,
.p_manufacturer_data = &test_manufacturer[0],
.service_data_len = 0,
.p_service_data = NULL,
.service_uuid_len = 2,
.p_service_uuid = (uint8_t *)&test_service_uuid,
};
static esp_ble_adv_params_t test_adv_params = {
.adv_int_min = 0x20,
.adv_int_max = 0x40,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
//.peer_addr =
//.peer_addr_type =
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
struct gatts_test_inst {
uint16_t gatt_if;
uint16_t app_id;
uint16_t conn_id;
uint16_t service_handle;
esp_gatt_srvc_id_t service_id;
uint16_t char_handle;
esp_bt_uuid_t char_uuid;
esp_gatt_perm_t perm;
esp_gatt_char_prop_t property;
uint16_t descr_handle;
esp_bt_uuid_t descr_uuid;
};
static struct gatts_test_inst gl_test;
static void gap_event_handler(uint32_t event, void *param)
{
LOG_ERROR("GAP_EVT, event %d\n", event);
}
static void gatts_event_handler(uint32_t event, void *param)
{
esp_ble_gatts_cb_param_t *p = (esp_ble_gatts_cb_param_t *)param;
switch (event) {
case ESP_GATTS_REG_EVT:
LOG_ERROR("REGISTER_APP_EVT, status %d, gatt_if %d, app_id %d\n", p->reg.status, p->reg.gatt_if, p->reg.app_id);
gl_test.gatt_if = p->reg.gatt_if;
gl_test.service_id.is_primary = 1;
gl_test.service_id.id.inst_id = 0x00;
gl_test.service_id.id.uuid.len = ESP_UUID_LEN_16;
gl_test.service_id.id.uuid.uuid.uuid16 = GATTS_SERVICE_UUID_TEST;
esp_ble_gatts_create_service(gl_test.gatt_if, &gl_test.service_id, GATTS_NUM_HANDLE_TEST);
esp_ble_gap_set_device_name(TEST_DEVICE_NAME);
esp_ble_gap_config_adv_data(&test_adv_data);
esp_ble_gap_start_advertising(&test_adv_params);
break;
case ESP_GATTS_READ_EVT: {
LOG_ERROR("GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", p->read.conn_id, p->read.trans_id, p->read.handle);
esp_gatt_rsp_t rsp;
memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
rsp.attr_value.handle = p->read.handle;
rsp.attr_value.len = 4;
rsp.attr_value.value[0] = 0xde;
rsp.attr_value.value[1] = 0xed;
rsp.attr_value.value[2] = 0xbe;
rsp.attr_value.value[3] = 0xef;
esp_ble_gatts_send_response(p->read.conn_id, p->read.trans_id,
ESP_GATT_OK, &rsp);
break;
}
case ESP_GATTS_WRITE_EVT:
case ESP_GATTS_EXEC_WRITE_EVT:
case ESP_GATTS_MTU_EVT:
case ESP_GATTS_CONF_EVT:
case ESP_GATTS_UNREG_EVT:
break;
case ESP_GATTS_CREATE_EVT:
LOG_ERROR("CREATE_SERVICE_EVT, status %d, gatt_if %d, service_handle %d\n", p->create.status, p->create.gatt_if, p->create.service_handle);
gl_test.service_handle = p->create.service_handle;
gl_test.char_uuid.len = ESP_UUID_LEN_16;
gl_test.char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST;
esp_ble_gatts_start_service(gl_test.service_handle);
esp_ble_gatts_add_char(gl_test.service_handle, &gl_test.char_uuid,
ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE,
ESP_GATT_CHAR_PROP_BIT_READ|ESP_GATT_CHAR_PROP_BIT_WRITE|ESP_GATT_CHAR_PROP_BIT_NOTIFY);
break;
case ESP_GATTS_ADD_INCL_SRVC_EVT:
break;
case ESP_GATTS_ADD_CHAR_EVT:
LOG_ERROR("ADD_CHAR_EVT, status %d, gatt_if %d, attr_handle %d, service_handle %d\n",
p->add_char.status, p->add_char.gatt_if, p->add_char.attr_handle, p->add_char.service_handle);
gl_test.char_handle = p->add_char.attr_handle;
gl_test.descr_uuid.len = ESP_UUID_LEN_16;
gl_test.descr_uuid.uuid.uuid16 = GATT_UUID_CHAR_CLIENT_CONFIG;
esp_ble_gatts_add_char_descr(gl_test.service_handle, &gl_test.descr_uuid,
ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE);
break;
case ESP_GATTS_ADD_CHAR_DESCR_EVT:
LOG_ERROR("ADD_DESCR_EVT, status %d, gatt_if %d, attr_handle %d, service_handle %d\n",
p->add_char.status, p->add_char.gatt_if, p->add_char.attr_handle, p->add_char.service_handle);
break;
case ESP_GATTS_DELELTE_EVT:
break;
case ESP_GATTS_START_EVT:
LOG_ERROR("SERVICE_START_EVT, status %d, gatt_if %d, service_handle %d\n",
p->start.status, p->start.gatt_if, p->start.service_handle);
break;
case ESP_GATTS_STOP_EVT:
break;
case ESP_GATTS_CONNECT_EVT:
LOG_ERROR("SERVICE_START_EVT, conn_id %d, gatt_if %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
p->connect.conn_id, p->connect.gatt_if,
p->connect.remote_bda[0], p->connect.remote_bda[1], p->connect.remote_bda[2],
p->connect.remote_bda[3], p->connect.remote_bda[4], p->connect.remote_bda[5],
p->connect.is_connected);
gl_test.conn_id = p->connect.conn_id;
break;
case ESP_GATTS_DISCONNECT_EVT:
case ESP_GATTS_OPEN_EVT:
case ESP_GATTS_CANCEL_OPEN_EVT:
case ESP_GATTS_CLOSE_EVT:
case ESP_GATTS_LISTEN_EVT:
case ESP_GATTS_CONGEST_EVT:
default:
break;
}
}
void app_main()
{
esp_err_t ret;
bt_controller_init();
LOG_ERROR("%s init bluetooth\n", __func__);
ret = esp_init_bluetooth();
if (ret) {
LOG_ERROR("%s init bluetooth failed\n", __func__);
return;
}
ret = esp_enable_bluetooth();
if (ret) {
LOG_ERROR("%s enable bluetooth failed\n", __func__);
return;
}
esp_ble_gatts_register_callback(gatts_event_handler);
esp_ble_gap_register_callback(gap_event_handler);
esp_ble_gatts_app_register(0x18);
return;
}