AppControl 2.0/2.1 argument handling
[framework/osp/app-controls.git] / src / bluetooth-app-control / BluetoothAppControlDllEntry.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
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         BluetoothAppControlDllEntry.cpp
20  * @brief       This is the implementation for the BluetoothAppControlDllEntry.cpp class.
21  */
22
23 #include <appsvc/appsvc.h>
24
25 #include <FBaseSysLog.h>
26 #include <FBaseInteger.h>
27 #include <FBaseLong.h>
28 #include <FAppAppControl.h>
29 #include <FBaseColHashMap.h>
30
31 #include <FBase_StringConverter.h>
32 #include <FApp_AppControlManager.h>
33 #include <FApp_AppMessageImpl.h>
34 #include <FNetBt_BluetoothAdapterUtility.h>
35 #include <FSys_SystemInfoImpl.h>
36 #include <FApp_Aul.h>
37
38
39 using namespace Tizen::App;
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Net::Bluetooth;
43
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49
50 result _OSP_EXPORT_ StartAppControl(int req, const String&, const String&, const String*, const String*, const IMap*);
51 result _OSP_EXPORT_ TerminateAppControl(int req);
52 void OnAppControlResult(void*, int, service_result_e, void*);
53
54 static int __req = -1;
55 static int __processId = -1;
56
57 static const char __allowedAppControlTable[][2][96] =
58 {
59         {"osp.appcontrol.BT", "osp.appcontrol.operation.PICK"},
60         {"osp.appcontrol.provider.bluetooth", "osp.appcontrol.operation.pick"},
61         {"tizen.bluetooth", "http://tizen.org/appcontrol/operation/pick"},
62         {"tizen.bluetooth", "http://tizen.org/appcontrol/operation/bluetooth/pick"},
63 };
64
65
66 result
67 StartAppControl(int req, const String& aId, const String& oId, const String* pUri, const String* pMime, const IMap* pMap)
68 {
69         SysLog(NID_APP, "Entry to Bluetooth AppControl");
70
71         result r = E_SUCCESS;
72         bool isBtSupported = false;
73         _AppMessageImpl msg;
74
75         r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/network.bluetooth", isBtSupported);
76         SysTryReturnResult(NID_APP, (r == E_SUCCESS) && (isBtSupported == true), E_SYSTEM, "[%s] Bluetooth is not supported.", GetErrorMessage(r));
77
78         const bool isAllowed = _AppControlManager::IsAllowedAppControl(__allowedAppControlTable, 4, aId, oId);
79         SysTryReturnResult(NID_APP, isAllowed, E_SYSTEM, "Invalid AppControl entry for (%ls, %ls).", aId.GetPointer(), oId.GetPointer());
80
81         SysLog(NID_APP, "Bluetooth AppControl.");
82
83         msg.AddData(pMap);
84         msg.AddData(L"launch-type", L"pick");
85
86         const String& package = _AppControlManager::GetAliasAppId(aId);
87         SysLog(NID_APP, "Actual packageId is %ls.", package.GetPointer());
88
89         __processId = _AppControlManager::GetInstance()->Launch(msg, package, oId, pUri, pMime, OnAppControlResult, 0);
90
91         SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Bluetooth AppControl is failed.");
92         SysLog(NID_APP, "Launching Bluetooth AppControl succeeded.");
93
94         __req = req;
95
96         return r;
97 }
98
99 result
100 TerminateAppControl(int req)
101 {
102         if (__processId >= 0)
103         {
104                 _Aul::TerminateApplicationByPid(__processId);           
105         }
106         return E_SUCCESS;
107 }
108
109 void
110 OnAppControlResult(void* b, int requestCode, service_result_e res, void* userData)
111 {
112         result r = E_SYSTEM;
113         bundle* pBundle = static_cast<bundle*>(b);
114         String dataStr;
115         int majClassType = 0;
116         int minClassType = 0;
117         int svcClassType = 0;
118         unsigned long svcType = 0;
119         int result = SERVICE_RESULT_SUCCEEDED;
120         const char* pBuf;
121
122         HashMap* pResult = new (std::nothrow) HashMap();
123         SysTryCatch(NID_APP, pResult != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
124
125         r = pResult->Construct();
126
127         pBuf = appsvc_get_data(pBundle, "result");
128         if (strcmp(pBuf, "success") == 0)
129         {
130                 result = SERVICE_RESULT_SUCCEEDED;
131         }
132         else
133         {
134                 result = SERVICE_RESULT_FAILED;
135         }
136
137         SysLog(NID_APP, "The service result %s", result == SERVICE_RESULT_SUCCEEDED? "succeeded." : "failed.");
138
139         switch (result)
140         {
141         case SERVICE_RESULT_SUCCEEDED:
142                 {
143                         pBuf = appsvc_get_data(pBundle, "address");
144                         if (pBuf)
145                         {
146                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/address"), new (std::nothrow) String(pBuf));
147                         }
148
149                         pBuf = appsvc_get_data(pBundle, "name");
150                         if (pBuf)
151                         {
152                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/name"), new (std::nothrow) String(pBuf));
153                         }
154
155                         pBuf = appsvc_get_data(pBundle, "rssi");
156                         if (pBuf)
157                         {
158                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/rssi"), new (std::nothrow) String(pBuf));
159                         }
160
161                         pBuf = appsvc_get_data(pBundle, "is_bonded");
162                         if (pBuf)
163                         {
164                                 if (*pBuf == '1')
165                                 {
166                                         pBuf = "true";
167                                 }
168                                 else
169                                 {
170                                         pBuf = "false";
171                                 }
172                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/is_paired"), new (std::nothrow) String(pBuf));
173                         }
174
175                         pBuf = appsvc_get_data(pBundle, "major_class");
176                         if (pBuf)
177                         {
178                                 Integer::Decode(String(pBuf), majClassType);
179                                 majClassType = (int)_BluetoothAdapterUtility::ConvertToMajorDeviceClassType((bt_major_device_class_e)majClassType);
180
181                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/major_class"), new (std::nothrow) String(Integer::ToString(majClassType)));
182                         }
183
184                         pBuf = appsvc_get_data(pBundle, "minor_class");
185                         if (pBuf)
186                         {
187                                 Integer::Decode(String(pBuf), minClassType);
188                                 minClassType = (int)_BluetoothAdapterUtility::ConvertToMinorDeviceClassType((bt_major_device_class_e)majClassType, (bt_minor_device_class_e)minClassType);
189
190                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/minor_class"), new (std::nothrow) String(Integer::ToString(minClassType)));
191                         }
192
193                         pBuf = appsvc_get_data(pBundle, "service_class");
194                         if (pBuf)
195                         {
196                                 Integer::Decode(String(pBuf), svcClassType);
197                                 svcClassType = (int)_BluetoothAdapterUtility::ConvertToServiceClassList(svcClassType);
198
199                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/service_class"), new (std::nothrow) String(Integer::ToString(svcClassType)));
200                         }
201
202                         int len = 0;
203                         char** ppArrayBuf = (char**)appsvc_get_data_array(pBundle, "uuids", &len);
204                         ArrayList* pUuidList = null;
205
206                         if (len > 0 && ppArrayBuf != null)
207                         {
208                                 svcType = _BluetoothAdapterUtility::ConvertToServiceList(ppArrayBuf, len);
209                                 pUuidList = dynamic_cast<ArrayList*>(_BluetoothAdapterUtility::ConvertServiceUuidListN(ppArrayBuf, len));
210
211                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/service_type"), new (std::nothrow) String(Long::ToString(svcType)));
212                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/uuid_list"), pUuidList);
213                         }
214                 }
215                 break;
216         case SERVICE_RESULT_FAILED:
217                 // fall through
218         case SERVICE_RESULT_CANCELED:
219                 // fall through
220         default:
221                 break;
222         }
223
224         _AppControlManager::GetInstance()->FinishAppControl(__req, result, pResult);
225
226 CATCH:
227         __req = -1;
228 }
229
230
231 #ifdef __cplusplus
232 }
233 #endif