diff --git a/include/bluetooth/mesh/main.h b/include/bluetooth/mesh/main.h index 3489ebdb80c..b1532da9ac7 100644 --- a/include/bluetooth/mesh/main.h +++ b/include/bluetooth/mesh/main.h @@ -34,6 +34,11 @@ typedef enum { BT_MESH_ENTER_STRING = BIT(3), } bt_mesh_input_action_t; +typedef enum { + BT_MESH_PROV_ADV = BIT(0), + BT_MESH_PROV_GATT = BIT(1), +} bt_mesh_prov_bearer_t; + struct bt_mesh_prov { const u8_t *uuid; @@ -51,14 +56,18 @@ struct bt_mesh_prov { int (*input)(bt_mesh_input_action_t act, u8_t size); - void (*link_open)(bt); - void (*link_close)(void); + void (*link_open)(bt_mesh_prov_bearer_t bearer); + void (*link_close)(bt_mesh_prov_bearer_t bearer); void (*complete)(u16_t addr); + void (*reset)(void); }; int bt_mesh_input_string(const char *str); int bt_mesh_input_number(u32_t num); +int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers); +int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers); + /** * @} */ diff --git a/samples/bluetooth/mesh/src/main.c b/samples/bluetooth/mesh/src/main.c index c9616bff4d2..99873725ef2 100644 --- a/samples/bluetooth/mesh/src/main.c +++ b/samples/bluetooth/mesh/src/main.c @@ -152,6 +152,11 @@ static void prov_complete(u16_t addr) board_prov_complete(); } +static void prov_reset(void) +{ + bt_mesh_prov_enable(BT_MESH_PROV_ADV | BT_MESH_PROV_GATT); +} + static const uint8_t dev_uuid[16] = { 0xdd, 0xdd }; static const struct bt_mesh_prov prov = { @@ -160,6 +165,7 @@ static const struct bt_mesh_prov prov = { .output_actions = BT_MESH_DISPLAY_NUMBER, .output_number = output_number, .complete = prov_complete, + .reset = prov_reset, }; static void bt_ready(int err) @@ -179,6 +185,8 @@ static void bt_ready(int err) return; } + bt_mesh_prov_enable(BT_MESH_PROV_ADV | BT_MESH_PROV_GATT); + printk("Mesh initialized\n"); } diff --git a/subsys/bluetooth/host/mesh/beacon.c b/subsys/bluetooth/host/mesh/beacon.c index 154d3adb0aa..e16f3416d09 100644 --- a/subsys/bluetooth/host/mesh/beacon.c +++ b/subsys/bluetooth/host/mesh/beacon.c @@ -351,9 +351,6 @@ void bt_mesh_beacon_recv(struct net_buf_simple *buf) void bt_mesh_beacon_init(void) { k_delayed_work_init(&beacon_timer, beacon_send); - - /* Start beaconing since we're unprovisioned */ - k_work_submit(&beacon_timer.work); } void bt_mesh_beacon_ivu_initiator(bool enable) diff --git a/subsys/bluetooth/host/mesh/main.c b/subsys/bluetooth/host/mesh/main.c index 515f3dcfb3a..b0f7ed2e5d5 100644 --- a/subsys/bluetooth/host/mesh/main.c +++ b/subsys/bluetooth/host/mesh/main.c @@ -95,7 +95,7 @@ int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx, void bt_mesh_reset(void) { if (!provisioned) { - goto enable_beacon; + return; } bt_mesh_comp_unprovision(); @@ -119,25 +119,17 @@ void bt_mesh_reset(void) bt_mesh_proxy_gatt_disable(); } - if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT)) { - bt_mesh_proxy_prov_enable(); - } - memset(bt_mesh.dev_key, 0, sizeof(bt_mesh.dev_key)); memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl)); provisioned = false; -enable_beacon: - if (IS_ENABLED(CONFIG_BT_MESH_PB_ADV)) { - /* Make sure we're scanning for provisioning inviations */ - bt_mesh_scan_enable(); - /* Enable unprovisioned beacon sending */ - bt_mesh_beacon_enable(); - } else { - bt_mesh_scan_disable(); - bt_mesh_beacon_disable(); + bt_mesh_scan_disable(); + bt_mesh_beacon_disable(); + + if (IS_ENABLED(CONFIG_BT_MESH_PROV)) { + bt_mesh_prov_reset(); } } @@ -146,6 +138,50 @@ bool bt_mesh_is_provisioned(void) return provisioned; } +int bt_mesh_prov_enable(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)) { + /* 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(); + bt_mesh_adv_update(); + } + + return 0; +} + int bt_mesh_init(const struct bt_mesh_prov *prov, const struct bt_mesh_comp *comp) { @@ -177,16 +213,5 @@ int bt_mesh_init(const struct bt_mesh_prov *prov, bt_mesh_proxy_init(); } - if (IS_ENABLED(CONFIG_BT_MESH_PB_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)) { - bt_mesh_proxy_prov_enable(); - } - return 0; } diff --git a/subsys/bluetooth/host/mesh/prov.c b/subsys/bluetooth/host/mesh/prov.c index 0e7433f6859..901919a5054 100644 --- a/subsys/bluetooth/host/mesh/prov.c +++ b/subsys/bluetooth/host/mesh/prov.c @@ -230,7 +230,7 @@ static void reset_link(void) prov_clear_tx(); if (prov->link_close) { - prov->link_close(); + prov->link_close(BT_MESH_PROV_ADV); } /* Clear everything except the retransmit delayed work config */ @@ -1167,7 +1167,7 @@ static void link_open(struct prov_rx *rx, struct net_buf_simple *buf) } if (prov->link_open) { - prov->link_open(); + prov->link_open(BT_MESH_PROV_ADV); } link.id = rx->link_id; @@ -1472,7 +1472,7 @@ int bt_mesh_pb_gatt_open(struct bt_conn *conn) link.expect = PROV_INVITE; if (prov->link_open) { - prov->link_open(); + prov->link_open(BT_MESH_PROV_GATT); } return 0; @@ -1495,7 +1495,7 @@ int bt_mesh_pb_gatt_close(struct bt_conn *conn) } if (prov->link_close) { - prov->link_close(); + prov->link_close(BT_MESH_PROV_GATT); } bt_conn_unref(link.conn); @@ -1558,3 +1558,10 @@ int bt_mesh_prov_init(const struct bt_mesh_prov *prov_info) return 0; } + +void bt_mesh_prov_reset(void) +{ + if (prov->reset) { + prov->reset(); + } +} diff --git a/subsys/bluetooth/host/mesh/prov.h b/subsys/bluetooth/host/mesh/prov.h index bb73df3b338..778356a3968 100644 --- a/subsys/bluetooth/host/mesh/prov.h +++ b/subsys/bluetooth/host/mesh/prov.h @@ -17,3 +17,5 @@ int bt_mesh_pb_gatt_recv(struct bt_conn *conn, struct net_buf_simple *buf); const u8_t *bt_mesh_prov_get_uuid(void); int bt_mesh_prov_init(const struct bt_mesh_prov *prov); + +void bt_mesh_prov_reset(void); diff --git a/subsys/bluetooth/host/mesh/proxy.c b/subsys/bluetooth/host/mesh/proxy.c index 005e33e2359..7e435b0ff69 100644 --- a/subsys/bluetooth/host/mesh/proxy.c +++ b/subsys/bluetooth/host/mesh/proxy.c @@ -1043,6 +1043,10 @@ s32_t bt_mesh_proxy_adv_start(void) { BT_DBG(""); + if (gatt_svc == MESH_GATT_NONE) { + return K_FOREVER; + } + #if defined(CONFIG_BT_MESH_PB_GATT) if (!bt_mesh_is_provisioned()) { if (bt_le_adv_start(proxy_adv_param, diff --git a/subsys/bluetooth/host/mesh/shell.c b/subsys/bluetooth/host/mesh/shell.c index 84170f304d8..ea2b4100924 100644 --- a/subsys/bluetooth/host/mesh/shell.c +++ b/subsys/bluetooth/host/mesh/shell.c @@ -73,6 +73,11 @@ static void prov_complete(u16_t addr) dst = addr; } +static void prov_reset(void) +{ + printk("The local node has been reset and needs reprovisioning\n"); +} + static int output_number(bt_mesh_output_action_t action, uint32_t number) { printk("OOB Number: %u\n", number); @@ -168,14 +173,26 @@ static int input(bt_mesh_input_action_t act, u8_t size) return 0; } -static void link_open(void) +static const char *bearer2str(bt_mesh_prov_bearer_t bearer) { - printk("Provisioning link opened\n"); + switch (bearer) { + case BT_MESH_PROV_ADV: + return "PB-ADV"; + case BT_MESH_PROV_GATT: + return "PB-GATT"; + default: + return "unknown"; + } } -static void link_close(void) +static void link_open(bt_mesh_prov_bearer_t bearer) { - printk("Provisioning link closed\n"); + printk("Provisioning link opened on %s\n", bearer2str(bearer)); +} + +static void link_close(bt_mesh_prov_bearer_t bearer) +{ + printk("Provisioning link closed on %s\n", bearer2str(bearer)); } static const u8_t static_val[] = { @@ -187,6 +204,7 @@ static const struct bt_mesh_prov prov = { .link_open = link_open, .link_close = link_close, .complete = prov_complete, + .reset = prov_reset, .static_val = static_val, .static_val_len = sizeof(static_val), .output_size = 6, @@ -660,8 +678,59 @@ static int cmd_hb_sub_set(int argc, char *argv[]) return 0; } +#if defined(CONFIG_BT_MESH_PROV) +static int cmd_pb(bt_mesh_prov_bearer_t bearer, int argc, char *argv[]) +{ + int err; + + if (argc < 2) { + return -EINVAL; + } + + if (str2bool(argv[1])) { + err = bt_mesh_prov_enable(bearer); + if (err) { + printk("Failed to enable %s\n (err %d)", + bearer2str(bearer), err); + } else { + printk("%s enabled\n", bearer2str(bearer)); + } + } else { + err = bt_mesh_prov_disable(bearer); + if (err) { + printk("Failed to disable %s (err %d)\n", + bearer2str(bearer), err); + } else { + printk("%s disabled\n", bearer2str(bearer)); + } + } + + return 0; +} +#endif + +#if defined(CONFIG_BT_MESH_PB_ADV) +static int cmd_pb_adv(int argc, char *argv[]) +{ + return cmd_pb(BT_MESH_PROV_ADV, argc, argv); +} +#endif /* CONFIG_BT_MESH_PB_ADV */ + +#if defined(CONFIG_BT_MESH_PB_GATT) +static int cmd_pb_gatt(int argc, char *argv[]) +{ + return cmd_pb(BT_MESH_PROV_GATT, argc, argv); +} +#endif /* CONFIG_BT_MESH_PB_GATT */ + static const struct shell_cmd mesh_commands[] = { { "init", cmd_init, NULL }, +#if defined(CONFIG_BT_MESH_PB_ADV) + { "pb-adv", cmd_pb_adv, "" }, +#endif +#if defined(CONFIG_BT_MESH_PB_GATT) + { "pb-gatt", cmd_pb_gatt, "" }, +#endif { "reset", cmd_reset, NULL }, { "input-num", cmd_input_num, "" }, { "input-str", cmd_input_str, "" }, diff --git a/tests/bluetooth/mesh/src/main.c b/tests/bluetooth/mesh/src/main.c index c901c39ce61..900f165f9cd 100644 --- a/tests/bluetooth/mesh/src/main.c +++ b/tests/bluetooth/mesh/src/main.c @@ -161,6 +161,11 @@ static void prov_complete(u16_t addr) board_prov_complete(); } +static void prov_reset(void) +{ + bt_mesh_prov_enable(BT_MESH_PROV_ADV | BT_MESH_PROV_GATT); +} + static const u8_t dev_uuid[16] = { 0xdd, 0xdd }; static const struct bt_mesh_prov prov = { @@ -171,6 +176,7 @@ static const struct bt_mesh_prov prov = { .output_number = output_number, #endif .complete = prov_complete, + .reset = prov_reset, }; static void bt_ready(int err) @@ -190,6 +196,8 @@ static void bt_ready(int err) return; } + bt_mesh_prov_enable(BT_MESH_PROV_ADV | BT_MESH_PROV_GATT); + printk("Mesh initialized\n"); }