mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-02 03:00:34 +03:00
Protocomm : Added support for choice of transport - WiFi (SoftAP+HTTPD), BLE, Console (development friendly transport)
Co-Authored-By: Amey Inamdar <amey@espressif.com> Co-Authored-By: Anurag Kar <anurag.kar@espressif.com>
This commit is contained in:
committed by
Anurag Kar
co-authored by
Anurag Kar
parent
e94dffc9c5
commit
9428375368
@@ -0,0 +1,268 @@
|
||||
// Copyright 2015-2018 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 <freertos/FreeRTOS.h>
|
||||
#include <esp_system.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_bt.h>
|
||||
#include <esp_gap_ble_api.h>
|
||||
#include <esp_gatts_api.h>
|
||||
#include <esp_bt_main.h>
|
||||
#include <esp_gatt_common_api.h>
|
||||
|
||||
#include "simple_ble.h"
|
||||
|
||||
#define SIMPLE_BLE_MAX_GATT_TABLE_SIZE 20
|
||||
|
||||
static const char *TAG = "simple_ble";
|
||||
|
||||
static simple_ble_cfg_t *g_ble_cfg_p;
|
||||
static uint16_t g_gatt_table_map[SIMPLE_BLE_MAX_GATT_TABLE_SIZE];
|
||||
|
||||
uint16_t simple_ble_get_uuid(uint16_t handle)
|
||||
{
|
||||
uint16_t *uuid_ptr;
|
||||
|
||||
for (int i = 0; i < SIMPLE_BLE_MAX_GATT_TABLE_SIZE; i++) {
|
||||
if (g_gatt_table_map[i] == handle) {
|
||||
uuid_ptr = (uint16_t *) g_ble_cfg_p->gatt_db[i].att_desc.uuid_p;
|
||||
return *uuid_ptr;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
|
||||
{
|
||||
switch (event) {
|
||||
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
|
||||
esp_ble_gap_start_advertising(&g_ble_cfg_p->adv_params);
|
||||
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t p_gatts_if, esp_ble_gatts_cb_param_t *param)
|
||||
{
|
||||
static esp_gatt_if_t gatts_if = ESP_GATT_IF_NONE;
|
||||
esp_err_t ret;
|
||||
uint8_t service_instance_id = 0;
|
||||
if (event == ESP_GATTS_REG_EVT) {
|
||||
if (param->reg.status == ESP_GATT_OK) {
|
||||
gatts_if = p_gatts_if;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "reg app failed, app_id 0x0x%x, status %d",
|
||||
param->reg.app_id,
|
||||
param->reg.status);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (gatts_if != ESP_GATT_IF_NONE && gatts_if != p_gatts_if) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (event) {
|
||||
case ESP_GATTS_REG_EVT:
|
||||
ret = esp_ble_gatts_create_attr_tab(g_ble_cfg_p->gatt_db, gatts_if, g_ble_cfg_p->gatt_db_count, service_instance_id);
|
||||
if (ret) {
|
||||
ESP_LOGE(TAG, "create attr table failed, error code = 0x%x", ret);
|
||||
return;
|
||||
}
|
||||
ret = esp_ble_gap_set_device_name(g_ble_cfg_p->device_name);
|
||||
if (ret) {
|
||||
ESP_LOGE(TAG, "set device name failed, error code = 0x%x", ret);
|
||||
return;
|
||||
}
|
||||
ret = esp_ble_gap_config_adv_data(&g_ble_cfg_p->adv_data);
|
||||
if (ret) {
|
||||
ESP_LOGE(TAG, "config adv data failed, error code = 0x%x", ret);
|
||||
return;
|
||||
}
|
||||
break;
|
||||
case ESP_GATTS_READ_EVT:
|
||||
g_ble_cfg_p->read_fn(event, gatts_if, param);
|
||||
break;
|
||||
case ESP_GATTS_WRITE_EVT:
|
||||
g_ble_cfg_p->write_fn(event, gatts_if, param);
|
||||
break;
|
||||
case ESP_GATTS_EXEC_WRITE_EVT:
|
||||
g_ble_cfg_p->exec_write_fn(event, gatts_if, param);
|
||||
break;
|
||||
case ESP_GATTS_MTU_EVT:
|
||||
ESP_LOGV(TAG, "ESP_GATTS_MTU_EVT, MTU %d", param->mtu.mtu);
|
||||
if (g_ble_cfg_p->set_mtu_fn) {
|
||||
g_ble_cfg_p->set_mtu_fn(event, gatts_if, param);
|
||||
}
|
||||
break;
|
||||
case ESP_GATTS_CONF_EVT:
|
||||
ESP_LOGD(TAG, "ESP_GATTS_CONF_EVT, status = %d", param->conf.status);
|
||||
break;
|
||||
case ESP_GATTS_START_EVT:
|
||||
ESP_LOGD(TAG, "SERVICE_START_EVT, status %d, service_handle %d", param->start.status, param->start.service_handle);
|
||||
break;
|
||||
case ESP_GATTS_CONNECT_EVT:
|
||||
ESP_LOGD(TAG, "ESP_GATTS_CONNECT_EVT, conn_id = %d", param->connect.conn_id);
|
||||
g_ble_cfg_p->connect_fn(event, gatts_if, param);
|
||||
esp_ble_conn_update_params_t conn_params = {0};
|
||||
memcpy(conn_params.bda, param->connect.remote_bda, sizeof(esp_bd_addr_t));
|
||||
/* For the iOS system, please refer the official Apple documents about BLE connection parameters restrictions. */
|
||||
conn_params.latency = 0;
|
||||
conn_params.max_int = 0x20; // max_int = 0x20*1.25ms = 40ms
|
||||
conn_params.min_int = 0x10; // min_int = 0x10*1.25ms = 20ms
|
||||
conn_params.timeout = 400; // timeout = 400*10ms = 4000ms
|
||||
esp_ble_gap_update_conn_params(&conn_params);
|
||||
break;
|
||||
case ESP_GATTS_DISCONNECT_EVT:
|
||||
ESP_LOGD(TAG, "ESP_GATTS_DISCONNECT_EVT, reason = %d", param->disconnect.reason);
|
||||
g_ble_cfg_p->disconnect_fn(event, gatts_if, param);
|
||||
esp_ble_gap_start_advertising(&g_ble_cfg_p->adv_params);
|
||||
break;
|
||||
case ESP_GATTS_CREAT_ATTR_TAB_EVT: {
|
||||
if (param->add_attr_tab.status != ESP_GATT_OK) {
|
||||
ESP_LOGE(TAG, "creating the attribute table failed, error code=0x%x", param->add_attr_tab.status);
|
||||
} else if (param->add_attr_tab.num_handle != g_ble_cfg_p->gatt_db_count) {
|
||||
ESP_LOGE(TAG, "created attribute table abnormally ");
|
||||
} else {
|
||||
ESP_LOGD(TAG, "created attribute table successfully, the number handle = %d", param->add_attr_tab.num_handle);
|
||||
memcpy(g_gatt_table_map, param->add_attr_tab.handles, param->add_attr_tab.num_handle * sizeof(g_gatt_table_map[0]));
|
||||
/* We assume, for now, that the first entry is always the index to the 'service' definition */
|
||||
esp_ble_gatts_start_service(g_gatt_table_map[0]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ESP_GATTS_STOP_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:
|
||||
case ESP_GATTS_UNREG_EVT:
|
||||
case ESP_GATTS_DELETE_EVT:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
simple_ble_cfg_t *simple_ble_init()
|
||||
{
|
||||
simple_ble_cfg_t *ble_cfg_p = (simple_ble_cfg_t *) malloc(sizeof(simple_ble_cfg_t));
|
||||
if (ble_cfg_p == NULL) {
|
||||
ESP_LOGE(TAG, "No memory for simple_ble_cfg_t");
|
||||
return NULL;
|
||||
}
|
||||
return ble_cfg_p;
|
||||
}
|
||||
|
||||
esp_err_t simple_ble_deinit()
|
||||
{
|
||||
free(g_ble_cfg_p->gatt_db);
|
||||
free(g_ble_cfg_p);
|
||||
g_ble_cfg_p = NULL;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/* Expects the pointer stays valid throughout */
|
||||
esp_err_t simple_ble_start(simple_ble_cfg_t *cfg)
|
||||
{
|
||||
g_ble_cfg_p = cfg;
|
||||
ESP_LOGD(TAG, "Free mem at start of simple_ble_init %d", esp_get_free_heap_size());
|
||||
esp_err_t ret;
|
||||
|
||||
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
||||
ret = esp_bt_controller_init(&bt_cfg);
|
||||
if (ret) {
|
||||
ESP_LOGE(TAG, "%s enable controller failed %d", __func__, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
|
||||
if (ret) {
|
||||
ESP_LOGE(TAG, "%s enable controller failed %d", __func__, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = esp_bluedroid_init();
|
||||
if (ret) {
|
||||
ESP_LOGE(TAG, "%s init bluetooth failed %d", __func__, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = esp_bluedroid_enable();
|
||||
if (ret) {
|
||||
ESP_LOGE(TAG, "%s enable bluetooth failed %d", __func__, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = esp_ble_gatts_register_callback(gatts_profile_event_handler);
|
||||
if (ret) {
|
||||
ESP_LOGE(TAG, "gatts register error, error code = 0x%x", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = esp_ble_gap_register_callback(gap_event_handler);
|
||||
if (ret) {
|
||||
ESP_LOGE(TAG, "gap register error, error code = 0x%x", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint16_t app_id = 0x55;
|
||||
ret = esp_ble_gatts_app_register(app_id);
|
||||
if (ret) {
|
||||
ESP_LOGE(TAG, "gatts app register error, error code = 0x%x", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
|
||||
if (local_mtu_ret) {
|
||||
ESP_LOGE(TAG, "set local MTU failed, error code = 0x%x", local_mtu_ret);
|
||||
}
|
||||
ESP_LOGD(TAG, "Free mem at end of simple_ble_init %d", esp_get_free_heap_size());
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t simple_ble_stop()
|
||||
{
|
||||
esp_err_t err;
|
||||
ESP_LOGD(TAG, "Free mem at start of simple_ble_stop %d", esp_get_free_heap_size());
|
||||
err = esp_bluedroid_disable();
|
||||
if (err != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
ESP_LOGD(TAG, "esp_bluedroid_disable called successfully");
|
||||
err = esp_bluedroid_deinit();
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
ESP_LOGD(TAG, "esp_bluedroid_deinit called successfully");
|
||||
err = esp_bt_controller_disable();
|
||||
if (err != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
/* The API `esp_bt_controller_deinit` will have to be removed when we add support for
|
||||
* `reset to provisioning`
|
||||
*/
|
||||
ESP_LOGD(TAG, "esp_bt_controller_disable called successfully");
|
||||
err = esp_bt_controller_deinit();
|
||||
if (err != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
ESP_LOGD(TAG, "esp_bt_controller_deinit called successfully");
|
||||
|
||||
ESP_LOGD(TAG, "Free mem at end of simple_ble_stop %d", esp_get_free_heap_size());
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
// Copyright 2015-2018 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.
|
||||
#ifndef _SIMPLE_BLE_
|
||||
#define _SIMPLE_BLE_
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <esp_gap_ble_api.h>
|
||||
#include <esp_gatts_api.h>
|
||||
|
||||
typedef void (simple_ble_cb_t)(esp_gatts_cb_event_t event, esp_gatt_if_t p_gatts_if, esp_ble_gatts_cb_param_t *param);
|
||||
|
||||
/**
|
||||
* This structure is populated with the details required
|
||||
* to create an instance of BLE easily. It requires function
|
||||
* pointers, advertising parameters and gatt description table
|
||||
*/
|
||||
typedef struct {
|
||||
/** Name to be displayed to devices scanning for ESP32 */
|
||||
const char *device_name;
|
||||
/** Advertising data content, according to "Supplement to the Bluetooth Core Specification" */
|
||||
esp_ble_adv_data_t adv_data;
|
||||
/** Parameters to configure the nature of advertising */
|
||||
esp_ble_adv_params_t adv_params;
|
||||
/** Descriptor table which consists the configuration required by services and characteristics */
|
||||
esp_gatts_attr_db_t *gatt_db;
|
||||
/** Number of entries in the gatt_db descriptor table */
|
||||
ssize_t gatt_db_count;
|
||||
/** BLE read callback */
|
||||
simple_ble_cb_t *read_fn;
|
||||
/** BLE write callback */
|
||||
simple_ble_cb_t *write_fn;
|
||||
/** BLE exec write callback */
|
||||
simple_ble_cb_t *exec_write_fn;
|
||||
/** Client disconnect callback */
|
||||
simple_ble_cb_t *disconnect_fn;
|
||||
/** Client connect callback */
|
||||
simple_ble_cb_t *connect_fn;
|
||||
/** MTU set callback */
|
||||
simple_ble_cb_t *set_mtu_fn;
|
||||
} simple_ble_cfg_t;
|
||||
|
||||
|
||||
/** Initialise a simple ble connection
|
||||
*
|
||||
* This function allocates memory and returns a pointer to the
|
||||
* configuration structure.
|
||||
*
|
||||
* @return simple_ble_cfg_t* Pointer to configuration structure
|
||||
*/
|
||||
simple_ble_cfg_t *simple_ble_init();
|
||||
|
||||
/** Deallocates memory
|
||||
*
|
||||
* This function deallocate memory of the configuration structure.
|
||||
*
|
||||
* @return ESP_OK
|
||||
*/
|
||||
esp_err_t simple_ble_deinit();
|
||||
|
||||
/** Starts BLE service
|
||||
*
|
||||
* This function makes calls to the GATT and GAP APIs
|
||||
* to initialize the BLE service as per parameters stored
|
||||
* in the config structure. At the end of this function,
|
||||
* one should be able to scan and connect to the ESP32 device
|
||||
* using BLE.
|
||||
* This API sets the MTU size to 500 (this is not part of the config structure)
|
||||
*
|
||||
* @return ESP_OK on success, and appropriate error code for failure
|
||||
*/
|
||||
esp_err_t simple_ble_start(simple_ble_cfg_t *cfg);
|
||||
|
||||
/** Stops the BLE service
|
||||
*
|
||||
* This API is called to stop the BLE service.
|
||||
* This includes calls to disable and deinit bluedroid and bt controller.
|
||||
*
|
||||
* @return ESP_OK on success, and appropriate error code for failure
|
||||
*/
|
||||
esp_err_t simple_ble_stop();
|
||||
|
||||
/** Convert handle to UUID of characteristic
|
||||
*
|
||||
* This function can be easily used to get the corresponding
|
||||
* UUID for a characteristic that has been created, and the one for
|
||||
* which we only have the handle for.
|
||||
*
|
||||
* @return uuid the UUID of the handle, -1 in case of invalid handle
|
||||
*/
|
||||
uint16_t simple_ble_get_uuid(uint16_t handle);
|
||||
|
||||
#endif /* _SIMPLE_BLE_ */
|
||||
Reference in New Issue
Block a user