Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | /* main.c - Application main entry point */
/*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <sys/printk.h>
#include <settings/settings.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/mesh.h>
#include "board.h"
#define MOD_LF 0x0000
#define GROUP_ADDR 0xc000
#define PUBLISHER_ADDR 0x000f
#define OP_VENDOR_BUTTON BT_MESH_MODEL_OP_3(0x00, BT_COMP_ID_LF)
static const uint8_t net_key[16] = {
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
};
static const uint8_t dev_key[16] = {
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
};
static const uint8_t app_key[16] = {
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
};
static const uint16_t net_idx;
static const uint16_t app_idx;
static const uint32_t iv_index;
static uint8_t flags;
static uint16_t addr = NODE_ADDR;
static void heartbeat(const struct bt_mesh_hb_sub *sub, uint8_t hops,
uint16_t feat)
{
board_heartbeat(hops, feat);
board_play("100H");
}
static struct bt_mesh_cfg_cli cfg_cli = {
};
static void attention_on(struct bt_mesh_model *model)
{
printk("attention_on()\n");
board_attention(true);
board_play("100H100C100H100C100H100C");
}
static void attention_off(struct bt_mesh_model *model)
{
printk("attention_off()\n");
board_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);
static struct bt_mesh_model root_models[] = {
BT_MESH_MODEL_CFG_SRV,
BT_MESH_MODEL_CFG_CLI(&cfg_cli),
BT_MESH_MODEL_HEALTH_SRV(&health_srv, &health_pub),
};
static void vnd_button_pressed(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
printk("src 0x%04x\n", ctx->addr);
if (ctx->addr == bt_mesh_model_elem(model)->addr) {
return;
}
board_other_dev_pressed(ctx->addr);
board_play("100G200 100G");
}
static const struct bt_mesh_model_op vnd_ops[] = {
{ OP_VENDOR_BUTTON, 0, vnd_button_pressed },
BT_MESH_MODEL_OP_END,
};
static struct bt_mesh_model vnd_models[] = {
BT_MESH_MODEL_VND(BT_COMP_ID_LF, MOD_LF, vnd_ops, NULL, NULL),
};
static struct bt_mesh_elem elements[] = {
BT_MESH_ELEM(0, root_models, vnd_models),
};
static const struct bt_mesh_comp comp = {
.cid = BT_COMP_ID_LF,
.elem = elements,
.elem_count = ARRAY_SIZE(elements),
};
static void configure(void)
{
printk("Configuring...\n");
/* Add Application Key */
bt_mesh_cfg_app_key_add(net_idx, addr, net_idx, app_idx, app_key, NULL);
/* Bind to vendor model */
bt_mesh_cfg_mod_app_bind_vnd(net_idx, addr, addr, app_idx,
MOD_LF, BT_COMP_ID_LF, NULL);
/* Bind to Health model */
bt_mesh_cfg_mod_app_bind(net_idx, addr, addr, app_idx,
BT_MESH_MODEL_ID_HEALTH_SRV, NULL);
/* Add model subscription */
bt_mesh_cfg_mod_sub_add_vnd(net_idx, addr, addr, GROUP_ADDR,
MOD_LF, BT_COMP_ID_LF, NULL);
#if NODE_ADDR == PUBLISHER_ADDR
{
struct bt_mesh_cfg_hb_pub pub = {
.dst = GROUP_ADDR,
.count = 0xff,
.period = 0x05,
.ttl = 0x07,
.feat = 0,
.net_idx = net_idx,
};
bt_mesh_cfg_hb_pub_set(net_idx, addr, &pub, NULL);
printk("Publishing heartbeat messages\n");
}
#endif
printk("Configuration complete\n");
board_play("100C100D100E100F100G100A100H");
}
static const uint8_t dev_uuid[16] = { 0xdd, 0xdd };
static const struct bt_mesh_prov prov = {
.uuid = dev_uuid,
};
BT_MESH_HB_CB_DEFINE(hb_cb) = {
.recv = heartbeat,
};
static void bt_ready(int err)
{
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return;
}
printk("Bluetooth initialized\n");
err = bt_mesh_init(&prov, &comp);
if (err) {
printk("Initializing mesh failed (err %d)\n", err);
return;
}
printk("Mesh initialized\n");
if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
printk("Loading stored settings\n");
settings_load();
}
err = bt_mesh_provision(net_key, net_idx, flags, iv_index, addr,
dev_key);
if (err == -EALREADY) {
printk("Using stored settings\n");
} else if (err) {
printk("Provisioning failed (err %d)\n", err);
return;
} else {
printk("Provisioning completed\n");
configure();
}
#if NODE_ADDR != PUBLISHER_ADDR
/* Heartbeat subcscription is a temporary state (due to there
* not being an "indefinite" value for the period, so it never
* gets stored persistently. Therefore, we always have to configure
* it explicitly.
*/
{
struct bt_mesh_cfg_hb_sub sub = {
.src = PUBLISHER_ADDR,
.dst = GROUP_ADDR,
.period = 0x10,
};
bt_mesh_cfg_hb_sub_set(net_idx, addr, &sub, NULL);
printk("Subscribing to heartbeat messages\n");
}
#endif
}
static uint16_t target = GROUP_ADDR;
void board_button_1_pressed(void)
{
NET_BUF_SIMPLE_DEFINE(msg, 3 + 4);
struct bt_mesh_msg_ctx ctx = {
.app_idx = app_idx,
.addr = target,
.send_ttl = BT_MESH_TTL_DEFAULT,
};
/* Bind to Health model */
bt_mesh_model_msg_init(&msg, OP_VENDOR_BUTTON);
if (bt_mesh_model_send(&vnd_models[0], &ctx, &msg, NULL, NULL)) {
printk("Unable to send Vendor Button message\n");
}
printk("Button message sent with OpCode 0x%08x\n", OP_VENDOR_BUTTON);
}
uint16_t board_set_target(void)
{
switch (target) {
case GROUP_ADDR:
target = 1U;
break;
case 9:
target = GROUP_ADDR;
break;
default:
target++;
break;
}
return target;
}
static K_SEM_DEFINE(tune_sem, 0, 1);
static const char *tune_str;
void board_play(const char *str)
{
tune_str = str;
k_sem_give(&tune_sem);
}
void main(void)
{
int err;
printk("Initializing...\n");
err = board_init(&addr);
if (err) {
printk("Board initialization failed\n");
return;
}
printk("Unicast address: 0x%04x\n", addr);
/* Initialize the Bluetooth Subsystem */
err = bt_enable(bt_ready);
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return;
}
while (1) {
k_sem_take(&tune_sem, K_FOREVER);
board_play_tune(tune_str);
}
}
|