Scan filter refactoring 05/217805/2
authorDeokhyun Kim <dukan.kim@samsung.com>
Thu, 14 Nov 2019 08:48:45 +0000 (17:48 +0900)
committerDeokhyun Kim <dukan.kim@samsung.com>
Fri, 15 Nov 2019 08:25:57 +0000 (17:25 +0900)
Change-Id: I3e6192f92bf8e699031f6afc48ad750c3718ed5c
Signed-off-by: Deokhyun Kim <dukan.kim@samsung.com>
include/bluetooth_private.h
src/bluetooth-adapter.c

index 882ed04..ebb5d46 100644 (file)
@@ -296,7 +296,7 @@ typedef struct {
 } bt_advertiser_s;
 
 typedef struct {
-       int slot_id;
+       int filter_id;
        char *device_address;
        char *device_name;
        char *service_uuid;
index 8ceacbf..e6c72dc 100644 (file)
@@ -44,7 +44,9 @@ static bt_scan_filter_h pxp_linkloss_alert_filter;
 static bt_scan_filter_h pxp_immediate_alert_filter;
 static bt_scan_filter_h pxp_signal_loss_filter;
 
-static GSList *advertiser_list = NULL;
+static GSList *advertiser_list;
+static GSList *scan_filter_list;
+static int scan_filter_id;
 
 /* LCOV_EXCL_START */
 int bt_adapter_enable(void)
@@ -2864,11 +2866,55 @@ static void __bt_adapter_le_convert_scan_filter(
                }
        }
 }
+
+static bt_le_scan_filter_s *__bt_adapter_le_duplicate_scan_filter(
+                                       const bt_le_scan_filter_s *src)
+{
+       bt_le_scan_filter_s *filter = g_malloc0(sizeof(bt_le_scan_filter_s));
+
+       filter->filter_id = src->filter_id;
+       filter->device_address = g_strdup(src->device_address);
+       filter->device_name = g_strdup(src->device_name);
+       filter->service_uuid = g_strdup(src->service_uuid);
+       filter->service_uuid_mask = g_strdup(src->service_uuid_mask);
+       filter->service_solicitation_uuid = g_strdup(src->service_solicitation_uuid);
+       filter->service_solicitation_uuid_mask = g_strdup(src->service_solicitation_uuid_mask);
+       filter->service_data_uuid = g_strdup(src->service_data_uuid);
+       filter->service_data = g_memdup(src->service_data, src->service_data_len);
+       filter->service_data_len = src->service_data_len;
+       filter->service_data_mask = g_memdup(src->service_data_mask, src->service_data_len);
+       filter->manufacturer_id = src->manufacturer_id;
+       filter->manufacturer_data = g_memdup(src->manufacturer_data, src->manufacturer_data_len);
+       filter->manufacturer_data_len = src->manufacturer_data_len;
+       filter->manufacturer_data_mask = g_memdup(src->manufacturer_data_mask, src->manufacturer_data_len);
+
+       return filter;
+}
+
+static void __bt_adapter_le_free_scan_filter(bt_le_scan_filter_s *filter)
+{
+       if (filter == NULL)
+               return;
+
+       g_free(filter->device_address);
+       g_free(filter->device_name);
+       g_free(filter->service_uuid);
+       g_free(filter->service_uuid_mask);
+       g_free(filter->service_solicitation_uuid);
+       g_free(filter->service_solicitation_uuid_mask);
+       g_free(filter->service_data_uuid);
+       g_free(filter->service_data);
+       g_free(filter->service_data_mask);
+       g_free(filter->manufacturer_data);
+       g_free(filter->manufacturer_data_mask);
+       g_free(filter);
+}
 /* LCOV_EXCL_STOP */
 
 int bt_adapter_le_start_scan(bt_adapter_le_scan_result_cb cb, void *user_data)
 {
        int error_code = BT_ERROR_NONE;
+       GSList *l;
 
        BT_CHECK_LE_SUPPORT();
        BT_CHECK_INIT_STATUS();
@@ -2876,6 +2922,19 @@ int bt_adapter_le_start_scan(bt_adapter_le_scan_result_cb cb, void *user_data)
 
        _bt_le_adapter_init();
 
+       for (l = scan_filter_list; l != NULL; l = l->next) {
+               bluetooth_le_scan_filter_t filter;
+               bt_le_scan_filter_s *__filter = l->data;
+               __bt_adapter_le_convert_scan_filter(&filter, __filter);
+
+               error_code = _bt_get_error_code(bluetooth_register_scan_filter(&filter));
+               if (error_code != BT_ERROR_NONE) {
+                       BT_ERR("Scan filter error %s(0x%08x)",
+                                       _bt_convert_error_to_string(error_code),
+                                       error_code); /* LCOV_EXCL_LINE */
+               }
+       }
+
        error_code = _bt_get_error_code(bluetooth_start_le_discovery());
        if (error_code != BT_ERROR_NONE) {
                BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code); /* LCOV_EXCL_LINE */
@@ -3446,6 +3505,7 @@ int bt_adapter_le_scan_filter_create(bt_scan_filter_h *scan_filter)
 
        __filter = (bt_le_scan_filter_s *)g_malloc0(sizeof(bt_le_scan_filter_s));
 
+       __filter->filter_id = scan_filter_id++;
        __filter->manufacturer_id = -1;
        *scan_filter = (bt_scan_filter_h)__filter;
 
@@ -3460,18 +3520,7 @@ int bt_adapter_le_scan_filter_destroy(bt_scan_filter_h scan_filter)
        BT_CHECK_INIT_STATUS();
        BT_CHECK_INPUT_PARAMETER(scan_filter);
 
-       g_free(__filter->device_address);
-       g_free(__filter->device_name);
-       g_free(__filter->service_uuid);
-       g_free(__filter->service_uuid_mask);
-       g_free(__filter->service_solicitation_uuid);
-       g_free(__filter->service_solicitation_uuid_mask);
-       g_free(__filter->service_data_uuid);
-       g_free(__filter->service_data);
-       g_free(__filter->service_data_mask);
-       g_free(__filter->manufacturer_data);
-       g_free(__filter->manufacturer_data_mask);
-       g_free(__filter);
+       __bt_adapter_le_free_scan_filter(__filter);
 
        return BT_ERROR_NONE;
 }
