Skip to content

Commit

Permalink
Bug 857896 - Convert device property "Connected" into a boolean if bl…
Browse files Browse the repository at this point in the history
…uez returns a two-byte array. r=echou, r=qdot, a=tef+
  • Loading branch information
Gina Yeh committed Apr 16, 2013
1 parent d0bae83 commit f0b3a97
Showing 1 changed file with 33 additions and 58 deletions.
91 changes: 33 additions & 58 deletions dom/bluetooth/linux/BluetoothDBusService.cpp
Expand Up @@ -90,7 +90,7 @@ static Properties sDeviceProperties[] = {
{"Class", DBUS_TYPE_UINT32},
{"UUIDs", DBUS_TYPE_ARRAY},
{"Paired", DBUS_TYPE_BOOLEAN},
{"Connected", DBUS_TYPE_ARRAY},
{"Connected", DBUS_TYPE_BOOLEAN},
{"Trusted", DBUS_TYPE_BOOLEAN},
{"Blocked", DBUS_TYPE_BOOLEAN},
{"Alias", DBUS_TYPE_STRING},
Expand Down Expand Up @@ -1002,7 +1002,7 @@ GetProperty(DBusMessageIter aIter, Properties* aPropertyTypes,
DBusMessageIter prop_val, array_val_iter;
char* property = NULL;
uint32_t array_type;
int i, type;
int i, expectedType, receivedType;

if (dbus_message_iter_get_arg_type(&aIter) != DBUS_TYPE_STRING) {
return false;
Expand Down Expand Up @@ -1030,23 +1030,35 @@ GetProperty(DBusMessageIter aIter, Properties* aPropertyTypes,
*aPropIndex = i;

dbus_message_iter_recurse(&aIter, &prop_val);
type = aPropertyTypes[*aPropIndex].type;
expectedType = aPropertyTypes[*aPropIndex].type;
receivedType = dbus_message_iter_get_arg_type(&prop_val);

if (dbus_message_iter_get_arg_type(&prop_val) != type) {
/**
* Bug 857896. Since device property "Connected" could be a boolean value or
* an 2-byte array, we need to check the value type here and convert the
* first byte into a boolean manually.
*/
bool convert = false;
if (propertyName.EqualsLiteral("Connected") &&
receivedType == DBUS_TYPE_ARRAY) {
convert = true;
}

if ((receivedType != expectedType) && !convert) {
NS_WARNING("Iterator not type we expect!");
nsAutoCString str;
str += "Property Name: ;";
str += NS_ConvertUTF16toUTF8(propertyName);
str += " Property Type Expected: ;";
str += type;
str += " Property Type Received: ";
str += dbus_message_iter_get_arg_type(&prop_val);
nsCString str;
str.AppendLiteral("Property Name: ");
str.Append(NS_ConvertUTF16toUTF8(propertyName));
str.AppendLiteral(", Property Type Expected: ");
str.AppendInt(expectedType);
str.AppendLiteral(", Property Type Received: ");
str.AppendInt(receivedType);
NS_WARNING(str.get());
return false;
}

BluetoothValue propertyValue;
switch (type) {
switch (receivedType) {
case DBUS_TYPE_STRING:
case DBUS_TYPE_OBJECT_PATH:
const char* c;
Expand Down Expand Up @@ -1098,6 +1110,14 @@ GetProperty(DBusMessageIter aIter, Properties* aPropertyTypes,
default:
NS_NOTREACHED("Cannot find dbus message type!");
}

if (convert) {
MOZ_ASSERT(propertyValue.type() == BluetoothValue::TArrayOfuint8_t);

bool b = propertyValue.get_ArrayOfuint8_t()[0];
propertyValue = BluetoothValue(b);
}

aProperties.AppendElement(BluetoothNamedValue(propertyName, propertyValue));
return true;
}
Expand Down Expand Up @@ -1161,43 +1181,14 @@ UnpackAdapterPropertiesMessage(DBusMessage* aMsg, DBusError* aErr,
ArrayLength(sAdapterProperties));
}

bool
ReplaceConnectedType(Properties* sourceProperties, Properties** destProperties, int aPropertyTypeLen)
{
if (!IsDeviceConnectedTypeBoolean()) {
return false;
}
*destProperties = (Properties *) malloc(sizeof(Properties) * aPropertyTypeLen);
if (*destProperties) {
CopyProperties(sourceProperties, *destProperties, aPropertyTypeLen);
int index = GetPropertyIndex(*destProperties, "Connected", aPropertyTypeLen);
if (index >= 0) {
(*destProperties)[index].type = DBUS_TYPE_BOOLEAN;
return true;
} else {
free(*destProperties);
}
}
return false;
}

void
UnpackDevicePropertiesMessage(DBusMessage* aMsg, DBusError* aErr,
BluetoothValue& aValue,
nsAString& aErrorStr)
{
Properties* props = sDeviceProperties;
Properties* newProps;
bool replaced = ReplaceConnectedType(sDeviceProperties, &newProps, ArrayLength(sDeviceProperties));
if (replaced) {
props = newProps;
}
UnpackPropertiesMessage(aMsg, aErr, aValue, aErrorStr,
props,
sDeviceProperties,
ArrayLength(sDeviceProperties));
if (replaced) {
free(newProps);
}
}

void
Expand Down Expand Up @@ -1346,19 +1337,11 @@ EventFilter(DBusConnection* aConn, DBusMessage* aMsg, void* aData)

if (dbus_message_iter_next(&iter)) {
Properties* props = sDeviceProperties;
Properties* newProps;
bool replaced = ReplaceConnectedType(sDeviceProperties, &newProps, ArrayLength(sDeviceProperties));
if (replaced) {
props = newProps;
}
ParseProperties(&iter,
v,
errorStr,
props,
ArrayLength(sDeviceProperties));
if (replaced) {
free(newProps);
}
if (v.type() == BluetoothValue::TArrayOfBluetoothNamedValue)
{
// The DBus DeviceFound message actually passes back a key value object
Expand Down Expand Up @@ -1451,19 +1434,11 @@ EventFilter(DBusConnection* aConn, DBusMessage* aMsg, void* aData)
ArrayLength(sAdapterProperties));
} else if (dbus_message_is_signal(aMsg, DBUS_DEVICE_IFACE, "PropertyChanged")) {
Properties* props = sDeviceProperties;
Properties* newProps;
bool replaced = ReplaceConnectedType(sDeviceProperties, &newProps, ArrayLength(sDeviceProperties));
if (replaced) {
props = newProps;
}
ParsePropertyChange(aMsg,
v,
errorStr,
props,
ArrayLength(sDeviceProperties));
if (replaced) {
free(newProps);
}
if (v.get_ArrayOfBluetoothNamedValue()[0].name().EqualsLiteral("Paired")) {
// transfer signal to BluetoothService and
// broadcast system message of bluetooth-pairingstatuschanged
Expand Down

0 comments on commit f0b3a97

Please sign in to comment.