1 /******************************************************************************
2  *
3  *  Copyright 2018 NXP
4  *  Copyright (C) 2018 ST Microelectronics S.A.
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 #define LOG_TAG "StEse-DataMgmt"
20 #include "DataMgmt.h"
21 #include <stdlib.h>
22 #include <string.h>
23 #include "Atp.h"
24 #include "android_logmsg.h"
25 
26 static TpduRecvBuff_List_t *head = NULL, *current = NULL;
27 static uint16_t total_len = 0;
28 
29 static int DataMgmt_DeletList(TpduRecvBuff_List_t* head);
30 static int DataMgmt_GetDataFromList(uint16_t* data_len, uint8_t* pbuff);
31 /******************************************************************************
32  * Function         DataMgmt_GetData
33  *
34  * Description      This function update the len and provided buffer
35  *
36  * Returns          On Success ESESTATUS_SUCCESS else proper error code
37  *
38  ******************************************************************************/
DataMgmt_GetData(uint16_t * data_len,uint8_t ** pbuffer)39 int DataMgmt_GetData(uint16_t* data_len, uint8_t** pbuffer) {
40   uint16_t total_data_len = 0;
41   uint8_t* pbuff = NULL;
42 
43   if (total_len == 0) {
44     STLOG_HAL_E("%s total_len = %d", __FUNCTION__, total_len);
45     return -1;
46   }
47   pbuff = (uint8_t*)malloc(total_len);
48   if (NULL == pbuff) {
49     STLOG_HAL_E("%s Error in malloc ", __FUNCTION__);
50     return -1;
51   }
52 
53   if (0 != DataMgmt_GetDataFromList(&total_data_len, pbuff)) {
54     STLOG_HAL_E("%s DataMgmt_GetDataFromList", __FUNCTION__);
55     free(pbuff);
56     return -1;
57   }
58   if (total_data_len != total_len) {
59     STLOG_HAL_E("%s Mismatch of len total_data_len %d total_len %d",
60                 __FUNCTION__, total_data_len, total_len);
61     free(pbuff);
62     return -1;
63   }
64 
65   *pbuffer = pbuff;
66   *data_len = total_data_len;
67   DataMgmt_DeletList(head);
68   total_len = 0;
69   head = NULL;
70   current = NULL;
71 
72   return 0;
73 }
74 
75 /******************************************************************************
76  * Function         DataMgmt_StoreDataInList
77  *
78  * Description      This function stores the received data in linked list
79  *
80  * Returns          On Success ESESTATUS_SUCCESS else proper error code
81  *
82  ******************************************************************************/
DataMgmt_StoreDataInList(uint16_t data_len,uint8_t * pbuff)83 int DataMgmt_StoreDataInList(uint16_t data_len, uint8_t* pbuff) {
84   TpduRecvBuff_List_t* newNode = NULL;
85 
86   if (data_len > ATP.ifsc) {
87     return -1;
88   }
89   newNode = (TpduRecvBuff_List_t*)malloc(sizeof(TpduRecvBuff_List_t));
90   if (newNode == NULL) {
91     return -1;
92   }
93   newNode->pNext = NULL;
94   newNode->tData.len = data_len;
95   newNode->tData.data = (uint8_t*)malloc(ATP.ifsc * sizeof(uint8_t));
96   if (newNode->tData.data == NULL) {
97     free(newNode);
98     return -1;
99   }
100   memcpy(newNode->tData.data, pbuff, data_len);
101 
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 0;
111 }
112 
113 /******************************************************************************
114  * Function         DataMgmt_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  ******************************************************************************/
DataMgmt_GetDataFromList(uint16_t * data_len,uint8_t * pbuff)121 static int DataMgmt_GetDataFromList(uint16_t* data_len, uint8_t* pbuff) {
122   TpduRecvBuff_List_t* new_node;
123   uint16_t offset = 0;
124   if (head == NULL || pbuff == NULL) {
125     return -1;
126   }
127 
128   new_node = head;
129   while (new_node != NULL) {
130     if (new_node->tData.len > ATP.ifsc) {
131       return -1;
132     }
133     memcpy((pbuff + offset), new_node->tData.data, new_node->tData.len);
134     offset += new_node->tData.len;
135     new_node = new_node->pNext;
136   }
137   *data_len = offset;
138   return 0;
139 }
140 
141 /******************************************************************************
142  * Function         DataMgmt_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  ******************************************************************************/
DataMgmt_DeletList(TpduRecvBuff_List_t * head)149 static int DataMgmt_DeletList(TpduRecvBuff_List_t* head) {
150   TpduRecvBuff_List_t *current, *next;
151   current = head;
152 
153   if (head == NULL) {
154     return -1;
155   }
156 
157   while (current != NULL) {
158     next = current->pNext;
159     free(current->tData.data);
160     current->tData.data = NULL;
161     free(current);
162     current = NULL;
163     current = next;
164   }
165   head = NULL;
166   return 0;
167 }
168