Merge the code from private
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-event-receiver.c
index b55b6c5..0581b01 100644 (file)
@@ -54,6 +54,7 @@ static GDBusConnection *opc_obexd_conn;
 static GList *p_cache_list = NULL;
 
 static guint event_id;
+static guint session_reinit_timer;
 guint nap_connected_device_count = 0;
 static guint hid_connected_device_count = 0;
 static GList *p_adv_ind_list = NULL;
@@ -104,7 +105,7 @@ static bt_le_adv_info_t *__bt_get_adv_ind_info(char *addr)
        retv_if(!addr, NULL);
        bt_le_adv_info_t *adv_info = NULL;
        GList *current = g_list_first((GList *)p_adv_ind_list);
-       while(current && current->data) {
+       while (current && current->data) {
                adv_info = (bt_le_adv_info_t *)current->data;
                if (adv_info && !g_strcmp0(adv_info->addr, addr))
                        return adv_info;
@@ -201,7 +202,7 @@ static gboolean __bt_parse_device_properties(GVariant *item,
                } else if (strcasecmp(key, "UUIDs") == 0) {
                        char **uuid_value;
                        gsize size = 0;
-                       int i =0;
+                       int i = 0;
                        size = g_variant_get_size(val);
 
                        if (size > 0) {
@@ -251,7 +252,7 @@ static gboolean __bt_parse_interface(GVariant *msg,
                                        bt_remote_dev_info_t *dev_info)
 {
        char *path = NULL;
-       GVariant *optional_param;
+       GVariant *optional_param = NULL;
        GVariantIter iter;
        GVariant *child;
        char *interface_name = NULL;
@@ -260,6 +261,8 @@ static gboolean __bt_parse_interface(GVariant *msg,
                                        &path, &optional_param);
        g_variant_iter_init(&iter, optional_param);
 
+       retv_if(optional_param == NULL, FALSE);
+
        while ((child = g_variant_iter_next_value(&iter))) {
                g_variant_get(child, "{&s@a{sv}}", &interface_name, &inner_iter);
                if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
@@ -279,10 +282,10 @@ static gboolean __bt_parse_interface(GVariant *msg,
                        }
                }
                g_variant_unref(inner_iter);
-       g_variant_unref(child);
+               g_variant_unref(child);
        }
-       if (optional_param)
-               g_variant_unref(optional_param);
+
+       g_variant_unref(optional_param);
 
        return FALSE;
 }
@@ -578,10 +581,16 @@ void _bt_handle_adapter_event(GVariant *msg, const char *member)
                g_free(address);
        } else if (strcasecmp(member, BT_HARDWARE_ERROR) == 0) {
                BT_ERR_C("Hardware error received from BLUEZ");
+/* Don't add the recovery logic into platform */
+#if 0
                _bt_recover_adapter();
+#endif
        } else if (strcasecmp(member, BT_TX_TIMEOUT_ERROR) == 0) {
                BT_ERR_C("Tx timeout error received from BLUEZ");
+/* Don't add the recovery logic into platform */
+#if 0
                _bt_recover_adapter();
+#endif
        }
        BT_DBG("-");
 }
@@ -1832,7 +1841,7 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                         */
                        _bt_obex_check_pending_transfer(address);
                        _bt_opp_client_is_sending(&sending);
-                       if(sending == TRUE)
+                       if (sending == TRUE)
                                _bt_opp_client_check_pending_transfer(address);
                }
                param = g_variant_new("(isy)", result, address, addr_type);
@@ -1947,7 +1956,7 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                                _bt_audio_connect(wait_list->req_id,
                                                        wait_list->type,
                                                        &device_address,
-                                                       wait_list->out_param1);
+                                                       NULL);
                        }
                } else if (strcmp(profile_uuid, AVRCP_TARGET_UUID) == 0) {
 
@@ -2022,7 +2031,7 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                                        _bt_audio_connect(wait_list->req_id,
                                                        wait_list->type,
                                                        &device_address,
-                                                       wait_list->out_param1);
+                                                       NULL);
                                        /* Now free the wait list */
                                        _bt_rel_wait_data();
                                }
@@ -2164,8 +2173,26 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                                                event,
                                                param);
                g_free(address);
