buffet: Add same as privetd D-Bus methods and properties

We are moving privet code into buffet.

BUG=brillo:1161
TEST=`FEATURES=test emerge-gizmo buffet`

Change-Id: I49993ed66f40d828d046dd5b2c7c44edfc394276
Reviewed-on: https://chromium-review.googlesource.com/276601
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
Tested-by: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/dbus_bindings/org.chromium.Buffet.Manager.xml b/buffet/dbus_bindings/org.chromium.Buffet.Manager.xml
index 0fa4282..67028b5 100644
--- a/buffet/dbus_bindings/org.chromium.Buffet.Manager.xml
+++ b/buffet/dbus_bindings/org.chromium.Buffet.Manager.xml
@@ -76,6 +76,38 @@
       <arg name="echoed_message" type="s" direction="out"/>
       <annotation name="org.chromium.DBus.Method.Kind" value="simple"/>
     </method>
+    <method name="EnableWiFiBootstrapping">
+      <tp:docstring>
+        Enables WiFiBootstrapping if manual bootstrapping is selected via the
+        configuration file.  This will re-purpose a WiFi interface for use in
+        bootstrapping.  This breaks any existing WiFi connection on the
+        interface.
+      </tp:docstring>
+      <arg name="listener_path" type="o" direction="in">
+        <tp:docstring>
+          Path to an object exposed by the caller.  This object must support
+          the org.chromium.Buffet.WiFiBootstrapListener interface.
+        </tp:docstring>
+      </arg>
+      <arg name="options" type="a{sv}" direction="in"/>
+      <annotation name="org.chromium.DBus.Method.Kind" value="normal"/>
+    </method>
+    <method name="DisableWiFiBootstrapping">
+      <tp:docstring>
+        If a previous call to EnableWiFiBootstrapping was successful and
+        has not been cancelled or completed since, disables that bootstrapping
+        process.
+      </tp:docstring>
+      <annotation name="org.chromium.DBus.Method.Kind" value="normal"/>
+    </method>
+    <method name="EnableGCDBootstrapping">
+      <arg name="listener_path" type="o" direction="in"/>
+      <arg name="options" type="a{sv}" direction="in"/>
+      <annotation name="org.chromium.DBus.Method.Kind" value="normal"/>
+    </method>
+    <method name="DisableGCDBootstrapping">
+      <annotation name="org.chromium.DBus.Method.Kind" value="normal"/>
+    </method>
 
     <property name="Status" type="s" access="read">
       <tp:docstring>
@@ -155,5 +187,55 @@
                   device.
       </tp:docstring>
     </property>
+    <property name="WiFiBootstrapState" type="s" access="read">
+      <tp:docstring>
+        Contains one of the following values describing the state of WiFi
+        bootstrapping:
+          “disabled” - Bootstrapping has been disabled in the config file.
+          “waiting” - buffet is waiting to receive WiFi credentials from
+                      a paired peer.
+          “connecting” - buffet has received WiFi credentials, and is now
+                         attempting to connect to a WiFi network.
+          “monitoring” - buffet is monitoring our connectivity and will
+                         re-enable bootstrapping if connectivity fails in
+                         automatic mode.
+
+       Note: more values may be added later to this list.
+      </tp:docstring>
+    </property>
+    <property name="GCDBootstrapState" type="s" access="read">
+      <tp:docstring>
+        Contains one of the following values describing the state of GCD
+        bootstrapping:
+          “disabled” - GCD registration has been disabled in the config file.
+          “offline” - GCD registration is unknown because the device is offline.
+          “connecting” - GCD registration is unknown because the device is still
+                         connecting to the cloud.
+          “waiting” - Waiting to be configured with GCD credentials.
+          “registering” - Registering the device with the GCD servers.
+          “online” - Device is online and registered with GCD servers.
+
+        Note: more values may be added later to this list.
+
+        Clients that wish to present a single linear bootstrapping flow to users
+        may treat GCD bootstrapping states as a suffix to WiFi bootstrapping
+        states.  If we have no cloud connectivity, we cannot possibly do GCD
+        registration/credential verification.
+      </tp:docstring>
+    </property>
+    <property name="PairingInfo" type="a{sv}" access="read">
+      <tp:docstring>
+        Describes the state of device pairing. While no pairing attempt is in
+        progress, this dictionary will be empty. When a client initiates a
+        pairing transaction via /privet/v3/pairing/start, dictionary will
+        contain the following keys:
+          “sessionId” - ID of the pairing session; generated by device
+          “pairingMode” - Selected type of pairing from /privet/v3/pairing/start
+                          (e.g. “pinCode” or “embeddedCode”)
+          “code” - The pin code or embedded code as appropriate to the
+                   “pairingMode” value.  See design document.
+                   This value will be a string.
+      </tp:docstring>
+    </property>
   </interface>
 </node>
