1 /*
2  * Copyright (c) 2024 Alexandre Bailon
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_REGISTER(coap);
9 
10 #include <coap_utils.h>
11 
12 #ifdef CONFIG_OT_COAP_SAMPLE_LED
13 #include "led.h"
14 #endif /* CONFIG_OT_COAP_SAMPLE_LED */
15 
16 #ifdef CONFIG_OT_COAP_SAMPLE_SW
17 #include <zephyr/drivers/gpio.h>
18 #include "button.h"
19 
20 /*
21  * Get button configuration from the devicetree sw0 alias. This is mandatory.
22  */
23 #define SW0_NODE DT_ALIAS(sw0)
24 #if !DT_NODE_HAS_STATUS_OKAY(SW0_NODE)
25 #error "Unsupported board: sw0 devicetree alias is not defined"
26 #endif
27 static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(SW0_NODE, gpios, {0});
28 static struct gpio_callback button_cb_data;
29 
button_pressed(const struct device * dev,struct gpio_callback * cb,uint32_t pins)30 void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
31 {
32 	coap_led_set_state("ff03::1", 0, LED_MSG_STATE_TOGGLE);
33 }
34 #endif /* CONFIG_OT_COAP_SAMPLE_SW */
35 
main(void)36 int main(void)
37 {
38 	int ret;
39 
40 #ifdef CONFIG_OT_COAP_SAMPLE_SERVER
41 #ifdef CONFIG_OT_COAP_SAMPLE_LED
42 	coap_led_reg_rsc();
43 #endif /* CONFIG_OT_COAP_SAMPLE_LED */
44 #ifdef CONFIG_OT_COAP_SAMPLE_SW
45 	coap_btn_reg_rsc();
46 #endif /* CONFIG_OT_COAP_SAMPLE_SW */
47 #endif /* CONFIG_OT_COAP_SAMPLE_SERVER */
48 
49 	ret = coap_init();
50 	if (ret) {
51 		return ret;
52 	}
53 
54 #ifdef CONFIG_OT_COAP_SAMPLE_SW
55 	button_init(&button);
56 
57 	gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin));
58 	gpio_add_callback_dt(&button, &button_cb_data);
59 #endif /*CONFIG_OT_COAP_SAMPLE_SW */
60 
61 	return 0;
62 }
63