biod: Use new FpInfoCommand

BUG=b:144956297, b:76037094
TEST=FEATURES="test" emerge-hatch biod
     cros_deploy <IP> biod
     Enroll finger, lock/unlock, delete finger

Change-Id: I60e3e38f42ff7105630e506b914915923978d8cd
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2268336
Reviewed-by: Yicheng Li <yichengli@chromium.org>
Commit-Queue: Tom Hughes <tomhughes@chromium.org>
Tested-by: Tom Hughes <tomhughes@chromium.org>
Auto-Submit: Tom Hughes <tomhughes@chromium.org>
diff --git a/biod/cros_fp_device.cc b/biod/cros_fp_device.cc
index 6890ea5..258a342 100644
--- a/biod/cros_fp_device.cc
+++ b/biod/cros_fp_device.cc
@@ -275,13 +275,13 @@
 }
 
 bool CrosFpDevice::UpdateFpInfo() {
-  EcCommand<EmptyParam, struct ec_response_fp_info> cmd(EC_CMD_FP_INFO,
-                                                        kVersionOne);
-  if (!cmd.Run(cros_fd_.get())) {
+  info_ = ec_command_factory_->FpInfoCommand();
+
+  if (!info_->Run(cros_fd_.get())) {
     LOG(ERROR) << "Failed to get FP information.";
     return false;
   }
-  info_ = *cmd.Resp();
+
   return true;
 }
 
@@ -470,30 +470,37 @@
     return false;
 
   LOG(INFO) << "CROS FP Sensor Info ";
-  LOG(INFO) << "  Vendor ID  : " << FourCC(info_.vendor_id);
-  LOG(INFO) << "  Product ID : " << info_.product_id;
-  LOG(INFO) << "  Model ID   : 0x" << std::hex << info_.model_id;
-  LOG(INFO) << "  Version    : " << info_.version;
+  LOG(INFO) << "  Vendor ID  : " << FourCC(info_->sensor_id()->vendor_id);
+  LOG(INFO) << "  Product ID : " << info_->sensor_id()->product_id;
+  LOG(INFO) << "  Model ID   : 0x" << std::hex << info_->sensor_id()->model_id;
+  LOG(INFO) << "  Version    : " << info_->sensor_id()->version;
   std::string error_flags;
-  if (info_.errors & FP_ERROR_NO_IRQ)
+  if ((info_->GetFpSensorErrors() & FpSensorErrors::kNoIrq) !=
+      FpSensorErrors::kNone)
     error_flags += "NO_IRQ ";
-  if (info_.errors & FP_ERROR_SPI_COMM)
+  if ((info_->GetFpSensorErrors() & FpSensorErrors::kSpiCommunication) !=
+      FpSensorErrors::kNone)
     error_flags += "SPI_COMM ";
-  if (info_.errors & FP_ERROR_BAD_HWID)
+  if ((info_->GetFpSensorErrors() & FpSensorErrors::kBadHardwareID) !=
+      FpSensorErrors::kNone)
     error_flags += "BAD_HWID ";
-  if (info_.errors & FP_ERROR_INIT_FAIL)
+  if ((info_->GetFpSensorErrors() & FpSensorErrors::kInitializationFailure) !=
+      FpSensorErrors::kNone)
     error_flags += "INIT_FAIL";
   LOG(INFO) << "  Errors     : " << error_flags;
   LOG(INFO) << "CROS FP Image Info ";
   // Prints the pixel format in FOURCC format.
-  LOG(INFO) << "  Pixel Format     : " << FourCC(info_.pixel_format);
-  LOG(INFO) << "  Image Data Size  : " << info_.frame_size;
-  LOG(INFO) << "  Image Dimensions : " << info_.width << "x" << info_.height
-            << " " << info_.bpp << " bpp";
+  LOG(INFO) << "  Pixel Format     : "
+            << FourCC(info_->sensor_image()->pixel_format);
+  LOG(INFO) << "  Image Data Size  : " << info_->sensor_image()->frame_size;
+  LOG(INFO) << "  Image Dimensions : " << info_->sensor_image()->width << "x"
+            << info_->sensor_image()->height << " "
+            << info_->sensor_image()->bpp << " bpp";
   LOG(INFO) << "CROS FP Finger Template Info ";
-  LOG(INFO) << "  Template data format  : " << info_.template_version;
-  LOG(INFO) << "  Template Data Size    : " << info_.template_size;
-  LOG(INFO) << "  Max number of fingers : " << info_.template_max;
+  LOG(INFO) << "  Template data format  : " << info_->template_info()->version;
+  LOG(INFO) << "  Template Data Size    : " << info_->template_info()->size;
+  LOG(INFO) << "  Max number of fingers : "
+            << info_->template_info()->max_templates;
 
   watcher_ = base::FileDescriptorWatcher::WatchReadable(
       cros_fd_.get(), base::BindRepeating(&CrosFpDevice::OnEventReadable,
@@ -516,14 +523,14 @@
   if (!UpdateFpInfo())
     return false;
 
-  *bitmap = std::bitset<32>(info_.template_dirty);
+  *bitmap = info_->template_info()->dirty;
   return true;
 }
 
 bool CrosFpDevice::GetIndexOfLastTemplate(int* index) {
   if (!UpdateFpInfo())
     return false;
-  *index = info_.template_valid - 1;
+  *index = info_->template_info()->num_valid - 1;
   if (*index < 0 || *index >= MaxTemplateCount()) {
     LOG(ERROR) << "Invalid index of last template: " << *index << ".";
     return false;
@@ -545,10 +552,11 @@
     if (!GetIndexOfLastTemplate(&index))
       return false;
     // Is the last one really a new created one ?
-    if (!(info_.template_dirty & (1 << index)))
+    const auto& dirty = info_->template_info()->dirty;
+    if (index >= dirty.size() || !dirty.test(index))
       return false;
   }
-  out->resize(static_cast<size_t>(info_.template_size));
+  out->resize(static_cast<size_t>(info_->template_info()->size));
   // In the EC_CMD_FP_FRAME host command, the templates are indexed starting
   // from 1 (aka FP_FRAME_INDEX_TEMPLATE), as 0 (aka FP_FRAME_INDEX_RAW_IMAGE)
   // is used for the finger image.
@@ -697,4 +705,22 @@
   return true;
 }
 
+int CrosFpDevice::MaxTemplateCount() {
+  if (!info_ || !info_->template_info()) {
+    UpdateFpInfo();
+  }
+  CHECK(info_);
+  CHECK(info_->template_info());
+  return info_->template_info()->max_templates;
+}
+
+int CrosFpDevice::TemplateVersion() {
+  if (!info_ || !info_->template_info()) {
+    UpdateFpInfo();
+  }
+  CHECK(info_);
+  CHECK(info_->template_info());
+  return info_->template_info()->version;
+}
+
 }  // namespace biod
diff --git a/biod/cros_fp_device.h b/biod/cros_fp_device.h
index 27922b7..0a4da9c 100644
--- a/biod/cros_fp_device.h
+++ b/biod/cros_fp_device.h
@@ -16,6 +16,7 @@
 #include "biod/biod_metrics.h"
 #include "biod/cros_fp_device_interface.h"
 #include "biod/ec_command_factory.h"
+#include "biod/fp_info_command.h"
 #include "biod/fp_mode.h"
 #include "biod/uinput_device.h"
 
@@ -62,8 +63,8 @@
   // if no entropy had been added before.
   bool InitEntropy(bool reset) override;
 
-  int MaxTemplateCount() override { return info_.template_max; }
-  int TemplateVersion() override { return info_.template_version; }
+  int MaxTemplateCount() override;
+  int TemplateVersion() override;
 
   EcCmdVersionSupportStatus EcCmdVersionSupported(uint16_t cmd,
                                                   uint32_t ver) override;
@@ -104,7 +105,7 @@
   std::unique_ptr<base::FileDescriptorWatcher::Controller> watcher_;
   ssize_t max_read_size_ = 0;
   ssize_t max_write_size_ = 0;
-  struct ec_response_fp_info info_ = {};
+  std::unique_ptr<FpInfoCommand> info_;
 
   std::unique_ptr<EcCommandFactoryInterface> ec_command_factory_;
   MkbpCallback mkbp_event_;