1
0
mirror of https://github.com/esphome/esphome.git synced 2025-08-09 07:39:36 +01:00
Files
.devcontainer
.github
docker
esphome
api
components
a4988
ac_dimmer
adalight
adc
ade7953
ads1115
aht10
am2320
apds9960
api
as3935
as3935_i2c
as3935_spi
async_tcp
atm90e32
bang_bang
bh1750
binary
binary_sensor
binary_sensor_map
ble_presence
ble_rssi
ble_scanner
bme280
bme680
bmp085
bmp280
captive_portal
ccs811
climate
climate_ir
climate_ir_lg
color
coolix
cover
cse7766
ct_clamp
custom
custom_component
cwww
daikin
dallas
debug
deep_sleep
dfplayer
dht
dht12
display
duty_cycle
e131
endstop
esp32_ble_beacon
__init__.py
esp32_ble_beacon.cpp
esp32_ble_beacon.h
esp32_ble_tracker
esp32_camera
esp32_dac
esp32_hall
esp32_touch
esp8266_pwm
ethernet
exposure_notifications
fan
fastled_base
fastled_clockless
fastled_spi
font
fujitsu_general
globals
gpio
gps
hdc1080
hlw8012
hm3301
hmc5883l
homeassistant
http_request
htu21d
hx711
i2c
image
ina219
ina226
ina3221
integration
interval
json
lcd_base
lcd_gpio
lcd_pcf8574
ledc
light
logger
max31855
max31856
max31865
max6675
max7219
max7219digit
mcp23008
mcp23016
mcp23017
mcp3008
mhz19
mitsubishi
modbus
monochromatic
mpr121
mpu6050
mqtt
mqtt_subscribe
ms5611
my9231
neopixelbus
network
nextion
ntc
ota
output
packages
partition
pca9685
pcd8544
pcf8574
pid
pmsx003
pn532
power_supply
prometheus
pulse_counter
pulse_width
pzem004t
pzemac
pzemdc
qmc5883l
rdm6300
remote_base
remote_receiver
remote_transmitter
resistance
restart
rf_bridge
rgb
rgbw
rgbww
rotary_encoder
rtttl
ruuvi_ble
ruuvitag
scd30
script
sds011
senseair
sensor
servo
sgp30
sht3xd
shtcx
shutdown
sim800l
slow_pwm
sm16716
sn74hc595
sntp
speed
spi
sps30
ssd1306_base
ssd1306_i2c
ssd1306_spi
ssd1325_base
ssd1325_spi
ssd1351_base
ssd1351_spi
st7789v
status
status_led
stepper
sts3x
substitutions
sun
switch
sx1509
tcl112
tcs34725
template
text_sensor
thermostat
time
time_based
tlc59208f
tm1637
tm1651
tmp117
toshiba
total_daily_energy
tsl2561
ttp229_bsf
ttp229_lsf
tuya
tx20
uart
uln2003
ultrasonic
uptime
version
vl53l0x
voltage_sampler
waveshare_epaper
web_server
web_server_base
whirlpool
wifi
wifi_info
wifi_signal
wled
xiaomi_ble
xiaomi_cgd1
xiaomi_cgg1
xiaomi_gcls002
xiaomi_hhccjcy01
xiaomi_hhccpot002
xiaomi_jqjcy01ym
xiaomi_lywsd02
xiaomi_lywsd03mmc
xiaomi_lywsdcgq
xiaomi_miflora
xiaomi_mijia
xiaomi_mjyd02yla
xiaomi_mue4094rt
xiaomi_wx08zm
yashima
zyaura
__init__.py
core
dashboard
__init__.py
__main__.py
automation.py
codegen.py
config.py
config_helpers.py
config_validation.py
const.py
core.py
core_config.py
cpp_generator.py
cpp_helpers.py
cpp_types.py
espota2.py
helpers.py
legacy.py
mqtt.py
pins.py
platformio_api.py
storage_json.py
util.py
voluptuous_schema.py
vscode.py
wizard.py
writer.py
yaml_util.py
zeroconf.py
script
tests
.clang-format
.clang-tidy
.coveragerc
.dockerignore
.editorconfig
.gitignore
.gitpod.yml
.pre-commit-config.yaml
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE
MANIFEST.in
README.md
platformio.ini
pylintrc
pytest.ini
requirements.txt
requirements_test.txt
setup.cfg
setup.py
esphome/esphome/components/esp32_ble_beacon/esp32_ble_beacon.cpp
2019-10-24 22:02:29 +02:00

140 lines
4.3 KiB
C++

