Fix the coverity issue (Dereference after null check)
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-adapter-le.c
index 634b19f..b7d9c49 100644 (file)
@@ -61,7 +61,6 @@ typedef struct {
        char *sender;
        GSList *filter_list;
        gboolean is_scanning;
-       gboolean stop_pending;
 } bt_adapter_le_scanner_t;
 
 static bluetooth_advertising_params_t adv_params = {
@@ -181,6 +180,26 @@ void __bt_free_le_adv_slot(void)
        le_adv_slot = NULL;
 }
 
+static void __bt_free_le_scanner(bt_adapter_le_scanner_t *scanner)
+{
+       g_free(scanner->sender);
+       g_slist_free_full(scanner->filter_list, g_free);
+       g_free(scanner);
+}
+
+static void __bt_free_le_scanner_all(void)
+{
+       g_slist_free_full(scanner_list, (GDestroyNotify)__bt_free_le_scanner);
+       scanner_list = NULL;
+
+       scan_filter_enabled = FALSE;
+       is_le_scanning = FALSE;
+       is_le_set_scan_parameter = FALSE;
+       le_scan_params.type = BT_LE_ACTIVE_SCAN;
+       le_scan_params.interval = 0;
+       le_scan_params.window = 0;
+}
+
 int _bt_le_set_max_packet_len(void)
 {
        int result = BLUETOOTH_ERROR_NONE;
@@ -956,7 +975,7 @@ bt_adapter_le_scanner_t* __bt_find_scanner_from_list(const char *sender)
        return NULL;
 }
 
-int __bt_get_available_scan_filter_slot_id(void)
+static int __bt_get_available_scan_filter_slot_id(void)
 {
        GSList *l;
        bt_adapter_le_scanner_t *scanner;
@@ -1000,7 +1019,7 @@ gboolean _bt_is_scan_filter_supported(void)
        return FALSE;
 }
 
