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 <bluetooth.h>
23 #include <bluetooth_internal.h>
26 #include "mobileap_softap.h"
27 #include "mobileap_common.h"
28 #include "mobileap_bluetooth.h"
29 #include "mobileap_handler.h"
30 #include "mobileap_notification.h"
33 bt_device_info_s *info;
35 const in_addr_t intf_ip;
36 } __bt_remote_device_s;
38 static __bt_remote_device_s __bt_remote_devices[MOBILE_AP_MAX_BT_STA] = {
39 {NULL, NULL, IP_ADDRESS_BT_1},
40 {NULL, NULL, IP_ADDRESS_BT_2},
41 {NULL, NULL, IP_ADDRESS_BT_3},
42 {NULL, NULL, IP_ADDRESS_BT_4}};
44 static GDBusMethodInvocation *g_context = NULL;
46 static void __bt_nap_connection_changed(bool connected, const char *remote_address,
47 const char *interface_name, void *user_data);
48 static void __bt_adapter_state_changed(int result, bt_adapter_state_e adapter_state, void *user_data);
49 static void __handle_bt_adapter_visibility();
51 int __recheck_bt_adapter_timer = 0;
53 static __bt_remote_device_s *__find_bt_remote(const char *mac)
57 for (i = 0; i < MOBILE_AP_MAX_BT_STA; i++) {
58 if (__bt_remote_devices[i].info == NULL)
61 if (!g_ascii_strcasecmp(__bt_remote_devices[i].info->remote_address, mac))
65 if (i == MOBILE_AP_MAX_BT_STA) {
66 SERR("Not found : %s\n", mac);
70 return &__bt_remote_devices[i];
73 static __bt_remote_device_s *__add_bt_remote(bt_device_info_s *info, const char *intf_name)
77 for (i = 0; i < MOBILE_AP_MAX_BT_STA; i++) {
78 if (__bt_remote_devices[i].info == NULL)
82 if (i == MOBILE_AP_MAX_BT_STA) {
83 ERR("Too many BT devices are connected\n");
87 __bt_remote_devices[i].intf_name = g_strdup(intf_name);
88 if (__bt_remote_devices[i].intf_name == NULL) {
89 ERR("Memory allocation failed\n");
93 _add_interface_routing(__bt_remote_devices[i].intf_name,
94 __bt_remote_devices[i].intf_ip);
95 _add_routing_rule(__bt_remote_devices[i].intf_name);
96 __bt_remote_devices[i].info = info;
98 return &__bt_remote_devices[i];
101 static gboolean __del_bt_remote(const char *mac)
105 for (i = 0; i < MOBILE_AP_MAX_BT_STA; i++) {
106 if (__bt_remote_devices[i].info == NULL)
109 if (!g_ascii_strcasecmp(__bt_remote_devices[i].info->remote_address, mac))
113 if (i == MOBILE_AP_MAX_BT_STA) {
114 SERR("Not found : %s\n", mac);
118 _del_routing_rule(__bt_remote_devices[i].intf_name);
119 _del_interface_routing(__bt_remote_devices[i].intf_name,
120 __bt_remote_devices[i].intf_ip);
121 bt_adapter_free_device_info(__bt_remote_devices[i].info);
122 g_free(__bt_remote_devices[i].intf_name);
124 __bt_remote_devices[i].info = NULL;
125 __bt_remote_devices[i].intf_name = NULL;
130 static void __del_bt_remote_all(void)
134 for (i = 0; i < MOBILE_AP_MAX_BT_STA; i++) {
135 if (__bt_remote_devices[i].info) {
136 bt_adapter_free_device_info(__bt_remote_devices[i].info);
137 __bt_remote_devices[i].info = NULL;
140 if (__bt_remote_devices[i].intf_name) {
141 _del_routing_rule(__bt_remote_devices[i].intf_name);
142 _del_interface_routing(__bt_remote_devices[i].intf_name,
143 __bt_remote_devices[i].intf_ip);
144 g_free(__bt_remote_devices[i].intf_name);
145 __bt_remote_devices[i].intf_name = NULL;
152 static mobile_ap_error_code_e __init_bt(Tethering *obj)
156 ret = bt_initialize();
157 if (ret != BT_ERROR_NONE) {
158 ERR("bt_initialize is failed : %d\n", ret);
159 return MOBILE_AP_ERROR_RESOURCE;
162 ret = bt_adapter_set_state_changed_cb(__bt_adapter_state_changed, (void *)obj);
163 if (ret != BT_ERROR_NONE) {
164 ERR("bt_adapter_set_state_changed_cb is failed : %d\n", ret);
166 return MOBILE_AP_ERROR_RESOURCE;
169 return MOBILE_AP_ERROR_NONE;
172 static void __deinit_bt(void)
176 ret = bt_adapter_unset_state_changed_cb();
177 if (ret != BT_ERROR_NONE)
178 ERR("bt_adapter_unset_state_changed_cb is failed : %d\n", ret);
180 ret = bt_deinitialize();
181 if (ret != BT_ERROR_NONE)
182 ERR("bt_deinitialize is failed : %d\n", ret);
187 static gboolean __is_bt_adapter_on(void)
190 bt_adapter_state_e adapter_state = BT_ADAPTER_DISABLED;
192 ret = bt_adapter_get_state(&adapter_state);
193 if (ret != BT_ERROR_NONE) {
194 ERR("bt_adapter_get_state is failed : %d\n", ret);
198 if (adapter_state == BT_ADAPTER_ENABLED)
204 gboolean __bt_adapter_timeout_cb(Tethering *obj)
208 static int retry_count = 0;
209 if (__is_bt_adapter_on() == TRUE) {
210 DBG("BT Adapter is enabled by other process \n");
215 if (++retry_count >= PS_RECHECK_COUNT_MAX) {
217 ERR("_enable_bt_tethering() is failed because of bt_adapter_eanbled() failed:n");
218 _mobileap_clear_state(MOBILE_AP_STATE_BT);
220 tethering_complete_enable_bt_tethering(obj, g_context, MOBILE_AP_ERROR_INTERNAL);
222 _unblock_device_sleep();
232 static mobile_ap_error_code_e __turn_on_bt_adapter(gpointer data)
236 Tethering *obj = (Tethering *)data;
238 ret = bt_adapter_enable();
239 if (ret == BT_ERROR_NOW_IN_PROGRESS) {
240 if (__recheck_bt_adapter_timer) {
241 g_source_remove(__recheck_bt_adapter_timer);
243 __recheck_bt_adapter_timer = g_timeout_add(PS_RECHECK_INTERVAL,
244 __bt_adapter_timeout_cb, obj);
245 return MOBILE_AP_ERROR_NONE;
248 if (ret != BT_ERROR_NONE && ret != BT_ERROR_ALREADY_DONE) {
249 ERR("bt_adapter_enable is failed : %d\n", ret);
251 if (ret == BT_ERROR_PERMISSION_DENIED)
252 return MOBILE_AP_ERROR_PERMISSION_DENIED;
255 return MOBILE_AP_ERROR_RESOURCE;
258 return MOBILE_AP_ERROR_NONE;
261 static mobile_ap_error_code_e __turn_on_bt_nap(Tethering *obj)
263 int bt_ret = BT_ERROR_NONE;
265 bt_ret = bt_nap_set_connection_state_changed_cb(__bt_nap_connection_changed, (void *)obj);
266 if (bt_ret != BT_ERROR_NONE) {
267 ERR("bt_nap_set_connection_state_changed_cb is failed : %d\n", bt_ret);
268 return MOBILE_AP_ERROR_RESOURCE;
271 bt_ret = bt_nap_activate();
272 if (bt_ret != BT_ERROR_NONE && bt_ret != BT_ERROR_ALREADY_DONE) {
273 bt_nap_unset_connection_state_changed_cb();
274 ERR("bt_nap_activate is failed : %d\n", bt_ret);
276 if (bt_ret == BT_ERROR_PERMISSION_DENIED)
277 return MOBILE_AP_ERROR_PERMISSION_DENIED;
280 return MOBILE_AP_ERROR_RESOURCE;
283 return MOBILE_AP_ERROR_NONE;
286 static void __turn_off_bt_nap(void)
290 bt_ret = bt_nap_deactivate();
291 if (bt_ret != BT_ERROR_NONE)
292 ERR("bt_nap_deactivate is failed : %d\n", bt_ret);
294 DBG("bt_nap_deactivate is called\n");
296 bt_ret = bt_nap_unset_connection_state_changed_cb();
297 if (bt_ret != BT_ERROR_NONE)
298 ERR("bt_nap_unset_connection_state_changed_cb is failed : %d\n", bt_ret);
303 mobile_ap_error_code_e _enable_bt_tethering(Tethering *obj)
305 mobile_ap_error_code_e ret = MOBILE_AP_ERROR_NONE;
308 if (__recheck_bt_adapter_timer) {
309 g_source_remove(__recheck_bt_adapter_timer);
310 __recheck_bt_adapter_timer = 0;
313 if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP)) {
314 ERR("Wi-Fi AP is enabled\n");
315 return MOBILE_AP_ERROR_RESOURCE;
318 ret = _init_tethering();
319 if (ret != MOBILE_AP_ERROR_NONE) {
323 ret = __turn_on_bt_nap(obj);
324 if (ret != MOBILE_AP_ERROR_NONE) {
328 _delete_timeout_noti();
329 _init_timeout_cb(MOBILE_AP_TYPE_BT, (void *)obj);
330 _start_timeout_cb(MOBILE_AP_TYPE_BT, time(NULL) + TETHERING_CONN_TIMEOUT);
336 mobile_ap_error_code_e _disable_bt_tethering(Tethering *obj)
338 int ret = BT_ERROR_NONE;
339 if (!_mobileap_is_enabled(MOBILE_AP_STATE_BT)) {
340 ERR("BT tethering has not been enabled\n");
341 return MOBILE_AP_ERROR_NOT_ENABLED;
343 ret = bt_adapter_unset_visibility_mode_changed_cb();
344 if (ret != BT_ERROR_NONE)
345 ERR("bt_adapter_unset_visibility_mode_changed_cb is failed : %d\n", ret);
347 _block_device_sleep();
348 if (__is_bt_adapter_on()) {
353 _remove_station_info_all(MOBILE_AP_TYPE_BT);
354 __del_bt_remote_all();
355 _deinit_timeout_cb(MOBILE_AP_TYPE_BT);
358 _mobileap_clear_state(MOBILE_AP_STATE_BT);
359 _unblock_device_sleep();
361 return MOBILE_AP_ERROR_NONE;
365 static void __bt_nap_connection_changed(bool connected, const char *remote_address, const char *interface_name, void *user_data)
367 if (remote_address == NULL || interface_name == NULL || user_data == NULL) {
368 ERR("Invalid param\n");
372 __bt_remote_device_s *remote;
373 bt_device_info_s *info;
377 SDBG("Remote address : %s, Interface : %s, %s\n",
378 remote_address, interface_name,
379 connected ? "Connected" : "Disconnected");
382 ret = bt_adapter_get_bonded_device_info(remote_address, &info);
383 if (ret != BT_ERROR_NONE) {
384 ERR("bt_adapter_get_bonded_device_info is failed : %d\n", ret);
388 remote = __add_bt_remote(info, interface_name);
389 if (remote == NULL) {
390 ERR("__add_bt_remote is failed\n");
391 bt_adapter_free_device_info(info);
395 ret = _mh_core_set_ip_address(interface_name, remote->intf_ip);
396 if (ret != MOBILE_AP_ERROR_NONE) {
397 ERR("Setting ip address error : %d\n", ret);
400 _remove_station_info(remote_address, _slist_find_station_by_mac);
401 if (__del_bt_remote(remote_address) == FALSE) {
402 ERR("__del_bt_remote is failed\n");
405 _get_station_count((gconstpointer)MOBILE_AP_TYPE_BT,
406 _slist_find_station_by_interface, &n_station);
408 _start_timeout_cb(MOBILE_AP_TYPE_BT, time(NULL) + TETHERING_CONN_TIMEOUT);
414 static void __bt_adapter_state_changed(int result, bt_adapter_state_e adapter_state, void *user_data)
416 if (user_data == NULL) {
417 ERR("Invalid param\n");
423 if (!_mobileap_is_enabled(MOBILE_AP_STATE_BT))
426 int ret = MOBILE_AP_ERROR_RESOURCE;
427 Tethering *obj = (Tethering *)user_data;
429 if (result != BT_ERROR_NONE) {
430 ERR("BT Adapter operation is failed : %d\n", result);
434 SDBG("BT Adapter is %s\n", adapter_state == BT_ADAPTER_ENABLED ?
435 "enabled" : "disabled");
436 if (adapter_state == BT_ADAPTER_DISABLED) {
437 _disable_bt_tethering(obj);
438 tethering_emit_bluetooth_off(obj, SIGNAL_MSG_NOT_AVAIL_INTERFACE);
441 ret = _enable_bt_tethering(obj);
442 if (ret != MOBILE_AP_ERROR_NONE) {
443 ERR("_enable_bt_tethering() is failed : %d\n", ret);
448 tethering_emit_bluetooth_on(obj);
449 _create_tethering_active_noti();
450 tethering_complete_enable_bt_tethering(obj, g_context, ret);
451 __handle_bt_adapter_visibility();
453 _unblock_device_sleep();
459 tethering_complete_enable_bt_tethering(obj, g_context, ret);
461 _mobileap_clear_state(MOBILE_AP_STATE_BT);
462 _unblock_device_sleep();
467 void _bt_get_remote_device_name(const char *mac, char **name)
469 if (mac == NULL || name == NULL) {
470 ERR("Invalid param\n");
474 __bt_remote_device_s *remote = NULL;
476 remote = __find_bt_remote(mac);
480 *name = g_strdup(remote->info->remote_name);
482 ERR("Memory allocation failed\n");
489 static void __bt_adapter_visibility_changed_cb(int result,
490 bt_adapter_visibility_mode_e visibility_mode, void *user_data)
496 bt_adapter_visibility_mode_e mode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
498 ret = bt_adapter_get_visibility(&mode, &duration);
499 if (ret != BT_ERROR_NONE) {
500 ERR("bt_adapter_get_visibility is failed 0x[%X]\n", ret);
503 if (mode == BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE) {
504 ERR("_launch_toast_popup() is failed\n");
510 static void __handle_bt_adapter_visibility()
516 bt_adapter_visibility_mode_e mode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
518 ret = bt_adapter_get_visibility(&mode, &duration);
519 if (ret != BT_ERROR_NONE) {
520 ERR("bt_adapter_get_visibility is failed 0x[%X]\n", ret);
523 if (mode == BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE) {
524 ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_LIMITED_DISCOVERABLE, 120);
525 if (ret != BT_ERROR_NONE) {
526 ERR("bt_adapter_set_visibility is failed 0x[%X]\n", ret);
529 bt_adapter_set_visibility_mode_changed_cb(__bt_adapter_visibility_changed_cb, NULL);
533 gboolean tethering_enable_bt_tethering(Tethering *obj,
534 GDBusMethodInvocation *context)
536 mobile_ap_error_code_e ret;
537 gboolean ret_val = FALSE;
541 g_assert(obj != NULL);
542 g_assert(context != NULL);
545 DBG("It is turnning on\n");
546 tethering_complete_enable_bt_tethering(obj, context,
547 MOBILE_AP_ERROR_IN_PROGRESS);
553 _block_device_sleep();
555 ret = __init_bt(obj);
556 if (ret != MOBILE_AP_ERROR_NONE)
559 if (!_mobileap_set_state(MOBILE_AP_STATE_BT)) {
560 ret = MOBILE_AP_ERROR_RESOURCE;
565 if (__is_bt_adapter_on() == FALSE) {
566 DBG("Bluetooth is deactivated\n");
567 if (__turn_on_bt_adapter((gpointer)obj) != MOBILE_AP_ERROR_NONE) {
568 ERR("__turn_on_bt_adapter is failed\n");
569 ret = MOBILE_AP_ERROR_INTERNAL;
570 _mobileap_clear_state(MOBILE_AP_STATE_BT);
578 ret = _enable_bt_tethering(obj);
579 if (ret != MOBILE_AP_ERROR_NONE) {
580 ERR("_enable_bt_tethering() is failed : %d\n", ret);
581 _mobileap_clear_state(MOBILE_AP_STATE_BT);
584 tethering_emit_bluetooth_on(obj);
585 _create_tethering_active_noti();
586 __handle_bt_adapter_visibility();
591 tethering_complete_enable_bt_tethering(obj, g_context, ret);
594 _unblock_device_sleep();
599 gboolean tethering_disable_bt_tethering(Tethering *obj,
600 GDBusMethodInvocation *context)
602 mobile_ap_error_code_e ret;
606 g_assert(obj != NULL);
607 g_assert(context != NULL);
609 ret = _disable_bt_tethering(obj);
610 if (ret != MOBILE_AP_ERROR_NONE) {
611 tethering_complete_disable_bt_tethering(obj, context,
612 MOBILE_AP_DISABLE_BT_TETHERING_CFM, ret);
616 tethering_emit_bluetooth_off(obj, NULL);
617 tethering_complete_disable_bt_tethering(obj, context,
618 MOBILE_AP_DISABLE_BT_TETHERING_CFM, ret);
622 gboolean _is_trying_bt_operation(void)
624 return (g_context ? TRUE : FALSE);