a59c75df5af9e7eb87e4b440461ab5e21e5dc0a1
[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         if (error != 0) {
86                 event->setResult(false);
87                 if (error == -4)
88                         event->setError(DeviceAPI::Common::JSTizenException::INVALID_VALUES_ERROR);
89                 else
90                         event->setError(DeviceAPI::Common::JSTizenException::UNKNOWN_ERROR);
91         } else {
92                 event->setResult(true);
93                 SEUtil util;
94                 event->setResponse(util.toVector(buffer, length));
95         }
96         EventRequestReceiver<EventSEChannelTransmit>::ManualAnswer(event);
97
98         std::vector<SEChannelTransmitDataPtr>::iterator it;
99         for (it = EventSEChannelTransmits.begin(); it != EventSEChannelTransmits.end(); ++it) {
100                 if ((*it)->getEventPtrs() == event) {
101                         EventSEChannelTransmits.erase(it);
102                         LogDebug("event is removed. (" << EventSEChannelTransmits.size() << ")");
103                         break;
104                 }
105         }
106 }
107
108 void SEChannel::OnRequestReceived(const EventSEChannelTransmitPtr& event) {
109         LogDebug("Enter");
110         
111         try {
112                 event->switchToManualAnswer();
113
114                 SEChannelTransmitDataPtr eventData( new EventSEPtrs<EventSEChannelTransmit>(event, this));
115                 EventSEChannelTransmits.push_back(eventData);
116
117                 SEUtil util;
118                 std::vector<unsigned char> data = event->getSendData();
119                 unsigned char *chrTransmitData = util.toCharPtr(data);
120                 ByteArray transmitData(chrTransmitData, data.size());
121                 if (chrTransmitData)
122                         free(chrTransmitData);
123                 m_channel->transmit(transmitData, transmitCallback, eventData.Get());
124         } catch (const WrtDeviceApis::Commons::Exception& ex) {
125                 LogError("Exception: " << ex.GetMessage());
126                 event->setResult(false);
127         
128                 EventRequestReceiver<EventSEChannelTransmit>::ManualAnswer(event);
129         }       
130 }
131
132 }
133 }