blob: 300c2583e55d685e7ba084fa6426990c77e681ef [file] [log] [blame]
mukesh agrawalb54601c2011-06-07 17:39:22 -07001// 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 Masone092df3e2011-08-22 09:41:39 -07005#include "shill/wifi_endpoint.h"
6
7#include <base/logging.h>
mukesh agrawalb54601c2011-06-07 17:39:22 -07008#include <base/stringprintf.h>
9#include <base/string_number_conversions.h>
Chris Masone092df3e2011-08-22 09:41:39 -070010#include <chromeos/dbus/service_constants.h>
mukesh agrawalb54601c2011-06-07 17:39:22 -070011
12using std::string;
13
14namespace shill {
15
16const char WiFiEndpoint::kSupplicantPropertySSID[] = "SSID";
17const char WiFiEndpoint::kSupplicantPropertyBSSID[] = "BSSID";
18const char WiFiEndpoint::kSupplicantPropertySignal[] = "Signal";
19const char WiFiEndpoint::kSupplicantPropertyMode[] = "Mode";
20
21const char WiFiEndpoint::kSupplicantNetworkModeInfrastructure[] =
22 "infrastructure";
23const char WiFiEndpoint::kSupplicantNetworkModeAdHoc[] = "ad-hoc";
24const char WiFiEndpoint::kSupplicantNetworkModeAccessPoint[] = "ap";
25
26const uint32_t WiFiEndpoint::kSupplicantNetworkModeInfrastructureInt = 0;
27const uint32_t WiFiEndpoint::kSupplicantNetworkModeAdHocInt = 1;
28const uint32_t WiFiEndpoint::kSupplicantNetworkModeAccessPointInt = 2;
29
30WiFiEndpoint::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 Masone092df3e2011-08-22 09:41:39 -070041 network_mode_ = ParseMode(
mukesh agrawalb54601c2011-06-07 17:39:22 -070042 properties.find(kSupplicantPropertyMode)->second);
43
Chris Masone092df3e2011-08-22 09:41:39 -070044 if (network_mode_.empty()) {
mukesh agrawalb54601c2011-06-07 17:39:22 -070045 // 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
56WiFiEndpoint::~WiFiEndpoint() {}
57
Chris Masone092df3e2011-08-22 09:41:39 -070058// static
59uint32_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 agrawalb54601c2011-06-07 17:39:22 -070070const std::vector<uint8_t> &WiFiEndpoint::ssid() const {
71 return ssid_;
72}
73
74const string &WiFiEndpoint::ssid_string() const {
75 return ssid_string_;
76}
77
78const string &WiFiEndpoint::ssid_hex() const {
79 return ssid_hex_;
80}
81
82const string &WiFiEndpoint::bssid_string() const {
83 return bssid_string_;
84}
85
86const string &WiFiEndpoint::bssid_hex() const {
87 return bssid_hex_;
88}
89
90int16_t WiFiEndpoint::signal_strength() const {
91 return signal_strength_;
92}
93
Chris Masone092df3e2011-08-22 09:41:39 -070094const string &WiFiEndpoint::network_mode() const {
mukesh agrawalf60e4062011-05-27 13:13:41 -070095 return network_mode_;
mukesh agrawalb54601c2011-06-07 17:39:22 -070096}
97
Chris Masone092df3e2011-08-22 09:41:39 -070098// static
99const char *WiFiEndpoint::ParseMode(const std::string &mode_string) {
mukesh agrawalb54601c2011-06-07 17:39:22 -0700100 if (mode_string == kSupplicantNetworkModeInfrastructure) {
Chris Masone092df3e2011-08-22 09:41:39 -0700101 return flimflam::kModeManaged;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700102 } else if (mode_string == kSupplicantNetworkModeAdHoc) {
Chris Masone092df3e2011-08-22 09:41:39 -0700103 return flimflam::kModeAdhoc;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700104 } else if (mode_string == kSupplicantNetworkModeAccessPoint) {
Chris Masone092df3e2011-08-22 09:41:39 -0700105 NOTREACHED() << "Shill does not support AP mode at this time.";
106 return NULL;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700107 } else {
Chris Masone092df3e2011-08-22 09:41:39 -0700108 NOTREACHED() << "Unknown WiFi endpoint mode!";
109 return NULL;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700110 }
111}
112
113} // namespace shill