--- /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_DeviceManagerServiceIpcEventForAsync.h"
+
+namespace Tizen { namespace System
+{
+
+class _IDeviceManager;
+
+class _OSP_EXPORT_ _DeviceManagerStub
+ : public Tizen::Base::Object
+ , public Tizen::Io::_IIpcServerEventListener
+ , public Tizen::System::IDeviceEventListener
+ , public Tizen::App::_IAppEventListener
+{
+public:
+ static _DeviceManagerStub* CreateInstanceN(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);
+
+ _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 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 OnIpcRequestReceived(Tizen::Io::_IpcServer& server, const IPC::Message& message);
+
+ virtual void OnApplicationLaunched(const Tizen::App::AppId& appId, int pid);
+ virtual void OnApplicationTerminated(const Tizen::App::AppId& appId, int pid);
+
+private:
+ std::unique_ptr<Tizen::Io::_IpcServer> __pIpcServer;
+ static _DeviceManagerStub* __pDeviceManagerStub;
+ Tizen::Base::Runtime::Mutex __Mutex;
+ Tizen::App::AppId __currentPkgId;
+ int __currentPid;
+
+ void* __pSystemServiceDllHandle;
+ _IDeviceManager* __pIDeviceManager;
+ Tizen::Base::Collection::ArrayList __interestedPidList;
+ static _DeviceManagerServiceIpcEventForAsync* __pDeviceManagerServiceIpcEventForAsync;
+}; // _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 <dlfcn.h>
+#include <FBaseSysLog.h>
+#include <FIo_IpcServer.h>
+#include <FIo_AppServiceIpcMessages.h>
+#include <FSystem.h>
+
+#include "FApp_AppManagerImpl.h"
+#include "FSys_IDeviceManager.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";
+
+_DeviceManagerServiceIpcEventForAsync* _DeviceManagerStub::__pDeviceManagerServiceIpcEventForAsync = null;
+_DeviceManagerStub* _DeviceManagerStub::__pDeviceManagerStub = null;
+
+_DeviceManagerStub::_DeviceManagerStub(void)
+ : __pSystemServiceDllHandle(null)
+ , __pIpcServer(null)
+ , __pIDeviceManager(null)
+{
+ 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.", 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 (__pSystemServiceDllHandle)
+ {
+ dlclose(__pSystemServiceDllHandle);
+ }
+
+ 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.", r);
+}
+
+_DeviceManagerStub*
+_DeviceManagerStub::CreateInstanceN()
+{
+ if (__pDeviceManagerStub == null)
+ {
+ __pDeviceManagerStub = new (std::nothrow) _DeviceManagerStub();
+ result r = __pDeviceManagerStub->Construct();
+ SysTryReturn(NID_SYS, !IsFailed(r), null, r, "[%s] It is failed to get DeviceManagerCommunicationStub.", GetErrorMessage(r));
+ }
+ return __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.");
+
+ __pIpcServer = move(pIpcServer);
+
+ __pSystemServiceDllHandle = dlopen("libosp-system-server.so", RTLD_LAZY);
+
+ if (!__pSystemServiceDllHandle)
+ {
+ SysLog(NID_SYS, "It is failed to load system service (%s).", dlerror());
+ return E_SYSTEM;
+ }
+
+ _IDeviceManager* (*pFnDeviceManager_CreateInstance)(void) = null;
+ pFnDeviceManager_CreateInstance = reinterpret_cast< _IDeviceManager* (*)(void) >(dlsym(__pSystemServiceDllHandle, "DeviceManager_CreateInstance"));
+ SysTryReturn(NID_SYS, pFnDeviceManager_CreateInstance, E_SYSTEM, E_SYSTEM, "It is failed to find DeviceManager_CreateInstance.");
+
+ __pIDeviceManager = pFnDeviceManager_CreateInstance();
+ SysTryReturn(NID_SYS, __pIDeviceManager, E_SYSTEM, E_SYSTEM,"It is failed to create DeviceManager instance.");
+
+ r = __pIDeviceManager->InitializeDevice();
+ SysTryReturn(NID_SYS, !IsFailed(r), E_SYSTEM, E_SYSTEM, "It is failed to init device");
+ return E_SUCCESS;
+}
+
+
+
+result
+_DeviceManagerStub::SendData(const int pid, Tizen::Base::Collection::ArrayList* data)
+{
+ 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) _DeviceManagerServiceIpcEventForAsync(__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.");
+
+CATCH:
+ if(r != E_SUCCESS)
+ {
+ delete data;
+ }
+
+ 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)
+ {
+ __pIDeviceManager->RegisterListner(*this);
+ _AppManagerImpl* pAppManagerImpl = Tizen::App::_AppManagerImpl::GetInstance();
+ pAppManagerImpl->AddAppEventListener(*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)
+{
+ int count = 0;
+ for(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)
+ {
+ __pIDeviceManager->DeinitializeDevice();
+ __pIDeviceManager->UnregisterListner(*this);
+ _AppManagerImpl* pAppManagerImpl = Tizen::App::_AppManagerImpl::GetInstance();
+ pAppManagerImpl->RemoveAppEventListener(*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
+ {
+ data = new (std::nothrow) ArrayList(SingleObjectDeleter);
+ data->Construct();
+ data->Add(new (std::nothrow) String(DEVICE_MANAGER_SERVICE_ID));
+ data->Add(new (std::nothrow) String(DEVICE_MANAGER_COMMAND_EVENT));
+ data->Add(new (std::nothrow) String(DEVICE_MANAGER_BLUETOOTH));
+ data->Add(new (std::nothrow) String(state));
+
+ 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");
+ __Mutex.Acquire();
+ __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()
+ __Mutex.Release();
+}
+
+bool
+_DeviceManagerStub::OnRequestOccured(const ArrayList& request, ArrayList* response)
+{
+ SysLog(NID_SYS, "OnRequestOccured");
+
+ StringComparer strComparer;
+ int cmp = 0;
+ result r = E_SUCCESS;
+ bool isSuccess = false;
+ String* command = null;
+ String* device = null;
+ command = (String*)request.GetAt(1);
+ device = (String*)request.GetAt(2);
+ SysTryCatch(NID_SYS, command!= null && device != null, r = E_INVALID_ARG, E_INVALID_ARG, "Parameters has no command %x, %x", command, device);
+
+ SysLog(NID_SYS, "command is %ls, device is %ls", command->GetPointer(), device->GetPointer());
+
+ strComparer.Compare(*command, String(DEVICE_MANAGER_COMMAND_OPEN), cmp);
+ if(cmp == 0)
+ {
+ strComparer.Compare(*device, 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(*device, String(DEVICE_MANAGER_BLUETOOTH), cmp);
+ if(cmp == 0)
+ {
+ RemoveInterestedApp(__currentPid);
+ }
+ }
+ strComparer.Compare(*command, String(DEVICE_MANAGER_COMMAND_STATUS), cmp);
+ if(cmp == 0)
+ {
+ strComparer.Compare(*device, String(DEVICE_MANAGER_BLUETOOTH), cmp);
+ if(cmp == 0)
+ {
+ bool isBluetoothHeadSetConnected = __pIDeviceManager->GetBluetoothStatus();
+ SysLog(NID_SYS, "Bluetooth headset status is %d", isBluetoothHeadSetConnected);
+
+ if(response != null)
+ {
+ String* serviceId = new (std::nothrow) String(DEVICE_MANAGER_SERVICE_ID);
+ String* commandId = new (std::nothrow) String(DEVICE_MANAGER_COMMAND_STATUS);
+ String* deviceId = new (std::nothrow) String(DEVICE_MANAGER_BLUETOOTH);
+ String* status = null;
+ if(isBluetoothHeadSetConnected == true)
+ {
+ status = new (std::nothrow) String(BLUETOOTH_A2DP_CONNECTED);
+ }
+ else
+ {
+ status = new (std::nothrow) String(BLUETOOTH_A2DP_DISCONNECTED);
+ }
+ response->Add(*serviceId);
+ response->Add(*commandId);
+ response->Add(*deviceId);
+ response->Add(*status);
+ }
+ }
+ }
+
+ isSuccess = true;
+CATCH:
+ ArrayList* temp = const_cast<ArrayList*>(&request);
+ temp->RemoveAll(true);
+ return isSuccess;
+}
+void
+_DeviceManagerStub::OnIpcServerStarted(const _IpcServer& server)
+{
+ SysLog(NID_SYS, "Started");
+}
+
+void
+_DeviceManagerStub::OnIpcServerStopped(const _IpcServer& server)
+{
+ SysLog(NID_SYS, "Stop");
+}
+
+void
+_DeviceManagerStub::OnIpcClientConnected(const _IpcServer& server, int clientId)
+{
+ SysLog(NID_SYS, "Connected");
+}
+
+void
+_DeviceManagerStub::OnIpcClientDisconnected(const _IpcServer&server, int clientId)
+{
+ SysLog(NID_SYS, "Disconnected");
+}
+
+void
+_DeviceManagerStub::OnApplicationLaunched(const AppId& appId, int pid)
+{
+ //dummy
+}
+void
+_DeviceManagerStub::OnApplicationTerminated(const AppId& appId, int pid)
+{
+ RemoveInterestedApp(pid);
+}