2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 // @file FNetBt_BluetoothOppSystemAdapter.cpp
18 // @brief This is the implementation file for the _BluetoothOppSystemAdapter class.
21 #include <unique_ptr.h>
23 #include <FBaseByteBuffer.h>
24 #include <FBaseUtilStringUtil.h>
25 #include <FNetBtBluetoothTypes.h>
26 #include <FNetBtBluetoothDevice.h>
27 #include <FBaseSysLog.h>
28 #include <FBase_StringConverter.h>
29 #include "FNetBt_BluetoothOppSystemAdapter.h"
30 #include "FNetBt_BluetoothGapSystemAdapter.h"
31 #include "FNetBt_BluetoothDeviceImpl.h"
32 #include "FNetBt_BluetoothOppClientImpl.h"
33 #include "FNetBt_BluetoothOppServerImpl.h"
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Base::Utility;
40 namespace Tizen { namespace Net { namespace Bluetooth
43 _BluetoothOppSystemAdapter* _BluetoothOppSystemAdapter::__pInstance = null;
45 _BluetoothOppSystemAdapter::_BluetoothOppSystemAdapter(void)
46 : __pOppServerImpl(null)
47 , __pOppClientImpl(null)
48 , __serverTransferId(0)
49 , __serverRecvFileName()
50 , __serverRecvFileSize(0)
54 _BluetoothOppSystemAdapter::~_BluetoothOppSystemAdapter(void)
59 _BluetoothOppSystemAdapter::InitSingleton(void)
61 static _BluetoothOppSystemAdapter inst = _BluetoothOppSystemAdapter();
66 _BluetoothOppSystemAdapter*
67 _BluetoothOppSystemAdapter::GetInstance(void)
69 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
71 if (__pInstance == null)
74 pthread_once(&onceBlock, InitSingleton);
75 result r = GetLastResult();
78 onceBlock = PTHREAD_ONCE_INIT;
86 _BluetoothOppSystemAdapter::StartOppServer(const _BluetoothOppServerImpl& impl, const Tizen::Base::String& dstPath)
88 unique_ptr<char[]> pUuidStr;
90 unique_ptr<char[]> pConvertedDestPath;
92 int ret = BT_ERROR_NONE;
94 // check OPP server's availability
95 pUuidStr.reset(_StringConverter::CopyToCharArrayN(UuId(BT_SVC_UUID_OPP).ToString()));
96 ret = bt_adapter_is_service_used(pUuidStr.get() , &isUsed);
97 SysTryReturnResult(NID_NET_BT, (ret == BT_ERROR_NONE) && !isUsed, E_SERVICE_UNAVAILABLE, "OPP Server is not available. [0x%08X]", ret);
99 pConvertedDestPath.reset(_StringConverter::CopyToCharArrayN(dstPath));
100 SysTryReturnResult(NID_NET_BT, pConvertedDestPath != null, E_INACCESSIBLE_PATH, "The file path is invalid.");
102 ret = bt_opp_server_initialize_by_connection_request(pConvertedDestPath.get(), &OnOppServerConnectionRequested, null);
103 SysLog(NID_NET_BT, "Starting the OPP Server %s. [0x%-04X]", ret == BT_ERROR_NONE ? "is successful" : "fails", ret);
105 if (ret == BT_ERROR_NONE)
107 __pOppServerImpl = const_cast <_BluetoothOppServerImpl*>(&impl);
118 _BluetoothOppSystemAdapter::StopOppServer(void)
120 int ret = BT_ERROR_NONE;
122 ret = bt_opp_server_deinitialize();
123 SysLog(NID_NET_BT, "Stop the OPP Server %s. [0x%08X]", ret == BT_ERROR_NONE ? "is successful" : "fails", ret);
125 if (ret == BT_ERROR_NONE)
127 __pOppServerImpl = null;
135 _BluetoothOppSystemAdapter::SetOppReceivePath(const Tizen::Base::String& dstPath)
137 char* pConvertedDestPath = NULL;
138 result r = E_FAILURE;
139 int ret = BT_ERROR_NONE;
141 pConvertedDestPath = _StringConverter::CopyToCharArrayN(dstPath);
142 SysTryReturnResult(NID_NET_BT, pConvertedDestPath != null, E_INACCESSIBLE_PATH, "The file path is invalid.");
144 ret = bt_opp_server_set_destination(pConvertedDestPath);
145 SysLog(NID_NET_BT, "Setting the receive path of the OPP Server %s. [0x%08X]",
146 ret == BT_ERROR_NONE ? "is successful" : "fails", ret);
148 if (ret == BT_ERROR_NONE)
153 delete[] pConvertedDestPath;
158 _BluetoothOppSystemAdapter::AcceptOppPush(const Tizen::Base::String& fileName)
160 result r = E_FAILURE;
161 int ret = BT_ERROR_NONE;
163 char* pConvertedfileName = _StringConverter::CopyToCharArrayN(fileName);
164 SysTryReturnResult(NID_NET_BT, pConvertedfileName != null, E_FAILURE, "The file name is invalid.");
166 ret = bt_opp_server_accept(&OnOppServerTransferInProgress, &OnOppServerTransferCompleted, pConvertedfileName, null,
167 &__serverTransferId);
168 SysLog(NID_NET_BT, "Accepting the OPP push request %s. [0x%08X]", ret == BT_ERROR_NONE ? "is successful" : "fails", ret);
170 if (ret == BT_ERROR_NONE)
175 delete[] pConvertedfileName;
180 _BluetoothOppSystemAdapter::RejectOppPush(void)
182 int ret = BT_ERROR_NONE;
184 ret = bt_opp_server_reject();
185 SysLog(NID_NET_BT, "Rejecting the OPP push request %s. [0x%08X]", ret == BT_ERROR_NONE ? "is successful" : "fails", ret);
187 if (ret == BT_ERROR_NONE)
196 _BluetoothOppSystemAdapter::StopOppTransfer(void)
198 int ret = BT_ERROR_NONE;
200 // if a file transfer is not started yet, the default transfer ID (0) is used for terminating the OPP server.
201 ret = bt_opp_server_cancel_transfer(__serverTransferId);
202 SysLog(NID_NET_BT, "Stop the OPP file transfer (ID:%d) %s. [0x%08X]", __serverTransferId,
203 ret == BT_ERROR_NONE ? "is successful" : "fails", ret);
205 if (ret == BT_ERROR_NONE)
210 return E_OPERATION_FAILED;
214 _BluetoothOppSystemAdapter::PushOppFile(const _BluetoothOppClientImpl& impl, const Tizen::Base::ByteBuffer& serverAddress,
215 const Tizen::Base::String& filePath)
217 result r = E_FAILURE;
218 int ret = BT_ERROR_NONE;
219 unique_ptr<char[]> pServerDevAddr;
220 unique_ptr<char[]> pFileName;
222 SysTryReturnResult(NID_NET_BT, __pOppClientImpl == null, E_SERVICE_UNAVAILABLE, "The already OPP client is running.");
223 SysTryReturnResult(NID_NET_BT, serverAddress.GetRemaining() == _BT_ADDRESS_LENGTH, E_FAILURE,
224 "The address of the remote device is incorrect.");
226 pFileName.reset(_StringConverter::CopyToCharArrayN(filePath));
227 SysTryReturnResult(NID_NET_BT, pFileName != null, E_INACCESSIBLE_PATH, "The file path is invalid.");
228 pServerDevAddr.reset(_StringConverter::CopyToCharArrayN(_BluetoothDeviceImpl::GetAddressString(serverAddress)));
229 SysTryReturnResult(NID_NET_BT, pServerDevAddr != null, E_FAILURE, "The server address is invalid.");
231 if (bt_opp_client_initialize() == BT_ERROR_NONE)
233 (void) bt_opp_client_add_file(pFileName.get());
234 ret = bt_opp_client_push_files(pServerDevAddr.get(), &OnOppClientConnectionResponded, &OnOppClientTransferInProgress,
235 &OnOppClientTransferCompleted, null);
236 SysLog(NID_NET_BT, "Sending a push request to device [%s] %s. [0x%08X]", pServerDevAddr.get(),
237 ret == BT_ERROR_NONE ? "is successful" : "fails", ret);
239 if (ret == BT_ERROR_NONE)
241 __pOppClientImpl = const_cast<_BluetoothOppClientImpl*>(&impl);
246 bt_opp_client_deinitialize();
254 _BluetoothOppSystemAdapter::CancelOppPush(void)
256 int ret = BT_ERROR_NONE;
258 ret = bt_opp_client_cancel_push();
259 SysLog(NID_NET_BT, "Cancel the OPP file transfer %s. [0x%08X]", ret == BT_ERROR_NONE ? "is successful" : "fails", ret);
261 if (ret == BT_ERROR_NONE)
270 _BluetoothOppSystemAdapter::OnOppClientConnectionResponded(int status, const char* pRemoteAddress, void* pUserData)
272 _BluetoothOppSystemAdapter* pOppAdapter = _BluetoothOppSystemAdapter::GetInstance();
274 if (pOppAdapter->__pOppClientImpl)
276 if (status == BT_ERROR_NONE)
278 pOppAdapter->__pOppClientImpl->OnOppPushResponded(E_SUCCESS);
282 pOppAdapter->__pOppClientImpl->OnOppPushResponded(E_SYSTEM);
283 pOppAdapter->__pOppClientImpl = null;
284 bt_opp_client_deinitialize();
290 _BluetoothOppSystemAdapter::OnOppClientTransferInProgress(const char* pFilePath, long long fileSize, int progress, void* pUserData)
292 _BluetoothOppSystemAdapter* pOppAdapter = _BluetoothOppSystemAdapter::GetInstance();
294 pOppAdapter->__serverRecvFileSize = fileSize;
295 // converts the UTF8 multibyte string to Unicode String
296 (void) StringUtil::Utf8ToString(pFilePath, pOppAdapter->__serverRecvFileName);
298 if (pOppAdapter->__pOppClientImpl)
300 pOppAdapter->__pOppClientImpl->OnOppTransferInProgress(progress);
305 _BluetoothOppSystemAdapter::OnOppClientTransferCompleted(int status, const char* pRemoteAddress, void* pUserData)
307 bool isCompleted = true;
308 _BluetoothOppSystemAdapter* pOppAdapter = _BluetoothOppSystemAdapter::GetInstance();
310 if (pOppAdapter->__pOppClientImpl)
312 if (status != BT_ERROR_NONE)
317 pOppAdapter->__pOppClientImpl->OnOppTransferDone(pOppAdapter->__serverRecvFileName, pOppAdapter->__serverRecvFileSize,
319 pOppAdapter->__pOppClientImpl = null;
320 // TODO: Is it right that the following statement is located in the next of callback?
321 bt_opp_client_deinitialize();
326 _BluetoothOppSystemAdapter::OnOppServerConnectionRequested(const char* pRemoteAddress, void* pUserData)
328 result r = E_SUCCESS;
329 BluetoothDevice requester;
330 ByteBuffer deviceAddress;
331 String deviceAddressStr(pRemoteAddress);
332 _BluetoothOppSystemAdapter* pOppAdapter = _BluetoothOppSystemAdapter::GetInstance();
334 if (pOppAdapter->__pOppServerImpl)
336 deviceAddress.Construct(_BT_ADDRESS_LENGTH);
337 (void) _BluetoothDeviceImpl::GetAddressByteBuffer(deviceAddressStr, L":", deviceAddress);
339 r = _BluetoothGapSystemAdapter::GetPairedDevice(deviceAddress, requester);
340 SysTryReturnVoidResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] Getting information of the request has failed.");
342 pOppAdapter->__pOppServerImpl->OnOppPushRequested(requester);
347 _BluetoothOppSystemAdapter::OnOppServerTransferInProgress(const char* pFilePath, long long fileSize, int progress, void* pUserData)
349 _BluetoothOppSystemAdapter* pOppAdapter = _BluetoothOppSystemAdapter::GetInstance();
351 if (pOppAdapter->__pOppServerImpl)
353 pOppAdapter->__pOppServerImpl->OnOppTransferInProgress(progress);
358 _BluetoothOppSystemAdapter::OnOppServerTransferCompleted(int status, const char* pFilePath, long long fileSize, void* pUserData)
360 bool isCompleted = true;
361 String convertedFilePath;
363 _BluetoothOppSystemAdapter* pOppAdapter = _BluetoothOppSystemAdapter::GetInstance();
365 if (pOppAdapter->__pOppServerImpl)
367 pOppAdapter->__serverTransferId = 0;
369 if (status != BT_ERROR_NONE)
373 // TODO: not file name but file path
374 // converts the UTF8 multibyte string to Unicode String
375 (void) StringUtil::Utf8ToString(pFilePath, convertedFilePath);
376 pOppAdapter->__pOppServerImpl->OnOppTransferDone(convertedFilePath, fileSize, isCompleted);
380 } } } // Tizen::Net::Bluetooth