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