diff --git a/buffet/manager.cc b/buffet/manager.cc
index 6ca276d..5b6a6e5 100644
--- a/buffet/manager.cc
+++ b/buffet/manager.cc
@@ -25,6 +25,9 @@
 #include "buffet/base_api_handler.h"
 #include "buffet/commands/command_instance.h"
 #include "buffet/commands/schema_constants.h"
+#include "buffet/privet/constants.h"
+#include "buffet/privet/security_manager.h"
+#include "buffet/privet/wifi_bootstrap_manager.h"
 #include "buffet/states/state_change_queue.h"
 #include "buffet/states/state_manager.h"
 #include "buffet/storage_impls.h"
@@ -35,10 +38,16 @@
 namespace buffet {
 
 namespace {
+
 // Max of 100 state update events should be enough in the queue.
 const size_t kMaxStateChangeQueueSize = 100;
 // The number of seconds each HTTP request will be allowed before timing out.
 const int kRequestTimeoutSeconds = 30;
+
+const char kPairingSessionIdKey[] = "sessionId";
+const char kPairingModeKey[] = "mode";
+const char kPairingCodeKey[] = "code";
+
 }  // anonymous namespace
 
 Manager::Manager(const base::WeakPtr<ExportedObjectManager>& object_manager)
@@ -222,6 +231,40 @@
   return message;
 }
 
+bool Manager::EnableWiFiBootstrapping(
+    chromeos::ErrorPtr* error,
+    const dbus::ObjectPath& in_listener_path,
+    const chromeos::VariantDictionary& in_options) {
+  chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain,
+                         privetd::errors::kNotImplemented,
+                         "Manual WiFi bootstrapping is not implemented");
+  return false;
+}
+
+bool Manager::DisableWiFiBootstrapping(chromeos::ErrorPtr* error) {
+  chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain,
+                         privetd::errors::kNotImplemented,
+                         "Manual WiFi bootstrapping is not implemented");
+  return false;
+}
+
+bool Manager::EnableGCDBootstrapping(
+    chromeos::ErrorPtr* error,
+    const dbus::ObjectPath& in_listener_path,
+    const chromeos::VariantDictionary& in_options) {
+  chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain,
+                         privetd::errors::kNotImplemented,
+                         "Manual GCD bootstrapping is not implemented");
+  return false;
+}
+
+bool Manager::DisableGCDBootstrapping(chromeos::ErrorPtr* error) {
+  chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain,
+                         privetd::errors::kNotImplemented,
+                         "Manual GCD bootstrapping is not implemented");
+  return false;
+}
+
 bool Manager::UpdateDeviceInfo(chromeos::ErrorPtr* error,
                                const std::string& in_name,
                                const std::string& in_description,
@@ -277,4 +320,46 @@
   dbus_adaptor_.SetAnonymousAccessRole(config.local_anonymous_access_role());
 }
 
