-//
-// Copyright (c) 2012 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Apache License, Version 2.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-
-/**
- * @file FSys_DeviceManager.cpp
- * @brief This is the implementation file for _DeviceManager class.
- */
-
-#include <new>
-#include <system/media_key.h>
-#include <bluetooth.h>
-
-#include <FBaseSysLog.h>
-#include <FSys_DeviceManagerEventProvider.h>
-#include "FSys_DeviceManager.h"
-
-using namespace Tizen::App;
-using namespace Tizen::Io;
-using namespace Tizen::System;
-using namespace Tizen::Base;
-using namespace Tizen::Base::Collection;
-
-namespace {
- const static wchar_t* DEVICE_MANAGER_SERVICE_ID = L"osp.devicemanager.service";
- const static wchar_t* DEVICE_MANAGER_BLUETOOTH = L"osp.devicemanager.bluetooth";
-
- const static wchar_t* BLUETOOTH_A2DP_CONNECTED = L"Connected";
- const static wchar_t* BLUETOOTH_A2DP_DISCONNECTED = L"Disconnected";
- const static wchar_t* BLUETOOTH_A2DP_PLAY = L"Play";
- const static wchar_t* BLUETOOTH_A2DP_STOP = L"Stop";
- const static wchar_t* BLUETOOTH_A2DP_PAUSE = L"Pause";
- const static wchar_t* BLUETOOTH_A2DP_RESUME = L"Resume";
- const static wchar_t* BLUETOOTH_A2DP_FORWARD = L"Forward";
- const static wchar_t* BLUETOOTH_A2DP_FASTFORWARD = L"FastForward";
- const static wchar_t* BLUETOOTH_A2DP_BACKWARD = L"Backward";
- const static wchar_t* BLUETOOTH_A2DP_REWIND = L"Rewind";
-}
-
-Tizen::System::_DeviceManager* Tizen::System::_DeviceManager::__pDeviceManager = null;
-
-void bluetooth_connection_state_changed(int result, bool connected, const char* remote_address, bt_audio_profile_type_e type, void* user_data)
-{
- SysLog(NID_SYS, "Bluetooth headset[%s] connection[%d] event", remote_address, connected);
- String bt_event;
- _DeviceManager* pDeviceManager = _DeviceManager::GetInstance();
- SysTryReturn(NID_SYS, pDeviceManager, ,E_SYSTEM, "DeviceManager is not created");
-
- IDeviceEventListener* pDeviceEventListener = pDeviceManager->GetEventListener();
-
- if(pDeviceManager->GetBluetoothStatus() != connected && pDeviceEventListener)
- {
- if(connected == true)
- {
- bt_event = BLUETOOTH_A2DP_CONNECTED;
- }
- else
- {
- bt_event = BLUETOOTH_A2DP_DISCONNECTED;
- }
-
- pDeviceEventListener->OnDeviceStateChanged(DEVICE_TYPE_BLUETOOTH_HEADSET, bt_event);
- }
- pDeviceManager->SetBluetoothStatus(connected);
-}
-
-void app_media_key_handler(media_key_e key, media_key_event_e status, void* pUserData)
-{
- String event;
- SysLog(NID_SYS, "Bluetooth headset event is occured %d, %d", (int)key, (int)status);
- _DeviceManager* pDeviceManager = _DeviceManager::GetInstance();
- SysTryReturn(NID_SYS, pDeviceManager, ,E_SYSTEM, "DeviceManager is not created");
-
- if(status == MEDIA_KEY_STATUS_RELEASED)
- {
- switch(key)
- {
- case MEDIA_KEY_PLAY:
- event = BLUETOOTH_A2DP_PLAY;
- break;
- case MEDIA_KEY_STOP:
- event = BLUETOOTH_A2DP_STOP;
- break;
- case MEDIA_KEY_PAUSE:
- event = BLUETOOTH_A2DP_PAUSE;
- break;
- case MEDIA_KEY_PREVIOUS:
- event = BLUETOOTH_A2DP_BACKWARD;
- break;
- case MEDIA_KEY_NEXT:
- event = BLUETOOTH_A2DP_FORWARD;
- break;
- case MEDIA_KEY_FASTFORWARD:
- event = BLUETOOTH_A2DP_FASTFORWARD;
- break;
- case MEDIA_KEY_REWIND:
- event = BLUETOOTH_A2DP_REWIND;
- break;
- case MEDIA_KEY_UNKNOWN:
- default:
- SysLog(NID_SYS, "Unsupported key[%d] is occured.", key);
- return;
- }
-
- IDeviceEventListener* pDeviceEventListener = pDeviceManager->GetEventListener();
- if (pDeviceEventListener)
- {
- pDeviceEventListener->OnDeviceStateChanged(DEVICE_TYPE_BLUETOOTH_HEADSET, event);
- }
- }
-}
-
-_DeviceManager::_DeviceManager()
- : isBluetoothHeadSetConnected(false)
- , isInitBluetooth(false)
- , __pDeviceManagerListener(null)
-{
-}
-
-_DeviceManager::~_DeviceManager()
-{
- if(isInitBluetooth == true)
- {
- int btResult = 0;
- SysLog(NID_SYS, "Bluetooth headset event is released.");
- media_key_release();
- btResult = bt_audio_unset_connection_state_changed_cb();
- SysTryReturnVoidResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to unregister bluetooth headset connection event");
-
- btResult = bt_audio_deinitialize();
- SysTryReturnVoidResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to close bluetooth");
-
- btResult = bt_deinitialize();
- SysTryReturnVoidResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to init bluetooth module");
- isInitBluetooth = false;
- }
-}
-
-_DeviceManager*
-_DeviceManager::GetInstance(void)
-{
- if(__pDeviceManager == null)
- {
- __pDeviceManager = new (std::nothrow) _DeviceManager();
- }
- return __pDeviceManager;
-}
-
-result
-_DeviceManager::InitializeDevice(void)
-{
- result r = E_SUCCESS;
-
- if(isInitBluetooth == false)
- {
- SysLog(NID_SYS, "Bluetooth headset event is reserved.");
-
- int btResult = 0;
- btResult = bt_initialize();
-
- SysTryReturnResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to init bluetooth module");
-
- btResult = bt_audio_initialize();
- SysTryReturnResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to init bluetooth audio module");
-
- btResult = bt_audio_set_connection_state_changed_cb(bluetooth_connection_state_changed, null);
- SysTryReturnResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to register bluetooth audio event");
-
- isInitBluetooth = true;
- }
- media_key_reserve(app_media_key_handler, null);
-
- return r;
-}
-
-
-result
-_DeviceManager::DeinitializeDevice(void)
-{
- result r = E_SUCCESS;
- media_key_release();
- return r;
-}
-
-
-result
-_DeviceManager::RegisterListner(IDeviceEventListener &listener)
-{
- __pDeviceManagerListener = &listener;
- return E_SUCCESS;
-}
-
-result
-_DeviceManager::UnregisterListner(IDeviceEventListener &listener)
-{
- __pDeviceManagerListener = null;
- return E_SUCCESS;
-}
-
-IDeviceEventListener*
-_DeviceManager::GetEventListener()
-{
- return __pDeviceManagerListener;
-}
-
-String
-_DeviceManager::GetId(void)
-{
- return DEVICE_MANAGER_SERVICE_ID;
-}
-
-bool
-_DeviceManager::GetBluetoothStatus(void)
-{
- return isBluetoothHeadSetConnected;
-}
-void
-_DeviceManager::SetBluetoothStatus(bool status)
-{
- isBluetoothHeadSetConnected = status;
-}
-
-extern "C" _OSP_EXPORT_ _IDeviceManager* DeviceManager_CreateInstance()
-{
- return _DeviceManager::GetInstance();
-}