more characteristics and descriptors

Signed-off-by: Peter Siegmund <mars3142@users.noreply.github.com>
This commit is contained in:
2025-08-27 21:09:20 +02:00
parent 1f0d36dc1a
commit e26dd83f5d
7 changed files with 195 additions and 164 deletions

View File

@@ -1,111 +1,107 @@
#include "include/light_service.h"
#include "beacon.h"
static const char *TAG = "light_service";
/// Capabilities of Device
int led_capabilities_read(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
static uint8_t g_led_enabled = 1; // 0=false, 1=true
/// 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";
os_mbuf_append(ctxt->om, data, strlen(data));
return 0;
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR)
{
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 led_write(uint16_t conn_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)
{
const char *received_payload = (const char *)ctxt->om->om_data;
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)
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR)
{
ESP_LOGI(TAG, "LIGHT ON");
// for (int i = 0; i < led_matrix_get_size(); i++)
return os_mbuf_append(ctxt->om, &g_led_enabled, sizeof(g_led_enabled)) == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}
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;
}
}
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++)
uint8_t val;
os_mbuf_copydata(ctxt->om, 0, 1, &val);
if (val > 1)
{
// 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";
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)
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;
}
// 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;
return BLE_ATT_ERR_READ_NOT_PERMITTED;
}
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;
}
// 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.
const 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;
}
int bool_char_presentation_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)
{
// 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;
}