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