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.
20 #include <dpl/log/log.h>
22 #include <Commons/Exception.h>
23 #include <JSTizenException.h>
28 using namespace WrtDeviceApis;
29 using namespace smartcard_service_api;
32 namespace SecureElement {
35 static void openSessionCallback(SessionHelper *session, int error, void *userData) {
36 LogDebug("Callback openSessionCallback.");
38 SEReader *sePtr = (SEReader *)((EventSEPtrs<EventSEOpenSession> *)userData)->getThisPtr();
40 EventSEOpenSessionPtr event = ((EventSEPtrs<EventSEOpenSession> *)userData)->getEventPtrs();
41 sePtr->openSessionManualAnswer((void *)session, error, event);
44 LogError("Callback private data is NULL.");
50 SEReader::SEReader(void *reader)
55 ThrowMsg(Commons::UnknownException, "SecureElement Handler is Null Pointer.");
57 m_reader = (Reader *)reader;
63 m_reader->closeSessions();
67 std::string SEReader::getName() {
68 LogDebug("Enter" << m_reader->getName());
69 return m_reader->getName();
72 bool SEReader::isPresent() {
73 LogDebug("Enter" << m_reader->isSecureElementPresent());
74 return m_reader->isSecureElementPresent();
77 void SEReader::openSession(const EventSEOpenSessionPtr& event) {
79 EventRequestReceiver<EventSEOpenSession>::PostRequest(event);
82 void SEReader::closeSessions() {
84 m_reader->closeSessions();
87 void SEReader::openSessionManualAnswer(void *session, int error, const EventSEOpenSessionPtr &event) {
89 if (error != SCARD_ERROR_OK) {
90 event->setResult(false);
91 event->setError(util.getErrorString(error));
92 event->setErrorMessage(util.getErrorMessage(error));
93 } else if (session == NULL) {
94 event->setResult(false);
95 event->setError(DeviceAPI::Common::JSTizenException::UNKNOWN_ERROR);
96 event->setErrorMessage("Unknown Error");
98 event->setSession(session);
99 event->setResult(true);
101 EventRequestReceiver<EventSEOpenSession>::ManualAnswer(event);
103 std::vector<SEOpenSessionDataPtr>::iterator it;
104 for (it = EventSEOpenSessions.begin(); it != EventSEOpenSessions.end(); ++it) {
105 if ((*it)->getEventPtrs() == event) {
106 EventSEOpenSessions.erase(it);
107 LogDebug("event is removed. (" << EventSEOpenSessions.size() << ")");
113 void SEReader::OnRequestReceived(const EventSEOpenSessionPtr& event) {
117 event->switchToManualAnswer();
119 SEOpenSessionDataPtr data( new EventSEPtrs<EventSEOpenSession>(event, this));
120 EventSEOpenSessions.push_back(data);
122 m_reader->openSession(openSessionCallback, data.Get());
123 } catch (const WrtDeviceApis::Commons::Exception& ex) {
124 LogError("Exception: " << ex.GetMessage());
125 event->setResult(false);
126 event->setError(DeviceAPI::Common::JSTizenException::UNKNOWN_ERROR);
127 event->setErrorMessage("Unknown Error");
128 EventRequestReceiver<EventSEOpenSession>::ManualAnswer(event);