+void Manager::UpdateWiFiBootstrapState(
+    privetd::WifiBootstrapManager::State state) {
+  switch (state) {
+    case privetd::WifiBootstrapManager::kDisabled:
+      dbus_adaptor_.SetWiFiBootstrapState("disabled");
+      break;
+    case privetd::WifiBootstrapManager::kBootstrapping:
+      dbus_adaptor_.SetWiFiBootstrapState("waiting");
+      break;
+    case privetd::WifiBootstrapManager::kMonitoring:
+      dbus_adaptor_.SetWiFiBootstrapState("monitoring");
+      break;
+    case privetd::WifiBootstrapManager::kConnecting:
+      dbus_adaptor_.SetWiFiBootstrapState("connecting");
+      break;
+  }
+}
+
+void Manager::OnPairingStart(const std::string& session_id,
+                             privetd::PairingType pairing_type,
+                             const std::vector<uint8_t>& code) {
+  // For now, just overwrite the exposed PairInfo with
+  // the most recent pairing attempt.
+  dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{
+      {kPairingSessionIdKey, session_id},
+      {kPairingModeKey, PairingTypeToString(pairing_type)},
+      {kPairingCodeKey, code},
+  });
+}
+
+void Manager::OnPairingEnd(const std::string& session_id) {
+  auto exposed_pairing_attempt = dbus_adaptor_.GetPairingInfo();
+  auto it = exposed_pairing_attempt.find(kPairingSessionIdKey);
+  if (it == exposed_pairing_attempt.end()) {
+    return;
+  }
+  std::string exposed_session{it->second.TryGet<std::string>()};
+  if (exposed_session == session_id) {
+    dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{});
+  }
+}
+
 }  // namespace buffet
diff --git a/buffet/manager.h b/buffet/manager.h
index 35e6e25..781fa03 100644
--- a/buffet/manager.h
+++ b/buffet/manager.h
@@ -22,6 +22,8 @@
 #include "buffet/commands/command_manager.h"
 #include "buffet/device_registration_info.h"
 #include "buffet/org.chromium.Buffet.Manager.h"
