Add client library for weaved

Added client library for weaved to hide the underlying D-Bus IPC
as much as possible.

Change-Id: Id91dad80c593c58124b27a90a34cd3b1852e898f
diff --git a/Android.mk b/Android.mk
index c245939..f207dbe 100644
--- a/Android.mk
+++ b/Android.mk
@@ -94,16 +94,16 @@
 LOCAL_SRC_FILES += buffet/fake_encryptor.cc
 endif
 
-
 include $(BUILD_STATIC_LIBRARY)
 
-# buffet
+# weaved
 # ========================================================
 include $(CLEAR_VARS)
 LOCAL_MODULE := weaved
 LOCAL_REQUIRED_MODULES := \
 	avahi-daemon \
 	com.android.Weave.conf \
+	libweaved \
 	webservd \
 
 LOCAL_CPP_EXTENSION := $(buffetCommonCppExtension)
@@ -121,10 +121,12 @@
 
 include $(BUILD_EXECUTABLE)
 
-# libweaved-client
+# libweaved-internal
 # ========================================================
+# You do not want to depend on this.  Depend on libweaved instead.
+# libweaved abstracts and helps you consume this interface.
 include $(CLEAR_VARS)
-LOCAL_MODULE := libweaved-client
+LOCAL_MODULE := libweaved-internal
 LOCAL_DBUS_PROXY_PREFIX := buffet
 
 LOCAL_SRC_FILES := \
@@ -134,6 +136,29 @@
 
 include $(BUILD_SHARED_LIBRARY)
 
+# libweaved
+# ========================================================
+include $(CLEAR_VARS)
+LOCAL_MODULE := libweaved
+LOCAL_CPP_EXTENSION := $(buffetCommonCppExtension)
+LOCAL_CFLAGS := $(buffetCommonCFlags)
+LOCAL_CPPFLAGS := $(buffetCommonCppFlags)
+LOCAL_C_INCLUDES := external/gtest/include
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)
+LOCAL_SHARED_LIBRARIES := \
+	$(buffetSharedLibraries) \
+	libweaved-internal \
+
+LOCAL_STATIC_LIBRARIES :=
+LOCAL_RTTI_FLAG := -frtti
+LOCAL_CLANG := true
+
+LOCAL_SRC_FILES := \
+	libweaved/command.cc \
+	libweaved/device.cc \
+
+include $(BUILD_SHARED_LIBRARY)
+
 # buffet_testrunner
 # ========================================================
 include $(CLEAR_VARS)
