1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*=============================================================================
18
19                  Module Name: dmtDataChunk.cc
20
21           General Description: Implementation of DmtDataChunk class.
22
23=============================================================================*/
24
25#include "dmt.hpp"
26#include "xpl_dm_Manager.h"
27#include "dmMemory.h"
28
29/*=============================================================================
30*Function called:   DmtDataChunk
31*Parameters :
32*Returns:
33*Synopsis:          The constructor of DmtDataChunk
34*Pre-conditions:
35*Post-Conditions:
36
37=============================================================================*/
38DmtDataChunk::DmtDataChunk()
39{
40    chunkBuffer = NULL;
41    dataSize = 0L;
42    returnLen = 0L;
43    maxDataSize =  XPL_DM_GetChunkSize();
44}
45
46/*=============================================================================
47*Function called:   DmtDataChunk::attach
48*Parameters :
49*Returns:
50*Synopsis:          Attach chunk buffer
51*Pre-conditions:
52*Post-Conditions:
53
54=============================================================================*/
55void DmtDataChunk::attach(UINT8    *chunkBuffer, UINT32 dataSize)
56{
57    this->chunkBuffer = chunkBuffer;
58    this->dataSize = dataSize;
59    returnLen = 0L;
60    maxDataSize =  XPL_DM_GetChunkSize();
61}
62
63/*=============================================================================
64*Function called:   DmtDataChunk::detach
65*Parameters :
66*Returns:
67*Synopsis:          Detach chunk buffer
68*Pre-conditions:
69*Post-Conditions:
70
71=============================================================================*/
72UINT8 * DmtDataChunk::detach()
73{
74    UINT8 *pBuffer = this->chunkBuffer;
75    this->chunkBuffer = NULL;
76    this->dataSize = 0;
77    returnLen = 0L;
78    return pBuffer;
79}
80
81/*=============================================================================
82*Function called:   ~DmtDataChunk
83*Parameters :
84*Returns:
85*Synopsis:          The destructor of DmtDataChunk
86*Pre-conditions:
87*Post-Conditions:
88
89=============================================================================*/
90DmtDataChunk::~DmtDataChunk()
91{
92    if(chunkBuffer != NULL)
93        DmFreeMem(chunkBuffer);
94}
95
96/*=============================================================================
97*Function called:  GetChunkSize
98*Parameters :
99*Returns:       Return chunk buffer size to acess ESN data
100*Synopsis:
101*Pre-conditions:
102*Post-Conditions:
103
104=============================================================================*/
105 UINT32 DmtDataChunk::GetChunkSize()
106{
107     return XPL_DM_GetChunkSize();
108}
109
110/*=============================================================================
111*Function called:   GetChunkData
112*Parameters :
113*            bufp --Pointer to the pointer of  chunk data
114*Returns:
115*    return SYNCML_DM_SUCCESS if get data successfully
116*    return other in case of error
117
118*Synopsis:
119
120*Pre-conditions:
121*Post-Conditions:
122=============================================================================*/
123 SYNCML_DM_RET_STATUS_T DmtDataChunk::GetChunkData( UINT8 **bufp)
124 {
125 *bufp = chunkBuffer;
126  return SYNCML_DM_SUCCESS;
127 }
128
129/*=============================================================================
130*Function called:   GetChunkDataSize
131*Parameters :
132*             dataSize - return data size
133*Returns:
134*    return SYNCML_DM_SUCCESS if get data size successfully
135*    return other in case of error
136
137*Synopsis:
138
139*Pre-conditions:
140*Post-Conditions:
141=============================================================================*/
142 SYNCML_DM_RET_STATUS_T DmtDataChunk::GetChunkDataSize( UINT32& dataSize)
143{
144 dataSize = this->dataSize;
145  return SYNCML_DM_SUCCESS;
146}
147
148/*=============================================================================
149*Function called:   GetReturnLen
150*Parameters :
151*            len --Reference to return data size in chunk buffer
152*Returns:
153*    return SYNCML_DM_SUCCESS if get  return size successfully
154*    return other in case of error
155
156*Synopsis:
157
158*Pre-conditions:
159*Post-Conditions:
160=============================================================================*/
161
162 SYNCML_DM_RET_STATUS_T DmtDataChunk::GetReturnLen( UINT32& len ) const
163{
164    len = returnLen;
165      return SYNCML_DM_SUCCESS;
166}
167/*=============================================================================
168*Function called:   SetReturnLen
169*Parameters :
170*            len --Reference to return data size in chunk buffer
171*Returns:
172*    return SYNCML_DM_SUCCESS if set return length successfully
173*    return other in case of error
174
175*Synopsis:
176
177*Pre-conditions:
178*Post-Conditions:
179=============================================================================*/
180 SYNCML_DM_RET_STATUS_T DmtDataChunk::SetReturnLen( UINT32& len )
181{
182  returnLen = len;
183  return SYNCML_DM_SUCCESS;
184}
185
186/*=============================================================================
187*Function called:   SetChunkData
188*Parameters :
189            buf --Pointer to data buffer need to copy to chunk buffer
190            dataSize -- Data size in the buf
191*Returns:
192*    return SYNCML_DM_SUCCESS if if set chunk data successfully
193*    return other in case of error
194
195*Synopsis:
196
197*Pre-conditions:
198*Post-Conditions:
199=============================================================================*/
200
201SYNCML_DM_RET_STATUS_T DmtDataChunk::SetChunkData(const UINT8 *buf, UINT32 dataSize)
202{
203SYNCML_DM_RET_STATUS_T resStatus = SYNCML_DM_SUCCESS;
204    if( buf != NULL)
205    {
206        if(dataSize > maxDataSize)
207            return  SYNCML_DM_INVALID_PARAMETER;
208
209        if(chunkBuffer == NULL)
210        {      chunkBuffer = (UINT8*)DmAllocMem(maxDataSize);
211            if(chunkBuffer == NULL)
212                return SYNCML_DM_DEVICE_FULL;
213        }
214        memcpy(chunkBuffer, buf , dataSize);
215    }
216    this->dataSize = dataSize;
217    return resStatus;
218}
219/*=============================================================================
220*Function called:   AllocateChunkBuffer
221*Parameters :
222*Returns:
223*    return SYNCML_DM_SUCCESS if if allocate chunk buffer successfully
224*    return other in case of error
225
226*Synopsis:
227
228*Pre-conditions:
229*Post-Conditions:
230=============================================================================*/
231SYNCML_DM_RET_STATUS_T DmtDataChunk::AllocateChunkBuffer()
232{
233    if (chunkBuffer == NULL)
234    {
235        chunkBuffer = (UINT8*)DmAllocMem(maxDataSize);
236        if (chunkBuffer == NULL)
237            return SYNCML_DM_DEVICE_FULL;
238    }
239    return SYNCML_DM_SUCCESS;
240}
241/*=============================================================================
242*Function called:   FreeChunkBuffer
243*Parameters :
244*Returns:
245*    return SYNCML_DM_SUCCESS if if free chunk buffer successfully
246*    return other in case of error
247
248*Synopsis:
249
250*Pre-conditions:
251*Post-Conditions:
252=============================================================================*/
253SYNCML_DM_RET_STATUS_T DmtDataChunk::FreeChunkBuffer()
254{
255    if (chunkBuffer != NULL)
256    {
257        DmFreeMem(chunkBuffer);
258        chunkBuffer = NULL;
259    }
260    return SYNCML_DM_SUCCESS;
261}
262