blob: 04a3d4c2426857a079fe1a14fa8172a2465f2223 [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
19using org::chromium::apmanager::ConfigProxy;
20using org::chromium::apmanager::ManagerProxy;
21using org::chromium::apmanager::ServiceProxy;
22
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
69void ApManagerClient::OnManagerAdded(ManagerProxy* 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
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
82void ApManagerClient::OnServiceAdded(ServiceProxy* 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 ConfigProxy* config_proxy =
91 object_manager_proxy_->GetConfigProxy(service_proxy->config());
92 ConfigProxy::PropertySet* properties = config_proxy->GetProperties();
93 properties->ssid.Set(ssid_, base::Bind(&ApManagerClient::OnSsidSet,
94 weak_ptr_factory_.GetWeakPtr()));
95}
96
97void ApManagerClient::OnSsidSet(bool success) {
98 if (!success || !service_proxy_) {
99 LOG(ERROR) << "Failed to set ssid.";
100 return;
101 }
102 VLOG(1) << "SSID is set: " << ssid_;
103
Alex Vakulenko41705852015-10-13 10:12:06 -0700104 brillo::ErrorPtr error;
Vitaly Buka3ef4fff2015-07-31 01:12:07 -0700105 if (!service_proxy_->Start(&error)) {
106 LOG(ERROR) << "Service start failed: " << error->GetMessage();
107 }
108}
109
110void ApManagerClient::OnServiceRemoved(const dbus::ObjectPath& object_path) {
111 VLOG(1) << "service removed: " << object_path.value();
112 if (object_path != service_path_)
113 return;
114 service_path_ = dbus::ObjectPath();
115 service_proxy_ = nullptr;
116}
117
118void ApManagerClient::OnManagerRemoved(const dbus::ObjectPath& object_path) {
119 VLOG(1) << "manager removed: " << object_path.value();
120 manager_proxy_ = nullptr;
121 Stop();
122}
123
124} // namespace buffet