Skip to content

Commit

Permalink
Managed to build with multiple elements and fixed models extension
Browse files Browse the repository at this point in the history
PROBLEM: health_pub is passed directly. Need to find a way how to
initialize models with multiple args.
  • Loading branch information
PavelVPV committed Dec 13, 2024
1 parent 84cd139 commit 20e1408
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 4 deletions.
13 changes: 12 additions & 1 deletion samples/bluetooth/mesh/ngcdp/ble_mesh.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
compatible = "bt-mesh-element";
models {
model-cfg-srv {
vname = "cfg-srv";
vname = "cfg_srv";
cmodel = "bt-mesh-model-cfg-srv";
compatible = "bt-mesh-model";
};
Expand All @@ -22,6 +22,17 @@
};
};
};
element_2 {
location = <2>;
compatible = "bt-mesh-element";
models {
model-ponoff-srv-0 {
vname = "ponoff_srv";
cmodel = "bt-mesh-model-ponoff-srv";
compatible = "bt-mesh-model";
};
};
};
};
};
};
2 changes: 2 additions & 0 deletions samples/bluetooth/mesh/ngcdp/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ CONFIG_BT_MESH_DK_PROV=y

# Bluetooth Mesh models
CONFIG_BT_MESH_ONOFF_CLI=y
CONFIG_BT_MESH_ONOFF_SRV=y
CONFIG_BT_MESH_PONOFF_SRV=y
130 changes: 127 additions & 3 deletions samples/bluetooth/mesh/ngcdp/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <stdio.h>
#include <zephyr/kernel.h>
#include <bluetooth/mesh/dk_prov.h>
#include <dk_buttons_and_leds.h>

// FIXME:...
#define CONFIG_BT_MESH_USES_TINYCRYPT 1
Expand All @@ -16,6 +18,62 @@
#include <zephyr/devicetree.h>

static struct bt_mesh_onoff_cli onoff_cli;
static struct bt_mesh_ponoff_srv ponoff_srv;

/* Set up a repeating delayed work to blink the DK's LEDs when attention is
* requested.
*/
static struct k_work_delayable attention_blink_work;
static bool attention;

static void attention_blink(struct k_work *work)
{
static int idx;
const uint8_t pattern[] = {
#if DT_NODE_EXISTS(DT_ALIAS(sw0))
BIT(0),
#endif
#if DT_NODE_EXISTS(DT_ALIAS(sw1))
BIT(1),
#endif
#if DT_NODE_EXISTS(DT_ALIAS(sw2))
BIT(2),
#endif
#if DT_NODE_EXISTS(DT_ALIAS(sw3))
BIT(3),
#endif
};

if (attention) {
dk_set_leds(pattern[idx++ % ARRAY_SIZE(pattern)]);
k_work_reschedule(&attention_blink_work, K_MSEC(30));
} else {
dk_set_leds(DK_NO_LEDS_MSK);
}
}

static void attention_on(const struct bt_mesh_model *mod)
{
attention = true;
k_work_reschedule(&attention_blink_work, K_NO_WAIT);
}

static void attention_off(const struct bt_mesh_model *mod)
{
/* Will stop rescheduling blink timer */
attention = false;
}

static const struct bt_mesh_health_srv_cb health_srv_cb = {
.attn_on = attention_on,
.attn_off = attention_off,
};

static struct bt_mesh_health_srv health_srv = {
.cb = &health_srv_cb,
};

BT_MESH_HEALTH_PUB_DEFINE(health_pub, 0);

/* Define your mesh element nodes */
#define MESH_NODE DT_PATH(mesh) //DT_NODELABEL(mesh)
Expand All @@ -26,8 +84,9 @@ static struct bt_mesh_onoff_cli onoff_cli;
#define EXPAND(x) x

#define bt_mesh_model_cfg_srv_MACRO(name) EXPAND(BT_MESH_MODEL_CFG_SRV)
#define bt_mesh_model_health_srv_MACRO(name) EXPAND(BT_MESH_MODEL_HEALTH_SRV(NULL, NULL))
#define bt_mesh_model_health_srv_MACRO(name) EXPAND(BT_MESH_MODEL_HEALTH_SRV(&name, &health_pub))
#define bt_mesh_model_onoff_cli_MACRO(name) EXPAND(BT_MESH_MODEL_ONOFF_CLI(&name))
#define bt_mesh_model_ponoff_srv_MACRO(name) EXPAND(BT_MESH_MODEL_PONOFF_SRV(&name))


//#define MACRO_FROM_COMPAT(compat) compat##_MACRO
Expand All @@ -52,16 +111,80 @@ static struct bt_mesh_onoff_cli onoff_cli;
#define DT_BT_MESH_ELEM_INIT(element) { \
.rt = &(struct bt_mesh_elem_rt_ctx) { 0 }, \
.loc = (DT_PROP(element, location)), \
.model_count = DT_CHILD_NUM(DT_CHILD(element, models)), \
.model_count = ARRAY_SIZE(MODELS(element)), \
.vnd_model_count = 0, \
.models = MODELS(element), \
.vnd_models = NULL, \
},
}

struct bt_mesh_elem elements[] = {
DT_FOREACH_CHILD_SEP(DT_CHILD(MESH_NODE, elements), DT_BT_MESH_ELEM_INIT, (,))
};

static const struct bt_mesh_comp comp = {
.cid = CONFIG_BT_COMPANY_ID,
.elem = elements,
.elem_count = ARRAY_SIZE(elements),
};

static void bt_ready(int err)
{
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return;
}

printk("Bluetooth initialized\n");

err = dk_leds_init();
if (err) {
printk("Initializing LEDs failed (err %d)\n", err);
return;
}

err = dk_buttons_init(NULL);
if (err) {
printk("Initializing buttons failed (err %d)\n", err);
return;
}

err = bt_mesh_init(bt_mesh_dk_prov_init(), &comp);
if (err) {
printk("Initializing mesh failed (err %d)\n", err);
return;
}

if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER)) {
bt_mesh_lpn_set(true);
}

if (IS_ENABLED(CONFIG_SETTINGS)) {
settings_load();
}

/* This will be a no-op if settings_load() loaded provisioning info */
bt_mesh_prov_enable(BT_MESH_PROV_ADV | BT_MESH_PROV_GATT);

printk("Mesh initialized\n");
}

int main(void)
{
int err;

printk("Initializing...\n");

k_work_init_delayable(&attention_blink_work, attention_blink);

err = bt_enable(bt_ready);
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
}

return 0;
}

#if 0
int main(void)
{
#if 0
Expand Down Expand Up @@ -93,3 +216,4 @@ int main(void)

return 0;
}
#endif

0 comments on commit 20e1408

Please sign in to comment.