1 /******************************************************************************
2  *
3  *  Copyright 2018-2019 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 static phNxpEse_sCoreRecvBuff_List_t *head = NULL, *current = NULL;
24 static uint32_t total_len = 0;
25 
26 static ESESTATUS phNxpEse_DeletList(phNxpEse_sCoreRecvBuff_List_t* head);
27 static ESESTATUS phNxpEse_GetDataFromList(uint32_t* data_len, uint8_t* pbuff);
28 /******************************************************************************
29  * Function         phNxpEse_GetData
30  *
31  * Description      This function update the len and provided buffer
32  *
33  * Returns          On Success ESESTATUS_SUCCESS else proper error code
34  *
35  ******************************************************************************/
phNxpEse_GetData(uint32_t * data_len,uint8_t ** pbuffer)36 ESESTATUS phNxpEse_GetData(uint32_t* data_len, uint8_t** pbuffer) {
37   uint32_t total_data_len = 0;
38   uint8_t* pbuff = NULL;
39   ESESTATUS status = ESESTATUS_FAILED;
40 
41   if (total_len > 0) {
42     pbuff = (uint8_t*)phNxpEse_memalloc(total_len);
43     if (NULL != pbuff) {
44       if (ESESTATUS_SUCCESS ==
45           phNxpEse_GetDataFromList(&total_data_len, pbuff)) {
46         if (total_data_len == total_len) {
47           /***** Success Case *****/
48           *pbuffer = pbuff;
49           *data_len = total_data_len;
50           phNxpEse_DeletList(head);
51           head = NULL;
52           current = NULL;
53           total_len = 0;
54           status = ESESTATUS_SUCCESS;
55         } else {
56           ALOGD_IF(ese_debug_enabled,
57                    "%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         ALOGE("%s phNxpEse_GetDataFromList failed", __FUNCTION__);
63         phNxpEse_free(pbuff);
64       }
65     } else {
66       ALOGE("%s Error in malloc ", __FUNCTION__);
67       status = ESESTATUS_NOT_ENOUGH_MEMORY;
68     }
69   } else {
70     ALOGD_IF(ese_debug_enabled, "%s total_len = %d", __FUNCTION__, total_len);
71   }
72 
73   if (ESESTATUS_SUCCESS != status) {
74     *pbuffer = NULL;
75     *data_len = 0;
76   }
77   ALOGD_IF(ese_debug_enabled, "%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     ALOGE("%s Error in malloc ", __FUNCTION__);
96     return ESESTATUS_NOT_ENOUGH_MEMORY;
97   }
98   newNode->pNext = NULL;
99   newNode->tData.wLen = data_len;
100   phNxpEse_memcpy(newNode->tData.sbuffer, pbuff, data_len);
101   total_len += data_len;
102   if (head == NULL) {
103     head = newNode;
104     current = newNode;
105   } else {
106     current->pNext = newNode;
107     current = newNode;
108   }
109   return ESESTATUS_SUCCESS;
110 }
111 
112 /******************************************************************************
113  * Function         phNxpEse_GetDataFromList
114  *
115  * Description      This function copies all linked list data in provided buffer
116  *
117  * Returns          On Success ESESTATUS_SUCCESS else proper error code
118  *
119  ******************************************************************************/
phNxpEse_GetDataFromList(uint32_t * data_len,uint8_t * pbuff)120 static ESESTATUS phNxpEse_GetDataFromList(uint32_t* data_len, uint8_t* pbuff) {
121   phNxpEse_sCoreRecvBuff_List_t* new_node;
122   uint32_t offset = 0;
123   ALOGD_IF(ese_debug_enabled, "%s Enter ", __FUNCTION__);
124   if (head == NULL || pbuff == NULL) {
125     return ESESTATUS_FAILED;
126   }
127 
128   new_node = head;
129   while (new_node != NULL) {
130     phNxpEse_memcpy((pbuff + offset), new_node->tData.sbuffer,
131                     new_node->tData.wLen);
132     offset += new_node->tData.wLen;
133     new_node = new_node->pNext;
134   }
135   *data_len = offset;
136   ALOGD_IF(ese_debug_enabled, "%s Exit ", __FUNCTION__);
137   return ESESTATUS_SUCCESS;
138 }
139 
140 /******************************************************************************
141  * Function         phNxpEse_DeletList
142  *
143  * Description      This function deletes all nodes from linked list
144  *
145  * Returns          On Success ESESTATUS_SUCCESS else proper error code
146  *
147  ******************************************************************************/
phNxpEse_DeletList(phNxpEse_sCoreRecvBuff_List_t * head)148 static ESESTATUS phNxpEse_DeletList(phNxpEse_sCoreRecvBuff_List_t* head) {
149   ESESTATUS status = ESESTATUS_SUCCESS;
150   phNxpEse_sCoreRecvBuff_List_t *current, *next;
151   current = head;
152 
153   if (head == NULL) {
154     return ESESTATUS_FAILED;
155   }
156 
157   while (current != NULL) {
158     next = current->pNext;
159     phNxpEse_free(current);
160     current = NULL;
161     current = next;
162   }
163   head = NULL;
164   return status;
165 }
166