code clean up and persistence for beacon last state

Signed-off-by: Peter Siegmund <mars3142@users.noreply.github.com>
This commit is contained in:
2025-08-29 16:01:25 +02:00
parent 8bf9e8f462
commit 13b009c0db
12 changed files with 139 additions and 88 deletions

View File

@@ -1,12 +1,12 @@
#include "include/light_service.h"
#include "beacon.h"
#include "persistence.h"
static const char *TAG = "light_service";
static uint8_t g_led_enabled = 1; // 0=false, 1=true
static uint8_t g_beacon_enabled = 0;
/// Characteristic Callbacks
int led_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
int gatt_svr_chr_light_led_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt,
void *arg)
{
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR)
{
@@ -17,11 +17,15 @@ int led_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ct
return BLE_ATT_ERR_UNLIKELY;
}
int beacon_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
int gatt_svr_chr_light_beacon_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt,
void *arg)
{
persistence_load(VALUE_TYPE_INT32, "BEACON_ENABLED", &g_beacon_enabled);
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR)
{
return os_mbuf_append(ctxt->om, &g_led_enabled, sizeof(g_led_enabled)) == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
return os_mbuf_append(ctxt->om, &g_beacon_enabled, sizeof(g_beacon_enabled)) == 0
? 0
: BLE_ATT_ERR_INSUFFICIENT_RES;
}
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR)
{
@@ -37,14 +41,14 @@ int beacon_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access
return BLE_ATT_ERR_UNLIKELY; // or BLE_ATT_ERR_INVALID_ATTR_VALUE
}
if (val == g_led_enabled) // value is already set
if (val == g_beacon_enabled) // value is already set
{
return 0;
}
g_led_enabled = val;
g_beacon_enabled = val;
if (g_led_enabled)
if (g_beacon_enabled)
{
beacon_start();
}
@@ -52,13 +56,15 @@ int beacon_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access
{
beacon_stop();
}
persistence_save(VALUE_TYPE_INT32, "BEACON_ENABLED", &g_beacon_enabled);
return 0;
}
return BLE_ATT_ERR_UNLIKELY;
}
// 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)
int gatt_svr_desc_led_user_desc_access(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)
{
@@ -68,7 +74,8 @@ int led_char_user_desc_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble
return BLE_ATT_ERR_READ_NOT_PERMITTED;
}
int beacon_char_user_desc_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
int gatt_svr_desc_beacon_user_desc_access(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)
{
@@ -77,31 +84,3 @@ int beacon_char_user_desc_cb(uint16_t conn_handle, uint16_t attr_handle, struct
}
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;
}