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)
35 FileObject::FileObject(Channel *channel, ByteArray selectResponse):ProviderHelper(channel)
38 setSelectResponse(selectResponse);
41 FileObject::~FileObject()
45 bool FileObject::setSelectResponse(ByteArray &response)
49 if (response.getLength() >= 2)
51 ResponseHelper resp(response);
52 selectResponse = response;
54 if (resp.getStatus() == 0)
58 fcp.setFCP(resp.getDataField());
60 SCARD_DEBUG("FCP : %s", fcp.toString());
67 SCARD_DEBUG_ERR("status word [%d][ %02X %02X ]", resp.getStatus(), resp.getSW1(), resp.getSW2());
72 SCARD_DEBUG_ERR("invalid response : %s", response.toString());
78 int FileObject::_select(ByteArray command)
80 int ret = ERROR_ILLEGAL_STATE;
83 if (channel == NULL || channel->isClosed())
85 SCARD_DEBUG_ERR("channel is not open");
92 ret = channel->transmitSync(command, result);
95 if (setSelectResponse(result) == true)
101 ret = ERROR_ILLEGAL_STATE;
106 SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, result.getLength());
108 ret = ERROR_ILLEGAL_STATE;
114 int FileObject::select(ByteArray aid)
116 int ret = ERROR_ILLEGAL_STATE;
119 /* make apdu command */
120 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_DF_NAME, 0, aid);
121 SCARD_DEBUG("command : %s", command.toString());
123 ret = _select(command);
128 int FileObject::select(ByteArray path, bool fromCurrentDF)
130 int ret = ERROR_ILLEGAL_STATE;
133 /* make apdu command */
134 if (fromCurrentDF == true)
136 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_PATH_FROM_CURRENT_DF, 0, path);
140 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_PATH, 0, path);
142 SCARD_DEBUG("command : %s", command.toString());
144 ret = _select(command);
149 int FileObject::select(unsigned int fid)
151 int ret = ERROR_ILLEGAL_STATE;
152 ByteArray command, fidData((unsigned char *)&fid, 2);
154 /* make apdu command */
155 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_ID, 0, fidData);
156 SCARD_DEBUG("command : %s", command.toString());
158 ret = _select(command);
163 int FileObject::selectParent()
165 int ret = ERROR_ILLEGAL_STATE;
168 /* make apdu command */
169 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_PARENT_DF, 0, ByteArray::EMPTY);
170 SCARD_DEBUG("command : %s", command.toString());
172 ret = _select(command);
177 FCI *FileObject::getFCI()
182 FCP *FileObject::getFCP()
187 int FileObject::readRecord(unsigned int sfi, unsigned int recordId, Record &result)
189 ByteArray command, response;
193 apdu.setCommand(0, APDUCommand::INS_READ_RECORD, recordId, 4, ByteArray::EMPTY, 0);
194 apdu.getBuffer(command);
195 SCARD_DEBUG("command : %s", command.toString());
197 ret = channel->transmitSync(command, response);
198 if (ret == 0 && response.getLength() >= 2)
200 ResponseHelper resp(response);
202 if (resp.getStatus() == 0)
204 SCARD_DEBUG("response [%d] : %s", response.getLength(), response.toString());
206 // result = resp.getDataField();
212 SCARD_DEBUG_ERR("status word [%d][ %02X %02X ]", resp.getStatus(), resp.getSW1(), resp.getSW2());
217 SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, response.getLength());
223 int FileObject::writeRecord(unsigned int sfi, Record record)
228 int FileObject::searchRecord(unsigned int sfi, ByteArray searchParam, vector<int> &result)
233 int FileObject::readBinary(unsigned int sfi, unsigned int offset, unsigned int length, ByteArray &result)
235 ByteArray command, response;
239 apdu.setCommand(0, APDUCommand::INS_READ_BINARY, offset, 0, ByteArray::EMPTY, length);
240 apdu.getBuffer(command);
241 SCARD_DEBUG("command : %s", command.toString());
243 ret = channel->transmitSync(command, response);
244 if (ret == 0 && response.getLength() >= 2)
246 ResponseHelper resp(response);
248 if (resp.getStatus() == 0)
250 SCARD_DEBUG("response [%d] : %s", response.getLength(), response.toString());
252 result = resp.getDataField();
258 SCARD_DEBUG_ERR("status word [%d][ %02X %02X ]", resp.getStatus(), resp.getSW1(), resp.getSW2());
263 SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, response.getLength());
269 int FileObject::writeBinary(unsigned int sfi, ByteArray data, unsigned int offset, unsigned int length)
271 ByteArray command, response;
275 apdu.setCommand(0, APDUCommand::INS_WRITE_BINARY, offset, 0, data, 0);
276 apdu.getBuffer(command);
277 SCARD_DEBUG("command : %s", command.toString());
279 ret = channel->transmitSync(command, response);
280 if (ret == 0 && response.getLength() >= 2)
282 ResponseHelper resp(response);
284 if (resp.getStatus() == 0)
286 SCARD_DEBUG("response [%d] : %s", response.getLength(), response.toString());
292 SCARD_DEBUG_ERR("status word [%d][ %02X %02X ]", resp.getStatus(), resp.getSW1(), resp.getSW2());
297 SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, response.getLength());
303 } /* namespace smartcard_service_api */