2017-06-16 12:30:54 +03:00
|
|
|
/* Bluetooth Mesh */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <zephyr.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <net/buf.h>
|
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
#include <bluetooth/conn.h>
|
2018-08-16 13:51:24 +03:00
|
|
|
#include <bluetooth/uuid.h>
|
2017-06-16 12:30:54 +03:00
|
|
|
#include <bluetooth/mesh.h>
|
|
|
|
|
2017-08-09 09:21:11 +03:00
|
|
|
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_MESH_DEBUG)
|
2018-07-17 10:35:52 +03:00
|
|
|
#define LOG_MODULE_NAME bt_mesh_main
|
2017-06-16 12:30:54 +03:00
|
|
|
#include "common/log.h"
|
|
|
|
|
|
|
|
#include "test.h"
|
|
|
|
#include "adv.h"
|
|
|
|
#include "prov.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "beacon.h"
|
|
|
|
#include "lpn.h"
|
|
|
|
#include "friend.h"
|
|
|
|
#include "transport.h"
|
|
|
|
#include "access.h"
|
|
|
|
#include "foundation.h"
|
|
|
|
#include "proxy.h"
|
2018-05-07 12:23:37 +03:00
|
|
|
#include "settings.h"
|
2017-06-16 12:30:54 +03:00
|
|
|
#include "mesh.h"
|
|
|
|
|
|
|
|
int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx,
|
2018-05-11 11:16:47 +03:00
|
|
|
u8_t flags, u32_t iv_index, u16_t addr,
|
|
|
|
const u8_t dev_key[16])
|
2017-06-16 12:30:54 +03:00
|
|
|
{
|
2019-01-25 18:24:44 +02:00
|
|
|
bool pb_gatt_enabled;
|
2017-06-16 12:30:54 +03:00
|
|
|
int err;
|
|
|
|
|
|
|
|
BT_INFO("Primary Element: 0x%04x", addr);
|
2017-11-05 21:45:24 +02:00
|
|
|
BT_DBG("net_idx 0x%04x flags 0x%02x iv_index 0x%04x",
|
|
|
|
net_idx, flags, iv_index);
|
2017-06-16 12:30:54 +03:00
|
|
|
|
2019-01-25 18:24:44 +02:00
|
|
|
if (atomic_test_and_set_bit(bt_mesh.flags, BT_MESH_VALID)) {
|
|
|
|
return -EALREADY;
|
|
|
|
}
|
|
|
|
|
2017-08-09 09:21:11 +03:00
|
|
|
if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT)) {
|
2019-03-27 14:53:36 +01:00
|
|
|
if (bt_mesh_proxy_prov_disable(false) == 0) {
|
2019-01-25 18:24:44 +02:00
|
|
|
pb_gatt_enabled = true;
|
|
|
|
} else {
|
|
|
|
pb_gatt_enabled = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pb_gatt_enabled = false;
|
2017-06-16 12:30:54 +03:00
|
|
|
}
|
|
|
|
|
2019-11-04 16:14:07 +01:00
|
|
|
/*
|
|
|
|
* FIXME:
|
|
|
|
* Should net_key and iv_index be over-ridden?
|
|
|
|
*/
|
|
|
|
if (IS_ENABLED(CONFIG_BT_MESH_CDB)) {
|
|
|
|
const struct bt_mesh_comp *comp;
|
|
|
|
const struct bt_mesh_prov *prov;
|
|
|
|
struct bt_mesh_cdb_node *node;
|
|
|
|
|
|
|
|
if (!atomic_test_bit(bt_mesh_cdb.flags,
|
|
|
|
BT_MESH_CDB_VALID)) {
|
|
|
|
BT_ERR("No valid network");
|
|
|
|
atomic_clear_bit(bt_mesh.flags, BT_MESH_VALID);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
comp = bt_mesh_comp_get();
|
|
|
|
if (comp == NULL) {
|
|
|
|
BT_ERR("Failed to get node composition");
|
|
|
|
atomic_clear_bit(bt_mesh.flags, BT_MESH_VALID);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bt_mesh_cdb_subnet_get(net_idx)) {
|
|
|
|
BT_ERR("No subnet with idx %d", net_idx);
|
|
|
|
atomic_clear_bit(bt_mesh.flags, BT_MESH_VALID);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
prov = bt_mesh_prov_get();
|
|
|
|
node = bt_mesh_cdb_node_alloc(prov->uuid, addr,
|
|
|
|
comp->elem_count, net_idx);
|
|
|
|
if (node == NULL) {
|
|
|
|
BT_ERR("Failed to allocate database node");
|
|
|
|
atomic_clear_bit(bt_mesh.flags, BT_MESH_VALID);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = node->addr;
|
|
|
|
iv_index = bt_mesh_cdb.iv_index;
|
|
|
|
memcpy(node->dev_key, dev_key, 16);
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
|
|
|
|
bt_mesh_store_cdb_node(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 12:30:54 +03:00
|
|
|
err = bt_mesh_net_create(net_idx, flags, net_key, iv_index);
|
|
|
|
if (err) {
|
2019-01-25 18:24:44 +02:00
|
|
|
atomic_clear_bit(bt_mesh.flags, BT_MESH_VALID);
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT) && pb_gatt_enabled) {
|
2017-06-16 12:30:54 +03:00
|
|
|
bt_mesh_proxy_prov_enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-11-29 11:23:03 -08:00
|
|
|
bt_mesh.seq = 0U;
|
2017-06-16 12:30:54 +03:00
|
|
|
|
|
|
|
bt_mesh_comp_provision(addr);
|
|
|
|
|
|
|
|
memcpy(bt_mesh.dev_key, dev_key, 16);
|
|
|
|
|
2018-05-07 12:23:37 +03:00
|
|
|
if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
|
|
|
|
BT_DBG("Storing network information persistently");
|
2018-05-08 23:34:56 +03:00
|
|
|
bt_mesh_store_net();
|
2018-05-07 12:23:37 +03:00
|
|
|
bt_mesh_store_subnet(&bt_mesh.sub[0]);
|
2018-05-15 14:35:07 +03:00
|
|
|
bt_mesh_store_iv(false);
|
2018-05-07 12:23:37 +03:00
|
|
|
}
|
|
|
|
|
2019-12-20 12:18:14 +01:00
|
|
|
bt_mesh_start();
|
2017-11-14 18:45:32 +02:00
|
|
|
|
2017-06-16 12:30:54 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:05:00 +02:00
|
|
|
int bt_mesh_provision_adv(const u8_t uuid[16], u16_t net_idx, u16_t addr,
|
|
|
|
u8_t attention_duration)
|
|
|
|
{
|
|
|
|
if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bt_mesh_subnet_get(net_idx) == NULL) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_BT_MESH_PROVISIONER) &&
|
|
|
|
IS_ENABLED(CONFIG_BT_MESH_PB_ADV)) {
|
|
|
|
return bt_mesh_pb_adv_open(uuid, net_idx, addr,
|
|
|
|
attention_duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2017-06-16 12:30:54 +03:00
|
|
|
void bt_mesh_reset(void)
|
|
|
|
{
|
2019-01-25 15:57:45 +02:00
|
|
|
if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
|
2017-11-13 20:52:26 +02:00
|
|
|
return;
|
2017-06-16 12:30:54 +03:00
|
|
|
}
|
|
|
|
|
2018-11-29 11:23:03 -08:00
|
|
|
bt_mesh.iv_index = 0U;
|
|
|
|
bt_mesh.seq = 0U;
|
2019-01-25 15:57:45 +02:00
|
|
|
|
|
|
|
memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags));
|
2017-06-16 12:30:54 +03:00
|
|
|
|
2018-05-15 14:35:07 +03:00
|
|
|
k_delayed_work_cancel(&bt_mesh.ivu_timer);
|
2017-06-16 12:30:54 +03:00
|
|
|
|
2017-11-20 14:51:32 +02:00
|
|
|
bt_mesh_cfg_reset();
|
|
|
|
|
|
|
|
bt_mesh_rx_reset();
|
|
|
|
bt_mesh_tx_reset();
|
|
|
|
|
2017-08-09 09:21:11 +03:00
|
|
|
if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER)) {
|
2017-11-17 13:06:09 +02:00
|
|
|
bt_mesh_lpn_disable(true);
|
2017-06-16 12:30:54 +03:00
|
|
|
}
|
|
|
|
|
2017-11-27 21:02:38 +02:00
|
|
|
if (IS_ENABLED(CONFIG_BT_MESH_FRIEND)) {
|
|
|
|
bt_mesh_friend_clear_net_idx(BT_MESH_KEY_ANY);
|
|
|
|
}
|
|
|
|
|
2017-08-09 09:21:11 +03:00
|
|
|
if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) {
|
2017-06-16 12:30:54 +03:00
|
|
|
bt_mesh_proxy_gatt_disable();
|
|
|
|
}
|
|
|
|
|
2018-05-08 09:21:57 +03:00
|
|
|
if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
|
|
|
|
bt_mesh_clear_net();
|
|
|
|
}
|
|
|
|
|
2018-09-11 19:09:03 -07:00
|
|
|
(void)memset(bt_mesh.dev_key, 0, sizeof(bt_mesh.dev_key));
|
2017-06-16 12:30:54 +03:00
|
|
|
|
2017-11-13 20:52:26 +02:00
|
|
|
bt_mesh_scan_disable();
|
|
|
|
bt_mesh_beacon_disable();
|
|
|
|
|
2018-06-26 11:29:24 +03:00
|
|
|
bt_mesh_comp_unprovision();
|
|
|
|
|
2017-11-13 20:52:26 +02:00
|
|
|
if (IS_ENABLED(CONFIG_BT_MESH_PROV)) {
|
|
|
|
bt_mesh_prov_reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bt_mesh_is_provisioned(void)
|
|
|
|
{
|
2019-01-25 15:57:45 +02:00
|
|
|
return atomic_test_bit(bt_mesh.flags, BT_MESH_VALID);
|
2017-11-13 20:52:26 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 19:25:17 +02:00
|
|
|
static void model_suspend(struct bt_mesh_model *mod, struct bt_mesh_elem *elem,
|
|
|
|
bool vnd, bool primary, void *user_data)
|
|
|
|
{
|
|
|
|
if (mod->pub && mod->pub->update) {
|
2019-03-26 19:57:45 -06:00
|
|
|
mod->pub->count = 0U;
|
2019-01-25 19:25:17 +02:00
|
|
|
k_delayed_work_cancel(&mod->pub->timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int bt_mesh_suspend(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atomic_test_and_set_bit(bt_mesh.flags, BT_MESH_SUSPENDED)) {
|
|
|
|
return -EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bt_mesh_scan_disable();
|
|
|
|
if (err) {
|
|
|
|
atomic_clear_bit(bt_mesh.flags, BT_MESH_SUSPENDED);
|
|
|
|
BT_WARN("Disabling scanning failed (err %d)", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_mesh_hb_pub_disable();
|
|
|
|
|
|
|
|
if (bt_mesh_beacon_get() == BT_MESH_BEACON_ENABLED) {
|
|
|
|
bt_mesh_beacon_disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_mesh_model_foreach(model_suspend, NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void model_resume(struct bt_mesh_model *mod, struct bt_mesh_elem *elem,
|
|
|
|
bool vnd, bool primary, void *user_data)
|
|
|
|
{
|
|
|
|
if (mod->pub && mod->pub->update) {
|
|
|
|
s32_t period_ms = bt_mesh_model_pub_period_get(mod);
|
|
|
|
|
|
|
|
if (period_ms) {
|
|
|
|
k_delayed_work_submit(&mod->pub->timer, period_ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int bt_mesh_resume(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_SUSPENDED)) {
|
|
|
|
return -EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bt_mesh_scan_enable();
|
|
|
|
if (err) {
|
|
|
|
BT_WARN("Re-enabling scanning failed (err %d)", err);
|
|
|
|
atomic_set_bit(bt_mesh.flags, BT_MESH_SUSPENDED);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bt_mesh_beacon_get() == BT_MESH_BEACON_ENABLED) {
|
|
|
|
bt_mesh_beacon_enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_mesh_model_foreach(model_resume, NULL);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-06-16 12:30:54 +03:00
|
|
|
int bt_mesh_init(const struct bt_mesh_prov *prov,
|
|
|
|
const struct bt_mesh_comp *comp)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = bt_mesh_test();
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bt_mesh_comp_register(comp);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-01-27 13:01:14 +02:00
|
|
|
if (IS_ENABLED(CONFIG_BT_MESH_PROXY)) {
|
|
|
|
bt_mesh_proxy_init();
|
|
|
|
}
|
|
|
|
|
2017-08-09 09:21:11 +03:00
|
|
|
if (IS_ENABLED(CONFIG_BT_MESH_PROV)) {
|
2017-11-02 13:50:29 +01:00
|
|
|
err = bt_mesh_prov_init(prov);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
2017-06-16 12:30:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bt_mesh_net_init();
|
|
|
|
bt_mesh_trans_init();
|
|
|
|
bt_mesh_beacon_init();
|
|
|
|
bt_mesh_adv_init();
|
|
|
|
|
2018-05-07 22:22:30 +03:00
|
|
|
if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
|
|
|
|
bt_mesh_settings_init();
|
|
|
|
}
|
|
|
|
|
2017-06-16 12:30:54 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2019-12-20 12:18:14 +01:00
|
|
|
|
|
|
|
static void model_start(struct bt_mesh_model *mod, struct bt_mesh_elem *elem,
|
|
|
|
bool vnd, bool primary, void *user_data)
|
|
|
|
{
|
|
|
|
if (mod->cb && mod->cb->start) {
|
|
|
|
mod->cb->start(mod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int bt_mesh_start(void)
|
|
|
|
{
|
|
|
|
bt_mesh_net_start();
|
|
|
|
bt_mesh_model_foreach(model_start, NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|