1 /*
2  * Copyright 2017, NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include <string.h>
10 #include <assert.h>
11 
12 #include "srtm_message_pool.h"
13 #include "srtm_message.h"
14 #include "srtm_message_struct.h"
15 
16 /*******************************************************************************
17  * Definitions
18  ******************************************************************************/
19 
20 /*******************************************************************************
21  * Prototypes
22  ******************************************************************************/
23 
24 /*******************************************************************************
25  * Variables
26  ******************************************************************************/
27 
28 /*******************************************************************************
29  * Code
30  ******************************************************************************/
SRTM_Message_Create(uint32_t len)31 srtm_message_t SRTM_Message_Create(uint32_t len)
32 {
33     srtm_message_t msg;
34 
35     msg = (srtm_message_t)SRTM_MessagePool_Alloc(sizeof(struct _srtm_message) + len);
36     if (msg != NULL)
37     {
38 #ifdef SRTM_ZEROIZE_MESSAGE_DATA
39         (void)memset(msg, 0, sizeof(struct _srtm_message) + len);
40 #else
41         (void)memset(msg, 0, sizeof(struct _srtm_message));
42 #endif
43         msg->dataLen = len;
44         if (len > 0UL) /* Data exists */
45         {
46             msg->data = msg + 1;
47         }
48         SRTM_List_Init(&msg->node);
49     }
50 
51     return msg;
52 }
53 
SRTM_Message_Duplicate(srtm_message_t msg)54 srtm_message_t SRTM_Message_Duplicate(srtm_message_t msg)
55 {
56     srtm_message_t dup;
57 
58     assert(msg);
59 
60     dup = (srtm_message_t)SRTM_MessagePool_Alloc(sizeof(struct _srtm_message) + msg->dataLen);
61 
62     if (dup != NULL)
63     {
64         (void)memcpy(dup, msg, sizeof(struct _srtm_message));
65         if (msg->dataLen != 0UL) /* Data exists */
66         {
67             dup->data = dup + 1;
68         }
69         SRTM_List_Init(&dup->node);
70     }
71 
72     return dup;
73 }
74 
SRTM_Message_Destroy(srtm_message_t message)75 void SRTM_Message_Destroy(srtm_message_t message)
76 {
77     assert(message);
78 
79     if (message->free != NULL)
80     {
81         message->free(message, message->freeParam);
82     }
83     else
84     {
85         SRTM_MessagePool_Free(message);
86     }
87 }
88 
SRTM_CommMessage_Create(srtm_message_type_t type,srtm_message_direct_t direct,srtm_channel_t channel,uint8_t category,uint16_t version,uint8_t command,uint16_t payloadLen)89 static srtm_message_t SRTM_CommMessage_Create(srtm_message_type_t type,
90                                               srtm_message_direct_t direct,
91                                               srtm_channel_t channel,
92                                               uint8_t category,
93                                               uint16_t version,
94                                               uint8_t command,
95                                               uint16_t payloadLen)
96 {
97     srtm_message_t msg;
98     srtm_packet_head_t *head;
99     uint32_t dataLen = (uint32_t)sizeof(struct _srtm_packet_head) + payloadLen;
100 
101     msg = SRTM_Message_Create(dataLen);
102 
103     if (msg != NULL)
104     {
105         msg->type    = type;
106         msg->direct  = direct;
107         msg->channel = channel;
108 
109         head               = (srtm_packet_head_t *)(msg->data);
110         head->category     = category;
111         head->majorVersion = (uint8_t)SRTM_MESSAGE_MAJOR_VERSION(version);
112         head->minorVersion = (uint8_t)SRTM_MESSAGE_MINOR_VERSION(version);
113         head->type         = (uint8_t)type;
114         head->command      = command;
115         head->priority     = msg->priority;
116     }
117 
118     return msg;
119 }
120 
SRTM_CommMessage_Destroy(srtm_message_t message)121 static void SRTM_CommMessage_Destroy(srtm_message_t message)
122 {
123     SRTM_Message_Destroy(message);
124 }
125 
SRTM_CommMessage_GetPacketHead(srtm_message_t message)126 static srtm_packet_head_t *SRTM_CommMessage_GetPacketHead(srtm_message_t message)
127 {
128     srtm_packet_head_t *head;
129 
130     assert(message);
131     assert(message->data);
132 
133     head = (srtm_packet_head_t *)(message->data);
134 
135     return head;
136 }
137 
SRTM_Procedure_Create(srtm_message_proc_cb_t procedure,void * param1,void * param2)138 srtm_procedure_t SRTM_Procedure_Create(srtm_message_proc_cb_t procedure, void *param1, void *param2)
139 {
140     srtm_message_t msg = SRTM_Message_Create(0);
141 
142     if (msg != NULL)
143     {
144         msg->type           = SRTM_MessageTypeProcedure;
145         msg->direct         = SRTM_MessageDirectNone;
146         msg->procMsg.cb     = procedure;
147         msg->procMsg.param1 = param1;
148         msg->procMsg.param2 = param2;
149     }
150 
151     return msg;
152 }
153 
SRTM_Procedure_Destroy(srtm_procedure_t procedure)154 void SRTM_Procedure_Destroy(srtm_procedure_t procedure)
155 {
156     SRTM_Message_Destroy(procedure);
157 }
158 
SRTM_RawData_Create(srtm_channel_t channel,uint32_t dataLen)159 srtm_rawdata_t SRTM_RawData_Create(srtm_channel_t channel, uint32_t dataLen)
160 {
161     srtm_message_t msg;
162 
163     msg = SRTM_Message_Create(dataLen);
164 
165     if (msg != NULL)
166     {
167         msg->type    = SRTM_MessageTypeRawData;
168         msg->direct  = SRTM_MessageDirectTx;
169         msg->channel = channel;
170     }
171 
172     return msg;
173 }
174 
SRTM_RawData_Destroy(srtm_rawdata_t data)175 void SRTM_RawData_Destroy(srtm_rawdata_t data)
176 {
177     SRTM_Message_Destroy(data);
178 }
179 
SRTM_RawData_GetData(srtm_rawdata_t data)180 void *SRTM_RawData_GetData(srtm_rawdata_t data)
181 {
182     assert(data);
183 
184     return data->data;
185 }
186 
SRTM_RawData_GetDataLen(srtm_rawdata_t data)187 uint32_t SRTM_RawData_GetDataLen(srtm_rawdata_t data)
188 {
189     assert(data);
190 
191     return data->dataLen;
192 }
193 
SRTM_Request_Create(srtm_channel_t channel,uint8_t category,uint16_t version,uint8_t command,uint16_t payloadLen)194 srtm_request_t SRTM_Request_Create(
195     srtm_channel_t channel, uint8_t category, uint16_t version, uint8_t command, uint16_t payloadLen)
196 {
197     return SRTM_CommMessage_Create(SRTM_MessageTypeRequest, SRTM_MessageDirectTx, channel, category, version, command,
198                                    payloadLen);
199 }
200 
SRTM_Request_Destroy(srtm_request_t request)201 void SRTM_Request_Destroy(srtm_request_t request)
202 {
203     SRTM_CommMessage_Destroy(request);
204 }
205 
SRTM_Request_GetErrorCode(srtm_request_t request)206 srtm_status_t SRTM_Request_GetErrorCode(srtm_request_t request)
207 {
208     assert(request);
209 
210     return request->error;
211 }
212 
SRTM_Response_Create(srtm_channel_t channel,uint8_t category,uint16_t version,uint8_t command,uint16_t payloadLen)213 srtm_response_t SRTM_Response_Create(
214     srtm_channel_t channel, uint8_t category, uint16_t version, uint8_t command, uint16_t payloadLen)
215 {
216     return SRTM_CommMessage_Create(SRTM_MessageTypeResponse, SRTM_MessageDirectTx, channel, category, version, command,
217                                    payloadLen);
218 }
219 
SRTM_Response_Destroy(srtm_response_t response)220 void SRTM_Response_Destroy(srtm_response_t response)
221 {
222     SRTM_CommMessage_Destroy(response);
223 }
224 
SRTM_Notification_Create(srtm_channel_t channel,uint8_t category,uint16_t version,uint8_t command,uint16_t payloadLen)225 srtm_notification_t SRTM_Notification_Create(
226     srtm_channel_t channel, uint8_t category, uint16_t version, uint8_t command, uint16_t payloadLen)
227 {
228     return SRTM_CommMessage_Create(SRTM_MessageTypeNotification, SRTM_MessageDirectTx, channel, category, version,
229                                    command, payloadLen);
230 }
231 
SRTM_Notification_Destroy(srtm_notification_t notification)232 void SRTM_Notification_Destroy(srtm_notification_t notification)
233 {
234     SRTM_CommMessage_Destroy(notification);
235 }
236 
SRTM_Message_GetPriority(srtm_message_t message)237 uint8_t SRTM_Message_GetPriority(srtm_message_t message)
238 {
239     assert(message);
240 
241     return message->priority;
242 }
243 
SRTM_Message_SetPriority(srtm_message_t message,uint8_t priority)244 void SRTM_Message_SetPriority(srtm_message_t message, uint8_t priority)
245 {
246     srtm_packet_head_t *head;
247     assert(message);
248 
249     message->priority = priority;
250     if (message->type < SRTM_MessageTypeCommLast)
251     {
252         head           = SRTM_CommMessage_GetPacketHead(message);
253         head->priority = priority;
254     }
255 }
256 
SRTM_Message_SetFreeFunc(srtm_message_t message,srtm_message_free_t func,void * param)257 void SRTM_Message_SetFreeFunc(srtm_message_t message, srtm_message_free_t func, void *param)
258 {
259     assert(message);
260 
261     message->free      = func;
262     message->freeParam = param;
263 }
264 
SRTM_CommMessage_GetCategory(srtm_message_t message)265 uint8_t SRTM_CommMessage_GetCategory(srtm_message_t message)
266 {
267     srtm_packet_head_t *head = SRTM_CommMessage_GetPacketHead(message);
268 
269     return head->category;
270 }
271 
SRTM_CommMessage_GetVersion(srtm_message_t message)272 uint16_t SRTM_CommMessage_GetVersion(srtm_message_t message)
273 {
274     srtm_packet_head_t *head = SRTM_CommMessage_GetPacketHead(message);
275 
276     return (((uint16_t)(head->majorVersion)) << 8U) | head->minorVersion;
277 }
278 
SRTM_CommMessage_GetCommand(srtm_message_t message)279 uint8_t SRTM_CommMessage_GetCommand(srtm_message_t message)
280 {
281     srtm_packet_head_t *head = SRTM_CommMessage_GetPacketHead(message);
282 
283     return head->command;
284 }
285 
SRTM_CommMessage_GetPayload(srtm_message_t message)286 uint8_t *SRTM_CommMessage_GetPayload(srtm_message_t message)
287 {
288     assert(message);
289     assert(message->data);
290 
291     return (uint8_t *)((srtm_packet_head_t *)(message->data) + 1);
292 }
293 
SRTM_CommMessage_GetPayloadLen(srtm_message_t message)294 uint32_t SRTM_CommMessage_GetPayloadLen(srtm_message_t message)
295 {
296     assert(message);
297     assert(message->dataLen >= sizeof(struct _srtm_packet_head));
298 
299     return message->dataLen - sizeof(struct _srtm_packet_head);
300 }
301 
SRTM_CommMessage_GetChannel(srtm_message_t message)302 srtm_channel_t SRTM_CommMessage_GetChannel(srtm_message_t message)
303 {
304     return message->channel;
305 }
306