1 /******************************************************************************
2  *
3  *  Copyright 2018 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 <log/log.h>
20 #include <phNxpEseDataMgr.h>
21 #include <phNxpEsePal.h>
22 
23 extern bool ese_debug_enabled;
24 
25 static phNxpEse_sCoreRecvBuff_List_t *head = NULL, *current = NULL;
26 static uint32_t total_len = 0;
27 
28 static ESESTATUS phNxpEse_DeletList(phNxpEse_sCoreRecvBuff_List_t* head);
29 static ESESTATUS phNxpEse_GetDataFromList(uint32_t* data_len, uint8_t* pbuff);
30 /******************************************************************************
31  * Function         phNxpEse_GetData
32  *
33  * Description      This function update the len and provided buffer
34  *
35  * Returns          On Success ESESTATUS_SUCCESS else proper error code
36  *
37  ******************************************************************************/
phNxpEse_GetData(uint32_t * data_len,uint8_t ** pbuffer)38 ESESTATUS phNxpEse_GetData(uint32_t* data_len, uint8_t** pbuffer) {
39   uint32_t total_data_len = 0;
40   uint8_t* pbuff = NULL;
41 
42   if (total_len == 0) {
43     ALOGE("%s total_len = %d", __FUNCTION__, total_len);
44     return ESESTATUS_FAILED;
45   }
46   pbuff = (uint8_t*)phNxpEse_memalloc(total_len);
47   if (NULL == pbuff) {
48     ALOGE("%s Error in malloc ", __FUNCTION__);
49     return ESESTATUS_NOT_ENOUGH_MEMORY;
50   }
51 
52   if (ESESTATUS_SUCCESS != phNxpEse_GetDataFromList(&total_data_len, pbuff)) {
53     ALOGE("%s phNxpEse_GetDataFromList", __FUNCTION__);
54     phNxpEse_free(pbuff);
55     return ESESTATUS_FAILED;
56   }
57   if (total_data_len != total_len) {
58     ALOGE("%s Mismatch of len total_data_len %d total_len %d", __FUNCTION__,
59           total_data_len, total_len);
60     phNxpEse_free(pbuff);
61     return ESESTATUS_FAILED;
62   }
63 
64   *pbuffer = pbuff;
65   *data_len = total_data_len;
66   phNxpEse_DeletList(head);
67   total_len = 0;
68   head = NULL;
69   current = NULL;
70 
71   return ESESTATUS_SUCCESS;
72 }
73 /******************************************************************************
74  * Function         phNxpEse_StoreDatainList
75  *
76  * Description      This function stores the received data in linked list
77  *
78  * Returns          On Success ESESTATUS_SUCCESS else proper error code
79  *
80  ******************************************************************************/
phNxpEse_StoreDatainList(uint32_t data_len,uint8_t * pbuff)81 ESESTATUS phNxpEse_StoreDatainList(uint32_t data_len, uint8_t* pbuff) {
82   phNxpEse_sCoreRecvBuff_List_t* newNode = NULL;
83 
84   newNode = (phNxpEse_sCoreRecvBuff_List_t*)phNxpEse_memalloc(
85       sizeof(phNxpEse_sCoreRecvBuff_List_t));
86   if (newNode == NULL) {
87     return ESESTATUS_NOT_ENOUGH_MEMORY;
88   }
89   newNode->pNext = NULL;
90   newNode->tData.wLen = data_len;
91   phNxpEse_memcpy(newNode->tData.sbuffer, pbuff, data_len);
92   total_len += data_len;
93   if (head == NULL) {
94     head = newNode;
95     current = newNode;
96   } else {
97     current->pNext = newNode;
98     current = newNode;
99   }
100   return ESESTATUS_SUCCESS;
101 }
102 
103 /******************************************************************************
104  * Function         phNxpEse_GetDataFromList
105  *
106  * Description      This function copies all linked list data in provided buffer
107  *
108  * Returns          On Success ESESTATUS_SUCCESS else proper error code
109  *
110  ******************************************************************************/
phNxpEse_GetDataFromList(uint32_t * data_len,uint8_t * pbuff)111 static ESESTATUS phNxpEse_GetDataFromList(uint32_t* data_len, uint8_t* pbuff) {
112   phNxpEse_sCoreRecvBuff_List_t* new_node;
113   uint32_t offset = 0;
114   ALOGD_IF(ese_debug_enabled, "%s Enter ", __FUNCTION__);
115   if (head == NULL || pbuff == NULL) {
116     return ESESTATUS_FAILED;
117   }
118 
119   new_node = head;
120   while (new_node != NULL) {
121     phNxpEse_memcpy((pbuff + offset), new_node->tData.sbuffer,
122                     new_node->tData.wLen);
123     offset += new_node->tData.wLen;
124     new_node = new_node->pNext;
125   }
126   *data_len = offset;
127   ALOGD_IF(ese_debug_enabled, "%s Exit ", __FUNCTION__);
128   return ESESTATUS_SUCCESS;
129 }
130 
131 /******************************************************************************
132  * Function         phNxpEse_DeletList
133  *
134  * Description      This function deletes all nodes from linked list
135  *
136  * Returns          On Success ESESTATUS_SUCCESS else proper error code
137  *
138  ******************************************************************************/
phNxpEse_DeletList(phNxpEse_sCoreRecvBuff_List_t * head)139 static ESESTATUS phNxpEse_DeletList(phNxpEse_sCoreRecvBuff_List_t* head) {
140   ESESTATUS status = ESESTATUS_SUCCESS;
141   phNxpEse_sCoreRecvBuff_List_t *current, *next;
142   current = head;
143 
144   if (head == NULL) {
145     return ESESTATUS_FAILED;
146   }
147 
148   while (current != NULL) {
149     next = current->pNext;
150     phNxpEse_free(current);
151     current = NULL;
152     current = next;
153   }
154   head = NULL;
155   return status;
156 }
157