Skip to content

Commit

Permalink
ble_mesh: Support BLE advertising simultaneously
Browse files Browse the repository at this point in the history
  • Loading branch information
lly authored and espressif-bot committed Apr 2, 2020
1 parent 8210802 commit a919e20
Show file tree
Hide file tree
Showing 12 changed files with 729 additions and 26 deletions.
3 changes: 2 additions & 1 deletion components/bt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ if(CONFIG_BT_ENABLED)
"esp_ble_mesh/api/models/include"
"esp_ble_mesh/api")

list(APPEND srcs "esp_ble_mesh/api/core/esp_ble_mesh_common_api.c"
list(APPEND srcs "esp_ble_mesh/api/core/esp_ble_mesh_ble_api.c"
"esp_ble_mesh/api/core/esp_ble_mesh_common_api.c"
"esp_ble_mesh/api/core/esp_ble_mesh_local_data_operation_api.c"
"esp_ble_mesh/api/core/esp_ble_mesh_low_power_api.c"
"esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c"
Expand Down
18 changes: 18 additions & 0 deletions components/bt/esp_ble_mesh/Kconfig.in
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,24 @@ if BLE_MESH
SDU size is 60 bytes, which leaves 56 bytes for application layer data
using a 4-byte MIC, or 52 bytes using an 8-byte MIC.

config BLE_MESH_SUPPORT_BLE_ADV
bool "Support sending normal BLE advertising packets"
default n
help
When selected, users can send normal BLE advertising packets
with specific API.

if BLE_MESH_SUPPORT_BLE_ADV

config BLE_MESH_BLE_ADV_BUF_COUNT
int "Number of advertising buffers for BLE advertising packets"
default 3
range 1 255
help
Number of advertising buffers for BLE packets available.

endif # BLE_MESH_SUPPORT_BLE_ADV

config BLE_MESH_IVU_DIVIDER
int "Divider for IV Update state refresh timer"
default 4
Expand Down
73 changes: 73 additions & 0 deletions components/bt/esp_ble_mesh/api/core/esp_ble_mesh_ble_api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2017-2019 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 <stdint.h>
#include <string.h>

#include "btc/btc_manage.h"

#include "esp_err.h"

#include "btc_ble_mesh_prov.h"
#include "esp_ble_mesh_defs.h"

#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV

esp_err_t esp_ble_mesh_start_ble_advertising(const esp_ble_mesh_ble_adv_param_t *param,
const esp_ble_mesh_ble_adv_data_t *data)
{
btc_ble_mesh_prov_args_t arg = {0};
btc_msg_t msg = {0};

if (param == NULL) {
return ESP_ERR_INVALID_ARG;
}

ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);

msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_PROV;
msg.act = BTC_BLE_MESH_ACT_START_BLE_ADVERTISING;

memcpy(&arg.start_ble_advertising.param, param, sizeof(esp_ble_mesh_ble_adv_param_t));
if (data) {
memcpy(&arg.start_ble_advertising.data, data, sizeof(esp_ble_mesh_ble_adv_data_t));
}

return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL)
== BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}

esp_err_t esp_ble_mesh_stop_ble_advertising(uint8_t index)
{
btc_ble_mesh_prov_args_t arg = {0};
btc_msg_t msg = {0};

if (index >= CONFIG_BLE_MESH_BLE_ADV_BUF_COUNT) {
return ESP_ERR_INVALID_ARG;
}

ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);

msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_PROV;
msg.act = BTC_BLE_MESH_ACT_STOP_BLE_ADVERTISING;

arg.stop_ble_advertising.index = index;

return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL)
== BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}

#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */
57 changes: 57 additions & 0 deletions components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2017-2019 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 _ESP_BLE_MESH_BLE_API_H_
#define _ESP_BLE_MESH_BLE_API_H_

#include "esp_ble_mesh_defs.h"

