• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..--

README.mdD22-Nov-20232.6 KiB7663

command.ccD22-Nov-20236.5 KiB192150

command.hD22-Nov-20235 KiB15375

export.hD22-Nov-2023893 246

service.ccD22-Nov-202320.8 KiB511335

service.hD22-Nov-20235.9 KiB14262

README.md

1For system daemons which need to interface with the weave daemon (weaved), these
2daemons will need to link to **libweaved**.
3
4The `weaved::Service` class is an entry point into weave daemon interface.
5This class maintains an IPC connection to the daemon and allows clients to
6register weave command handlers and update the device state they are
7responsible for.
8
9In order to create an instance of `Service`, call asynchronous
10`Service::Connect` static method. This method initiates a connection to weaved
11and once established invokes the provided `callback`. When the callback is
12invoked, the connection to the weave daemon is available and the client should
13create their component, register command handlers and update the state.
14If connection is lost (e.g. the weave daemon exist), the provided weak
15pointer to the `Service` object becomes invalidated. As soon as weaved is
16restarted and the connection is restored, the `callback` is invoked again and
17the client can re-register command handlers, update the state again.
18
19A simple client daemon that works with weaved could be as follows:
20
21```
22class 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
41int 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
52void 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
65void 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```