2 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
8 * http://floralicense.org/license/
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.
18 #include "bt-main-ug.h"
19 #include "bt-main-view.h"
21 #include "bt-ipc-handler.h"
23 #include <net_connection.h>
25 /**********************************************************************
27 ***********************************************************************/
29 void __bt_cb_profile_state_changed(connection_profile_state_e state, void *user_data)
33 bt_address_t address = { { 0 } };
36 if (user_data == NULL)
39 dev = (bt_dev_t *)user_data;
41 BT_DBG("state: %d", state);
43 memcpy(address.bd_addr, dev->bd_addr, BT_ADDRESS_LENGTH_MAX);
45 if (state == CONNECTION_PROFILE_STATE_CONNECTED) {
46 _bt_ipc_update_connected_status(dev->ugd, BT_NETWORK_CONNECTED,
47 TRUE, BT_UG_ERROR_NONE, &address);
48 } else if (state == CONNECTION_PROFILE_STATE_DISCONNECTED) {
49 _bt_ipc_update_connected_status(dev->ugd, BT_NETWORK_CONNECTED,
50 FALSE, BT_UG_ERROR_NONE, &address);
54 static void __bt_cb_net_opened(connection_error_e result, void* user_data)
62 BT_DBG("result: %d", result);
64 ret_if(user_data == NULL);
66 clone_dev = (bt_dev_t *)user_data;
68 ugd = (bt_ug_data *)clone_dev->ugd;
69 if (clone_dev->ugd == NULL) {
74 dev = _bt_main_get_dev_info_by_address(ugd->paired_device,
83 ugd->connect_req = FALSE;
85 if (dev->status == BT_CONNECTING)
86 dev->status = BT_IDLE;
88 elm_genlist_item_update((Elm_Object_Item *)dev->genlist_item);
94 static void __bt_cb_net_closed(connection_error_e result, void* user_data)
101 BT_DBG("result: %d", result);
103 ret_if(user_data == NULL);
105 dev = (bt_dev_t *)user_data;
109 ugd->connect_req = FALSE;
111 if (dev->status == BT_DISCONNECTING)
112 dev->status = BT_IDLE;
114 elm_genlist_item_update((Elm_Object_Item *)dev->genlist_item);
119 static connection_profile_h __bt_get_net_profile(void *connection,
120 connection_iterator_type_e type,
121 unsigned char *address)
124 gchar **split_string;
125 char net_address[BT_ADDRESS_STR_LEN + 1] = { 0 };
126 char *profile_name = NULL;
127 connection_profile_iterator_h profile_iter;
128 connection_profile_h profile_h;
129 connection_profile_type_e profile_type;
131 retv_if(connection == NULL, NULL);
132 retv_if(address == NULL, NULL);
134 _bt_util_addr_type_to_addr_net_string(net_address, address);
136 result = connection_get_profile_iterator(connection,
139 if (result != CONNECTION_ERROR_NONE) {
140 BT_ERR("Fail to get profile iterator [%d]", result);
144 while (connection_profile_iterator_has_next(profile_iter)) {
149 if (connection_profile_iterator_next(profile_iter,
150 &profile_h) != CONNECTION_ERROR_NONE) {
151 BT_ERR("Fail to get profile handle");
155 if (connection_profile_get_type(profile_h,
156 &profile_type) != CONNECTION_ERROR_NONE) {
157 BT_ERR("Fail to get profile type\n");
161 if (profile_type != CONNECTION_PROFILE_TYPE_BT)
164 if (connection_profile_get_name(profile_h,
165 &profile_name) != CONNECTION_ERROR_NONE) {
166 BT_ERR("Fail to get profile name");
170 split_string = g_strsplit(profile_name, "_", 3);
172 g_free(profile_name);
174 if (g_strv_length(split_string) < 3)
177 if (g_ascii_strcasecmp(split_string[2], net_address) == 0) {
178 BT_DBG("matched profile");
179 g_strfreev(split_string);
183 g_strfreev(split_string);
191 /**********************************************************************
193 ***********************************************************************/
195 int _bt_create_net_connection(void **net_connection)
200 connection_h connection = NULL;
202 result = connection_create(&connection);
204 if (result != CONNECTION_ERROR_NONE ||
205 connection == NULL) {
206 BT_DBG("connection_create() failed: %d", result);
210 *net_connection = connection;
213 return BT_UG_ERROR_NONE;
216 int _bt_destroy_net_connection(void *net_connection)
222 retv_if(net_connection == NULL, BT_UG_FAIL);
224 result = connection_destroy(net_connection);
225 connection_unset_type_changed_cb(net_connection);
229 return (result == CONNECTION_ERROR_NONE) ?
230 BT_UG_ERROR_NONE : BT_UG_FAIL;
233 void _bt_set_profile_state_changed_cb(void *profile, void *user_data)
237 connection_profile_h profile_clone = NULL;
240 ret_if(profile == NULL);
241 ret_if(user_data == NULL);
243 dev = (bt_dev_t *)user_data;
245 if (connection_profile_clone(&profile_clone,
246 profile) != CONNECTION_ERROR_NONE) {
247 BT_ERR("Fail to clone the profile");
251 if (connection_profile_set_state_changed_cb(profile,
252 __bt_cb_profile_state_changed,
253 dev) != CONNECTION_ERROR_NONE) {
254 connection_profile_destroy(profile_clone);
258 dev->net_profile = profile_clone;
263 void _bt_unset_profile_state_changed_cb(void *profile)
267 ret_if(profile == NULL);
268 connection_profile_unset_state_changed_cb(profile);
269 connection_profile_destroy(profile);
274 void *_bt_get_registered_net_profile(void *connection, unsigned char *address)
278 return __bt_get_net_profile(connection,
279 CONNECTION_ITERATOR_TYPE_REGISTERED,
283 void *_bt_get_connected_net_profile(void *connection, unsigned char *address)
287 return __bt_get_net_profile(connection,
288 CONNECTION_ITERATOR_TYPE_CONNECTED,
292 int _bt_connect_net_profile(void *connection, void *profile, void *user_data)
299 connection_wifi_state_e wifi_state;
301 retv_if(connection == NULL, BT_UG_FAIL);
302 retv_if(profile == NULL, BT_UG_FAIL);
303 retv_if(user_data == NULL, BT_UG_FAIL);
305 dev = (bt_dev_t *)user_data;
307 result = connection_get_wifi_state(connection, &wifi_state);
308 if (result != CONNECTION_ERROR_NONE) {
309 BT_ERR("Fail to get wifi state: %d", result);
313 if (wifi_state == CONNECTION_WIFI_STATE_CONNECTED) {
314 BT_ERR("Wifi is connected");
318 /* Fix P121126-0868 */
319 /* 'dev' can be freed, if use try to unbond during connecting NAP */
320 clone_dev = g_malloc0(sizeof(bt_dev_t));
321 g_strlcpy(clone_dev->addr_str, dev->addr_str,
322 BT_ADDRESS_STR_LEN + 1);
323 clone_dev->ugd = dev->ugd;
324 clone_dev->genlist_item = dev->genlist_item;
326 result = connection_open_profile(connection,
331 if (result != CONNECTION_ERROR_NONE) {
332 BT_ERR("Connection open Failed: %d", result);
338 return BT_UG_ERROR_NONE;
341 int _bt_disconnect_net_profile(void *connection, void *profile, void *user_data)
347 retv_if(connection == NULL, BT_UG_FAIL);
348 retv_if(profile == NULL, BT_UG_FAIL);
350 result = connection_close_profile(connection,
355 if (result != CONNECTION_ERROR_NONE) {
356 BT_ERR("Connection close Failed: %d", result);
361 return BT_UG_ERROR_NONE;