Adds check/set for first_active_omaha_ping_sent (from vpd).

first_active_omaha_ping_sent is a boolean in RW_VPD that is checked to
decide whether to send the first active ping (a=-1) to omaha. update_engine
reads its value and sets it to "true" after first active ping is sent or
if the first ping has been sent already and it does not exits in the vpd
(to capture the same feature for older devices).

BUG=chromium:717788
TEST=cros_workon_make --test; Deployed on a device and made sure the
flag is read and writen properly.
CQ-DEPEND=CL:469126

Change-Id: I867d16de1d5d2da4d7a7e1f26aa8e951d90b68f2
Reviewed-on: https://chromium-review.googlesource.com/469107
Commit-Ready: Amin Hassani <ahassani@chromium.org>
Tested-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Andrew de los Reyes <adlr@chromium.org>
diff --git a/hardware_chromeos.cc b/hardware_chromeos.cc
index 4b0b82f..47beea8 100644
--- a/hardware_chromeos.cc
+++ b/hardware_chromeos.cc
@@ -69,6 +69,8 @@
 // UpdateManager config options:
 const char* kConfigOptsIsOOBEEnabled = "is_oobe_enabled";
 
+const char* kActivePingKey = "first_active_omaha_ping_sent";
+
 }  // namespace
 
 namespace chromeos_update_engine {
@@ -248,4 +250,49 @@
     is_oobe_enabled_ = true;  // Default value.
 }
 
+bool HardwareChromeOS::GetFirstActiveOmahaPingSent() const {
+  int exit_code = 0;
+  string active_ping_str;
+  vector<string> cmd = { "vpd_get_value", kActivePingKey };
+  if (!Subprocess::SynchronousExec(cmd, &exit_code, &active_ping_str) ||
+      exit_code) {
+    LOG(ERROR) << "Failed to get vpd key for " << kActivePingKey
+               << " with exit code: " << exit_code;
+    return false;
+  }
+
+  base::TrimWhitespaceASCII(active_ping_str,
+                            base::TRIM_ALL,
+                            &active_ping_str);
+  int active_ping;
+  if (active_ping_str.empty() ||
+      !base::StringToInt(active_ping_str, &active_ping)) {
+    LOG(INFO) << "Failed to parse active_ping value: " << active_ping_str;
+    return false;
+  }
+  return static_cast<bool>(active_ping);
+}
+
+void HardwareChromeOS::SetFirstActiveOmahaPingSent() {
+  int exit_code = 0;
+  string output;
+  vector<string> vpd_set_cmd = {
+    "vpd", "-i", "RW_VPD", "-s", string(kActivePingKey) + "=1" };
+  if (!Subprocess::SynchronousExec(vpd_set_cmd, &exit_code, &output) ||
+      exit_code) {
+    LOG(ERROR) << "Failed to set vpd key for " << kActivePingKey
+               << " with exit code: " << exit_code
+               << " with error: " << output;
+    return;
+  }
+
+  vector<string> vpd_dump_cmd = { "dump_vpd_log", "--force" };
+  if (!Subprocess::SynchronousExec(vpd_dump_cmd, &exit_code, &output) ||
+      exit_code) {
+    LOG(ERROR) << "Failed to cache " << kActivePingKey<< " using dump_vpd_log"
+               << " with exit code: " << exit_code
+               << " with error: " << output;
+  }
+}
+
 }  // namespace chromeos_update_engine