1 /* main.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 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/settings/settings.h>
18 
19 #include <zephyr/bluetooth/bluetooth.h>
20 #include <zephyr/bluetooth/hci.h>
21 #include <zephyr/bluetooth/conn.h>
22 #include <zephyr/bluetooth/uuid.h>
23 #include <zephyr/bluetooth/gatt.h>
24 
25 #include "hog.h"
26 
27 static const struct bt_data ad[] = {
28 	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
29 	BT_DATA_BYTES(BT_DATA_UUID16_ALL,
30 		      BT_UUID_16_ENCODE(BT_UUID_HIDS_VAL),
31 		      BT_UUID_16_ENCODE(BT_UUID_BAS_VAL)),
32 };
33 
34 static const struct bt_data sd[] = {
35 	BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
36 };
37 
connected(struct bt_conn * conn,uint8_t err)38 static void connected(struct bt_conn *conn, uint8_t err)
39 {
40 	char addr[BT_ADDR_LE_STR_LEN];
41 
42 	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
43 
44 	if (err) {
45 		printk("Failed to connect to %s, err 0x%02x %s\n", addr,
46 		       err, bt_hci_err_to_str(err));
47 		return;
48 	}
49 
50 	printk("Connected %s\n", addr);
51 
52 	if (bt_conn_set_security(conn, BT_SECURITY_L2)) {
53 		printk("Failed to set security\n");
54 	}
55 }
56 
disconnected(struct bt_conn * conn,uint8_t reason)57 static void disconnected(struct bt_conn *conn, uint8_t reason)
58 {
59 	char addr[BT_ADDR_LE_STR_LEN];
60 
61 	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
62 
63 	printk("Disconnected from %s, reason 0x%02x %s\n", addr,
64 	       reason, bt_hci_err_to_str(reason));
65 }
66 
security_changed(struct bt_conn * conn,bt_security_t level,enum bt_security_err err)67 static void security_changed(struct bt_conn *conn, bt_security_t level,
68 			     enum bt_security_err err)
69 {
70 	char addr[BT_ADDR_LE_STR_LEN];
71 
72 	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
73 
74 	if (!err) {
75 		printk("Security changed: %s level %u\n", addr, level);
76 	} else {
77 		printk("Security failed: %s level %u err %s(%d)\n", addr, level,
78 		       bt_security_err_to_str(err), err);
79 	}
80 }
81 
82 BT_CONN_CB_DEFINE(conn_callbacks) = {
83 	.connected = connected,
84 	.disconnected = disconnected,
85 	.security_changed = security_changed,
86 };
87 
bt_ready(int err)88 static void bt_ready(int err)
89 {
90 	if (err) {
91 		printk("Bluetooth init failed (err %d)\n", err);
92 		return;
93 	}
94 
95 	printk("Bluetooth initialized\n");
96 
97 	hog_init();
98 
99 	if (IS_ENABLED(CONFIG_SETTINGS)) {
100 		settings_load();
101 	}
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;
107 	}
108 
109 	printk("Advertising successfully started\n");
110 }
111 
auth_passkey_display(struct bt_conn * conn,unsigned int passkey)112 static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey)
113 {
114 	char addr[BT_ADDR_LE_STR_LEN];
115 
116 	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
117 
118 	printk("Passkey for %s: %06u\n", addr, passkey);
119 }
120 
auth_cancel(struct bt_conn * conn)121 static void auth_cancel(struct bt_conn *conn)
122 {
123 	char addr[BT_ADDR_LE_STR_LEN];
124 
125 	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
126 
127 	printk("Pairing cancelled: %s\n", addr);
128 }
129 
130 static struct bt_conn_auth_cb auth_cb_display = {
131 	.passkey_display = auth_passkey_display,
132 	.passkey_entry = NULL,
133 	.cancel = auth_cancel,
134 };
135 
main(void)136 int main(void)
137 {
138 	int err;
139 
140 	err = bt_enable(bt_ready);
141 	if (err) {
142 		printk("Bluetooth init failed (err %d)\n", err);
143 		return 0;
144 	}
145 
146 	if (IS_ENABLED(CONFIG_SAMPLE_BT_USE_AUTHENTICATION)) {
147 		bt_conn_auth_cb_register(&auth_cb_display);
148 		printk("Bluetooth authentication callbacks registered.\n");
149 	}
150 
151 	hog_button_loop();
152 	return 0;
153 }
154