#include "esp32_ble_beacon.h"
#include "esphome/core/log.h"
#ifdef ARDUINO_ARCH_ESP32
#include <nvs_flash.h>
#include <freertos/FreeRTOSConfig.h>
#include <esp_bt_main.h>
#include <esp_bt.h>
#include <freertos/task.h>
#include <esp_gap_ble_api.h>
namespace esphome {
namespace esp32_ble_beacon {
static const char *TAG = "esp32_ble_beacon";
static esp_ble_adv_params_t ble_adv_params = {
.adv_int_min = 0x20,
.adv_int_max = 0x40,
.adv_type = ADV_TYPE_NONCONN_IND,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
.peer_addr = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
.peer_addr_type = BLE_ADDR_TYPE_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
#define ENDIAN_CHANGE_U16(x) ((((x) &0xFF00) >> 8) + (((x) &0xFF) << 8))
static esp_ble_ibeacon_head_t ibeacon_common_head = {
.flags = {0x02, 0x01, 0x06}, .length = 0x1A, .type = 0xFF, .company_id = 0x004C, .beacon_type = 0x1502};
void ESP32BLEBeacon::dump_config() {
ESP_LOGCONFIG(TAG, "ESP32 BLE Beacon:");
ESP_LOGCONFIG(TAG, " Major: %u, Minor: %u", this->major_, this->minor_);
}
void ESP32BLEBeacon::setup() {
ESP_LOGCONFIG(TAG, "Setting up ESP32 BLE beacon...");
global_esp32_ble_beacon = this;
xTaskCreatePinnedToCore(ESP32BLEBeacon::ble_core_task,
"ble_task", // name
10000, // stack size (in words)
nullptr, // input params
1, // priority
nullptr, // Handle, not needed
0 // core
);
}
float ESP32BLEBeacon::get_setup_priority() const { return setup_priority::DATA; }
void ESP32BLEBeacon::ble_core_task(void *params) {
ble_setup();
while (true) {
delay(1000); // NOLINT
}
}
void ESP32BLEBeacon::ble_setup() {
// Initialize non-volatile storage for the bluetooth controller
esp_err_t err = nvs_flash_init();
if (err != ESP_OK) {
ESP_LOGE(TAG, "nvs_flash_init failed: %d", err);
return;
}
if (!btStart()) {
ESP_LOGE(TAG, "btStart failed: %d", esp_bt_controller_get_status());
return;
}
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
err = esp_bluedroid_init();
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_bluedroid_init failed: %d", err);
return;
}
err = esp_bluedroid_enable();
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_bluedroid_enable failed: %d", err);
return;
}
err = esp_ble_gap_register_callback(ESP32BLEBeacon::gap_event_handler);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_ble_gap_register_callback failed: %d", err);
return;
}
esp_ble_ibeacon_t ibeacon_adv_data;
memcpy(&ibeacon_adv_data.ibeacon_head, &ibeacon_common_head, sizeof(esp_ble_ibeacon_head_t));
memcpy(&ibeacon_adv_data.ibeacon_vendor.proximity_uuid, global_esp32_ble_beacon->uuid_.data(),
sizeof(ibeacon_adv_data.ibeacon_vendor.proximity_uuid));
ibeacon_adv_data.ibeacon_vendor.minor = ENDIAN_CHANGE_U16(global_esp32_ble_beacon->minor_);
ibeacon_adv_data.ibeacon_vendor.major = ENDIAN_CHANGE_U16(global_esp32_ble_beacon->major_);
ibeacon_adv_data.ibeacon_vendor.measured_power = 0xC5;
esp_ble_gap_config_adv_data_raw((uint8_t *) &ibeacon_adv_data, sizeof(ibeacon_adv_data));
}
void ESP32BLEBeacon::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
esp_err_t err;
switch (event) {
case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT: {
err = esp_ble_gap_start_advertising(&ble_adv_params);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_ble_gap_start_advertising failed: %d", err);
}
break;
}
case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: {
err = param->adv_start_cmpl.status;
if (err != ESP_BT_STATUS_SUCCESS) {
ESP_LOGE(TAG, "BLE adv start failed: %s", esp_err_to_name(err));
}
break;
}
case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: {
err = param->adv_start_cmpl.status;
if (err != ESP_BT_STATUS_SUCCESS) {
ESP_LOGE(TAG, "BLE adv stop failed: %s", esp_err_to_name(err));
} else {
ESP_LOGD(TAG, "BLE stopped advertising successfully");
}
break;
}
default:
break;
}
}
ESP32BLEBeacon *global_esp32_ble_beacon = nullptr;
} // namespace esp32_ble_beacon
} // namespace esphome
#endif