+#include "buffet/privet/privet_manager.h"
+#include "buffet/privet/wifi_bootstrap_manager.h"
 
 namespace chromeos {
 namespace dbus_utils {
@@ -86,11 +88,26 @@
       const std::vector<std::string>& in_names,
       const std::string& in_visibility) override;
   std::string TestMethod(const std::string& message) override;
+  bool EnableWiFiBootstrapping(
+      chromeos::ErrorPtr* error,
+      const dbus::ObjectPath& in_listener_path,
+      const chromeos::VariantDictionary& in_options) override;
+  bool DisableWiFiBootstrapping(chromeos::ErrorPtr* error) override;
+  bool EnableGCDBootstrapping(
+      chromeos::ErrorPtr* error,
+      const dbus::ObjectPath& in_listener_path,
+      const chromeos::VariantDictionary& in_options) override;
+  bool DisableGCDBootstrapping(chromeos::ErrorPtr* error) override;
 
   void OnCommandDefsChanged();
   void OnStateChanged();
   void OnRegistrationChanged(RegistrationStatus status);
   void OnConfigChanged(const BuffetConfig& config);
+  void UpdateWiFiBootstrapState(privetd::WifiBootstrapManager::State state);
+  void OnPairingStart(const std::string& session_id,
+                      privetd::PairingType pairing_type,
+                      const std::vector<uint8_t>& code);
+  void OnPairingEnd(const std::string& session_id);
 
   org::chromium::Buffet::ManagerAdaptor dbus_adaptor_{this};
   chromeos::dbus_utils::DBusObject dbus_object_;
diff --git a/buffet/privet/dbus_manager.cc b/buffet/privet/dbus_manager.cc
deleted file mode 100644
index c6aa416..0000000
--- a/buffet/privet/dbus_manager.cc
+++ /dev/null
@@ -1,138 +0,0 @@
-// Copyright 2015 The Chromium OS Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "buffet/privet/dbus_manager.h"
-
-#include <base/memory/ref_counted.h>
-#include <chromeos/any.h>
-
-#include "buffet/privet/constants.h"
-#include "buffet/privet/security_delegate.h"
-#include "buffet/privet/security_manager.h"
-
-using chromeos::Any;
-using chromeos::dbus_utils::AsyncEventSequencer;
-using chromeos::dbus_utils::DBusObject;
-using chromeos::dbus_utils::ExportedObjectManager;
-using org::chromium::privetd::ManagerAdaptor;
-
-namespace privetd {
-
-namespace {
-
-const char kPingResponse[] = "Hello world!";
-const char kPairingSessionIdKey[] = "sessionId";
-const char kPairingModeKey[] = "mode";
-const char kPairingCodeKey[] = "code";
-
-}  // namespace
-
-DBusManager::DBusManager(ExportedObjectManager* object_manager,
-                         WifiBootstrapManager* wifi_bootstrap_manager,
-                         CloudDelegate* cloud_delegate,
-                         SecurityManager* security_manager)
-    : dbus_object_{new DBusObject{object_manager,
-                                  object_manager->GetBus(),
-                                  ManagerAdaptor::GetObjectPath()}} {
-  if (wifi_bootstrap_manager) {
-    wifi_bootstrap_manager->RegisterStateListener(
-        base::Bind(&DBusManager::UpdateWiFiBootstrapState,
-                   weak_ptr_factory_.GetWeakPtr()));
-  } else {
-    UpdateWiFiBootstrapState(WifiBootstrapManager::kDisabled);
-  }
-  security_manager->RegisterPairingListeners(
-      base::Bind(&DBusManager::OnPairingStart, weak_ptr_factory_.GetWeakPtr()),
-      base::Bind(&DBusManager::OnPairingEnd, weak_ptr_factory_.GetWeakPtr()));
-  // TODO(wiley) Watch for appropriate state variables from |cloud_delegate|.
-}
-
-void DBusManager::RegisterAsync(const CompletionAction& on_done) {
-  scoped_refptr<AsyncEventSequencer> sequencer(new AsyncEventSequencer());
-  dbus_adaptor_.RegisterWithDBusObject(dbus_object_.get());
-  dbus_object_->RegisterAsync(
-      sequencer->GetHandler("Failed exporting DBusManager.", true));
-  sequencer->OnAllTasksCompletedCall({on_done});
-}
-
-bool DBusManager::EnableWiFiBootstrapping(
-    chromeos::ErrorPtr* error,
-    const dbus::ObjectPath& in_listener_path,
-    const chromeos::VariantDictionary& in_options) {
-  chromeos::Error::AddTo(error, FROM_HERE, errors::kDomain,
-                         errors::kNotImplemented,
-                         "Manual WiFi bootstrapping is not implemented");
-  return false;
-}
-
-bool DBusManager::DisableWiFiBootstrapping(chromeos::ErrorPtr* error) {
-  chromeos::Error::AddTo(error, FROM_HERE, errors::kDomain,
-                         errors::kNotImplemented,
-                         "Manual WiFi bootstrapping is not implemented");
-  return false;
-}
-
-bool DBusManager::EnableGCDBootstrapping(
-    chromeos::ErrorPtr* error,
-    const dbus::ObjectPath& in_listener_path,
-    const chromeos::VariantDictionary& in_options) {
-  chromeos::Error::AddTo(error, FROM_HERE, errors::kDomain,
-                         errors::kNotImplemented,
-                         "Manual GCD bootstrapping is not implemented");
-  return false;
-}
-
-bool DBusManager::DisableGCDBootstrapping(chromeos::ErrorPtr* error) {
-  chromeos::Error::AddTo(error, FROM_HERE, errors::kDomain,
-                         errors::kNotImplemented,
-                         "Manual GCD bootstrapping is not implemented");
-  return false;
-}
-
-std::string DBusManager::Ping() {
-  return kPingResponse;
-}
-
-void DBusManager::UpdateWiFiBootstrapState(WifiBootstrapManager::State state) {
-  switch (state) {
-    case WifiBootstrapManager::kDisabled:
-      dbus_adaptor_.SetWiFiBootstrapState("disabled");
-      break;
-    case WifiBootstrapManager::kBootstrapping:
-      dbus_adaptor_.SetWiFiBootstrapState("waiting");
-      break;
-    case WifiBootstrapManager::kMonitoring:
-      dbus_adaptor_.SetWiFiBootstrapState("monitoring");
-      break;
-    case WifiBootstrapManager::kConnecting:
-      dbus_adaptor_.SetWiFiBootstrapState("connecting");
-      break;
-  }
-}
-
-void DBusManager::OnPairingStart(const std::string& session_id,
-                                 PairingType pairing_type,
-                                 const std::vector<uint8_t>& code) {
-  // For now, just overwrite the exposed PairInfo with
-  // the most recent pairing attempt.
-  dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{
-      {kPairingSessionIdKey, session_id},
-      {kPairingModeKey, PairingTypeToString(pairing_type)},
-      {kPairingCodeKey, code},
-  });
-}
-
-void DBusManager::OnPairingEnd(const std::string& session_id) {
-  auto exposed_pairing_attempt = dbus_adaptor_.GetPairingInfo();
-  auto it = exposed_pairing_attempt.find(kPairingSessionIdKey);
-  if (it == exposed_pairing_attempt.end()) {
-    return;
-  }
-  std::string exposed_session{it->second.TryGet<std::string>()};
-  if (exposed_session == session_id) {
-    dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{});
-  }
-}
-
-}  // namespace privetd
diff --git a/buffet/privet/dbus_manager.h b/buffet/privet/dbus_manager.h
deleted file mode 100644
index 03cce45..0000000
--- a/buffet/privet/dbus_manager.h
+++ /dev/null
@@ -1,76 +0,0 @@
-// Copyright 2015 The Chromium OS Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef BUFFET_PRIVET_DBUS_MANAGER_H_
-#define BUFFET_PRIVET_DBUS_MANAGER_H_
-
-#include <memory>
-#include <string>
-#include <vector>
-
-#include <base/macros.h>
-#include <chromeos/dbus/async_event_sequencer.h>
-#include <chromeos/dbus/dbus_object.h>
-#include <chromeos/errors/error.h>
-#include <chromeos/variant_dictionary.h>
-#include <dbus/object_path.h>
-
-#include "buffet/privet/org.chromium.privetd.Manager.h"
-#include "buffet/privet/wifi_bootstrap_manager.h"
-
-namespace chromeos {
-namespace dbus_utils {
-class ExportedObjectManager;
-}  // dbus_utils
-}  // chromeos
-
-namespace privetd {
-
-class CloudDelegate;
-class SecurityManager;
-enum class PairingType;
-
-// Exposes most of the privetd DBus interface.
-class DBusManager : public org::chromium::privetd::ManagerInterface {
- public:
-  using CompletionAction =
-      chromeos::dbus_utils::AsyncEventSequencer::CompletionAction;
-
-  DBusManager(chromeos::dbus_utils::ExportedObjectManager* object_manager,
-              WifiBootstrapManager* wifi_bootstrap_manager,
-              CloudDelegate* cloud_delegate,
-              SecurityManager* security_manager);
-  ~DBusManager() override = default;
-  void RegisterAsync(const CompletionAction& on_done);
-
-  // DBus handlers
-  bool EnableWiFiBootstrapping(
-      chromeos::ErrorPtr* error,
-      const dbus::ObjectPath& in_listener_path,
-      const chromeos::VariantDictionary& in_options) override;
-  bool DisableWiFiBootstrapping(chromeos::ErrorPtr* error) override;
-  bool EnableGCDBootstrapping(
-      chromeos::ErrorPtr* error,
-      const dbus::ObjectPath& in_listener_path,
-      const chromeos::VariantDictionary& in_options) override;
-  bool DisableGCDBootstrapping(chromeos::ErrorPtr* error) override;
-  std::string Ping() override;
-
- private:
-  void UpdateWiFiBootstrapState(WifiBootstrapManager::State state);
-  void OnPairingStart(const std::string& session_id,
-                      PairingType pairing_type,
-                      const std::vector<uint8_t>& code);
-  void OnPairingEnd(const std::string& session_id);
-
-  org::chromium::privetd::ManagerAdaptor dbus_adaptor_{this};
-  std::unique_ptr<chromeos::dbus_utils::DBusObject> dbus_object_;
-  base::WeakPtrFactory<DBusManager> weak_ptr_factory_{this};
-
-  DISALLOW_COPY_AND_ASSIGN(DBusManager);
-};
-
-}  // namespace privetd
-
-#endif  // BUFFET_PRIVET_DBUS_MANAGER_H_