Tizen 2.1 base
[apps/native/ug-bluetooth-efl.git] / src / libraries / bt-ipc-handler.c
1 /*
2  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <vconf.h>
18 #include <bluetooth.h>
19
20 #include "bt-main-ug.h"
21 #include "bt-main-view.h"
22 #include "bt-ipc-handler.h"
23 #include "bt-string-define.h"
24 #include "bt-debug.h"
25 #include "bt-util.h"
26
27 /**********************************************************************
28 *                                                 Static Functions
29 ***********************************************************************/
30
31 int _bt_ipc_init_event_signal(void *data)
32 {
33         FN_START;
34
35         bt_ug_data *ugd;
36         E_DBus_Connection *conn;
37
38         retvm_if(data == NULL, BT_UG_FAIL, "Invalid argument: data is NULL\n");
39
40         ugd = (bt_ug_data *)data;
41         retvm_if(ugd->EDBusHandle != NULL, BT_UG_FAIL,
42                 "Invalid argument: ugd->EDBusHandle already exist\n");
43
44         e_dbus_init();
45
46         conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
47         retvm_if(conn == NULL, BT_UG_FAIL, "conn is NULL\n");
48
49         e_dbus_request_name(conn, BT_UG_IPC_INTERFACE, 0, NULL, NULL);
50
51         ugd->EDBusHandle = conn;
52
53         FN_END;
54         return TRUE;
55 }
56
57 int _bt_ipc_deinit_event_signal(void *data)
58 {
59         FN_START;
60
61         bt_ug_data *ugd;
62
63         retvm_if(data == NULL, BT_UG_FAIL, "Invalid argument: data is NULL\n");
64
65         ugd = (bt_ug_data *)data;
66         retvm_if(ugd->EDBusHandle == NULL, BT_UG_FAIL,
67                 "Invalid argument: ugd->EDBusHandle is NULL\n");
68
69         FN_END;
70         return TRUE;
71 }
72
73 static void __bt_ipc_receive_popup_event(void *data, DBusMessage * msg)
74 {
75         FN_START;
76
77         int response;
78         char *member = NULL;
79         bt_ug_data *ugd = NULL;
80
81         retm_if(data == NULL, "Invalid argument: data is NULL\n");
82         retm_if(msg == NULL, "Invalid argument: msg is NULL\n");
83
84         ugd = (bt_ug_data *)data;
85
86         member = (char *)dbus_message_get_member(msg);
87         retm_if(member == NULL, "dbus signal member get failed\n");
88
89         if (!strcmp(member, BT_SYSPOPUP_METHOD_RESPONSE)) {
90                 if (!dbus_message_get_args(msg, NULL,
91                                            DBUS_TYPE_INT32, &response,
92                                            DBUS_TYPE_INVALID)) {
93                         BT_DBG("User Event handling for [%s] failed\n", member);
94                         return;
95                 } else {
96                         BT_DBG("Success User Event handling response = %d ",
97                                response);
98                         switch (ugd->confirm_req) {
99                         case BT_CONNECTION_REQ:
100                                 _bt_main_retry_connection(data, response);
101                                 break;
102
103                         case BT_PAIRING_REQ:
104                                 /* response - 0: Yes, 1: No */
105                                 _bt_main_retry_pairing(data, response);
106                                 break;
107
108                         case BT_NONE_REQ:
109                         default:
110                                 BT_DBG("Unidentified request\n");
111                                 break;
112                         }
113                 }
114         }
115
116         _bt_ipc_unregister_popup_event_signal(ugd->EDBusHandle, data);
117 }
118
119 /**********************************************************************
120 *                                                Common Functions
121 ***********************************************************************/
122
123 int _bt_ipc_register_popup_event_signal(E_DBus_Connection *conn, void *data)
124 {
125         FN_START;
126
127         bt_ug_data *ugd = NULL;
128         E_DBus_Signal_Handler *sh = NULL;
129
130         retvm_if(conn == NULL, BT_UG_FAIL, "Invalid argument: conn is NULL\n");
131         retvm_if(data == NULL, BT_UG_FAIL, "Invalid argument: data is NULL\n");
132
133         ugd = (bt_ug_data *)data;
134
135         sh = e_dbus_signal_handler_add(conn,
136                                        NULL,
137                                        BT_SYSPOPUP_IPC_RESPONSE_OBJECT,
138                                        BT_SYSPOPUP_INTERFACE,
139                                        BT_SYSPOPUP_METHOD_RESPONSE,
140                                        __bt_ipc_receive_popup_event, data);
141
142         retvm_if(sh == NULL, BT_UG_FAIL, "AG Response Event register failed\n");
143
144         ugd->popup_sh = sh;
145
146         FN_END;
147         return BT_UG_ERROR_NONE;
148 }
149
150 int _bt_ipc_unregister_popup_event_signal(E_DBus_Connection *conn, void *data)
151 {
152         FN_START;
153
154         bt_ug_data *ugd = NULL;
155
156         retvm_if(conn == NULL, BT_UG_FAIL, "Invalid argument: conn is NULL\n");
157         retvm_if(data == NULL, BT_UG_FAIL, "Invalid argument: data is NULL\n");
158
159         ugd = (bt_ug_data *)data;
160
161         retvm_if(ugd->popup_sh == NULL, BT_UG_FAIL, "Signal Handler is NULL\n");
162
163         e_dbus_signal_handler_del(conn, ugd->popup_sh);
164
165         ugd->popup_sh = NULL;
166
167         FN_END;
168         return BT_UG_ERROR_NONE;
169 }
170
171 int _bt_ipc_send_obex_message(obex_ipc_param_t *param, void *data)
172 {
173         FN_START;
174
175         bt_ug_data *ugd = NULL;
176         DBusMessage *msg = NULL;
177         char *param_str = NULL;
178         DBusPendingCall *ret = NULL;
179
180         retvm_if(param == NULL, BT_UG_FAIL,
181                  "Invalid argument: param is NULL\n");
182         retvm_if(data == NULL, BT_UG_FAIL, "Invalid argument: data is NULL\n");
183
184         ugd = (bt_ug_data *)data;
185         param_str = param->param2;
186         retvm_if(ugd->EDBusHandle == NULL, BT_UG_FAIL,
187                  "Invalid argument: ugd->EDBusHandle is NULL\n");
188         retvm_if(param_str == NULL, BT_UG_FAIL,
189                  "Invalid argument: param_str is NULL\n");
190
191         BT_DBG("Request to connect [%d]\n", param->param1);
192         BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", param->param2[0],
193                param->param2[1], param->param2[2], param->param2[3],
194                param->param2[4], param->param2[5]);
195
196         msg = dbus_message_new_signal(BT_UG_IPC_REQUEST_OBJECT,
197                                                       BT_UG_IPC_INTERFACE,
198                                                       BT_UG_IPC_METHOD_SEND);
199
200         retvm_if(msg == NULL, BT_UG_FAIL, "msg is NULL\n");
201
202         if (!dbus_message_append_args(msg,
203                                       DBUS_TYPE_INT32, &param->param1,
204                                       DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
205                                       &param_str, BT_ADDRESS_LENGTH_MAX,
206                                       DBUS_TYPE_INT32, &param->param3,
207                                       DBUS_TYPE_STRING, &param->param4,
208                                       DBUS_TYPE_STRING, &param->param5,
209                                       DBUS_TYPE_STRING, &param->param6,
210                                       DBUS_TYPE_INVALID)) {
211                 BT_DBG("Connect sending failed\n");
212                 dbus_message_unref(msg);
213                 return BT_UG_FAIL;
214         }
215
216         ret = e_dbus_message_send(ugd->EDBusHandle, msg, NULL, -1, NULL);
217         dbus_message_unref(msg);
218
219         FN_END;
220         return ret ? BT_UG_ERROR_NONE : BT_UG_FAIL;
221 }
222
223 void _bt_ipc_update_connected_status(bt_ug_data *ugd, int connected_type,
224                                                 bool connected, int result,
225                                                 bt_address_t *addr)
226 {
227         FN_START;
228
229         bt_dev_t *item = NULL;
230         char addr_str[BT_ADDRESS_STR_LEN + 1] = { 0 };
231
232         _bt_util_addr_type_to_addr_string(addr_str, addr->bd_addr);
233
234         item = _bt_main_get_dev_info_by_address(ugd->paired_device, addr_str);
235
236         if (item == NULL)
237                 item = _bt_main_get_dev_info(ugd->paired_device, ugd->paired_item);
238
239         ugd->connect_req = FALSE;
240
241         if (item != NULL) {
242                 item->status = BT_IDLE;
243
244                 if (connected == TRUE) {
245                         item->connected_mask |= (result == BT_UG_ERROR_NONE) ? \
246                                 connected_type : 0x00;
247                 } else {
248                         item->connected_mask &= (result == BT_UG_ERROR_NONE) ? \
249                                 ~connected_type : 0xFF;
250                 }
251
252                 elm_genlist_item_update((Elm_Object_Item *)item->genlist_item);
253
254                 if (ugd->profile_vd && ugd->profile_vd->genlist) {
255                         item->call_checked = item->connected_mask & \
256                                                 BT_HEADSET_CONNECTED;
257
258                         item->media_checked = item->connected_mask & \
259                                                 BT_STEREO_HEADSET_CONNECTED;
260
261                         item->hid_checked = item->connected_mask & \
262                                                 BT_HID_CONNECTED;
263
264                         item->network_checked = item->connected_mask & \
265                                                 BT_NETWORK_CONNECTED;
266
267                         _bt_util_set_list_disabled(ugd->profile_vd->genlist,
268                                                 EINA_FALSE);
269                 }
270         }
271
272         FN_END;
273 }