2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
22 #include <Commons/Exception.h>
23 #include <JSTizenException.h>
24 #include "SESession.h"
29 using namespace WrtDeviceApis;
30 using namespace smartcard_service_api;
33 namespace SecureElement {
36 static void openChannelCallback(Channel *channel, int error, void *userData) {
37 LoggerD("Callback openChannelCallback.");
39 SESession *seSessionPtr = (SESession *)((EventSEPtrs<EventSEOpenChannel> *)userData)->getThisPtr();
41 EventSEOpenChannelPtr event = ((EventSEPtrs<EventSEOpenChannel> *)userData)->getEventPtrs();
42 seSessionPtr->openChannelManualAnswer((void *)channel, error, event);
45 LoggerE("Callback private data is NULL.");
50 SESession::SESession(void *session)
55 ThrowMsg(Commons::UnknownException, "SecureElement Handler is Null Pointer.");
57 m_session = (Session *)session;
60 SESession::~SESession()
68 bool SESession::isClosed() {
69 LoggerD("Enter" << m_session->isClosed());
70 return m_session->isClosed();
73 std::vector<unsigned char> SESession::getATR() {
75 ByteArray atrByteArray = m_session->getATRSync();
77 return util.toVector(atrByteArray.getBuffer(), atrByteArray.getLength());
80 void SESession::close() {
82 m_session->closeSync();
85 void SESession::closeChannels() {
87 m_session->closeChannels();
90 void SESession::openChannel(const EventSEOpenChannelPtr& event) {
92 EventRequestReceiver<EventSEOpenChannel>::PostRequest(event);
96 void SESession::openChannelManualAnswer(void * channel, int error, const EventSEOpenChannelPtr &event) {
98 if (error != SCARD_ERROR_OK) {
99 event->setResult(false);
100 event->setError(util.getErrorString(error));
101 event->setErrorMessage(util.getErrorMessage(error));
102 } else if (channel == NULL) {
103 event->setResult(false);
104 event->setError(DeviceAPI::Common::JSTizenException::UNKNOWN_ERROR);
105 event->setErrorMessage("Unknown Error");
107 event->setChannel(channel);
108 event->setResult(true);
110 EventRequestReceiver<EventSEOpenChannel>::ManualAnswer(event);
112 std::vector<SEOpenChannelDataPtr>::iterator it;
113 for (it = EventSEOpenChannels.begin(); it != EventSEOpenChannels.end(); ++it) {
114 if ((*it)->getEventPtrs() == event) {
115 EventSEOpenChannels.erase(it);
116 LoggerD("event is removed. (" << EventSEOpenChannels.size() << ")");
122 void SESession::OnRequestReceived(const EventSEOpenChannelPtr& event) {
126 event->switchToManualAnswer();
128 SEOpenChannelDataPtr data( new EventSEPtrs<EventSEOpenChannel>(event, this));
129 EventSEOpenChannels.push_back(data);
131 std::vector<unsigned char> aid = event->getAID();
133 unsigned char *channelAid = util.toCharPtr(aid);
134 if (event->isBasicChannel())
135 m_session->openBasicChannel(channelAid, (unsigned int)aid.size(), openChannelCallback, data.Get());
137 m_session->openLogicalChannel(channelAid, (unsigned int)aid.size(), openChannelCallback, data.Get());
140 } catch (const WrtDeviceApis::Commons::Exception& ex) {
141 LoggerE("Exception: " << ex.GetMessage());
142 event->setResult(false);
143 event->setError(DeviceAPI::Common::JSTizenException::UNKNOWN_ERROR);
144 event->setErrorMessage("Unknown Error");
145 EventRequestReceiver<EventSEOpenChannel>::ManualAnswer(event);