blob: bce571e1c3ae643cb152e02cafa0c065d24d8b1b [file] [log] [blame]
Vitaly Bukacad20f02015-10-16 17:27:15 -07001// 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.
Vitaly Buka3ef4fff2015-07-31 01:12:07 -070014
15#include "buffet/ap_manager_client.h"
16
17namespace buffet {
18
Alex Vakulenkode94eb52015-12-08 17:11:20 -080019using org::chromium::apmanager::ConfigProxyInterface;
20using org::chromium::apmanager::ManagerProxyInterface;
21using org::chromium::apmanager::ServiceProxyInterface;
Vitaly Buka3ef4fff2015-07-31 01:12:07 -070022
23ApManagerClient::ApManagerClient(const scoped_refptr<dbus::Bus>& bus)
24 : bus_(bus) {}
25
26ApManagerClient::~ApManagerClient() {
27 Stop();
28}
29
30void 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
50void 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
61void ApManagerClient::RemoveService(const dbus::ObjectPath& object_path) {
62 CHECK(object_path.IsValid());
Alex Vakulenko41705852015-10-13 10:12:06 -070063 brillo::ErrorPtr error;
Vitaly Buka3ef4fff2015-07-31 01:12:07 -070064 if (!manager_proxy_->RemoveService(object_path, &error)) {
65 LOG(ERROR) << "RemoveService failed: " << error->GetMessage();
66 }
67}
68
Alex Vakulenkode94eb52015-12-08 17:11:20 -080069void ApManagerClient::OnManagerAdded(ManagerProxyInterface* manager_proxy) {
Vitaly Buka3ef4fff2015-07-31 01:12:07 -070070 VLOG(1) << "manager added: " << manager_proxy->GetObjectPath().value();
71 manager_proxy_ = manager_proxy;
72
73 if (service_path_.IsValid())
74 return;
75
Alex Vakulenko41705852015-10-13 10:12:06 -070076 brillo::ErrorPtr error;
Vitaly Buka3ef4fff2015-07-31 01:12:07 -070077 if (!manager_proxy_->CreateService(&service_path_, &error)) {
78 LOG(ERROR) << "CreateService failed: " << error->GetMessage();
79 }
80}
81
Alex Vakulenkode94eb52015-12-08 17:11:20 -080082void ApManagerClient::OnServiceAdded(ServiceProxyInterface* service_proxy) {
Vitaly Buka3ef4fff2015-07-31 01:12:07 -070083 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
Alex Vakulenkode94eb52015-12-08 17:11:20 -080090 ConfigProxyInterface* config_proxy =
Vitaly Buka3ef4fff2015-07-31 01:12:07 -070091 object_manager_proxy_->GetConfigProxy(service_proxy->config());
Alex Vakulenkode94eb52015-12-08 17:11:20 -080092 config_proxy->set_ssid(ssid_, base::Bind(&ApManagerClient::OnSsidSet,
93 weak_ptr_factory_.GetWeakPtr()));
Vitaly Buka3ef4fff2015-07-31 01:12:07 -070094}
95
96void 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
Alex Vakulenko41705852015-10-13 10:12:06 -0700103 brillo::ErrorPtr error;
Vitaly Buka3ef4fff2015-07-31 01:12:07 -0700104 if (!service_proxy_->Start(&error)) {
105 LOG(ERROR) << "Service start failed: " << error->GetMessage();
106 }
107}
108
109void 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
117void 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