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 <errno.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/sys/printk.h>
14
15 #include <zephyr/bluetooth/bluetooth.h>
16 #include <zephyr/bluetooth/hci.h>
17 #include <zephyr/bluetooth/conn.h>
18 #include <zephyr/bluetooth/uuid.h>
19 #include <zephyr/bluetooth/gatt.h>
20 #include <zephyr/sys/byteorder.h>
21
22 static void start_scan(void);
23
24 static struct bt_conn *default_conn;
25
device_found(const bt_addr_le_t * addr,int8_t rssi,uint8_t type,struct net_buf_simple * ad)26 static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
27 struct net_buf_simple *ad)
28 {
29 char addr_str[BT_ADDR_LE_STR_LEN];
30 int err;
31
32 if (default_conn) {
33 return;
34 }
35
36 /* We're only interested in connectable events */
37 if (type != BT_GAP_ADV_TYPE_ADV_IND &&
38 type != BT_GAP_ADV_TYPE_ADV_DIRECT_IND) {
39 return;
40 }
41
42 bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
43 printk("Device found: %s (RSSI %d)\n", addr_str, rssi);
44
45 /* connect only to devices in close proximity */
46 if (rssi < -70) {
47 return;
48 }
49
50 if (bt_le_scan_stop()) {
51 return;
52 }
53
54 err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
55 BT_LE_CONN_PARAM_DEFAULT, &default_conn);
56 if (err) {
57 printk("Create conn to %s failed (%d)\n", addr_str, err);
58 start_scan();
59 }
60 }
61
start_scan(void)62 static void start_scan(void)
63 {
64 int err;
65
66 /* This demo doesn't require active scan */
67 err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found);
68 if (err) {
69 printk("Scanning failed to start (err %d)\n", err);
70 return;
71 }
72
73 printk("Scanning successfully started\n");
74 }
75
connected(struct bt_conn * conn,uint8_t err)76 static void connected(struct bt_conn *conn, uint8_t err)
77 {
78 char addr[BT_ADDR_LE_STR_LEN];
79
80 bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
81
82 if (err) {
83 printk("Failed to connect to %s (%u)\n", addr, err);
84
85 bt_conn_unref(default_conn);
86 default_conn = NULL;
87
88 start_scan();
89 return;
90 }
91
92 if (conn != default_conn) {
93 return;
94 }
95
96 printk("Connected: %s\n", addr);
97
98 bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
99 }
100
disconnected(struct bt_conn * conn,uint8_t reason)101 static void disconnected(struct bt_conn *conn, uint8_t reason)
102 {
103 char addr[BT_ADDR_LE_STR_LEN];
104
105 if (conn != default_conn) {
106 return;
107 }
108
109 bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
110
111 printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
112
113 bt_conn_unref(default_conn);
114 default_conn = NULL;
115
116 start_scan();
117 }
118
119 BT_CONN_CB_DEFINE(conn_callbacks) = {
120 .connected = connected,
121 .disconnected = disconnected,
122 };
123
main(void)124 int main(void)
125 {
126 int err;
127
128 err = bt_enable(NULL);
129 if (err) {
130 printk("Bluetooth init failed (err %d)\n", err);
131 return 0;
132 }
133
134 printk("Bluetooth initialized\n");
135
136 start_scan();
137 return 0;
138 }
139