ff69b8693f70af73903d074114a7437d72581f9d
[framework/web/wrt-plugins-tizen.git] / src / SecureElement / SESession.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18
19
20 #include <Ecore.h>
21
22 #include <Commons/Exception.h>
23 #include <JSTizenException.h>
24 #include "SESession.h"
25 #include "SEUtil.h"
26 #include <Logger.h>
27
28
29 using namespace WrtDeviceApis;
30 using namespace smartcard_service_api;
31
32 namespace DeviceAPI {
33 namespace SecureElement {
34 namespace
35 {
36         static void openChannelCallback(Channel *channel, int error, void *userData) {
37                 LoggerD("Callback openChannelCallback.");
38                 if (userData) {
39                         SESession *seSessionPtr = (SESession *)((EventSEPtrs<EventSEOpenChannel> *)userData)->getThisPtr();
40                         if (seSessionPtr) {
41                                 EventSEOpenChannelPtr event = ((EventSEPtrs<EventSEOpenChannel> *)userData)->getEventPtrs();
42                                 seSessionPtr->openChannelManualAnswer((void *)channel, error, event);
43                         }
44                 } else {
45                         LoggerE("Callback private data is NULL.");
46                 }
47         }
48 }//private namespace
49
50 SESession::SESession(void *session)
51 {
52         LoggerD("entered");
53
54         if (session == NULL)
55                 ThrowMsg(Commons::UnknownException, "SecureElement Handler is Null Pointer.");
56
57         m_session = (Session *)session;
58 }
59
60 SESession::~SESession()
61 {
62         LoggerD("entered");
63         if (!isClosed())
64                 close();
65         m_session = NULL;
66 }
67
68 bool SESession::isClosed() {
69         LoggerD("Enter" << m_session->isClosed());
70         return m_session->isClosed();
71 }
72
73 std::vector<unsigned char> SESession::getATR() {
74         LoggerD("Enter");
75         ByteArray atrByteArray = m_session->getATRSync();
76         SEUtil util;
77         return util.toVector(atrByteArray.getBuffer(), atrByteArray.getLength());
78 }
79
80 void SESession::close() {
81         LoggerD("Enter");
82         m_session->closeSync();
83 }
84
85 void SESession::closeChannels() {
86         LoggerD("Enter");
87         m_session->closeChannels();
88 }
89
90 void SESession::openChannel(const EventSEOpenChannelPtr& event) {
91         LoggerD("Enter");
92         EventRequestReceiver<EventSEOpenChannel>::PostRequest(event);
93 }
94
95
96 void SESession::openChannelManualAnswer(void * channel, int error, const EventSEOpenChannelPtr &event) {
97         SEUtil util;
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");
106         } else {
107                 event->setChannel(channel);
108                 event->setResult(true);
109         }
110         EventRequestReceiver<EventSEOpenChannel>::ManualAnswer(event);
111
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() << ")");
117                         break;
118                 }
119         }
120 }
121
122 void SESession::OnRequestReceived(const EventSEOpenChannelPtr& event) {
123         LoggerD("Enter");
124         
125         try {
126                 event->switchToManualAnswer();
127
128                 SEOpenChannelDataPtr data( new EventSEPtrs<EventSEOpenChannel>(event, this));
129                 EventSEOpenChannels.push_back(data);
130
131                 std::vector<unsigned char> aid = event->getAID();
132                 SEUtil util;
133                 unsigned char *channelAid = util.toCharPtr(aid);
134                 if (event->isBasicChannel())
135                         m_session->openBasicChannel(channelAid, (unsigned int)aid.size(), openChannelCallback, data.Get());
136                 else
137                         m_session->openLogicalChannel(channelAid, (unsigned int)aid.size(), openChannelCallback, data.Get());
138                 if (channelAid)
139                         free(channelAid);
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);
146         }
147 }
148
149 }
150 }