/**
* @brief This function is called to start BLE advertising with the corresponding data
* and parameters while BLE Mesh is working at the same time.
*
* @note 1. When this function is called, the BLE advertising packet will be posted to
* the BLE mesh adv queue in the mesh stack and waited to be sent.
* 2. In the BLE advertising parameters, the "duration" means the time used for
* sending the BLE advertising packet each time, it shall not be smaller than the
* advertising interval. When the packet is sent successfully, it will be posted
* to the adv queue again after the "period" time if the "count" is bigger than 0.
* The "count" means how many durations the packet will be sent after it is sent
* successfully for the first time. And if the "count" is set to 0xFFFF, which
* means the packet will be sent infinitely.
* 3. The "priority" means the priority of BLE advertising packet compared with
* BLE Mesh packets. Currently two options (i.e. low/high) are provided. If the
* "priority" is high, the BLE advertising packet will be posted to the front of
* adv queue. Otherwise it will be posted to the back of adv queue.
*
* @param[in] param: Pointer to the BLE advertising parameters
* @param[in] data: Pointer to the BLE advertising data and scan response data
*
* @return ESP_OK on success or error code otherwise.
*
*/
esp_err_t esp_ble_mesh_start_ble_advertising(const esp_ble_mesh_ble_adv_param_t *param,
const esp_ble_mesh_ble_adv_data_t *data);

/**
* @brief This function is called to stop BLE advertising with the corresponding index.
*
* @param[in] index: Index of BLE advertising
*
* @return ESP_OK on success or error code otherwise.
*
*/
esp_err_t esp_ble_mesh_stop_ble_advertising(uint8_t index);

#endif /* _ESP_BLE_MESH_BLE_API_H_ */
46 changes: 46 additions & 0 deletions components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,36 @@ typedef enum {
PROXY_FILTER_BLACKLIST,
} esp_ble_mesh_proxy_filter_type_t;

/** Count for sending BLE advertising packet infinitely */
#define ESP_BLE_MESH_BLE_ADV_INFINITE 0xFFFF

/*!< This enum value is the priority of BLE advertising packet */
typedef enum {
ESP_BLE_MESH_BLE_ADV_PRIO_LOW,
ESP_BLE_MESH_BLE_ADV_PRIO_HIGH,
} esp_ble_mesh_ble_adv_priority_t;

/** Context of BLE advertising parameters. */
typedef struct {
uint16_t interval; /*!< BLE advertising interval */
uint8_t adv_type; /*!< BLE advertising type */
uint8_t own_addr_type; /*!< Own address type */
uint8_t peer_addr_type; /*!< Peer address type */
uint8_t peer_addr[BD_ADDR_LEN]; /*!< Peer address */
uint16_t duration; /*!< Duration is milliseconds */
uint16_t period; /*!< Period in milliseconds */
uint16_t count; /*!< Number of advertising duration */
uint8_t priority:2; /*!< Priority of BLE advertising packet */
} esp_ble_mesh_ble_adv_param_t;

/** Context of BLE advertising data. */
typedef struct {
uint8_t adv_data_len; /*!< Advertising data length */
uint8_t adv_data[31]; /*!< Advertising data */
uint8_t scan_rsp_data_len; /*!< Scan response data length */
uint8_t scan_rsp_data[31]; /*!< Scan response data */
} esp_ble_mesh_ble_adv_data_t;

