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 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 5 | #include "shill/wifi_endpoint.h" |
| 6 | |
| 7 | #include <base/logging.h> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 8 | #include <base/stringprintf.h> |
| 9 | #include <base/string_number_conversions.h> |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 10 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 11 | |
| 12 | using std::string; |
| 13 | |
| 14 | namespace shill { |
| 15 | |
| 16 | const char WiFiEndpoint::kSupplicantPropertySSID[] = "SSID"; |
| 17 | const char WiFiEndpoint::kSupplicantPropertyBSSID[] = "BSSID"; |
| 18 | const char WiFiEndpoint::kSupplicantPropertySignal[] = "Signal"; |
| 19 | const char WiFiEndpoint::kSupplicantPropertyMode[] = "Mode"; |
| 20 | |
| 21 | const char WiFiEndpoint::kSupplicantNetworkModeInfrastructure[] = |
| 22 | "infrastructure"; |
| 23 | const char WiFiEndpoint::kSupplicantNetworkModeAdHoc[] = "ad-hoc"; |
| 24 | const char WiFiEndpoint::kSupplicantNetworkModeAccessPoint[] = "ap"; |
| 25 | |
| 26 | const uint32_t WiFiEndpoint::kSupplicantNetworkModeInfrastructureInt = 0; |
| 27 | const uint32_t WiFiEndpoint::kSupplicantNetworkModeAdHocInt = 1; |
| 28 | const uint32_t WiFiEndpoint::kSupplicantNetworkModeAccessPointInt = 2; |
| 29 | |
| 30 | WiFiEndpoint::WiFiEndpoint( |
| 31 | const std::map<string, ::DBus::Variant> &properties) { |
| 32 | // XXX will segfault on missing properties |
| 33 | ssid_ = |
| 34 | properties.find(kSupplicantPropertySSID)->second. |
| 35 | operator std::vector<uint8_t>(); |
| 36 | bssid_ = |
| 37 | properties.find(kSupplicantPropertyBSSID)->second. |
| 38 | operator std::vector<uint8_t>(); |
| 39 | signal_strength_ = |
| 40 | properties.find(kSupplicantPropertySignal)->second; |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 41 | network_mode_ = ParseMode( |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 42 | properties.find(kSupplicantPropertyMode)->second); |
| 43 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 44 | if (network_mode_.empty()) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 45 | // XXX log error? |
| 46 | } |
| 47 | |
| 48 | ssid_string_ = string(ssid_.begin(), ssid_.end()); |
| 49 | ssid_hex_ = base::HexEncode(&(*ssid_.begin()), ssid_.size()); |
| 50 | bssid_string_ = StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x", |
| 51 | bssid_[0], bssid_[1], bssid_[2], |
| 52 | bssid_[3], bssid_[4], bssid_[5]); |
| 53 | bssid_hex_ = base::HexEncode(&(*bssid_.begin()), bssid_.size()); |
| 54 | } |
| 55 | |
| 56 | WiFiEndpoint::~WiFiEndpoint() {} |
| 57 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 58 | // static |
| 59 | uint32_t WiFiEndpoint::ModeStringToUint(const std::string &mode_string) { |
| 60 | if (mode_string == flimflam::kModeManaged) |
| 61 | return kSupplicantNetworkModeInfrastructureInt; |
| 62 | else if (mode_string == flimflam::kModeAdhoc) |
| 63 | return kSupplicantNetworkModeAdHocInt; |
| 64 | else |
| 65 | NOTIMPLEMENTED() << "Shill dos not support " << mode_string |
| 66 | << " mode at this time."; |
| 67 | return 0; |
| 68 | } |
| 69 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 70 | const std::vector<uint8_t> &WiFiEndpoint::ssid() const { |
| 71 | return ssid_; |
| 72 | } |
| 73 | |
| 74 | const string &WiFiEndpoint::ssid_string() const { |
| 75 | return ssid_string_; |
| 76 | } |
| 77 | |
| 78 | const string &WiFiEndpoint::ssid_hex() const { |
| 79 | return ssid_hex_; |
| 80 | } |
| 81 | |
| 82 | const string &WiFiEndpoint::bssid_string() const { |
| 83 | return bssid_string_; |
| 84 | } |
| 85 | |
| 86 | const string &WiFiEndpoint::bssid_hex() const { |
| 87 | return bssid_hex_; |
| 88 | } |
| 89 | |
| 90 | int16_t WiFiEndpoint::signal_strength() const { |
| 91 | return signal_strength_; |
| 92 | } |
| 93 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 94 | const string &WiFiEndpoint::network_mode() const { |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 95 | return network_mode_; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 98 | // static |
| 99 | const char *WiFiEndpoint::ParseMode(const std::string &mode_string) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 100 | if (mode_string == kSupplicantNetworkModeInfrastructure) { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 101 | return flimflam::kModeManaged; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 102 | } else if (mode_string == kSupplicantNetworkModeAdHoc) { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 103 | return flimflam::kModeAdhoc; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 104 | } else if (mode_string == kSupplicantNetworkModeAccessPoint) { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 105 | NOTREACHED() << "Shill does not support AP mode at this time."; |
| 106 | return NULL; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 107 | } else { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 108 | NOTREACHED() << "Unknown WiFi endpoint mode!"; |
| 109 | return NULL; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | } // namespace shill |