1 /*!
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2019, 2023 NXP
4  * All rights reserved.
5  *
6  *
7  * SPDX-License-Identifier: BSD-3-Clause
8  */
9 #include "fsl_component_mem_manager.h"
10 #include "fsl_component_generic_list.h"
11 #include "fsl_component_messaging.h"
12 
13 /*******************************************************************************
14  * Definitions
15  ******************************************************************************/
16 
17 /*******************************************************************************
18  * Prototypes
19  ******************************************************************************/
20 
21 /*******************************************************************************
22  * Variables
23  ******************************************************************************/
24 
25 /*******************************************************************************
26  * Code
27  ******************************************************************************/
28 #if (defined(MSG_USE_MACRO) && (MSG_USE_MACRO == 0U))
MSG_QueueInit(messaging_t * msgQueue)29 void MSG_QueueInit(messaging_t *msgQueue)
30 {
31     assert(msgQueue);
32 
33     LIST_Init(msgQueue, 0);
34 }
35 
MSG_QueueDeinit(messaging_t * msgQueue)36 void MSG_QueueDeinit(messaging_t *msgQueue)
37 {
38     assert(msgQueue);
39     while (NULL != MSG_QueueGetHead(msgQueue))
40     {
41         (void)MEM_BufferFree(LIST_RemoveHead(msgQueue));
42     }
43 }
44 #endif
MSG_QueueAddTail(messaging_t * msgQueue,void * msg)45 messaging_status_t MSG_QueueAddTail(messaging_t *msgQueue, void *msg)
46 {
47     list_status_t listStatus;
48 
49     assert(msgQueue);
50     assert(msg);
51 
52     msg = (list_element_t *)msg - 1;
53 
54     listStatus = LIST_AddTail(msgQueue, (list_element_t *)msg);
55 
56     return ((list_status_t)kLIST_Ok == listStatus) ? (messaging_status_t)kMSG_Success : (messaging_status_t)kMSG_Error;
57 }
58 
MSG_QueueAddHead(messaging_t * msgQueue,void * msg)59 messaging_status_t MSG_QueueAddHead(messaging_t *msgQueue, void *msg)
60 {
61     list_status_t listStatus;
62 
63     assert(msgQueue);
64     assert(msg);
65 
66     msg = (list_element_t *)msg - 1;
67 
68     listStatus = LIST_AddHead(msgQueue, (list_element_t *)msg);
69 
70     return ((list_status_t)kLIST_Ok == listStatus) ? (messaging_status_t)kMSG_Success : (messaging_status_t)kMSG_Error;
71 }
72 
MSG_QueueAddPrev(void * msg,void * newMsg)73 messaging_status_t MSG_QueueAddPrev(void *msg, void *newMsg)
74 {
75     list_element_t *pElem;
76     list_element_t *pNewElem;
77     list_status_t listStatus;
78 
79     assert(msg);
80     assert(newMsg);
81 
82     pElem    = (list_element_t *)msg - 1;
83     pNewElem = (list_element_t *)newMsg - 1;
84 
85     listStatus = LIST_AddPrevElement((list_element_t *)pElem, (list_element_t *)pNewElem);
86 
87     return ((list_status_t)kLIST_Ok == listStatus) ? (messaging_status_t)kMSG_Success : (messaging_status_t)kMSG_Error;
88 }
89 
MSG_QueueRemove(void * msg)90 messaging_status_t MSG_QueueRemove(void *msg)
91 {
92     list_element_t *p;
93     list_status_t listStatus;
94 
95     assert(msg);
96 
97     p          = (list_element_t *)msg - 1;
98     listStatus = LIST_RemoveElement((list_element_t *)p);
99 
100     return ((list_status_t)kLIST_Ok == listStatus) ? (messaging_status_t)kMSG_Success : (messaging_status_t)kMSG_Error;
101 }
102 
MSG_QueueRemoveHead(messaging_t * msgQueue)103 void *MSG_QueueRemoveHead(messaging_t *msgQueue)
104 {
105     void *buffer;
106 
107     assert(msgQueue);
108 
109     buffer = LIST_RemoveHead(msgQueue);
110 
111     return (buffer != NULL) ? ((list_element_t *)buffer + 1) : buffer;
112 }
113 
MSG_QueueGetHead(messaging_t * msgQueue)114 void *MSG_QueueGetHead(messaging_t *msgQueue)
115 {
116     void *buffer;
117 
118     assert(msgQueue);
119 
120     buffer = LIST_GetHead(msgQueue);
121 
122     return (buffer != NULL) ? ((list_element_t *)buffer + 1) : buffer;
123 }
124 
MSG_QueueGetNext(void * msg)125 void *MSG_QueueGetNext(void *msg)
126 {
127     list_element_t *p;
128 
129     assert(msg);
130 
131     p = (list_element_t *)msg - 1;
132     p = LIST_GetNext(p);
133 
134     return (p != NULL) ? ((list_element_t *)p + 1) : p;
135 }
136 
MSG_QueueGetPrev(void * msg)137 void *MSG_QueueGetPrev(void *msg)
138 {
139     list_element_t *p;
140 
141     assert(msg);
142 
143     p = (list_element_t *)msg - 1;
144     p = LIST_GetPrev(p);
145 
146     return (p != NULL) ? ((list_element_t *)p + 1) : p;
147 }
148 
MSG_Alloc(uint32_t length)149 void *MSG_Alloc(uint32_t length)
150 {
151     void *buffer;
152 
153     assert(length);
154 
155     buffer = MEM_BufferAlloc(length + sizeof(list_element_t));
156     if (buffer != NULL)
157     {
158         ((list_element_t *)buffer)->list = NULL;
159         buffer                           = (list_element_t *)buffer + 1;
160         (void)memset(buffer, 0, length);
161     }
162     return buffer;
163 }
164 
MSG_Free(void * buffer)165 void MSG_Free(void *buffer)
166 {
167     if (buffer != NULL)
168     {
169         (void)MSG_QueueRemove(buffer);
170         (void)MEM_BufferFree((list_element_t *)buffer - 1);
171     }
172 }
173