1 // Copyright 2015 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/ap_manager_client.h"
16 
17 namespace buffet {
18 
19 using org::chromium::apmanager::ConfigProxyInterface;
20 using org::chromium::apmanager::ManagerProxyInterface;
21 using org::chromium::apmanager::ServiceProxyInterface;
22 
ApManagerClient(const scoped_refptr<dbus::Bus> & bus)23 ApManagerClient::ApManagerClient(const scoped_refptr<dbus::Bus>& bus)
24     : bus_(bus) {}
25 
~ApManagerClient()26 ApManagerClient::~ApManagerClient() {
27   Stop();
28 }
29 
Start(const std::string & ssid)30 void ApManagerClient::Start(const std::string& ssid) {
31   if (service_path_.IsValid()) {
32     return;
33   }
34 
35   ssid_ = ssid;
36 
37   object_manager_proxy_.reset(
38       new org::chromium::apmanager::ObjectManagerProxy{bus_});
39   object_manager_proxy_->SetManagerAddedCallback(base::Bind(
40       &ApManagerClient::OnManagerAdded, weak_ptr_factory_.GetWeakPtr()));
41   object_manager_proxy_->SetServiceAddedCallback(base::Bind(
42       &ApManagerClient::OnServiceAdded, weak_ptr_factory_.GetWeakPtr()));
43 
44   object_manager_proxy_->SetServiceRemovedCallback(base::Bind(
45       &ApManagerClient::OnServiceRemoved, weak_ptr_factory_.GetWeakPtr()));
46   object_manager_proxy_->SetManagerRemovedCallback(base::Bind(
47       &ApManagerClient::OnManagerRemoved, weak_ptr_factory_.GetWeakPtr()));
48 }
49 
Stop()50 void ApManagerClient::Stop() {
51   if (manager_proxy_ && service_path_.IsValid()) {
52     RemoveService(service_path_);
53   }
54   service_path_ = dbus::ObjectPath();
55   service_proxy_ = nullptr;
56   manager_proxy_ = nullptr;
57   object_manager_proxy_.reset();
58   ssid_.clear();
59 }
60 
RemoveService(const dbus::ObjectPath & object_path)61 void ApManagerClient::RemoveService(const dbus::ObjectPath& object_path) {
62   CHECK(object_path.IsValid());
63   brillo::ErrorPtr error;
64   if (!manager_proxy_->RemoveService(object_path, &error)) {
65     LOG(ERROR) << "RemoveService failed: " << error->GetMessage();
66   }
67 }
68 
OnManagerAdded(ManagerProxyInterface * manager_proxy)69 void ApManagerClient::OnManagerAdded(ManagerProxyInterface* manager_proxy) {
70   VLOG(1) << "manager added: " << manager_proxy->GetObjectPath().value();
71   manager_proxy_ = manager_proxy;
72 
73   if (service_path_.IsValid())
74     return;
75 
76   brillo::ErrorPtr error;
77   if (!manager_proxy_->CreateService(&service_path_, &error)) {
78     LOG(ERROR) << "CreateService failed: " << error->GetMessage();
79   }
80 }
81 
OnServiceAdded(ServiceProxyInterface * service_proxy)82 void ApManagerClient::OnServiceAdded(ServiceProxyInterface* service_proxy) {
83   VLOG(1) << "service added: " << service_proxy->GetObjectPath().value();
84   if (service_proxy->GetObjectPath() != service_path_) {
85     RemoveService(service_proxy->GetObjectPath());
86     return;
87   }
88   service_proxy_ = service_proxy;
89 
90   ConfigProxyInterface* config_proxy =
91       object_manager_proxy_->GetConfigProxy(service_proxy->config());
92   config_proxy->set_ssid(ssid_, base::Bind(&ApManagerClient::OnSsidSet,
93                                            weak_ptr_factory_.GetWeakPtr()));
94 }
95 
OnSsidSet(bool success)96 void ApManagerClient::OnSsidSet(bool success) {
97   if (!success || !service_proxy_) {
98     LOG(ERROR) << "Failed to set ssid.";
99     return;
100   }
101   VLOG(1) << "SSID is set: " << ssid_;
102 
103   brillo::ErrorPtr error;
104   if (!service_proxy_->Start(&error)) {
105     LOG(ERROR) << "Service start failed: " << error->GetMessage();
106   }
107 }
108 
OnServiceRemoved(const dbus::ObjectPath & object_path)109 void ApManagerClient::OnServiceRemoved(const dbus::ObjectPath& object_path) {
110   VLOG(1) << "service removed: " << object_path.value();
111   if (object_path != service_path_)
112     return;
113   service_path_ = dbus::ObjectPath();
114   service_proxy_ = nullptr;
115 }
116 
OnManagerRemoved(const dbus::ObjectPath & object_path)117 void ApManagerClient::OnManagerRemoved(const dbus::ObjectPath& object_path) {
118   VLOG(1) << "manager removed: " << object_path.value();
119   manager_proxy_ = nullptr;
120   Stop();
121 }
122 
123 }  // namespace buffet
124