Tizen 2.1 base
[platform/framework/native/bluetooth.git] / src / FNetBtBluetoothManager.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 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 // @file    FNetBtBluetoothManager.cpp
18 // @brief   This is the implementation file for the BluetoothManager class.
19 //
20
21 #include <FNetBtBluetoothManager.h>
22 #include <FNetBtBluetoothDevice.h>
23 #include <FNetBtIBluetoothManagerEventListener.h>
24 #include <FNetBtIBluetoothDeviceEventListener.h>
25 #include <FBaseByteBuffer.h>
26 #include <FBaseColIList.h>
27 #include <FBaseSysLog.h>
28 #include <FSec_AccessController.h>
29 #include <FSys_SystemInfoImpl.h>
30 #include "FNetBt_BluetoothManagerImpl.h"
31
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Runtime;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Security;
36
37 namespace Tizen { namespace Net { namespace Bluetooth
38 {
39
40 BluetoothManager::BluetoothManager(void)
41         : __pImpl(null)
42 {
43 }
44
45 BluetoothManager::~BluetoothManager(void)
46 {
47         delete __pImpl;
48 }
49
50 result
51 BluetoothManager::Construct(IBluetoothManagerEventListener& listener)
52 {
53         result r = E_SUCCESS;
54         bool isBtSupported = false;
55
56         r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/network.bluetooth", isBtSupported);
57         SysTryReturnResult(NID_NET_BT, (r == E_SUCCESS) && (isBtSupported == true), E_UNSUPPORTED_OPERATION,
58                         "Bluetooth is not supported.");
59
60         SysAssertf(__pImpl == null,
61                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
62
63         __pImpl = new (std::nothrow) _BluetoothManagerImpl;
64
65         SysTryReturnResult(NID_NET_BT, __pImpl != null, E_OUT_OF_MEMORY, "Creation of a __pImpl instance failed.");
66
67         r = __pImpl->Construct(listener);
68
69         if (r != E_SUCCESS)
70         {
71                 delete __pImpl;
72                 __pImpl = null;
73         }
74
75         return r;
76 }
77
78 result
79 BluetoothManager::Activate(void)
80 {
81         result r = E_SUCCESS;
82
83         // Privilege check
84         r = _AccessController::CheckUserPrivilege(_PRV_BLUETOOTH_ADMIN);
85         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_PRIVILEGE_DENIED,
86                         "The application does not have the privilege to call this method.");
87
88         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
89
90         return __pImpl->Activate();
91 }
92
93 result
94 BluetoothManager::Deactivate(void)
95 {
96         result r = E_SUCCESS;
97
98         // Privilege check
99         r = _AccessController::CheckUserPrivilege(_PRV_BLUETOOTH_ADMIN);
100         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_PRIVILEGE_DENIED,
101                         "The application does not have the privilege to call this method.");
102
103         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
104
105         return __pImpl->Deactivate();
106 }
107
108 bool
109 BluetoothManager::IsAvailable(BluetoothConnectionType type) const
110 {
111         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
112
113         return __pImpl->IsAvailable(type);
114 }
115
116 const BluetoothDevice*
117 BluetoothManager::GetLocalDevice(void) const
118 {
119         ClearLastResult();
120         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
121
122         return __pImpl->GetLocalDevice();
123 }
124
125 Tizen::Base::String
126 BluetoothManager::GetLocalDeviceAddress(void) const
127 {
128         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
129
130         return __pImpl->GetLocalDeviceAddress();
131 }
132
133 Tizen::Base::String
134 BluetoothManager::GetLocalDeviceName(void) const
135 {
136         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
137
138         return __pImpl->GetLocalDeviceName();
139 }
140
141 BluetoothDeviceStateType
142 BluetoothManager::GetLocalDeviceState() const
143 {
144         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
145
146         return __pImpl->GetLocalDeviceState();
147 }
148
149 bool
150 BluetoothManager::IsActivated(void) const
151 {
152         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
153
154         return __pImpl->IsActivated();
155 }
156
157 BluetoothDiscoverableMode
158 BluetoothManager::GetDiscoverableMode(void) const
159 {
160         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
161
162         return __pImpl->GetDiscoverableMode();
163 }
164
165 int
166 BluetoothManager::GetRemainingTimeAsDiscoverable(void) const
167 {
168         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
169
170         return __pImpl->GetRemainingTimeAsDiscoverable();
171 }
172
173 bool
174 BluetoothManager::IsDiscoveryInProgress(void) const
175 {
176         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
177
178         return __pImpl->IsDiscoveryInProgress();
179 }
180
181 result
182 BluetoothManager::SetLocalDeviceName(const Tizen::Base::String& deviceName)
183 {
184         result r = E_SUCCESS;
185
186         // Privilege check
187         r = _AccessController::CheckUserPrivilege(_PRV_BLUETOOTH_ADMIN);
188         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_PRIVILEGE_DENIED,
189                         "The application does not have the privilege to call this method.");
190
191         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
192
193         return __pImpl->SetLocalDeviceName(deviceName);
194 }
195
196 result
197 BluetoothManager::SetDiscoverableMode(BluetoothDiscoverableMode mode, int seconds)
198 {
199         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
200
201         // Privilege check will be done by Impl class
202         return __pImpl->SetDiscoverableMode(mode, seconds);
203 }
204
205 result
206 BluetoothManager::RefreshPairedDeviceList(void)
207 {
208         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
209
210         return __pImpl->RefreshPairedDeviceList();
211 }
212
213 const BluetoothDevice*
214 BluetoothManager::GetPairedDeviceByAddress(const Tizen::Base::ByteBuffer& deviceAddress) const
215 {
216         ClearLastResult();
217
218         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
219
220         return __pImpl->GetPairedDeviceByAddress(deviceAddress);
221 }
222
223 BluetoothDevice*
224 BluetoothManager::GetPairedDeviceByAddressN(const Tizen::Base::ByteBuffer& deviceAddress) const
225 {
226         ClearLastResult();
227
228         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
229
230         return __pImpl->GetPairedDeviceByAddressN(deviceAddress);
231 }
232
233 Tizen::Base::Collection::IList*
234 BluetoothManager::GetPairedDeviceByNameN(const Base::String& deviceName) const
235 {
236         ClearLastResult();
237
238         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
239
240         return __pImpl->GetPairedDeviceByNameN(deviceName);
241 }
242
243 const BluetoothDevice*
244 BluetoothManager::GetPairedDeviceAt(int index) const
245 {
246         ClearLastResult();
247
248         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
249
250         return __pImpl->GetPairedDeviceAt(index);
251 }
252
253 const Tizen::Base::Collection::IList*
254 BluetoothManager::GetPairedDeviceList(void) const
255 {
256         ClearLastResult();
257
258         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
259
260         return __pImpl->GetPairedDeviceList();
261 }
262
263 Tizen::Base::Collection::IList*
264 BluetoothManager::GetPairedDeviceListN(void) const
265 {
266         ClearLastResult();
267
268         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
269
270         return __pImpl->GetPairedDeviceListN();
271 }
272
273 result
274 BluetoothManager::SetBluetoothDeviceListener(IBluetoothDeviceEventListener* pListener)
275 {
276         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
277
278         return __pImpl->SetBluetoothDeviceListener(pListener);
279 }
280
281 result
282 BluetoothManager::StartDiscovery(void)
283 {
284         result r = E_SUCCESS;
285
286         // Privilege check
287         r = _AccessController::CheckUserPrivilege(_PRV_BLUETOOTH_GAP);
288         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_PRIVILEGE_DENIED,
289                         "The application does not have the privilege to call this method.");
290
291         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
292
293         return __pImpl->StartDiscovery();
294 }
295
296 result
297 BluetoothManager::CancelDiscovery(void)
298 {
299         result r = E_SUCCESS;
300
301         // Privilege check
302         r = _AccessController::CheckUserPrivilege(_PRV_BLUETOOTH_GAP);
303         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_PRIVILEGE_DENIED,
304                         "The application does not have the privilege to call this method.");
305
306         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
307
308         return __pImpl->CancelDiscovery();
309 }
310
311 result
312 BluetoothManager::RetrieveServiceList(const BluetoothDevice& pairedDevice)
313 {
314         result r = E_SUCCESS;
315
316         // Privilege check
317         r = _AccessController::CheckUserPrivilege(_PRV_BLUETOOTH_GAP);
318         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_PRIVILEGE_DENIED,
319                         "The application does not have the privilege to call this method.");
320
321         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
322
323         return __pImpl->RetrieveServiceList(pairedDevice);
324 }
325
326 result
327 BluetoothManager::Pair(const BluetoothDevice& remoteDevice)
328 {
329         result r = E_SUCCESS;
330
331         // Privilege check
332         r = _AccessController::CheckUserPrivilege(_PRV_BLUETOOTH_GAP);
333         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_PRIVILEGE_DENIED,
334                         "The application does not have the privilege to call this method.");
335
336         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
337
338         return __pImpl->Pair(remoteDevice);
339 }
340
341 result
342 BluetoothManager::CancelPair(void)
343 {
344         result r = E_SUCCESS;
345
346         // Privilege check
347         r = _AccessController::CheckUserPrivilege(_PRV_BLUETOOTH_GAP);
348         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_PRIVILEGE_DENIED,
349                         "The application does not have the privilege to call this method.");
350
351         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
352
353         return __pImpl->CancelPair();
354 }
355
356 result
357 BluetoothManager::Unpair(const BluetoothDevice& pairedDevice)
358 {
359         result r = E_SUCCESS;
360
361         // Privilege check
362         r = _AccessController::CheckUserPrivilege(_PRV_BLUETOOTH_GAP);
363         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_PRIVILEGE_DENIED,
364                         "The application does not have the privilege to call this method.");
365
366         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
367
368         return __pImpl->Unpair(pairedDevice);
369 }
370
371 } } } // Tizen::Net::Bluetooth