mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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/wifi_service.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <base/logging.h> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 10 | #include <base/stringprintf.h> |
| 11 | #include <base/string_number_conversions.h> |
| 12 | #include <base/string_util.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 13 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 14 | #include <dbus/dbus.h> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 15 | |
| 16 | #include "shill/control_interface.h" |
| 17 | #include "shill/device.h" |
| 18 | #include "shill/shill_event.h" |
| 19 | #include "shill/wifi.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 20 | #include "shill/wifi_endpoint.h" |
| 21 | #include "shill/wpa_supplicant.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 22 | |
| 23 | using std::string; |
| 24 | |
| 25 | namespace shill { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 26 | |
| 27 | WiFiService::WiFiService(ControlInterface *control_interface, |
| 28 | EventDispatcher *dispatcher, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 29 | Manager *manager, |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 30 | const WiFiRefPtr &device, |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 31 | const std::vector<uint8_t> ssid, |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 32 | const std::string &mode, |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 33 | const std::string &security) |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 34 | : Service(control_interface, dispatcher, manager, flimflam::kTypeWifi), |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 35 | security_(security), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 36 | mode_(mode), |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 37 | task_factory_(this), |
| 38 | wifi_(device), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 39 | ssid_(ssid) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 40 | PropertyStore *store = this->mutable_store(); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 41 | store->RegisterConstString(flimflam::kModeProperty, &mode_); |
| 42 | store->RegisterString(flimflam::kPassphraseProperty, &passphrase_); |
| 43 | store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_); |
| 44 | store->RegisterConstString(flimflam::kSecurityProperty, &security_); |
| 45 | store->RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 46 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 47 | store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_); |
| 48 | store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_); |
| 49 | store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_); |
| 50 | store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 51 | store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_); |
| 52 | |
| 53 | hex_ssid_ = base::HexEncode(&(*ssid_.begin()), ssid_.size()); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 54 | set_name(name() + |
| 55 | "_" + |
| 56 | string(reinterpret_cast<const char*>(ssid_.data()), ssid_.size())); |
| 57 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 58 | // TODO(quiche): determine if it is okay to set EAP.KeyManagement for |
| 59 | // a service that is not 802.1x. |
| 60 | if (security_ == flimflam::kSecurity8021x) { |
| 61 | NOTIMPLEMENTED(); |
| 62 | // XXX needs_passpharse_ = false ? |
| 63 | } else if (security_ == flimflam::kSecurityPsk) { |
| 64 | SetEAPKeyManagement("WPA-PSK"); |
| 65 | need_passphrase_ = true; |
| 66 | } else if (security_ == flimflam::kSecurityRsn) { |
| 67 | SetEAPKeyManagement("WPA-PSK"); |
| 68 | need_passphrase_ = true; |
| 69 | } else if (security_ == flimflam::kSecurityWpa) { |
| 70 | SetEAPKeyManagement("WPA-PSK"); |
| 71 | need_passphrase_ = true; |
| 72 | } else if (security_ == flimflam::kSecurityWep) { |
| 73 | SetEAPKeyManagement("NONE"); |
| 74 | need_passphrase_ = true; |
| 75 | } else if (security_ == flimflam::kSecurityNone) { |
| 76 | SetEAPKeyManagement("NONE"); |
| 77 | need_passphrase_ = false; |
| 78 | } else { |
| 79 | LOG(ERROR) << "unsupported security method " << security_; |
| 80 | } |
| 81 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 82 | // TODO(quiche): figure out when to set true |
| 83 | hidden_ssid_ = false; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | WiFiService::~WiFiService() { |
| 87 | LOG(INFO) << __func__; |
| 88 | } |
| 89 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 90 | void WiFiService::Connect(Error */*error*/) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 91 | LOG(INFO) << __func__; |
| 92 | |
| 93 | // NB(quiche) defer handling, since dbus-c++ does not permit us to |
| 94 | // send an outbound request while processing an inbound one. |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 95 | dispatcher()->PostTask( |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 96 | task_factory_.NewRunnableMethod(&WiFiService::ConnectTask)); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 99 | void WiFiService::Disconnect() { |
| 100 | // TODO(quiche) RemoveNetwork from supplicant |
| 101 | // XXX remove from favorite networks list? |
| 102 | } |
| 103 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 104 | bool WiFiService::TechnologyIs(const Technology::Identifier type) const { |
| 105 | return wifi_->TechnologyIs(type); |
| 106 | } |
| 107 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 108 | string WiFiService::GetStorageIdentifier() { |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 109 | return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s", |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 110 | flimflam::kTypeWifi, |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 111 | wifi_->address().c_str(), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 112 | hex_ssid_.c_str(), |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 113 | mode_.c_str(), |
| 114 | security_.c_str())); |
| 115 | } |
| 116 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 117 | const string &WiFiService::mode() const { |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 118 | return mode_; |
| 119 | } |
| 120 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 121 | const string &WiFiService::key_management() const { |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 122 | return GetEAPKeyManagement(); |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | const std::vector<uint8_t> &WiFiService::ssid() const { |
| 126 | return ssid_; |
| 127 | } |
| 128 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 129 | // private methods |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 130 | void WiFiService::ConnectTask() { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 131 | std::map<string, DBus::Variant> params; |
| 132 | DBus::MessageIter writer; |
| 133 | |
| 134 | params[wpa_supplicant::kNetworkPropertyMode].writer(). |
| 135 | append_uint32(WiFiEndpoint::ModeStringToUint(mode_)); |
| 136 | |
| 137 | if (security_ == flimflam::kSecurity8021x) { |
| 138 | NOTIMPLEMENTED(); |
| 139 | } else if (security_ == flimflam::kSecurityPsk) { |
| 140 | NOTIMPLEMENTED(); |
| 141 | } else if (security_ == flimflam::kSecurityRsn) { |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame^] | 142 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 143 | append_string(wpa_supplicant::kSecurityModeRSN); |
| 144 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 145 | append_string(passphrase_.c_str()); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 146 | } else if (security_ == flimflam::kSecurityWpa) { |
| 147 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 148 | append_string(wpa_supplicant::kSecurityModeWPA); |
| 149 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 150 | append_string(passphrase_.c_str()); |
| 151 | } else if (security_ == flimflam::kSecurityWep) { |
| 152 | NOTIMPLEMENTED(); |
| 153 | } else if (security_ == flimflam::kSecurityNone) { |
| 154 | // nothing special to do here |
| 155 | } else { |
| 156 | LOG(ERROR) << "can't connect. unsupported security method " << security_; |
| 157 | } |
| 158 | |
| 159 | params[wpa_supplicant::kPropertyKeyManagement].writer(). |
| 160 | append_string(key_management().c_str()); |
| 161 | // TODO(quiche): figure out why we can't use operator<< without the |
| 162 | // temporary variable. |
| 163 | writer = params[wpa_supplicant::kNetworkPropertySSID].writer(); |
| 164 | writer << ssid_; |
| 165 | |
| 166 | wifi_->ConnectTo(this, params); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 169 | string WiFiService::GetDeviceRpcId() { |
| 170 | return wifi_->GetRpcIdentifier(); |
| 171 | } |
| 172 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 173 | } // namespace shill |