@@ -3742,9 +3791,8 @@ int bt_adapter_le_scan_filter_set_manufacturer_data_with_mask(bt_scan_filter_h s
 
 int bt_adapter_le_scan_filter_register(bt_scan_filter_h scan_filter)
 {
-       int error_code = BT_ERROR_NONE;
        bt_le_scan_filter_s *__filter = (bt_le_scan_filter_s *)scan_filter;
-       bluetooth_le_scan_filter_t filter;
+       GSList *l;
 
        BT_CHECK_LE_SUPPORT();
        BT_CHECK_INIT_STATUS();
@@ -3755,28 +3803,25 @@ int bt_adapter_le_scan_filter_register(bt_scan_filter_h scan_filter)
                return BT_ERROR_NOW_IN_PROGRESS;
        }
 
-       __bt_adapter_le_convert_scan_filter(&filter, __filter);
-
-       error_code = _bt_get_error_code(bluetooth_register_scan_filter(&filter,
-                                                       &__filter->slot_id));
-
-#ifdef TIZEN_FEATURE_TCT_TMP_SUPPORT
-       if (error_code == BT_ERROR_NOT_SUPPORTED)
-               error_code = BT_ERROR_NONE;
-#endif
-
-       if (error_code != BT_ERROR_NONE) {
-               BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code),
-                                       error_code); /* LCOV_EXCL_LINE */
+       for (l = scan_filter_list; l != NULL; l = l->next) {
+               bt_le_scan_filter_s *filter = l->data;
+               if (__filter->filter_id == filter->filter_id) {
+                       scan_filter_list = g_slist_remove(scan_filter_list, filter);
+                       __bt_adapter_le_free_scan_filter(filter);
+                       break;
+               }
        }
 
-       return error_code;
+       scan_filter_list = g_slist_append(scan_filter_list,
+                       __bt_adapter_le_duplicate_scan_filter(__filter));
+
+       return BT_ERROR_NONE;
 }
 
 int bt_adapter_le_scan_filter_unregister(bt_scan_filter_h scan_filter)
 {
-       int error_code = BT_ERROR_NONE;
        bt_le_scan_filter_s *__filter = (bt_le_scan_filter_s *)scan_filter;
+       GSList *l;
 
        BT_CHECK_LE_SUPPORT();
        BT_CHECK_INIT_STATUS();
@@ -3787,18 +3832,16 @@ int bt_adapter_le_scan_filter_unregister(bt_scan_filter_h scan_filter)
                return BT_ERROR_NOW_IN_PROGRESS;
        }
 
-       error_code = _bt_get_error_code(bluetooth_unregister_scan_filter(__filter->slot_id));
-
-#ifdef TIZEN_FEATURE_TCT_TMP_SUPPORT
-       if (error_code == BT_ERROR_NOT_SUPPORTED)
-               error_code = BT_ERROR_NONE;
-#endif
-       if (error_code != BT_ERROR_NONE) {
-               BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code),
-                                       error_code); /* LCOV_EXCL_LINE */
+       for (l = scan_filter_list; l != NULL; l = l->next) {
+               bt_le_scan_filter_s *filter = l->data;
+               if (__filter->filter_id == filter->filter_id) {
+                       scan_filter_list = g_slist_remove(scan_filter_list, filter);
+                       __bt_adapter_le_free_scan_filter(filter);
+                       break;
+               }
        }
 
-       return error_code;
+       return BT_ERROR_NONE;
 }
 
 int bt_adapter_le_scan_filter_unregister_all(void)
@@ -3814,16 +3857,8 @@ int bt_adapter_le_scan_filter_unregister_all(void)
                return BT_ERROR_NOW_IN_PROGRESS;
        }
 
-       error_code = _bt_get_error_code(bluetooth_unregister_all_scan_filters());
-
-#ifdef TIZEN_FEATURE_TCT_TMP_SUPPORT
-       if (error_code == BT_ERROR_NOT_SUPPORTED)
-               error_code = BT_ERROR_NONE;
-#endif
-       if (error_code != BT_ERROR_NONE) {
-               BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code),
-                                       error_code); /* LCOV_EXCL_LINE */
-       }
+       g_slist_free_full(scan_filter_list, (GDestroyNotify)__bt_adapter_le_free_scan_filter);
+       scan_filter_list = NULL;
 
        return error_code;
 }