1 /*
2  * Copyright (c) 2023 Basalte bv
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_DECLARE(net_coap_service_sample);
9 
10 #include <zephyr/net/coap_mgmt.h>
11 #include <zephyr/net/coap_service.h>
12 
13 #define COAP_EVENTS_SET (NET_EVENT_COAP_OBSERVER_ADDED | NET_EVENT_COAP_OBSERVER_REMOVED |	\
14 			 NET_EVENT_COAP_SERVICE_STARTED | NET_EVENT_COAP_SERVICE_STOPPED)
15 
coap_event_handler(uint32_t mgmt_event,struct net_if * iface,void * info,size_t info_length,void * user_data)16 void coap_event_handler(uint32_t mgmt_event, struct net_if *iface,
17 			void *info, size_t info_length, void *user_data)
18 {
19 	ARG_UNUSED(iface);
20 	ARG_UNUSED(user_data);
21 
22 	switch (mgmt_event) {
23 	case NET_EVENT_COAP_OBSERVER_ADDED:
24 		LOG_INF("CoAP observer added");
25 		break;
26 	case NET_EVENT_COAP_OBSERVER_REMOVED:
27 		LOG_INF("CoAP observer removed");
28 		break;
29 	case NET_EVENT_COAP_SERVICE_STARTED:
30 		if (info != NULL && info_length == sizeof(struct net_event_coap_service)) {
31 			struct net_event_coap_service *net_event = info;
32 
33 			LOG_INF("CoAP service %s started", net_event->service->name);
34 		} else {
35 			LOG_INF("CoAP service started");
36 		}
37 		break;
38 	case NET_EVENT_COAP_SERVICE_STOPPED:
39 		if (info != NULL && info_length == sizeof(struct net_event_coap_service)) {
40 			struct net_event_coap_service *net_event = info;
41 
42 			LOG_INF("CoAP service %s stopped", net_event->service->name);
43 		} else {
44 			LOG_INF("CoAP service stopped");
45 		}
46 		break;
47 	}
48 }
49 
50 NET_MGMT_REGISTER_EVENT_HANDLER(coap_events, COAP_EVENTS_SET, coap_event_handler, NULL);
51