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; |
| 33 | } else if (name == flimflam::kTypeWifi) { |
| 34 | return kWifi; |
Ben Chan | 46af127 | 2012-05-01 10:34:00 -0700 | [diff] [blame] | 35 | } else if (name == flimflam::kTypeWimax) { |
| 36 | return kWiMax; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 37 | } else if (name == flimflam::kTypeCellular) { |
| 38 | return kCellular; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 39 | } else if (name == flimflam::kTypeVPN) { |
| 40 | return kVPN; |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 41 | } else if (name == kLoopbackName) { |
| 42 | return kLoopback; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 43 | } else if (name == kTunnelName) { |
| 44 | return kTunnel; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 45 | } else if (name == kPPPName) { |
| 46 | return kPPP; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 47 | } else { |
| 48 | return kUnknown; |
| 49 | } |
| 50 | } |
| 51 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 52 | // static |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 53 | string Technology::NameFromIdentifier(Technology::Identifier id) { |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 54 | if (id == kEthernet) { |
| 55 | return flimflam::kTypeEthernet; |
| 56 | } else if (id == kWifi) { |
| 57 | return flimflam::kTypeWifi; |
Ben Chan | 46af127 | 2012-05-01 10:34:00 -0700 | [diff] [blame] | 58 | } else if (id == kWiMax) { |
| 59 | return flimflam::kTypeWimax; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 60 | } else if (id == kCellular) { |
| 61 | return flimflam::kTypeCellular; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 62 | } else if (id == kVPN) { |
| 63 | return flimflam::kTypeVPN; |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 64 | } else if (id == kLoopback) { |
| 65 | return kLoopbackName; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 66 | } else if (id == kTunnel) { |
| 67 | return kTunnelName; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 68 | } else if (id == kPPP) { |
| 69 | return kPPPName; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 70 | } else { |
| 71 | return kUnknownName; |
| 72 | } |
| 73 | } |
| 74 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 75 | // static |
| 76 | Technology::Identifier Technology::IdentifierFromStorageGroup( |
| 77 | const string &group) { |
| 78 | vector<string> group_parts; |
| 79 | base::SplitString(group, '_', &group_parts); |
| 80 | if (group_parts.empty()) { |
| 81 | return kUnknown; |
| 82 | } |
| 83 | return IdentifierFromName(group_parts[0]); |
| 84 | } |
| 85 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 86 | // static |
| 87 | bool Technology::GetTechnologyVectorFromString( |
| 88 | const string &technologies_string, |
| 89 | vector<Identifier> *technologies_vector, |
| 90 | Error *error) { |
Ben Chan | c12cf66 | 2012-04-05 14:47:18 -0700 | [diff] [blame] | 91 | CHECK(technologies_vector); |
| 92 | CHECK(error); |
| 93 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 94 | vector<string> technology_parts; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 95 | set<Technology::Identifier> seen; |
Ben Chan | c12cf66 | 2012-04-05 14:47:18 -0700 | [diff] [blame] | 96 | technologies_vector->clear(); |
| 97 | |
| 98 | // Check if |technologies_string| is empty as base::SplitString returns |
| 99 | // a vector with one empty string when given an empty string. |
| 100 | if (!technologies_string.empty()) |
| 101 | base::SplitString(technologies_string, ',', &technology_parts); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 102 | |
| 103 | for (vector<string>::iterator it = technology_parts.begin(); |
| 104 | it != technology_parts.end(); |
| 105 | ++it) { |
| 106 | Technology::Identifier identifier = Technology::IdentifierFromName(*it); |
| 107 | |
| 108 | if (identifier == Technology::kUnknown) { |
| 109 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 110 | *it + " is an unknown technology name"); |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | if (ContainsKey(seen, identifier)) { |
| 115 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 116 | *it + " is duplicated in the list"); |
| 117 | return false; |
| 118 | } |
| 119 | seen.insert(identifier); |
| 120 | technologies_vector->push_back(identifier); |
| 121 | } |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 126 | } // namespace shill |