weaved: Split command and state property name into a trait and a name

Service::AddCommandHandler and Service::SetStateProperty now accept
the propetry name and the trait name as separate parameter which allows
for using a literal constant for the trait name in client code.

BUG: 26443877
Change-Id: I77a7cf22db74b64b28a890cdd2dafeb5546ce78b
diff --git a/libweaved/service.cc b/libweaved/service.cc
index 83a404d..037c037 100644
--- a/libweaved/service.cc
+++ b/libweaved/service.cc
@@ -18,6 +18,7 @@
 
 #include <base/bind.h>
 #include <base/memory/weak_ptr.h>
+#include <base/strings/stringprintf.h>
 #include <binderwrapper/binder_wrapper.h>
 #include <brillo/message_loops/message_loop.h>
 
@@ -163,13 +164,15 @@
                     const std::vector<std::string>& traits,
                     brillo::ErrorPtr* error) override;
   void AddCommandHandler(const std::string& component,
+                         const std::string& trait_name,
                          const std::string& command_name,
                          const CommandHandlerCallback& callback) override;
   bool SetStateProperties(const std::string& component,
                           const brillo::VariantDictionary& dict,
                           brillo::ErrorPtr* error) override;
   bool SetStateProperty(const std::string& component,
-                        const std::string& name,
+                        const std::string& trait_name,
+                        const std::string& property_name,
                         const brillo::Any& value,
                         brillo::ErrorPtr* error) override;
 
@@ -276,20 +279,24 @@
 }
 
 void ServiceImpl::AddCommandHandler(const std::string& component,
+                                    const std::string& trait_name,
                                     const std::string& command_name,
                                     const CommandHandlerCallback& callback) {
   CHECK(!component.empty() && !command_name.empty());
   CHECK(weave_service_.get());
 
+  std::string full_command_name =
+      base::StringPrintf("%s.%s", trait_name.c_str(), command_name.c_str());
+
   CommandHandlerEntry entry;
   entry.component = component;
-  entry.command_name = command_name;
+  entry.command_name = full_command_name;
   entry.callback = callback;
   command_handlers_.push_back(std::move(entry));
 
   auto status = weave_service_->registerCommandHandler(
       android::String16{component.c_str()},
-      android::String16{command_name.c_str()});
+      android::String16{full_command_name.c_str()});
   CHECK(status.isOk());
 }
 
@@ -307,9 +314,12 @@
 }
 
 bool ServiceImpl::SetStateProperty(const std::string& component,
-                                   const std::string& name,
+                                   const std::string& trait_name,
+                                   const std::string& property_name,
                                    const brillo::Any& value,
                                    brillo::ErrorPtr* error) {
+  std::string name =
+      base::StringPrintf("%s.%s", trait_name.c_str(), property_name.c_str());
   return SetStateProperties(component, brillo::VariantDictionary{{name, value}},
                             error);
 }
diff --git a/libweaved/service.h b/libweaved/service.h
index 7ae6824..415b230 100644
--- a/libweaved/service.h
+++ b/libweaved/service.h
@@ -54,10 +54,11 @@
                             brillo::ErrorPtr* error) = 0;
 
   // Sets handler for new commands added to the queue for a given |component|.
-  // |command_name| is the full name of the command to handle, including the
-  // name of the trait, e.g. "base.reboot".
+  // |command_name| is the name of the command to handle (e.g. "reboot").
+  // |trait_name| is the name of a trait the command belongs to (e.g. "base").
   // Each command can have no more than one handler.
   virtual void AddCommandHandler(const std::string& component,
+                                 const std::string& trait_name,
                                  const std::string& command_name,
                                  const CommandHandlerCallback& callback) = 0;
 
@@ -68,9 +69,12 @@
                                   brillo::ErrorPtr* error) = 0;
 
   // Sets value of the single property.
-  // |name| is full property name, including trait name. e.g. "base.network".
+  // |component| is a path to the component to set the property value to.
+  // |trait_name| is the name of the trait this property is part of.
+  // |property_name| is the property name.
   virtual bool SetStateProperty(const std::string& component,
-                                const std::string& name,
+                                const std::string& trait_name,
+                                const std::string& property_name,
                                 const brillo::Any& value,
                                 brillo::ErrorPtr* error) = 0;