2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 /* standard library header */
21 /* SLP library header */
25 #include "FileObject.h"
26 #include "APDUHelper.h"
28 namespace smartcard_service_api
30 FileObject::FileObject(Channel *channel):ProviderHelper(channel)
34 FileObject::FileObject(Channel *channel, ByteArray selectResponse):ProviderHelper(channel)
36 setSelectResponse(selectResponse);
39 FileObject::~FileObject()
43 bool FileObject::setSelectResponse(ByteArray response)
47 selectResponse = response;
49 if (selectResponse.getLength() > 2)
51 ResponseHelper resp(selectResponse);
53 fcp.setFCP(resp.getDataField());
59 SCARD_DEBUG_ERR("invalid response : %s", selectResponse.toString());
65 int FileObject::select(ByteArray aid)
67 int ret = ERROR_ILLEGAL_STATE;
68 ByteArray command, result;
71 if (channel == NULL || channel->isClosed())
73 SCARD_DEBUG_ERR("channel is not open");
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);
83 if (ret == 0 && result.getLength() >= 2)
85 ResponseHelper resp(result);
86 this->selectResponse = result;
88 if (resp.getStatus() == 0)
90 SCARD_DEBUG("response [%d] : %s", result.getLength(), result.toString());
94 if (result.getLength() > 2)
96 fcp.setFCP(resp.getDataField());
98 SCARD_DEBUG("FCP : %s", fcp.toString());
105 SCARD_DEBUG_ERR("status word [%d][ 0x%02X 0x%02X ]", resp.getStatus(), result[result.getLength() - 2], result[result.getLength() - 1]);
110 SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, result.getLength());
116 int FileObject::select(ByteArray path, bool fromCurrentDF)
118 int ret = ERROR_ILLEGAL_STATE;
119 ByteArray command, result;
122 if (channel == NULL || channel->isClosed())
124 SCARD_DEBUG_ERR("channel is not open");
129 /* make apdu command */
130 if (fromCurrentDF == true)
132 command = apdu.generateAPDU(APDUHelper::COMMAND_SELECT_BY_PATH_FROM_CURRENT_DF, 0, path);
136 command = apdu.generateAPDU(APDUHelper::COMMAND_SELECT_BY_PATH, 0, path);
138 SCARD_DEBUG("command : %s", command.toString());
140 ret = channel->transmitSync(command, result);
142 if (ret == 0 && result.getLength() >= 2)
144 ResponseHelper resp(result);
145 this->selectResponse = result;
147 if (resp.getStatus() == 0)
149 SCARD_DEBUG("response [%d] : %s", result.getLength(), result.toString());
153 if (result.getLength() > 2)
155 fcp.setFCP(resp.getDataField());
157 SCARD_DEBUG("FCP : %s", fcp.toString());
164 SCARD_DEBUG_ERR("status word [%d][ 0x%02X 0x%02X ]", resp.getStatus(), result[result.getLength() - 2], result[result.getLength() - 1]);
169 SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, result.getLength());
175 int FileObject::select(unsigned int fid)
177 int ret = ERROR_ILLEGAL_STATE;
178 ByteArray command, result, fidData((unsigned char *)&fid, 2);
180 if (channel == NULL || channel->isClosed())
182 SCARD_DEBUG_ERR("channel is not open");
187 /* make apdu command */
188 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_ID, 0, fidData);
189 ret = channel->transmitSync(command, result);
191 if (ret == 0 && result.getLength() >= 2)
193 ResponseHelper resp(result);
194 this->selectResponse = result;
196 if (resp.getStatus() == 0)
198 SCARD_DEBUG("response [%d] : %s", result.getLength(), result.toString());
202 if (result.getLength() > 2)
204 fcp.setFCP(resp.getDataField());
206 SCARD_DEBUG("FCP : %s", fcp.toString());
213 SCARD_DEBUG_ERR("status word [%d][ 0x%02X 0x%02X ]", resp.getStatus(), result[result.getLength() - 2], result[result.getLength() - 1]);
218 SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, result.getLength());
224 int FileObject::selectParent()
226 int ret = ERROR_ILLEGAL_STATE;
227 ByteArray command, result;
230 if (channel == NULL || channel->isClosed())
232 SCARD_DEBUG_ERR("channel is not open");
237 /* make apdu command */
238 command = apdu.generateAPDU(APDUHelper::COMMAND_SELECT_PARENT_DF, 0, ByteArray::EMPTY);
239 SCARD_DEBUG("command : %s", command.toString());
241 ret = channel->transmitSync(command, result);
243 if (ret == 0 && result.getLength() >= 2)
245 ResponseHelper resp(result);
246 this->selectResponse = result;
248 if (resp.getStatus() == 0)
250 SCARD_DEBUG("response [%d] : %s", result.getLength(), result.toString());
254 if (result.getLength() > 2)
256 fcp.setFCP(resp.getDataField());
258 SCARD_DEBUG("FCP : %s", fcp.toString());
265 SCARD_DEBUG_ERR("status word [%d][ 0x%02X 0x%02X ]", resp.getStatus(), result[result.getLength() - 2], result[result.getLength() - 1]);
270 SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, result.getLength());
276 FCI *FileObject::getFCI()
281 FCP *FileObject::getFCP()
286 int FileObject::readRecord(unsigned int sfi, unsigned int recordId, Record &result)
288 ByteArray command, response;
292 apdu.setCommand(0, APDUCommand::INS_READ_RECORD, recordId, 4, ByteArray::EMPTY, 0);
293 apdu.getBuffer(command);
294 SCARD_DEBUG("command : %s", command.toString());
296 ret = channel->transmitSync(command, response);
297 if (ret == 0 && response.getLength() >= 2)
299 ResponseHelper resp(response);
301 if (resp.getStatus() == 0)
303 SCARD_DEBUG("response [%d] : %s", response.getLength(), response.toString());
305 // result = resp.getDataField();
311 SCARD_DEBUG_ERR("status word [%d][ 0x%02X 0x%02X ]", resp.getStatus(), response[response.getLength() - 2], response[response.getLength() - 1]);
316 SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, response.getLength());
322 int FileObject::writeRecord(unsigned int sfi, Record record)
327 int FileObject::searchRecord(unsigned int sfi, ByteArray searchParam, vector<int> &result)
332 int FileObject::readBinary(unsigned int sfi, unsigned int offset, unsigned int length, ByteArray &result)
334 ByteArray command, response;
338 apdu.setCommand(0, APDUCommand::INS_READ_BINARY, offset, 0, ByteArray::EMPTY, length);
339 apdu.getBuffer(command);
340 SCARD_DEBUG("command : %s", command.toString());
342 ret = channel->transmitSync(command, response);
343 if (ret == 0 && response.getLength() >= 2)
345 ResponseHelper resp(response);
347 if (resp.getStatus() == 0)
349 SCARD_DEBUG("response [%d] : %s", response.getLength(), response.toString());
351 result = resp.getDataField();
357 SCARD_DEBUG_ERR("status word [%d][ 0x%02X 0x%02X ]", resp.getStatus(), response[response.getLength() - 2], response[response.getLength() - 1]);
362 SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, response.getLength());
368 int FileObject::writeBinary(unsigned int sfi, ByteArray data, unsigned int offset, unsigned int length)
370 ByteArray command, response;
374 apdu.setCommand(0, APDUCommand::INS_WRITE_BINARY, offset, 0, data, 0);
375 apdu.getBuffer(command);
376 SCARD_DEBUG("command : %s", command.toString());
378 ret = channel->transmitSync(command, response);
379 if (ret == 0 && response.getLength() >= 2)
381 ResponseHelper resp(response);
383 if (resp.getStatus() == 0)
385 SCARD_DEBUG("response [%d] : %s", response.getLength(), response.toString());
391 SCARD_DEBUG_ERR("status word [%d][ 0x%02X 0x%02X ]", resp.getStatus(), response[response.getLength() - 2], response[response.getLength() - 1]);
396 SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, response.getLength());
402 } /* namespace smartcard_service_api */