Merge tizen_next codes into tizen branch
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-event-receiver.c
index ce0ef6a..f49f812 100644 (file)
@@ -39,6 +39,7 @@
 #include "bt-service-headset-connection.h"
 #include "bt-service-avrcp-controller.h"
 #include "bt-service-hid.h"
+#include "bt-service-proximity.h"
 
 #include "bt-service-opp-client.h"
 
@@ -73,7 +74,7 @@ typedef enum {
        OBEX_PBAP = (1 << 4),
        OBEX_IRMC = (1 << 5),
        OBEX_PCSUITE = (1 << 6),
-       OBEX_SYNCEVOLUTION =    (1 << 7),
+       OBEX_SYNCEVOLUTION = (1 << 7),
        OBEX_MAS = (1 << 8),
 } bluetooth_obex_connection_type_t;
 
@@ -105,7 +106,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;
@@ -202,7 +203,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) {
@@ -308,18 +309,16 @@ void __bt_set_device_values(gboolean connected, int state)
 {
        int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
 
-       if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0) {
+       if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
                BT_ERR("vconf_get_int failed");
-       }
 
        if (connected == TRUE)
                bt_device_state |= state;
        else if (bt_device_state & state)
                bt_device_state ^= state;
 
-       if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0) {
+       if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0)
                BT_ERR("vconf_set_int failed");
-       }
 }
 
 gboolean _bt_discovery_finished_cb(gpointer user_data)
@@ -328,9 +327,8 @@ gboolean _bt_discovery_finished_cb(gpointer user_data)
        event_id = 0;
        GVariant *param = NULL;
        if (_bt_get_discovering_property(DISCOVERY_ROLE_BREDR) == FALSE) {
-               if (_bt_get_cancel_by_user() == TRUE) {
+               if (_bt_get_cancel_by_user() == TRUE)
                        result = BLUETOOTH_ERROR_CANCEL_BY_USER;
-               }
 
                _bt_set_cancel_by_user(FALSE);
                _bt_set_discovery_status(FALSE);
@@ -349,15 +347,14 @@ static gboolean __bt_le_discovery_finished_cb(gpointer user_data)
        event_id = 0;
        GVariant *param = NULL;
        if (_bt_get_discovering_property(DISCOVERY_ROLE_LE) == FALSE) {
-               if (_bt_get_cancel_by_user() == TRUE) {
+               if (_bt_get_cancel_by_user() == TRUE)
                        result = BLUETOOTH_ERROR_CANCEL_BY_USER;
-               }
 
                g_list_free_full(p_adv_ind_list, __bt_free_bt_le_adv_info_t);
                p_adv_ind_list = NULL;
 
                _bt_set_cancel_by_user(FALSE);
-                _bt_disable_all_scanner_status();
+               _bt_disable_all_scanner_status();
                _bt_set_le_scan_status(FALSE);
                param = g_variant_new("(i)", result);
                _bt_send_event(BT_LE_ADAPTER_EVENT,
@@ -433,6 +430,22 @@ static void __bt_device_remote_connected_properties(
        BT_DBG("-");
 }
 
+gboolean __bt_handle_is_flight_mode_enabled(void)
+{
+#ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
+       int is_flight_mode = 0;
+       int ret = -1;
+
+       ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &is_flight_mode);
+       if (ret != 0)
+               BT_ERR("vconf_get_bool failed");
+
+       return (is_flight_mode == 0) ? FALSE : TRUE;
+#else
+       return FALSE;
+#endif
+}
+
 void _bt_handle_adapter_event(GVariant *msg, const char *member)
 {
        BT_DBG("+");
@@ -581,10 +594,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("-");
 }
@@ -771,7 +790,10 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
 #else
                                if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_state) == 0 &&
                                bt_state != VCONFKEY_BT_STATUS_OFF) {
-                                       _bt_disable_adapter();
+                                       if (__bt_handle_is_flight_mode_enabled() == FALSE)
+                                               _bt_disable_adapter();
+                                       else
+                                               _bt_handle_adapter_removed();
                                }
 #endif
                                if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_state) == 0 &&
