1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef NRFS_BACKEND_IPC_SERVICE_H
8 #define NRFS_BACKEND_IPC_SERVICE_H
9 
10 #include <stdint.h>
11 #include <nrfs_common.h>
12 #include <zephyr/ipc/ipc_service.h>
13 #include <zephyr/kernel.h>
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 struct __packed ipc_data_packet {
20 	uint16_t channel_id;
21 	uint16_t size;
22 	uint8_t data[CONFIG_NRFS_MAX_BACKEND_PACKET_SIZE];
23 };
24 
25 enum ipc_channel_status {
26 	NOT_CONNECTED = 0,
27 	CONNECTED     = 1
28 };
29 
30 enum nrfs_backend_error {
31 	NRFS_ERROR_EPT_RECEIVE_QUEUE_ERROR = 0,
32 	NRFS_ERROR_EPT_RECEIVE_DATA_TOO_LONG,
33 	NRFS_ERROR_IPC_CHANNEL_INIT,
34 	NRFS_ERROR_SEND_DATA,
35 	NRFS_ERROR_NO_DATA_RECEIVED,
36 	NRFS_ERROR_IPC_OPEN_INSTANCE,
37 	NRFS_ERROR_IPC_REGISTER_ENDPOINT,
38 	NRFS_ERROR_BACKEND_NOT_CONNECTED,
39 	NRFS_ERROR_SEND_DATA_FROM_QUEUE,
40 	NRFS_ERROR_COUNT
41 };
42 
43 #define IPC_CPUSYS_CHANNEL_ID 0x5C
44 
45 /**
46  * @brief function to check if backend is connected to sysctrl
47  *
48  * @return true Backend connected.
49  * @return false Backend not connected.
50  */
51 bool nrfs_backend_connected(void);
52 
53 /**
54  * @brief this function will block until connection or timeout expires
55  *
56  * @param timeout
57  *
58  * @return 0 Connection done.
59  * @return -EAGAIN Waiting period timed out.
60  */
61 int nrfs_backend_wait_for_connection(k_timeout_t timeout);
62 
63 /**
64  * @brief Extended function for sending a message over the chosen transport backend.
65  *
66  * This function is used by services to send requests to the System Controller.
67  *
68  * @param[in] message   Pointer to the message payload.
69  * @param[in] size      Message payload size.
70  * @param[in] timeout   Non-negative waiting period to add the message,
71  *                      or one of the special values K_NO_WAIT and K_FOREVER.
72  * @param[in] high_prio True if message should be sent with higher priority.
73  *
74  * @retval NRFS_SUCCESS Message sent successfully.
75  * @retval NRFS_ERR_IPC Backend returned error during message sending.
76  */
77 nrfs_err_t nrfs_backend_send_ex(void *message, size_t size, k_timeout_t timeout, bool high_prio);
78 
79 /**
80  * @brief Fatal error handler for unrecoverable errors
81  *
82  * This is weak function so it can be overridden if needed.
83  * Error is considered fatal when there is no option to send message to sysctrl
84  * even after retry. Communication with sysctrl is crucial for system to work properly.
85  *
86  * @param error_id parameter to identify error.
87  */
88 void nrfs_backend_fatal_error_handler(enum nrfs_backend_error error_id);
89 
90 typedef void (*nrfs_backend_bound_info_cb_t)(void);
91 struct nrfs_backend_bound_info_subs {
92 	sys_snode_t node;
93 	nrfs_backend_bound_info_cb_t cb;
94 };
95 /**
96  * @brief Register callback function to notify when nrfs is connected.
97  * There can be multiple callbacks registered.
98  *
99  * @param subs Subcription instance.
100  * @param cb Callback
101  */
102 void nrfs_backend_register_bound_subscribe(struct nrfs_backend_bound_info_subs *subs,
103 				    nrfs_backend_bound_info_cb_t cb);
104 
105 #ifdef __cplusplus
106 }
107 #endif
108 
109 #endif /* NRFS_BACKEND_IPC_SERVICE_H */
110