1 /*
2  * Copyright 2017, NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef __SRTM_MESSAGE_STRUCT_H__
10 #define __SRTM_MESSAGE_STRUCT_H__
11 
12 #include "srtm_defs.h"
13 #include "srtm_dispatcher.h"
14 #include "srtm_sem.h"
15 #include "srtm_list.h"
16 
17 /*******************************************************************************
18  * Definitions
19  ******************************************************************************/
20 /**
21  * @brief SRTM message type fields
22  */
23 typedef enum _srtm_message_type
24 {
25     SRTM_MessageTypeRequest = 0x00U, /*!< Request message */
26     SRTM_MessageTypeResponse,        /*!< Response message for certain Request */
27     SRTM_MessageTypeNotification,    /*!< Notification message that doesn't require response */
28     SRTM_MessageTypeCommLast,        /*!< Last value of communication message */
29 
30     SRTM_MessageTypeProcedure = 0x40, /*!< Local procedure */
31     SRTM_MessageTypeRawData   = 0x41, /*!< Raw data message */
32 } srtm_message_type_t;
33 
34 /**
35  * @brief SRTM message direction fields
36  */
37 typedef enum _srtm_message_direct
38 {
39     SRTM_MessageDirectNone = 0x00U, /*!< Local procedure message has no direction */
40     SRTM_MessageDirectRx,           /*!< Received message */
41     SRTM_MessageDirectTx,           /*!< Transfer message */
42 } srtm_message_direct_t;
43 
44 SRTM_ANON_DEC_BEGIN
45 
46 /**
47  * @brief SRTM message structure
48  */
49 typedef struct _srtm_message
50 {
51     srtm_list_t node;             /*!< SRTM message list node to link to a list */
52     srtm_message_type_t type;     /*!< SRTM message type */
53     srtm_message_direct_t direct; /*!< SRTM message direction */
54     void *data;                   /*!< SRTM raw data, including header and payload for CommMessage */
55     uint32_t dataLen;             /*!< SRTM raw data bytes */
56     srtm_channel_t channel;       /*!< SRTM communication channel */
57     srtm_status_t error;          /*!< SRTM message error status */
58     uint8_t priority;             /*!< SRTM message priority */
59     srtm_message_free_t free;     /*!< SRTM user defined message free function */
60     void *freeParam;              /*!< SRTM user defined message free param */
61 
62     union
63     {
64         struct
65         {
66             srtm_message_proc_cb_t cb; /*!< SRTM procedure message callabck function */
67             void *param1;              /*!< SRTM procedure message callabck parameter */
68             void *param2;              /*!< SRTM procedure message callabck parameter */
69             srtm_sem_t sig;            /*!< SRTM procedure message signal to wait for procedure finish */
70         } procMsg;
71         struct
72         {
73             bool isSyncReq; /*!< Flag to indicate synchronized request */
74             union
75             {
76                 struct
77                 {
78                     srtm_sem_t sig;       /*!< SRTM request message signal to wait for response */
79                     srtm_response_t resp; /*!< SRTM request message response */
80                 } sync;                   /*!< SRTM request message from SRTM_Request() */
81                 struct
82                 {
83                     srtm_dispatcher_resp_cb_t cb; /*!< SRTM request message response callabck */
84                     void *param;                  /*!< SRTM request message response callabck parameter */
85                 } async;                          /*!< SRTM request message from SRTM_Deliver_Request() */
86             };
87         } reqMsg;
88         struct
89         {
90             uint32_t dummy; /*!< SRTM response message unused field */
91         } rspMsg;
92         struct
93         {
94             uint32_t dummy; /*!< SRTM notification message unused field */
95         } ntfMsg;
96     };
97 } srtm_message;
98 
99 /**
100  * @brief SRTM communication packet head
101  */
102 typedef SRTM_PACKED_BEGIN struct _srtm_packet_head
103 {
104     union
105     {
106         struct
107         {
108             uint8_t category;
109             uint8_t majorVersion;
110             uint8_t minorVersion;
111             uint8_t type;
112             uint8_t command;
113             uint8_t priority;
114             uint8_t reserved[4U];
115         };
116         uint8_t header[10U];
117     };
118 } SRTM_PACKED_END srtm_packet_head_t;
119 
120 SRTM_ANON_DEC_END
121 
122 /*******************************************************************************
123  * API
124  ******************************************************************************/
125 #ifdef __cplusplus
126 extern "C" {
127 #endif
128 
129 /*!
130  * @brief Create SRTM base message.
131  *
132  * @param len the data length in bytes.
133  * @return SRTM base message handle, or NULL on failure.
134  */
135 srtm_message_t SRTM_Message_Create(uint32_t len);
136 
137 /*!
138  * @brief Duplicate a SRTM message.
139  *
140  * @param msg The message to duplicate.
141  * @return SRTM message handle, or NULL on failure.
142  */
143 srtm_message_t SRTM_Message_Duplicate(srtm_message_t msg);
144 
145 /*!
146  * @brief Destroy SRTM base message.
147  *
148  * @param message SRTM base message handle.
149  */
150 void SRTM_Message_Destroy(srtm_message_t message);
151 
152 #ifdef __cplusplus
153 }
154 #endif
155 
156 #endif /* __SRTM_MESSAGE_STRUCT_H__ */
157