Update change log and spec for wrt-plugins-tizen_0.4.70
[framework/web/wrt-plugins-tizen.git] / src / Bluetooth / BluetoothHealthChannel.cpp
1 // Tizen Web Device API
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <GlobalContextManager.h>
18 #include <PlatformException.h>
19
20 #include "BluetoothHealthChannel.h"
21 #include "JSBluetoothDevice.h"
22 #include "JSBluetoothHealthApplication.h"
23
24 #include <Logger.h>
25 #include <TimeTracer.h>
26
27 using namespace DeviceAPI::Common;
28
29 namespace DeviceAPI {
30 namespace Bluetooth {
31
32 BluetoothHealthChannel::BluetoothHealthChannel(unsigned int channel, BluetoothDeviceSharedPtr remoteDevice, 
33         bt_hdp_channel_type_e type, BluetoothHealthApplicationSharedPtr application)
34 {
35         Common::SecurityAccessor();
36     mChannel = channel;
37     mRemoteDevice = remoteDevice;
38     if(type == BT_HDP_CHANNEL_TYPE_RELIABLE) {
39         mChannelTypeStr = "RELIABLE";
40     }
41     else {
42         mChannelTypeStr = "STREAMING";
43     }
44     mChannelType = type;
45     mApp = application;
46     mIsConnected = true;
47 }
48
49 BluetoothHealthChannel::~BluetoothHealthChannel()
50 {
51     if(mIsConnected) {
52         bt_hdp_disconnect(mRemoteDevice->getAddress().c_str(), mChannel);
53     }
54 }
55
56 bool BluetoothHealthChannel::getConnectionState() const
57 {
58     return mIsConnected;
59 }
60
61 void BluetoothHealthChannel::setConnectionState(bool isConnected)
62 {
63     mIsConnected = isConnected;
64 }
65
66 unsigned int BluetoothHealthChannel::getChannel() const
67 {
68     return mChannel;
69 }
70
71 std::string BluetoothHealthChannel::getChannelTypeStr() const
72 {
73     return mChannelTypeStr;
74 }
75
76 bt_hdp_channel_type_e BluetoothHealthChannel::getChannelType() const
77 {
78     return mChannelType;
79 }
80
81 JSValueRef BluetoothHealthChannel::getApp(JSContextRef context)
82 {
83     return JSBluetoothHealthApplication::createJSObject(context, mApp);
84 }
85
86 JSValueRef BluetoothHealthChannel::getPeer(JSContextRef context)
87 {
88     return JSBluetoothDevice::createJSObject(context, mRemoteDevice);
89 }
90
91 Common::MultiCallbackUserDataPtr BluetoothHealthChannel::getListener() const
92 {
93     return mListener;
94 }
95
96 unsigned long BluetoothHealthChannel::sendData(char* data, unsigned long size)
97 {
98     unsigned long ret = 0;
99     TIME_TRACER_ITEM_BEGIN("sendData::bt_hdp_send_data", 1);
100     if(bt_hdp_send_data(mChannel, data, static_cast<unsigned int>(size)) == BT_ERROR_NONE) {
101         TIME_TRACER_ITEM_END("sendData::bt_hdp_send_data", 1);
102         LoggerD("bt_hdp_send_data() succeeded");
103         ret = size;
104     }
105     else {
106         throw UnknownException("Unknown error");
107     }
108
109     //delete data;
110     return ret;
111 }
112
113 void BluetoothHealthChannel::close()
114 {
115     LoggerD("Enter");
116     if(!mIsConnected) {
117         LoggerD("Already disconnected");
118         return;
119     }
120
121     TIME_TRACER_ITEM_BEGIN("close::bt_hdp_disconnect", 1);
122     if(bt_hdp_disconnect(mRemoteDevice->getAddress().c_str(), mChannel) != BT_ERROR_NONE) {
123         LoggerE("bt_hdp_disconnect() failed");
124         throw UnknownException("Unknown error");
125     }
126     TIME_TRACER_ITEM_END("close::bt_hdp_disconnect", 1);
127
128     mIsConnected = false;
129
130     LoggerD("End");
131 }
132
133 void BluetoothHealthChannel::setListener(Common::MultiCallbackUserDataPtr callback)
134 {
135     mListener = callback;
136 }
137
138 void BluetoothHealthChannel::unsetListener()
139 {
140     mListener.reset();
141 }
142
143
144 } // Bluetooth
145 } // DeviceAPI