3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <dbus/dbus.h>
21 #include <dbus/dbus-glib.h>
22 #include <dbus/dbus-glib-lowlevel.h>
23 #include <bluetooth.h>
25 #include "mobileap_softap.h"
26 #include "mobileap_common.h"
27 #include "mobileap_bluetooth.h"
28 #include "mobileap_handler.h"
29 #include "mobileap_notification.h"
32 bt_device_info_s *info;
34 const in_addr_t intf_ip;
35 } __bt_remote_device_s;
37 static __bt_remote_device_s __bt_remote_devices[MOBILE_AP_MAX_BT_STA] = {
38 {NULL, NULL, IP_ADDRESS_BT_1},
39 {NULL, NULL, IP_ADDRESS_BT_2},
40 {NULL, NULL, IP_ADDRESS_BT_3},
41 {NULL, NULL, IP_ADDRESS_BT_4}};
43 static DBusGMethodInvocation *g_context = NULL;
45 static void __bt_nap_connection_changed(bool connected, const char *remote_address,
46 const char *interface_name, void *user_data);
47 static void __bt_adapter_state_changed(int result, bt_adapter_state_e adapter_state, void *user_data);
48 static void __handle_bt_adapter_visibility();
50 int __recheck_bt_adapter_timer = 0;
52 static __bt_remote_device_s *__find_bt_remote(const char *mac)
56 for (i = 0; i < MOBILE_AP_MAX_BT_STA; i++) {
57 if (__bt_remote_devices[i].info == NULL)
60 if (!g_ascii_strcasecmp(__bt_remote_devices[i].info->remote_address, mac))
64 if (i == MOBILE_AP_MAX_BT_STA) {
65 SERR("Not found : %s\n", mac);
69 return &__bt_remote_devices[i];
72 static __bt_remote_device_s *__add_bt_remote(bt_device_info_s *info, const char *intf_name)
76 for (i = 0; i < MOBILE_AP_MAX_BT_STA; i++) {
77 if (__bt_remote_devices[i].info == NULL)
81 if (i == MOBILE_AP_MAX_BT_STA) {
82 ERR("Too many BT devices are connected\n");
86 __bt_remote_devices[i].intf_name = g_strdup(intf_name);
87 if (__bt_remote_devices[i].intf_name == NULL) {
88 ERR("Memory allocation failed\n");
92 _add_interface_routing(__bt_remote_devices[i].intf_name,
93 __bt_remote_devices[i].intf_ip);
94 _add_routing_rule(__bt_remote_devices[i].intf_name);
95 __bt_remote_devices[i].info = info;
97 return &__bt_remote_devices[i];
100 static gboolean __del_bt_remote(const char *mac)
104 for (i = 0; i < MOBILE_AP_MAX_BT_STA; i++) {
105 if (__bt_remote_devices[i].info == NULL)
108 if (!g_ascii_strcasecmp(__bt_remote_devices[i].info->remote_address, mac))
112 if (i == MOBILE_AP_MAX_BT_STA) {
113 SERR("Not found : %s\n", mac);
117 _del_routing_rule(__bt_remote_devices[i].intf_name);
118 _del_interface_routing(__bt_remote_devices[i].intf_name,
119 __bt_remote_devices[i].intf_ip);
120 bt_adapter_free_device_info(__bt_remote_devices[i].info);
121 g_free(__bt_remote_devices[i].intf_name);
123 __bt_remote_devices[i].info = NULL;
124 __bt_remote_devices[i].intf_name = NULL;
129 static void __del_bt_remote_all(void)
133 for (i = 0; i < MOBILE_AP_MAX_BT_STA; i++) {
134 if (__bt_remote_devices[i].info) {
135 bt_adapter_free_device_info(__bt_remote_devices[i].info);
136 __bt_remote_devices[i].info = NULL;
139 if (__bt_remote_devices[i].intf_name) {
140 _del_routing_rule(__bt_remote_devices[i].intf_name);
141 _del_interface_routing(__bt_remote_devices[i].intf_name,
142 __bt_remote_devices[i].intf_ip);
143 g_free(__bt_remote_devices[i].intf_name);
144 __bt_remote_devices[i].intf_name = NULL;
151 static mobile_ap_error_code_e __init_bt(TetheringObject *obj)
155 ret = bt_initialize();
156 if (ret != BT_ERROR_NONE) {
157 ERR("bt_initialize is failed : %d\n", ret);
158 return MOBILE_AP_ERROR_RESOURCE;
161 ret = bt_adapter_set_state_changed_cb(__bt_adapter_state_changed, (void *)obj);
162 if (ret != BT_ERROR_NONE) {
163 ERR("bt_adapter_set_state_changed_cb is failed : %d\n", ret);
165 return MOBILE_AP_ERROR_RESOURCE;
168 return MOBILE_AP_ERROR_NONE;
171 static void __deinit_bt(void)
175 ret = bt_adapter_unset_state_changed_cb();
176 if (ret != BT_ERROR_NONE)
177 ERR("bt_adapter_unset_state_changed_cb is failed : %d\n", ret);
179 ret = bt_deinitialize();
180 if (ret != BT_ERROR_NONE)
181 ERR("bt_deinitialize is failed : %d\n", ret);
186 static gboolean __is_bt_adapter_on(void)
189 bt_adapter_state_e adapter_state = BT_ADAPTER_DISABLED;
191 ret = bt_adapter_get_state(&adapter_state);
192 if (ret != BT_ERROR_NONE) {
193 ERR("bt_adapter_get_state is failed : %d\n", ret);
197 if (adapter_state == BT_ADAPTER_ENABLED)
203 gboolean __bt_adapter_timeout_cb(gpointer data)
207 static int retry_count = 0;
208 if (__is_bt_adapter_on() == TRUE) {
209 DBG("BT Adapter is enabled by other process \n");
214 if (++retry_count >= PS_RECHECK_COUNT_MAX) {
216 ERR("_enable_bt_tethering() is failed because of bt_adapter_eanbled() failed:n");
217 _mobileap_clear_state(MOBILE_AP_STATE_BT);
219 dbus_g_method_return(g_context,
220 MOBILE_AP_ENABLE_BT_TETHERING_CFM, MOBILE_AP_ERROR_INTERNAL);
222 _unblock_device_sleep();
232 static mobile_ap_error_code_e __turn_on_bt_adapter(TetheringObject *obj)
236 ret = bt_adapter_enable();
237 if (ret == BT_ERROR_NOW_IN_PROGRESS) {
238 if (__recheck_bt_adapter_timer) {
239 g_source_remove(__recheck_bt_adapter_timer);
241 __recheck_bt_adapter_timer = g_timeout_add(PS_RECHECK_INTERVAL,
242 __bt_adapter_timeout_cb, NULL);
243 return MOBILE_AP_ERROR_NONE;
246 if (ret != BT_ERROR_NONE && ret != BT_ERROR_ALREADY_DONE) {
247 ERR("bt_adapter_enable is failed : %d\n", ret);
248 if (ret == BT_ERROR_PERMISSION_DENIED)
249 return MOBILE_AP_ERROR_PERMISSION_DENIED;
251 return MOBILE_AP_ERROR_RESOURCE;
254 return MOBILE_AP_ERROR_NONE;
257 static mobile_ap_error_code_e __turn_on_bt_nap(TetheringObject *obj)
259 int bt_ret = BT_ERROR_NONE;
261 bt_ret = bt_nap_set_connection_state_changed_cb(__bt_nap_connection_changed, (void *)obj);
262 if (bt_ret != BT_ERROR_NONE) {
263 ERR("bt_nap_set_connection_state_changed_cb is failed : %d\n", bt_ret);
264 return MOBILE_AP_ERROR_RESOURCE;
267 bt_ret = bt_nap_activate();
268 if (bt_ret != BT_ERROR_NONE && bt_ret != BT_ERROR_ALREADY_DONE) {
269 bt_nap_unset_connection_state_changed_cb();
270 ERR("bt_nap_activate is failed : %d\n", bt_ret);
271 if (bt_ret == BT_ERROR_PERMISSION_DENIED)
272 return MOBILE_AP_ERROR_PERMISSION_DENIED;
274 return MOBILE_AP_ERROR_RESOURCE;
277 return MOBILE_AP_ERROR_NONE;
280 static void __turn_off_bt_nap(void)
284 bt_ret = bt_nap_disconnect_all();
285 if (bt_ret != BT_ERROR_NONE)
286 ERR("bt_nap_disconnect_all is failed : %d\n", bt_ret);
288 DBG("bt_nap_disconnect_all is called\n");
290 bt_ret = bt_nap_unset_connection_state_changed_cb();
291 if (bt_ret != BT_ERROR_NONE)
292 ERR("bt_nap_unset_connection_state_changed_cb is failed : %d\n", bt_ret);
297 mobile_ap_error_code_e _enable_bt_tethering(TetheringObject *obj)
299 mobile_ap_error_code_e ret = MOBILE_AP_ERROR_NONE;
302 if (__recheck_bt_adapter_timer) {
303 g_source_remove(__recheck_bt_adapter_timer);
304 __recheck_bt_adapter_timer = 0;
307 if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP)) {
308 ERR("Wi-Fi AP is enabled\n");
309 return MOBILE_AP_ERROR_RESOURCE;
312 ret = _init_tethering(obj);
313 if (ret != MOBILE_AP_ERROR_NONE) {
317 ret = __turn_on_bt_nap(obj);
318 if (ret != MOBILE_AP_ERROR_NONE) {
319 _deinit_tethering(obj);
322 _delete_timeout_noti();
323 _init_timeout_cb(MOBILE_AP_TYPE_BT, (void *)obj);
324 _start_timeout_cb(MOBILE_AP_TYPE_BT, time(NULL) + TETHERING_CONN_TIMEOUT);
329 mobile_ap_error_code_e _disable_bt_tethering(TetheringObject *obj)
331 int ret = BT_ERROR_NONE;
332 if (!_mobileap_is_enabled(MOBILE_AP_STATE_BT)) {
333 ERR("BT tethering has not been enabled\n");
334 return MOBILE_AP_ERROR_NOT_ENABLED;
336 ret = bt_adapter_unset_visibility_mode_changed_cb();
337 if (ret != BT_ERROR_NONE)
338 ERR("bt_adapter_unset_visibility_mode_changed_cb is failed : %d\n", ret);
340 _block_device_sleep();
341 if (__is_bt_adapter_on()) {
346 _remove_station_info_all(MOBILE_AP_TYPE_BT);
347 __del_bt_remote_all();
348 _deinit_timeout_cb(MOBILE_AP_TYPE_BT);
350 _deinit_tethering(obj);
351 _mobileap_clear_state(MOBILE_AP_STATE_BT);
352 _unblock_device_sleep();
354 return MOBILE_AP_ERROR_NONE;
358 static void __bt_nap_connection_changed(bool connected, const char *remote_address, const char *interface_name, void *user_data)
360 if (remote_address == NULL || interface_name == NULL || user_data == NULL) {
361 ERR("Invalid param\n");
365 __bt_remote_device_s *remote;
366 bt_device_info_s *info;
370 SDBG("Remote address : %s, Interface : %s, %s\n",
371 remote_address, interface_name,
372 connected ? "Connected" : "Disconnected");
375 ret = bt_adapter_get_bonded_device_info(remote_address, &info);
376 if (ret != BT_ERROR_NONE) {
377 ERR("bt_adapter_get_bonded_device_info is failed : %d\n", ret);
381 remote = __add_bt_remote(info, interface_name);
382 if (remote == NULL) {
383 ERR("__add_bt_remote is failed\n");
384 bt_adapter_free_device_info(info);
388 ret = _mh_core_set_ip_address(interface_name, remote->intf_ip);
389 if (ret != MOBILE_AP_ERROR_NONE) {
390 ERR("Setting ip address error : %d\n", ret);
393 _remove_station_info(remote_address, _slist_find_station_by_mac);
394 if (__del_bt_remote(remote_address) == FALSE) {
395 ERR("__del_bt_remote is failed\n");
398 _get_station_count((gconstpointer)MOBILE_AP_TYPE_BT,
399 _slist_find_station_by_interface, &n_station);
401 _start_timeout_cb(MOBILE_AP_TYPE_BT, time(NULL) + TETHERING_CONN_TIMEOUT);
407 static void __bt_adapter_state_changed(int result, bt_adapter_state_e adapter_state, void *user_data)
409 if (user_data == NULL) {
410 ERR("Invalid param\n");
416 if (!_mobileap_is_enabled(MOBILE_AP_STATE_BT))
419 int ret = MOBILE_AP_ERROR_RESOURCE;
420 TetheringObject *obj = (TetheringObject *)user_data;
422 if (result != BT_ERROR_NONE) {
423 ERR("BT Adapter operation is failed : %d\n", result);
427 SDBG("BT Adapter is %s\n", adapter_state == BT_ADAPTER_ENABLED ?
428 "enabled" : "disabled");
429 if (adapter_state == BT_ADAPTER_DISABLED) {
430 _disable_bt_tethering(obj);
431 _emit_mobileap_dbus_signal(obj, E_SIGNAL_BT_TETHER_OFF,
432 SIGNAL_MSG_NOT_AVAIL_INTERFACE);
435 ret = _enable_bt_tethering(obj);
436 if (ret != MOBILE_AP_ERROR_NONE) {
437 ERR("_enable_bt_tethering() is failed : %d\n", ret);
442 _emit_mobileap_dbus_signal(obj, E_SIGNAL_BT_TETHER_ON, NULL);
443 dbus_g_method_return(g_context,
444 MOBILE_AP_ENABLE_BT_TETHERING_CFM, ret);
445 __handle_bt_adapter_visibility();
447 _unblock_device_sleep();
453 dbus_g_method_return(g_context,
454 MOBILE_AP_ENABLE_BT_TETHERING_CFM, ret);
456 _mobileap_clear_state(MOBILE_AP_STATE_BT);
457 _unblock_device_sleep();
462 void _bt_get_remote_device_name(TetheringObject *obj, const char *mac, char **name)
464 if (obj == NULL || mac == NULL || name == NULL) {
465 ERR("Invalid param\n");
469 __bt_remote_device_s *remote = NULL;
471 remote = __find_bt_remote(mac);
475 *name = g_strdup(remote->info->remote_name);
477 ERR("Memory allocation failed\n");
484 static void __bt_adapter_visibility_changed_cb(int result,
485 bt_adapter_visibility_mode_e visibility_mode, void *user_data)
491 bt_adapter_visibility_mode_e mode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
493 ret = bt_adapter_get_visibility(&mode, &duration);
494 if (ret != BT_ERROR_NONE) {
495 ERR("bt_adapter_get_visibility is failed 0x[%X]\n", ret);
498 if (mode == BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE) {
499 ERR("_launch_toast_popup() is failed\n");
505 static void __handle_bt_adapter_visibility()
511 bt_adapter_visibility_mode_e mode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
513 ret = bt_adapter_get_visibility(&mode, &duration);
514 if (ret != BT_ERROR_NONE) {
515 ERR("bt_adapter_get_visibility is failed 0x[%X]\n", ret);
518 if (mode == BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE) {
519 ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_LIMITED_DISCOVERABLE, 120);
520 if (ret != BT_ERROR_NONE) {
521 ERR("bt_adapter_set_visibility is failed 0x[%X]\n", ret);
524 bt_adapter_set_visibility_mode_changed_cb(__bt_adapter_visibility_changed_cb, NULL);
527 gboolean tethering_enable_bt_tethering(TetheringObject *obj,
528 DBusGMethodInvocation *context)
530 mobile_ap_error_code_e ret;
531 gboolean ret_val = FALSE;
535 g_assert(obj != NULL);
536 g_assert(context != NULL);
539 DBG("It is turnning on\n");
540 dbus_g_method_return(context,
541 MOBILE_AP_ENABLE_BT_TETHERING_CFM,
542 MOBILE_AP_ERROR_IN_PROGRESS);
548 _block_device_sleep();
550 ret = __init_bt(obj);
551 if (ret != MOBILE_AP_ERROR_NONE)
554 if (!_mobileap_set_state(MOBILE_AP_STATE_BT)) {
555 ret = MOBILE_AP_ERROR_RESOURCE;
560 if (__is_bt_adapter_on() == FALSE) {
561 DBG("Bluetooth is deactivated\n");
562 if (__turn_on_bt_adapter(obj) != MOBILE_AP_ERROR_NONE) {
563 ERR("__turn_on_bt_adapter is failed\n");
564 ret = MOBILE_AP_ERROR_INTERNAL;
565 _mobileap_clear_state(MOBILE_AP_STATE_BT);
573 ret = _enable_bt_tethering(obj);
574 if (ret != MOBILE_AP_ERROR_NONE) {
575 ERR("_enable_bt_tethering() is failed : %d\n", ret);
576 _mobileap_clear_state(MOBILE_AP_STATE_BT);
579 _emit_mobileap_dbus_signal(obj, E_SIGNAL_BT_TETHER_ON, NULL);
580 __handle_bt_adapter_visibility();
585 dbus_g_method_return(g_context,
586 MOBILE_AP_ENABLE_BT_TETHERING_CFM, ret);
589 _unblock_device_sleep();
594 gboolean tethering_disable_bt_tethering(TetheringObject *obj,
595 DBusGMethodInvocation *context)
597 mobile_ap_error_code_e ret;
601 g_assert(obj != NULL);
602 g_assert(context != NULL);
604 ret = _disable_bt_tethering(obj);
605 if (ret != MOBILE_AP_ERROR_NONE) {
606 dbus_g_method_return(context, MOBILE_AP_DISABLE_BT_TETHERING_CFM, ret);
610 _emit_mobileap_dbus_signal(obj, E_SIGNAL_BT_TETHER_OFF, NULL);
611 dbus_g_method_return(context, MOBILE_AP_DISABLE_BT_TETHERING_CFM, ret);
615 gboolean _is_trying_bt_operation(void)
617 return (g_context ? TRUE : FALSE);