update the latest source
[platform/core/connectivity/smartcard-service.git] / common / FileObject.cpp
1 /*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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 /* standard library header */
19 #include <stdio.h>
20
21 /* SLP library header */
22
23 /* local header */
24 #include "Debug.h"
25 #include "FileObject.h"
26 #include "APDUHelper.h"
27
28 namespace smartcard_service_api
29 {
30         FileObject::FileObject(Channel *channel):ProviderHelper(channel)
31         {
32         }
33
34         FileObject::FileObject(Channel *channel, ByteArray selectResponse):ProviderHelper(channel)
35         {
36                 setSelectResponse(selectResponse);
37         }
38
39         FileObject::~FileObject()
40         {
41         }
42
43         bool FileObject::setSelectResponse(ByteArray response)
44         {
45                 bool result = false;
46
47                 selectResponse = response;
48
49                 if (selectResponse.getLength() > 2)
50                 {
51                         ResponseHelper resp(selectResponse);
52
53                         fcp.setFCP(resp.getDataField());
54
55                         result = true;
56                 }
57                 else
58                 {
59                         SCARD_DEBUG_ERR("invalid response : %s", selectResponse.toString());
60                 }
61
62                 return result;
63         }
64
65         int FileObject::select(ByteArray aid)
66         {
67                 int ret = ERROR_ILLEGAL_STATE;
68                 ByteArray command, result;
69                 APDUHelper apdu;
70
71                 if (channel == NULL || channel->isClosed())
72                 {
73                         SCARD_DEBUG_ERR("channel is not open");
74
75                         return ret;
76                 }
77
78                 /* make apdu command */
79                 command = apdu.generateAPDU(APDUHelper::COMMAND_SELECT_BY_DF_NAME, 0, aid);
80                 SCARD_DEBUG("command : %s", command.toString());
81                 ret = channel->transmitSync(command, result);
82
83                 if (ret == 0 && result.getLength() >= 2)
84                 {
85                         ResponseHelper resp(result);
86                         this->selectResponse = result;
87
88                         if (resp.getStatus() == 0)
89                         {
90                                 SCARD_DEBUG("response [%d] : %s", result.getLength(), result.toString());
91
92                                 fcp.releaseFCP();
93
94                                 if (result.getLength() > 2)
95                                 {
96                                         fcp.setFCP(resp.getDataField());
97
98                                         SCARD_DEBUG("FCP : %s", fcp.toString());
99                                 }
100
101                                 ret = SUCCESS;
102                         }
103                         else
104                         {
105                                 SCARD_DEBUG_ERR("status word [%d][ 0x%02X 0x%02X ]", resp.getStatus(), result[result.getLength() - 2], result[result.getLength() - 1]);
106                         }
107                 }
108                 else
109                 {
110                         SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, result.getLength());
111                 }
112
113                 return ret;
114         }
115
116         int FileObject::select(ByteArray path, bool fromCurrentDF)
117         {
118                 int ret = ERROR_ILLEGAL_STATE;
119                 ByteArray command, result;
120                 APDUHelper apdu;
121
122                 if (channel == NULL || channel->isClosed())
123                 {
124                         SCARD_DEBUG_ERR("channel is not open");
125
126                         return ret;
127                 }
128
129                 /* make apdu command */
130                 if (fromCurrentDF == true)
131                 {
132                         command = apdu.generateAPDU(APDUHelper::COMMAND_SELECT_BY_PATH_FROM_CURRENT_DF, 0, path);
133                 }
134                 else
135                 {
136                         command = apdu.generateAPDU(APDUHelper::COMMAND_SELECT_BY_PATH, 0, path);
137                 }
138                 SCARD_DEBUG("command : %s", command.toString());
139
140                 ret = channel->transmitSync(command, result);
141
142                 if (ret == 0 && result.getLength() >= 2)
143                 {
144                         ResponseHelper resp(result);
145                         this->selectResponse = result;
146
147                         if (resp.getStatus() == 0)
148                         {
149                                 SCARD_DEBUG("response [%d] : %s", result.getLength(), result.toString());
150
151                                 fcp.releaseFCP();
152
153                                 if (result.getLength() > 2)
154                                 {
155                                         fcp.setFCP(resp.getDataField());
156
157                                         SCARD_DEBUG("FCP : %s", fcp.toString());
158                                 }
159
160                                 ret = SUCCESS;
161                         }
162                         else
163                         {
164                                 SCARD_DEBUG_ERR("status word [%d][ 0x%02X 0x%02X ]", resp.getStatus(), result[result.getLength() - 2], result[result.getLength() - 1]);
165                         }
166                 }
167                 else
168                 {
169                         SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, result.getLength());
170                 }
171
172                 return ret;
173         }
174
175         int FileObject::select(unsigned int fid)
176         {
177                 int ret = ERROR_ILLEGAL_STATE;
178                 ByteArray command, result, fidData((unsigned char *)&fid, 2);
179
180                 if (channel == NULL || channel->isClosed())
181                 {
182                         SCARD_DEBUG_ERR("channel is not open");
183
184                         return ret;
185                 }
186
187                 /* make apdu command */
188                 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_ID, 0, fidData);
189                 ret = channel->transmitSync(command, result);
190
191                 if (ret == 0 && result.getLength() >= 2)
192                 {
193                         ResponseHelper resp(result);
194                         this->selectResponse = result;
195
196                         if (resp.getStatus() == 0)
197                         {
198                                 SCARD_DEBUG("response [%d] : %s", result.getLength(), result.toString());
199
200                                 fcp.releaseFCP();
201
202                                 if (result.getLength() > 2)
203                                 {
204                                         fcp.setFCP(resp.getDataField());
205
206                                         SCARD_DEBUG("FCP : %s", fcp.toString());
207                                 }
208
209                                 ret = SUCCESS;
210                         }
211                         else
212                         {
213                                 SCARD_DEBUG_ERR("status word [%d][ 0x%02X 0x%02X ]", resp.getStatus(), result[result.getLength() - 2], result[result.getLength() - 1]);
214                         }
215                 }
216                 else
217                 {
218                         SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, result.getLength());
219                 }
220
221                 return ret;
222         }
223
224         int FileObject::selectParent()
225         {
226                 int ret = ERROR_ILLEGAL_STATE;
227                 ByteArray command, result;
228                 APDUHelper apdu;
229
230                 if (channel == NULL || channel->isClosed())
231                 {
232                         SCARD_DEBUG_ERR("channel is not open");
233
234                         return ret;
235                 }
236
237                 /* make apdu command */
238                 command = apdu.generateAPDU(APDUHelper::COMMAND_SELECT_PARENT_DF, 0, ByteArray::EMPTY);
239                 SCARD_DEBUG("command : %s", command.toString());
240
241                 ret = channel->transmitSync(command, result);
242
243                 if (ret == 0 && result.getLength() >= 2)
244                 {
245                         ResponseHelper resp(result);
246                         this->selectResponse = result;
247
248                         if (resp.getStatus() == 0)
249                         {
250                                 SCARD_DEBUG("response [%d] : %s", result.getLength(), result.toString());
251
252                                 fcp.releaseFCP();
253
254                                 if (result.getLength() > 2)
255                                 {
256                                         fcp.setFCP(resp.getDataField());
257
258                                         SCARD_DEBUG("FCP : %s", fcp.toString());
259                                 }
260
261                                 ret = SUCCESS;
262                         }
263                         else
264                         {
265                                 SCARD_DEBUG_ERR("status word [%d][ 0x%02X 0x%02X ]", resp.getStatus(), result[result.getLength() - 2], result[result.getLength() - 1]);
266                         }
267                 }
268                 else
269                 {
270                         SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, result.getLength());
271                 }
272
273                 return ret;
274         }
275
276         FCI *FileObject::getFCI()
277         {
278                 return NULL;
279         }
280
281         FCP *FileObject::getFCP()
282         {
283                 return &fcp;
284         }
285
286         int FileObject::readRecord(unsigned int sfi, unsigned int recordId, Record &result)
287         {
288                 ByteArray command, response;
289                 APDUCommand apdu;
290                 int ret;
291
292                 apdu.setCommand(0, APDUCommand::INS_READ_RECORD, recordId, 4, ByteArray::EMPTY, 0);
293                 apdu.getBuffer(command);
294                 SCARD_DEBUG("command : %s", command.toString());
295
296                 ret = channel->transmitSync(command, response);
297                 if (ret == 0 && response.getLength() >= 2)
298                 {
299                         ResponseHelper resp(response);
300
301                         if (resp.getStatus() == 0)
302                         {
303                                 SCARD_DEBUG("response [%d] : %s", response.getLength(), response.toString());
304
305 //                              result = resp.getDataField();
306
307                                 ret = SUCCESS;
308                         }
309                         else
310                         {
311                                 SCARD_DEBUG_ERR("status word [%d][ 0x%02X 0x%02X ]", resp.getStatus(), response[response.getLength() - 2], response[response.getLength() - 1]);
312                         }
313                 }
314                 else
315                 {
316                         SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, response.getLength());
317                 }
318
319                 return ret;
320         }
321
322         int FileObject::writeRecord(unsigned int sfi, Record record)
323         {
324                 return 0;
325         }
326
327         int FileObject::searchRecord(unsigned int sfi, ByteArray searchParam, vector<int> &result)
328         {
329                 return 0;
330         }
331
332         int FileObject::readBinary(unsigned int sfi, unsigned int offset, unsigned int length, ByteArray &result)
333         {
334                 ByteArray command, response;
335                 APDUCommand apdu;
336                 int ret;
337
338                 apdu.setCommand(0, APDUCommand::INS_READ_BINARY, offset, 0, ByteArray::EMPTY, length);
339                 apdu.getBuffer(command);
340                 SCARD_DEBUG("command : %s", command.toString());
341
342                 ret = channel->transmitSync(command, response);
343                 if (ret == 0 && response.getLength() >= 2)
344                 {
345                         ResponseHelper resp(response);
346
347                         if (resp.getStatus() == 0)
348                         {
349                                 SCARD_DEBUG("response [%d] : %s", response.getLength(), response.toString());
350
351                                 result = resp.getDataField();
352
353                                 ret = SUCCESS;
354                         }
355                         else
356                         {
357                                 SCARD_DEBUG_ERR("status word [%d][ 0x%02X 0x%02X ]", resp.getStatus(), response[response.getLength() - 2], response[response.getLength() - 1]);
358                         }
359                 }
360                 else
361                 {
362                         SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, response.getLength());
363                 }
364
365                 return ret;
366         }
367
368         int FileObject::writeBinary(unsigned int sfi, ByteArray data, unsigned int offset, unsigned int length)
369         {
370                 ByteArray command, response;
371                 APDUCommand apdu;
372                 int ret;
373
374                 apdu.setCommand(0, APDUCommand::INS_WRITE_BINARY, offset, 0, data, 0);
375                 apdu.getBuffer(command);
376                 SCARD_DEBUG("command : %s", command.toString());
377
378                 ret = channel->transmitSync(command, response);
379                 if (ret == 0 && response.getLength() >= 2)
380                 {
381                         ResponseHelper resp(response);
382
383                         if (resp.getStatus() == 0)
384                         {
385                                 SCARD_DEBUG("response [%d] : %s", response.getLength(), response.toString());
386
387                                 ret = SUCCESS;
388                         }
389                         else
390                         {
391                                 SCARD_DEBUG_ERR("status word [%d][ 0x%02X 0x%02X ]", resp.getStatus(), response[response.getLength() - 2], response[response.getLength() - 1]);
392                         }
393                 }
394                 else
395                 {
396                         SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, response.getLength());
397                 }
398
399                 return ret;
400         }
401
402 } /* namespace smartcard_service_api */