1 // Copyright 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "buffet/binder_weave_service.h"
16 
17 #include <algorithm>
18 
19 #include <base/bind.h>
20 #include <weave/command.h>
21 #include <weave/device.h>
22 
23 #include "buffet/binder_command_proxy.h"
24 #include "common/binder_utils.h"
25 
26 using weaved::binder_utils::ToStatus;
27 using weaved::binder_utils::ToString;
28 using weaved::binder_utils::ToString16;
29 
30 namespace buffet {
31 
BinderWeaveService(weave::Device * device,android::sp<android::weave::IWeaveClient> client)32 BinderWeaveService::BinderWeaveService(
33     weave::Device* device,
34     android::sp<android::weave::IWeaveClient> client)
35     : device_{device}, client_{client} {}
36 
~BinderWeaveService()37 BinderWeaveService::~BinderWeaveService() {
38   // TODO(avakulenko): Make it possible to remove components from the tree in
39   // libweave and enable the following code.
40   // for (const std::string& component : components_)
41   //   device_->RemoveComponent(component, nullptr);
42 }
43 
addComponent(const android::String16 & name,const std::vector<android::String16> & traits)44 android::binder::Status BinderWeaveService::addComponent(
45     const android::String16& name,
46     const std::vector<android::String16>& traits) {
47   std::string component_name = ToString(name);
48   weave::ErrorPtr error;
49   std::vector<std::string> supported_traits;
50   std::transform(traits.begin(), traits.end(),
51                  std::back_inserter(supported_traits), ToString);
52   if (!device_->AddComponent(component_name, supported_traits, &error))
53     return ToStatus(false, &error);
54   components_.push_back(component_name);
55   return android::binder::Status::ok();
56 }
57 
registerCommandHandler(const android::String16 & component,const android::String16 & command)58 android::binder::Status BinderWeaveService::registerCommandHandler(
59     const android::String16& component,
60     const android::String16& command) {
61   std::string component_name = ToString(component);
62   std::string command_name = ToString(command);
63   device_->AddCommandHandler(component_name, command_name,
64                              base::Bind(&BinderWeaveService::OnCommand,
65                                         weak_ptr_factory_.GetWeakPtr(),
66                                         component_name, command_name));
67   return android::binder::Status::ok();
68 }
69 
updateState(const android::String16 & component,const android::String16 & state)70 android::binder::Status BinderWeaveService::updateState(
71     const android::String16& component,
72     const android::String16& state) {
73   weave::ErrorPtr error;
74   return ToStatus(device_->SetStatePropertiesFromJson(ToString(component),
75                                                       ToString(state),
76                                                       &error),
77                   &error);
78 }
79 
OnCommand(const std::string & component_name,const std::string & command_name,const std::weak_ptr<weave::Command> & command)80 void BinderWeaveService::OnCommand(
81     const std::string& component_name,
82     const std::string& command_name,
83     const std::weak_ptr<weave::Command>& command) {
84   android::sp<android::weave::IWeaveCommand> command_proxy =
85       new BinderCommandProxy{command};
86   client_->onCommand(ToString16(component_name), ToString16(command_name),
87                      command_proxy);
88 }
89 
90 }  // namespace buffet
91