1 /* main.c - Application main entry point */
2
3 /*
4 * Copyright (c) 2015-2016 Intel Corporation
5 * Copyright (c) 2021 Linaro Limited
6 *
7 * SPDX-License-Identifier: Apache-2.0
8 */
9
10 #include <zephyr/types.h>
11 #include <zephyr/device.h>
12 #include <stddef.h>
13 #include <zephyr/sys/printk.h>
14 #include <zephyr/sys/util.h>
15 #include <zephyr/sys/poweroff.h>
16
17 #include <zephyr/bluetooth/bluetooth.h>
18 #include <zephyr/bluetooth/hci.h>
19
20 #define DEVICE_NAME CONFIG_BT_DEVICE_NAME
21 #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
22
23 /*
24 * Set Advertisement data. Based on the Eddystone specification:
25 * https://github.com/google/eddystone/blob/master/protocol-specification.md
26 * https://github.com/google/eddystone/tree/master/eddystone-url
27 */
28 static const struct bt_data ad[] = {
29 BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR),
30 BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0xaa, 0xfe),
31 BT_DATA_BYTES(BT_DATA_SVC_DATA16,
32 0xaa, 0xfe, /* Eddystone UUID */
33 0x10, /* Eddystone-URL frame type */
34 0x00, /* Calibrated Tx power at 0m */
35 0x00, /* URL Scheme Prefix http://www. */
36 'z', 'e', 'p', 'h', 'y', 'r',
37 'p', 'r', 'o', 'j', 'e', 'c', 't',
38 0x08) /* .org */
39 };
40
41 /* Set Scan Response data */
42 static const struct bt_data sd[] = {
43 BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
44 };
45
bt_ready(int err)46 static void bt_ready(int err)
47 {
48 char addr_s[BT_ADDR_LE_STR_LEN];
49 bt_addr_le_t addr = {0};
50 size_t count = 1;
51
52 if (err) {
53 printk("Bluetooth init failed (err %d)\n", err);
54 return;
55 }
56
57 printk("Bluetooth initialized\n");
58
59 k_sleep(K_MSEC(500));
60
61 /* Start advertising */
62 err = bt_le_adv_start(BT_LE_ADV_NCONN_IDENTITY, ad, ARRAY_SIZE(ad),
63 sd, ARRAY_SIZE(sd));
64 if (err) {
65 printk("Advertising failed to start (err %d)\n", err);
66 return;
67 }
68
69 /* For connectable advertising you would use
70 * bt_le_oob_get_local(). For non-connectable non-identity
71 * advertising an non-resolvable private address is used;
72 * there is no API to retrieve that.
73 */
74
75 bt_id_get(&addr, &count);
76 bt_addr_le_to_str(&addr, addr_s, sizeof(addr_s));
77
78 printk("Beacon started, advertising as %s\n", addr_s);
79
80 k_sleep(K_SECONDS(2));
81
82 err = bt_le_adv_stop();
83 if (err != 0) {
84 printk("Advertising failed to stop: %d", err);
85 return;
86 }
87
88 printk("Beacon stopped\n");
89
90 k_sleep(K_SECONDS(1));
91
92 /* Start advertising */
93 err = bt_le_adv_start(BT_LE_ADV_NCONN_IDENTITY, ad, ARRAY_SIZE(ad),
94 sd, ARRAY_SIZE(sd));
95 if (err) {
96 printk("Advertising failed to start (err %d)\n", err);
97 return;
98 }
99
100 printk("Beacon started\n");
101
102 err = bt_le_adv_stop();
103 if (err != 0) {
104 printk("Advertising failed to stop: %d", err);
105 return;
106 }
107
108 printk("Beacon stopped\n");
109 }
110
main(void)111 int main(void)
112 {
113 int err;
114
115 printk("Starting Beacon Demo\n");
116 /* Initialize the Bluetooth Subsystem */
117 err = bt_enable(bt_ready);
118 if (err) {
119 printk("Bluetooth init failed (err %d)\n", err);
120 }
121
122 /* Give time to bt_ready sequence */
123 k_sleep(K_SECONDS(6));
124
125 printk("BLE disable\n");
126 err = bt_disable();
127 if (err) {
128 printk("Bluetooth disable failed (err %d)\n", err);
129 }
130
131 k_sleep(K_SECONDS(2));
132
133 printk("BLE restart\n");
134 /* Initialize the Bluetooth Subsystem */
135 err = bt_enable(bt_ready);
136 if (err) {
137 printk("Bluetooth init failed (err %d)\n", err);
138 }
139
140 /* Give time to bt_ready sequence */
141 k_sleep(K_SECONDS(6));
142
143 printk("BLE disable\n");
144 err = bt_disable();
145 if (err) {
146 printk("Bluetooth disable failed (err %d)\n", err);
147 }
148
149 printk("Powering off\n");
150 sys_poweroff();
151
152 return 0;
153 }
154