blob: aa71d6da5823d04dc179df499551f03f1afeecdd [file] [log] [blame]
Ben Chan99c8a4d2012-05-01 08:11:53 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/wimax_service.h"
6
Darin Petkov9893d9c2012-05-17 15:27:31 -07007#include <algorithm>
8
Darin Petkovd1cd7972012-05-22 15:26:15 +02009#include <base/string_number_conversions.h>
Darin Petkove4b27022012-05-16 13:28:50 +020010#include <base/string_util.h>
Darin Petkov9893d9c2012-05-17 15:27:31 -070011#include <base/stringprintf.h>
Darin Petkove4b27022012-05-16 13:28:50 +020012#include <chromeos/dbus/service_constants.h>
13
Darin Petkov25665aa2012-05-21 14:08:12 +020014#include "shill/key_value_store.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070015#include "shill/logging.h"
Darin Petkovc63dcf02012-05-24 11:51:43 +020016#include "shill/manager.h"
Darin Petkovd1cd7972012-05-22 15:26:15 +020017#include "shill/store_interface.h"
Ben Chan99c8a4d2012-05-01 08:11:53 -070018#include "shill/technology.h"
19#include "shill/wimax.h"
20
Darin Petkov9893d9c2012-05-17 15:27:31 -070021using std::replace_if;
Ben Chanc07362b2012-05-12 10:54:11 -070022using std::string;
23
Ben Chan99c8a4d2012-05-01 08:11:53 -070024namespace shill {
25
Darin Petkovd1cd7972012-05-22 15:26:15 +020026const char WiMaxService::kStorageNetworkId[] = "NetworkId";
27const char WiMaxService::kNetworkIdProperty[] = "NetworkId";
Darin Petkov6b9b2e12012-07-10 15:51:42 +020028
Darin Petkovd1cd7972012-05-22 15:26:15 +020029
Ben Chan99c8a4d2012-05-01 08:11:53 -070030WiMaxService::WiMaxService(ControlInterface *control,
31 EventDispatcher *dispatcher,
32 Metrics *metrics,
Darin Petkovc63dcf02012-05-24 11:51:43 +020033 Manager *manager)
Ben Chan99c8a4d2012-05-01 08:11:53 -070034 : Service(control, dispatcher, metrics, manager, Technology::kWiMax),
Darin Petkovc1e52732012-05-25 15:23:45 +020035 need_passphrase_(true),
36 is_default_(false) {
Ben Chan4e5c1312012-05-18 18:45:38 -070037 PropertyStore *store = this->mutable_store();
38 // TODO(benchan): Support networks that require no user credentials or
39 // implicitly defined credentials.
40 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
Darin Petkovd1cd7972012-05-22 15:26:15 +020041 store->RegisterConstString(kNetworkIdProperty, &network_id_);
42
43 IgnoreParameterForConfigure(kNetworkIdProperty);
Darin Petkovc1e52732012-05-25 15:23:45 +020044
45 // Initialize a default storage identifier based on the service's unique
46 // name. The identifier most likely needs to be reinitialized by the caller
47 // when its components have been set.
48 InitStorageIdentifier();
Ben Chan4e5c1312012-05-18 18:45:38 -070049}
Ben Chan99c8a4d2012-05-01 08:11:53 -070050
51WiMaxService::~WiMaxService() {}
52
Darin Petkov25665aa2012-05-21 14:08:12 +020053void WiMaxService::GetConnectParameters(KeyValueStore *parameters) const {
Ben Chan4e5c1312012-05-18 18:45:38 -070054 CHECK(parameters);
Darin Petkov25665aa2012-05-21 14:08:12 +020055 if (!eap().anonymous_identity.empty()) {
56 parameters->SetString(wimax_manager::kEAPAnonymousIdentity,
57 eap().anonymous_identity);
58 }
59 if (!eap().identity.empty()) {
60 parameters->SetString(wimax_manager::kEAPUserIdentity, eap().identity);
61 }
62 if (!eap().password.empty()) {
63 parameters->SetString(wimax_manager::kEAPUserPassword, eap().password);
64 }
Ben Chan4e5c1312012-05-18 18:45:38 -070065}
66
Darin Petkov1e52a1b2012-05-21 10:35:56 +020067RpcIdentifier WiMaxService::GetNetworkObjectPath() const {
Ben Chan4e5c1312012-05-18 18:45:38 -070068 CHECK(proxy_.get());
Darin Petkov1e52a1b2012-05-21 10:35:56 +020069 return proxy_->path();
Ben Chan4e5c1312012-05-18 18:45:38 -070070}
71
Darin Petkovd1cd7972012-05-22 15:26:15 +020072void WiMaxService::Stop() {
Darin Petkovc63dcf02012-05-24 11:51:43 +020073 if (!IsStarted()) {
74 return;
75 }
76 LOG(INFO) << "Stopping WiMAX service: " << GetStorageIdentifier();
Darin Petkovd1cd7972012-05-22 15:26:15 +020077 proxy_.reset();
78 SetStrength(0);
Darin Petkovc63dcf02012-05-24 11:51:43 +020079 if (device_) {
80 device_->OnServiceStopped(this);
81 device_ = NULL;
82 }
Darin Petkovc1e52732012-05-25 15:23:45 +020083 UpdateConnectable();
Darin Petkovd1cd7972012-05-22 15:26:15 +020084}
85
Darin Petkov9893d9c2012-05-17 15:27:31 -070086bool WiMaxService::Start(WiMaxNetworkProxyInterface *proxy) {
87 SLOG(WiMax, 2) << __func__;
Darin Petkov1e52a1b2012-05-21 10:35:56 +020088 CHECK(proxy);
Darin Petkovd1cd7972012-05-22 15:26:15 +020089 scoped_ptr<WiMaxNetworkProxyInterface> local_proxy(proxy);
90 if (IsStarted()) {
91 return true;
92 }
93 if (friendly_name().empty()) {
94 LOG(ERROR) << "Empty service name.";
95 return false;
96 }
Darin Petkov9893d9c2012-05-17 15:27:31 -070097 Error error;
Darin Petkovd1cd7972012-05-22 15:26:15 +020098 network_name_ = proxy->Name(&error);
99 if (error.IsFailure()) {
Darin Petkov9893d9c2012-05-17 15:27:31 -0700100 return false;
101 }
Darin Petkovd1cd7972012-05-22 15:26:15 +0200102 uint32 identifier = proxy->Identifier(&error);
103 if (error.IsFailure()) {
Darin Petkov9893d9c2012-05-17 15:27:31 -0700104 return false;
105 }
Darin Petkovd1cd7972012-05-22 15:26:15 +0200106 WiMaxNetworkId id = ConvertIdentifierToNetworkId(identifier);
107 if (id != network_id_) {
108 LOG(ERROR) << "Network identifiers don't match: "
109 << id << " != " << network_id_;
110 return false;
111 }
112 int signal_strength = proxy->SignalStrength(&error);
113 if (error.IsFailure()) {
Ben Chanac6e8362012-05-20 00:39:58 -0700114 return false;
115 }
116 SetStrength(signal_strength);
Darin Petkovd1cd7972012-05-22 15:26:15 +0200117 proxy->set_signal_strength_changed_callback(
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200118 Bind(&WiMaxService::OnSignalStrengthChanged, Unretained(this)));
Darin Petkovd1cd7972012-05-22 15:26:15 +0200119 proxy_.reset(local_proxy.release());
Darin Petkova3f9f772012-05-31 12:11:28 +0200120 UpdateConnectable();
Darin Petkovc63dcf02012-05-24 11:51:43 +0200121 LOG(INFO) << "WiMAX service started: " << GetStorageIdentifier();
Darin Petkov9893d9c2012-05-17 15:27:31 -0700122 return true;
123}
124
Darin Petkovd1cd7972012-05-22 15:26:15 +0200125bool WiMaxService::IsStarted() const {
126 return proxy_.get();
127}
128
Ben Chan99c8a4d2012-05-01 08:11:53 -0700129void WiMaxService::Connect(Error *error) {
Darin Petkovc63dcf02012-05-24 11:51:43 +0200130 if (device_) {
Ben Chanb5310a12012-06-15 13:53:29 -0700131 // TODO(benchan): Populate error again after changing the way that
132 // Chrome handles Error::kAlreadyConnected situation.
133 LOG(WARNING) << "Service " << GetStorageIdentifier()
134 << " is already being connected or already connected.";
Darin Petkovc63dcf02012-05-24 11:51:43 +0200135 return;
136 }
Darin Petkova3f9f772012-05-31 12:11:28 +0200137 if (!connectable()) {
138 LOG(ERROR) << "Can't connect. Service " << GetStorageIdentifier()
139 << " is not connectable.";
140 Error::PopulateAndLog(error, Error::kOperationFailed,
141 Error::GetDefaultMessage(Error::kOperationFailed));
142 return;
143 }
Darin Petkov8ea0eaf2012-05-29 11:21:33 +0200144 WiMaxRefPtr carrier = manager()->wimax_provider()->SelectCarrier(this);
145 if (!carrier) {
Darin Petkovc63dcf02012-05-24 11:51:43 +0200146 Error::PopulateAndLog(
147 error, Error::kNoCarrier, "No suitable WiMAX device available.");
148 return;
149 }
Ben Chan99c8a4d2012-05-01 08:11:53 -0700150 Service::Connect(error);
Darin Petkov8ea0eaf2012-05-29 11:21:33 +0200151 carrier->ConnectTo(this, error);
152 if (error->IsSuccess()) {
153 // Associate with the carrier device if the connection process has been
154 // initiated successfully.
155 device_ = carrier;
156 }
Ben Chan99c8a4d2012-05-01 08:11:53 -0700157}
158
159void WiMaxService::Disconnect(Error *error) {
Darin Petkovc63dcf02012-05-24 11:51:43 +0200160 if (!device_) {
161 Error::PopulateAndLog(
162 error, Error::kNotConnected, "Not connected.");
163 return;
164 }
Ben Chan99c8a4d2012-05-01 08:11:53 -0700165 Service::Disconnect(error);
Darin Petkovc63dcf02012-05-24 11:51:43 +0200166 device_->DisconnectFrom(this, error);
167 device_ = NULL;
Ben Chan1411a042012-06-05 18:56:20 -0700168 // Set |need_passphrase_| to true so that after users explicitly disconnect
169 // from the network, the UI will prompt for credentials when they try to
170 // re-connect to the same network. This works around the fact that there is
171 // currently no mechanism for changing credentials for WiMAX connections.
172 // TODO(benchan,petkov): Find a better way to allow users to change the
173 // EAP credentials.
174 need_passphrase_ = true;
Ben Chancdbfc9f2012-06-06 13:05:11 -0700175 UpdateConnectable();
Ben Chan99c8a4d2012-05-01 08:11:53 -0700176}
177
Ben Chanc07362b2012-05-12 10:54:11 -0700178string WiMaxService::GetStorageIdentifier() const {
Darin Petkove4b27022012-05-16 13:28:50 +0200179 return storage_id_;
Ben Chanc07362b2012-05-12 10:54:11 -0700180}
181
182string WiMaxService::GetDeviceRpcId(Error *error) {
Darin Petkovc63dcf02012-05-24 11:51:43 +0200183 if (device_) {
184 return device_->GetRpcIdentifier();
185 }
186 error->Populate(Error::kNotSupported);
187 return "/";
Ben Chanc07362b2012-05-12 10:54:11 -0700188}
189
Darin Petkov6b9b2e12012-07-10 15:51:42 +0200190bool WiMaxService::IsAutoConnectable(const char **reason) const {
191 if (!Service::IsAutoConnectable(reason)) {
192 return false;
193 }
194 WiMaxRefPtr device = manager()->wimax_provider()->SelectCarrier(this);
195 DCHECK(device);
196 if (!device->IsIdle()) {
197 *reason = kAutoConnBusy;
198 return false;
199 }
200 return true;
201}
202
Darin Petkov8021e7f2012-05-21 12:15:00 +0200203bool WiMaxService::Is8021x() const {
204 return true;
205}
206
207void WiMaxService::set_eap(const EapCredentials &eap) {
208 Service::set_eap(eap);
Darin Petkova3f9f772012-05-31 12:11:28 +0200209 need_passphrase_ = eap.identity.empty() || eap.password.empty();
Darin Petkov8021e7f2012-05-21 12:15:00 +0200210 UpdateConnectable();
211}
212
213void WiMaxService::UpdateConnectable() {
Darin Petkovb2ba39f2012-06-06 10:33:43 +0200214 SetConnectable(IsStarted() && !need_passphrase_);
Darin Petkov8021e7f2012-05-21 12:15:00 +0200215}
216
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200217void WiMaxService::OnSignalStrengthChanged(int strength) {
218 SLOG(WiMax, 2) << __func__ << "(" << strength << ")";
219 SetStrength(strength);
220}
221
Darin Petkovd1cd7972012-05-22 15:26:15 +0200222bool WiMaxService::Save(StoreInterface *storage) {
223 SLOG(WiMax, 2) << __func__;
224 if (!Service::Save(storage)) {
225 return false;
226 }
227 const string id = GetStorageIdentifier();
228 storage->SetString(id, kStorageNetworkId, network_id_);
229 return true;
230}
231
Darin Petkovc1e52732012-05-25 15:23:45 +0200232bool WiMaxService::Unload() {
233 // The base method also disconnects the service.
234 Service::Unload();
Darin Petkova3f9f772012-05-31 12:11:28 +0200235 ClearPassphrase();
Darin Petkovc1e52732012-05-25 15:23:45 +0200236 // Notify the WiMAX provider that this service has been unloaded. If the
237 // provider releases ownership of this service, it needs to be deregistered.
238 return manager()->wimax_provider()->OnServiceUnloaded(this);
239}
240
Darin Petkov8ea0eaf2012-05-29 11:21:33 +0200241void WiMaxService::SetState(ConnectState state) {
242 Service::SetState(state);
243 if (!IsConnecting() && !IsConnected()) {
244 // Disassociate from any carrier device if it's not connected anymore.
245 device_ = NULL;
246 }
247}
248
Darin Petkovd1cd7972012-05-22 15:26:15 +0200249// static
250WiMaxNetworkId WiMaxService::ConvertIdentifierToNetworkId(uint32 identifier) {
251 return base::StringPrintf("%08x", identifier);
252}
253
254void WiMaxService::InitStorageIdentifier() {
255 storage_id_ = CreateStorageIdentifier(network_id_, friendly_name());
256}
257
258// static
259string WiMaxService::CreateStorageIdentifier(const WiMaxNetworkId &id,
260 const string &name) {
261 string storage_id =
262 base::StringPrintf("%s_%s_%s",
263 flimflam::kTypeWimax, name.c_str(), id.c_str());
264 StringToLowerASCII(&storage_id);
265 replace_if(storage_id.begin(), storage_id.end(), &Service::IllegalChar, '_');
266 return storage_id;
267}
268
Darin Petkova3f9f772012-05-31 12:11:28 +0200269void WiMaxService::ClearPassphrase() {
270 EapCredentials creds = eap();
271 creds.password.clear();
272 // Updates the service credentials and connectability status.
273 set_eap(creds);
274}
275
Ben Chan99c8a4d2012-05-01 08:11:53 -0700276} // namespace shill