1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

skald_bthome_saul: add capability for custom SAUL entries

This commit is contained in:
Martine S. Lenders 2024-11-16 18:38:22 +01:00 committed by Martine Lenders
parent ce6a7e0b96
commit 7d0d41a10a
No known key found for this signature in database
GPG Key ID: 2134D77A5336DD80
4 changed files with 71 additions and 18 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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.

View File

@ -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;