Remove the devider of genlist for IOT profile
[apps/native/ug-bluetooth-efl.git] / standard / bt-ipc-handler.c
1 /*
2 * ug-bluetooth-efl
3 *
4 * Copyright 2012 Samsung Electronics Co., Ltd
5 *
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 *           GirishAshok Joshi <girish.joshi@samsung.com>
8 *           DoHyun Pyun <dh79.pyun@samsung.com>
9 *
10 * Licensed under the Flora License, Version 1.1 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.tizenopensource.org/license
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 */
23
24 #include <vconf.h>
25 #include <bluetooth.h>
26 #include <gio/gio.h>
27
28 #include "bt-main-ug.h"
29 #include "bt-main-view.h"
30 #include "bt-ipc-handler.h"
31 #include "bt-string-define.h"
32 #include "bt-debug.h"
33 #include "bt-util.h"
34 #include "bt-widget.h"
35 #include "bt-callback.h"
36
37 static void __bt_on_bus_acquired(GDBusConnection *connection,
38                                 const gchar *name,
39                                 gpointer user_data)
40 {
41         bt_ug_data *ugd = (bt_ug_data *)user_data;
42
43         retm_if(user_data == NULL, "Invalid argument: user_data is NULL");
44         retm_if(connection == NULL, "Invalid argument: connection is NULL");
45
46         ugd->g_conn = connection;
47 }
48
49 static void __bt_on_name_acquired(GDBusConnection *connection,
50                                 const gchar *name,
51                                 gpointer user_data)
52 {
53         BT_DBG("Acquired the name %s on the system bus", name);
54 }
55
56 static void __bt_on_name_lost(GDBusConnection *connection,
57                                 const gchar *name,
58                                 gpointer user_data)
59 {
60         BT_DBG("Lost the name %s on the system bus", name);
61 }
62
63 int _bt_ipc_register_popup_event_signal(bt_ug_data *ugd)
64 {
65         FN_START;
66
67         retvm_if(ugd == NULL, BT_UG_FAIL, "Invalid argument: data is NULL");
68
69         ugd->gdbus_owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
70                                 BT_UG_IPC_INTERFACE,
71                                 G_BUS_NAME_OWNER_FLAGS_NONE,
72                                 __bt_on_bus_acquired,
73                                 __bt_on_name_acquired,
74                                 __bt_on_name_lost,
75                                 ugd,
76                                 NULL);
77         retvm_if(ugd->gdbus_owner_id == 0, BT_UG_FAIL,
78                                         "Failed registering event signal");
79
80         FN_END;
81         return BT_UG_ERROR_NONE;
82 }
83
84 int _bt_ipc_unregister_popup_event_signal(bt_ug_data *ugd)
85 {
86         FN_START;
87
88         retvm_if(ugd == NULL, BT_UG_FAIL, "Invalid argument: data is NULL");
89
90         g_bus_unown_name(ugd->gdbus_owner_id);
91         ugd->gdbus_owner_id = 0;
92
93         FN_END;
94         return BT_UG_ERROR_NONE;
95 }
96
97 int _bt_ipc_send_obex_message(obex_ipc_param_t *param, void *data)
98 {
99         FN_START;
100
101         bt_ug_data *ugd = NULL;
102         GVariantBuilder *filepath_builder;
103         GVariantBuilder *bd_addr;
104         GError *error = NULL;
105         gboolean ret;
106         int i;
107
108         retvm_if(param == NULL, BT_UG_FAIL,
109                  "Invalid argument: param is NULL");
110         retvm_if(data == NULL, BT_UG_FAIL, "Invalid argument: data is NULL");
111
112         ugd = (bt_ug_data *)data;
113
114         bd_addr = g_variant_builder_new(G_VARIANT_TYPE("ay"));
115         for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++)
116                 g_variant_builder_add(bd_addr, "y", param->addr[i]);
117
118         filepath_builder = g_variant_builder_new(G_VARIANT_TYPE("aay"));
119         for (i = 0; i < param->file_cnt; i++)
120                 g_variant_builder_add(filepath_builder, "^ay", param->filepath[i]);
121
122         ret = g_dbus_connection_emit_signal(ugd->g_conn,
123                                         NULL,
124                                         BT_UG_IPC_REQUEST_OBJECT,
125                                         BT_UG_IPC_INTERFACE,
126                                         BT_UG_IPC_METHOD_SEND,
127                                         g_variant_new("(ayssaay)",
128                                                         bd_addr,
129                                                         param->dev_name,
130                                                         param->type,
131                                                         filepath_builder),
132                                         &error);
133
134         if (ret == FALSE) {
135                 BT_ERR("Unable to connect to dbus: %s", error->message);
136                 g_clear_error(&error);
137         }
138
139         g_variant_builder_unref(filepath_builder);
140         g_variant_builder_unref(bd_addr);
141
142         FN_END;
143         return ret ? BT_UG_ERROR_NONE : BT_UG_FAIL;
144 }
145
146 void _bt_ipc_update_connected_status(void *data, int connected_type,
147                                                 bool connected, int result,
148                                                 bt_address_t *addr)
149 {
150         FN_START;
151
152         ret_if(!data);
153         ret_if(!addr);
154         bt_ug_data *ugd = (bt_ug_data *)data;
155         ret_if(ugd->op_status == BT_DEACTIVATING || ugd->op_status == BT_DEACTIVATED);
156
157         bt_dev_t *item = NULL;
158         char addr_str[BT_ADDRESS_STR_LEN + 1] = { 0 };
159
160         _bt_util_addr_type_to_addr_string(addr_str, addr->bd_addr);
161
162         item = _bt_main_get_dev_info_by_address(ugd->paired_device, addr_str);
163
164         if (item == NULL)
165                 item = _bt_main_get_dev_info(ugd->paired_device, ugd->paired_item);
166
167         /* In GATT connection case, connection failed event is recieved before the pairing is completed.
168            So, it can get device info from searched device list */
169         if (TIZEN_FEATURE_BT_HOG && (item == NULL) && (connected_type == BT_LE_HID_CONNECTED))
170                 item = _bt_main_get_dev_info_by_address(ugd->searched_device, addr_str);
171
172         if (item == NULL)
173                 return;
174
175         if (connected == TRUE) {
176                 item->status = BT_IDLE;
177                 item->connected_mask |= (result == BT_UG_ERROR_NONE) ? \
178                         connected_type : 0x00;
179         } else {
180                 if (!ugd->disconn_req)
181                         item->status = BT_IDLE;
182
183                 item->connected_mask &= (result == BT_UG_ERROR_NONE) ? \
184                         ~connected_type : 0xFF;
185         }
186
187         if (item->connected_mask == 0x00) {
188                 item->status = BT_IDLE;
189                 item->is_connected = 0;
190         } else {
191                 item->is_connected = 1;
192         }
193
194         BT_DBG("is_connected : %d, connected_mask : 0x%02x",
195                         item->is_connected, item->connected_mask);
196
197         if (result != BT_UG_ERROR_NONE &&
198                         item->connected_mask == 0x00) {
199                 BT_ERR("Connection Failed");
200                 _bt_update_genlist_item((Elm_Object_Item *)item->genlist_item);
201                 if (!ugd->profile_vd) {
202                         Evas_Object *btn1 = NULL;
203                         Evas_Object *btn2 = NULL;
204                         _bt_main_popup_del_cb(ugd, NULL, NULL);
205
206                         ugd->popup_data.type = BT_POPUP_CONNECTION_ERROR;
207                         ugd->popup_data.data = g_strdup(item->name);
208
209                         if (ugd->connect_req == true) {
210                                 ugd->popup = _bt_create_popup(ugd, NULL, NULL, 0);
211                                 retm_if(ugd->popup == NULL, "fail to create popup!");
212
213                                 btn1 = elm_button_add(ugd->popup);
214                                 elm_object_style_set(btn1, "popup");
215                                 elm_object_domain_translatable_text_set(
216                                         btn1 ,
217                                         PKGNAME, "IDS_BR_SK_CANCEL");
218                                 elm_object_part_content_set(ugd->popup, "button1", btn1);
219                                 evas_object_smart_callback_add(btn1,
220                                                 "clicked", _bt_retry_connection_cb, item);
221
222                                 btn2 = elm_button_add(ugd->popup);
223                                 elm_object_style_set(btn2, "popup");
224                                 elm_object_domain_translatable_text_set(
225                                         btn2 ,
226                                         PKGNAME, "IDS_ST_BUTTON_RETRY");
227                                 elm_object_part_content_set(ugd->popup, "button2", btn2);
228                                 evas_object_smart_callback_add(btn2,
229                                                 "clicked", _bt_retry_connection_cb, item);
230
231                                 evas_object_data_set(ugd->popup, "bd_addr", (void *)item->addr_str);
232                                 eext_object_event_callback_add(ugd->popup,
233                                         EEXT_CALLBACK_BACK, _bt_retry_connection_cb, item);
234                                 evas_object_show(ugd->popup);
235                         }
236                 }
237
238         } else if (item->status == BT_IDLE) {
239                 /* No need to check for connected state as that is handled in _bt_connect_net_profile api */
240                 _bt_sort_paired_device_list(ugd, item, item->is_connected);
241                 _bt_update_genlist_item((Elm_Object_Item *)item->genlist_item);
242         }
243
244         ugd->connect_req = FALSE;
245
246         if (ugd->bt_launch_mode == BT_LAUNCH_CONNECT_HEADSET &&
247                 connected_type == BT_HEADSET_CONNECTED &&
248                 connected == TRUE &&
249                 result == BT_UG_ERROR_NONE) {
250                 BT_DBG("BT_LAUNCH_CONNECT_HEADSET: Connected, destroying UG");
251                 _bt_ug_destroy(ugd, NULL);
252         }
253
254         ret_if(!ugd->profile_vd);
255         BT_DBG("is_connected : %d, connected_mask : 0x%02x",
256                         item->is_connected, item->connected_mask);
257
258         /* Check if the device update and the Profile view device is same */
259         /* Go through the ugd->profile_vd->genlist and check device address */
260         bt_dev_t *dev_info = NULL;
261         Elm_Object_Item *dev_item;
262
263         dev_item = elm_genlist_first_item_get(ugd->profile_vd->genlist);
264
265         if (dev_item == NULL) {
266                 BT_DBG("No item in the list");
267                 return;
268         }
269
270         while (dev_item != NULL) {
271                 dev_info = (bt_dev_t *)elm_object_item_data_get(dev_item);
272
273                 if (dev_info == NULL)
274                         dev_item = elm_genlist_item_next_get(dev_item);
275                 else
276                         break;
277         }
278
279         /* dev_info can be NULL again, so a check is applied */
280         if (dev_info == NULL) {
281                 BT_DBG("No item in the list");
282                 return;
283         }
284
285         /* Match the BD address */
286         if (g_strcmp0(dev_info->addr_str, addr_str) != 0)
287                 return;
288
289         dev_info->call_checked = dev_info->connected_mask & \
290                                 BT_HEADSET_CONNECTED;
291
292 #ifdef TIZEN_BT_A2DP_SINK_ENABLE
293         dev_info->media_checked = dev_info->connected_mask & \
294                                   BT_MUSIC_PLAYER_CONNECTED;
295 #else
296         dev_info->media_checked = dev_info->connected_mask & \
297                                 BT_STEREO_HEADSET_CONNECTED;
298 #endif
299
300         dev_info->hid_checked = dev_info->connected_mask & \
301                                 BT_HID_CONNECTED;
302
303         dev_info->network_checked = dev_info->connected_mask & \
304                                 BT_NETWORK_CONNECTED;
305
306         _bt_util_set_list_disabled(ugd->profile_vd->genlist,
307                                 EINA_FALSE);
308         FN_END;
309 }