1 /* main.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 2023 Nordic Semiconductor ASA
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <stdint.h>
10 #include <stddef.h>
11 
12 #include <zephyr/kernel.h>
13 #include <zephyr/device.h>
14 
15 #include <zephyr/drivers/gpio.h>
16 
17 #include <zephyr/sys/util.h>
18 #include <zephyr/sys/printk.h>
19 
20 #include <zephyr/bluetooth/ead.h>
21 #include <zephyr/bluetooth/conn.h>
22 
23 #include <zephyr/logging/log.h>
24 
25 #include "common.h"
26 
27 LOG_MODULE_DECLARE(ead_central_sample, CONFIG_BT_EAD_LOG_LEVEL);
28 
29 /*
30  * Get button configuration from the devicetree sw0 alias. This is mandatory.
31  */
32 #define SW0_NODE DT_ALIAS(sw0)
33 #if !DT_NODE_HAS_STATUS(SW0_NODE, okay)
34 #error "Unsupported board: sw0 devicetree alias is not defined"
35 #endif
36 static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(SW0_NODE, gpios, {0});
37 static struct gpio_callback button_cb_data;
38 
39 extern int run_central_sample(int get_passkey_confirmation(struct bt_conn *conn),
40 			      uint8_t *received_data, size_t received_data_size,
41 			      struct key_material *keymat);
42 
43 static struct k_poll_signal button_pressed_signal;
44 
button_pressed(const struct device * dev,struct gpio_callback * cb,uint32_t pins)45 static void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
46 {
47 	LOG_DBG("Button pressed...");
48 
49 	k_poll_signal_raise(&button_pressed_signal, 0);
50 	k_sleep(K_SECONDS(1));
51 	k_poll_signal_reset(&button_pressed_signal);
52 }
53 
get_passkey_confirmation(struct bt_conn * conn)54 static int get_passkey_confirmation(struct bt_conn *conn)
55 {
56 	int err;
57 
58 	printk("Confirm passkey by pressing button at %s pin %d...\n", button.port->name,
59 	       button.pin);
60 
61 	await_signal(&button_pressed_signal);
62 
63 	err = bt_conn_auth_passkey_confirm(conn);
64 	if (err) {
65 		LOG_DBG("Failed to confirm passkey.");
66 		return -1;
67 	}
68 
69 	printk("Passkey confirmed.\n");
70 
71 	return 0;
72 }
73 
setup_btn(void)74 static int setup_btn(void)
75 {
76 	int ret;
77 
78 	k_poll_signal_init(&button_pressed_signal);
79 
80 	if (!gpio_is_ready_dt(&button)) {
81 		LOG_ERR("Error: button device %s is not ready", button.port->name);
82 		return -1;
83 	}
84 
85 	ret = gpio_pin_configure_dt(&button, GPIO_INPUT);
86 	if (ret != 0) {
87 		LOG_ERR("Error %d: failed to configure %s pin %d", ret, button.port->name,
88 			button.pin);
89 		return -1;
90 	}
91 
92 	ret = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE);
93 	if (ret != 0) {
94 		LOG_ERR("Error %d: failed to configure interrupt on %s pin %d", ret,
95 			button.port->name, button.pin);
96 		return -1;
97 	}
98 
99 	gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin));
100 	gpio_add_callback(button.port, &button_cb_data);
101 	LOG_DBG("Set up button at %s pin %d", button.port->name, button.pin);
102 
103 	return 0;
104 }
105 
main(void)106 int main(void)
107 {
108 	int err;
109 
110 	err = setup_btn();
111 	if (err) {
112 		return 0;
113 	}
114 
115 	LOG_DBG("Starting central sample...");
116 
117 	(void)run_central_sample(get_passkey_confirmation, NULL, 0, NULL);
118 
119 	return 0;
120 }
121