1 /* main.c - Application main entry point */
2
3 /*
4 * Copyright (c) 2015-2016 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #include <zephyr/types.h>
10 #include <stddef.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <zephyr/sys/printk.h>
14 #include <zephyr/sys/byteorder.h>
15 #include <zephyr/kernel.h>
16
17 #include <zephyr/bluetooth/bluetooth.h>
18 #include <zephyr/bluetooth/hci.h>
19 #include <zephyr/bluetooth/conn.h>
20 #include <zephyr/bluetooth/uuid.h>
21 #include <zephyr/bluetooth/gatt.h>
22 #include <zephyr/settings/settings.h>
23
24 static const struct bt_data ad[] = {
25 BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
26 BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_DIS_VAL)),
27 };
28
29 static const struct bt_data sd[] = {
30 BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
31 };
32
connected(struct bt_conn * conn,uint8_t err)33 static void connected(struct bt_conn *conn, uint8_t err)
34 {
35 if (err) {
36 printk("Connection failed, err 0x%02x %s\n", err, bt_hci_err_to_str(err));
37 } else {
38 printk("Connected\n");
39 }
40 }
41
disconnected(struct bt_conn * conn,uint8_t reason)42 static void disconnected(struct bt_conn *conn, uint8_t reason)
43 {
44 printk("Disconnected, reason 0x%02x %s\n", reason, bt_hci_err_to_str(reason));
45 }
46
47 BT_CONN_CB_DEFINE(conn_callbacks) = {
48 .connected = connected,
49 .disconnected = disconnected,
50 };
51
settings_runtime_load(void)52 static int settings_runtime_load(void)
53 {
54 #if defined(CONFIG_BT_DIS_SETTINGS)
55 settings_runtime_set("bt/dis/model",
56 "Zephyr Model",
57 sizeof("Zephyr Model"));
58 settings_runtime_set("bt/dis/manuf",
59 "Zephyr Manufacturer",
60 sizeof("Zephyr Manufacturer"));
61 #if defined(CONFIG_BT_DIS_SERIAL_NUMBER)
62 settings_runtime_set("bt/dis/serial",
63 CONFIG_BT_DIS_SERIAL_NUMBER_STR,
64 sizeof(CONFIG_BT_DIS_SERIAL_NUMBER_STR));
65 #endif
66 #if defined(CONFIG_BT_DIS_SW_REV)
67 settings_runtime_set("bt/dis/sw",
68 CONFIG_BT_DIS_SW_REV_STR,
69 sizeof(CONFIG_BT_DIS_SW_REV_STR));
70 #endif
71 #if defined(CONFIG_BT_DIS_FW_REV)
72 settings_runtime_set("bt/dis/fw",
73 CONFIG_BT_DIS_FW_REV_STR,
74 sizeof(CONFIG_BT_DIS_FW_REV_STR));
75 #endif
76 #if defined(CONFIG_BT_DIS_HW_REV)
77 settings_runtime_set("bt/dis/hw",
78 CONFIG_BT_DIS_HW_REV_STR,
79 sizeof(CONFIG_BT_DIS_HW_REV_STR));
80 #endif
81 #endif
82 return 0;
83 }
84
main(void)85 int main(void)
86 {
87 int err;
88
89 err = bt_enable(NULL);
90 if (err) {
91 printk("Bluetooth init failed (err %d)\n", err);
92 return 0;
93 }
94
95 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
96 settings_load();
97 }
98
99 settings_runtime_load();
100
101 printk("Bluetooth initialized\n");
102
103 err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
104 if (err) {
105 printk("Advertising failed to start (err %d)\n", err);
106 return 0;
107 }
108
109 printk("Advertising successfully started\n");
110 return 0;
111 }
112