Add uuid_list
[platform/framework/native/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 wchar_t __allowedAppControlTable[][2][64] =
58 {
59         {L"osp.appcontrol.BT", L"osp.appcontrol.operation.PICK"},
60         {L"osp.appcontrol.provider.bluetooth", L"osp.appcontrol.operation.pick"},
61         {L"http://tizen.org/appcontrol/provider/bluetooth", L"http://tizen.org/appcontrol/operation/pick"},
62         {L"tizen.bluetooth", L"http://tizen.org/appcontrol/operation/pick"},
63         {L"tizen.bluetooth", L"http://tizen.org/appcontrol/operation/bluetooth/pick"},
64 };
65
66
67 result
68 StartAppControl(int req, const String& aId, const String& oId, const String* pUri, const String* pMime, const IMap* pMap)
69 {
70         SysLog(NID_APP, "Entry to Bluetooth AppControl");
71
72         result r = E_SUCCESS;
73         bool isBtSupported = false;
74         _AppMessageImpl msg;
75
76         r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/network.bluetooth", isBtSupported);
77         SysTryReturnResult(NID_APP, (r == E_SUCCESS) && (isBtSupported == true), E_SYSTEM, "[%s] Bluetooth is not supported.", GetErrorMessage(r));
78
79         const bool isAllowed = _AppControlManager::IsAllowedAppControl(__allowedAppControlTable, 5, aId, oId);
80         SysTryReturnResult(NID_APP, isAllowed, E_SYSTEM, "Invalid AppControl entry for (%ls, %ls).", aId.GetPointer(), oId.GetPointer());
81
82         SysLog(NID_APP, "Bluetooth AppControl.");
83
84         msg.AddData(pMap);
85         msg.AddData(L"launch-type", L"pick");
86
87         __processId = _AppControlManager::GetInstance()->Launch(msg, "ug-bluetooth-efl", APPSVC_OPERATION_PICK, NULL, NULL, OnAppControlResult, 0);
88
89         SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Bluetooth AppControl is failed.");
90         SysLog(NID_APP, "Launching Bluetooth AppControl succeeded.");
91
92         __req = req;
93
94         return r;
95 }
96
97 result
98 TerminateAppControl(int req)
99 {
100         if (__processId >= 0)
101         {
102                 _Aul::TerminateApplicationByPid(__processId);           
103         }
104         return E_SUCCESS;
105 }
106
107 void
108 OnAppControlResult(void* b, int requestCode, service_result_e res, void* userData)
109 {
110         result r = E_SYSTEM;
111         bundle* pBundle = static_cast<bundle*>(b);
112         String dataStr;
113         int majClassType = 0;
114         int minClassType = 0;
115         int svcClassType = 0;
116         unsigned long svcType = 0;
117         int result = SERVICE_RESULT_SUCCEEDED;
118         const char* pBuf;
119
120         HashMap* pResult = new (std::nothrow) HashMap();
121         SysTryCatch(NID_APP, pResult != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
122
123         r = pResult->Construct();
124
125         pBuf = appsvc_get_data(pBundle, "result");
126         if (strcmp(pBuf, "success") == 0)
127         {
128                 result = SERVICE_RESULT_SUCCEEDED;
129         }
130         else
131         {
132                 result = SERVICE_RESULT_FAILED;
133         }
134
135         SysLog(NID_APP, "The service result %s", result == SERVICE_RESULT_SUCCEEDED? "succeeded." : "failed.");
136
137         switch (result)
138         {
139         case SERVICE_RESULT_SUCCEEDED:
140                 {
141                         pBuf = appsvc_get_data(pBundle, "address");
142                         if (pBuf)
143                         {
144                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/address"), new (std::nothrow) String(pBuf));
145                         }
146
147                         pBuf = appsvc_get_data(pBundle, "name");
148                         if (pBuf)
149                         {
150                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/name"), new (std::nothrow) String(pBuf));
151                         }
152
153                         pBuf = appsvc_get_data(pBundle, "rssi");
154                         if (pBuf)
155                         {
156                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/rssi"), new (std::nothrow) String(pBuf));
157                         }
158
159                         pBuf = appsvc_get_data(pBundle, "is_bonded");
160                         if (pBuf)
161                         {
162                                 if (*pBuf == '1')
163                                 {
164                                         pBuf = "true";
165                                 }
166                                 else
167                                 {
168                                         pBuf = "false";
169                                 }
170                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/is_paired"), new (std::nothrow) String(pBuf));
171                         }
172
173                         pBuf = appsvc_get_data(pBundle, "major_class");
174                         if (pBuf)
175                         {
176                                 Integer::Decode(String(pBuf), majClassType);
177                                 majClassType = (int)_BluetoothAdapterUtility::ConvertToMajorDeviceClassType((bt_major_device_class_e)majClassType);
178
179                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/major_class"), new (std::nothrow) String(Integer::ToString(majClassType)));
180                         }
181
182                         pBuf = appsvc_get_data(pBundle, "minor_class");
183                         if (pBuf)
184                         {
185                                 Integer::Decode(String(pBuf), minClassType);
186                                 minClassType = (int)_BluetoothAdapterUtility::ConvertToMinorDeviceClassType((bt_major_device_class_e)majClassType, (bt_minor_device_class_e)minClassType);
187
188                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/minor_class"), new (std::nothrow) String(Integer::ToString(minClassType)));
189                         }
190
191                         pBuf = appsvc_get_data(pBundle, "service_class");
192                         if (pBuf)
193                         {
194                                 Integer::Decode(String(pBuf), svcClassType);
195                                 svcClassType = (int)_BluetoothAdapterUtility::ConvertToServiceClassList(svcClassType);
196
197                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/service_class"), new (std::nothrow) String(Integer::ToString(svcClassType)));
198                         }
199
200                         int len = 0;
201                         char** ppArrayBuf = (char**)appsvc_get_data_array(pBundle, "uuids", &len);
202                         ArrayList* pUuidList = null;
203
204                         if (len > 0 && ppArrayBuf != null)
205                         {
206                                 svcType = _BluetoothAdapterUtility::ConvertToServiceList(ppArrayBuf, len);
207                                 pUuidList = dynamic_cast<ArrayList*>(_BluetoothAdapterUtility::ConvertServiceUuidListN(ppArrayBuf, len));
208                                 for (int i = 0; i < len; i++)
209                                 {
210                                         if (ppArrayBuf[i])
211                                         {
212                                                 free(ppArrayBuf[i]);
213                                         }
214                                 }
215                                 free(ppArrayBuf);
216
217                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/service_type"), new (std::nothrow) String(Long::ToString(svcType)));
218                                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/uuid_list"), pUuidList);
219                         }
220                 }
221                 break;
222         case SERVICE_RESULT_FAILED:
223                 // fall through
224         case SERVICE_RESULT_CANCELED:
225                 // fall through
226         default:
227                 break;
228         }
229
230         _AppControlManager::GetInstance()->FinishAppControl(__req, result, pResult);
231
232 CATCH:
233         __req = -1;
234 }
235
236
237 #ifdef __cplusplus
238 }
239 #endif