From 6db5547d631489462b6fc035d45999d992950e73 Mon Sep 17 00:00:00 2001 From: DoHyun Pyun Date: Mon, 20 Jun 2016 16:11:40 +0900 Subject: [PATCH] Implement BT search function in emulator Change-Id: I1633028eac2925eacf72b124347be58aa024fdcc Signed-off-by: DoHyun Pyun --- bt-service-emul/CMakeLists.txt | 2 +- bt-service-emul/bt-service-adapter.c | 171 ++++++++++++++- bt-service-emul/bt-service-event-generator.c | 25 --- bt-service-emul/bt-service-event-manager.c | 230 +++++++++++++++++++++ bt-service-emul/include/bt-service-event-manager.h | 54 +++++ 5 files changed, 446 insertions(+), 36 deletions(-) delete mode 100644 bt-service-emul/bt-service-event-generator.c create mode 100644 bt-service-emul/bt-service-event-manager.c create mode 100644 bt-service-emul/include/bt-service-event-manager.h diff --git a/bt-service-emul/CMakeLists.txt b/bt-service-emul/CMakeLists.txt index 1fe6449..a0e1702 100644 --- a/bt-service-emul/CMakeLists.txt +++ b/bt-service-emul/CMakeLists.txt @@ -5,7 +5,7 @@ SET(SRCS marshal.c bt-service-main.c bt-service-event-sender.c -bt-service-event-generator.c +bt-service-event-manager.c bt-service-common.c bt-service-util.c bt-service-adapter.c diff --git a/bt-service-emul/bt-service-adapter.c b/bt-service-emul/bt-service-adapter.c index 562de12..01a85fb 100644 --- a/bt-service-emul/bt-service-adapter.c +++ b/bt-service-emul/bt-service-adapter.c @@ -25,6 +25,7 @@ #include "bt-service-common.h" #include "bt-service-event.h" +#include "bt-service-event-manager.h" #include "bt-service-adapter.h" #include "bt-service-util.h" #include "bt-service-main.h" @@ -50,10 +51,13 @@ static uint status_reg_id; static char *g_local_name; static gboolean g_is_discoverable; +static int found_cnt; #define BT_DISABLE_TIME 500 /* 500 ms */ #define BT_DEFAULT_NAME "Tizen Emulator" +static gboolean __bt_adapter_enable_cb(gpointer user_data); +static gboolean __bt_adapter_disable_cb(gpointer user_data); static gboolean __bt_timeout_handler(gpointer user_data) { @@ -504,7 +508,7 @@ static void __bt_state_event_handler(const char *event_name, bundle *data, void #endif } -void _bt_handle_adapter_added(void) +static gboolean __bt_adapter_enable_cb(gpointer user_data) { BT_DBG("+"); bt_status_t status; @@ -549,9 +553,13 @@ void _bt_handle_adapter_added(void) (eventsystem_handler)__bt_state_event_handler, NULL) != ES_R_OK) { BT_ERR("Fail to register system event"); } + + _bt_delete_event_timer(BT_EVENT_TIMER_ENABLE); + + return FALSE; } -void _bt_handle_adapter_removed(void) +static gboolean __bt_adapter_disable_cb(gpointer user_data) { int ret; @@ -579,6 +587,128 @@ void _bt_handle_adapter_removed(void) BT_ERR("Fail to unregister system event"); } + _bt_delete_event_timer(BT_EVENT_TIMER_DISABLE); + + return FALSE; +} + +static gboolean __bt_adapter_device_found_cb(gpointer user_data) +{ + int i = 0; + int result = BLUETOOTH_ERROR_NONE; + GVariant *param = NULL; + GVariant *uuids = NULL; + GVariant *manufacturer_data = NULL; + GVariantBuilder *builder = NULL; + bt_remote_dev_info_t *dev_info; + + BT_DBG("+"); + + BT_DBG("found count: %d", found_cnt); + + if (found_cnt >= _bt_get_sample_device_number()) { + BT_DBG("Finish creating devices"); + goto done; + } + + BT_DBG("[%d] device found", found_cnt); + + dev_info = _bt_get_sample_device(found_cnt); + if (dev_info == NULL) { + BT_DBG("Fail to get the sample device"); + goto done; + } + + builder = g_variant_builder_new(G_VARIANT_TYPE("as")); + for (i = 0; i < dev_info->uuid_count; i++) { + g_variant_builder_add(builder, "s", + dev_info->uuids[i]); + } + uuids = g_variant_new("as", builder); + g_variant_builder_unref(builder); + + manufacturer_data = g_variant_new_from_data( + G_VARIANT_TYPE_BYTESTRING, + dev_info->manufacturer_data, + dev_info->manufacturer_data_len, + TRUE, NULL, NULL); + + param = g_variant_new("(isunsbub@asn@ay)", result, + dev_info->address, + dev_info->class, + dev_info->rssi, + dev_info->name, + dev_info->paired, + dev_info->connected, + dev_info->trust, + uuids, + dev_info->manufacturer_data_len, + manufacturer_data); + + _bt_send_event(BT_ADAPTER_EVENT, + BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND, + param); + + _bt_free_device_info(dev_info); + + found_cnt++; + + return TRUE; +done: + _bt_delete_event_timer(BT_EVENT_TIMER_FOUND_DEVICE); + + param = g_variant_new("(i)", result); + + _bt_send_event(BT_ADAPTER_EVENT, + BLUETOOTH_EVENT_DISCOVERY_FINISHED, + param); + + is_discovering = FALSE; + found_cnt = 0; + + return FALSE; +} + +static gboolean __bt_adapter_start_discovery_cb(gpointer user_data) +{ + int result = BLUETOOTH_ERROR_NONE; + GVariant *param = NULL; + + BT_DBG("Discovery started"); + + param = g_variant_new("(i)", result); + + _bt_send_event(BT_ADAPTER_EVENT, + BLUETOOTH_EVENT_DISCOVERY_STARTED, + param); + + _bt_delete_event_timer(BT_EVENT_TIMER_START_DISCOVERY); + + found_cnt = 0; + + _bt_create_event_timer(BT_EVENT_TIMER_FOUND_DEVICE, 500, + __bt_adapter_device_found_cb, NULL); + + return FALSE; +} + +static gboolean __bt_adapter_stop_discovery_cb(gpointer user_data) +{ + int result = BLUETOOTH_ERROR_NONE; + GVariant *param = NULL; + + BT_DBG("Discovery stopped"); + + param = g_variant_new("(i)", result); + + _bt_send_event(BT_ADAPTER_EVENT, + BLUETOOTH_EVENT_DISCOVERY_FINISHED, + param); + + _bt_delete_event_timer(BT_EVENT_TIMER_FOUND_DEVICE); + _bt_delete_event_timer(BT_EVENT_TIMER_STOP_DISCOVERY); + + return FALSE; } static gboolean __bt_enable_timeout_cb(gpointer user_data) @@ -662,9 +792,10 @@ int _bt_enable_adapter(void) return BLUETOOTH_ERROR_DEVICE_BUSY; } - _bt_adapter_set_status(BT_ADAPTER_ENABLED); + _bt_adapter_set_status(BT_ACTIVATING); - __bt_set_enabled(); + _bt_create_event_timer(BT_EVENT_TIMER_ENABLE, 2000, + __bt_adapter_enable_cb, NULL); return BLUETOOTH_ERROR_NONE; } @@ -688,7 +819,10 @@ int _bt_disable_adapter(void) timer_id = 0; } - _bt_handle_adapter_removed(); + _bt_adapter_set_status(BT_DEACTIVATING); + + _bt_create_event_timer(BT_EVENT_TIMER_DISABLE, 1000, + __bt_adapter_disable_cb, NULL); BT_DBG("-"); return BLUETOOTH_ERROR_NONE; @@ -706,7 +840,8 @@ int _bt_reset_adapter(void) timer_id = 0; } - _bt_set_disabled(BLUETOOTH_ERROR_NONE); + _bt_create_event_timer(BT_EVENT_TIMER_DISABLE, 1000, + __bt_adapter_disable_cb, NULL); return BLUETOOTH_ERROR_NONE; } @@ -882,21 +1017,38 @@ int _bt_start_discovery(void) int _bt_start_custom_discovery(bt_discovery_role_type_t role) { + BT_DBG("+"); + + if (_bt_is_discovering() == TRUE) { + BT_ERR("BT is already in discovering"); + return BLUETOOTH_ERROR_IN_PROGRESS; + } + is_discovering = TRUE; cancel_by_user = FALSE; - /* Need to implement the timer and event for this API */ + _bt_create_event_timer(BT_EVENT_TIMER_START_DISCOVERY, 100, + __bt_adapter_start_discovery_cb, NULL); return BLUETOOTH_ERROR_NONE; - } int _bt_cancel_discovery(void) { + if (_bt_is_discovering() == FALSE) { + BT_ERR("BT is not in discovering"); + return BLUETOOTH_ERROR_NOT_IN_OPERATION; + } + is_discovering = FALSE; cancel_by_user = TRUE; + found_cnt = 0; - /* Need to implement the event for this API */ + _bt_delete_event_timer(BT_EVENT_TIMER_START_DISCOVERY); + _bt_delete_event_timer(BT_EVENT_TIMER_FOUND_DEVICE); + + _bt_create_event_timer(BT_EVENT_TIMER_STOP_DISCOVERY, 100, + __bt_adapter_stop_discovery_cb, NULL); return BLUETOOTH_ERROR_NONE; } @@ -961,4 +1113,3 @@ int _bt_set_manufacturer_data(bluetooth_manufacturer_data_t *m_data) return BLUETOOTH_ERROR_NOT_SUPPORT; } - diff --git a/bt-service-emul/bt-service-event-generator.c b/bt-service-emul/bt-service-event-generator.c deleted file mode 100644 index 422f97f..0000000 --- a/bt-service-emul/bt-service-event-generator.c +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include - -#include "bluetooth-api.h" -#include "bt-internal-types.h" - -#include "bt-service-common.h" -#include "bt-service-event.h" - diff --git a/bt-service-emul/bt-service-event-manager.c b/bt-service-emul/bt-service-event-manager.c new file mode 100644 index 0000000..e82f3f5 --- /dev/null +++ b/bt-service-emul/bt-service-event-manager.c @@ -0,0 +1,230 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include + +#include "bluetooth-api.h" +#include "bt-internal-types.h" + +#include "bt-service-common.h" +#include "bt-service-event.h" +#include "bt-service-event-manager.h" + +#define MAX_DEVICES 100 + +struct uuid_def { + char *uuid; +}; + +static struct uuid_def hid_uuids[] = { + { "00001124-0000-1000-8000-00805f9b34fb" }, + { "00001200-0000-1000-8000-00805f9b34fb" }, +}; + +static struct uuid_def tizen_mobile_uuids[] = { + { "00001800-0000-1000-8000-00805f9b34fb" }, + { "00001801-0000-1000-8000-00805f9b34fb" }, + { "0000110c-0000-1000-8000-00805f9b34fb" }, + { "0000110a-0000-1000-8000-00805f9b34fb" }, + { "00001132-0000-1000-8000-00805f9b34fb" }, + { "00001133-0000-1000-8000-00805f9b34fb" }, + { "0000112f-0000-1000-8000-00805f9b34fb" }, + { "00001105-0000-1000-8000-00805f9b34fb" }, + { "0000111f-0000-1000-8000-00805f9b34fb" }, + { "00001112-0000-1000-8000-00805f9b34fb" }, +}; + +static struct uuid_def mobile_uuids[] = { + { "00001105-0000-1000-8000-00805f9b34fb" }, + { "0000110a-0000-1000-8000-00805f9b34fb" }, + { "0000110c-0000-1000-8000-00805f9b34fb" }, + { "00001112-0000-1000-8000-00805f9b34fb" }, + { "00001115-0000-1000-8000-00805f9b34fb" }, + { "00001116-0000-1000-8000-00805f9b34fb" }, + { "0000111f-0000-1000-8000-00805f9b34fb" }, + { "0000112d-0000-1000-8000-00805f9b34fb" }, + { "0000112f-0000-1000-8000-00805f9b34fb" }, + { "00001132-0000-1000-8000-00805f9b34fb" }, + { "00001200-0000-1000-8000-00805f9b34fb" }, + { "00001800-0000-1000-8000-00805f9b34fb" }, + { "00001801-0000-1000-8000-00805f9b34fb" }, +}; + +static struct uuid_def a2dp_only_uuids[] = { + { "0000110b-0000-1000-8000-00805f9b34fb" }, + { "0000110c-0000-1000-8000-00805f9b34fb" }, + { "0000110d-0000-1000-8000-00805f9b34fb" }, + { "0000110e-0000-1000-8000-00805f9b34fb" }, +}; + +static struct uuid_def headset_uuids[] = { + { "0000111e-0000-1000-8000-00805f9b34fb" }, + { "00001108-0000-1000-8000-00805f9b34fb" }, + { "0000110d-0000-1000-8000-00805f9b34fb" }, + { "0000110b-0000-1000-8000-00805f9b34fb" }, + { "0000110e-0000-1000-8000-00805f9b34fb" }, +}; + +struct bt_sample_dev_info_t { + int rssi; + int class; + char *address; + char *name; + struct uuid_def *uuids; + unsigned int uuid_count; + gboolean paired; + bluetooth_connected_link_t connected; + gboolean trust; + char *manufacturer_data; + int manufacturer_data_len; + guchar addr_type; +}; + +static struct bt_sample_dev_info_t sample_devices[] = { + { -69, 2360344, "00:1B:66:01:23:1C", "Sennheiser P", a2dp_only_uuids, sizeof(a2dp_only_uuids) / sizeof(a2dp_only_uuids[0]), FALSE, 0x00, FALSE, NULL, 0, BDADDR_BREDR}, + { -70, 5898764, "A8:7C:01:EF:3C:73", "Galaxy S6 edge", mobile_uuids, sizeof(mobile_uuids) / sizeof(mobile_uuids[0]), FALSE, 0x00, FALSE, NULL, 0, BDADDR_BREDR}, + { -58, 2360324, "50:C9:71:56:30:5A", "Jabra SUPREME a4.18.0", headset_uuids, sizeof(headset_uuids) / sizeof(headset_uuids[0]), FALSE, 0x00, FALSE, NULL, 0, BDADDR_BREDR}, + { -75, 5767692, "AC:5A:14:24:B9:33", "Tizen 3.0 Mobile", tizen_mobile_uuids, sizeof(tizen_mobile_uuids) / sizeof(tizen_mobile_uuids[0]), FALSE, 0x00, FALSE, NULL, 0, BDADDR_BREDR}, + { -60, 9600, "34:15:9E:D4:83:B3", "Apple Wireless Mouse", hid_uuids, sizeof(hid_uuids) / sizeof(hid_uuids[0]), FALSE, 0x00, FALSE, NULL, 0, BDADDR_BREDR}, +}; + +typedef struct { + int event_id; + guint timer_id; +} bt_timer_info_t; + +static GSList *timer_list = NULL; +static int sample_device_num = sizeof(sample_devices) / sizeof(sample_devices[0]); + +void _bt_create_event_timer(int event_id, int interval, void *event_cb, void *user_data) +{ + bt_timer_info_t *event_info = NULL; + + BT_DBG("+"); + + event_info = g_malloc0(sizeof(bt_timer_info_t)); + /* Fix : NULL_RETURNS */ + ret_if(event_info == NULL); + + _bt_delete_event_timer(event_id); + + event_info->event_id = event_id; + + /* Assign a timer id */ + event_info->timer_id = g_timeout_add(interval, (GSourceFunc)event_cb, (gpointer)user_data); + + BT_DBG("Create event timer. event id: %d, timer id: %d", event_id, event_info->timer_id); + + timer_list = g_slist_append(timer_list, event_info); + + BT_DBG("-"); +} + +void _bt_delete_event_timer(int event_id) +{ + GSList *l; + + BT_DBG("+"); + + BT_DBG("Remove event timer. event id: %d", event_id); + + for (l = timer_list; l != NULL; l = g_slist_next(l)) { + bt_timer_info_t *info = l->data; + if (info == NULL) + continue; + + if (info->event_id == event_id) { + BT_DBG("Found the event id"); + /* Remove the previous timer */ + g_source_remove(info->timer_id); + timer_list = g_slist_remove(timer_list, info); + g_free(info); + break; + } + } + + BT_DBG("-"); +} + +void _bt_delete_all_event_timer(void) +{ + GSList *l; + + BT_DBG("+"); + + for (l = timer_list; l != NULL; l = g_slist_next(l)) { + bt_timer_info_t *info = l->data; + if (info == NULL) + continue; + + g_source_remove(info->timer_id); + timer_list = g_slist_remove(timer_list, info); + g_free(info); + } + + g_slist_free(timer_list); + timer_list = NULL; + + BT_DBG("-"); +} + +int _bt_get_sample_device_number(void) +{ + return sample_device_num; +} + +bt_remote_dev_info_t *_bt_get_sample_device(int index) +{ + bt_remote_dev_info_t *dev_info; + + dev_info = g_malloc0(sizeof(bt_remote_dev_info_t)); + retv_if(dev_info == NULL, NULL); + + dev_info->rssi = sample_devices[index].rssi; + dev_info->class = sample_devices[index].class; + dev_info->paired = sample_devices[index].paired; + dev_info->connected = sample_devices[index].connected; + dev_info->trust = sample_devices[index].trust; + dev_info->addr_type = sample_devices[index].addr_type; + + dev_info->name = g_strdup(sample_devices[index].name); + dev_info->address = g_strdup(sample_devices[index].address); + + dev_info->uuid_count = sample_devices[index].uuid_count; + + if (dev_info->uuid_count > 0) { + int i; + dev_info->uuids = g_malloc0(sizeof(char *) * dev_info->uuid_count); + + for (i = 0; i < dev_info->uuid_count; i++) { + BT_DBG("uuid[%d]: %s", i, sample_devices[index].uuids[i]); + dev_info->uuids[i] = g_strdup(sample_devices[index].uuids[i].uuid); + } + } + + dev_info->manufacturer_data_len = sample_devices[index].manufacturer_data_len; + + if (dev_info->manufacturer_data_len > 0) { + dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len); + if (dev_info->manufacturer_data) + memcpy(dev_info->manufacturer_data, sample_devices[index].manufacturer_data, + dev_info->manufacturer_data_len); + } + + return dev_info; +} + diff --git a/bt-service-emul/include/bt-service-event-manager.h b/bt-service-emul/include/bt-service-event-manager.h new file mode 100644 index 0000000..77ff9d9 --- /dev/null +++ b/bt-service-emul/include/bt-service-event-manager.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +#ifndef _BT_SERVICE_EVENT_MANAGER_H_ +#define _BT_SERVICE_EVENT_MANAGER_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BT_EVENT_TIMER_ENABLE = 0x00, + BT_EVENT_TIMER_DISABLE, + BT_EVENT_TIMER_START_DISCOVERY, + BT_EVENT_TIMER_STOP_DISCOVERY, + BT_EVENT_TIMER_FOUND_DEVICE, + BT_EVENT_MAX +} bt_event_timer_e; + + +void _bt_create_event_timer(int event_id, int interval, void *event_cb, void *user_data); + +void _bt_delete_event_timer(int event_id); + +void _bt_delete_all_event_timer(void); + +int _bt_get_sample_device_number(void); + +bt_remote_dev_info_t *_bt_get_sample_device(int index); + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /*_BT_SERVICE_EVENT_MANAGER_H_*/ + -- 2.7.4