1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <internal/nrfs_dispatcher.h>
8 #include <internal/nrfs_callbacks.h>
9 #include <internal/services/nrfs_generic.h>
10 #include <nrfs_config.h>
11 
12 static const nrfs_service_cb_t services_callbacks[] = {
13 #ifdef NRFS_TEMP_SERVICE_ENABLED
14 	[NRFS_SERVICE_ID_TEMP] = nrfs_temp_service_notify,
15 #else
16 	[NRFS_SERVICE_ID_TEMP]	= NULL,
17 #endif
18 #ifdef NRFS_MRAM_SERVICE_ENABLED
19 	[NRFS_SERVICE_ID_MRAM] = nrfs_mram_service_notify,
20 #else
21 	[NRFS_SERVICE_ID_MRAM]	= NULL,
22 #endif
23 #ifdef NRFS_RESET_SERVICE_ENABLED
24 	[NRFS_SERVICE_ID_RESET] = nrfs_reset_service_notify,
25 #else
26 	[NRFS_SERVICE_ID_RESET] = NULL,
27 #endif
28 #ifdef NRFS_VBUS_DETECTOR_SERVICE_ENABLED
29 	[NRFS_SERVICE_ID_USB] = nrfs_usb_service_notify,
30 #else
31 	[NRFS_SERVICE_ID_USB]	= NULL,
32 #endif
33 #ifdef NRFS_PMIC_SERVICE_ENABLED
34 	[NRFS_SERVICE_ID_PMIC] = nrfs_pmic_service_notify,
35 #else
36 	[NRFS_SERVICE_ID_PMIC]	= NULL,
37 #endif
38 #ifdef NRFS_DVFS_SERVICE_ENABLED
39 	[NRFS_SERVICE_ID_DVFS] = nrfs_dvfs_service_notify,
40 #else
41 	[NRFS_SERVICE_ID_DVFS]	= NULL,
42 #endif
43 #ifdef NRFS_DIAG_SERVICE_ENABLED
44 	[NRFS_SERVICE_ID_DIAG] = nrfs_diag_service_notify,
45 #else
46 	[NRFS_SERVICE_ID_DIAG]	= NULL,
47 #endif
48 #ifdef NRFS_CLOCK_SERVICE_ENABLED
49 	[NRFS_SERVICE_ID_CLOCK] = nrfs_clock_service_notify,
50 #else
51 	[NRFS_SERVICE_ID_CLOCK]	= NULL,
52 #endif
53 #ifdef NRFS_GDPWR_SERVICE_ENABLED
54 	[NRFS_SERVICE_ID_GDPWR] = nrfs_gdpwr_service_notify,
55 #else
56 	[NRFS_SERVICE_ID_GDPWR]	= NULL,
57 #endif
58 #ifdef NRFS_GDFS_SERVICE_ENABLED
59 	[NRFS_SERVICE_ID_GDFS] = nrfs_gdfs_service_notify,
60 #else
61 	[NRFS_SERVICE_ID_GDFS]	= NULL,
62 #endif
63 #ifdef NRFS_SWEXT_SERVICE_ENABLED
64 	[NRFS_SERVICE_ID_SWEXT] = nrfs_swext_service_notify,
65 #else
66 	[NRFS_SERVICE_ID_SWEXT]	= NULL,
67 #endif
68 };
69 
70 /* Warning! All "UNSOLICITED" features are not supported. This is intended for possible future use. */
nrfs_unsolicited_handler(void * p_buffer,size_t size)71 void __attribute__((weak)) nrfs_unsolicited_handler(void *p_buffer, size_t size)
72 {
73 	(void)p_buffer;
74 	(void)size;
75 }
76 
nrfs_dispatcher_notify(void * p_notification,size_t size)77 void nrfs_dispatcher_notify(void *p_notification, size_t size)
78 {
79 	nrfs_hdr_t *p_hdr = (nrfs_hdr_t *)p_notification;
80 
81 	if (NRFS_HDR_UNSOLICITED_GET(p_hdr)) {
82 		void *p_buffer = ((nrfs_generic_t *)p_notification)->payload;
83 
84 		nrfs_unsolicited_handler(p_buffer, size - sizeof(nrfs_generic_t));
85 	} else {
86 		uint8_t srv_id = NRFS_SERVICE_ID_GET(p_hdr->req);
87 
88 		if ((srv_id < NRFS_ARRAY_SIZE(services_callbacks)) &&
89 		    (services_callbacks[srv_id] != NULL)) {
90 			services_callbacks[srv_id](p_notification, size);
91 		} else {
92 			/* TODO: error! unknown service */
93 		}
94 	}
95 }
96