--- /dev/null
+//
+// Open Service Platform
+// 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_DeviceManagerStub.h
+ * @brief This is the header file of the _DeviceManagerStub class.
+ *
+ * This header file contains the declarations of the _DeviceManagerStub class.
+ */
+
+#ifndef _FSYS_DEVICE_MANAGER_STUB_H_
+#define _FSYS_DEVICE_MANAGER_STUB_H_
+
+#include <unique_ptr.h>
+#include <FBase.h>
+#include <FIo_IIpcServerEventListener.h>
+#include <FSystem.h>
+#include "FApp_IAppEventListener.h"
+#include "FSys_SystemServiceIpcEventForAsync.h"
+#include "FSys_DeviceManager.h"
+
+namespace Tizen { namespace System
+{
+class _OSP_EXPORT_ _DeviceManagerStub
+ : public Tizen::Base::Object
+ , public Tizen::Io::_IIpcServerEventListener
+ , public Tizen::System::IDeviceEventListener
+ , public Tizen::App::_IAppEventListener
+{
+public:
+ static _DeviceManagerStub* GetInstance(void);
+
+ virtual ~_DeviceManagerStub(void);
+ result SendData(const int pid, Tizen::Base::Collection::ArrayList* data);
+ virtual void OnDeviceStateChanged (DeviceType deviceType, const Tizen::Base::String &state);
+private:
+ _DeviceManagerStub(void);
+ result Construct(void);
+
+ static void InitSingleton(void);
+ static void DestroySingleton(void);
+
+ _DeviceManagerStub(const _DeviceManagerStub& rhs);
+ _DeviceManagerStub& operator =(const _DeviceManagerStub& rhs);
+
+ void AddInterestedApp(int pid);
+ void RemoveInterestedApp(int pid);
+
+ bool OnRequestOccured(const Tizen::Base::Collection::ArrayList& request, Tizen::Base::Collection::ArrayList* response);
+ virtual void OnIpcRequestReceived(Tizen::Io::_IpcServer& server, const IPC::Message& message);
+ virtual void OnApplicationTerminated(const Tizen::App::AppId& appId, int pid);
+
+ virtual void OnIpcServerStarted(const Tizen::Io::_IpcServer& server){};
+ virtual void OnIpcServerStopped(const Tizen::Io::_IpcServer& server){};
+ virtual void OnIpcClientConnected(const Tizen::Io::_IpcServer& server, int clientId){};
+ virtual void OnIpcClientDisconnected(const Tizen::Io::_IpcServer&server, int clientId){};
+
+ virtual void OnApplicationLaunched(const Tizen::App::AppId& appId, int pid){};
+
+private:
+ std::unique_ptr<Tizen::Io::_IpcServer> __pIpcServer;
+ static _DeviceManagerStub* __pDeviceManagerStub;
+ _DeviceManager* __pDeviceManager;
+ Tizen::Base::Runtime::Mutex __Mutex;
+ Tizen::App::AppId __currentPkgId;
+ int __currentPid;
+ Tizen::Base::String __serviceId;
+ Tizen::Base::String __command;
+ Tizen::Base::String __deviceType;
+ Tizen::Base::String __result;
+
+ Tizen::Base::Collection::ArrayList __interestedPidList;
+ _SystemServiceIpcEventForAsync* __pDeviceManagerServiceIpcEventForAsync;
+ bool __IsInitializedDevice;
+}; // _DeviceManagerStub
+
+}} // Tizen::System
+
+#endif // _FSYS_DEVICE_MANAGER_STUB_H_
--- /dev/null
+//
+// Open Service Platform
+// 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_DeviceManagerStub.cpp
+ * @brief This is the implementation for the _DeviceManagerStub class.
+ */
+#include <pthread.h>
+
+#include <FBaseSysLog.h>
+#include <FIo_IpcServer.h>
+#include <FIo_AppServiceIpcMessages.h>
+#include <FSystem.h>
+
+#include "FApp_AppManagerImpl.h"
+#include "FSys_DeviceManagerStub.h"
+
+using namespace std;
+using namespace Tizen::Base;
+using namespace Tizen::Base::Collection;
+using namespace Tizen::Io;
+using namespace Tizen::System;
+using namespace Tizen::App;
+
+const static wchar_t* DEVICE_MANAGER_SERVICE_ID = L"osp.sys.ipcserver.devicemanager";
+const static wchar_t* DEVIVE_MANAGER_SERVICE_MUTEX_ID = L"osp.sys.ipcserver.devicemanager";
+const static wchar_t* DEVICE_MANAGER_COMMAND_OPEN = L"osp.devicemanager.command.open";
+const static wchar_t* DEVICE_MANAGER_COMMAND_CLOSE = L"osp.devicemanager.command.close";
+const static wchar_t* DEVICE_MANAGER_COMMAND_STATUS= L"osp.devicemanager.command.status";
+const static wchar_t* DEVICE_MANAGER_COMMAND_EVENT = L"osp.devicemanager.command.event";
+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";
+
+_DeviceManagerStub* _DeviceManagerStub::__pDeviceManagerStub = null;
+
+_DeviceManagerStub::_DeviceManagerStub(void)
+ : __pIpcServer(null)
+ , __pDeviceManager(null)
+ , __pDeviceManagerServiceIpcEventForAsync(null)
+ , __IsInitializedDevice(false)
+{
+ result r = E_SUCCESS;
+ r = __Mutex.Create(DEVIVE_MANAGER_SERVICE_MUTEX_ID);
+ SysTryReturn(NID_SYS, r == E_SUCCESS, , r, "It is failed to create mutex. [%s] Propaged.", GetErrorMessage(r));
+ r = __interestedPidList.Construct();
+ SysTryReturn(NID_SYS, r == E_SUCCESS, , r, "It is failed to initiate bluetooth headset application list.");
+}
+
+_DeviceManagerStub::~_DeviceManagerStub(void)
+{
+ if (__pIpcServer)
+ {
+ __pIpcServer->Stop();
+ }
+
+ if (__pDeviceManagerServiceIpcEventForAsync)
+ {
+ delete __pDeviceManagerServiceIpcEventForAsync;
+ }
+ __interestedPidList.RemoveAll();
+ result r = __Mutex.Release();
+ SysTryReturn(NID_SYS, r == E_SUCCESS, , r, "It is failed to release mutex. [%s] Propaged.", GetErrorMessage(r));
+}
+
+_DeviceManagerStub*
+_DeviceManagerStub::GetInstance(void)
+{
+ static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
+
+ if (__pDeviceManagerStub == null)
+ {
+ pthread_once(&onceBlock, InitSingleton);
+ }
+ return __pDeviceManagerStub;
+}
+
+void
+_DeviceManagerStub::InitSingleton(void)
+{
+ unique_ptr< _DeviceManagerStub > pDeviceManagerStub(new (std::nothrow) _DeviceManagerStub());
+ SysTryReturn(NID_SYS, pDeviceManagerStub, ,E_OUT_OF_MEMORY, "It is not enough memory.");
+
+ result r = pDeviceManagerStub->Construct();
+ SysTryReturn(NID_SYS, !IsFailed(r), , r, "[%s] It is failed to get DeviceManagerCommunicationStub.", GetErrorMessage(r));
+ __pDeviceManagerStub = pDeviceManagerStub.release();
+ SysLog(NID_SYS, "It is success InitSingleton.");
+ atexit(DestroySingleton);
+}
+
+void
+_DeviceManagerStub::DestroySingleton(void)
+{
+ delete __pDeviceManagerStub;
+}
+
+
+result
+_DeviceManagerStub::Construct(void)
+{
+ unique_ptr<_IpcServer> pIpcServer(new (std::nothrow) _IpcServer());
+ SysTryReturnResult(NID_SYS, pIpcServer, E_OUT_OF_MEMORY, "Memory is insufficient.");
+
+ result r = pIpcServer->Construct(String(DEVICE_MANAGER_SERVICE_ID), *this, true);
+ SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to construct.");
+
+ r = pIpcServer->Start();
+ SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to start.");
+
+ __pDeviceManager = _DeviceManager::GetInstance();
+ SysTryReturn(NID_SYS, __pDeviceManager, E_SYSTEM, E_SYSTEM,"It is failed to create DeviceManager instance.");
+
+ if (__IsInitializedDevice == false)
+ {
+ r = __pDeviceManager->InitializeDevice();
+ SysTryReturn(NID_SYS, !IsFailed(r), false, E_SYSTEM, "It is failed to init device");
+ __IsInitializedDevice = true;
+ }
+
+ __pIpcServer = move(pIpcServer);
+
+ _AppManagerImpl* pAppManagerImpl = Tizen::App::_AppManagerImpl::GetInstance();
+ pAppManagerImpl->AddAppEventListener(*this);
+
+ return E_SUCCESS;
+}
+
+
+
+result
+_DeviceManagerStub::SendData(const int pid, Tizen::Base::Collection::ArrayList* data)
+{
+ __Mutex.Acquire();
+ result r = E_SUCCESS;
+
+ SysTryCatch(NID_SYS, __pIpcServer != null, r = E_SYSTEM, E_SYSTEM, "IPC server is not ready.");
+ SysTryCatch(NID_SYS, data != null, r = E_SYSTEM, E_SYSTEM, "There is no data.");
+
+ if(__pDeviceManagerServiceIpcEventForAsync == null)
+ {
+ __pDeviceManagerServiceIpcEventForAsync = new (std::nothrow) _SystemServiceIpcEventForAsync(__pIpcServer.get());
+ __pDeviceManagerServiceIpcEventForAsync->Construct(1048576, Tizen::Base::Runtime::THREAD_PRIORITY_MID);
+ __pDeviceManagerServiceIpcEventForAsync->Start();
+ }
+ SysTryCatch(NID_SYS, __pDeviceManagerServiceIpcEventForAsync != null, r = E_SYSTEM, E_SYSTEM, "Event thread is not ready.");
+
+ r = __pDeviceManagerServiceIpcEventForAsync->SendUserEvent((RequestId)pid, (IList*)data);
+ SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "Message is failed to add on event thread.");
+ SysLog(NID_SYS, "It is success to send data");
+CATCH:
+ if(r != E_SUCCESS)
+ {
+ delete data;
+ }
+ __Mutex.Release();
+ return r;
+}
+
+void
+_DeviceManagerStub::AddInterestedApp(int pid)
+{
+ int count = 0;
+ for(count = 0; count < __interestedPidList.GetCount(); count++)
+ {
+ Integer* pPid = (Integer*)__interestedPidList.GetAt(count);
+ if(pPid->value == pid)
+ {
+ return;
+ }
+ }
+
+ if (__interestedPidList.GetCount() == 0)
+ {
+ __pDeviceManager->RegisterListner(*this);
+ }
+
+ std::unique_ptr< Integer > pPid(new (std::nothrow) Integer(pid));
+ SysTryReturnVoidResult(NID_SYS, pPid, E_OUT_OF_MEMORY, "It is not enough memory.");
+
+ result r = __interestedPidList.Add(pPid.get());
+ SysTryReturnVoidResult(NID_SYS, !IsFailed(r), r, "It is failed to add pid");
+ pPid.release();
+}
+
+
+void
+_DeviceManagerStub::RemoveInterestedApp(int pid)
+{
+ for(int count = 0; count < __interestedPidList.GetCount(); count++)
+ {
+ Integer* pPid = (Integer*)__interestedPidList.GetAt(count);
+ SysTryReturnVoidResult(NID_SYS, pPid != null, E_SYSTEM, "It is failed to get integerested PID.");
+ if(pPid->value == pid)
+ {
+ __interestedPidList.RemoveAt(count, true);
+ break;
+ }
+ }
+
+ if(__interestedPidList.GetCount() == 0)
+ {
+ __pDeviceManager->UnregisterListner(*this);
+ }
+}
+
+
+void
+_DeviceManagerStub::OnDeviceStateChanged (DeviceType deviceType, const Tizen::Base::String &state)
+{
+ if(__interestedPidList.GetCount() > 0)
+ {
+ int count = 0;
+ Integer* pPid = null;
+ ArrayList* data = null;
+
+ for(count = 0 ; count < __interestedPidList.GetCount() ; count++)
+ {
+ pPid = (Integer*)__interestedPidList.GetAt(count);
+ if(pPid == null)
+ {
+ SysLogException(NID_SYS, E_SYSTEM, "It is failed to get appid from bluetooth headset app list");
+ }
+ else
+ {
+ __serviceId = DEVICE_MANAGER_SERVICE_ID;
+ __command = DEVICE_MANAGER_COMMAND_EVENT;
+ __deviceType = DEVICE_MANAGER_BLUETOOTH;
+ __result = state;
+
+ data = new (std::nothrow) ArrayList();
+ data->Construct();
+ data->Add(__serviceId);
+ data->Add(__command);
+ data->Add(__deviceType);
+ data->Add(__result);
+
+ SendData(pPid->value, data);
+ SysLog(NID_SYS, "Bluetooth headset event is sent to %d [%ls].", pPid->value, state.GetPointer());
+ }
+ }
+ }
+ else
+ {
+ SysLog(NID_SYS, "Bluetooth Headset Event is not required by any application.");
+ }
+}
+
+void
+_DeviceManagerStub::OnIpcRequestReceived(_IpcServer& server, const IPC::Message& message)
+{
+ SysLog(NID_SYS, "Recevied");
+ __currentPkgId = server.GetClientPackageId();
+ __currentPid = server.GetClientId();
+ IPC_BEGIN_MESSAGE_MAP(_DeviceManagerStub, message)
+ IPC_MESSAGE_HANDLER_EX(IoService_Request, &server, OnRequestOccured)
+ IPC_END_MESSAGE_MAP_EX()
+}
+
+bool
+_DeviceManagerStub::OnRequestOccured(const ArrayList& request, ArrayList* response)
+{
+ SysLog(NID_SYS, "OnRequestOccured");
+
+ int cmp = 0;
+ result r = E_SUCCESS;
+ bool isSuccess = false;
+ StringComparer strComparer;
+
+ __command = *(String*)request.GetAt(1);
+ __deviceType = *(String*)request.GetAt(2);
+
+ SysLog(NID_SYS, "command is %ls, device is %ls", __command.GetPointer(), __deviceType.GetPointer());
+
+ strComparer.Compare(__command, String(DEVICE_MANAGER_COMMAND_OPEN), cmp);
+ if(cmp == 0)
+ {
+ strComparer.Compare(__deviceType, String(DEVICE_MANAGER_BLUETOOTH), cmp);
+ if(cmp == 0)
+ {
+ SysLog(NID_SYS, "Bluetooth headset event is required");
+ AddInterestedApp(__currentPid);
+ }
+ }
+
+ strComparer.Compare(__command, String(DEVICE_MANAGER_COMMAND_CLOSE), cmp);
+ if(cmp == 0)
+ {
+ strComparer.Compare(__deviceType, String(DEVICE_MANAGER_BLUETOOTH), cmp);
+ if(cmp == 0)
+ {
+ RemoveInterestedApp(__currentPid);
+ }
+ }
+ strComparer.Compare(__command, String(DEVICE_MANAGER_COMMAND_STATUS), cmp);
+ if(cmp == 0)
+ {
+ strComparer.Compare(__deviceType, String(DEVICE_MANAGER_BLUETOOTH), cmp);
+ if(cmp == 0)
+ {
+ bool isBluetoothHeadSetConnected = __pDeviceManager->GetBluetoothStatus();
+ SysLog(NID_SYS, "Bluetooth headset status is %d", isBluetoothHeadSetConnected);
+
+ if(response != null)
+ {
+ __serviceId = DEVICE_MANAGER_SERVICE_ID;
+ __command = DEVICE_MANAGER_COMMAND_STATUS;
+ __deviceType = DEVICE_MANAGER_BLUETOOTH;
+
+ if(isBluetoothHeadSetConnected == true)
+ {
+ __result = BLUETOOTH_A2DP_CONNECTED;
+ }
+ else
+ {
+ __result = BLUETOOTH_A2DP_DISCONNECTED;
+ }
+ response->Add(__serviceId);
+ response->Add(__command);
+ response->Add(__deviceType);
+ response->Add(__result);
+ }
+ }
+ }
+
+ isSuccess = true;
+CATCH:
+ ArrayList* temp = const_cast<ArrayList*>(&request);
+ temp->RemoveAll(true);
+ return isSuccess;
+}
+
+
+void
+_DeviceManagerStub::OnApplicationTerminated(const AppId& appId, int pid)
+{
+ SysLog(NID_SYS, "OnApplicationTerminated %ls, %d", appId.GetPointer(), pid);
+ RemoveInterestedApp(pid);
+}