4 * Copyright 2012 Samsung Electronics Co., Ltd
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * GirishAshok Joshi <girish.joshi@samsung.com>
8 * DoHyun Pyun <dh79.pyun@samsung.com>
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
14 * http://www.tizenopensource.org/license
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.
25 #include "bt-main-ug.h"
26 #include "bt-main-view.h"
28 #include "bt-ipc-handler.h"
29 #include "bt-widget.h"
30 #include "bt-net-connection.h"
31 #include "bt-string-define.h"
32 #include "bt-callback.h"
34 /**********************************************************************
36 ***********************************************************************/
38 void __bt_cb_profile_state_changed(connection_profile_state_e state, void *user_data)
42 bt_address_t address = { { 0 } };
47 dev = (bt_dev_t *)user_data;
48 bt_ug_data *ugd = dev->ugd;
50 ret_if(ugd->op_status == BT_DEACTIVATING || ugd->op_status == BT_DEACTIVATED);
52 BT_DBG("profile state: %d", state);
54 memcpy(address.bd_addr, dev->bd_addr, BT_ADDRESS_LENGTH_MAX);
56 if (state == CONNECTION_PROFILE_STATE_CONNECTED) {
57 _bt_ipc_update_connected_status(dev->ugd, BT_NETWORK_CONNECTED,
58 TRUE, dev->pan_connection_result, &address);
59 } else if (state == CONNECTION_PROFILE_STATE_DISCONNECTED) {
60 _bt_ipc_update_connected_status(dev->ugd, BT_NETWORK_CONNECTED,
61 FALSE, dev->pan_connection_result, &address);
66 static void __bt_cb_net_opened(connection_error_e result, void* user_data)
69 bt_address_t address = { { 0 } };
74 dev = (bt_dev_t *)user_data;
75 bt_ug_data *ugd = dev->ugd;
78 BT_DBG("result: %d", result);
81 dev->pan_connection_result = BT_UG_FAIL;
82 memcpy(address.bd_addr, dev->bd_addr, BT_ADDRESS_LENGTH_MAX);
83 _bt_ipc_update_connected_status(dev->ugd, BT_NETWORK_CONNECTED,
84 FALSE, dev->pan_connection_result, &address);
86 dev->pan_connection_result = BT_UG_ERROR_NONE;
92 static void __bt_cb_net_closed(connection_error_e result, void* user_data)
99 dev = (bt_dev_t *)user_data;
100 bt_ug_data *ugd = dev->ugd;
103 BT_DBG("result: %d", result);
106 dev->pan_connection_result = BT_UG_FAIL;
108 dev->pan_connection_result = BT_UG_ERROR_NONE;
113 static void *__bt_get_net_profile_list(void *connection,
114 connection_iterator_type_e type)
117 Eina_List *ret_list = NULL;
118 gchar **split_string;
119 char *profile_name = NULL;
120 connection_profile_iterator_h profile_iter;
121 connection_profile_h profile_h;
122 connection_profile_type_e profile_type;
124 retv_if(connection == NULL, NULL);
126 result = connection_get_profile_iterator(connection,
129 if (result != CONNECTION_ERROR_NONE) {
130 BT_ERR("Fail to get profile iterator [%d]", result);
134 while (connection_profile_iterator_has_next(profile_iter)) {
139 if (connection_profile_iterator_next(profile_iter,
140 &profile_h) != CONNECTION_ERROR_NONE) {
141 BT_ERR("Fail to get profile handle");
145 if (connection_profile_get_type(profile_h,
146 &profile_type) != CONNECTION_ERROR_NONE) {
147 BT_ERR("Fail to get profile type");
151 if (profile_type != CONNECTION_PROFILE_TYPE_BT)
154 if (connection_profile_get_name(profile_h,
155 &profile_name) != CONNECTION_ERROR_NONE) {
156 BT_ERR("Fail to get profile name");
160 split_string = (gchar **)g_strsplit(profile_name, "_", 3);
162 g_free(profile_name);
164 if (g_strv_length(split_string) < 3)
167 bt_net_profile_t *profile_data =
168 (bt_net_profile_t *)g_malloc0(sizeof(bt_net_profile_t));
170 if (profile_data == NULL)
173 profile_data->profile_h = profile_h;
174 profile_data->address = (unsigned char *)g_strdup(split_string[2]);
175 ret_list = eina_list_append(ret_list, profile_data);
176 g_strfreev(split_string);
184 static connection_profile_h __bt_get_net_profile(void *connection,
185 connection_iterator_type_e type,
186 unsigned char *address)
189 gchar **split_string;
190 char net_address[BT_ADDRESS_STR_LEN + 1] = { 0 };
191 char *profile_name = NULL;
192 connection_profile_iterator_h profile_iter;
193 connection_profile_h profile_h;
194 connection_profile_type_e profile_type;
196 retv_if(connection == NULL, NULL);
197 retv_if(address == NULL, NULL);
199 _bt_util_addr_type_to_addr_net_string(net_address, address);
201 result = connection_get_profile_iterator(connection,
204 if (result != CONNECTION_ERROR_NONE) {
205 BT_ERR("Fail to get profile iterator [%d]", result);
209 while (connection_profile_iterator_has_next(profile_iter)) {
214 if (connection_profile_iterator_next(profile_iter,
215 &profile_h) != CONNECTION_ERROR_NONE) {
216 BT_ERR("Fail to get profile handle");
220 if (connection_profile_get_type(profile_h,
221 &profile_type) != CONNECTION_ERROR_NONE) {
222 BT_ERR("Fail to get profile type");
226 if (profile_type != CONNECTION_PROFILE_TYPE_BT)
229 if (connection_profile_get_name(profile_h,
230 &profile_name) != CONNECTION_ERROR_NONE) {
231 BT_ERR("Fail to get profile name");
235 split_string = g_strsplit(profile_name, "_", 3);
237 g_free(profile_name);
239 if (g_strv_length(split_string) < 3)
242 if (g_ascii_strcasecmp(split_string[2], net_address) == 0) {
243 BT_DBG("matched profile");
244 g_strfreev(split_string);
248 g_strfreev(split_string);
256 /**********************************************************************
258 ***********************************************************************/
260 int _bt_create_net_connection(void **net_connection)
265 connection_h connection = NULL;
267 result = connection_create(&connection);
269 if (result != CONNECTION_ERROR_NONE ||
270 connection == NULL) {
271 BT_ERR("connection_create() failed: %d", result);
272 *net_connection = NULL;
276 *net_connection = connection;
279 return BT_UG_ERROR_NONE;
282 int _bt_destroy_net_connection(void *net_connection)
288 retv_if(net_connection == NULL, BT_UG_FAIL);
290 connection_unset_type_changed_cb(net_connection);
291 result = connection_destroy(net_connection);
295 return (result == CONNECTION_ERROR_NONE) ?
296 BT_UG_ERROR_NONE : BT_UG_FAIL;
299 void _bt_set_profile_state_changed_cb(void *profile, void *user_data)
303 connection_profile_h profile_clone = NULL;
306 ret_if(profile == NULL);
307 ret_if(user_data == NULL);
309 dev = (bt_dev_t *)user_data;
311 if (connection_profile_clone(&profile_clone,
312 profile) != CONNECTION_ERROR_NONE) {
313 BT_ERR("Fail to clone the profile");
317 if (connection_profile_set_state_changed_cb(profile,
318 __bt_cb_profile_state_changed,
319 dev) != CONNECTION_ERROR_NONE) {
320 connection_profile_destroy(profile_clone);
324 dev->net_profile = profile_clone;
329 void _bt_unset_profile_state_changed_cb(void *profile)
333 ret_if(profile == NULL);
334 connection_profile_unset_state_changed_cb(profile);
335 connection_profile_destroy(profile);
340 void *_bt_get_registered_net_profile(void *connection, unsigned char *address)
344 return __bt_get_net_profile(connection,
345 CONNECTION_ITERATOR_TYPE_REGISTERED,
349 void *_bt_get_registered_net_profile_list(void *connection)
353 return __bt_get_net_profile_list(connection,
354 CONNECTION_ITERATOR_TYPE_REGISTERED);
357 void _bt_free_net_profile_list(void *list)
362 bt_net_profile_t *profile_data = NULL;
364 EINA_LIST_FOREACH(list, l, profile_data) {
365 if (profile_data && profile_data->address)
366 g_free(profile_data->address);
368 eina_list_free(list);
371 int _bt_connect_net_profile(void *connection, void *profile, void *user_data)
377 connection_wifi_state_e wifi_state;
379 retv_if(connection == NULL, BT_UG_FAIL);
380 retv_if(profile == NULL, BT_UG_FAIL);
381 retv_if(user_data == NULL, BT_UG_FAIL);
383 dev = (bt_dev_t *)user_data;
384 result = connection_get_wifi_state(connection, &wifi_state);
385 if (result != CONNECTION_ERROR_NONE) {
386 BT_ERR("connection_get_wifi_state Failed: %d", result);
390 if (wifi_state == CONNECTION_WIFI_STATE_CONNECTED) {
391 bt_address_t address = { { 0 } };
394 BT_INFO("wifi_state: %d", wifi_state);
395 result = notification_status_message_post(BT_STR_BLUETOOTH_TETHERING_CONNECTION_ERROR);
396 if (result != NOTIFICATION_ERROR_NONE)
397 BT_ERR("notification_status_message_post() ERROR [%d]", result);
399 /* Set PAN Connection as Disconnected to Update the Checkbox*/
400 _bt_util_addr_string_to_addr_type(address.bd_addr, (const char *)&dev->addr_str);
401 BT_DBG("Updating Checkbox as not connected");
402 dev->network_checked = dev->connected_mask & BT_NETWORK_CONNECTED;
403 bt_ug_data * ugd = dev->ugd;
404 if (ugd->profile_vd == NULL) {
405 BT_ERR("Profile view NULL");
408 /* No need to change connect_req and ugd status as they are set only when
409 * this api is sussessfull */
410 BT_DBG("As we didn't Initiated Connection only update the list");
411 _bt_util_set_list_disabled(ugd->profile_vd->genlist,
418 /* Fix P121126-0868 */
419 /* 'dev' can be freed, if use try to unbond during connecting NAP */
420 /*clone_dev = g_malloc0(sizeof(bt_dev_t));
421 g_strlcpy(clone_dev->addr_str, dev->addr_str,
422 BT_ADDRESS_STR_LEN + 1);
423 clone_dev->ugd = dev->ugd;
424 clone_dev->genlist_item = dev->genlist_item;*/
426 result = connection_open_profile(connection,
431 if (result != CONNECTION_ERROR_NONE) {
432 BT_ERR("Connection open Failed: %d", result);
437 return BT_UG_ERROR_NONE;
440 int _bt_disconnect_net_profile(void *connection, void *profile, void *user_data)
446 retv_if(connection == NULL, BT_UG_FAIL);
447 retv_if(profile == NULL, BT_UG_FAIL);
449 result = connection_close_profile(connection,
454 if (result != CONNECTION_ERROR_NONE) {
455 BT_ERR("Connection close Failed: %d", result);
460 return BT_UG_ERROR_NONE;