-       }
+       } else if (strcasecmp(member, "iBeaconReport") == 0) {
+               bt_remote_ibeacon_dev_info_t *ibeacon_dev_info = NULL;
+
+               ret_if(_bt_is_le_scanning() == FALSE);
 
+               ibeacon_dev_info = g_malloc0(sizeof(bt_remote_ibeacon_dev_info_t));
+               if (ibeacon_dev_info == NULL)
+                       return;
+
+               g_variant_get(msg, "(syuusuuy)", &ibeacon_dev_info->address,
+                                               &ibeacon_dev_info->addr_type,
+                                               &ibeacon_dev_info->company_id,
+                                               &ibeacon_dev_info->ibeacon_type,
+                                               &ibeacon_dev_info->uuid,
+                                               &ibeacon_dev_info->major_id,
+                                               &ibeacon_dev_info->minor_id,
+                                               &ibeacon_dev_info->measured_power);
+               _bt_send_ibeacon_scan_result_event(ibeacon_dev_info);
+               g_free(ibeacon_dev_info);
+       }
 }
 
 void __bt_set_audio_values(gboolean connected, char *address)
@@ -2285,7 +2312,7 @@ void _bt_handle_headset_event(GVariant *msg, const char *path)
                        _bt_convert_addr_string_to_type(device_address.addr,
                                                        wait_list->address);
                        _bt_audio_connect(wait_list->req_id, wait_list->type,
-                                       &device_address, wait_list->out_param1);
+                                       &device_address, NULL);
                        _bt_rel_wait_data();
                } else if (event == BLUETOOTH_EVENT_AG_CONNECTED) {
                        /* Add data to the connected list */
@@ -2422,7 +2449,7 @@ void _bt_handle_sink_event(GVariant *msg, const char *path)
                                        _bt_audio_connect(wait_list->req_id,
                                                                wait_list->type,
                                                                &device_address,
-                                                               wait_list->out_param1);
+                                                               NULL);
                                }
                                g_free(address);
                        } else if (strcasecmp(value, "Connected") == 0) {
@@ -3531,6 +3558,36 @@ static int __bt_init_obexd_receiver(void)
        return BLUETOOTH_ERROR_NONE;
 }
 
+gboolean __bt_reinit_obexd_receiver(gpointer user_data)
+{
+       static int retry_cnt = 0;
+       int result = BLUETOOTH_ERROR_NONE;
+
+       BT_DBG("+");
+
+       result = __bt_init_obexd_receiver();
+       if (result != BLUETOOTH_ERROR_NONE) {
+               /* 20 ms * 50 = 10 seconds. During 10 seconds fail to initialize,
+                  then it is not the timing issue. Just can't use the session bus connection */
+               if (retry_cnt > 100) {
+                       BT_ERR("Fail to init obexd receiver by 50 times.");
+                       retry_cnt = 0;
+                       session_reinit_timer = 0;
+                       return FALSE;
+               }
+               retry_cnt++;
+               BT_DBG("Retry to initialize the obexd receiver");
+               return TRUE;
+       }
+
+       retry_cnt = 0;
+       session_reinit_timer = 0;
+
+       BT_DBG("-");
+
+       return FALSE;
+}
+
 /* To receive the event from bluez */
 int _bt_init_service_event_receiver(void)
 {
@@ -3542,9 +3599,17 @@ int _bt_init_service_event_receiver(void)
        retv_if(result != BLUETOOTH_ERROR_NONE, result);
 
        result = __bt_init_obexd_receiver();
-       if (result != BLUETOOTH_ERROR_NONE)
+       if (result != BLUETOOTH_ERROR_NONE) {
                BT_ERR("Fail to init obexd receiver");
 
+               /* Try to re-initialize obexd receiver in the timer */
+               if (session_reinit_timer > 0)
+                       g_source_remove(session_reinit_timer);
+
+               session_reinit_timer = g_timeout_add(BT_SESSION_BUS_GET_TIMEOUT,
+                                                       (GSourceFunc)__bt_reinit_obexd_receiver, NULL);
+       }
+
        BT_DBG("-");
 
        return BLUETOOTH_ERROR_NONE;