@@ -1278,6 +1300,70 @@ static void __bt_media_control_changed_event(GVariant *msg, const char *path)
        BT_DBG("-");
 }
 
+int get_alert_level_enum(const char *alert)
+{
+       int lvl = -1;
+
+       if (strcmp(alert, "none") == 0)
+               lvl = BT_PXP_ALERT_NONE;
+       else if (strcmp(alert, "mild") == 0)
+               lvl = BT_PXP_ALERT_MILD;
+       else if (strcmp(alert, "high") == 0)
+               lvl = BT_PXP_ALERT_HIGH;
+
+       return lvl;
+}
+
+static void _bt_handle_pxp_property_changed_event(GVariant *msg, const char *path, int role)
+{
+       int result = BLUETOOTH_ERROR_NONE;
+       int service_type;
+       int alert_lvl;
+       GVariantIter value_iter;
+       char *property = NULL;
+       char *address;
+       GVariant *val = NULL;
+       GVariant *child = NULL;
+       GVariant *param = NULL;
+       g_variant_iter_init(&value_iter, msg);
+
+       BT_DBG("+");
+       while ((child = g_variant_iter_next_value(&value_iter))) {
+               g_variant_get(child, "{sv}", &property, &val);
+               BT_INFO("Property %s", property);
+
+               if ((strcasecmp(property, "LinkLossAlertLevel") == 0) ||
+                       (strcasecmp(property, "ImmediateAlertLevel") == 0)) {
+                       char *alert_str = NULL;
+
+                       address = g_malloc0(BT_ADDRESS_STRING_SIZE);
+                       _bt_convert_device_path_to_address(path, address);
+
+                       if (strcasecmp(property, "LinkLossAlertLevel") == 0)
+                               service_type = BT_PXP_PROPERTY_LLS;
+                       else
+                               service_type = BT_PXP_PROPERTY_IAS;
+
+                       g_variant_get(val, "s", &alert_str);
+                       alert_lvl = get_alert_level_enum(alert_str);
+
+                       param = g_variant_new("(isiii)", result, address,
+                                                               role, service_type, alert_lvl);
+
+                       /* Send event to application */
+                       _bt_send_event(BT_DEVICE_EVENT,
+                                       BLUETOOTH_EVENT_PXP_PROPERTY_CHANGED,
+                                       param);
+                       g_free(address);
+                       g_free(alert_str);
+               }
+               g_free(property);
+               g_variant_unref(child);
+               g_variant_unref(val);
+       }
+       BT_DBG("-");
+}
+
 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path)
 {
        char *interface_name = NULL;
@@ -1307,6 +1393,14 @@ void _bt_handle_property_changed_event(GVariant *msg, const char *object_path)
        } else if (strcasecmp(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
                __bt_gatt_char_property_changed_event(val,
                                        object_path);
+       } else if (strcasecmp(interface_name, BT_PROXIMITY_REPORTER_INTERFACE) == 0) {
+               BT_DBG("BT_PROXIMITY_REPORTER_INTERFACE");
+               _bt_handle_pxp_property_changed_event(val,
+                                       object_path, BT_PXP_REPORTER_ROLE);
+       } else if (strcasecmp(interface_name, BT_PROXIMITY_MONITOR_INTERFACE) == 0) {
+               BT_DBG("BT_PROXIMITY_MONITOR_INTERFACE");
+               _bt_handle_pxp_property_changed_event(val,
+                                       object_path, BT_PXP_MONITOR_ROLE);
        }
        g_variant_unref(val);
 }