-int _bt_register_scan_filter(const char *sender, bluetooth_le_scan_filter_t *filter, int *slot_id)
+int _bt_register_scan_filter(const char *sender, bluetooth_le_scan_filter_t *filter)
 {
        GDBusProxy *proxy;
        GError *error = NULL;
@@ -1014,9 +1033,10 @@ int _bt_register_scan_filter(const char *sender, bluetooth_le_scan_filter_t *fil
        bt_adapter_le_scanner_t *scanner = NULL;
        bluetooth_le_scan_filter_t *filter_data = NULL;
        int feature_selection = 0;
+       int slot_id;
 
-       *slot_id = __bt_get_available_scan_filter_slot_id();
-       if (*slot_id == -1)
+       slot_id = __bt_get_available_scan_filter_slot_id();
+       if (slot_id == -1)
                return BLUETOOTH_ERROR_NO_RESOURCES;
 
        proxy = _bt_get_adapter_proxy();
@@ -1041,7 +1061,7 @@ int _bt_register_scan_filter(const char *sender, bluetooth_le_scan_filter_t *fil
                                        0,      // client_if
                                        0,      // action (Add - 0x00, Delete - 0x01, Clear - 0x02)
                                        BLUETOOTH_LE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS,        // filter_type
-                                       *slot_id,       // filter_index
+                                       slot_id,        // filter_index
                                        0,      // company_id
                                        0,      // company_id_mask
                                        arr_uuid_param, // p_uuid
@@ -1079,7 +1099,7 @@ int _bt_register_scan_filter(const char *sender, bluetooth_le_scan_filter_t *fil
                                        0,      // client_if
                                        0,      // action (Add - 0x00, Delete - 0x01, Clear - 0x02)
                                        BLUETOOTH_LE_SCAN_FILTER_FEATURE_DEVICE_NAME,   // filter_type
-                                       *slot_id,       // filter_index
+                                       slot_id,        // filter_index
                                        0,      // company_id
                                        0,      // company_id_mask
                                        arr_uuid_param, // p_uuid
@@ -1123,7 +1143,7 @@ int _bt_register_scan_filter(const char *sender, bluetooth_le_scan_filter_t *fil
                                        0,      // client_if
                                        0,      // action (Add - 0x00, Delete - 0x01, Clear - 0x02)
                                        BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_UUID,  // filter_type
-                                       *slot_id,       // filter_index
+                                       slot_id,        // filter_index
                                        0,      // company_id
                                        0,      // company_id_mask
                                        arr_uuid_param, // p_uuid
@@ -1170,7 +1190,7 @@ int _bt_register_scan_filter(const char *sender, bluetooth_le_scan_filter_t *fil
                                        0,      // client_if
                                        0,      // action (Add - 0x00, Delete - 0x01, Clear - 0x02)
                                        BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID,     // filter_type
-                                       *slot_id,       // filter_index
+                                       slot_id,        // filter_index
                                        0,      // company_id
                                        0,      // company_id_mask
                                        arr_uuid_param, // p_uuid
@@ -1217,7 +1237,7 @@ int _bt_register_scan_filter(const char *sender, bluetooth_le_scan_filter_t *fil
                                        0,      // client_if
                                        0,      // action (Add - 0x00, Delete - 0x01, Clear - 0x02)
                                        BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_DATA,  // filter_type
-                                       *slot_id,       // filter_index
+                                       slot_id,        // filter_index
                                        0,      // company_id
                                        0,      // company_id_mask
                                        arr_uuid_param, // p_uuid
@@ -1264,7 +1284,7 @@ int _bt_register_scan_filter(const char *sender, bluetooth_le_scan_filter_t *fil
                                        0,      // client_if
                                        0,      // action (Add - 0x00, Delete - 0x01, Clear - 0x02)
                                        BLUETOOTH_LE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA,     // filter_type
-                                       *slot_id,       // filter_index
+                                       slot_id,        // filter_index
                                        filter->manufacturer_id,        // company_id
                                        0xFFFF, // company_id_mask
                                        arr_uuid_param, // p_uuid
@@ -1294,7 +1314,7 @@ int _bt_register_scan_filter(const char *sender, bluetooth_le_scan_filter_t *fil
        param = g_variant_new("(iiiiiiiiiiii)",
                                0,      // client_if
                                0,      // action (Add - 0x00, Delete - 0x01, Clear - 0x02)
-                               *slot_id,       // filter_index
+                               slot_id,        // filter_index
                                feature_selection,      // feat_seln
                                0,      // list_logic_type (OR - 0x00, AND - 0x01)
                                1,      // filt_logic_type (OR - 0x00, AND - 0x01)
@@ -1324,7 +1344,7 @@ int _bt_register_scan_filter(const char *sender, bluetooth_le_scan_filter_t *fil
        if (scanner) {
                filter_data = g_malloc0(sizeof(bluetooth_le_scan_filter_t));
                memcpy(filter_data, filter, sizeof(bluetooth_le_scan_filter_t));
-               filter_data->slot_id = *slot_id;
+               filter_data->slot_id = slot_id;
 
                scanner->filter_list = g_slist_append(scanner->filter_list, filter_data);
        }
@@ -1334,55 +1354,6 @@ int _bt_register_scan_filter(const char *sender, bluetooth_le_scan_filter_t *fil
        return BLUETOOTH_ERROR_NONE;
 }
 
-int _bt_unregister_scan_filter(const char *sender, int slot_id)
-{
-       GDBusProxy *proxy;
-       GError *error = NULL;
-       GVariant *ret;
-       bt_adapter_le_scanner_t *scanner = NULL;
-       bluetooth_le_scan_filter_t *filter_data = NULL;
-       GSList *l;
-       gboolean is_slot_id_found = FALSE;
-
-       scanner = __bt_find_scanner_from_list(sender);
-       if (scanner == NULL) {
-               BT_ERR("There is NO available scanner.");
-               return BLUETOOTH_ERROR_NOT_FOUND;
-       }
-
-       for (l = scanner->filter_list; l != NULL; l = g_slist_next(l)) {
-               filter_data = l->data;
-               if (filter_data->slot_id == slot_id) {
-                       is_slot_id_found = TRUE;
-                       break;
-               }
-       }
-       if (is_slot_id_found == FALSE) {
-               BT_ERR("There is NO registered slot.");
-               return BLUETOOTH_ERROR_NOT_FOUND;
-       }
-
-       proxy = _bt_get_adapter_proxy();
-       retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       ret = g_dbus_proxy_call_sync(proxy, "scan_filter_clear",
-                               g_variant_new("(ii)", 0, slot_id),
-                               G_DBUS_CALL_FLAGS_NONE,
-                               -1, NULL, &error);
-
-       if (error) {
-               BT_ERR("scan_filter_clear Fail: %s", error->message);
-               g_clear_error(&error);
-       }
-
-       scanner->filter_list = g_slist_remove(scanner->filter_list, filter_data);
-       g_free(filter_data);
-
-       if (ret)
-               g_variant_unref(ret);
-       return BLUETOOTH_ERROR_NONE;
-}
-
 int _bt_unregister_all_scan_filters(const char *sender)
 {
        GDBusProxy *proxy;
@@ -1422,15 +1393,6 @@ int _bt_unregister_all_scan_filters(const char *sender)
        return BLUETOOTH_ERROR_NONE;
 }
 
-static gboolean __start_le_scan_timeout(gpointer user_data)
-{
-       char *sender = (char *)user_data;
-       _bt_start_le_scan(sender);
-
-       return FALSE;
-}
-
-
 int _bt_start_le_scan(const char *sender)
 {
        GDBusProxy *proxy;
@@ -1444,12 +1406,6 @@ int _bt_start_le_scan(const char *sender)
                scanner_list = g_slist_append(scanner_list, scanner);
        }
 
-       if (scanner->stop_pending == TRUE) {
-               BT_DBG("Waiting LEDiscoveryFinished");
-               g_timeout_add(500, (GSourceFunc)__start_le_scan_timeout, scanner->sender);
-               return BLUETOOTH_ERROR_NONE;
-       }
-
        if (scanner->is_scanning == TRUE) {
                BT_ERR("BT is already in LE scanning");
                return BLUETOOTH_ERROR_IN_PROGRESS;
@@ -1533,17 +1489,18 @@ int _bt_stop_le_scan(const char *sender)
        GDBusProxy *proxy;
        GError *error = NULL;
        GVariant *ret;
-       bt_adapter_le_scanner_t *scan_sender = __bt_find_scanner_from_list(sender);
-       bt_adapter_le_scanner_t *scanner;
+       bt_adapter_le_scanner_t *scanner = __bt_find_scanner_from_list(sender);
        GSList *l;
        gboolean next_scanning = FALSE;
        gboolean need_scan_filter = TRUE;
 
-       if (scan_sender == NULL || scan_sender->is_scanning == FALSE)
+       if (scanner == NULL || scanner->is_scanning == FALSE)
                return BLUETOOTH_ERROR_NOT_IN_OPERATION;
 
-       scan_sender->is_scanning = FALSE;
-       scan_sender->stop_pending = TRUE;
+       _bt_unregister_all_scan_filters(sender);
+
+       scanner_list = g_slist_remove(scanner_list, scanner);
+       __bt_free_le_scanner(scanner);
 
        for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
                scanner = l->data;
@@ -1575,7 +1532,6 @@ int _bt_stop_le_scan(const char *sender)
                        scan_filter_enabled = TRUE;
                }
                BT_INFO("next_scanning exists. Keep the LE scanning");
-               scan_sender->stop_pending = FALSE;
                return BLUETOOTH_ERROR_NONE;
        } else {
                if (scan_filter_enabled == TRUE) {
@@ -1628,33 +1584,9 @@ void _bt_disable_all_scanner_status(void)
        for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
                scanner = l->data;
                scanner->is_scanning = FALSE;
-               scanner->stop_pending = FALSE;
        }
 }
 
-static void __bt_free_le_scanner(void)
-{
-       GSList *l;
-       bt_adapter_le_scanner_t *scanner;
-
-       for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
-               scanner = l->data;
-               g_free(scanner->sender);
-               g_slist_free_full(scanner->filter_list, g_free);
-               g_free(scanner);
-       }
-
-       g_slist_free(scanner_list);
-       scanner_list = NULL;
-
-       scan_filter_enabled = FALSE;
-       is_le_scanning = FALSE;
-       is_le_set_scan_parameter = FALSE;
-       le_scan_params.type = BT_LE_ACTIVE_SCAN;
-       le_scan_params.interval = 0;
-       le_scan_params.window = 0;
-}
-
 void _bt_set_le_scan_status(gboolean mode)
 {
        BT_DBG("set is_le_scanning : %d -> %d", is_le_scanning, mode);
@@ -2431,5 +2363,5 @@ int _bt_service_adapter_le_init(void)
 void _bt_service_adapter_le_deinit(void)
 {
        __bt_free_le_adv_slot();
-       __bt_free_le_scanner();
+       __bt_free_le_scanner_all();
 }