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