wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / SecureElement / SEChannel.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 #include <Commons/Exception.h>
19 #include <JSTizenException.h>
20 #include <ByteArray.h>
21 #include "SEChannel.h"
22 #include "SEUtil.h"
23 #include <Logger.h>
24
25 using namespace WrtDeviceApis;
26 using namespace smartcard_service_api;
27
28 namespace DeviceAPI {
29 namespace SecureElement {
30 namespace {
31         static void transmitCallback(unsigned char *buffer, unsigned int length, int error, void *userParam) {
32                 LoggerD("Callback transmitCallback.");
33                 if (userParam) {
34                         SEChannel *seChannelPtr = (SEChannel *)((EventSEPtrs<EventSEChannelTransmit> *)userParam)->getThisPtr();
35
36                         if (seChannelPtr) {
37                                 EventSEChannelTransmitPtr event = ((EventSEPtrs<EventSEChannelTransmit> *)userParam)->getEventPtrs();
38                                 seChannelPtr->transmitManualAnswer(buffer, length, error, event);
39                         }
40                 } else {
41                         LoggerE("Callback private data is NULL.");
42                 }
43         }
44 }
45
46 SEChannel::SEChannel(void *channel)
47 {
48         LoggerD("entered");
49
50         if (channel == NULL)
51                 ThrowMsg(UnknownException, "SEChannel Handler is Null Pointer.");
52
53         m_channel = (Channel *)channel;
54 }
55
56 SEChannel::~SEChannel()
57 {
58         LoggerD("entered");
59         if (!m_channel->isClosed())
60                 close();
61         m_channel = NULL;
62 }
63
64
65 void SEChannel::close() {
66         if (!m_channel->isClosed())
67                 m_channel->closeSync();
68 }
69
70 bool SEChannel::isBasicChannel() {
71         return m_channel->isBasicChannel();
72 }
73
74 void SEChannel::transmit(const EventSEChannelTransmitPtr& event) {
75         LoggerD("Enter");
76         EventRequestReceiver<EventSEChannelTransmit>::PostRequest(event);       
77 }
78
79 void SEChannel::transmitManualAnswer(unsigned char *buffer, unsigned int length, int error, const EventSEChannelTransmitPtr &event) {
80         SEUtil util;
81         if (error != 0) {
82                 event->setResult(false);
83                 event->setError(util.getErrorString(error));
84                 event->setErrorMessage(util.getErrorMessage(error));
85         } else {
86                 event->setResult(true);
87                 event->setResponse(util.toVector(buffer, length));
88         }
89         EventRequestReceiver<EventSEChannelTransmit>::ManualAnswer(event);
90
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() << ")");
96                         break;
97                 }
98         }
99 }
100
101 void SEChannel::OnRequestReceived(const EventSEChannelTransmitPtr& event) {
102         LoggerD("Enter");
103         
104         try {
105                 event->switchToManualAnswer();
106
107                 SEChannelTransmitDataPtr eventData( new EventSEPtrs<EventSEChannelTransmit>(event, this));
108                 EventSEChannelTransmits.push_back(eventData);
109
110                 SEUtil util;
111                 std::vector<unsigned char> data = event->getSendData();
112                 unsigned char *chrTransmitData = util.toCharPtr(data);
113                 ByteArray transmitData(chrTransmitData, data.size());
114                 if (chrTransmitData)
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);
123         }       
124 }
125
126 }
127 }