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