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_BluetoothOppServerImpl.cpp
18 // @brief This is the implementation file for the _BluetoothOppServerImpl class.
21 #include <FNetBtBluetoothTypes.h>
22 #include <FNetBtBluetoothDevice.h>
23 #include <FNetBtIBluetoothOppServerEventListener.h>
24 #include <FBaseSysLog.h>
25 #include <FIo_FileImpl.h>
26 #include <FApp_AppInfo.h>
27 #include "FNetBt_BluetoothOppServerImpl.h"
28 #include "FNetBt_BluetoothOppSystemAdapter.h"
29 #include "FNetBt_BluetoothGapSystemAdapter.h"
30 #include "FNetBt_BluetoothOppServerEvent.h"
31 #include "FNetBt_BluetoothOppServerEventArg.h"
34 using namespace Tizen::App;
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Runtime;
38 namespace Tizen { namespace Net { namespace Bluetooth
41 _BluetoothOppServerImpl::_BluetoothOppServerImpl(void)
46 , __currentState(_BT_OPP_SVR_STATE_DISABLED)
48 , __minProgressInterval(__defaultProgressInterval)
49 , __previousProgress(0)
53 _BluetoothOppServerImpl::~_BluetoothOppServerImpl(void)
55 if (__pOppAdapter != null)
57 if ((__currentState == _BT_OPP_SVR_STATE_ON_SERVICE)
58 || (__currentState == _BT_OPP_SVR_STATE_PUSH_REQUESTED)
59 || (__currentState == _BT_OPP_SVR_STATE_ON_TRANSFER))
61 (void) StopService(); // Ignores the result of this function.
66 if (__pGapAdapter != null)
68 __pGapAdapter->UnregisterManagerEventListener(*this);
74 _BluetoothOppServerImpl::Construct(IBluetoothOppServerEventListener& listener)
77 unique_ptr<_BluetoothOppServerEvent> pEvent;
79 r = __stateMutex.Create();
80 SysTryReturn(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to create the state mutex.");
82 __pGapAdapter = _BluetoothGapSystemAdapter::GetInstance();
83 SysTryReturn(NID_NET_BT, __pGapAdapter != null, E_SYSTEM, E_SYSTEM,
84 "[E_SYSTEM] Failed to invoke _BluetoothSysteAdapter::GetInstance().");
86 __pOppAdapter = _BluetoothOppSystemAdapter::GetInstance();
88 pEvent.reset(new (std::nothrow) _BluetoothOppServerEvent());
89 SysTryReturn(NID_NET_BT, pEvent != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
90 r = pEvent->Construct();
91 SysTryReturn(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to construct the event.");
92 // add the IBluetoothOppServerEventListener instance to a new created _BluetoothOppServerEvent.
93 r = pEvent->AddListener(listener, true);
94 SysTryReturn(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
95 "[%s] Propagating. Failed to add the application listener for OPP Server", GetErrorMessage(r));
97 // registers this callback listener to the system adapter for activation/deactivation event
98 r = __pGapAdapter->RegisterManagerEventListener(*this, true);
99 SysTryReturn(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
100 "[E_SYSTEM] Failed to register the callback listener to _BluetoothSystemAdapter.");
102 // check whether the Bluetooth is available
103 __stateMutex.Acquire();
105 if (__pGapAdapter->IsActivated() == true)
107 __currentState = _BT_OPP_SVR_STATE_IDLE;
110 __stateMutex.Release();
112 __pEvent = move(pEvent);
118 _BluetoothOppServerImpl::AcceptPush(void)
120 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
122 result r = E_FAILURE;
124 __stateMutex.Acquire();
126 switch (__currentState)
128 case _BT_OPP_SVR_STATE_DISABLED:
132 case _BT_OPP_SVR_STATE_IDLE:
133 case _BT_OPP_SVR_STATE_ON_SERVICE:
134 case _BT_OPP_SVR_STATE_ON_TRANSFER:
135 case _BT_OPP_SVR_STATE_ON_STOPPING:
136 r = E_INVALID_OPERATION;
139 case _BT_OPP_SVR_STATE_PUSH_REQUESTED:
140 r = __pOppAdapter->AcceptOppPush(__pushedFileName);
144 __currentState = _BT_OPP_SVR_STATE_ON_TRANSFER;
148 __currentState = _BT_OPP_SVR_STATE_ON_SERVICE;
153 // ignore other cases
157 __stateMutex.Release();
161 SysLogException(NID_NET_BT, r, "[%s] exception occurred on accepting a push request.", GetErrorMessage(r));
164 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [ActionResult:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
170 _BluetoothOppServerImpl::RejectPush(void)
172 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
174 result r = E_FAILURE;
176 __stateMutex.Acquire();
178 switch (__currentState)
180 case _BT_OPP_SVR_STATE_DISABLED:
184 case _BT_OPP_SVR_STATE_IDLE:
185 case _BT_OPP_SVR_STATE_ON_SERVICE:
186 case _BT_OPP_SVR_STATE_ON_TRANSFER:
187 case _BT_OPP_SVR_STATE_ON_STOPPING:
188 r = E_INVALID_OPERATION;
191 case _BT_OPP_SVR_STATE_PUSH_REQUESTED:
192 r = __pOppAdapter->RejectOppPush();
194 // change the state into ON_SERVICE regardless of the result
195 __currentState = _BT_OPP_SVR_STATE_ON_SERVICE;
199 // ignore other cases
203 __stateMutex.Release();
207 SysLogException(NID_NET_BT, r, "[%s] exception occurred on rejecting a push request.", GetErrorMessage(r));
210 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [ActionResult:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
216 _BluetoothOppServerImpl::SetDestinationPath(const Tizen::Base::String& dstPath)
218 result r = E_INVALID_OPERATION;
221 if ((__currentState != _BT_OPP_SVR_STATE_ON_TRANSFER) &&
222 (__currentState != _BT_OPP_SVR_STATE_ON_STOPPING))
224 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
226 if (Tizen::Io::_FileImpl::ConvertVirtualToPhysicalPath(dstPath, physicalPath) != E_SUCCESS)
228 r = E_INACCESSIBLE_PATH;
232 r = __pOppAdapter->SetOppReceivePath(physicalPath);
237 r = __pOppAdapter->SetOppReceivePath(dstPath);
243 SysLogException(NID_NET_BT, r, "[%s] exception occurred on setting the destination path.", GetErrorMessage(r));
250 _BluetoothOppServerImpl::SetMinProgressInterval(int percent)
252 result r = E_SUCCESS;
254 if ((percent < 1) || (percent > 100))
257 SysLogException(NID_NET_BT, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The input argument - percent - is outside the valid range.");
261 __minProgressInterval = percent;
268 _BluetoothOppServerImpl::StartService(const Tizen::Base::String& dstPath)
270 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
272 result r = E_FAILURE;
275 __stateMutex.Acquire();
277 switch (__currentState)
279 case _BT_OPP_SVR_STATE_DISABLED:
283 case _BT_OPP_SVR_STATE_IDLE:
284 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
286 if (Tizen::Io::_FileImpl::ConvertVirtualToPhysicalPath(dstPath, physicalPath) != E_SUCCESS)
288 r = E_INACCESSIBLE_PATH;
294 physicalPath = dstPath;
297 r = __pOppAdapter->StartOppServer(*this, physicalPath);
301 __currentState = _BT_OPP_SVR_STATE_ON_SERVICE;
305 case _BT_OPP_SVR_STATE_ON_SERVICE:
306 case _BT_OPP_SVR_STATE_PUSH_REQUESTED:
307 case _BT_OPP_SVR_STATE_ON_TRANSFER:
311 case _BT_OPP_SVR_STATE_ON_STOPPING:
312 r = E_INVALID_OPERATION;
316 // ignore other cases
320 __stateMutex.Release();
324 SysLogException(NID_NET_BT, r, "[%s] exception occurred on starting OPP service.", GetErrorMessage(r));
327 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [ActionResult:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
333 _BluetoothOppServerImpl::StopService(void)
335 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
337 result r = E_FAILURE;
339 __stateMutex.Acquire();
341 switch (__currentState)
343 case _BT_OPP_SVR_STATE_DISABLED:
347 case _BT_OPP_SVR_STATE_IDLE:
348 r = E_INVALID_OPERATION;
351 case _BT_OPP_SVR_STATE_ON_SERVICE:
352 case _BT_OPP_SVR_STATE_ON_STOPPING:
353 r = __pOppAdapter->StopOppServer();
356 __currentState = _BT_OPP_SVR_STATE_IDLE;
360 case _BT_OPP_SVR_STATE_PUSH_REQUESTED:
361 r = __pOppAdapter->RejectOppPush();
364 r = __pOppAdapter->StopOppServer();
367 __currentState = _BT_OPP_SVR_STATE_IDLE;
371 __currentState = _BT_OPP_SVR_STATE_ON_SERVICE;
374 // Stays in the current status, if the rejection fails
377 case _BT_OPP_SVR_STATE_ON_TRANSFER:
378 r = __pOppAdapter->StopOppTransfer();
379 r = TransExceptionsInclusive(r ,E_FAILURE, E_OPERATION_FAILED);
382 r = __pOppAdapter->StopOppServer();
385 __currentState = _BT_OPP_SVR_STATE_IDLE;
389 __currentState = _BT_OPP_SVR_STATE_ON_SERVICE;
392 // Stays in the current status, if the terminating file transfer fails
396 // ignore other cases
400 __stateMutex.Release();
404 SysLogException(NID_NET_BT, r, "[%s] exception occurred on stopping OPP service.", GetErrorMessage(r));
407 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [ActionResult:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
413 _BluetoothOppServerImpl::StopTransfer(void)
415 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
417 result r = E_OPERATION_FAILED;
419 __stateMutex.Acquire();
421 switch (__currentState)
423 case _BT_OPP_SVR_STATE_DISABLED:
427 case _BT_OPP_SVR_STATE_IDLE:
428 case _BT_OPP_SVR_STATE_ON_SERVICE:
429 case _BT_OPP_SVR_STATE_PUSH_REQUESTED:
430 case _BT_OPP_SVR_STATE_ON_STOPPING:
431 r = E_INVALID_OPERATION;
434 case _BT_OPP_SVR_STATE_ON_TRANSFER:
435 r = __pOppAdapter->StopOppTransfer();
438 __currentState = _BT_OPP_SVR_STATE_ON_STOPPING;
440 // It will be changed to IDLE after receiving the TransferDone event.
444 // ignore other cases
448 __stateMutex.Release();
452 SysLogException(NID_NET_BT, r, "[%s] exception occurred on stopping OPP service.", GetErrorMessage(r));
455 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [ActionResult:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
461 _BluetoothOppServerImpl::OnBluetoothActivated(result r)
463 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [ActionResult:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
465 __stateMutex.Acquire();
467 if ((__currentState == _BT_OPP_SVR_STATE_DISABLED) && (r == E_SUCCESS))
469 __currentState = _BT_OPP_SVR_STATE_IDLE;
472 __stateMutex.Release();
474 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s]", GetStringOfCurrentState());
478 _BluetoothOppServerImpl::OnBluetoothDeactivated(result r)
480 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [ActionResult:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
482 __stateMutex.Acquire();
486 __currentState = _BT_OPP_SVR_STATE_DISABLED;
489 __stateMutex.Release();
491 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s]", GetStringOfCurrentState());
495 _BluetoothOppServerImpl::OnOppPushRequested(const BluetoothDevice& device)
497 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
499 bool isFired = false;
501 __stateMutex.Acquire();
503 if (__currentState == _BT_OPP_SVR_STATE_ON_SERVICE)
505 __currentState = _BT_OPP_SVR_STATE_PUSH_REQUESTED;
508 else if ((__currentState == _BT_OPP_SVR_STATE_PUSH_REQUESTED)
509 || (__currentState == _BT_OPP_SVR_STATE_ON_TRANSFER)
510 || (__currentState == _BT_OPP_SVR_STATE_ON_STOPPING))
512 (void) __pOppAdapter->RejectOppPush(); // ignore the result
514 // ignore other cases
516 __stateMutex.Release();
520 _BluetoothOppServerEventArg* pArg = new (std::nothrow) _BluetoothOppServerEventArg(device);
523 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
528 __pEvent->FireAsync(*pArg);
532 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [PUSH_REQUESTED_Event:%s]",
533 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
537 _BluetoothOppServerImpl::OnOppTransferInProgress(int percent)
539 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [Progress:%d]", GetStringOfCurrentState(), percent);
541 bool isFired = false;
543 __stateMutex.Acquire();
545 if ((__currentState == _BT_OPP_SVR_STATE_ON_TRANSFER) || (__currentState == _BT_OPP_SVR_STATE_ON_STOPPING))
547 if (((percent - __previousProgress) >= __minProgressInterval) || (percent >= 100))
550 __previousProgress = percent;
553 // ignore other scases
555 __stateMutex.Release();
559 _BluetoothOppServerEventArg* pArg = new (std::nothrow) _BluetoothOppServerEventArg(percent);
562 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
567 __pEvent->FireAsync(*pArg);
571 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [TRANSFER_IN_PROGRESS_Event:%s]",
572 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
576 _BluetoothOppServerImpl::OnOppTransferDone(const Tizen::Base::String& filePath, int fileSize, bool isCompleted)
578 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [TransferDone:%s]",
579 GetStringOfCurrentState(), isCompleted ? "Completed" : "NotCompleted");
581 result r = E_SUCCESS;
582 bool isFired = false;
584 __stateMutex.Acquire();
586 if (__currentState == _BT_OPP_SVR_STATE_IDLE)
589 __previousProgress = 0; // initialization for the next push transaction
591 else if ((__currentState == _BT_OPP_SVR_STATE_ON_TRANSFER) || (__currentState == _BT_OPP_SVR_STATE_ON_STOPPING))
593 __currentState = _BT_OPP_SVR_STATE_ON_SERVICE;
595 __previousProgress = 0; // initialization for the next push transaction
597 // ignore other cases
599 __stateMutex.Release();
603 _BluetoothOppServerEventArg* pArg = new (std::nothrow) _BluetoothOppServerEventArg(filePath, fileSize, isCompleted);
606 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
611 __pEvent->FireAsync(*pArg);
615 if ((isFired) && (r != E_SUCCESS))
617 SysLogException(NID_NET_BT, r, "[%s] exception occurred on stopping OPP service in the transfer-done callback.", GetErrorMessage(r));
620 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [ActionResult:%s], [TRANSFER_DONE_Event:%s]",
621 GetStringOfCurrentState(), GetErrorMessage(r), isFired ? "Fired" : "NotFired");
625 _BluetoothOppServerImpl::GetStringOfCurrentState(void) const
627 const char* pStateString = null;
629 switch (__currentState)
631 case _BT_OPP_SVR_STATE_DISABLED:
632 pStateString = "DISABLED";
635 case _BT_OPP_SVR_STATE_IDLE:
636 pStateString = "IDLE";
639 case _BT_OPP_SVR_STATE_ON_SERVICE:
640 pStateString = "ON_SERVICE";
643 case _BT_OPP_SVR_STATE_PUSH_REQUESTED:
644 pStateString = "PUSH_REQUESTED";
647 case _BT_OPP_SVR_STATE_ON_TRANSFER:
648 pStateString = "ON_TRANSFER";
651 case _BT_OPP_SVR_STATE_ON_STOPPING:
652 pStateString = "ON_STOPPING";
656 pStateString = "Unknown";
663 } } } // Tizen::Net::Bluetooth