1 /******************************************************************************
2 *
3 * Copyright 2018-2019,2022-2023 NXP
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18 #define LOG_TAG "NxpEseHal"
19 #include <ese_logs.h>
20 #include <log/log.h>
21 #include <phNxpEseDataMgr.h>
22 #include <phNxpEsePal.h>
23
24 static phNxpEse_sCoreRecvBuff_List_t *head = NULL, *current = NULL;
25 static uint32_t total_len = 0;
26
27 static ESESTATUS phNxpEse_DeletList(phNxpEse_sCoreRecvBuff_List_t* head);
28 static ESESTATUS phNxpEse_GetDataFromList(uint32_t* data_len, uint8_t* pbuff);
29 /******************************************************************************
30 * Function phNxpEse_GetData
31 *
32 * Description This function update the len and provided buffer
33 *
34 * Returns On Success ESESTATUS_SUCCESS else proper error code
35 *
36 ******************************************************************************/
phNxpEse_GetData(uint32_t * data_len,uint8_t ** pbuffer)37 ESESTATUS phNxpEse_GetData(uint32_t* data_len, uint8_t** pbuffer) {
38 uint32_t total_data_len = 0;
39 uint8_t* pbuff = NULL;
40 ESESTATUS status = ESESTATUS_FAILED;
41
42 if (total_len > 0) {
43 pbuff = (uint8_t*)phNxpEse_memalloc(total_len);
44 if (NULL != pbuff) {
45 if (ESESTATUS_SUCCESS ==
46 phNxpEse_GetDataFromList(&total_data_len, pbuff)) {
47 if (total_data_len == total_len) {
48 /***** Success Case *****/
49 *pbuffer = pbuff;
50 *data_len = total_data_len;
51 phNxpEse_DeletList(head);
52 head = NULL;
53 current = NULL;
54 total_len = 0;
55 status = ESESTATUS_SUCCESS;
56 } else {
57 NXP_LOG_ESE_D("%s Mismatch of len total_data_len %d total_len %d",
58 __FUNCTION__, total_data_len, total_len);
59 phNxpEse_free(pbuff);
60 }
61 } else {
62 NXP_LOG_ESE_E("%s phNxpEse_GetDataFromList failed", __FUNCTION__);
63 phNxpEse_free(pbuff);
64 }
65 } else {
66 NXP_LOG_ESE_E("%s Error in malloc ", __FUNCTION__);
67 status = ESESTATUS_NOT_ENOUGH_MEMORY;
68 }
69 } else {
70 NXP_LOG_ESE_D("%s total_len = %d", __FUNCTION__, total_len);
71 }
72
73 if (ESESTATUS_SUCCESS != status) {
74 *pbuffer = NULL;
75 *data_len = 0;
76 }
77 NXP_LOG_ESE_D("%s exit status = %d", __FUNCTION__, status);
78 return status;
79 }
80
81 /******************************************************************************
82 * Function phNxpEse_StoreDatainList
83 *
84 * Description This function stores the received data in linked list
85 *
86 * Returns On Success ESESTATUS_SUCCESS else proper error code
87 *
88 ******************************************************************************/
phNxpEse_StoreDatainList(uint32_t data_len,uint8_t * pbuff)89 ESESTATUS phNxpEse_StoreDatainList(uint32_t data_len, uint8_t* pbuff) {
90 phNxpEse_sCoreRecvBuff_List_t* newNode = NULL;
91
92 newNode = (phNxpEse_sCoreRecvBuff_List_t*)phNxpEse_memalloc(
93 sizeof(phNxpEse_sCoreRecvBuff_List_t));
94 if (newNode == NULL) {
95 NXP_LOG_ESE_E("%s Error in malloc ", __FUNCTION__);
96 phNxpEse_FlushData();
97 return ESESTATUS_NOT_ENOUGH_MEMORY;
98 }
99 newNode->pNext = NULL;
100 newNode->tData.wLen = data_len;
101 phNxpEse_memcpy(newNode->tData.sbuffer, pbuff, data_len);
102 total_len += data_len;
103 if (head == NULL) {
104 head = newNode;
105 current = newNode;
106 } else {
107 current->pNext = newNode;
108 current = newNode;
109 }
110 return ESESTATUS_SUCCESS;
111 }
112
113 /******************************************************************************
114 * Function phNxpEse_GetDataFromList
115 *
116 * Description This function copies all linked list data in provided buffer
117 *
118 * Returns On Success ESESTATUS_SUCCESS else proper error code
119 *
120 ******************************************************************************/
phNxpEse_GetDataFromList(uint32_t * data_len,uint8_t * pbuff)121 static ESESTATUS phNxpEse_GetDataFromList(uint32_t* data_len, uint8_t* pbuff) {
122 phNxpEse_sCoreRecvBuff_List_t* new_node;
123 uint32_t offset = 0;
124 NXP_LOG_ESE_D("%s Enter ", __FUNCTION__);
125 if (head == NULL || pbuff == NULL) {
126 return ESESTATUS_FAILED;
127 }
128
129 new_node = head;
130 while (new_node != NULL) {
131 phNxpEse_memcpy((pbuff + offset), new_node->tData.sbuffer,
132 new_node->tData.wLen);
133 offset += new_node->tData.wLen;
134 new_node = new_node->pNext;
135 }
136 *data_len = offset;
137 NXP_LOG_ESE_D("%s Exit ", __FUNCTION__);
138 return ESESTATUS_SUCCESS;
139 }
140
141 /******************************************************************************
142 * Function phNxpEse_DeletList
143 *
144 * Description This function deletes all nodes from linked list
145 *
146 * Returns On Success ESESTATUS_SUCCESS else proper error code
147 *
148 ******************************************************************************/
phNxpEse_DeletList(phNxpEse_sCoreRecvBuff_List_t * head)149 static ESESTATUS phNxpEse_DeletList(phNxpEse_sCoreRecvBuff_List_t* head) {
150 ESESTATUS status = ESESTATUS_SUCCESS;
151 phNxpEse_sCoreRecvBuff_List_t *current, *next;
152 current = head;
153
154 if (head == NULL) {
155 return ESESTATUS_FAILED;
156 }
157
158 while (current != NULL) {
159 next = current->pNext;
160 phNxpEse_free(current);
161 current = NULL;
162 current = next;
163 }
164 head = NULL;
165 return status;
166 }
167
168 /******************************************************************************
169 * Function phNxpEse_FlushData
170 *
171 * Description This function flushes the data from the data list
172 *
173 * Returns void
174 *
175 ******************************************************************************/
phNxpEse_FlushData()176 void phNxpEse_FlushData() {
177 phNxpEse_data pRes;
178 phNxpEse_memset(&pRes, 0x00, sizeof(phNxpEse_data));
179
180 /* read if any residual data is there */
181 if ((total_len > 0) &&
182 (ESESTATUS_SUCCESS == phNxpEse_GetData(&pRes.len, &pRes.p_data))) {
183 NXP_LOG_ESE_D("%s Flushed data DataLen = %d", __FUNCTION__, pRes.len);
184 }
185 if (pRes.p_data != NULL) {
186 phNxpEse_free(pRes.p_data);
187 pRes.p_data = NULL;
188 }
189 }
190