OSDN Git Service

android/hal-bluetooth: Fix sending invalid adapter property
authorSzymon Janc <szymon.janc@tieto.com>
Fri, 29 Nov 2013 08:05:58 +0000 (09:05 +0100)
committerJohan Hedberg <johan.hedberg@intel.com>
Fri, 29 Nov 2013 08:32:00 +0000 (10:32 +0200)
If property to be set is of enum type it should be first converted to
byte value as size of enum might varry depending on architecture.

To keep code simple command buffer uses len received from framework
as this is more or equal to HAL property size.

android/hal-bluetooth.c

index f232afd..87d6fc7 100644 (file)
@@ -35,6 +35,18 @@ static const bt_callbacks_t *bt_hal_cbacks = NULL;
        e = *((uint8_t *) (hal_prop->val)); \
 } while (0)
 
+#define enum_prop_from_hal(prop, hal_len, hal_val, enum_type) do { \
+       enum_type e; \
+       if (prop->len != sizeof(e)) { \
+               error("invalid HAL property %u (%u vs %zu), aborting ", \
+                                       prop->type, prop->len, sizeof(e)); \
+               exit(EXIT_FAILURE); \
+       } \
+       memcpy(&e, prop->val, sizeof(e)); \
+       *((uint8_t *) hal_val) = e; /* enums are mapped to 1 byte */ \
+       *hal_len = 1; \
+} while (0)
+
 static void handle_adapter_state_changed(void *buf, uint16_t len)
 {
        struct hal_ev_adapter_state_changed *ev = buf;
@@ -91,6 +103,23 @@ static void adapter_props_to_hal(bt_property_t *send_props,
        exit(EXIT_FAILURE);
 }
 
+static void adapter_prop_from_hal(const bt_property_t *property, uint8_t *type,
+                                               uint16_t *len, void *val)
+{
+       /* type match IPC type */
+       *type = property->type;
+
+       switch(property->type) {
+       case HAL_PROP_ADAPTER_SCAN_MODE:
+               enum_prop_from_hal(property, len, val, bt_scan_mode_t);
+               break;
+       default:
+               *len = property->len;
+               memcpy(val, property->val, property->len);
+               break;
+       }
+}
+
 static void device_props_to_hal(bt_property_t *send_props,
                                struct hal_property *prop, uint8_t num_props,
                                uint16_t len)
@@ -458,13 +487,10 @@ static int set_adapter_property(const bt_property_t *property)
        if (!interface_ready())
                return BT_STATUS_NOT_READY;
 
-       /* type match IPC type */
-       cmd->type = property->type;
-       cmd->len = property->len;
-       memcpy(cmd->val, property->val, property->len);
+       adapter_prop_from_hal(property, &cmd->type, &cmd->len, cmd->val);
 
        return hal_ipc_cmd(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_SET_ADAPTER_PROP,
-                                       sizeof(buf), cmd, 0, NULL, NULL);
+                               sizeof(*cmd) + cmd->len, cmd, 0, NULL, NULL);
 }
 
 static int get_remote_device_properties(bt_bdaddr_t *remote_addr)