more characteristics and descriptors
Signed-off-by: Peter Siegmund <mars3142@users.noreply.github.com>
This commit is contained in:
@@ -28,24 +28,27 @@ static bool IRAM_ATTR beacon_timer_callback(gptimer_handle_t timer, const gptime
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void led_refresh(uint32_t brightness)
|
||||||
|
{
|
||||||
|
LedMatrix_t led_matrix = get_led_matrix();
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < led_matrix.size; i++)
|
||||||
|
{
|
||||||
|
led_strip_set_pixel(led_matrix.led_strip, i, 0, brightness, 0);
|
||||||
|
}
|
||||||
|
led_strip_refresh(led_matrix.led_strip);
|
||||||
|
}
|
||||||
|
|
||||||
static void beacon_timer_event_task(void *arg)
|
static void beacon_timer_event_task(void *arg)
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (xSemaphoreTake(timer_semaphore, portMAX_DELAY))
|
if (xSemaphoreTake(timer_semaphore, portMAX_DELAY))
|
||||||
{
|
{
|
||||||
LedMatrix_t led_matrix = get_led_matrix();
|
|
||||||
|
|
||||||
static bool level = false;
|
static bool level = false;
|
||||||
level = !level;
|
level = !level;
|
||||||
if (led_matrix.led_strip)
|
led_refresh(level ? value : 0);
|
||||||
{
|
|
||||||
for (uint32_t i = 0; i < led_matrix.size; i++)
|
|
||||||
{
|
|
||||||
led_strip_set_pixel(led_matrix.led_strip, i, 0, (level) ? value : 0, 0);
|
|
||||||
}
|
|
||||||
led_strip_refresh(led_matrix.led_strip);
|
|
||||||
}
|
|
||||||
ESP_LOGD(TAG, "Timer Event, LED now %s", level ? "ON" : "OFF");
|
ESP_LOGD(TAG, "Timer Event, LED now %s", level ? "ON" : "OFF");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,7 +61,21 @@ esp_err_t beacon_start(void)
|
|||||||
ESP_LOGE(TAG, "GPTimer not initialized");
|
ESP_LOGE(TAG, "GPTimer not initialized");
|
||||||
return ESP_ERR_INVALID_STATE;
|
return ESP_ERR_INVALID_STATE;
|
||||||
}
|
}
|
||||||
esp_err_t ret = gptimer_start(gptimer);
|
esp_err_t ret = gptimer_enable(gptimer);
|
||||||
|
if (ret != ESP_OK)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "Failed to enable gptimer: %s", esp_err_to_name(ret));
|
||||||
|
return beacon_stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = gptimer_set_raw_count(gptimer, 0);
|
||||||
|
if (ret != ESP_OK)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "Failed to set gptimer raw count: %s", esp_err_to_name(ret));
|
||||||
|
return beacon_stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = gptimer_start(gptimer);
|
||||||
if (ret != ESP_OK)
|
if (ret != ESP_OK)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "Failed to start gptimer: %s", esp_err_to_name(ret));
|
ESP_LOGE(TAG, "Failed to start gptimer: %s", esp_err_to_name(ret));
|
||||||
@@ -72,6 +89,7 @@ esp_err_t beacon_start(void)
|
|||||||
|
|
||||||
esp_err_t beacon_stop(void)
|
esp_err_t beacon_stop(void)
|
||||||
{
|
{
|
||||||
|
led_refresh(0);
|
||||||
if (gptimer == NULL)
|
if (gptimer == NULL)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "GPTimer not initialized");
|
ESP_LOGE(TAG, "GPTimer not initialized");
|
||||||
@@ -86,6 +104,12 @@ esp_err_t beacon_stop(void)
|
|||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "GPTimer stopped.");
|
ESP_LOGI(TAG, "GPTimer stopped.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ret = gptimer_disable(gptimer);
|
||||||
|
if (ret != ESP_OK)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "Failed to enable gptimer: %s", esp_err_to_name(ret));
|
||||||
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,13 +139,6 @@ esp_err_t beacon_init(void)
|
|||||||
goto cleanupTimer;
|
goto cleanupTimer;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = gptimer_enable(gptimer);
|
|
||||||
if (ret != ESP_OK)
|
|
||||||
{
|
|
||||||
ESP_LOGE(TAG, "Failed to enable gptimer: %s", esp_err_to_name(ret));
|
|
||||||
goto cleanupTimer;
|
|
||||||
}
|
|
||||||
|
|
||||||
gptimer_alarm_config_t alarm_config = {
|
gptimer_alarm_config_t alarm_config = {
|
||||||
.alarm_count = 2000000,
|
.alarm_count = 2000000,
|
||||||
.reload_count = 0,
|
.reload_count = 0,
|
||||||
|
@@ -6,4 +6,5 @@ idf_component_register(SRCS
|
|||||||
PRIV_REQUIRES
|
PRIV_REQUIRES
|
||||||
bt
|
bt
|
||||||
esp_app_format
|
esp_app_format
|
||||||
|
light
|
||||||
)
|
)
|
||||||
|
@@ -3,7 +3,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
int device_name_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
int device_name_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||||
{
|
{
|
||||||
char firmware_revision_str[33];
|
char firmware_revision_str[33];
|
||||||
const esp_app_desc_t *app_desc = esp_app_get_description();
|
const esp_app_desc_t *app_desc = esp_app_get_description();
|
||||||
@@ -21,8 +21,8 @@ int device_name_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int device_firmware_revision_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt,
|
int device_firmware_revision_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt,
|
||||||
void *arg)
|
void *arg)
|
||||||
{
|
{
|
||||||
char firmware_revision_str[33];
|
char firmware_revision_str[33];
|
||||||
const esp_app_desc_t *app_desc = esp_app_get_description();
|
const esp_app_desc_t *app_desc = esp_app_get_description();
|
||||||
@@ -40,15 +40,15 @@ int device_firmware_revision_read(uint16_t conn_handle, uint16_t attr_handle, st
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int device_hardware_revision_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt,
|
int device_hardware_revision_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt,
|
||||||
void *arg)
|
void *arg)
|
||||||
{
|
{
|
||||||
char *hardware_revision = "rev1";
|
char *hardware_revision = "rev1";
|
||||||
os_mbuf_append(ctxt->om, hardware_revision, strlen(hardware_revision));
|
os_mbuf_append(ctxt->om, hardware_revision, strlen(hardware_revision));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int device_manufacturer_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
int device_manufacturer_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||||
{
|
{
|
||||||
char *manufacturer = "mars3142";
|
char *manufacturer = "mars3142";
|
||||||
os_mbuf_append(ctxt->om, manufacturer, strlen(manufacturer));
|
os_mbuf_append(ctxt->om, manufacturer, strlen(manufacturer));
|
||||||
|
@@ -3,16 +3,16 @@
|
|||||||
#include "host/ble_hs.h"
|
#include "host/ble_hs.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
// 0x2A00 Device Name
|
// 0x2A00 - Device Name
|
||||||
int device_name_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
int device_name_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||||
|
|
||||||
// 0x2A26 Firmware Revision String
|
// 0x2A26 - Firmware Revision String
|
||||||
int device_firmware_revision_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt,
|
int device_firmware_revision_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt,
|
||||||
void *arg);
|
void *arg);
|
||||||
|
|
||||||
// 0x2A27 Hardware Revision String
|
// 0x2A27 - Hardware Revision String
|
||||||
int device_hardware_revision_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt,
|
int device_hardware_revision_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt,
|
||||||
void *arg);
|
void *arg);
|
||||||
|
|
||||||
// 0x2A29 Manufacturer Name String
|
// 0x2A29 - Manufacturer Name String
|
||||||
int device_manufacturer_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
int device_manufacturer_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||||
|
@@ -3,12 +3,16 @@
|
|||||||
#include "host/ble_hs.h"
|
#include "host/ble_hs.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
/// LED Service Characteristic Callbacks
|
// 0xA000 - Light Service
|
||||||
int led_write(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
int led_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||||
int led_capabilities_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
|
||||||
|
|
||||||
/// LED Service Characteristic User Description
|
// 0xBEA0 - Beacon Control
|
||||||
int led_char_a000_user_desc(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
int beacon_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||||
int led_char_dead_user_desc(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
|
||||||
int led_char_dead_presentation(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
/// Outdoor Light Descriptors
|
||||||
int led_char_dead_valid_range(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
int led_char_user_desc_cb(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||||
|
|
||||||
|
/// Beacon Characteristic Descriptors
|
||||||
|
int beacon_char_user_desc_cb(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||||
|
int bool_char_presentation_cb(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||||
|
int bool_char_valid_range_cb(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||||
|
@@ -1,111 +1,107 @@
|
|||||||
#include "include/light_service.h"
|
#include "include/light_service.h"
|
||||||
|
#include "beacon.h"
|
||||||
|
|
||||||
static const char *TAG = "light_service";
|
static const char *TAG = "light_service";
|
||||||
|
|
||||||
/// Capabilities of Device
|
static uint8_t g_led_enabled = 1; // 0=false, 1=true
|
||||||
int led_capabilities_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
||||||
|
/// Characteristic Callbacks
|
||||||
|
int led_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||||
{
|
{
|
||||||
char *data = "To be implemented later";
|
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR)
|
||||||
os_mbuf_append(ctxt->om, data, strlen(data));
|
{
|
||||||
return 0;
|
char *data = "To be implemented later";
|
||||||
|
os_mbuf_append(ctxt->om, data, strlen(data));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return BLE_ATT_ERR_UNLIKELY;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write data to ESP32 defined as server
|
int beacon_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||||
int led_write(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
||||||
{
|
{
|
||||||
const char *received_payload = (const char *)ctxt->om->om_data;
|
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR)
|
||||||
uint16_t payload_len = ctxt->om->om_len;
|
|
||||||
|
|
||||||
// Define command strings
|
|
||||||
const char CMD_LIGHT_ON[] = "LIGHT ON";
|
|
||||||
const char CMD_LIGHT_OFF[] = "LIGHT OFF";
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "LIGHT ON");
|
return os_mbuf_append(ctxt->om, &g_led_enabled, sizeof(g_led_enabled)) == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||||
// for (int i = 0; i < led_matrix_get_size(); i++)
|
}
|
||||||
|
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR)
|
||||||
|
{
|
||||||
|
// it has to be 1 Byte (0 or 1)
|
||||||
|
if (OS_MBUF_PKTLEN(ctxt->om) != 1)
|
||||||
{
|
{
|
||||||
// led_matrix_set_pixel(i, 10, 10, 0);
|
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
|
||||||
}
|
}
|
||||||
}
|
uint8_t val;
|
||||||
else if (payload_len == (sizeof(CMD_LIGHT_OFF) - 1) && strncmp(received_payload, CMD_LIGHT_OFF, payload_len) == 0)
|
os_mbuf_copydata(ctxt->om, 0, 1, &val);
|
||||||
{
|
if (val > 1)
|
||||||
ESP_LOGI(TAG, "LIGHT OFF");
|
|
||||||
// for (int i = 0; i < led_matrix_get_size(); i++)
|
|
||||||
{
|
{
|
||||||
// led_matrix_set_pixel(i, 0, 0, 0);
|
return BLE_ATT_ERR_UNLIKELY; // or BLE_ATT_ERR_INVALID_ATTR_VALUE
|
||||||
}
|
}
|
||||||
}
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "FAN OFF");
|
|
||||||
// TODO: Implement action for FAN OFF
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
char temp_buffer[payload_len + 1];
|
|
||||||
memcpy(temp_buffer, received_payload, payload_len);
|
|
||||||
temp_buffer[payload_len] = '\0';
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Unknown command from client: %s", temp_buffer);
|
if (val == g_led_enabled) // value is already set
|
||||||
}
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
g_led_enabled = val;
|
||||||
|
|
||||||
|
if (g_led_enabled)
|
||||||
|
{
|
||||||
|
beacon_start();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
beacon_stop();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return BLE_ATT_ERR_UNLIKELY;
|
||||||
}
|
}
|
||||||
|
|
||||||
int led_char_a000_user_desc(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
// Characteristic User Descriptions
|
||||||
|
int led_char_user_desc_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||||
{
|
{
|
||||||
const char *desc = "Capabilities of Device";
|
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC)
|
||||||
os_mbuf_append(ctxt->om, desc, strlen(desc));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int led_char_dead_user_desc(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
int led_char_dead_presentation(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
||||||
{
|
|
||||||
if (ctxt->op != BLE_GATT_ACCESS_OP_READ_DSC)
|
|
||||||
{
|
{
|
||||||
return BLE_ATT_ERR_READ_NOT_PERMITTED;
|
const char *desc = "Aussenbeleuchtung";
|
||||||
|
return os_mbuf_append(ctxt->om, desc, strlen(desc)) == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||||
}
|
}
|
||||||
|
return BLE_ATT_ERR_READ_NOT_PERMITTED;
|
||||||
// GATT Presentation Format (0x2904), 7 Bytes:
|
|
||||||
// [format, exponent, unit(2), namespace, description(2)]
|
|
||||||
// format 0x04 = uint8, exponent 0, unit 0x2700 (unitless), namespace 1 (Bluetooth SIG Assigned Numbers),
|
|
||||||
// description 0
|
|
||||||
const uint8_t fmt[7] = {
|
|
||||||
0x04, // format = uint8
|
|
||||||
0x00, // exponent
|
|
||||||
0x00, 0x27, // unit = org.bluetooth.unit.unitless (0x2700)
|
|
||||||
0x01, // namespace = 1 (SIG)
|
|
||||||
0x00, 0x00 // description
|
|
||||||
};
|
|
||||||
return os_mbuf_append(ctxt->om, fmt, sizeof(fmt)) == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int led_char_dead_valid_range(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
int beacon_char_user_desc_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||||
{
|
{
|
||||||
if (ctxt->op != BLE_GATT_ACCESS_OP_READ_DSC)
|
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC)
|
||||||
{
|
{
|
||||||
return BLE_ATT_ERR_READ_NOT_PERMITTED;
|
const char *desc = "Leuchtfeuer";
|
||||||
|
return os_mbuf_append(ctxt->om, desc, strlen(desc)) == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||||
}
|
}
|
||||||
|
return BLE_ATT_ERR_READ_NOT_PERMITTED;
|
||||||
// Valid Range Descriptor (0x2906) Payload:
|
}
|
||||||
// - Für numerische Werte: [min .. max] im "nativen" Datentyp der Characteristic.
|
|
||||||
// Hier: uint8, also 1 Byte min + 1 Byte max.
|
int bool_char_presentation_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||||
const uint8_t range[2] = {0x00, 0x01}; // min=0, max=1
|
{
|
||||||
|
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC)
|
||||||
return os_mbuf_append(ctxt->om, range, sizeof(range)) == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
{
|
||||||
|
// 7-Byte Format: [format, exponent, unit(2), namespace, description(2)]
|
||||||
|
uint8_t fmt[7] = {
|
||||||
|
0x01, // format = boolean
|
||||||
|
0x00, // exponent
|
||||||
|
0x00, 0x00, // unit = none
|
||||||
|
0x01, // namespace = Bluetooth SIG
|
||||||
|
0x00, 0x00 // description
|
||||||
|
};
|
||||||
|
return os_mbuf_append(ctxt->om, fmt, sizeof(fmt)) == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||||
|
}
|
||||||
|
return BLE_ATT_ERR_READ_NOT_PERMITTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
int bool_char_valid_range_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||||
|
{
|
||||||
|
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC)
|
||||||
|
{
|
||||||
|
// for bool optional. but here as 1-Byte-Min/Max (0..1)
|
||||||
|
uint8_t range[2] = {0x00, 0x01}; // min=0, max=1
|
||||||
|
return os_mbuf_append(ctxt->om, range, sizeof(range)) == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||||
|
}
|
||||||
|
return BLE_ATT_ERR_READ_NOT_PERMITTED;
|
||||||
}
|
}
|
||||||
|
@@ -19,11 +19,9 @@
|
|||||||
|
|
||||||
static const char *TAG = "remote_control";
|
static const char *TAG = "remote_control";
|
||||||
|
|
||||||
// static const ble_uuid128_t capability_service_uuid =
|
|
||||||
// BLE_UUID128_INIT(0x91, 0xB6, 0xCA, 0x95, 0xB2, 0xC6, 0x7B, 0x90, 0x31, 0x45, 0x77, 0xE6, 0x67, 0x10, 0x68, 0xB9);
|
|
||||||
|
|
||||||
static const ble_uuid16_t device_service_uuid = BLE_UUID16_INIT(0x180A);
|
static const ble_uuid16_t device_service_uuid = BLE_UUID16_INIT(0x180A);
|
||||||
static const ble_uuid16_t light_service_uuid = BLE_UUID16_INIT(0xA000);
|
static const ble_uuid16_t light_service_uuid = BLE_UUID16_INIT(0xA000);
|
||||||
|
static const ble_uuid16_t settings_service_uuid = BLE_UUID16_INIT(0xA999);
|
||||||
|
|
||||||
uint8_t ble_addr_type;
|
uint8_t ble_addr_type;
|
||||||
|
|
||||||
@@ -32,32 +30,42 @@ static uint16_t g_capa_char_val_handle;
|
|||||||
|
|
||||||
static void ble_app_advertise(void);
|
static void ble_app_advertise(void);
|
||||||
|
|
||||||
static struct ble_gatt_dsc_def char_0xA000_descs[] = {{
|
// Descriptors for the characteristics
|
||||||
.uuid = BLE_UUID16_DECLARE(0x2901),
|
static struct ble_gatt_dsc_def led_char_desc[] = {{
|
||||||
.att_flags = BLE_ATT_F_READ,
|
.uuid = BLE_UUID16_DECLARE(0x2901),
|
||||||
.access_cb = led_char_a000_user_desc,
|
.att_flags = BLE_ATT_F_READ,
|
||||||
},
|
.access_cb = led_char_user_desc_cb,
|
||||||
{0}};
|
},
|
||||||
|
{
|
||||||
|
.uuid = BLE_UUID16_DECLARE(0x2904),
|
||||||
|
.att_flags = BLE_ATT_F_READ,
|
||||||
|
.access_cb = bool_char_presentation_cb,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.uuid = BLE_UUID16_DECLARE(0x2906),
|
||||||
|
.att_flags = BLE_ATT_F_READ,
|
||||||
|
.access_cb = bool_char_valid_range_cb,
|
||||||
|
},
|
||||||
|
{0}};
|
||||||
|
|
||||||
static struct ble_gatt_dsc_def char_0xDEAD_descs[] = {
|
static struct ble_gatt_dsc_def beacon_char_desc[] = {{
|
||||||
{
|
.uuid = BLE_UUID16_DECLARE(0x2901),
|
||||||
.uuid = BLE_UUID16_DECLARE(0x2901),
|
.att_flags = BLE_ATT_F_READ,
|
||||||
.att_flags = BLE_ATT_F_WRITE,
|
.access_cb = beacon_char_user_desc_cb,
|
||||||
.access_cb = led_char_dead_user_desc,
|
},
|
||||||
},
|
{
|
||||||
{
|
.uuid = BLE_UUID16_DECLARE(0x2904),
|
||||||
.uuid = BLE_UUID16_DECLARE(0x2904), // Presentation Format (optional, empfehlenswert)
|
.att_flags = BLE_ATT_F_READ,
|
||||||
.att_flags = BLE_ATT_F_READ,
|
.access_cb = bool_char_presentation_cb,
|
||||||
.access_cb = led_char_dead_presentation,
|
},
|
||||||
},
|
{
|
||||||
{
|
.uuid = BLE_UUID16_DECLARE(0x2906),
|
||||||
.uuid = BLE_UUID16_DECLARE(0x2906), // Valid Range
|
.att_flags = BLE_ATT_F_READ,
|
||||||
.att_flags = BLE_ATT_F_READ,
|
.access_cb = bool_char_valid_range_cb,
|
||||||
.access_cb = led_char_dead_valid_range,
|
},
|
||||||
},
|
{0}};
|
||||||
{0}};
|
|
||||||
|
|
||||||
// Array of pointers to other service definitions
|
// Array of pointers to service definitions
|
||||||
static const struct ble_gatt_svc_def gatt_svcs[] = {
|
static const struct ble_gatt_svc_def gatt_svcs[] = {
|
||||||
{
|
{
|
||||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||||
@@ -65,22 +73,22 @@ static const struct ble_gatt_svc_def gatt_svcs[] = {
|
|||||||
.characteristics = (struct ble_gatt_chr_def[]){{
|
.characteristics = (struct ble_gatt_chr_def[]){{
|
||||||
.uuid = BLE_UUID16_DECLARE(0x2A29),
|
.uuid = BLE_UUID16_DECLARE(0x2A29),
|
||||||
.flags = BLE_GATT_CHR_F_READ,
|
.flags = BLE_GATT_CHR_F_READ,
|
||||||
.access_cb = device_manufacturer_read,
|
.access_cb = device_manufacturer_cb,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.uuid = BLE_UUID16_DECLARE(0x2A27),
|
.uuid = BLE_UUID16_DECLARE(0x2A27),
|
||||||
.flags = BLE_GATT_CHR_F_READ,
|
.flags = BLE_GATT_CHR_F_READ,
|
||||||
.access_cb = device_hardware_revision_read,
|
.access_cb = device_hardware_revision_cb,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.uuid = BLE_UUID16_DECLARE(0x2A26),
|
.uuid = BLE_UUID16_DECLARE(0x2A26),
|
||||||
.flags = BLE_GATT_CHR_F_READ,
|
.flags = BLE_GATT_CHR_F_READ,
|
||||||
.access_cb = device_firmware_revision_read,
|
.access_cb = device_firmware_revision_cb,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.uuid = BLE_UUID16_DECLARE(0x2A00),
|
.uuid = BLE_UUID16_DECLARE(0x2A00),
|
||||||
.flags = BLE_GATT_CHR_F_READ,
|
.flags = BLE_GATT_CHR_F_READ,
|
||||||
.access_cb = device_name_read,
|
.access_cb = device_name_cb,
|
||||||
},
|
},
|
||||||
{0}},
|
{0}},
|
||||||
},
|
},
|
||||||
@@ -88,19 +96,24 @@ static const struct ble_gatt_svc_def gatt_svcs[] = {
|
|||||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||||
.uuid = &light_service_uuid.u,
|
.uuid = &light_service_uuid.u,
|
||||||
.characteristics = (struct ble_gatt_chr_def[]){{
|
.characteristics = (struct ble_gatt_chr_def[]){{
|
||||||
.uuid = BLE_UUID16_DECLARE(0xA000),
|
.uuid = BLE_UUID16_DECLARE(0xBEA0),
|
||||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
|
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
|
||||||
.access_cb = led_capabilities_read,
|
.access_cb = beacon_cb,
|
||||||
.descriptors = char_0xA000_descs,
|
.descriptors = beacon_char_desc,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.uuid = BLE_UUID16_DECLARE(0xDEAD),
|
.uuid = BLE_UUID16_DECLARE(0xF037),
|
||||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
|
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
|
||||||
.access_cb = led_write,
|
.access_cb = led_cb,
|
||||||
.descriptors = char_0xDEAD_descs,
|
.descriptors = led_char_desc,
|
||||||
},
|
},
|
||||||
{0}},
|
{0}},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||||
|
.uuid = &settings_service_uuid.u,
|
||||||
|
.characteristics = (struct ble_gatt_chr_def[]){{0}},
|
||||||
|
},
|
||||||
{0}};
|
{0}};
|
||||||
|
|
||||||
// BLE event handling
|
// BLE event handling
|
||||||
@@ -170,7 +183,7 @@ static void ble_app_advertise(void)
|
|||||||
struct ble_hs_adv_fields fields;
|
struct ble_hs_adv_fields fields;
|
||||||
memset(&fields, 0, sizeof(fields));
|
memset(&fields, 0, sizeof(fields));
|
||||||
uint8_t mfg_data[] = {0xDE, 0xC0, 0x05, 0x10, 0x20, 0x25};
|
uint8_t mfg_data[] = {0xDE, 0xC0, 0x05, 0x10, 0x20, 0x25};
|
||||||
static const ble_uuid16_t services[] = {device_service_uuid, light_service_uuid};
|
static const ble_uuid16_t services[] = {device_service_uuid, light_service_uuid, settings_service_uuid};
|
||||||
|
|
||||||
fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
|
fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
|
||||||
fields.uuids16 = services;
|
fields.uuids16 = services;
|
||||||
|
Reference in New Issue
Block a user