Tizen 2.1 base
[platform/framework/native/app-service.git] / src / FSys_AccessoryManagerService.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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
8 //
9 //     http://floralicense.org/license/
10 //
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.
16 //
17
18 /**
19  * @file                FSys_AccessoryManagerService.cpp
20  * @brief               This is the implementation file for _AccessoryManagerService class.
21  */
22
23 #include <new>
24 #include <FBaseSysLog.h>
25 #include <FBaseRtLibrary.h>
26
27 #include "FSys_AccessoryManagerService.h"
28 #include "FApp_CommunicationDispatcher.h"
29
30 using namespace Tizen::App;
31 using namespace Tizen::Io;
32 using namespace Tizen::System;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Base::Runtime;
36
37 namespace {
38         const String ACCESSORY_MANAGER_SERVICE_ID = L"osp.accessorymanager.service";
39         const String ACCESSORY_PLUGIN_LIBRARY_PATH = L"/opt/apps/aospd00043/lib/libosp-cond-accessory.so";
40 }
41
42 typedef result (*SendDataFunction)(Tizen::App::AppId appId, Tizen::Base::Collection::ArrayList* data);
43 typedef void (*RegisterSendDataFunction)(SendDataFunction function);
44
45 _AccessoryManagerService* gpAccessoryManagerService = null;
46
47 result
48 SendDataFunc(AppId appId, ArrayList* data)
49 {
50         SysLog(NID_SYS, "Request to send data");
51         SysTryReturnResult(NID_SYS, gpAccessoryManagerService != null, E_SYSTEM, "AccessoryManagerService is not ready");
52         return gpAccessoryManagerService->SendData(appId, data);
53 }
54
55 _AccessoryManagerService*
56 _AccessoryManagerService::GetInstance()
57 {
58         if(gpAccessoryManagerService == null)
59         {
60                 gpAccessoryManagerService = new (std::nothrow) _AccessoryManagerService();
61         }
62         return gpAccessoryManagerService;
63 }
64
65 _AccessoryManagerService::_AccessoryManagerService()
66         : _ICommunicationRequestListener()
67         , __pLib(null)
68         , __pCommunicationDispatcher(null)
69         , __pOnRequestOccuredFunction(null)
70         , __pRemoveOwnerApplicationFunction(null)
71 {
72         result r = E_SUCCESS;
73         RegisterSendDataFunction pRegisterSendDataFunction;
74
75         __pCommunicationDispatcher = _CommunicationDispatcher::GetInstance();
76         SysTryCatch(NID_SYS, __pCommunicationDispatcher != null, r = E_SYSTEM, E_SYSTEM, "Failed to get _CommunicationDispatcher");
77
78         r = __pCommunicationDispatcher->AddCommunicationEventListener(*this);
79         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "Failed to add event listener on _CommunicationDispatcher");
80
81         __pLib = new Library();
82         r = __pLib->Construct(ACCESSORY_PLUGIN_LIBRARY_PATH);
83         SysTryCatch(NID_APP, !IsFailed(r),, r, "dlopen fails (%s)", GetErrorMessage(r));
84
85         pRegisterSendDataFunction = (RegisterSendDataFunction)__pLib->GetProcAddress(L"RegisterSendData");
86         SysTryCatch(NID_APP, pRegisterSendDataFunction != null, r = E_SYSTEM, E_SYSTEM, "Failed to find RegisterSendDataFunction");
87
88         __pOnRequestOccuredFunction = (OnRequestOccuredFunction)__pLib->GetProcAddress(L"OnRequestOccured");
89         SysTryCatch(NID_APP, __pOnRequestOccuredFunction != null, r = E_SYSTEM, E_SYSTEM, "Failed to find OnRequestOccuredFunction");
90
91         __pRemoveOwnerApplicationFunction = (RemoveOwnerApplicationFunction)__pLib->GetProcAddress(L"RemoveOwnerApplication");
92         SysTryCatch(NID_APP, __pRemoveOwnerApplicationFunction != null, r = E_SYSTEM, E_SYSTEM, "Failed to find OnRequestOccuredFunction");
93
94         pRegisterSendDataFunction(SendDataFunc);
95
96 CATCH:
97         SetLastResult(r);
98 }
99
100 _AccessoryManagerService::~_AccessoryManagerService()
101 {
102         result r = E_SUCCESS;
103
104         SysTryCatch(NID_SYS, __pCommunicationDispatcher != null, r = E_SYSTEM, E_SYSTEM, "_CommunicationDispatcher is not initiated.");
105
106         r = __pCommunicationDispatcher->RemoveCommunicationEventListener(*this);
107         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "Failed to remove event listener on _CommunicationDispatcher");
108
109 CATCH:
110         SetLastResult(r);
111
112 }
113
114 result
115 _AccessoryManagerService::SendData(AppId appId, ArrayList* data)
116 {
117         SysTryReturnResult(NID_SYS, __pCommunicationDispatcher != null, E_SYSTEM, "_CommunicationDispatcher is not initiated.");
118
119         return __pCommunicationDispatcher->SendData(appId, *data);
120 }
121
122 String
123 _AccessoryManagerService::GetId(void)
124 {
125         return ACCESSORY_MANAGER_SERVICE_ID;
126 }
127
128 void
129 _AccessoryManagerService::OnRequestOccured(AppId appId, ArrayList* request, ArrayList* response)
130 {
131         result r = E_SUCCESS;
132         SysTryCatch(NID_SYS, __pOnRequestOccuredFunction != null, r = E_SYSTEM, E_SYSTEM, "There is no OnRequestOccuredFunction");
133
134         SysLog(NID_SYS, "AppId is [%ls]", appId.GetPointer());
135         __pOnRequestOccuredFunction(appId, request, response);
136
137 CATCH:
138         SetLastResult(r);
139 }
140
141 void
142 _AccessoryManagerService::OnApplicationTerminated(const AppId& appId, _AppType type)
143 {
144         result r = E_SUCCESS;
145         SysTryCatch(NID_SYS, __pRemoveOwnerApplicationFunction != null, r = E_SYSTEM, E_SYSTEM, "There is no RemoveOwnerApplicationFunction");
146
147         __pRemoveOwnerApplicationFunction(appId, L"");
148 CATCH:
149         SetLastResult(r);
150 }
151