dynamic_sensor: Use HID unique ID for head tracker UUID.

Also, don't require 16-bit strings for HID name, manufacturer, and
unique ID. Also change dump of HID unique ID to be in hex.

Bug: 213483369
Test: Verified that the standard Android head tracker sensor uses the
 HID unique ID for the sensor UUID.
Test: Verified that custom Android sensors use a fabricated UUID.
Change-Id: I125f84187868543dccab758e22dce744c013578e
diff --git a/modules/sensors/dynamic_sensor/HidRawSensor.cpp b/modules/sensors/dynamic_sensor/HidRawSensor.cpp
index 8aaf2d4..6654228 100644
--- a/modules/sensors/dynamic_sensor/HidRawSensor.cpp
+++ b/modules/sensors/dynamic_sensor/HidRawSensor.cpp
@@ -439,6 +439,7 @@
 
     featureValue->reportModeFlag = SENSOR_FLAG_SPECIAL_REPORTING_MODE;
     featureValue->isWakeUp = false;
+    featureValue->useUniqueIdForUuid = false;
     memset(featureValue->uuid, 0, sizeof(featureValue->uuid));
     featureValue->isAndroidCustom = false;
 }
@@ -465,28 +466,16 @@
         for (const auto & r : packet.reports) {
             switch (r.usage) {
                 case FRIENDLY_NAME:
-                    if (!r.isByteAligned() || r.bitSize != 16 || r.count < 1) {
-                        // invalid friendly name
-                        break;
-                    }
                     if (decodeString(r, buffer, &str) && !str.empty()) {
                         featureValue->name = str;
                     }
                     break;
                 case SENSOR_MANUFACTURER:
-                    if (!r.isByteAligned() || r.bitSize != 16 || r.count < 1) {
-                        // invalid manufacturer
-                        break;
-                    }
                     if (decodeString(r, buffer, &str) && !str.empty()) {
                         featureValue->vendor = str;
                     }
                     break;
                 case PERSISTENT_UNIQUE_ID:
-                    if (!r.isByteAligned() || r.bitSize != 16 || r.count < 1) {
-                        // invalid unique id string
-                        break;
-                    }
                     if (decodeString(r, buffer, &str) && !str.empty()) {
                         featureValue->uniqueId = str;
                     }
@@ -541,10 +530,19 @@
     }
 
     // initialize uuid field, use name, vendor and uniqueId
-    if (mFeatureInfo.name.size() >= 4
-            && mFeatureInfo.vendor.size() >= 4
-            && mFeatureInfo.typeString.size() >= 4
-            && mFeatureInfo.uniqueId.size() >= 4) {
+    // initialize uuid field using one of the following methods:
+    //
+    // 1. use uniqueId
+    // 2. use name, vendor and uniqueId
+    if (mFeatureInfo.useUniqueIdForUuid) {
+        if (mFeatureInfo.uniqueId.size() == sizeof(mFeatureInfo.uuid)) {
+            memcpy(mFeatureInfo.uuid, mFeatureInfo.uniqueId.c_str(),
+                   sizeof(mFeatureInfo.uuid));
+        }
+    } else if (mFeatureInfo.name.size() >= 4
+                   && mFeatureInfo.vendor.size() >= 4
+                   && mFeatureInfo.typeString.size() >= 4
+                   && mFeatureInfo.uniqueId.size() >= 4) {
         uint32_t tmp[4], h;
         std::hash<std::string> stringHash;
         h = stringHash(mFeatureInfo.uniqueId);
@@ -643,6 +641,11 @@
     mFeatureInfo.permission = "";
     mFeatureInfo.isWakeUp = false;
 
+    // HID head tracker sensors must use the HID unique ID for the sensor UUID
+    // to permit association between the sensor and audio device (see
+    // specification for HEAD_TRACKER in SensorType).
+    mFeatureInfo.useUniqueIdForUuid = true;
+
     return true;
 }
 
@@ -1055,11 +1058,15 @@
           << "  fifoSize: " << mFeatureInfo.fifoSize << LOG_ENDL
           << "  fifoMaxSize: " << mFeatureInfo.fifoMaxSize << LOG_ENDL
           << "  reportModeFlag: " << mFeatureInfo.reportModeFlag << LOG_ENDL
-          << "  isWakeUp: " << (mFeatureInfo.isWakeUp ? "true" : "false") << LOG_ENDL
-          << "  uniqueId: " << mFeatureInfo.uniqueId << LOG_ENDL
-          << "  uuid: ";
+          << "  isWakeUp: " << (mFeatureInfo.isWakeUp ? "true" : "false") << LOG_ENDL;
 
-    ss << std::hex << std::setfill('0');
+    ss << "  uniqueId: " << std::hex << std::setfill('0');
+    for (auto d : mFeatureInfo.uniqueId) {
+          ss << std::setw(2) << static_cast<int>(d) << " ";
+    }
+    ss << std::dec << std::setfill(' ') << LOG_ENDL;
+
+    ss << "  uuid: " << std::hex << std::setfill('0');
     for (auto d : mFeatureInfo.uuid) {
           ss << std::setw(2) << static_cast<int>(d) << " ";
     }
diff --git a/modules/sensors/dynamic_sensor/HidRawSensor.h b/modules/sensors/dynamic_sensor/HidRawSensor.h
index 0989651..f6d13b5 100644
--- a/modules/sensors/dynamic_sensor/HidRawSensor.h
+++ b/modules/sensors/dynamic_sensor/HidRawSensor.h
@@ -86,6 +86,7 @@
         size_t fifoMaxSize;
         uint32_t reportModeFlag;
         bool isWakeUp;
+        bool useUniqueIdForUuid;
 
         // dynamic sensor specific
         std::string uniqueId;