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.
18 #include <Commons/Exception.h>
19 #include "SEService.h"
23 namespace SecureElement {
25 using namespace WrtDeviceApis;
30 class SEServiceEventHandler : public smartcard_service_api::SEServiceListener
32 void serviceConnected(smartcard_service_api::SEServiceHelper *service, void *context)
36 (static_cast<SEService*>(context))->getSEServiceCompleted();
39 void eventHandler(smartcard_service_api::SEServiceHelper *service, char *seName, int event, void *context)
42 (static_cast<SEService*>(context))->eventChanged(seName, event);
45 void errorHandler(smartcard_service_api::SEServiceHelper *service, int error, void *context)
50 static SEServiceEventHandler SEEventHandler;
53 SEService::SEService()
58 m_EventListSEsPtr.Reset();
59 m_seService = new smartcard_service_api::SEService((void *)this, &SEEventHandler);
63 SEService::~SEService()
68 m_seService->shutdownSync();
75 void SEService::getSEServiceCompleted() {
76 LoggerD("getSEServiceCompleted");
79 m_seService->shutdownSync();
82 while (eventListSEsPtrs.size() > 0) {
83 EventRequestReceiver<EventListSEs>::PostRequest(eventListSEsPtrs.front());
84 eventListSEsPtrs.pop();
88 void SEService::getReaders(const EventListSEsPtr& event) {
91 EventRequestReceiver<EventListSEs>::PostRequest(event);
93 eventListSEsPtrs.push(event);
97 void SEService::registerSEListener(const EventSEStateChangedEmitterPtr& emitter) {
98 m_EventSEStateChangedEmitterPtrs.push_back(emitter);
101 void SEService::unregisterSEListener(unsigned long id) {
102 std::vector<EventSEStateChangedEmitterPtr>::iterator it;
103 for (it = m_EventSEStateChangedEmitterPtrs.begin(); it != m_EventSEStateChangedEmitterPtrs.end(); ++it) {
104 if (id == static_cast<long>((*it)->getId())) {
105 m_EventSEStateChangedEmitterPtrs.erase(it);
106 LoggerD("m_EventSEStateChangedEmitterPtrs is removed. (" << m_EventSEStateChangedEmitterPtrs.size() << ")");
110 ThrowMsg(NotFoundException, "Not Found ID.");
113 void SEService::eventChanged(char *seName, int event) {
114 if (m_EventSEStateChangedEmitterPtrs.size() > 0) {
115 EventSEStateChangedPtr eventState( new EventSEStateChanged() );
118 eventState->setSEState(SE_INSERTED);
121 eventState->setSEState(SE_REMOVED);
124 LoggerD("Not correct event");
127 std::vector<smartcard_service_api::ReaderHelper *> readers = m_seService->getReaders();
129 for (i = readers.size(); i > 0; i--) {
130 if ((strlen(readers[i-1]->getName()) == strlen(seName))
131 && !strncmp(readers[i-1]->getName(), seName, strlen(seName))) {
132 if (((event == 1) && readers[i-1]->isSecureElementPresent())
133 ||((event == 2) && !readers[i-1]->isSecureElementPresent()))
138 LoggerE("Not found reader");
142 eventState->setReader((void *)(readers[i-1]));
144 for (i = 0; i < m_EventSEStateChangedEmitterPtrs.size(); i++)
145 m_EventSEStateChangedEmitterPtrs[i]->emit(eventState);
149 void SEService::OnRequestReceived(const EventListSEsPtr& event) {
150 std::vector<smartcard_service_api::ReaderHelper *> readers = m_seService->getReaders();
151 LoggerD("readers.size() : " << readers.size());
152 event->setResult(true);
153 for (std::size_t i = 0; i < readers.size(); i++) {
154 if (readers[i]->isSecureElementPresent())
155 event->addSEs((void *)readers[i]);
160 void SEService::shutdown() {
163 m_seService->shutdownSync();