Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 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/technology.h" |
| 6 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 7 | #include <set> |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 8 | #include <string> |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 9 | #include <vector> |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 10 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 11 | #include <base/stl_util.h> |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 12 | #include <base/string_split.h> |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 13 | #include <chromeos/dbus/service_constants.h> |
| 14 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 15 | #include "shill/error.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 16 | #include "shill/logging.h" |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 17 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 18 | namespace shill { |
| 19 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 20 | using std::set; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 21 | using std::string; |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 22 | using std::vector; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 23 | |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 24 | const char Technology::kLoopbackName[] = "loopback"; |
| 25 | const char Technology::kTunnelName[] = "tunnel"; |
| 26 | const char Technology::kPPPName[] = "ppp"; |
| 27 | const char Technology::kUnknownName[] = "unknown"; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 28 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 29 | // static |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 30 | Technology::Identifier Technology::IdentifierFromName(const string &name) { |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 31 | if (name == flimflam::kTypeEthernet) { |
| 32 | return kEthernet; |
Paul Stewart | 44dba7b | 2013-04-16 09:43:42 -0700 | [diff] [blame] | 33 | } else if (name == kTypeEthernetEap) { |
| 34 | return kEthernetEap; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 35 | } else if (name == flimflam::kTypeWifi) { |
| 36 | return kWifi; |
Ben Chan | 46af127 | 2012-05-01 10:34:00 -0700 | [diff] [blame] | 37 | } else if (name == flimflam::kTypeWimax) { |
| 38 | return kWiMax; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 39 | } else if (name == flimflam::kTypeCellular) { |
| 40 | return kCellular; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 41 | } else if (name == flimflam::kTypeVPN) { |
| 42 | return kVPN; |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 43 | } else if (name == kLoopbackName) { |
| 44 | return kLoopback; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 45 | } else if (name == kTunnelName) { |
| 46 | return kTunnel; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 47 | } else if (name == kPPPName) { |
| 48 | return kPPP; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 49 | } else { |
| 50 | return kUnknown; |
| 51 | } |
| 52 | } |
| 53 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 54 | // static |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 55 | string Technology::NameFromIdentifier(Technology::Identifier id) { |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 56 | if (id == kEthernet) { |
| 57 | return flimflam::kTypeEthernet; |
Paul Stewart | 44dba7b | 2013-04-16 09:43:42 -0700 | [diff] [blame] | 58 | } else if (id == kEthernetEap) { |
| 59 | return kTypeEthernetEap; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 60 | } else if (id == kWifi) { |
| 61 | return flimflam::kTypeWifi; |
Ben Chan | 46af127 | 2012-05-01 10:34:00 -0700 | [diff] [blame] | 62 | } else if (id == kWiMax) { |
| 63 | return flimflam::kTypeWimax; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 64 | } else if (id == kCellular) { |
| 65 | return flimflam::kTypeCellular; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 66 | } else if (id == kVPN) { |
| 67 | return flimflam::kTypeVPN; |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 68 | } else if (id == kLoopback) { |
| 69 | return kLoopbackName; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 70 | } else if (id == kTunnel) { |
| 71 | return kTunnelName; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 72 | } else if (id == kPPP) { |
| 73 | return kPPPName; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 74 | } else { |
| 75 | return kUnknownName; |
| 76 | } |
| 77 | } |
| 78 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 79 | // static |
| 80 | Technology::Identifier Technology::IdentifierFromStorageGroup( |
| 81 | const string &group) { |
| 82 | vector<string> group_parts; |
| 83 | base::SplitString(group, '_', &group_parts); |
| 84 | if (group_parts.empty()) { |
| 85 | return kUnknown; |
| 86 | } |
| 87 | return IdentifierFromName(group_parts[0]); |
| 88 | } |
| 89 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 90 | // static |
| 91 | bool Technology::GetTechnologyVectorFromString( |
| 92 | const string &technologies_string, |
| 93 | vector<Identifier> *technologies_vector, |
| 94 | Error *error) { |
Ben Chan | c12cf66 | 2012-04-05 14:47:18 -0700 | [diff] [blame] | 95 | CHECK(technologies_vector); |
| 96 | CHECK(error); |
| 97 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 98 | vector<string> technology_parts; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 99 | set<Technology::Identifier> seen; |
Ben Chan | c12cf66 | 2012-04-05 14:47:18 -0700 | [diff] [blame] | 100 | technologies_vector->clear(); |
| 101 | |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 102 | // Check if |technologies_string| is empty as some versions of |
| 103 | // base::SplitString return a vector with one empty string when given an |
| 104 | // empty string. |
Ben Chan | c12cf66 | 2012-04-05 14:47:18 -0700 | [diff] [blame] | 105 | if (!technologies_string.empty()) |
| 106 | base::SplitString(technologies_string, ',', &technology_parts); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 107 | |
| 108 | for (vector<string>::iterator it = technology_parts.begin(); |
| 109 | it != technology_parts.end(); |
| 110 | ++it) { |
| 111 | Technology::Identifier identifier = Technology::IdentifierFromName(*it); |
| 112 | |
| 113 | if (identifier == Technology::kUnknown) { |
| 114 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 115 | *it + " is an unknown technology name"); |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | if (ContainsKey(seen, identifier)) { |
| 120 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 121 | *it + " is duplicated in the list"); |
| 122 | return false; |
| 123 | } |
| 124 | seen.insert(identifier); |
| 125 | technologies_vector->push_back(identifier); |
| 126 | } |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
Ben Chan | 5086b97 | 2013-01-15 21:51:38 -0800 | [diff] [blame] | 131 | // static |
| 132 | bool Technology::IsPrimaryConnectivityTechnology(Identifier technology) { |
| 133 | return (technology == kCellular || |
| 134 | technology == kEthernet || |
| 135 | technology == kWifi || |
| 136 | technology == kWiMax); |
| 137 | } |
| 138 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 139 | } // namespace shill |