Use Trusted Profiles instead of Restricted Profiles 09/143609/3
authorBiman Paul <biman.paul@samsung.com>
Fri, 4 Aug 2017 14:29:40 +0000 (19:59 +0530)
committerBiman Paul <biman.paul@samsung.com>
Fri, 11 Aug 2017 09:13:53 +0000 (14:43 +0530)
Trusted profiles and restricted profiles concept are similar.
Trusted profiles concept supports 3 states for each profile:
1. Unknown state: Let the user decide using authorization.
2. Blocked: Do not allow connection
3. Trusted: Always accept connection
In case of restricted profiles we only have Blocked and Trusted
states but Unknown state can be treated as Trusted because by
default, we should allow connection in case of Audio profiles.
We can remove Restricted Profiles implementation and this would
reduce redundant code.

Change-Id: I0200611a54745f4fc701e7d5e4ace4e1d31b898c
Signed-off-by: Biman Paul <biman.paul@samsung.com>
profiles/audio/a2dp.c
src/device.c
src/device.h
src/profile.c

index 78c4dc2..d55a4c7 100644 (file)
@@ -1555,7 +1555,7 @@ static void confirm_cb(GIOChannel *io, gpointer data)
 {
        gboolean restricted = FALSE;
 
-       restricted = device_is_profile_restricted(device, A2DP_SINK_UUID);
+       restricted = device_is_profile_blocked(device, A2DP_SINK_UUID);
        if (restricted) {
                DBG("A2DP is restricted");
                goto drop;
index 85f1b39..9e05f00 100644 (file)
@@ -225,12 +225,9 @@ struct trusted_profile_t {
        uint32_t        pbap:2;
        uint32_t        map:2;
        uint32_t        sap:2;
+       uint32_t        hfp_hs:2;
+       uint32_t        a2dp:2;
 } __packed;
-
-struct restricted_profile_t {
-       uint32_t        hfp_hs;
-       uint32_t        a2dp;
-};
 #endif
 
 struct btd_device {
@@ -304,7 +301,6 @@ struct btd_device {
        gboolean        trusted;
 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
        struct trusted_profile_t trusted_profiles;
-       struct restricted_profile_t restricted_profiles;
 #endif
        gboolean        blocked;
        gboolean        auto_connect;
@@ -360,17 +356,11 @@ typedef enum {
 #define PBAP_SHIFT_OFFSET 0
 #define MAP_SHIFT_OFFSET 2
 #define SAP_SHIFT_OFFSET 4
+#define HFP_HS_SHIFT_OFFSET 6
+#define A2DP_SHIFT_OFFSET 8
 
 #define PROFILE_SUPPORTED 0x3 /* This corresponds to binary 0b11*/
 
-typedef enum {
-       CONNECTION_PERMITTED = 0x0, /* 0b00 */
-       CONNECTION_RESTRICTED = 0x1, /* 0b01 */
-} bt_profile_restricted_states;
-
-#define HFP_HS_SHIFT_OFFSET 0
-#define A2DP_SHIFT_OFFSET 2
-
 #endif
 
 static int device_browse_gatt(struct btd_device *device, DBusMessage *msg);
@@ -572,18 +562,12 @@ static gboolean store_device_info_cb(gpointer user_data)
        struct trusted_profile_t trust_profile = device->trusted_profiles;
        int trusted_profiles = (trust_profile.pbap << PBAP_SHIFT_OFFSET) |
                        (trust_profile.map << MAP_SHIFT_OFFSET) |
-                       (trust_profile.sap << SAP_SHIFT_OFFSET);
+                       (trust_profile.sap << SAP_SHIFT_OFFSET) |
+                       (trust_profile.hfp_hs << HFP_HS_SHIFT_OFFSET) |
+                       (trust_profile.a2dp << A2DP_SHIFT_OFFSET);
        DBG("Storing TrustedProfiles %d", trusted_profiles);
        g_key_file_set_integer(key_file, "General", "TrustedProfiles",
                                                        trusted_profiles);
-
-       struct restricted_profile_t restrict_profile = device->restricted_profiles;
-       int restricted_profiles = (restrict_profile.hfp_hs << HFP_HS_SHIFT_OFFSET) |
-                       (restrict_profile.a2dp << A2DP_SHIFT_OFFSET);
-       DBG("Storing RestrictedProfiles %d", restricted_profiles);
-       g_key_file_set_integer(key_file, "General", "RestrictedProfiles",
-                                                       restricted_profiles);
-
 #endif
        g_key_file_set_boolean(key_file, "General", "Blocked",
                                                        device->blocked);
@@ -937,18 +921,11 @@ gboolean device_is_profile_trusted(struct btd_device *device,
        } else if (g_strcmp0(uuid, SAP_UUID) == 0) {
                if (device->trusted_profiles.sap == SUPPORTED_TRUSTED)
                        return TRUE;
-       }
-       return FALSE;
-}
-
-gboolean device_is_profile_restricted(struct btd_device *device,
-               const char *uuid)
-{
-       if (g_strcmp0(uuid, HFP_HS_UUID) == 0) {
-               if (device->restricted_profiles.hfp_hs == CONNECTION_RESTRICTED)
+       } else if (g_strcmp0(uuid, HFP_HS_UUID) == 0) {
+               if (device->trusted_profiles.hfp_hs == SUPPORTED_TRUSTED)
                        return TRUE;
        } else if (g_strcmp0(uuid, A2DP_SINK_UUID) == 0) {
-               if (device->restricted_profiles.a2dp == CONNECTION_RESTRICTED)
+               if (device->trusted_profiles.a2dp == SUPPORTED_TRUSTED)
                        return TRUE;
        }
        return FALSE;
@@ -966,6 +943,12 @@ gboolean device_is_profile_blocked(struct btd_device *device,
        } else if (g_strcmp0(uuid, SAP_UUID) == 0) {
                if (device->trusted_profiles.sap == SUPPORTED_BLOCKED)
                        return TRUE;
+       } else if (g_strcmp0(uuid, HFP_HS_UUID) == 0) {
+               if (device->trusted_profiles.hfp_hs == SUPPORTED_BLOCKED)
+                       return TRUE;
+       } else if (g_strcmp0(uuid, A2DP_SINK_UUID) == 0) {
+               if (device->trusted_profiles.a2dp == SUPPORTED_BLOCKED)
+                       return TRUE;
        }
        return FALSE;
 }
@@ -1337,26 +1320,15 @@ static gboolean dev_property_get_trusted_profiles(const GDBusPropertyTable *prop
        uint32_t pbap = device->trusted_profiles.pbap;
        uint32_t map = device->trusted_profiles.map;
        uint32_t sap = device->trusted_profiles.sap;
+       uint32_t hfp_hs = device->trusted_profiles.hfp_hs;
+       uint32_t a2dp = device->trusted_profiles.a2dp;
 
        unsigned int val = (pbap << PBAP_SHIFT_OFFSET) |
                        (map << MAP_SHIFT_OFFSET) |
-                       (sap << SAP_SHIFT_OFFSET);
-
-       dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &val);
-
-       return TRUE;
-}
-
-static gboolean dev_property_get_restricted_profiles(const GDBusPropertyTable *property,
-                                       DBusMessageIter *iter, void *data)
-{
-       struct btd_device *device = data;
-       uint32_t hfp_hs = device->restricted_profiles.hfp_hs;
-       uint32_t a2dp = device->restricted_profiles.a2dp;
-
-       unsigned int val = (hfp_hs << HFP_HS_SHIFT_OFFSET) |
+                       (sap << SAP_SHIFT_OFFSET) |
+                       (hfp_hs << HFP_HS_SHIFT_OFFSET) |
                        (a2dp << A2DP_SHIFT_OFFSET);
-
+       DBG("TRUST :%x", val);
        dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &val);
 
        return TRUE;
@@ -3543,6 +3515,8 @@ static DBusMessage *set_trusted_profile(DBusConnection *conn,
        uint32_t pbap = dev->trusted_profiles.pbap;
        uint32_t map = dev->trusted_profiles.map;
        uint32_t sap = dev->trusted_profiles.sap;
+       uint32_t hfp_hs = dev->trusted_profiles.hfp_hs;
+       uint32_t a2dp = dev->trusted_profiles.a2dp;
 
        if (!dbus_message_get_args(msg, NULL,
                                DBUS_TYPE_STRING, &pattern,
@@ -3555,6 +3529,8 @@ static DBusMessage *set_trusted_profile(DBusConnection *conn,
        DBG("UUID : %s", uuid);
        DBG("profile Trusted : %d %d %d", dev->trusted_profiles.pbap,
                        dev->trusted_profiles.map, dev->trusted_profiles.sap);
+       DBG("profile Restricted : %d %d", dev->trusted_profiles.hfp_hs,
+                       dev->trusted_profiles.a2dp);
        if (g_strcmp0(uuid, OBEX_PBAP_UUID) == 0) {
                if (profile_trusted)
                        pbap = SUPPORTED_TRUSTED;
@@ -3570,50 +3546,21 @@ static DBusMessage *set_trusted_profile(DBusConnection *conn,
                        sap = SUPPORTED_TRUSTED;
                else
                        sap = SUPPORTED_BLOCKED;
-       } else {
-               return btd_error_invalid_args(msg);
-       }
-
-       btd_device_set_trusted_profiles(dev, pbap, map, sap);
-       return dbus_message_new_method_return(msg);
-}
-
-static DBusMessage *set_restricted_profile(DBusConnection *conn,
-                                               DBusMessage *msg, void *data)
-{
-       struct btd_device *dev = data;
-       dbus_bool_t profile_restricted;
-       const char *pattern;
-       char *uuid;
-       uint32_t hfp_hs = dev->restricted_profiles.hfp_hs;
-       uint32_t a2dp = dev->restricted_profiles.a2dp;
-
-       if (!dbus_message_get_args(msg, NULL,
-                               DBUS_TYPE_STRING, &pattern,
-                               DBUS_TYPE_BOOLEAN, &profile_restricted,
-                               DBUS_TYPE_INVALID))
-               return btd_error_invalid_args(msg);
-
-       DBG("Pattern : %s", pattern);
-       uuid = bt_name2string(pattern);
-       DBG("UUID : %s", uuid);
-       DBG("profile Restricted : %d %d", dev->restricted_profiles.hfp_hs,
-                       dev->restricted_profiles.a2dp);
-       if (g_strcmp0(uuid, HFP_HS_UUID) == 0) {
-               if (profile_restricted)
-                       hfp_hs = CONNECTION_RESTRICTED;
+       } else  if (g_strcmp0(uuid, HFP_HS_UUID) == 0) {
+               if (profile_trusted)
+                       hfp_hs = SUPPORTED_TRUSTED;
                else
-                       hfp_hs = CONNECTION_PERMITTED;
+                       hfp_hs = SUPPORTED_BLOCKED;
        } else if (g_strcmp0(uuid, A2DP_SINK_UUID) == 0) {
-               if (profile_restricted)
-                       a2dp = CONNECTION_RESTRICTED;
+               if (profile_trusted)
+                       a2dp = SUPPORTED_TRUSTED;
                else
-                       a2dp = CONNECTION_PERMITTED;
+                       a2dp = SUPPORTED_BLOCKED;
        } else {
                return btd_error_invalid_args(msg);
        }
 
-       btd_device_set_restricted_profiles(dev, hfp_hs, a2dp);
+       btd_device_set_trusted_profiles(dev, pbap, map, sap, hfp_hs, a2dp);
        return dbus_message_new_method_return(msg);
 }
 
@@ -3892,9 +3839,6 @@ static const GDBusMethodTable device_methods[] = {
        { GDBUS_METHOD("SetTrustedProfile",
                        GDBUS_ARGS({ "uuid", "s"}, { "trusted", "b"}), NULL,
                        set_trusted_profile) },
-       { GDBUS_METHOD("SetRestrictedProfile",
-                       GDBUS_ARGS({ "uuid", "s"}, { "restricted", "b"}), NULL,
-                       set_restricted_profile) },
 #endif
        { }
 };
@@ -3936,7 +3880,6 @@ static const GDBusPropertyTable device_properties[] = {
        { "IpspBtInterfaceInfo", "s", dev_property_get_ipsp_conn_bt_iface_name },
        { "AttMtu", "q", dev_property_get_att_mtu },
        { "TrustedProfiles", "u", dev_property_get_trusted_profiles},
-       { "RestrictedProfiles", "u", dev_property_get_restricted_profiles},
 #endif
        { "ManufacturerData", "a{qv}", dev_property_get_manufacturer_data,
                                NULL, dev_property_manufacturer_data_exist,
@@ -4324,13 +4267,10 @@ next:
                        (PROFILE_SUPPORTED << MAP_SHIFT_OFFSET)) >> MAP_SHIFT_OFFSET);
        device->trusted_profiles.sap = ((trusted_profiles &
                        (PROFILE_SUPPORTED << SAP_SHIFT_OFFSET)) >> SAP_SHIFT_OFFSET);
-
-       /* Load Restricted Profiles*/
-       int restricted_profiles = g_key_file_get_integer(key_file, "General",
-                                                       "RestrictedProfiles", NULL);
-       DBG("Loading RestrictedProfiles %d", restricted_profiles);
-       device->restricted_profiles.hfp_hs = (restricted_profiles >> HFP_HS_SHIFT_OFFSET) & 0x01;
-       device->restricted_profiles.a2dp = (restricted_profiles >> A2DP_SHIFT_OFFSET) & 0x01;
+       device->trusted_profiles.hfp_hs = ((trusted_profiles &
+                       (PROFILE_SUPPORTED << HFP_HS_SHIFT_OFFSET)) >> HFP_HS_SHIFT_OFFSET);
+       device->trusted_profiles.a2dp = ((trusted_profiles &
+                       (PROFILE_SUPPORTED << A2DP_SHIFT_OFFSET)) >> A2DP_SHIFT_OFFSET);
 
 #endif
 
@@ -5549,6 +5489,8 @@ void device_unpair(struct btd_device *device, gboolean remove_stored)
        device->trusted_profiles.pbap = SHOW_AUTHORIZATION;
        device->trusted_profiles.map = SHOW_AUTHORIZATION;
        device->trusted_profiles.sap = SHOW_AUTHORIZATION;
+       device->trusted_profiles.hfp_hs = SUPPORTED_TRUSTED;
+       device->trusted_profiles.a2dp = SUPPORTED_TRUSTED;
        if (device->alias != NULL) {
                /* Remove alias name because
                 * In UG if we rename and then unpair device and
@@ -7256,76 +7198,69 @@ void btd_device_set_trusted(struct btd_device *device, gboolean trusted)
 }
 
 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
+void device_disconnect_blocked(struct btd_device *device,
+                                                       const char *uuid)
+{
+       struct btd_service *service;
+       GSList *l;
+
+       if (!device || !uuid)
+               return;
+
+       l = find_service_with_uuid(device->services, uuid);
+       if (l == NULL)
+               return;
+
+       service = l->data;
+       if (btd_service_get_state(service) ==
+                       BTD_SERVICE_STATE_CONNECTED) {
+               int err;
+               err = btd_service_disconnect(service);
+               if (err)
+                       error("error: %s", strerror(-err));
+       }
+}
+
 void btd_device_set_trusted_profiles(struct btd_device *device,
-               uint32_t pbap, uint32_t map, uint32_t sap)
+               uint32_t pbap, uint32_t map, uint32_t sap,
+               uint32_t hfp_hs, uint32_t a2dp)
 {
        char *uuid = NULL;
        if (!device)
                return;
-       DBG("TrustedProfiles Parameters: [PBAP %d] [MAP %d] [SAP %d]", pbap, map, sap);
+       DBG("TrustedProfiles Parameters: [PBAP %d] [MAP %d] [SAP %d] [HFP %d] [A2DP %d]",
+                       pbap, map, sap, hfp_hs, a2dp);
 
        if (device->trusted_profiles.pbap == pbap &&
                        device->trusted_profiles.map == map &&
-                       device->trusted_profiles.sap == sap)
+                       device->trusted_profiles.sap == sap &&
+                       device->trusted_profiles.hfp_hs == hfp_hs &&
+                       device->trusted_profiles.a2dp == a2dp)
                return;
 
-       /* If any of the profile is Blocked than only assign uuid */
+       /* Disconnect OBEX based profiles if connected */
        if (device->trusted_profiles.pbap != pbap) {
                device->trusted_profiles.pbap = pbap;
                if (pbap == SUPPORTED_BLOCKED)
-                       uuid = OBEX_PSE_UUID;
+                       device_disconnect_blocked(device, OBEX_PSE_UUID);
        } else if (device->trusted_profiles.map != map) {
                device->trusted_profiles.map = map;
                if (map == SUPPORTED_BLOCKED)
-                       uuid = OBEX_MAP_UUID;
-       } else {
+                       device_disconnect_blocked(device, OBEX_MAP_UUID);
+       } else if (device->trusted_profiles.sap != sap) {
                device->trusted_profiles.sap = sap;
                if (sap == SUPPORTED_BLOCKED)
-                       uuid = SAP_UUID;
+                       device_disconnect_blocked(device, SAP_UUID);
+       } else if (device->trusted_profiles.hfp_hs != hfp_hs) {
+               device->trusted_profiles.hfp_hs = hfp_hs;
+       } else if (device->trusted_profiles.a2dp != a2dp) {
+               device->trusted_profiles.a2dp = a2dp;
        }
 
        store_device_info(device);
-
-       /* If uuid is set than only disconnect */
-       if (uuid) {
-               GSList *l;
-               l = find_service_with_uuid(device->services, uuid);
-               if (l == NULL)
-                       goto emit;
-               struct btd_service *service = l->data;
-               if (btd_service_get_state(service) ==
-                       BTD_SERVICE_STATE_CONNECTED) {
-                       int err;
-                       err = btd_service_disconnect(service);
-                       if (err)
-                               error("error: %s", strerror(-err));
-               }
-       }
-emit:
-
        g_dbus_emit_property_changed(dbus_conn, device->path,
                                        DEVICE_INTERFACE, "TrustedProfiles");
 }
-
-void btd_device_set_restricted_profiles(struct btd_device *device,
-               uint32_t hfp_hs, uint32_t a2dp)
-{
-       if (!device)
-               return;
-       DBG("RestrictedProfiles Parameters: [HFP %d] [A2DP %d]", hfp_hs, a2dp);
-
-       if (device->restricted_profiles.hfp_hs == hfp_hs &&
-                       device->restricted_profiles.a2dp == a2dp)
-               return;
-
-       device->restricted_profiles.hfp_hs = hfp_hs;
-       device->restricted_profiles.a2dp = a2dp;
-
-       store_device_info(device);
-
-       g_dbus_emit_property_changed(dbus_conn, device->path,
-                                       DEVICE_INTERFACE, "RestrictedProfiles");
-}
 #endif
 
 void device_set_bonded(struct btd_device *device, uint8_t bdaddr_type)
index d00a93c..93133e1 100644 (file)
@@ -110,7 +110,8 @@ void device_set_irk_value(struct btd_device *device, const uint8_t *val);
 void device_set_conn_update_state(struct btd_device *device, bool state);
 bool device_get_conn_update_state(struct btd_device *device);
 void btd_device_set_trusted_profiles(struct btd_device *device,
-               uint32_t pbap, uint32_t map, uint32_t sap);
+               uint32_t pbap, uint32_t map, uint32_t sap,
+               uint32_t hfp_hs, uint32_t a2dp);
 #endif
 gboolean device_is_temporary(struct btd_device *device);
 bool device_is_paired(struct btd_device *device, uint8_t bdaddr_type);
@@ -217,8 +218,6 @@ gboolean device_is_profile_trusted(struct btd_device *device,
                const char *uuid);
 gboolean device_is_profile_blocked(struct btd_device *device,
                const char *uuid);
-gboolean device_is_profile_restricted(struct btd_device *device,
-               const char *uuid);
 void btd_device_disconnect(struct btd_device *dev);
 void btd_device_set_legacy_pairing(struct btd_device *dev, bool legacy_pairing);
 void btd_device_set_svc_changed_indication(struct btd_device *dev, bool value);
index af2db7d..3caee5a 100644 (file)
@@ -1476,7 +1476,7 @@ static void ext_confirm(GIOChannel *io, gpointer user_data)
                device = btd_adapter_find_device(adapter_find(&src), &dst,
                                                                        BDADDR_BREDR);
                if (device) {
-                       restricted = device_is_profile_restricted(device, HFP_HS_UUID);
+                       restricted = device_is_profile_blocked(device, HFP_HS_UUID);
                        if (restricted) {
                                DBG("HFP_HS is restricted");
                                return;