1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2018 NXP
4  * All rights reserved.
5  *
6  *
7  * SPDX-License-Identifier: BSD-3-Clause
8  */
9 
10 #ifndef __MESSAGING_H__
11 #define __MESSAGING_H__
12 
13 #ifndef SDK_COMPONENT_DEPENDENCY_FSL_COMMON
14 #define SDK_COMPONENT_DEPENDENCY_FSL_COMMON (1U)
15 #endif
16 #if (defined(SDK_COMPONENT_DEPENDENCY_FSL_COMMON) && (SDK_COMPONENT_DEPENDENCY_FSL_COMMON > 0U))
17 #include "fsl_common.h"
18 #else
19 #endif
20 #include "fsl_component_generic_list.h"
21 
22 /*! @brief Definition to determine whether use message macro define. */
23 #ifndef MSG_USE_MACRO
24 #define MSG_USE_MACRO (1)
25 #endif
26 
27 #if (defined(MSG_USE_MACRO) && (MSG_USE_MACRO == 1U))
28 #define MSG_QueueInit(anchor) LIST_Init(anchor, 0)
29 #define MSG_QueueDeinit(anchor)                        \
30     while (MSG_QueueGetHead(anchor))                   \
31     {                                                  \
32         (void)MEM_BufferFree(LIST_RemoveHead(anchor)); \
33     }
34 #endif
35 /*!
36  * @addtogroup Messaging
37  * @{
38  */
39 
40 /*******************************************************************************
41  * Definitions
42  ******************************************************************************/
43 
44 /*! @brief The msg status */
45 #if (defined(SDK_COMPONENT_DEPENDENCY_FSL_COMMON) && (SDK_COMPONENT_DEPENDENCY_FSL_COMMON > 0U))
46 typedef enum _messaging_status
47 {
48     kMSG_Success = kStatus_Success,                  /*!< Success */
49     kMSG_Error   = MAKE_STATUS(kStatusGroup_MSG, 1), /*!< Error */
50 } messaging_status_t;
51 #else
52 typedef enum _messaging_status
53 {
54     kMSG_Success = 0, /*!< Success */
55     kMSG_Error   = 1, /*!< Error */
56 } messaging_status_t;
57 
58 #endif
59 
60 typedef list_label_t messaging_t;
61 
62 /*******************************************************************************
63  * API
64  ******************************************************************************/
65 
66 #if defined(__cplusplus)
67 extern "C" {
68 #endif /* _cplusplus */
69 
70 #if (defined(MSG_USE_MACRO) && (MSG_USE_MACRO == 0U))
71 /*!
72  * @brief Initializes the message queue.
73  *
74  * This function initializes the message queue.
75  *
76  * @param msgQueue The message queue pointer.
77  * @retval kMSG_Success Initialization is successful.
78  */
79 void MSG_QueueInit(messaging_t *msgQueue);
80 
81 /*!
82  * @brief De-initializes the message queue.
83  *
84  * This function de-initializes the message queue.
85  *
86  * @param msgQueue The message queue pointer.
87  */
88 void MSG_QueueDeinit(messaging_t *msgQueue);
89 #endif
90 /*!
91  * @brief Allocates a message node with the length.
92  *
93  * This function allocates a message node with the length.
94  *
95  * @param length The length of the message.
96  * @retval The allocated memory buffer address.
97  */
98 void *MSG_Alloc(uint32_t length);
99 
100 /*!
101  * @brief frees a buffer allocated by #MSG_Alloc.
102  *
103  * This function frees a buffer allocated by #MSG_Alloc.
104  *
105  * @param buffer The buffer address.
106  */
107 void MSG_Free(void *buffer);
108 
109 /*!
110  * @brief Links a message to the tail of the message queue.
111  *
112  * The function links a message to the tail of the message queue.
113  * The memory space of the message must be allocated by calling the function
114  * #MSG_Alloc. And the memory space of the message must be freed by calling
115  * the function #MSG_Free.
116  *
117  * @param msgQueue The message queue pointer.
118  * @param msg The pointer of the message allocated by the #MSG_Alloc.
119  * @retval kMSG_Success The message is added.
120  * @retval kMSG_Error The message addition failed.
121  */
122 messaging_status_t MSG_QueueAddTail(messaging_t *msgQueue, void *msg);
123 
124 /*!
125  * @brief Links a message to the head of the message queue.
126  *
127  * The function links a message to the head of the message queue.
128  * The memory space of the message must be allocated by calling the function
129  * #MSG_Alloc. And the memory space of the message must be freed by calling
130  * the function #MSG_Free.
131  *
132  * @param msgQueue The message queue pointer.
133  * @param msg The pointer of the message allocated by the #MSG_Alloc.
134  * @retval kMSG_Success The message is added.
135  * @retval kMSG_Error The message addition failed.
136  */
137 messaging_status_t MSG_QueueAddHead(messaging_t *msgQueue, void *msg);
138 
139 /*!
140  * @brief Links a message to the previous of the given message.
141  *
142  * The function links a message to the previous of the given message.
143  * The memory space of the message must be allocated by calling the function
144  * #MSG_Alloc. And the memory space of the message must be freed by calling
145  * the function #MSG_Free.
146  *
147  * @param msg The given message pointer.
148  * @param newMsg A new message pointer needs to be added.
149  * @retval kMSG_Success The message is added.
150  * @retval kMSG_Error The message addition failed.
151  */
152 messaging_status_t MSG_QueueAddPrev(void *msg, void *newMsg);
153 
154 /*!
155  * @brief Unlinks a message from its message queue.
156  *
157  * The function unlinks a message from its message queue.
158  * The memory space of the message must be allocated by calling the function
159  * #MSG_Alloc. And the memory space of the message must be freed by calling
160  * the function #MSG_Free.
161  *
162  * @param msg The message pointer.
163  * @retval kMSG_Success The message is removed.
164  * @retval kMSG_Error The message removal failed.
165  */
166 messaging_status_t MSG_QueueRemove(void *msg);
167 
168 /*!
169  * @brief Unlinks the head from the message queue.
170  *
171  * The function unlinks the head from the message queue.
172  *
173  * @param msgQueue The message queue pointer.
174  * @retval NULL if the message queue is empty, or the pointer of removed message if removal was successful.
175  */
176 void *MSG_QueueRemoveHead(messaging_t *msgQueue);
177 
178 /*!
179  * @brief Gets the head of the message queue.
180  *
181  * The function gets the head of the message queue.
182  *
183  * @param msgQueue The message queue pointer.
184  * @retval The pointer of the message if the head is not NULL, or NULL.
185  */
186 void *MSG_QueueGetHead(messaging_t *msgQueue);
187 
188 /*!
189  * @brief Gets the next node of a given message.
190  *
191  * The function gets the next node of a given message.
192  * The memory space of the message must be allocated by calling the function
193  * #MSG_Alloc. And the memory space of the message must be freed by calling
194  * the function #MSG_Free.
195  *
196  * @param msg The message pointer.
197  * @retval The pointer of the message if the next pointer is not NULL, or NULL.
198  */
199 void *MSG_QueueGetNext(void *msg);
200 
201 /*!
202  * @brief Gets the previous node of a given message.
203  *
204  * The function gets the previous node of a given message.
205  * The memory space of the message must be allocated by calling the function
206  * #MSG_Alloc. And the memory space of the message must be freed by calling
207  * the function #MSG_Free.
208  *
209  * @param msg The message pointer.
210  * @retval The pointer of the message if the previous pointer is not NULL, or NULL.
211  */
212 void *MSG_QueueGetPrev(void *msg);
213 
214 #if defined(__cplusplus)
215 }
216 #endif
217 /*! @}*/
218 #endif /* __MESSAGING_H__ */
219