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 <JSTizenException.h>
20 #include <ByteArray.h>
21 #include "SEChannel.h"
25 using namespace WrtDeviceApis;
26 using namespace smartcard_service_api;
29 namespace SecureElement {
31 static void transmitCallback(unsigned char *buffer, unsigned int length, int error, void *userParam) {
32 LoggerD("Callback transmitCallback.");
34 SEChannel *seChannelPtr = (SEChannel *)((EventSEPtrs<EventSEChannelTransmit> *)userParam)->getThisPtr();
37 EventSEChannelTransmitPtr event = ((EventSEPtrs<EventSEChannelTransmit> *)userParam)->getEventPtrs();
38 seChannelPtr->transmitManualAnswer(buffer, length, error, event);
41 LoggerE("Callback private data is NULL.");
46 SEChannel::SEChannel(void *channel)
51 ThrowMsg(UnknownException, "SEChannel Handler is Null Pointer.");
53 m_channel = (Channel *)channel;
56 SEChannel::~SEChannel()
59 if (!m_channel->isClosed())
65 void SEChannel::close() {
66 if (!m_channel->isClosed())
67 m_channel->closeSync();
70 bool SEChannel::isBasicChannel() {
71 return m_channel->isBasicChannel();
74 void SEChannel::transmit(const EventSEChannelTransmitPtr& event) {
76 EventRequestReceiver<EventSEChannelTransmit>::PostRequest(event);
79 void SEChannel::transmitManualAnswer(unsigned char *buffer, unsigned int length, int error, const EventSEChannelTransmitPtr &event) {
82 event->setResult(false);
83 event->setError(util.getErrorString(error));
84 event->setErrorMessage(util.getErrorMessage(error));
86 event->setResult(true);
87 event->setResponse(util.toVector(buffer, length));
89 EventRequestReceiver<EventSEChannelTransmit>::ManualAnswer(event);
91 std::vector<SEChannelTransmitDataPtr>::iterator it;
92 for (it = EventSEChannelTransmits.begin(); it != EventSEChannelTransmits.end(); ++it) {
93 if ((*it)->getEventPtrs() == event) {
94 EventSEChannelTransmits.erase(it);
95 LoggerD("event is removed. (" << EventSEChannelTransmits.size() << ")");
101 void SEChannel::OnRequestReceived(const EventSEChannelTransmitPtr& event) {
105 event->switchToManualAnswer();
107 SEChannelTransmitDataPtr eventData( new EventSEPtrs<EventSEChannelTransmit>(event, this));
108 EventSEChannelTransmits.push_back(eventData);
111 std::vector<unsigned char> data = event->getSendData();
112 unsigned char *chrTransmitData = util.toCharPtr(data);
113 ByteArray transmitData(chrTransmitData, data.size());
115 free(chrTransmitData);
116 m_channel->transmit(transmitData, transmitCallback, eventData.Get());
117 } catch (const WrtDeviceApis::Commons::Exception& ex) {
118 LoggerE("Exception: " << ex.GetMessage());
119 event->setResult(false);
120 event->setError(DeviceAPI::Common::JSTizenException::UNKNOWN_ERROR);
121 event->setErrorMessage("Unknown Error");
122 EventRequestReceiver<EventSEChannelTransmit>::ManualAnswer(event);