Snap for 8593949 from 15693f5e93f0c0d68ebe858c1d4f3f98061c7819 to tm-release

Change-Id: Iad128c508ddb5ff9cb210e44c8e7b676f77b9b48
diff --git a/apps/ble_world/ble_world.cc b/apps/ble_world/ble_world.cc
index 4835caa..507b583 100644
--- a/apps/ble_world/ble_world.cc
+++ b/apps/ble_world/ble_world.cc
@@ -40,10 +40,8 @@
 
 enum ScanRequestType {
   NO_FILTER = 0,
-  SERVICE_DATA_UUID_16 = 1,
-  SERVICE_DATA_UUID_32 = 2,
-  SERVICE_DATA_UUID_128 = 3,
-  STOP_SCAN = 4,
+  SERVICE_DATA_16 = 1,
+  STOP_SCAN = 2,
 };
 
 chreBleScanFilter *getBleScanFilter(ScanRequestType &scanRequestType) {
@@ -56,24 +54,12 @@
   switch (scanRequestType) {
     case NO_FILTER:
       filter = nullptr;
-      scanRequestType = SERVICE_DATA_UUID_16;
+      scanRequestType = SERVICE_DATA_16;
       break;
-    case SERVICE_DATA_UUID_16:
-      scanFilter->type = CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_16;
+    case SERVICE_DATA_16:
+      scanFilter->type = CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16;
       scanFilter->len = 2;
       filter->scanFilters = scanFilter.release();
-      scanRequestType = SERVICE_DATA_UUID_32;
-      break;
-    case SERVICE_DATA_UUID_32:
-      scanFilter->type = CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_32;
-      scanFilter->len = 4;
-      filter->scanFilters = scanFilter.release();
-      scanRequestType = SERVICE_DATA_UUID_128;
-      break;
-    case SERVICE_DATA_UUID_128:
-      scanFilter->type = CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_128;
-      scanFilter->len = 16;
-      filter->scanFilters = scanFilter.release();
       scanRequestType = STOP_SCAN;
       break;
     case STOP_SCAN:
diff --git a/chre_api/include/chre_api/chre/ble.h b/chre_api/include/chre_api/chre/ble.h
index ac473df..5212891 100644
--- a/chre_api/include/chre_api/chre/ble.h
+++ b/chre_api/include/chre_api/chre/ble.h
@@ -72,7 +72,7 @@
  * 0x0157) for that particular filtering capability, as found in
  * https://source.android.com/devices/bluetooth/hci_requirements
  *
- * For example, the Service Data UUID filter has a sub-command of 0x7; hence
+ * For example, the Service Data filter has a sub-command of 0x7; hence
  * the filtering capability is indicated by (1 << 0x7).
  *
  * @defgroup CHRE_BLE_FILTER_CAPABILITIES
@@ -84,9 +84,9 @@
 //! CHRE BLE supports RSSI filters
 #define CHRE_BLE_FILTER_CAPABILITIES_RSSI UINT32_C(1 << 1)
 
-//! CHRE BLE supports Service Data UUID filters (Corresponding HCI OCF: 0x0157,
-//! Sub: 0x07)
-#define CHRE_BLE_FILTER_CAPABILITIES_SERVICE_DATA_UUID UINT32_C(1 << 7)
+//! CHRE BLE supports Service Data filters (Corresponding HCI OCF: 0x0157,
+//! Sub-command: 0x07)
+#define CHRE_BLE_FILTER_CAPABILITIES_SERVICE_DATA UINT32_C(1 << 7)
 /** @} */
 
 /**
@@ -126,11 +126,6 @@
 /** @} */
 
 /**
- * Maximum length of a BLE UUID in bytes.
- */
-#define CHRE_BLE_UUID_LEN_MAX (16)
-
-/**
  * Maximum BLE (legacy) advertisement payload data length, in bytes
  * This is calculated by subtracting 2 (type + len) from 31 (max payload).
  */
@@ -157,6 +152,11 @@
 #define CHRE_BLE_TX_POWER_NONE (127)
 
 /**
+ * Indicates ADI field was not provided in advertisement.
+ */
+#define CHRE_BLE_ADI_NONE (0xFF)
+
+/**
  * The CHRE BLE advertising event type is based on the BT Core Spec v5.2,
  * Vol 4, Part E, Section 7.7.65.13, LE Extended Advertising Report event,
  * Event_Type.
@@ -239,15 +239,9 @@
  * Assigned Numbers, Generic Access Profile.
  * ref: https://www.bluetooth.com/specifications/assigned-numbers/
  */
-enum chreBleScanFilterAdType {
-  //! Service Data - 16-bit UUID
-  CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_16 = 0x16,
-
-  //! Service Data - 32-bit UUID
-  CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_32 = 0x20,
-
-  //! Service Data - 128-bit UUID
-  CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_128 = 0x21,
+enum chreBleAdType {
+  //! Service Data with 16-bit UUID
+  CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16 = 0x16,
 };
 
 /**
@@ -259,26 +253,32 @@
  *   data & dataMask == advData & dataMask
  * where advData is the advertisement packet data for the specified AD type.
  *
- * The syntax of CHRE scan filter definitions are based on the upcoming Android
- * Advertising Packet Content Filter (APCF) HCI requirement subtype 0x08
- * ref:
- * https://source.android.com/devices/bluetooth/hci_requirements#le_apcf_command-set_filtering_parameters_sub_cmd
- * and specific AD Types among those defined in the Bluetooth spec Assigned
- * Numbers, Generic Access Profile
- * ref: https://www.bluetooth.com/specifications/assigned-numbers/
+ * The CHRE generic filter structure represents a generic filter on an AD Type
+ * as defined in the Bluetooth spec Assigned Numbers, Generic Access Profile
+ * (ref: https://www.bluetooth.com/specifications/assigned-numbers/). This
+ * generic structure is used by the Advertising Packet Content Filter
+ * (APCF) HCI generic AD type sub-command 0x08 (ref:
+ * https://source.android.com/devices/bluetooth/hci_requirements#le_apcf_command).
  *
- * For example, to add a 32-bit service data UUID filter requiring the most
- * significant byte to be 0x42, the following settings would be used:
- *   type = CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_32
- *   len = 4
- *   data = {0x42, 0, 0, 0}
- *   dataMask = {0xff, 0, 0, 0}
+ * Note that the CHRE implementation may not support every kind of filter that
+ * can be represented by this structure. Use chreBleGetFilterCapabilities() to
+ * discover supported filtering capabilities at runtime.
+ *
+ * For example, to filter on a 16 bit service data UUID of 0xFE2C, the following
+ * settings would be used:
+ *   type = CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16
+ *   len = 2
+ *   data = {0xFE, 0x2C}
+ *   dataMask = {0xFF, 0xFF}
  */
 struct chreBleGenericFilter {
-  //! Acceptable values among enum chreBleScanFilterAdType
+  //! Acceptable values among enum chreBleAdType
   uint8_t type;
 
-  //! Length of data and dataMask
+  /**
+   * Length of data and dataMask. AD payloads shorter than this length will not
+   * be matched by the filter. Length must be greater than 0.
+   */
   uint8_t len;
 
   //! Used in combination with dataMask to filter an advertisement
@@ -298,7 +298,7 @@
  * to take advantage of CHRE scan filters as much as possible, but must design
  * their logic as to not depend on CHRE filtering.
  *
- * The syntax of CHRE scan filter definitions are based on the upcoming Android
+ * The syntax of CHRE scan filter definitions are based on the Android
  * Advertising Packet Content Filter (APCF) HCI requirement subtype 0x08
  * ref:
  * https://source.android.com/devices/bluetooth/hci_requirements#le_apcf_command-set_filtering_parameters_sub_cmd
@@ -402,7 +402,7 @@
 
   //! Value of the Advertising SID subfield in the ADI field of the PDU among
   //! the range of [0, 0x0f].
-  //! A value of 0xff indicates no ADI field was provided.
+  //! CHRE_BLE_ADI_NONE indicates no ADI field was provided.
   //! Other values are reserved.
   uint8_t advertisingSid;
 
diff --git a/core/ble_request_manager.cc b/core/ble_request_manager.cc
index a959761..f5aef54 100644
--- a/core/ble_request_manager.cc
+++ b/core/ble_request_manager.cc
@@ -396,9 +396,7 @@
         valid = false;
         break;
       }
-
-      uint8_t expectedLen = getFilterLenByAdType(filter.type);
-      if (expectedLen != filter.len) {
+      if (filter.len == 0 || filter.len > CHRE_BLE_DATA_LEN_MAX) {
         valid = false;
         break;
       }
@@ -426,23 +424,7 @@
 }
 
 bool BleRequestManager::isValidAdType(uint8_t adType) {
-  return adType == CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_16 ||
-         adType == CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_32 ||
-         adType == CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_128;
-}
-
-uint8_t BleRequestManager::getFilterLenByAdType(uint8_t adType) {
-  switch (adType) {
-    case CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_16:
-      return 2;
-    case CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_32:
-      return 4;
-    case CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_128:
-      return 16;
-    default:
-      CHRE_ASSERT(false);
-      return UINT8_MAX;
-  }
+  return adType == CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16;
 }
 
 bool BleRequestManager::bleSettingEnabled() {
diff --git a/core/include/chre/core/ble_request_manager.h b/core/include/chre/core/ble_request_manager.h
index 654ef09..432a3c0 100644
--- a/core/include/chre/core/ble_request_manager.h
+++ b/core/include/chre/core/ble_request_manager.h
@@ -359,12 +359,6 @@
   static bool isValidAdType(uint8_t adType);
 
   /**
-   * @return Valid length associated with a given advertisement type. UINT8_MAX
-   *         if ad type is invalid.
-   */
-  static uint8_t getFilterLenByAdType(uint8_t adType);
-
-  /**
    * @return true if BLE setting is enabled.
    */
   bool bleSettingEnabled();
diff --git a/core/tests/ble_request_test.cc b/core/tests/ble_request_test.cc
index d3894d4..f3ed88b 100644
--- a/core/tests/ble_request_test.cc
+++ b/core/tests/ble_request_test.cc
@@ -52,7 +52,7 @@
   filter.rssiThreshold = -5;
   filter.scanFilterCount = 1;
   auto scanFilters = std::make_unique<chreBleGenericFilter>();
-  scanFilters->type = CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_16;
+  scanFilters->type = CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16;
   scanFilters->len = 2;
   filter.scanFilters = scanFilters.get();
   BleRequest enabled(0, true, CHRE_BLE_SCAN_MODE_AGGRESSIVE, 20, &filter);
@@ -65,7 +65,7 @@
   EXPECT_EQ(20, mergedRequest.getReportDelayMs());
   EXPECT_EQ(-5, mergedRequest.getRssiThreshold());
   EXPECT_EQ(1, mergedRequest.getGenericFilters().size());
-  EXPECT_EQ(CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_16,
+  EXPECT_EQ(CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16,
             mergedRequest.getGenericFilters()[0].type);
   EXPECT_EQ(2, mergedRequest.getGenericFilters()[0].len);
 }
@@ -92,7 +92,7 @@
   filter.rssiThreshold = -5;
   filter.scanFilterCount = 1;
   auto scanFilters = std::make_unique<chreBleGenericFilter>();
-  scanFilters->type = CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_16;
+  scanFilters->type = CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16;
   scanFilters->len = 4;
   filter.scanFilters = scanFilters.get();
 
@@ -107,7 +107,7 @@
   filter.rssiThreshold = -5;
   filter.scanFilterCount = 1;
   auto scanFilters = std::make_unique<chreBleGenericFilter>();
-  scanFilters->type = CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_16;
+  scanFilters->type = CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16;
   scanFilters->len = 4;
   filter.scanFilters = scanFilters.get();
 
@@ -126,7 +126,7 @@
   filter.rssiThreshold = -5;
   filter.scanFilterCount = 1;
   auto scanFilters = std::make_unique<chreBleGenericFilter>();
-  scanFilters->type = CHRE_BLE_FILTER_TYPE_SERVICE_DATA_UUID_16;
+  scanFilters->type = CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16;
   scanFilters->len = 4;
   filter.scanFilters = scanFilters.get();
 
diff --git a/platform/linux/pal_ble.cc b/platform/linux/pal_ble.cc
index 7cb074f..2762170 100644
--- a/platform/linux/pal_ble.cc
+++ b/platform/linux/pal_ble.cc
@@ -92,7 +92,7 @@
 
 uint32_t chrePalBleGetFilterCapabilities() {
   return CHRE_BLE_FILTER_CAPABILITIES_RSSI |
-         CHRE_BLE_FILTER_CAPABILITIES_SERVICE_DATA_UUID;
+         CHRE_BLE_FILTER_CAPABILITIES_SERVICE_DATA;
 }
 
 bool chrePalBleStartScan(chreBleScanMode mode, uint32_t /* reportDelayMs */,
diff --git a/test/simulation/ble_test.cc b/test/simulation/ble_test.cc
index 5c3ff8e..eb65b2e 100644
--- a/test/simulation/ble_test.cc
+++ b/test/simulation/ble_test.cc
@@ -81,7 +81,7 @@
   sendEventToNanoapp(app, GET_FILTER_CAPABILITIES);
   waitForEvent(GET_FILTER_CAPABILITIES, &capabilities);
   ASSERT_EQ(capabilities, CHRE_BLE_FILTER_CAPABILITIES_RSSI |
-                              CHRE_BLE_FILTER_CAPABILITIES_SERVICE_DATA_UUID);
+                              CHRE_BLE_FILTER_CAPABILITIES_SERVICE_DATA);
 }
 
 struct BleTestNanoapp : public TestNanoapp {