1 /******************************************************************************
2 *
3 * Copyright 2018,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 <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 if (data_len > MAX_DATA_LEN) {
83 ALOGE("%s Data length causes oob write error", __FUNCTION__);
84 return ESESTATUS_INVALID_RECEIVE_LENGTH;
85 }
86 phNxpEse_sCoreRecvBuff_List_t* newNode = NULL;
87
88 newNode = (phNxpEse_sCoreRecvBuff_List_t*)phNxpEse_memalloc(
89 sizeof(phNxpEse_sCoreRecvBuff_List_t));
90 if (newNode == NULL) {
91 return ESESTATUS_NOT_ENOUGH_MEMORY;
92 }
93 newNode->pNext = NULL;
94 newNode->tData.wLen = data_len;
95 phNxpEse_memcpy(newNode->tData.sbuffer, pbuff, data_len);
96 total_len += data_len;
97 if (head == NULL) {
98 head = newNode;
99 current = newNode;
100 } else {
101 current->pNext = newNode;
102 current = newNode;
103 }
104 return ESESTATUS_SUCCESS;
105 }
106
107 /******************************************************************************
108 * Function phNxpEse_GetDataFromList
109 *
110 * Description This function copies all linked list data in provided buffer
111 *
112 * Returns On Success ESESTATUS_SUCCESS else proper error code
113 *
114 ******************************************************************************/
phNxpEse_GetDataFromList(uint32_t * data_len,uint8_t * pbuff)115 static ESESTATUS phNxpEse_GetDataFromList(uint32_t* data_len, uint8_t* pbuff) {
116 phNxpEse_sCoreRecvBuff_List_t* new_node;
117 uint32_t offset = 0;
118 ALOGD_IF(ese_debug_enabled, "%s Enter ", __FUNCTION__);
119 if (head == NULL || pbuff == NULL) {
120 return ESESTATUS_FAILED;
121 }
122
123 new_node = head;
124 while (new_node != NULL) {
125 phNxpEse_memcpy((pbuff + offset), new_node->tData.sbuffer,
126 new_node->tData.wLen);
127 offset += new_node->tData.wLen;
128 new_node = new_node->pNext;
129 }
130 *data_len = offset;
131 ALOGD_IF(ese_debug_enabled, "%s Exit ", __FUNCTION__);
132 return ESESTATUS_SUCCESS;
133 }
134
135 /******************************************************************************
136 * Function phNxpEse_DeletList
137 *
138 * Description This function deletes all nodes from linked list
139 *
140 * Returns On Success ESESTATUS_SUCCESS else proper error code
141 *
142 ******************************************************************************/
phNxpEse_DeletList(phNxpEse_sCoreRecvBuff_List_t * head)143 static ESESTATUS phNxpEse_DeletList(phNxpEse_sCoreRecvBuff_List_t* head) {
144 ESESTATUS status = ESESTATUS_SUCCESS;
145 phNxpEse_sCoreRecvBuff_List_t *current, *next;
146 current = head;
147
148 if (head == NULL) {
149 return ESESTATUS_FAILED;
150 }
151
152 while (current != NULL) {
153 next = current->pNext;
154 phNxpEse_free(current);
155 current = NULL;
156 current = next;
157 }
158 head = NULL;
159 return status;
160 }
161