diff --git a/libweaved/command.cc b/libweaved/command.cc
new file mode 100644
index 0000000..4df7c4f
--- /dev/null
+++ b/libweaved/command.cc
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "command.h"
+
+#include "buffet/dbus-proxies.h"
+
+namespace weaved {
+
+Command::Command(com::android::Weave::CommandProxy* proxy) : proxy_{proxy} {}
+
+const std::string& Command::GetID() const {
+  return proxy_->id();
+}
+
+const std::string& Command::GetName() const {
+  return proxy_->name();
+}
+
+Command::State Command::GetState() const {
+  std::string state = proxy_->state();
+  if (state == "queued")
+    return Command::State::kQueued;
+  else if (state == "inProgress")
+    return Command::State::kInProgress;
+  else if (state == "paused")
+    return Command::State::kPaused;
+  else if (state == "error")
+    return Command::State::kError;
+  else if (state == "done")
+    return Command::State::kDone;
+  else if (state == "cancelled")
+    return Command::State::kCancelled;
+  else if (state == "aborted")
+    return Command::State::kAborted;
+  else if (state == "expired")
+    return Command::State::kExpired;
+  LOG(WARNING) << "Unknown command state: " << state;
+  return Command::State::kQueued;
+}
+
+Command::Origin Command::GetOrigin() const {
+  std::string origin = proxy_->origin();
+  if (origin == "local")
+    return Command::Origin::kLocal;
+  else if (origin == "cloud")
+    return Command::Origin::kCloud;
+  LOG(WARNING) << "Unknown command origin: " << origin;
+  return Command::Origin::kLocal;
+}
+
+const chromeos::VariantDictionary& Command::GetParameters() const {
+  return proxy_->parameters();
+}
+
+bool Command::SetProgress(const chromeos::VariantDictionary& progress,
+                          chromeos::ErrorPtr* error) {
+  return proxy_->SetProgress(progress, error);
+}
+
+bool Command::Complete(const chromeos::VariantDictionary& results,
+                       chromeos::ErrorPtr* error) {
+  return proxy_->Complete(results, error);
+}
+
+bool Command::Abort(const std::string& error_code,
+                    const std::string& error_message,
+                    chromeos::ErrorPtr* error) {
+  return proxy_->Abort(error_code, error_message, error);
+}
+
+bool Command::Cancel(chromeos::ErrorPtr* error) {
+  return proxy_->Cancel(error);
+}
+
+}  // namespace weave
diff --git a/libweaved/command.h b/libweaved/command.h
new file mode 100644
index 0000000..e9eb24b
--- /dev/null
+++ b/libweaved/command.h
@@ -0,0 +1,117 @@
+/*
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef LIBWEAVED_COMMAND_H_
+#define LIBWEAVED_COMMAND_H_
+
+#include <string>
+
+#include <base/macros.h>
+#include <chromeos/errors/error.h>
+#include <chromeos/variant_dictionary.h>
+#include <libweaved/export.h>
+
+namespace com {
+namespace android {
+namespace Weave {
+class CommandProxy;
+}  // namespace Weave
+}  // namespace android
+}  // namespace com
+
+
+namespace weaved {
+
+class Device;
+
+class LIBWEAVED_EXPORT Command final {
+ public:
+  enum class State {
+    kQueued,
+    kInProgress,
+    kPaused,
+    kError,
+    kDone,
+    kCancelled,
+    kAborted,
+    kExpired,
+  };
+
+  enum class Origin { kLocal, kCloud };
+
+  // Returns the full command ID.
+  const std::string& GetID() const;
+
+  // Returns the full name of the command.
+  const std::string& GetName() const;
+
+  // Returns the command state.
+  Command::State GetState() const;
+
+  // Returns the origin of the command.
+  Command::Origin GetOrigin() const;
+
+  // Returns the command parameters.
+  const chromeos::VariantDictionary& GetParameters() const;
+
+  // Helper function to get a command parameter of particular type T from the
+  // command parameter list. Returns default value for type T (e.g. 0 for int or
+  // or "" for std::string) if the parameter with the given name is not found or
+  // is of incorrect type.
+  template <typename T>
+  T GetParameter(const std::string& name) const {
+    const chromeos::VariantDictionary& parameters = GetParameters();
+    T value{};
+    auto p = parameters.find(name);
+    if (p != parameters.end())
+      p->second.GetValue<T>(&value);
+    return value;
+  }
+
+  // Updates the command progress. The |progress| should match the schema.
+  // Returns false if |progress| value is incorrect.
+  bool SetProgress(const chromeos::VariantDictionary& progress,
+                   chromeos::ErrorPtr* error);
+
+  // Sets command into terminal "done" state.
+  // Updates the command results. The |results| should match the schema.
+  // Returns false if |results| value is incorrect.
+  bool Complete(const chromeos::VariantDictionary& results,
+                chromeos::ErrorPtr* error);
+
+  // Aborts command execution.
+  // Sets command into terminal "aborted" state.
+  bool Abort(const std::string& error_code,
+             const std::string& error_message,
+             chromeos::ErrorPtr* error);
+
+  // Cancels command execution.
+  // Sets command into terminal "canceled" state.
+  bool Cancel(chromeos::ErrorPtr* error);
+
+ protected:
+  Command(com::android::Weave::CommandProxy* proxy);
+
+ private:
+  friend class Device;
+  com::android::Weave::CommandProxy* proxy_{nullptr};
+
+  DISALLOW_COPY_AND_ASSIGN(Command);
+};
+
+}  // namespace weave
+
+#endif  // LIBWEAVED_COMMAND_H_
diff --git a/libweaved/device.cc b/libweaved/device.cc
new file mode 100644
index 0000000..6e218ae
--- /dev/null
+++ b/libweaved/device.cc
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "device.h"
+
+#include "buffet/dbus-proxies.h"
+
+using com::android::Weave::CommandProxy;
+using com::android::Weave::ManagerProxy;
+
+namespace weaved {
+
+Device::Device(const scoped_refptr<dbus::Bus>& bus,
+               const base::Closure& state_required_callback)
+    : bus_{bus}, state_required_callback_{state_required_callback} {
+  weaved_object_mgr_.reset(new com::android::Weave::ObjectManagerProxy{bus_});
+  weaved_object_mgr_->SetCommandAddedCallback(
+      base::Bind(&Device::OnCommandAdded, base::Unretained(this)));
+  weaved_object_mgr_->SetCommandRemovedCallback(
+      base::Bind(&Device::OnCommandRemoved, base::Unretained(this)));
+  weaved_object_mgr_->SetManagerAddedCallback(
+      base::Bind(&Device::OnManagerAdded, base::Unretained(this)));
+  weaved_object_mgr_->SetCommandRemovedCallback(
+      base::Bind(&Device::OnManagerRemoved, base::Unretained(this)));
+}
+
+Device::~Device() {
+}
+
+std::unique_ptr<Device> Device::CreateInstance(
+    const scoped_refptr<dbus::Bus>& bus,
+    const base::Closure& state_required_callback) {
+  return std::unique_ptr<Device>{new Device{bus, state_required_callback}};
+}
+
+void Device::AddCommandHandler(const std::string& command_name,
+                               const CommandHandlerCallback& callback) {
+  command_handler_map_.emplace(command_name, callback);
+
+  // If there are any commands already received, call the handler immediately.
+  for (auto& pair : command_map_) {
+    if (pair.first->name() == command_name) {
+      if (!pair.second)
+        pair.second.reset(new Command{pair.first});
+      callback.Run(pair.second);
+    }
+  }
+}
+
+bool Device::SetStateProperties(const chromeos::VariantDictionary& dict,
+                                chromeos::ErrorPtr* error) {
+  if (proxy_)
+    return proxy_->UpdateState(dict, error);
+
+  chromeos::Error::AddTo(error, FROM_HERE, "weaved", "service_unavailable",
+                         "Process 'weaved' is unreachable");
+  return false;
+}
+
+bool Device::SetStateProperty(const std::string& name,
+                              const chromeos::Any& value,
+                              chromeos::ErrorPtr* error) {
+  return SetStateProperties(chromeos::VariantDictionary{{name, value}}, error);
+}
+
+void Device::OnCommandAdded(CommandProxy* proxy) {
+  std::shared_ptr<Command>& command = command_map_[proxy];
+  auto iter = command_handler_map_.find(proxy->name());
+  if (iter == command_handler_map_.end())
+    return;
+  if (!command)
+    command.reset(new Command{proxy});
+  iter->second.Run(command);
+}
+
+void Device::OnCommandRemoved(const dbus::ObjectPath& object_path) {
+  auto proxy = weaved_object_mgr_->GetCommandProxy(object_path);
+  if (!proxy)
+    return;
+  command_map_.erase(proxy);
+}
+
+void Device::OnManagerAdded(ManagerProxy* proxy) {
+  proxy_ = proxy;
+  state_required_callback_.Run();
+}
+
+void Device::OnManagerRemoved(const dbus::ObjectPath& object_path) {
+  proxy_ = nullptr;
+}
+
+}  // namespace weave
diff --git a/libweaved/device.h b/libweaved/device.h
new file mode 100644
index 0000000..45e0c16
--- /dev/null
+++ b/libweaved/device.h
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#ifndef LIBWEAVED_DEVICE_H_
+#define LIBWEAVED_DEVICE_H_
+
+#include <map>
+#include <memory>
+#include <string>
+
+#include <base/callback.h>
+#include <base/macros.h>
+#include <base/memory/ref_counted.h>
+#include <chromeos/any.h>
+#include <chromeos/errors/error.h>
+#include <chromeos/variant_dictionary.h>
+#include <libweaved/command.h>
+#include <libweaved/export.h>
+
+namespace com {
+namespace android {
+namespace Weave {
+class CommandProxy;
+class ManagerProxy;
+class ObjectManagerProxy;
+}  // namespace Weave
+}  // namespace android
+}  // namespace com
+
+namespace dbus {
+class Bus;
+class ObjectPath;
+}  // namespace dbus
+
+namespace weaved {
+
+class LIBWEAVED_EXPORT Device final {
+ public:
+  ~Device();
+
+  static std::unique_ptr<Device> CreateInstance(
+      const scoped_refptr<dbus::Bus>& bus,
+      const base::Closure& state_required_callback);
+
+  // Callback type for AddCommandHandler.
+  using CommandHandlerCallback =
+      base::Callback<void(const std::weak_ptr<Command>& command)>;
+
+  // Sets handler for new commands added to the queue.
+  // |command_name| is the full command name of the command to handle. e.g.
+  // "base.reboot". Each command can have no more than one handler.
+  // Empty |command_name| sets default handler for all unhanded commands.
+  // No new command handlers can be set after default handler was set.
+  void AddCommandHandler(const std::string& command_name,
+                         const CommandHandlerCallback& callback);
+
+  bool SetStateProperties(const chromeos::VariantDictionary& dict,
+                          chromeos::ErrorPtr* error);
+
+  // Sets value of the single property.
+  // |name| is full property name, including package name. e.g. "base.network".
+  bool SetStateProperty(const std::string& name,
+                        const chromeos::Any& value,
+                        chromeos::ErrorPtr* error);
+
+ private:
+  Device(const scoped_refptr<dbus::Bus>& bus,
+         const base::Closure& state_required_callback);
+
+  void OnCommandAdded(com::android::Weave::CommandProxy* proxy);
+  void OnCommandRemoved(const dbus::ObjectPath& object_path);
+
+  void OnManagerAdded(com::android::Weave::ManagerProxy* proxy);
+  void OnManagerRemoved(const dbus::ObjectPath& object_path);
+
+  std::unique_ptr<com::android::Weave::ObjectManagerProxy> weaved_object_mgr_;
+  com::android::Weave::ManagerProxy* proxy_{nullptr};
+
+  using CommandMap =
+      std::map<com::android::Weave::CommandProxy*, std::shared_ptr<Command>>;
+  CommandMap command_map_;
+  std::map<std::string, CommandHandlerCallback> command_handler_map_;
+  scoped_refptr<dbus::Bus> bus_;
+  base::Closure state_required_callback_;
+
+
+  DISALLOW_COPY_AND_ASSIGN(Device);
+};
+
+}  // namespace weave
+
+#endif  // LIBWEAVE_INCLUDE_WEAVE_DEVICE_H_
diff --git a/libweaved/export.h b/libweaved/export.h
new file mode 100644
index 0000000..eb88023
--- /dev/null
+++ b/libweaved/export.h
@@ -0,0 +1,21 @@
+// Copyright 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef LIBWEAVED_EXPORT_H_
+#define LIBWEAVED_EXPORT_H_
+
+#define LIBWEAVED_EXPORT __attribute__((__visibility__("default")))
+#define LIBWEAVED_PRIVATE __attribute__((__visibility__("hidden")))
+
+#endif  // LIBWEAVED_EXPORT_H_