8000 sync · ICELUCKBR/esp32-snippets@c9d2d20 · GitHub
[go: up one dir, main page]

Skip to content

Commit c9d2d20

Browse files
author
kolban
committed
sync
1 parent 9e423b9 commit c9d2d20

20 files changed

+1256
-232
lines changed

cpp_utils/BLE.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "BLEUtils.h"
2424
#include "BLE.h"
2525
#include "BLEDevice.h"
26-
#include "BLECharacteristic.h"
26+
#include "BLEXXXCharacteristic.h"
2727

2828
static char tag[] = "BLE";
2929

@@ -177,12 +177,13 @@ static void dump_adv_payload(uint8_t *payload) {
177177
* @param [in] param
178178
*/
179179
void gatt_server_event_handler(
180-
esp_gatts_cb_event_t event,
181-
esp_gatt_if_t gatts_if,
180+
< 8000 span class="pl-c1">esp_gatts_cb_event_t event,
181+
esp_gatt_if_t gatts_if,
182182
esp_ble_gatts_cb_param_t *param
183183
) {
184184
ESP_LOGD(tag, "gatt_server_event_handler [esp_gatt_if: %d] ... %s",
185-
gatts_if, bt_utils_gatt_server_event_type_to_string(event).c_str());
185+
gatts_if,
186+
bt_utils_gatt_server_event_type_to_string(event).c_str());
186187
BLEUtils::dumpGattServerEvent(event, gatts_if, param);
187188
if (BLE::m_bleServer != nullptr) {
188189
BLE::m_bleServer->handleGATTServerEvent(event, gatts_if, param);
@@ -253,7 +254,7 @@ static void gatt_client_event_handler(
253254
case ESP_GATTC_GET_CHAR_EVT: {
254255
if (param->get_char.status == ESP_GATT_OK) {
255256
BLEDevice *pDevice = BLEUtils::findByConnId(param->get_char.conn_id);
256-
BLECharacteristic characteristic(param->get_char.conn_id,
257+
BLECharacteristicXXX characteristic(param->get_char.conn_id,
257258
param->get_char.srvc_id, param->get_char.char_id, param->get_char.char_prop);
258259
pDevice->onCharacteristic(characteristic);
259260
}
@@ -358,11 +359,11 @@ std::map<ble_address, BLEDevice> getDevices() {
358359
return g_devices;
359360
} // getDevices
360361

362+
361363
/**
362-
* @brief Initialize the server %BLE enevironment.
364+
* @brief Initialize the server %BLE environment.
363365
*
364366
*/
365-
366367
void BLE::initServer() {
367368
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
368369
esp_err_t errRc = esp_bt_controller_init(&bt_cfg);

cpp_utils/BLEAdvertising.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* BLEAdvertising.cpp
3+
*
4+
* Created on: Jun 21, 2017
5+
* Author: kolban
6+
*/
7+
8+
#include "BLEAdvertising.h"
9+
#include <esp_log.h>
10+
#include <esp_err.h>
11+
12+
static char TAG[] = "BLEAdvertising";
13+
14+
extern "C" {
15+
char *espToString(esp_err_t value);
16+
}
17+
18+
BLEAdvertising::BLEAdvertising() {
19+
// TODO Auto-generated constructor stub
20+
m_advData.set_scan_rsp = false;
21+
m_advData.include_name = true;
22+
m_advData.include_txpower = true;
23+
m_advData.min_interval = 0x20;
24+
m_advData.max_interval = 0x40;
25+
m_advData.appearance = 0x00;
26+
m_advData.manufacturer_len = 0;
27+
m_advData.p_manufacturer_data = nullptr;
28+
m_advData.service_data_len = 0;
29+
m_advData.p_service_data = nullptr;
30+
m_advData.service_uuid_len = 0;
31+
m_advData.p_service_uuid = nullptr;
32+
m_advData.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT);
33+
34+
m_advParams.adv_int_min = 0x20;
35+
m_advParams.adv_int_max = 0x40;
36+
m_advParams.adv_type = ADV_TYPE_IND;
37+
m_advParams.own_addr_type = BLE_ADDR_TYPE_PUBLIC;
38+
m_advParams.channel_map = ADV_CHNL_ALL;
39+
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
40+
} // BLEAdvertising
41+
42+
BLEAdvertising::~BLEAdvertising() {
43+
// TODO Auto-generated destructor stub
44+
}
45+
46+
47+
/**
48+
* @brief Start advertising.
49+
* Start advertising.
50+
* @return N/A.
51+
*/
52+
void BLEAdvertising::start() {
53+
// Set the configuration for advertising.
54+
esp_err_t errRc = ::esp_ble_gap_config_adv_data(&m_advData);
55+
if (errRc != ESP_OK) {
56+
ESP_LOGE(TAG, "esp_ble_gap_config_adv_data: rc=%d %s", errRc, espToString(errRc));
57+
return;
58+
}
59+
60+
// Start advertising.
61+
errRc = ::esp_ble_gap_start_advertising(&m_advParams);
62+
if (errRc != ESP_OK) {
63+
ESP_LOGE(TAG, "esp_ble_gap_start_advertising: rc=%d %s", errRc, espToString(errRc));
64+
return;
65+
}
66+
} // advertising
67+
68+
69+
/**
70+
* @brief Stop advertising.
71+
* Stop advertising.
72+
* @return N/A.
73+
*/
74+
void BLEAdvertising::stop() {
75+
esp_err_t errRc = ::esp_ble_gap_stop_advertising();
76+
if (errRc != ESP_OK) {
77+
ESP_LOGE(TAG, "esp_ble_gap_stop_advertising: rc=%d %s", errRc, espToString(errRc));
78+
return;
79+
}
80+
} // stop
81+
82+
83+
/**
84+
* @brief Set the device appearance in the advertising data.
85+
* The apperance attribute is of type 0x19. The codes for distinct appearances can be found here:
86+
* https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.gap.appearance.xml.
87+
* @param [in] appearance The appearance of the device in the advertising data.
88+
* @return N/A.
89+
*/
90+
void BLEAdvertising::setAppearance(uint16_t appearance) {
91+
m_advData.appearance = appearance;
92+
} // setAppearance

cpp_utils/BLEAdvertising.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* BLEAdvertising.h
3+
*
4+
* Created on: Jun 21, 2017
5+
* Author: kolban
6+
*/
7+
8+
#ifndef COMPONENTS_CPP_UTILS_BLEADVERTISING_H_
9+
#define COMPONENTS_CPP_UTILS_BLEADVERTISING_H_
10+
#include <esp_gap_ble_api.h>
11+
12+
class BLEAdvertising {
13+
public:
14+
BLEAdvertising();
15+
virtual ~BLEAdvertising();
16+
void start();
17+
void stop();
18+
void setAppearance(uint16_t appearance);
19+
private:
20+
esp_ble_adv_data_t m_advData;
21+
esp_ble_adv_params_t m_advParams;
22+
};
23+
24+
#endif /* COMPONENTS_CPP_UTILS_BLEADVERTISING_H_ */

0 commit comments

Comments
 (0)
0