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