1 /*
2 * Copyright (c) 2012-2014 Wind River Systems, Inc.
3 * Copyright (c) 2020 Prevas A/S
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/bluetooth/bluetooth.h>
9 #include <zephyr/bluetooth/conn.h>
10 #include <zephyr/bluetooth/gatt.h>
11 #include <zephyr/mgmt/mcumgr/transport/smp_bt.h>
12
13 #define LOG_LEVEL LOG_LEVEL_DBG
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_REGISTER(smp_bt_sample);
16
17 static struct k_work advertise_work;
18
19 static const struct bt_data ad[] = {
20 BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
21 BT_DATA_BYTES(BT_DATA_UUID128_ALL,
22 0x84, 0xaa, 0x60, 0x74, 0x52, 0x8a, 0x8b, 0x86,
23 0xd3, 0x4c, 0xb7, 0x1d, 0x1d, 0xdc, 0x53, 0x8d),
24 };
25
advertise(struct k_work * work)26 static void advertise(struct k_work *work)
27 {
28 int rc;
29
30 bt_le_adv_stop();
31
32 rc = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
33 if (rc) {
34 LOG_ERR("Advertising failed to start (rc %d)", rc);
35 return;
36 }
37
38 LOG_INF("Advertising successfully started");
39 }
40
connected(struct bt_conn * conn,uint8_t err)41 static void connected(struct bt_conn *conn, uint8_t err)
42 {
43 if (err) {
44 LOG_ERR("Connection failed (err 0x%02x)", err);
45 } else {
46 LOG_INF("Connected");
47 }
48 }
49
disconnected(struct bt_conn * conn,uint8_t reason)50 static void disconnected(struct bt_conn *conn, uint8_t reason)
51 {
52 LOG_INF("Disconnected (reason 0x%02x)", reason);
53 k_work_submit(&advertise_work);
54 }
55
56 BT_CONN_CB_DEFINE(conn_callbacks) = {
57 .connected = connected,
58 .disconnected = disconnected,
59 };
60
bt_ready(int err)61 static void bt_ready(int err)
62 {
63 if (err != 0) {
64 LOG_ERR("Bluetooth failed to initialise: %d", err);
65 } else {
66 k_work_submit(&advertise_work);
67 }
68 }
69
start_smp_bluetooth_adverts(void)70 void start_smp_bluetooth_adverts(void)
71 {
72 int rc;
73
74 k_work_init(&advertise_work, advertise);
75 rc = bt_enable(bt_ready);
76
77 if (rc != 0) {
78 LOG_ERR("Bluetooth enable failed: %d", rc);
79 }
80 }
81