From 7d0d41a10a39559ced5d2035eedda8d7ba2726f4 Mon Sep 17 00:00:00 2001 From: "Martine S. Lenders" Date: Sat, 16 Nov 2024 18:38:22 +0100 Subject: [PATCH] skald_bthome_saul: add capability for custom SAUL entries --- examples/skald_bthome/main.c | 22 ++++++++++++++++- sys/Makefile.dep | 5 ++++ sys/include/net/skald/bthome.h | 28 ++++++++++++++++++++-- sys/net/ble/skald/skald_bthome_saul.c | 34 +++++++++++++++------------ 4 files changed, 71 insertions(+), 18 deletions(-) diff --git a/examples/skald_bthome/main.c b/examples/skald_bthome/main.c index 5b9f28ee37..e2c48ecf28 100644 --- a/examples/skald_bthome/main.c +++ b/examples/skald_bthome/main.c @@ -78,10 +78,20 @@ int _get_encryption_key(void) } #endif +static int _add_text(skald_bthome_ctx_t *ctx, uint8_t obj_id, phydat_t *data, uint8_t idx) +{ + static const char info[] = "RIOT"; + + (void)data; + (void)idx; + return skald_bthome_add_measurement(ctx, obj_id, info, strlen(info)); +} + int main(void) { saul_reg_t *dev = saul_reg; unsigned i = 0; + int res; ztimer_sleep(ZTIMER_MSEC, 2000); printf("Skald and the tale of Harald's home\n"); @@ -112,7 +122,6 @@ int main(void) return 1; } while (dev && (i < CONFIG_BTHOME_SAUL_REG_DEVS)) { - int res; _saul_devs[i].saul = *dev; /* copy registry entry */ _saul_devs[i].saul.next = NULL; printf("Adding %s (%s) to BTHome.\n", dev->name, saul_class_to_str(dev->driver->type)); @@ -126,6 +135,17 @@ int main(void) dev = dev->next; } assert(!saul_reg || _ctx.devs); + if (i < CONFIG_BTHOME_SAUL_REG_DEVS) { + memset(&_saul_devs[i].saul, 0, sizeof(_saul_devs[i].saul)); + _saul_devs[i].obj_id = BTHOME_ID_TEXT; + _saul_devs[i].flags = SKALD_BTHOME_SAUL_FLAGS_CUSTOM; + _saul_devs[i].add_measurement = _add_text; + if ((res = skald_bthome_saul_add(&_ctx, &_saul_devs[i])) < 0) { + errno = -res; + perror("Unable to add text info to BTHome"); + }; + i++; + } skald_bthome_advertise(&_ctx, BTHOME_ADV_INTERVAL); return 0; } diff --git a/sys/Makefile.dep b/sys/Makefile.dep index a3ef833a3e..138790ba53 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -592,6 +592,11 @@ ifneq (,$(filter skald_bthome_saul,$(USEMODULE))) USEMODULE += saul_reg endif +ifneq (,$(filter skald_update_pkt_cb,$(USEMODULE))) + USEMODULE += event + USEMODULE += skald +endif + ifneq (,$(filter bluetil_addr,$(USEMODULE))) USEMODULE += fmt endif diff --git a/sys/include/net/skald/bthome.h b/sys/include/net/skald/bthome.h index 5a9ad33977..4a4edf50d8 100644 --- a/sys/include/net/skald/bthome.h +++ b/sys/include/net/skald/bthome.h @@ -60,6 +60,16 @@ extern "C" { typedef struct skald_bthome_ctx skald_bthome_ctx_t; #if IS_USED(MODULE_SKALD_BTHOME_SAUL) || defined(DOXYGEN) +/** + * @brief Flags for the BTHome-SAUL-adapter + */ +typedef enum { + /** + * @brief Use custom object ID and add-measurement callback + */ + SKALD_BTHOME_SAUL_FLAGS_CUSTOM = 0x01, +} skald_bthome_saul_flags_t; + /** * @brief BTHome-SAUL-adapter */ @@ -76,9 +86,20 @@ typedef struct { * * @see skald_bthome_id_t * - * Will be filled by @ref skald_bthome_saul_add(). + * Will be filled by @ref skald_bthome_saul_add() if + * @ref SKALD_BTHOME_SAUL_FLAGS_CUSTOM is unset in + * skald_bthome_saul_t::flags. Otherwise, set to the desired + * object ID for the measurement. */ skald_bthome_id_t obj_id; + /** + * @brief Flags for the BTHome-SAUL-adapter + * + * @see skald_bthome_saul_flags_t + * + * May be set before calling @ref skald_bthome_saul_add() + */ + skald_bthome_saul_flags_t flags; /** * @brief Callback to add measurement from SAUL registry entry @@ -87,7 +108,10 @@ typedef struct { * @ref saul_reg_read() and should ultimately call * @ref skald_bthome_add_measurement() * (or one of its wrappers). * - * Will be filled by @ref skald_bthome_saul_add(). + * Will be filled by @ref skald_bthome_saul_add() if + * @ref SKALD_BTHOME_SAUL_FLAGS_CUSTOM is unset in + * skald_bthome_saul_t::flags. Otherwise, set to the desired + * object ID for the measurement. * * @param[in,out] ctx BTHome advertising context. MUST not be NULL. * @param[in] obj_id The object ID for the measurement. diff --git a/sys/net/ble/skald/skald_bthome_saul.c b/sys/net/ble/skald/skald_bthome_saul.c index 35436ecec5..7c36c855db 100644 --- a/sys/net/ble/skald/skald_bthome_saul.c +++ b/sys/net/ble/skald/skald_bthome_saul.c @@ -427,12 +427,14 @@ static void _update_saul_measurements(skald_ctx_t *skald_ctx) _reset_hdr(ctx); skald_bthome_saul_t *ptr = ctx->devs; while (ptr) { - int dim; - phydat_t data; + int dim = 1; + phydat_t data = { 0 }; - dim = saul_reg_read(&ptr->saul, &data); - if (dim <= 0) { - continue; + if (ptr->saul.driver) { + dim = saul_reg_read(&ptr->saul, &data); + if (dim <= 0) { + continue; + } } for (uint8_t i = 0; i < dim; i++) { if (ptr->add_measurement(ctx, ptr->obj_id, &data, i) < 0) { @@ -451,22 +453,24 @@ static void _update_saul_measurements(skald_ctx_t *skald_ctx) int skald_bthome_saul_add(skald_bthome_ctx_t *ctx, skald_bthome_saul_t *saul) { - int dim; - phydat_t sample; - - if ((saul->saul.driver->type & SAUL_CAT_MASK) != SAUL_CAT_SENSE) { + if ((saul->saul.driver != NULL) && + (saul->saul.driver->type & SAUL_CAT_MASK) != SAUL_CAT_SENSE) { return -ENOTSUP; } if (!ctx->skald.update_pkt) { ctx->skald.update_pkt = &_update_saul_measurements; } - dim = saul_reg_read(&saul->saul, &sample); - if (dim <= 0) { - return -ENODEV; - } - if (_saul_sense_to_bthome_id(saul, &sample) < 0) { - return -ENOENT; + if (!(saul->flags & SKALD_BTHOME_SAUL_FLAGS_CUSTOM)) { + phydat_t sample; + int dim = saul_reg_read(&saul->saul, &sample); + + if (dim <= 0) { + return -ENODEV; + } + if (_saul_sense_to_bthome_id(saul, &sample) < 0) { + return -ENOENT; + } } if (ctx->devs) { skald_bthome_saul_t *ptr = ctx->devs;