@@ -1329,13 +1423,13 @@ void __bt_opc_property_changed_event(GVariant *msg,
                        g_variant_get(val, "s", &status);
                        BT_DBG("Status is %s", status);
 
-                       if (strcasecmp(status, "active") == 0) {
+                       if (strcasecmp(status, "active") == 0)
                                _bt_obex_client_started(path);
-                       } else if (strcasecmp(status, "complete") == 0) {
+                       else if (strcasecmp(status, "complete") == 0)
                                _bt_obex_client_completed(path, TRUE);
-                       } else if (strcasecmp(status, "error") == 0) {
+                       else if (strcasecmp(status, "error") == 0)
                                _bt_obex_client_completed(path, FALSE);
-                       }
+
                        g_free(status);
                } else if (strcasecmp(property, "Transferred") == 0) {
                        guint64 transferred  = 0;
@@ -1835,7 +1929,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);
@@ -1950,7 +2044,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) {
 
@@ -2001,7 +2095,7 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                                _bt_audio_stop_auto_connect();
 #endif
                                _bt_add_headset_to_list(BT_AUDIO_A2DP_SOURCE,
-                                                    BT_STATE_CONNECTED, address);
+                                               BT_STATE_CONNECTED, address);
                                _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
                                        g_variant_new("(is)", result, address));
                        } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
@@ -2025,7 +2119,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();
                                }
@@ -2167,8 +2261,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)
@@ -2177,24 +2289,19 @@ void __bt_set_audio_values(gboolean connected, char *address)
        int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
 
        /*  Set the headset name */
-       if (connected == TRUE) {
+       if (connected == TRUE)
                name = _bt_get_bonded_device_name(address);
-       } else {
+       else
                name = g_strdup("");
-       }
 
-       if (vconf_set_str(VCONFKEY_BT_HEADSET_NAME,
-                                       name) != 0) {
+       if (vconf_set_str(VCONFKEY_BT_HEADSET_NAME, name) != 0)
                BT_ERR("vconf_set_str failed");
-       }
 
        g_free(name);
 
        /*  Set the headset state */
-       if (vconf_get_int(VCONFKEY_BT_DEVICE,
-                               &bt_device_state) != 0) {
+       if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
                BT_ERR("vconf_get_int failed");
-       }
 
 #ifdef TIZEN_SUPPORT_DUAL_HF
        if ((connected == TRUE) &&
@@ -2205,11 +2312,10 @@ void __bt_set_audio_values(gboolean connected, char *address)
                bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
        }
 #else
-       if (connected == TRUE) {
+       if (connected == TRUE)
                bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
-       } else if (bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) {
+       else if (bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED)
                bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
-       }
 #endif
 
        if (vconf_set_int(VCONFKEY_BT_DEVICE,
@@ -2288,7 +2394,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 */
@@ -2358,7 +2464,7 @@ void _bt_handle_headset_event(GVariant *msg, const char *path)
        if (property)
                g_free(property);
        g_variant_unref(value);
- }
+}
 
 void _bt_handle_sink_event(GVariant *msg, const char *path)
 {
@@ -2425,7 +2531,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) {
@@ -2693,13 +2799,13 @@ static  void __bt_manager_event_filter(GDBusConnection *connection,
                BT_DBG("InterfacesRemoved");
                _bt_handle_adapter_removed();
 #endif
-               if (g_strcmp0(interface_name, BT_MEDIATRANSPORT_INTERFACE) == 0) {
+               if (g_strcmp0(interface_name, BT_MEDIATRANSPORT_INTERFACE) == 0)
                        bt_event = BT_MEDIA_TRANSFER_EVENT;
-               } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
+               else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0)
                        bt_event = BT_DEVICE_EVENT;
-               } else if (g_strcmp0(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
+               else if (g_strcmp0(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0)
                        bt_event = BT_AVRCP_CONTROL_EVENT;
-               }
+
                if ((bt_event != 0) && (bt_event != BT_MEDIA_TRANSFER_EVENT)) {
                        _bt_handle_adapter_event(parameters, signal_name);
                        if (bt_event == BT_AVRCP_CONTROL_EVENT) {
@@ -3617,4 +3723,3 @@ void _bt_deinit_service_event_receiver(void)
 
        BT_DBG("-");
 }
-