Tizen 2.0 Release
[framework/osp/bluetooth.git] / src / FNetBtBluetoothSppAcceptor.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    FNetBtBluetoothSppAcceptor.cpp
18 // @brief   This is the implementation file for the BluetoothSppAcceptor class.
19 //
20
21 #include <FNetBtBluetoothSppAcceptor.h>
22 #include <FNetBtIBluetoothSppAcceptorEventListener.h>
23 #include <FBaseByteBuffer.h>
24 #include <FBaseUuId.h>
25 #include <FBaseSysLog.h>
26 #include <FSec_AccessController.h>
27 #include <FSys_SystemInfoImpl.h>
28 #include "FNetBt_BluetoothSppAcceptorImpl.h"
29
30 using namespace Tizen::Security;
31
32 namespace Tizen { namespace Net { namespace Bluetooth
33 {
34
35 BluetoothSppAcceptor::BluetoothSppAcceptor(void)
36         : __pImpl(null)
37 {
38 }
39
40 BluetoothSppAcceptor::~BluetoothSppAcceptor(void)
41 {
42         delete __pImpl;
43 }
44
45 result
46 BluetoothSppAcceptor::Construct(IBluetoothSppAcceptorEventListener& listener)
47 {
48         result r = E_SUCCESS;
49         bool isBtSupported = false;
50
51         Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/network.bluetooth", isBtSupported);
52         SysTryReturnResult(NID_NET_BT, isBtSupported == true, E_UNSUPPORTED_OPERATION, "Bluetooth is not supported.");
53
54         SysAssertf(__pImpl == null,
55                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
56
57         __pImpl = new (std::nothrow) _BluetoothSppAcceptorImpl;
58         SysTryReturnResult(NID_NET_BT, __pImpl != null, E_OUT_OF_MEMORY, "Creation of a _BluetoothSppAcceptorImpl instance failed.");
59
60         r = __pImpl->Construct(listener);
61
62         if (r != E_SUCCESS)
63         {
64                 delete __pImpl;
65                 __pImpl = null;
66         }
67
68         return r;
69 }
70
71 result
72 BluetoothSppAcceptor::AcceptConnection(void)
73 {
74         result r = E_SUCCESS;
75
76         // Privilege check
77         r = _AccessController::CheckUserPrivilege(_PRV_BLUETOOTH_SPP);
78         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
79
80         // Check Construct
81         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
82
83         return __pImpl->AcceptConnection();
84 }
85
86 result
87 BluetoothSppAcceptor::RejectConnection()
88 {
89         result r = E_SUCCESS;
90
91         // Privilege check
92         r = _AccessController::CheckUserPrivilege(_PRV_BLUETOOTH_SPP);
93         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
94
95         // Check Construct
96         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
97
98         return __pImpl->RejectConnection();
99 }
100
101 result
102 BluetoothSppAcceptor::StartService(void)
103 {
104         // Actually, E_INVALID_ARG never occur because the fixed SPP UUID is used.
105         return StartService(Tizen::Base::UuId(BT_SVC_UUID_SPP));
106 }
107
108 result
109 BluetoothSppAcceptor::StartService(const Tizen::Base::UuId& serviceUuid)
110 {
111         result r = E_SUCCESS;
112
113         // Privilege check
114         r = _AccessController::CheckUserPrivilege(_PRV_BLUETOOTH_SPP);
115         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
116
117         // Check Construct
118         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
119
120         return __pImpl->StartService(serviceUuid);
121 }
122
123 result
124 BluetoothSppAcceptor::StopService(void)
125 {
126         result r = E_SUCCESS;
127
128         // Privilege check
129         r = _AccessController::CheckUserPrivilege(_PRV_BLUETOOTH_SPP);
130         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
131
132         // Check Construct
133         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
134
135         return __pImpl->StopService();
136 }
137
138 result
139 BluetoothSppAcceptor::SendData(const Tizen::Base::ByteBuffer& buffer)
140 {
141         result r = E_SUCCESS;
142
143         // Privilege check
144         r = _AccessController::CheckUserPrivilege(_PRV_BLUETOOTH_SPP);
145         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
146
147         // Check Construct
148         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
149
150         return __pImpl->SendData(buffer);
151 }
152
153 result
154 BluetoothSppAcceptor::Disconnect(void)
155 {
156         result r = E_SUCCESS;
157
158         // Privilege check
159         r = _AccessController::CheckUserPrivilege(_PRV_BLUETOOTH_SPP);
160         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
161
162         // Check Construct
163         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
164
165         return __pImpl->Disconnect();
166 }
167
168 } } } // Tizen::Net::Bluetooth