some modifications
- clang formatter - new NimBLE settings - BLE has more advertise data Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2
.clang-format
Normal file
2
.clang-format
Normal file
@@ -0,0 +1,2 @@
|
||||
---
|
||||
BasedOnStyle: Microsoft
|
17
.clangd
Normal file
17
.clangd
Normal file
@@ -0,0 +1,17 @@
|
||||
CompileFlags:
|
||||
Add:
|
||||
- -Wno-unknown-warning-option
|
||||
- -Wno-format
|
||||
Remove:
|
||||
- -mword-relocations
|
||||
|
||||
Diagnostics:
|
||||
ClangTidy:
|
||||
FastCheckFilter: None
|
||||
|
||||
---
|
||||
|
||||
If:
|
||||
PathMatch: .*\.h
|
||||
Diagnostics:
|
||||
UnusedIncludes: None
|
@@ -1,8 +1,8 @@
|
||||
#include "led_matrix.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "led_strip.h"
|
||||
#include "esp_log.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
typedef struct
|
||||
@@ -17,8 +17,7 @@ static void led_strip_init(uint8_t gpio_pin, uint32_t size)
|
||||
{
|
||||
led_matrix.size = size;
|
||||
|
||||
led_strip_config_t strip_config = {
|
||||
.strip_gpio_num = gpio_pin,
|
||||
led_strip_config_t strip_config = {.strip_gpio_num = gpio_pin,
|
||||
.max_leds = size,
|
||||
.led_model = LED_MODEL_WS2812,
|
||||
.color_component_format = LED_STRIP_COLOR_COMPONENT_FMT_RGB,
|
||||
@@ -26,8 +25,7 @@ static void led_strip_init(uint8_t gpio_pin, uint32_t size)
|
||||
.invert_out = false,
|
||||
}};
|
||||
|
||||
led_strip_rmt_config_t rmt_config = {
|
||||
.clk_src = RMT_CLK_SRC_DEFAULT,
|
||||
led_strip_rmt_config_t rmt_config = {.clk_src = RMT_CLK_SRC_DEFAULT,
|
||||
.resolution_hz = 0,
|
||||
.mem_block_symbols = 0,
|
||||
.flags = {
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include "host/ble_hs.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int ds_model_number_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||
int ds_manufacturer_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||
|
@@ -1,7 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include "host/ble_hs.h"
|
||||
#include <stdio.h>
|
||||
|
||||
/// LED Service Characteristic Callbacks
|
||||
int ls_write(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||
int ls_read(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||
int ls_capabilities_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||
|
||||
/// LED Service Characteristic User Description
|
||||
int ls_char_a000_user_desc_read(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt,
|
||||
void *arg);
|
||||
int ls_char_dead_user_desc_read(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt,
|
||||
void *arg);
|
||||
|
@@ -4,6 +4,14 @@
|
||||
|
||||
static const char *TAG = "led_service";
|
||||
|
||||
/// Capabilities of Device
|
||||
int ls_capabilities_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||
{
|
||||
char *data = "To be implemented later";
|
||||
os_mbuf_append(ctxt->om, data, strlen(data));
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Write data to ESP32 defined as server
|
||||
int ls_write(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||
{
|
||||
@@ -16,8 +24,7 @@ int ls_write(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_
|
||||
const char CMD_FAN_ON[] = "FAN ON";
|
||||
const char CMD_FAN_OFF[] = "FAN OFF";
|
||||
|
||||
if (payload_len == (sizeof(CMD_LIGHT_ON) - 1) &&
|
||||
strncmp(received_payload, CMD_LIGHT_ON, payload_len) == 0)
|
||||
if (payload_len == (sizeof(CMD_LIGHT_ON) - 1) && strncmp(received_payload, CMD_LIGHT_ON, payload_len) == 0)
|
||||
{
|
||||
ESP_LOGI(TAG, "LIGHT ON");
|
||||
for (int i = 0; i < led_matrix_get_size(); i++)
|
||||
@@ -25,8 +32,7 @@ int ls_write(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_
|
||||
led_matrix_set_pixel(i, 10, 10, 0);
|
||||
}
|
||||
}
|
||||
else if (payload_len == (sizeof(CMD_LIGHT_OFF) - 1) &&
|
||||
strncmp(received_payload, CMD_LIGHT_OFF, payload_len) == 0)
|
||||
else if (payload_len == (sizeof(CMD_LIGHT_OFF) - 1) && strncmp(received_payload, CMD_LIGHT_OFF, payload_len) == 0)
|
||||
{
|
||||
ESP_LOGI(TAG, "LIGHT OFF");
|
||||
for (int i = 0; i < led_matrix_get_size(); i++)
|
||||
@@ -34,14 +40,12 @@ int ls_write(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_
|
||||
led_matrix_set_pixel(i, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
else if (payload_len == (sizeof(CMD_FAN_ON) - 1) &&
|
||||
strncmp(received_payload, CMD_FAN_ON, payload_len) == 0)
|
||||
else if (payload_len == (sizeof(CMD_FAN_ON) - 1) && strncmp(received_payload, CMD_FAN_ON, payload_len) == 0)
|
||||
{
|
||||
ESP_LOGI(TAG, "FAN ON");
|
||||
// TODO: Implement action for FAN ON
|
||||
}
|
||||
else if (payload_len == (sizeof(CMD_FAN_OFF) - 1) &&
|
||||
strncmp(received_payload, CMD_FAN_OFF, payload_len) == 0)
|
||||
else if (payload_len == (sizeof(CMD_FAN_OFF) - 1) && strncmp(received_payload, CMD_FAN_OFF, payload_len) == 0)
|
||||
{
|
||||
ESP_LOGI(TAG, "FAN OFF");
|
||||
// TODO: Implement action for FAN OFF
|
||||
@@ -58,10 +62,18 @@ int ls_write(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Read data from ESP32 defined as server
|
||||
int ls_read(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||
int ls_char_a000_user_desc_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt,
|
||||
void *arg)
|
||||
{
|
||||
char *data = "Data from the server";
|
||||
os_mbuf_append(ctxt->om, data, strlen(data));
|
||||
const char *desc = "Capabilities of Device";
|
||||
os_mbuf_append(ctxt->om, desc, strlen(desc));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ls_char_dead_user_desc_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt,
|
||||
void *arg)
|
||||
{
|
||||
const char *desc = "Readable Data from Server";
|
||||
os_mbuf_append(ctxt->om, desc, strlen(desc));
|
||||
return 0;
|
||||
}
|
||||
|
@@ -1,41 +1,75 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_nimble_hci.h"
|
||||
#include "nimble/nimble_port.h"
|
||||
#include "nimble/nimble_port_freertos.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "freertos/task.h"
|
||||
#include "host/ble_hs.h"
|
||||
#include "host/ble_sm.h"
|
||||
#include "services/gap/ble_svc_gap.h"
|
||||
#include "services/gatt/ble_svc_gatt.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#include "led_matrix.h"
|
||||
|
||||
#include "host/ble_uuid.h"
|
||||
#include "include/device_service.h"
|
||||
#include "include/led_service.h"
|
||||
#include "led_matrix.h"
|
||||
#include "nimble/nimble_port.h"
|
||||
#include "nimble/nimble_port_freertos.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "services/gap/ble_svc_gap.h"
|
||||
#include "services/gatt/ble_svc_gatt.h"
|
||||
|
||||
static const char *TAG = "remote_control";
|
||||
|
||||
static const ble_uuid16_t device_service_uuid = BLE_UUID16_INIT(0x180A);
|
||||
static const ble_uuid128_t led_service_uuid128 =
|
||||
BLE_UUID128_INIT(0x91, 0xB6, 0xCA, 0x95, 0xB2, 0xC6, 0x7B, 0x90, 0x31, 0x45, 0x77, 0xE6, 0x67, 0x10, 0x68, 0xB9);
|
||||
|
||||
uint8_t ble_addr_type;
|
||||
|
||||
void ble_app_advertise(void);
|
||||
|
||||
static struct ble_gatt_dsc_def char_0xA000_descs[] = {{
|
||||
.uuid = BLE_UUID16_DECLARE(0x2901),
|
||||
.att_flags = BLE_ATT_F_READ,
|
||||
.access_cb = ls_char_a000_user_desc_read,
|
||||
},
|
||||
{0}};
|
||||
|
||||
static struct ble_gatt_dsc_def char_0xDEAD_descs[] = {{
|
||||
.uuid = BLE_UUID16_DECLARE(0x2901),
|
||||
.att_flags = BLE_ATT_F_WRITE,
|
||||
.access_cb = ls_char_dead_user_desc_read,
|
||||
},
|
||||
{0}};
|
||||
|
||||
// Array of pointers to other service definitions
|
||||
static const struct ble_gatt_svc_def gatt_svcs[] = {
|
||||
{
|
||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||
.uuid = BLE_UUID16_DECLARE(0x180A),
|
||||
.characteristics = (struct ble_gatt_chr_def[]){{.uuid = BLE_UUID16_DECLARE(0x2A24), .flags = BLE_GATT_CHR_F_READ, .access_cb = ds_model_number_read}, {.uuid = BLE_UUID16_DECLARE(0x2A29), .flags = BLE_GATT_CHR_F_READ, .access_cb = ds_manufacturer_read}, {0}},
|
||||
.uuid = &device_service_uuid.u,
|
||||
.characteristics =
|
||||
(struct ble_gatt_chr_def[]){
|
||||
{.uuid = BLE_UUID16_DECLARE(0x2A24), .flags = BLE_GATT_CHR_F_READ, .access_cb = ds_model_number_read},
|
||||
{.uuid = BLE_UUID16_DECLARE(0x2A29), .flags = BLE_GATT_CHR_F_READ, .access_cb = ds_manufacturer_read},
|
||||
{0}},
|
||||
},
|
||||
{
|
||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||
.uuid = BLE_UUID16_DECLARE(0x180),
|
||||
.characteristics = (struct ble_gatt_chr_def[]){{.uuid = BLE_UUID16_DECLARE(0xFEF4), .flags = BLE_GATT_CHR_F_READ, .access_cb = ls_read}, {.uuid = BLE_UUID16_DECLARE(0xDEAD), .flags = BLE_GATT_CHR_F_WRITE, .access_cb = ls_write}, {0}},
|
||||
.uuid = &led_service_uuid128.u,
|
||||
.characteristics = (struct ble_gatt_chr_def[]){{
|
||||
.uuid = BLE_UUID16_DECLARE(0xA000),
|
||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
|
||||
.access_cb = ls_capabilities_read,
|
||||
.descriptors = char_0xA000_descs,
|
||||
},
|
||||
{
|
||||
.uuid = BLE_UUID16_DECLARE(0xDEAD),
|
||||
.flags = BLE_GATT_CHR_F_WRITE,
|
||||
.access_cb = ls_write,
|
||||
.descriptors = char_0xDEAD_descs,
|
||||
},
|
||||
{0}},
|
||||
},
|
||||
{0}};
|
||||
|
||||
@@ -46,13 +80,7 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg)
|
||||
{
|
||||
case BLE_GAP_EVENT_CONNECT:
|
||||
ESP_LOGI(TAG, "BLE GAP EVENT CONNECT %s", event->connect.status == 0 ? "OK!" : "FAILED!");
|
||||
if (event->connect.status == 0)
|
||||
{
|
||||
// Start security pairing without disconnecting
|
||||
int ret = ble_gap_security_initiate(event->connect.conn_handle);
|
||||
ESP_LOGI(TAG, "BLE GAP SECURITY INITIATE %s", ret == 0 ? "OK!" : "FAILED!");
|
||||
}
|
||||
else
|
||||
if (event->connect.status != 0)
|
||||
{
|
||||
// Re-advertise if connection failed
|
||||
ble_app_advertise();
|
||||
@@ -71,17 +99,6 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg)
|
||||
ble_app_advertise();
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_ENC_CHANGE:
|
||||
if (event->enc_change.status == 0)
|
||||
{
|
||||
ESP_LOGI(TAG, "Encryption enabled for connection");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to enable encryption, status=%d", event->enc_change.status);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -93,12 +110,18 @@ void ble_app_advertise(void)
|
||||
{
|
||||
// GAP - device name definition
|
||||
struct ble_hs_adv_fields fields;
|
||||
struct ble_hs_adv_fields scan_rsp_fields;
|
||||
const char *device_name;
|
||||
int ret;
|
||||
|
||||
memset(&fields, 0, sizeof(fields));
|
||||
device_name = ble_svc_gap_device_name(); // Read the BLE device name
|
||||
fields.name = (uint8_t *)device_name;
|
||||
fields.name_len = strlen(device_name);
|
||||
fields.name_is_complete = 1;
|
||||
|
||||
// GAP - advertising definition
|
||||
fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
|
||||
fields.uuids128 = (ble_uuid128_t[]){led_service_uuid128};
|
||||
fields.num_uuids128 = 1;
|
||||
fields.uuids128_is_complete = 1;
|
||||
|
||||
ble_gap_adv_set_fields(&fields);
|
||||
|
||||
// GAP - device connectivity definition
|
||||
@@ -106,7 +129,32 @@ void ble_app_advertise(void)
|
||||
memset(&adv_params, 0, sizeof(adv_params));
|
||||
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; // connectable or non-connectable
|
||||
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; // discoverable or non-discoverable
|
||||
ble_gap_adv_start(ble_addr_type, NULL, BLE_HS_FOREVER, &adv_params, ble_gap_event, NULL);
|
||||
ret = ble_gap_adv_start(ble_addr_type, NULL, BLE_HS_FOREVER, &adv_params, ble_gap_event, NULL);
|
||||
if (ret != 0)
|
||||
{
|
||||
ESP_LOGE(TAG, "Advertising failed to start (err %d)", ret);
|
||||
return;
|
||||
}
|
||||
|
||||
// --- Configure Scan Response Data (SCAN_RSP) ---
|
||||
memset(&scan_rsp_fields, 0, sizeof(scan_rsp_fields));
|
||||
|
||||
// Get the device name
|
||||
device_name = ble_svc_gap_device_name();
|
||||
scan_rsp_fields.name = (uint8_t *)device_name;
|
||||
scan_rsp_fields.name_len = strlen(device_name);
|
||||
scan_rsp_fields.name_is_complete = 1;
|
||||
|
||||
// Optionally, add TX power level to scan response
|
||||
scan_rsp_fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
|
||||
scan_rsp_fields.tx_pwr_lvl_is_present = 1;
|
||||
|
||||
ret = ble_gap_adv_rsp_set_fields(&scan_rsp_fields);
|
||||
if (ret != 0)
|
||||
{
|
||||
ESP_LOGE(TAG, "Error setting scan response data; rc=%d", ret);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// The application
|
||||
@@ -119,7 +167,8 @@ void ble_app_on_sync(void)
|
||||
// The infinite task
|
||||
void host_task(void *param)
|
||||
{
|
||||
nimble_port_run(); // This function will return only when nimble_port_stop() is executed
|
||||
nimble_port_run(); // This function will return only when nimble_port_stop()
|
||||
// is executed
|
||||
}
|
||||
|
||||
void ble_init(void *args)
|
||||
@@ -132,12 +181,6 @@ void ble_init(void *args)
|
||||
ble_gatts_add_svcs(gatt_svcs);
|
||||
ble_hs_cfg.sync_cb = ble_app_on_sync;
|
||||
|
||||
// Configure security settings
|
||||
ble_hs_cfg.sm_bonding = 1;
|
||||
ble_hs_cfg.sm_sc = 0;
|
||||
ble_hs_cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
|
||||
ble_hs_cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
|
||||
|
||||
nimble_port_freertos_init(host_task); // Run the host task
|
||||
|
||||
xTaskCreatePinnedToCore(led_matrix_init, "led_matrix", configMINIMAL_STACK_SIZE * 2, NULL, 5, NULL, 1);
|
||||
|
@@ -2,7 +2,8 @@
|
||||
CONFIG_BT_ENABLED=y
|
||||
CONFIG_BT_NIMBLE_ENABLED=y
|
||||
|
||||
# nimble options
|
||||
# NimBLE Options
|
||||
CONFIG_BT_NIMBLE_SECURITY_ENABLE=n
|
||||
CONFIG_BT_NIMBLE_SVC_GAP_DEVICE_NAME="miniature"
|
||||
|
||||
# Logging
|
||||
|
Reference in New Issue
Block a user