Home | History | Annotate | only in /system/weaved/libweaved
Up to higher level directory
NameDateSize
command.cc21-Oct-20166.5K
command.h21-Oct-20165K
export.h21-Oct-2016893
README.md21-Oct-20162.6K
service.cc21-Oct-201620.8K
service.h21-Oct-20165.9K

README.md

      1 For system daemons which need to interface with the weave daemon (weaved), these
      2 daemons will need to link to **libweaved**.
      3 
      4 The `weaved::Service` class is an entry point into weave daemon interface.
      5 This class maintains an IPC connection to the daemon and allows clients to
      6 register weave command handlers and update the device state they are
      7 responsible for.
      8 
      9 In order to create an instance of `Service`, call asynchronous
     10 `Service::Connect` static method. This method initiates a connection to weaved
     11 and once established invokes the provided `callback`. When the callback is
     12 invoked, the connection to the weave daemon is available and the client should
     13 create their component, register command handlers and update the state.
     14 If connection is lost (e.g. the weave daemon exist), the provided weak
     15 pointer to the `Service` object becomes invalidated. As soon as weaved is
     16 restarted and the connection is restored, the `callback` is invoked again and
     17 the client can re-register command handlers, update the state again.
     18 
     19 A simple client daemon that works with weaved could be as follows:
     20 
     21 ```
     22 class Daemon final : public brillo::Daemon {
     23  public:
     24   Daemon() = default;
     25 
     26  protected:
     27   int OnInit() override;
     28 
     29  private:
     30   void OnConnected(const std::weak_ptr<weaved::Service>& service);
     31   void OnCommand1(std::unique_ptr<weaved::Command> command);
     32   void UpdateDeviceState();
     33 
     34   std::unique_ptr<weaved::Service::Token> weave_service_token_;
     35   std::weak_ptr<weaved::Service> weave_service_;
     36   brillo::BinderWatcher binder_watcher_;
     37   base::WeakPtrFactory<Daemon> weak_ptr_factory_{this};
     38   DISALLOW_COPY_AND_ASSIGN(Daemon);
     39 };
     40 
     41 int Daemon::OnInit() {
     42   android::BinderWrapper::Create();
     43   if (!binder_watcher_.Init())
     44     return EX_OSERR;
     45 
     46   weave_service_token_ = weaved::Service::Connect(
     47       brillo::MessageLoop::current(),
     48       base::Bind(&Daemon::OnConnected, weak_ptr_factory_.GetWeakPtr()));
     49   return brillo::Daemon::OnInit();
     50 }
     51 
     52 void Daemon::OnConnected(const std::weak_ptr<weaved::Service>& service) {
     53   weave_service_ = service;
     54   auto weave_service = weave_service_.lock();
     55   if (!weave_service)
     56     return;
     57 
     58   weave_service->AddComponent("myComponent", {"_myTrait"}, nullptr);
     59   weave_service->AddCommandHandler(
     60       "myComponent", "_myTrait.command1",
     61       base::Bind(&Daemon::OnCommand1, base::Unretained(this)));
     62   UpdateDeviceState();
     63 }
     64 
     65 void Daemon::UpdateDeviceState() {
     66   auto weave_service = weave_service_.lock();
     67   if (!weave_service)
     68     return;
     69 
     70   brillo::VariantDictionary state_change{
     71     {"_myTrait.state1", 12},
     72     {"_myTrait.state2", std::string{"foo"}},
     73   };
     74   weave_service->SetStateProperties("myComponent", state_change, nullptr);
     75 }
     76 ```