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" |
| 16 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 17 | namespace shill { |
| 18 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 19 | using std::set; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 20 | using std::string; |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 21 | using std::vector; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 22 | |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 23 | const char Technology::kTunnelName[] = "Tunnel"; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 24 | const char Technology::kUnknownName[] = "Unknown"; |
| 25 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 26 | // static |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 27 | Technology::Identifier Technology::IdentifierFromName(const string &name) { |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 28 | if (name == flimflam::kTypeEthernet) { |
| 29 | return kEthernet; |
| 30 | } else if (name == flimflam::kTypeWifi) { |
| 31 | return kWifi; |
| 32 | } else if (name == flimflam::kTypeCellular) { |
| 33 | return kCellular; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 34 | } else if (name == flimflam::kTypeVPN) { |
| 35 | return kVPN; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 36 | } else if (name == kTunnelName) { |
| 37 | return kTunnel; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 38 | } else { |
| 39 | return kUnknown; |
| 40 | } |
| 41 | } |
| 42 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 43 | // static |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 44 | string Technology::NameFromIdentifier(Technology::Identifier id) { |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 45 | if (id == kEthernet) { |
| 46 | return flimflam::kTypeEthernet; |
| 47 | } else if (id == kWifi) { |
| 48 | return flimflam::kTypeWifi; |
| 49 | } else if (id == kCellular) { |
| 50 | return flimflam::kTypeCellular; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 51 | } else if (id == kVPN) { |
| 52 | return flimflam::kTypeVPN; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 53 | } else if (id == kTunnel) { |
| 54 | return kTunnelName; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 55 | } else { |
| 56 | return kUnknownName; |
| 57 | } |
| 58 | } |
| 59 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 60 | // static |
| 61 | Technology::Identifier Technology::IdentifierFromStorageGroup( |
| 62 | const string &group) { |
| 63 | vector<string> group_parts; |
| 64 | base::SplitString(group, '_', &group_parts); |
| 65 | if (group_parts.empty()) { |
| 66 | return kUnknown; |
| 67 | } |
| 68 | return IdentifierFromName(group_parts[0]); |
| 69 | } |
| 70 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 71 | // static |
| 72 | bool Technology::GetTechnologyVectorFromString( |
| 73 | const string &technologies_string, |
| 74 | vector<Identifier> *technologies_vector, |
| 75 | Error *error) { |
| 76 | vector<string> technology_parts; |
| 77 | base::SplitString(technologies_string, ',', &technology_parts); |
| 78 | set<Technology::Identifier> seen; |
| 79 | |
| 80 | for (vector<string>::iterator it = technology_parts.begin(); |
| 81 | it != technology_parts.end(); |
| 82 | ++it) { |
| 83 | Technology::Identifier identifier = Technology::IdentifierFromName(*it); |
| 84 | |
| 85 | if (identifier == Technology::kUnknown) { |
| 86 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 87 | *it + " is an unknown technology name"); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | if (ContainsKey(seen, identifier)) { |
| 92 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 93 | *it + " is duplicated in the list"); |
| 94 | return false; |
| 95 | } |
| 96 | seen.insert(identifier); |
| 97 | technologies_vector->push_back(identifier); |
| 98 | } |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 103 | } // namespace shill |