wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Bluetooth / BluetoothCallbackUtil.cpp
1 //
2 // Tizen Web Device API
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 #include <Ecore.h>
19
20 #include <JSUtil.h>
21 #include <JSWebAPIError.h>
22
23 #include "BluetoothCallbackUtil.h"
24 #include "BluetoothAdapter.h"
25
26 #include <Logger.h>
27
28 using namespace DeviceAPI::Common;
29
30 namespace DeviceAPI {
31 namespace Bluetooth {
32
33 static Eina_Bool jobCompleteCB(void *userData){
34     LoggerD("Entered");
35
36     BluetoothCallbackUserDataPtr data = static_cast<BluetoothCallbackUserDataPtr>(userData);
37
38     if(!data) {
39         LoggerW("BluetoothCallbackUserDataPtr is NULL");
40         return false;
41     }
42
43     if(!(data->mUserData)) {
44         LoggerW("MulticallbackUserData is NULL");
45         delete data;
46         return false;            
47     }
48
49     switch(data->mCBType) {
50         case BluetoothCallbackUserData::BT_CB_SUCCESS:
51         {
52             LoggerD("BT_CB_SUCCESS");
53             data->mUserData->invokeCallback("success");
54             break;
55         }
56         case BluetoothCallbackUserData::BT_CB_ERROR:
57         {
58             LoggerD("BT_CB_ERROR");
59             data->mUserData->invokeCallback("error", JSWebAPIError::makeJSWebAPIError(data->mUserData->getContext(), *(data->mError)));
60             break;
61         }
62         case BluetoothCallbackUserData::BT_CB_DEVICE:
63         {
64             LoggerD("BT_CB_DEVICE");
65             BluetoothAdapter::getInstance()->returnDevice(data->mAddress, data->mUserData);
66             break;      
67         }
68         case BluetoothCallbackUserData::BT_CB_DEVICES:
69         {
70             LoggerD("BT_CB_DEVICES");
71             BluetoothAdapter::getInstance()->returnKnownDevices(data->mUserData);
72             break;
73         }
74         case BluetoothCallbackUserData::BT_CB_SERVICE:
75         {
76             LoggerD("BT_CB_SERVICE");
77             BluetoothAdapter::getInstance()->returnRegisteredService(data->mUUID, data->mName, data->mUserData);
78             break;
79         }
80         default:
81         {
82             LoggerW("Unknown callback type");
83         }
84     }
85
86     delete data;
87     return false;
88 }
89
90 void BluetoothCallbackUtil::syncToAsyncSuccessCallback(Common::MultiCallbackUserDataPtr userData)
91 {
92     BluetoothCallbackUserDataPtr data = new BluetoothCallbackUserData(userData, BluetoothCallbackUserData::BT_CB_SUCCESS);
93     ecore_idler_add(jobCompleteCB, data);
94 }
95
96 void BluetoothCallbackUtil::syncToAsyncErrorCallback(DeviceAPI::Common::MultiCallbackUserDataPtr userData, Common::BasePlatformException *error)
97 {
98     BluetoothCallbackUserDataPtr data = new BluetoothCallbackUserData(userData, error);
99     ecore_idler_add(jobCompleteCB, data);
100 }
101
102 void BluetoothCallbackUtil::syncToAsyncDeviceCallback(Common::MultiCallbackUserDataPtr userData, std::string &address)
103 {
104     BluetoothCallbackUserDataPtr data = new BluetoothCallbackUserData(userData, address);
105     ecore_idler_add(jobCompleteCB, data);
106 }
107
108 void BluetoothCallbackUtil::syncToAsyncDeviceArrayCallback(Common::MultiCallbackUserDataPtr userData)
109 {
110     BluetoothCallbackUserDataPtr data = new BluetoothCallbackUserData(userData, BluetoothCallbackUserData::BT_CB_DEVICES);
111     ecore_idler_add(jobCompleteCB, data);
112 }
113
114 void BluetoothCallbackUtil::syncToAsyncServiceCallback(Common::MultiCallbackUserDataPtr userData, std::string &uuid, std::string &name)
115 {
116     BluetoothCallbackUserDataPtr data = new BluetoothCallbackUserData(userData, uuid, name);
117     ecore_idler_add(jobCompleteCB, data);
118 }
119
120
121 } // Bluetooth
122 } // DeviceAPI