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.
21 #include <dpl/log/log.h>
23 #include <Commons/Exception.h>
24 #include <JSTizenException.h>
25 #include <ByteArray.h>
26 #include "SEChannel.h"
30 using namespace WrtDeviceApis;
31 using namespace smartcard_service_api;
34 namespace SecureElement {
36 static void transmitCallback(unsigned char *buffer, unsigned int length, int error, void *userParam) {
37 LogDebug("Callback transmitCallback.");
39 SEChannel *seChannelPtr = (SEChannel *)((EventSEPtrs<EventSEChannelTransmit> *)userParam)->getThisPtr();
42 EventSEChannelTransmitPtr event = ((EventSEPtrs<EventSEChannelTransmit> *)userParam)->getEventPtrs();
43 seChannelPtr->transmitManualAnswer(buffer, length, error, event);
46 LogError("Callback private data is NULL.");
51 SEChannel::SEChannel(void *channel)
56 ThrowMsg(UnknownException, "SEChannel Handler is Null Pointer.");
58 m_channel = (Channel *)channel;
61 SEChannel::~SEChannel()
64 if (!m_channel->isClosed())
70 void SEChannel::close() {
71 if (!m_channel->isClosed())
72 m_channel->closeSync();
75 bool SEChannel::isBasicChannel() {
76 return m_channel->isBasicChannel();
79 void SEChannel::transmit(const EventSEChannelTransmitPtr& event) {
81 EventRequestReceiver<EventSEChannelTransmit>::PostRequest(event);
84 void SEChannel::transmitManualAnswer(unsigned char *buffer, unsigned int length, int error, const EventSEChannelTransmitPtr &event) {
87 event->setResult(false);
88 event->setError(util.getErrorString(error));
89 event->setErrorMessage(util.getErrorMessage(error));
91 event->setResult(true);
92 event->setResponse(util.toVector(buffer, length));
94 EventRequestReceiver<EventSEChannelTransmit>::ManualAnswer(event);
96 std::vector<SEChannelTransmitDataPtr>::iterator it;
97 for (it = EventSEChannelTransmits.begin(); it != EventSEChannelTransmits.end(); ++it) {
98 if ((*it)->getEventPtrs() == event) {
99 EventSEChannelTransmits.erase(it);
100 LogDebug("event is removed. (" << EventSEChannelTransmits.size() << ")");
106 void SEChannel::OnRequestReceived(const EventSEChannelTransmitPtr& event) {
110 event->switchToManualAnswer();
112 SEChannelTransmitDataPtr eventData( new EventSEPtrs<EventSEChannelTransmit>(event, this));
113 EventSEChannelTransmits.push_back(eventData);
116 std::vector<unsigned char> data = event->getSendData();
117 unsigned char *chrTransmitData = util.toCharPtr(data);
118 ByteArray transmitData(chrTransmitData, data.size());
120 free(chrTransmitData);
121 m_channel->transmit(transmitData, transmitCallback, eventData.Get());
122 } catch (const WrtDeviceApis::Commons::Exception& ex) {
123 LogError("Exception: " << ex.GetMessage());
124 event->setResult(false);
125 event->setError(DeviceAPI::Common::JSTizenException::UNKNOWN_ERROR);
126 event->setErrorMessage("Unknown Error");
127 EventRequestReceiver<EventSEChannelTransmit>::ManualAnswer(event);