blob: 81cf1ad1509b4542704ed38b4f9a6879d9a49f92 [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
mukesh agrawalcbfb34e2013-04-17 19:33:25 -070014#include "shill/dbus_adaptor.h"
Paul Stewartc43cbbe2013-04-11 06:29:30 -070015#include "shill/eap_credentials.h"
Darin Petkov25665aa2012-05-21 14:08:12 +020016#include "shill/key_value_store.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070017#include "shill/logging.h"
Darin Petkovc63dcf02012-05-24 11:51:43 +020018#include "shill/manager.h"
Darin Petkovd1cd7972012-05-22 15:26:15 +020019#include "shill/store_interface.h"
Ben Chan99c8a4d2012-05-01 08:11:53 -070020#include "shill/technology.h"
21#include "shill/wimax.h"
22
Darin Petkov9893d9c2012-05-17 15:27:31 -070023using std::replace_if;
Ben Chanc07362b2012-05-12 10:54:11 -070024using std::string;
25
Ben Chan99c8a4d2012-05-01 08:11:53 -070026namespace shill {
27
Darin Petkovd1cd7972012-05-22 15:26:15 +020028const char WiMaxService::kStorageNetworkId[] = "NetworkId";
29const char WiMaxService::kNetworkIdProperty[] = "NetworkId";
Darin Petkov6b9b2e12012-07-10 15:51:42 +020030
Ben Chan99c8a4d2012-05-01 08:11:53 -070031WiMaxService::WiMaxService(ControlInterface *control,
32 EventDispatcher *dispatcher,
33 Metrics *metrics,
Darin Petkovc63dcf02012-05-24 11:51:43 +020034 Manager *manager)
Ben Chan99c8a4d2012-05-01 08:11:53 -070035 : Service(control, dispatcher, metrics, manager, Technology::kWiMax),
Darin Petkovc1e52732012-05-25 15:23:45 +020036 need_passphrase_(true),
37 is_default_(false) {
Ben Chan4e5c1312012-05-18 18:45:38 -070038 PropertyStore *store = this->mutable_store();
39 // TODO(benchan): Support networks that require no user credentials or
40 // implicitly defined credentials.
41 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
Darin Petkovd1cd7972012-05-22 15:26:15 +020042 store->RegisterConstString(kNetworkIdProperty, &network_id_);
43
Paul Stewartc43cbbe2013-04-11 06:29:30 -070044 SetEapCredentials(new EapCredentials());
45
Darin Petkovd1cd7972012-05-22 15:26:15 +020046 IgnoreParameterForConfigure(kNetworkIdProperty);
Darin Petkovc1e52732012-05-25 15:23:45 +020047
48 // Initialize a default storage identifier based on the service's unique
49 // name. The identifier most likely needs to be reinitialized by the caller
50 // when its components have been set.
51 InitStorageIdentifier();
Ben Chan4e5c1312012-05-18 18:45:38 -070052}
Ben Chan99c8a4d2012-05-01 08:11:53 -070053
54WiMaxService::~WiMaxService() {}
55
Darin Petkov25665aa2012-05-21 14:08:12 +020056void WiMaxService::GetConnectParameters(KeyValueStore *parameters) const {
Ben Chan4e5c1312012-05-18 18:45:38 -070057 CHECK(parameters);
Paul Stewartc43cbbe2013-04-11 06:29:30 -070058 eap()->PopulateWiMaxProperties(parameters);
Ben Chan4e5c1312012-05-18 18:45:38 -070059}
60
Darin Petkov1e52a1b2012-05-21 10:35:56 +020061RpcIdentifier WiMaxService::GetNetworkObjectPath() const {
Ben Chan4e5c1312012-05-18 18:45:38 -070062 CHECK(proxy_.get());
Darin Petkov1e52a1b2012-05-21 10:35:56 +020063 return proxy_->path();
Ben Chan4e5c1312012-05-18 18:45:38 -070064}
65
Darin Petkovd1cd7972012-05-22 15:26:15 +020066void WiMaxService::Stop() {
Darin Petkovc63dcf02012-05-24 11:51:43 +020067 if (!IsStarted()) {
68 return;
69 }
70 LOG(INFO) << "Stopping WiMAX service: " << GetStorageIdentifier();
Darin Petkovd1cd7972012-05-22 15:26:15 +020071 proxy_.reset();
72 SetStrength(0);
Darin Petkovc63dcf02012-05-24 11:51:43 +020073 if (device_) {
74 device_->OnServiceStopped(this);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -070075 SetDevice(NULL);
Darin Petkovc63dcf02012-05-24 11:51:43 +020076 }
Darin Petkovc1e52732012-05-25 15:23:45 +020077 UpdateConnectable();
Darin Petkovd1cd7972012-05-22 15:26:15 +020078}
79
Darin Petkov9893d9c2012-05-17 15:27:31 -070080bool WiMaxService::Start(WiMaxNetworkProxyInterface *proxy) {
81 SLOG(WiMax, 2) << __func__;
Darin Petkov1e52a1b2012-05-21 10:35:56 +020082 CHECK(proxy);
Darin Petkovd1cd7972012-05-22 15:26:15 +020083 scoped_ptr<WiMaxNetworkProxyInterface> local_proxy(proxy);
84 if (IsStarted()) {
85 return true;
86 }
87 if (friendly_name().empty()) {
88 LOG(ERROR) << "Empty service name.";
89 return false;
90 }
Darin Petkov9893d9c2012-05-17 15:27:31 -070091 Error error;
Darin Petkovd1cd7972012-05-22 15:26:15 +020092 network_name_ = proxy->Name(&error);
93 if (error.IsFailure()) {
Darin Petkov9893d9c2012-05-17 15:27:31 -070094 return false;
95 }
Darin Petkovd1cd7972012-05-22 15:26:15 +020096 uint32 identifier = proxy->Identifier(&error);
97 if (error.IsFailure()) {
Darin Petkov9893d9c2012-05-17 15:27:31 -070098 return false;
99 }
Darin Petkovd1cd7972012-05-22 15:26:15 +0200100 WiMaxNetworkId id = ConvertIdentifierToNetworkId(identifier);
101 if (id != network_id_) {
102 LOG(ERROR) << "Network identifiers don't match: "
103 << id << " != " << network_id_;
104 return false;
105 }
106 int signal_strength = proxy->SignalStrength(&error);
107 if (error.IsFailure()) {
Ben Chanac6e8362012-05-20 00:39:58 -0700108 return false;
109 }
110 SetStrength(signal_strength);
Darin Petkovd1cd7972012-05-22 15:26:15 +0200111 proxy->set_signal_strength_changed_callback(
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200112 Bind(&WiMaxService::OnSignalStrengthChanged, Unretained(this)));
Darin Petkovd1cd7972012-05-22 15:26:15 +0200113 proxy_.reset(local_proxy.release());
Darin Petkova3f9f772012-05-31 12:11:28 +0200114 UpdateConnectable();
Darin Petkovc63dcf02012-05-24 11:51:43 +0200115 LOG(INFO) << "WiMAX service started: " << GetStorageIdentifier();
Darin Petkov9893d9c2012-05-17 15:27:31 -0700116 return true;
117}
118
Darin Petkovd1cd7972012-05-22 15:26:15 +0200119bool WiMaxService::IsStarted() const {
120 return proxy_.get();
121}
122
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700123void WiMaxService::Connect(Error *error, const char *reason) {
Darin Petkovc63dcf02012-05-24 11:51:43 +0200124 if (device_) {
Ben Chanb5310a12012-06-15 13:53:29 -0700125 // TODO(benchan): Populate error again after changing the way that
126 // Chrome handles Error::kAlreadyConnected situation.
127 LOG(WARNING) << "Service " << GetStorageIdentifier()
128 << " is already being connected or already connected.";
Darin Petkovc63dcf02012-05-24 11:51:43 +0200129 return;
130 }
Darin Petkova3f9f772012-05-31 12:11:28 +0200131 if (!connectable()) {
132 LOG(ERROR) << "Can't connect. Service " << GetStorageIdentifier()
133 << " is not connectable.";
134 Error::PopulateAndLog(error, Error::kOperationFailed,
135 Error::GetDefaultMessage(Error::kOperationFailed));
136 return;
137 }
Darin Petkov8ea0eaf2012-05-29 11:21:33 +0200138 WiMaxRefPtr carrier = manager()->wimax_provider()->SelectCarrier(this);
139 if (!carrier) {
Darin Petkovc63dcf02012-05-24 11:51:43 +0200140 Error::PopulateAndLog(
141 error, Error::kNoCarrier, "No suitable WiMAX device available.");
142 return;
143 }
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700144 Service::Connect(error, reason);
Darin Petkov8ea0eaf2012-05-29 11:21:33 +0200145 carrier->ConnectTo(this, error);
146 if (error->IsSuccess()) {
147 // Associate with the carrier device if the connection process has been
148 // initiated successfully.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700149 SetDevice(carrier);
Darin Petkov8ea0eaf2012-05-29 11:21:33 +0200150 }
Ben Chan99c8a4d2012-05-01 08:11:53 -0700151}
152
153void WiMaxService::Disconnect(Error *error) {
Darin Petkovc63dcf02012-05-24 11:51:43 +0200154 if (!device_) {
155 Error::PopulateAndLog(
156 error, Error::kNotConnected, "Not connected.");
157 return;
158 }
Ben Chan99c8a4d2012-05-01 08:11:53 -0700159 Service::Disconnect(error);
Darin Petkovc63dcf02012-05-24 11:51:43 +0200160 device_->DisconnectFrom(this, error);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700161 SetDevice(NULL);
Ben Chana289f662013-05-03 15:12:01 -0700162 // Clear the EAP passphrase so that after users explicitly disconnect
Ben Chan1411a042012-06-05 18:56:20 -0700163 // from the network, the UI will prompt for credentials when they try to
164 // re-connect to the same network. This works around the fact that there is
165 // currently no mechanism for changing credentials for WiMAX connections.
166 // TODO(benchan,petkov): Find a better way to allow users to change the
167 // EAP credentials.
Ben Chana289f662013-05-03 15:12:01 -0700168 ClearPassphrase();
Ben Chan99c8a4d2012-05-01 08:11:53 -0700169}
170
Ben Chanc07362b2012-05-12 10:54:11 -0700171string WiMaxService::GetStorageIdentifier() const {
Darin Petkove4b27022012-05-16 13:28:50 +0200172 return storage_id_;
Ben Chanc07362b2012-05-12 10:54:11 -0700173}
174
175string WiMaxService::GetDeviceRpcId(Error *error) {
mukesh agrawald4dc0832013-03-25 14:38:26 -0700176 if (!device_) {
177 error->Populate(Error::kNotFound, "Not associated with a device");
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700178 return DBusAdaptor::kNullPath;
Darin Petkovc63dcf02012-05-24 11:51:43 +0200179 }
mukesh agrawald4dc0832013-03-25 14:38:26 -0700180 return device_->GetRpcIdentifier();
Ben Chanc07362b2012-05-12 10:54:11 -0700181}
182
Darin Petkov6b9b2e12012-07-10 15:51:42 +0200183bool WiMaxService::IsAutoConnectable(const char **reason) const {
184 if (!Service::IsAutoConnectable(reason)) {
185 return false;
186 }
187 WiMaxRefPtr device = manager()->wimax_provider()->SelectCarrier(this);
188 DCHECK(device);
189 if (!device->IsIdle()) {
190 *reason = kAutoConnBusy;
191 return false;
192 }
193 return true;
194}
195
Darin Petkov8021e7f2012-05-21 12:15:00 +0200196bool WiMaxService::Is8021x() const {
197 return true;
198}
199
Ben Chanf4647d42013-05-02 22:01:01 -0700200bool WiMaxService::IsVisible() const {
201 // WiMAX services should be displayed only if they are in range (i.e.
202 // a corresponding network is exposed by WiMAX manager).
203 return IsStarted();
204}
205
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700206void WiMaxService::OnEapCredentialsChanged() {
207 need_passphrase_ = !eap()->IsConnectableUsingPassphrase();
Darin Petkov8021e7f2012-05-21 12:15:00 +0200208 UpdateConnectable();
209}
210
211void WiMaxService::UpdateConnectable() {
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700212 SetConnectableFull(IsStarted() && !need_passphrase_);
Darin Petkov8021e7f2012-05-21 12:15:00 +0200213}
214
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200215void WiMaxService::OnSignalStrengthChanged(int strength) {
216 SLOG(WiMax, 2) << __func__ << "(" << strength << ")";
217 SetStrength(strength);
218}
219
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700220void WiMaxService::SetDevice(WiMaxRefPtr new_device) {
221 if (device_ == new_device)
222 return;
223 if (new_device) {
224 adaptor()->EmitRpcIdentifierChanged(flimflam::kDeviceProperty,
225 new_device->GetRpcIdentifier());
226 } else {
227 adaptor()->EmitRpcIdentifierChanged(flimflam::kDeviceProperty,
228 DBusAdaptor::kNullPath);
229 }
230 device_ = new_device;
231}
232
Darin Petkovd1cd7972012-05-22 15:26:15 +0200233bool WiMaxService::Save(StoreInterface *storage) {
234 SLOG(WiMax, 2) << __func__;
235 if (!Service::Save(storage)) {
236 return false;
237 }
238 const string id = GetStorageIdentifier();
239 storage->SetString(id, kStorageNetworkId, network_id_);
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700240
Darin Petkovd1cd7972012-05-22 15:26:15 +0200241 return true;
242}
243
Darin Petkovc1e52732012-05-25 15:23:45 +0200244bool WiMaxService::Unload() {
245 // The base method also disconnects the service.
246 Service::Unload();
Darin Petkova3f9f772012-05-31 12:11:28 +0200247 ClearPassphrase();
Darin Petkovc1e52732012-05-25 15:23:45 +0200248 // Notify the WiMAX provider that this service has been unloaded. If the
249 // provider releases ownership of this service, it needs to be deregistered.
250 return manager()->wimax_provider()->OnServiceUnloaded(this);
251}
252
Darin Petkov8ea0eaf2012-05-29 11:21:33 +0200253void WiMaxService::SetState(ConnectState state) {
254 Service::SetState(state);
255 if (!IsConnecting() && !IsConnected()) {
256 // Disassociate from any carrier device if it's not connected anymore.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700257 SetDevice(NULL);
Darin Petkov8ea0eaf2012-05-29 11:21:33 +0200258 }
259}
260
Darin Petkovd1cd7972012-05-22 15:26:15 +0200261// static
262WiMaxNetworkId WiMaxService::ConvertIdentifierToNetworkId(uint32 identifier) {
263 return base::StringPrintf("%08x", identifier);
264}
265
266void WiMaxService::InitStorageIdentifier() {
267 storage_id_ = CreateStorageIdentifier(network_id_, friendly_name());
268}
269
270// static
271string WiMaxService::CreateStorageIdentifier(const WiMaxNetworkId &id,
272 const string &name) {
273 string storage_id =
274 base::StringPrintf("%s_%s_%s",
275 flimflam::kTypeWimax, name.c_str(), id.c_str());
276 StringToLowerASCII(&storage_id);
277 replace_if(storage_id.begin(), storage_id.end(), &Service::IllegalChar, '_');
278 return storage_id;
279}
280
Darin Petkova3f9f772012-05-31 12:11:28 +0200281void WiMaxService::ClearPassphrase() {
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700282 mutable_eap()->set_password("");
283 OnEapCredentialsChanged();
Darin Petkova3f9f772012-05-31 12:11:28 +0200284}
285
Ben Chan99c8a4d2012-05-01 08:11:53 -0700286} // namespace shill