1 /*
2  * Copyright (c) 2018 Henrik Brix Andersen <henrik@brixandersen.dk>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/types.h>
8 #include <stddef.h>
9 #include <zephyr/sys/printk.h>
10 #include <zephyr/sys/util.h>
11 
12 #include <zephyr/bluetooth/bluetooth.h>
13 #include <zephyr/bluetooth/hci.h>
14 
15 #ifndef IBEACON_RSSI
16 #define IBEACON_RSSI 0xc8
17 #endif
18 
19 /*
20  * Set iBeacon demo advertisement data. These values are for
21  * demonstration only and must be changed for production environments!
22  *
23  * UUID:  18ee1516-016b-4bec-ad96-bcb96d166e97
24  * Major: 0
25  * Minor: 0
26  * RSSI:  -56 dBm
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_MANUFACTURER_DATA,
31 		      0x4c, 0x00, /* Apple */
32 		      0x02, 0x15, /* iBeacon */
33 		      0x18, 0xee, 0x15, 0x16, /* UUID[15..12] */
34 		      0x01, 0x6b, /* UUID[11..10] */
35 		      0x4b, 0xec, /* UUID[9..8] */
36 		      0xad, 0x96, /* UUID[7..6] */
37 		      0xbc, 0xb9, 0x6d, 0x16, 0x6e, 0x97, /* UUID[5..0] */
38 		      0x00, 0x00, /* Major */
39 		      0x00, 0x00, /* Minor */
40 		      IBEACON_RSSI) /* Calibrated RSSI @ 1m */
41 };
42 
bt_ready(int err)43 static void bt_ready(int err)
44 {
45 	if (err) {
46 		printk("Bluetooth init failed (err %d)\n", err);
47 		return;
48 	}
49 
50 	printk("Bluetooth initialized\n");
51 
52 	/* Start advertising */
53 	err = bt_le_adv_start(BT_LE_ADV_NCONN, ad, ARRAY_SIZE(ad),
54 			      NULL, 0);
55 	if (err) {
56 		printk("Advertising failed to start (err %d)\n", err);
57 		return;
58 	}
59 
60 	printk("iBeacon started\n");
61 }
62 
main(void)63 int main(void)
64 {
65 	int err;
66 
67 	printk("Starting iBeacon Demo\n");
68 
69 	/* Initialize the Bluetooth Subsystem */
70 	err = bt_enable(bt_ready);
71 	if (err) {
72 		printk("Bluetooth init failed (err %d)\n", err);
73 	}
74 	return 0;
75 }
76