/*!< This enum value is the event of node/provisioner/fast provisioning */
typedef enum {
ESP_BLE_MESH_PROV_REGISTER_COMP_EVT, /*!< Initialize BLE Mesh provisioning capabilities and internal data information completion event */
Expand Down Expand Up @@ -792,6 +822,8 @@ typedef enum {
ESP_BLE_MESH_PROXY_CLIENT_SET_FILTER_TYPE_COMP_EVT, /*!< Proxy Client set filter type completion event */
ESP_BLE_MESH_PROXY_CLIENT_ADD_FILTER_ADDR_COMP_EVT, /*!< Proxy Client add filter address completion event */
ESP_BLE_MESH_PROXY_CLIENT_REMOVE_FILTER_ADDR_COMP_EVT, /*!< Proxy Client remove filter address completion event */
ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT, /*!< Start BLE advertising completion event */
ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT, /*!< Stop BLE advertising completion event */
ESP_BLE_MESH_DEINIT_MESH_COMP_EVT, /*!< De-initialize BLE Mesh stack completion event */
ESP_BLE_MESH_PROV_EVT_MAX,
} esp_ble_mesh_prov_cb_event_t;
Expand Down Expand Up @@ -1260,6 +1292,20 @@ typedef union {
uint8_t conn_handle; /*!< Proxy connection handle */
uint16_t net_idx; /*!< Corresponding NetKey Index */
} proxy_client_remove_filter_addr_comp; /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_REMOVE_FILTER_ADDR_COMP_EVT */
/**
* @brief ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT
*/
struct ble_mesh_start_ble_advertising_comp_param {
int err_code; /*!< Indicate the result of starting BLE advertising */
uint8_t index; /*!< Index of the BLE advertising */
} start_ble_advertising_comp; /*!< Event parameter of ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT */
/**
* @brief ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT
*/
struct ble_mesh_stop_ble_advertising_comp_param {
int err_code; /*!< Indicate the result of stopping BLE advertising */
uint8_t index; /*!< Index of the BLE advertising */
} stop_ble_advertising_comp; /*!< Event parameter of ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT */
/**
* @brief ESP_BLE_MESH_DEINIT_MESH_COMP_EVT
*/
Expand Down
21 changes: 21 additions & 0 deletions components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"

#include "adv.h"
#include "mesh_proxy.h"
#include "mesh.h"
#include "access.h"
Expand Down Expand Up @@ -1879,6 +1880,26 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg)
break;
}
#endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */
#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV
case BTC_BLE_MESH_ACT_START_BLE_ADVERTISING: {
struct bt_mesh_ble_adv_param *set = (struct bt_mesh_ble_adv_param *)&arg->start_ble_advertising.param;
struct bt_mesh_ble_adv_data *data = NULL;
if (arg->start_ble_advertising.data.adv_data_len ||
arg->start_ble_advertising.data.scan_rsp_data_len) {
data = (struct bt_mesh_ble_adv_data *)&arg->start_ble_advertising.data;
}
act = ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT;
param.start_ble_advertising_comp.err_code =
bt_mesh_start_ble_advertising(set, data, &param.start_ble_advertising_comp.index);
break;
}
case BTC_BLE_MESH_ACT_STOP_BLE_ADVERTISING:
act = ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT;
param.stop_ble_advertising_comp.index = arg->stop_ble_advertising.index;
param.stop_ble_advertising_comp.err_code =
bt_mesh_stop_ble_advertising(arg->stop_ble_advertising.index);
break;
#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */
case BTC_BLE_MESH_ACT_DEINIT_MESH:
act = ESP_BLE_MESH_DEINIT_MESH_COMP_EVT;
param.deinit_mesh_comp.err_code = bt_mesh_deinit((struct bt_mesh_deinit_param *)&arg->mesh_deinit.param);
Expand Down
9 changes: 9 additions & 0 deletions components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ typedef enum {
BTC_BLE_MESH_ACT_PROXY_CLIENT_SET_FILTER_TYPE,
BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR,
BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR,
BTC_BLE_MESH_ACT_START_BLE_ADVERTISING,
BTC_BLE_MESH_ACT_STOP_BLE_ADVERTISING,
BTC_BLE_MESH_ACT_DEINIT_MESH,
} btc_ble_mesh_prov_act_t;

Expand Down Expand Up @@ -235,6 +237,13 @@ typedef union {
uint16_t addr_num;
uint16_t *addr;
} proxy_client_remove_filter_addr;
struct ble_mesh_start_ble_advertising_args {
esp_ble_mesh_ble_adv_param_t param;
esp_ble_mesh_ble_adv_data_t data;
} start_ble_advertising;
struct ble_mesh_stop_ble_advertising_args {
uint8_t index;
} stop_ble_advertising;
struct ble_mesh_deinit_args {
esp_ble_mesh_deinit_param_t param;
} mesh_deinit;
Expand Down
Loading

0 comments on commit a919e20

Please sign in to comment.