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   if (data_len > MAX_DATA_LEN) {
91     ALOGE("%s Data length causes oob write error", __FUNCTION__);
92     return ESESTATUS_INVALID_RECEIVE_LENGTH;
93   }
94   phNxpEse_sCoreRecvBuff_List_t* newNode = NULL;
95 
96   newNode = (phNxpEse_sCoreRecvBuff_List_t*)phNxpEse_memalloc(
97       sizeof(phNxpEse_sCoreRecvBuff_List_t));
98   if (newNode == NULL) {
99     NXP_LOG_ESE_E("%s Error in malloc ", __FUNCTION__);
100     phNxpEse_FlushData();
101     return ESESTATUS_NOT_ENOUGH_MEMORY;
102   }
103   newNode->pNext = NULL;
104   newNode->tData.wLen = data_len;
105   phNxpEse_memcpy(newNode->tData.sbuffer, pbuff, data_len);
106   total_len += data_len;
107   if (head == NULL) {
108     head = newNode;
109     current = newNode;
110   } else {
111     current->pNext = newNode;
112     current = newNode;
113   }
114   return ESESTATUS_SUCCESS;
115 }
116 
117 /******************************************************************************
118  * Function         phNxpEse_GetDataFromList
119  *
120  * Description      This function copies all linked list data in provided buffer
121  *
122  * Returns          On Success ESESTATUS_SUCCESS else proper error code
123  *
124  ******************************************************************************/
phNxpEse_GetDataFromList(uint32_t * data_len,uint8_t * pbuff)125 static ESESTATUS phNxpEse_GetDataFromList(uint32_t* data_len, uint8_t* pbuff) {
126   phNxpEse_sCoreRecvBuff_List_t* new_node;
127   uint32_t offset = 0;
128   NXP_LOG_ESE_D("%s Enter ", __FUNCTION__);
129   if (head == NULL || pbuff == NULL) {
130     return ESESTATUS_FAILED;
131   }
132 
133   new_node = head;
134   while (new_node != NULL) {
135     phNxpEse_memcpy((pbuff + offset), new_node->tData.sbuffer,
136                     new_node->tData.wLen);
137     offset += new_node->tData.wLen;
138     new_node = new_node->pNext;
139   }
140   *data_len = offset;
141   NXP_LOG_ESE_D("%s Exit ", __FUNCTION__);
142   return ESESTATUS_SUCCESS;
143 }
144 
145 /******************************************************************************
146  * Function         phNxpEse_DeletList
147  *
148  * Description      This function deletes all nodes from linked list
149  *
150  * Returns          On Success ESESTATUS_SUCCESS else proper error code
151  *
152  ******************************************************************************/
phNxpEse_DeletList(phNxpEse_sCoreRecvBuff_List_t * head)153 static ESESTATUS phNxpEse_DeletList(phNxpEse_sCoreRecvBuff_List_t* head) {
154   ESESTATUS status = ESESTATUS_SUCCESS;
155   phNxpEse_sCoreRecvBuff_List_t *current, *next;
156   current = head;
157 
158   if (head == NULL) {
159     return ESESTATUS_FAILED;
160   }
161 
162   while (current != NULL) {
163     next = current->pNext;
164     phNxpEse_free(current);
165     current = NULL;
166     current = next;
167   }
168   head = NULL;
169   return status;
170 }
171 
172 /******************************************************************************
173  * Function         phNxpEse_FlushData
174  *
175  * Description      This function flushes the data from the data list
176  *
177  * Returns          void
178  *
179  ******************************************************************************/
phNxpEse_FlushData()180 void phNxpEse_FlushData() {
181   phNxpEse_data pRes;
182   phNxpEse_memset(&pRes, 0x00, sizeof(phNxpEse_data));
183 
184   /* read if any residual data is there */
185   if ((total_len > 0) &&
186       (ESESTATUS_SUCCESS == phNxpEse_GetData(&pRes.len, &pRes.p_data))) {
187     NXP_LOG_ESE_D("%s Flushed data DataLen = %d", __FUNCTION__, pRes.len);
188   }
189   if (pRes.p_data != NULL) {
190     phNxpEse_free(pRes.p_data);
191     pRes.p_data = NULL;
192   }
193 }
194