[zircon][hid] Remove hid::Report struct

Replace the hid::Report struct with the
raw array and the array length.
This is in preperation for APIs which
will edit the array and would not be
able to use the Report struct which
has a const array.

ZX-3699 #done

Change-Id: I95edde1d1c6397d1e3ba12e65a312e654a583a99
diff --git a/garnet/bin/ui/input_reader/buttons.cc b/garnet/bin/ui/input_reader/buttons.cc
index fee61d1..e7f6cf8 100644
--- a/garnet/bin/ui/input_reader/buttons.cc
+++ b/garnet/bin/ui/input_reader/buttons.cc
@@ -78,7 +78,6 @@
   double volume = 0;
   double mic_mute = 0;
 
-  hid::Report hid_report = {data, len};
   if (report_size_ != len) {
     FXL_LOG(ERROR) << "Sensor report: Expected size " << report_size_
                    << "Received size " << len;
@@ -86,13 +85,13 @@
   }
 
   if (capabilities_ & Capabilities::VOLUME) {
-    if (!hid::ExtractAsUnit(hid_report, volume_, &volume)) {
+    if (!hid::ExtractAsUnit(data, len, volume_, &volume)) {
       FXL_LOG(ERROR) << "Sensor report: Failed to parse volume";
       return false;
     }
   }
   if (capabilities_ & Capabilities::PHONE_MUTE) {
-    if (!hid::ExtractAsUnit(hid_report, phone_mute_, &mic_mute)) {
+    if (!hid::ExtractAsUnit(data, len, phone_mute_, &mic_mute)) {
       FXL_LOG(ERROR) << "Sensor report: Failed to parse phone_mute";
       return false;
     }
diff --git a/garnet/bin/ui/input_reader/mouse.cc b/garnet/bin/ui/input_reader/mouse.cc
index bad2f5f..2ed0d83 100644
--- a/garnet/bin/ui/input_reader/mouse.cc
+++ b/garnet/bin/ui/input_reader/mouse.cc
@@ -111,7 +111,6 @@
   }
 
   Report mouse_report = {};
-  hid::Report hid_report = {data, len};
   if (len != report_size_) {
     FXL_LOG(ERROR) << "Mouse HID Report is not correct size, (" << len
                    << " != " << report_size_ << ")";
@@ -120,21 +119,21 @@
 
   uint32_t clicked;
   if (capabilities_ & Capabilities::LEFT_CLICK) {
-    if (!hid::ExtractUint(hid_report, left_click_, &clicked)) {
+    if (!hid::ExtractUint(data, len, left_click_, &clicked)) {
       FXL_LOG(ERROR) << "Mouse report: Failed to parse LEFT_CLICK";
       return false;
     }
     mouse_report.left_click = (clicked == 1);
   }
   if (capabilities_ & Capabilities::MIDDLE_CLICK) {
-    if (!hid::ExtractUint(hid_report, middle_click_, &clicked)) {
+    if (!hid::ExtractUint(data, len, middle_click_, &clicked)) {
       FXL_LOG(ERROR) << "Mouse report: Failed to parse MIDDLE_CLICK";
       return false;
     }
     mouse_report.middle_click = (clicked == 1);
   }
   if (capabilities_ & Capabilities::RIGHT_CLICK) {
-    if (!hid::ExtractUint(hid_report, right_click_, &clicked)) {
+    if (!hid::ExtractUint(data, len, right_click_, &clicked)) {
       FXL_LOG(ERROR) << "Mouse report: Failed to parse RIGHT_CLICK";
       return false;
     }
@@ -149,7 +148,7 @@
 
   if (capabilities_ & Capabilities::X) {
     double x;
-    if (!hid::ExtractAsUnit(hid_report, x_, &x)) {
+    if (!hid::ExtractAsUnit(data, len, x_, &x)) {
       FXL_LOG(ERROR) << "Mouse report: Failed to parse X";
       return false;
     }
@@ -162,7 +161,7 @@
   }
   if (capabilities_ & Capabilities::Y) {
     double y;
-    if (!hid::ExtractAsUnit(hid_report, y_, &y)) {
+    if (!hid::ExtractAsUnit(data, len, y_, &y)) {
       FXL_LOG(ERROR) << "Mouse report: Failed to parse Y";
       return false;
     }
diff --git a/garnet/bin/ui/input_reader/sensor.cc b/garnet/bin/ui/input_reader/sensor.cc
index fa00d8b..fa37fe6 100644
--- a/garnet/bin/ui/input_reader/sensor.cc
+++ b/garnet/bin/ui/input_reader/sensor.cc
@@ -154,7 +154,6 @@
   double z = 0;
   double scalar = 0;
 
-  hid::Report hid_report = {data, len};
   if (report_size_ != len) {
     FXL_LOG(ERROR) << "Sensor report: Expected size " << report_size_
                    << "Recieved size " << len;
@@ -162,25 +161,25 @@
   }
 
   if (capabilities_ & Capabilities::X) {
-    if (!hid::ExtractAsUnit(hid_report, x_, &x)) {
+    if (!hid::ExtractAsUnit(data, len, x_, &x)) {
       FXL_LOG(ERROR) << "Sensor report: Failed to parse X";
       return false;
     }
   }
   if (capabilities_ & Capabilities::Y) {
-    if (!hid::ExtractAsUnit(hid_report, y_, &y)) {
+    if (!hid::ExtractAsUnit(data, len, y_, &y)) {
       FXL_LOG(ERROR) << "Sensor report: Failed to parse Y";
       return false;
     }
   }
   if (capabilities_ & Capabilities::Z) {
-    if (!hid::ExtractAsUnit(hid_report, z_, &z)) {
+    if (!hid::ExtractAsUnit(data, len, z_, &z)) {
       FXL_LOG(ERROR) << "Sensor report: Failed to parse Z";
       return false;
     }
   }
   if (capabilities_ & Capabilities::SCALAR) {
-    if (!hid::ExtractAsUnit(hid_report, scalar_, &scalar)) {
+    if (!hid::ExtractAsUnit(data, len, scalar_, &scalar)) {
       FXL_LOG(ERROR) << "Sensor report: Failed to parse Scalar";
       return false;
     }
diff --git a/garnet/bin/ui/input_reader/touch.cc b/garnet/bin/ui/input_reader/touch.cc
index d11c9ea..f4fe2b2 100644
--- a/garnet/bin/ui/input_reader/touch.cc
+++ b/garnet/bin/ui/input_reader/touch.cc
@@ -139,7 +139,6 @@
 bool Touch::ParseReport(const uint8_t *data, size_t len, Report *report) const {
   assert(report != nullptr);
 
-  hid::Report hid_report = {data, len};
   if (len != report_size_) {
     FXL_LOG(ERROR) << "Touch HID Report is not correct size, (" << len
                    << " != " << report_size_ << ")";
@@ -159,7 +158,7 @@
     if (config->capabilities & Capabilities::TIP_SWITCH) {
       uint8_t tip_switch;
       bool success =
-          hid::ExtractUint(hid_report, config->tip_switch, &tip_switch);
+          hid::ExtractUint(data, len, config->tip_switch, &tip_switch);
       if (!success || !tip_switch) {
         continue;
       }
@@ -169,14 +168,14 @@
 
     // XXX(konkers): Add 32 bit generic field extraction helpers.
     if (config->capabilities & Capabilities::CONTACT_ID) {
-      if (!hid::ExtractUint(hid_report, config->contact_id, &contact->id)) {
+      if (!hid::ExtractUint(data, len, config->contact_id, &contact->id)) {
         FXL_LOG(ERROR) << "Touch report: Failed to parse CONTACT_ID";
         return false;
       }
     }
     if (config->capabilities & Capabilities::X) {
       double x;
-      if (!hid::ExtractAsUnit(hid_report, config->x, &x)) {
+      if (!hid::ExtractAsUnit(data, len, config->x, &x)) {
         FXL_LOG(ERROR) << "Touch report: Failed to parse X";
         return false;
       }
@@ -187,7 +186,7 @@
     }
     if (config->capabilities & Capabilities::Y) {
       double y;
-      if (!hid::ExtractAsUnit(hid_report, config->y, &y)) {
+      if (!hid::ExtractAsUnit(data, len, config->y, &y)) {
         FXL_LOG(ERROR) << "Touchpad report: Failed to parse Y";
         return false;
       }
@@ -206,7 +205,7 @@
 
   if (capabilities_ & Capabilities::BUTTON) {
     uint8_t button;
-    if (!hid::ExtractUint(hid_report, button_, &button)) {
+    if (!hid::ExtractUint(data, len, button_, &button)) {
       FXL_LOG(ERROR) << "Touchpad report: Failed to parse BUTTON";
       return false;
     }
@@ -216,12 +215,12 @@
   if (capabilities_ & Capabilities::SCAN_TIME) {
     // If we don't have a unit, extract the raw data
     if (scan_time_.unit.type == 0) {
-      if (!hid::ExtractUint(hid_report, scan_time_, &report->scan_time)) {
+      if (!hid::ExtractUint(data, len, scan_time_, &report->scan_time)) {
         return false;
       }
     } else {
       double scan_time;
-      if (!hid::ExtractAsUnit(hid_report, scan_time_, &scan_time)) {
+      if (!hid::ExtractAsUnit(data, len, scan_time_, &scan_time)) {
         FXL_LOG(ERROR) << "Touchpad report: Failed to parse SCAN_TIME";
         return false;
       }
diff --git a/zircon/system/ulib/hid-parser/include/hid-parser/report.h b/zircon/system/ulib/hid-parser/include/hid-parser/report.h
index 0ad9dad..a8ddbfb 100644
--- a/zircon/system/ulib/hid-parser/include/hid-parser/report.h
+++ b/zircon/system/ulib/hid-parser/include/hid-parser/report.h
@@ -12,26 +12,27 @@
 #include <hid-parser/units.h>
 
 namespace hid {
-struct Report {
-    const uint8_t* data;
-    size_t len;
-};
 
-// Extracts the data from report that has been translated
-// into the logical units described by attr.unit. This is the recommended
-// extraction function for users of this library.
-bool ExtractAsUnit(const Report& report, const hid::Attributes& attr, double* value_out);
+// Extracts |value_out| from |report| and ensures that is in the units
+// specified by |attr|. This is the recommended extraction function for
+// users of this library.
+bool ExtractAsUnit(const uint8_t* report, size_t report_len, const hid::Attributes& attr,
+                   double* value_out);
 
-// Extracts the data already converted as a given unit
-bool ExtractWithUnit(const Report& report, const hid::Attributes& attr,
+// Extracts |value_out| from |report| and converts it into the units
+// specified by |unit_out|.
+bool ExtractWithUnit(const uint8_t* report, size_t report_len, const hid::Attributes& attr,
                      const Unit& unit_out, double* value_out);
 
 // Helper functions that extracts the raw byte data from a report. This is only
 // recommended for users that know what they are doing and are willing to
 // use raw data or do their own conversion between logical and physical values.
-bool ExtractUint(const Report& report, const hid::Attributes& attr, uint8_t* value_out);
-bool ExtractUint(const Report& report, const hid::Attributes& attr, uint16_t* value_out);
-bool ExtractUint(const Report& report, const hid::Attributes& attr, uint32_t* value_out);
+bool ExtractUint(const uint8_t* report, size_t report_len, const hid::Attributes& attr,
+                 uint8_t* value_out);
+bool ExtractUint(const uint8_t* report, size_t report_len, const hid::Attributes& attr,
+                 uint16_t* value_out);
+bool ExtractUint(const uint8_t* report, size_t report_len, const hid::Attributes& attr,
+                 uint32_t* value_out);
 
 } // namespace hid
 
diff --git a/zircon/system/ulib/hid-parser/report.cpp b/zircon/system/ulib/hid-parser/report.cpp
index 9b8f0b6..4284bae 100644
--- a/zircon/system/ulib/hid-parser/report.cpp
+++ b/zircon/system/ulib/hid-parser/report.cpp
@@ -26,8 +26,8 @@
     return static_cast<int32_t>((data ^ msb) - msb);
 }
 
-inline bool FieldFits(const Report& report, const Attributes& attr) {
-    return (attr.offset + attr.bit_sz) <= (report.len * 8);
+inline bool FieldFits(size_t report_len, const Attributes& attr) {
+    return (attr.offset + attr.bit_sz) <= (report_len * 8);
 }
 
 // Extracts bits from a byte and returns them.
@@ -43,12 +43,13 @@
 
 #define MIN(a, b) (a) < (b) ? (a) : (b)
 template <typename T>
-bool ExtractUint(const Report& report, const hid::Attributes& attr, T* value_out) {
+bool ExtractUint(const uint8_t* report, size_t report_len, const hid::Attributes& attr,
+                 T* value_out) {
     static_assert(std::is_pod<T>::value, "not POD");
     if (attr.bit_sz > sizeof(T) * 8) {
         return false;
     }
-    if (!FieldFits(report, attr)) {
+    if (!FieldFits(report_len, attr)) {
         return false;
     }
     T val = 0;
@@ -61,7 +62,7 @@
         uint8_t bit_count = static_cast<uint8_t>(MIN(bits_till_byte_end, end_bit - index_bit));
 
         uint8_t extracted_bits =
-            ExtractBitsFromByte(report.data[index_bit / 8u], index_bit % 8u, bit_count);
+            ExtractBitsFromByte(report[index_bit / 8u], index_bit % 8u, bit_count);
 
         val = static_cast<T>(val | (extracted_bits << (index_bit - start_bit)));
 
@@ -73,25 +74,29 @@
 }
 #undef MIN
 
-bool ExtractUint(const Report& report, const hid::Attributes& attr, uint8_t* value_out) {
-    return ExtractUint<uint8_t>(report, attr, value_out);
+bool ExtractUint(const uint8_t* report, size_t report_len, const hid::Attributes& attr,
+                 uint8_t* value_out) {
+    return ExtractUint<uint8_t>(report, report_len, attr, value_out);
 }
 
-bool ExtractUint(const Report& report, const hid::Attributes& attr, uint16_t* value_out) {
-    return ExtractUint<uint16_t>(report, attr, value_out);
+bool ExtractUint(const uint8_t* report, size_t report_len, const hid::Attributes& attr,
+                 uint16_t* value_out) {
+    return ExtractUint<uint16_t>(report, report_len, attr, value_out);
 }
 
-bool ExtractUint(const Report& report, const hid::Attributes& attr, uint32_t* value_out) {
-    return ExtractUint<uint32_t>(report, attr, value_out);
+bool ExtractUint(const uint8_t* report, size_t report_len, const hid::Attributes& attr,
+                 uint32_t* value_out) {
+    return ExtractUint<uint32_t>(report, report_len, attr, value_out);
 }
 
-bool ExtractAsUnit(const Report& report, const hid::Attributes& attr, double* value_out) {
+bool ExtractAsUnit(const uint8_t* report, size_t report_len, const hid::Attributes& attr,
+                   double* value_out) {
     if (value_out == nullptr) {
         return false;
     }
 
     uint32_t uint_out;
-    bool ret = ExtractUint(report, attr, &uint_out);
+    bool ret = ExtractUint(report, report_len, attr, &uint_out);
     if (!ret) {
         return false;
     }
@@ -104,8 +109,8 @@
     int64_t phys_max =
         (attr.phys_mm.min < 0) ? attr.phys_mm.max : static_cast<uint32_t>(attr.phys_mm.max);
     double val = (attr.logc_mm.min < 0)
-                    ? static_cast<double>(SignExtendFromNBits(uint_out, attr.bit_sz))
-                    : uint_out;
+                     ? static_cast<double>(SignExtendFromNBits(uint_out, attr.bit_sz))
+                     : uint_out;
 
     if (val < static_cast<double>(attr.logc_mm.min) || val > static_cast<double>(attr.logc_mm.max)) {
         return false;
@@ -124,10 +129,10 @@
     return true;
 }
 
-bool ExtractWithUnit(const Report& report, const hid::Attributes& attr,
+bool ExtractWithUnit(const uint8_t* report, size_t report_len, const hid::Attributes& attr,
                      const Unit& unit_out, double* value_out) {
     double val = 0;
-    if (!ExtractAsUnit(report, attr, &val)) {
+    if (!ExtractAsUnit(report, report_len, attr, &val)) {
         return false;
     }
 
diff --git a/zircon/system/utest/hid-parser/hid-helper-test.cpp b/zircon/system/utest/hid-parser/hid-helper-test.cpp
index 6e7b8b3..80f63a9 100644
--- a/zircon/system/utest/hid-parser/hid-helper-test.cpp
+++ b/zircon/system/utest/hid-parser/hid-helper-test.cpp
@@ -177,73 +177,73 @@
 
 bool extract_tests() {
     BEGIN_TEST;
-    uint8_t data[] = {0x0F, 0x0F, 0x0F, 0x0F, 0x0F};
-    hid::Report report = {data, 5};
+    uint8_t report[] = {0x0F, 0x0F, 0x0F, 0x0F, 0x0F};
+    size_t report_len = sizeof(report);
     hid::Attributes attr;
     bool ret;
 
     uint8_t int8;
     attr.offset = 0;
     attr.bit_sz = 8;
-    ret = ExtractUint(report, attr, &int8);
+    ret = ExtractUint(report, report_len, attr, &int8);
     EXPECT_TRUE(ret);
     EXPECT_EQ(int8, 0x0F);
 
     attr.offset = 2;
     attr.bit_sz = 6;
-    ret = ExtractUint(report, attr, &int8);
+    ret = ExtractUint(report, report_len, attr, &int8);
     EXPECT_TRUE(ret);
     EXPECT_EQ(int8, 0x03);
 
     attr.offset = 3;
     attr.bit_sz = 2;
-    ret = ExtractUint(report, attr, &int8);
+    ret = ExtractUint(report, report_len, attr, &int8);
     EXPECT_TRUE(ret);
     EXPECT_EQ(int8, 0x01);
 
     // Test over a byte boundary
     attr.offset = 4;
     attr.bit_sz = 8;
-    ret = ExtractUint(report, attr, &int8);
+    ret = ExtractUint(report, report_len, attr, &int8);
     EXPECT_TRUE(ret);
     EXPECT_EQ(int8, 0xF0);
 
     uint16_t int16;
     attr.offset = 0;
     attr.bit_sz = 16;
-    ret = ExtractUint(report, attr, &int16);
+    ret = ExtractUint(report, report_len, attr, &int16);
     EXPECT_TRUE(ret);
     EXPECT_EQ(int16, 0x0F0F);
 
     attr.offset = 4;
     attr.bit_sz = 16;
-    ret = ExtractUint(report, attr, &int16);
+    ret = ExtractUint(report, report_len, attr, &int16);
     EXPECT_TRUE(ret);
     EXPECT_EQ(int16, 0xF0F0);
 
     uint32_t int32;
     attr.offset = 0;
     attr.bit_sz = 32;
-    ret = ExtractUint(report, attr, &int32);
+    ret = ExtractUint(report, report_len, attr, &int32);
     EXPECT_TRUE(ret);
     EXPECT_EQ(int32, 0x0F0F0F0F);
 
     attr.offset = 4;
     attr.bit_sz = 32;
-    ret = ExtractUint(report, attr, &int32);
+    ret = ExtractUint(report, report_len, attr, &int32);
     EXPECT_TRUE(ret);
     EXPECT_EQ(int32, 0xF0F0F0F0);
 
     // Test that it fails if the attr is too large
     attr.offset = 0;
     attr.bit_sz = 9;
-    ret = ExtractUint(report, attr, &int8);
+    ret = ExtractUint(report, report_len, attr, &int8);
     EXPECT_FALSE(ret);
 
     // Test that it fails if it goes past the end of the report
     attr.offset = 36;
     attr.bit_sz = 16;
-    ret = ExtractUint(report, attr, &int16);
+    ret = ExtractUint(report, report_len, attr, &int16);
     EXPECT_FALSE(ret);
 
     END_TEST;
@@ -252,8 +252,8 @@
 bool extract_as_unit_tests() {
     BEGIN_TEST;
 
-    uint8_t data[] = {0x0F, 10, 0x0F, 0x0F, 0x0F};
-    hid::Report report = {data, 5};
+    uint8_t report[] = {0x0F, 10, 0x0F, 0x0F, 0x0F};
+    size_t report_len = sizeof(report);
     hid::Attributes attr = {};
     bool ret;
     double val_out;
@@ -267,7 +267,7 @@
     attr.phys_mm.max = 100;
     attr.phys_mm.min = -100;
 
-    ret = ExtractAsUnit(report, attr, &val_out);
+    ret = ExtractAsUnit(report, report_len, attr, &val_out);
     EXPECT_TRUE(ret);
     EXPECT_EQ(static_cast<int32_t>(val_out), 0x0F);
 
@@ -280,7 +280,7 @@
     attr.phys_mm.max = 10;
     attr.phys_mm.min = -10;
 
-    ret = ExtractAsUnit(report, attr, &val_out);
+    ret = ExtractAsUnit(report, report_len, attr, &val_out);
     EXPECT_TRUE(ret);
     EXPECT_EQ(static_cast<int32_t>(val_out), -1);
 
@@ -293,7 +293,7 @@
     attr.phys_mm.max = 100;
     attr.phys_mm.min = 0;
 
-    ret = ExtractAsUnit(report, attr, &val_out);
+    ret = ExtractAsUnit(report, report_len, attr, &val_out);
     EXPECT_TRUE(ret);
     EXPECT_EQ(static_cast<uint32_t>(val_out), 0xF);
 
@@ -306,7 +306,7 @@
     attr.phys_mm.max = 30;
     attr.phys_mm.min = -30;
 
-    ret = ExtractAsUnit(report, attr, &val_out);
+    ret = ExtractAsUnit(report, report_len, attr, &val_out);
     EXPECT_TRUE(ret);
     EXPECT_EQ(static_cast<int32_t>(val_out), -3);
 
@@ -319,7 +319,7 @@
     attr.phys_mm.max = 25;
     attr.phys_mm.min = 0;
 
-    ret = ExtractAsUnit(report, attr, &val_out);
+    ret = ExtractAsUnit(report, report_len, attr, &val_out);
     EXPECT_TRUE(ret);
     EXPECT_EQ(static_cast<int32_t>(val_out), 25);
 
@@ -332,7 +332,7 @@
     attr.phys_mm.max = 0;
     attr.phys_mm.min = 0;
 
-    ret = ExtractAsUnit(report, attr, &val_out);
+    ret = ExtractAsUnit(report, report_len, attr, &val_out);
     EXPECT_TRUE(ret);
     EXPECT_EQ(static_cast<int32_t>(val_out), 10);
 
@@ -360,7 +360,7 @@
 
     attr.unit = unit_in;
 
-    ret = ExtractWithUnit(report, attr, unit_out, &val_out);
+    ret = ExtractWithUnit(report, report_len, attr, unit_out, &val_out);
     EXPECT_TRUE(ret);
     EXPECT_EQ(static_cast<int32_t>(val_out), 250);