summaryrefslogtreecommitdiff
path: root/src/libs/mynewt-nimble/nimble/host/mesh/src/mesh.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs/mynewt-nimble/nimble/host/mesh/src/mesh.c')
-rw-r--r--src/libs/mynewt-nimble/nimble/host/mesh/src/mesh.c231
1 files changed, 136 insertions, 95 deletions
diff --git a/src/libs/mynewt-nimble/nimble/host/mesh/src/mesh.c b/src/libs/mynewt-nimble/nimble/host/mesh/src/mesh.c
index 52fbdbf6..bfb0e1af 100644
--- a/src/libs/mynewt-nimble/nimble/host/mesh/src/mesh.c
+++ b/src/libs/mynewt-nimble/nimble/host/mesh/src/mesh.c
@@ -18,7 +18,12 @@
#include "adv.h"
#include "prov.h"
+#include "provisioner.h"
#include "net.h"
+#include "subnet.h"
+#include "app_keys.h"
+#include "rpl.h"
+#include "cfg.h"
#include "beacon.h"
#include "lpn.h"
#include "friend.h"
@@ -26,16 +31,18 @@
#include "access.h"
#include "foundation.h"
#include "proxy.h"
+#include "heartbeat.h"
#include "shell.h"
#include "mesh_priv.h"
#include "settings.h"
-u8_t g_mesh_addr_type;
+
+uint8_t g_mesh_addr_type;
static struct ble_gap_event_listener mesh_event_listener;
-int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx,
- u8_t flags, u32_t iv_index, u16_t addr,
- const u8_t dev_key[16])
+int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
+ uint8_t flags, uint32_t iv_index, uint16_t addr,
+ const uint8_t dev_key[16])
{
bool pb_gatt_enabled;
int err;
@@ -58,6 +65,53 @@ int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx,
pb_gatt_enabled = false;
}
+ /*
+ * FIXME:
+ * Should net_key and iv_index be over-ridden?
+ */
+ if (IS_ENABLED(BLE_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);
+ }
+ }
+
err = bt_mesh_net_create(net_idx, flags, net_key, iv_index);
if (err) {
atomic_clear_bit(bt_mesh.flags, BT_MESH_VALID);
@@ -75,20 +129,18 @@ int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx,
memcpy(bt_mesh.dev_key, dev_key, 16);
- if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
- BT_DBG("Storing network information persistently");
- bt_mesh_store_net();
- bt_mesh_store_subnet(&bt_mesh.sub[0]);
- bt_mesh_store_iv(false);
+ if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER) &&
+ IS_ENABLED(CONFIG_BT_MESH_LPN_SUB_ALL_NODES_ADDR)) {
+ bt_mesh_lpn_group_add(BT_MESH_ADDR_ALL_NODES);
}
- bt_mesh_net_start();
+ bt_mesh_start();
return 0;
}
-int bt_mesh_provision_adv(const u8_t uuid[16], u16_t net_idx, u16_t addr,
- u8_t attention_duration)
+int bt_mesh_provision_adv(const uint8_t uuid[16], uint16_t net_idx, uint16_t addr,
+ uint8_t attention_duration)
{
if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
return -EINVAL;
@@ -122,25 +174,30 @@ void bt_mesh_reset(void)
bt_mesh_cfg_reset();
- bt_mesh_rx_reset();
- bt_mesh_tx_reset();
+ bt_mesh_trans_reset();
+ bt_mesh_app_keys_reset();
+ bt_mesh_net_keys_reset();
+
+ bt_mesh_net_loopback_clear(BT_MESH_KEY_ANY);
if ((MYNEWT_VAL(BLE_MESH_LOW_POWER))) {
+ if (IS_ENABLED(CONFIG_BT_MESH_LPN_SUB_ALL_NODES_ADDR)) {
+ uint16_t group = BT_MESH_ADDR_ALL_NODES;
+
+ bt_mesh_lpn_group_del(&group, 1);
+ }
+
bt_mesh_lpn_disable(true);
}
if ((MYNEWT_VAL(BLE_MESH_FRIEND))) {
- bt_mesh_friend_clear_net_idx(BT_MESH_KEY_ANY);
+ bt_mesh_friends_clear();
}
if ((MYNEWT_VAL(BLE_MESH_GATT_PROXY))) {
bt_mesh_proxy_gatt_disable();
}
- if ((MYNEWT_VAL(BLE_MESH_PB_GATT))) {
- bt_mesh_proxy_prov_enable();
- }
-
if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
bt_mesh_clear_net();
}
@@ -162,55 +219,6 @@ bool bt_mesh_is_provisioned(void)
return atomic_test_bit(bt_mesh.flags, BT_MESH_VALID);
}
-int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers)
-{
- if (bt_mesh_is_provisioned()) {
- return -EALREADY;
- }
-
- char uuid_buf[BLE_UUID_STR_LEN];
- const struct bt_mesh_prov *prov = bt_mesh_prov_get();
- ble_uuid_t *uuid = BLE_UUID128_DECLARE();
-
- memcpy(BLE_UUID128(uuid)->value, prov->uuid, 16);
- BT_INFO("Device UUID: %s", ble_uuid_to_str(uuid, uuid_buf));
-
- if (IS_ENABLED(CONFIG_BT_MESH_PB_ADV) &&
- (bearers & BT_MESH_PROV_ADV)) {
- /* Make sure we're scanning for provisioning inviations */
- bt_mesh_scan_enable();
- /* Enable unprovisioned beacon sending */
- bt_mesh_beacon_enable();
- }
-
- if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT) &&
- (bearers & BT_MESH_PROV_GATT)) {
- bt_mesh_proxy_prov_enable();
- bt_mesh_adv_update();
- }
-
- return 0;
-}
-
-int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers)
-{
- if (bt_mesh_is_provisioned()) {
- return -EALREADY;
- }
-
- if (IS_ENABLED(CONFIG_BT_MESH_PB_ADV) &&
- (bearers & BT_MESH_PROV_ADV)) {
- bt_mesh_beacon_disable();
- bt_mesh_scan_disable();
- }
-
- if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT) &&
- (bearers & BT_MESH_PROV_GATT)) {
- bt_mesh_proxy_prov_disable(true);
- }
-
- return 0;
-}
static int bt_mesh_gap_event(struct ble_gap_event *event, void *arg)
{
@@ -251,9 +259,9 @@ int bt_mesh_suspend(void)
return err;
}
- bt_mesh_hb_pub_disable();
+ bt_mesh_hb_suspend();
- if (bt_mesh_beacon_get() == BT_MESH_BEACON_ENABLED) {
+ if (bt_mesh_beacon_enabled()) {
bt_mesh_beacon_disable();
}
@@ -266,7 +274,7 @@ 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);
+ int32_t period_ms = bt_mesh_model_pub_period_get(mod);
if (period_ms) {
k_delayed_work_submit(&mod->pub->timer, period_ms);
@@ -293,7 +301,9 @@ int bt_mesh_resume(void)
return err;
}
- if (bt_mesh_beacon_get() == BT_MESH_BEACON_ENABLED) {
+ bt_mesh_hb_resume();
+
+ if (bt_mesh_beacon_enabled()) {
bt_mesh_beacon_enable();
}
@@ -317,45 +327,76 @@ int bt_mesh_init(uint8_t own_addr_type, const struct bt_mesh_prov *prov,
return err;
}
-#if (MYNEWT_VAL(BLE_MESH_PROV))
- err = bt_mesh_prov_init(prov);
- if (err) {
- return err;
- }
-#endif
-
#if (MYNEWT_VAL(BLE_MESH_PROXY))
bt_mesh_proxy_init();
#endif
#if (MYNEWT_VAL(BLE_MESH_PROV))
- /* Need this to proper link.rx.buf allocation */
- bt_mesh_prov_reset_link();
+ err = bt_mesh_prov_init(prov);
+ if (err) {
+ return err;
+ }
#endif
+ bt_mesh_cfg_init();
bt_mesh_net_init();
bt_mesh_trans_init();
+ bt_mesh_hb_init();
bt_mesh_beacon_init();
bt_mesh_adv_init();
-#if (MYNEWT_VAL(BLE_MESH_PB_ADV))
- /* Make sure we're scanning for provisioning inviations */
- bt_mesh_scan_enable();
- /* Enable unprovisioned beacon sending */
-
- bt_mesh_beacon_enable();
-#endif
-
-#if (MYNEWT_VAL(BLE_MESH_PB_GATT))
- bt_mesh_proxy_prov_enable();
+#if (MYNEWT_VAL(BLE_MESH_SETTINGS))
+ bt_mesh_settings_init();
#endif
ble_gap_event_listener_register(&mesh_event_listener,
bt_mesh_gap_event, NULL);
-#if (MYNEWT_VAL(BLE_MESH_SETTINGS))
- bt_mesh_settings_init();
-#endif
-
return 0;
}
+
+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)
+{
+ if (bt_mesh_beacon_enabled()) {
+ bt_mesh_beacon_enable();
+ } else {
+ bt_mesh_beacon_disable();
+ }
+
+ if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY) &&
+ bt_mesh_gatt_proxy_get() != BT_MESH_GATT_PROXY_NOT_SUPPORTED) {
+ bt_mesh_proxy_gatt_enable();
+ bt_mesh_adv_update();
+ }
+
+ if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER)) {
+ bt_mesh_lpn_init();
+ } else {
+ bt_mesh_scan_enable();
+ }
+
+ if (IS_ENABLED(CONFIG_BT_MESH_FRIEND)) {
+ bt_mesh_friend_init();
+ }
+
+ if (IS_ENABLED(CONFIG_BT_MESH_PROV)) {
+ struct bt_mesh_subnet *sub = bt_mesh_subnet_next(NULL);
+ uint16_t addr = bt_mesh_primary_addr();
+
+ bt_mesh_prov_complete(sub->net_idx, addr);
+ }
+
+ bt_mesh_hb_start();
+
+ bt_mesh_model_foreach(model_start, NULL);
+
+ return 0;
+} \ No newline at end of file