blob: 36c50af6feca44726a0eeb360fba284127dd73c4 [file] [log] [blame]
Paul Stewart20088d82012-02-16 06:58:55 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartfdd16072011-09-16 12:41:35 -07002// 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 Stewart20088d82012-02-16 06:58:55 -08007#include <set>
Paul Stewartfdd16072011-09-16 12:41:35 -07008#include <string>
Paul Stewart1b253142012-01-26 14:05:52 -08009#include <vector>
Paul Stewartfdd16072011-09-16 12:41:35 -070010
Paul Stewart20088d82012-02-16 06:58:55 -080011#include <base/stl_util-inl.h>
Paul Stewart1b253142012-01-26 14:05:52 -080012#include <base/string_split.h>
Paul Stewartfdd16072011-09-16 12:41:35 -070013#include <chromeos/dbus/service_constants.h>
14
Paul Stewart20088d82012-02-16 06:58:55 -080015#include "shill/error.h"
16
Paul Stewartfdd16072011-09-16 12:41:35 -070017namespace shill {
18
Paul Stewart20088d82012-02-16 06:58:55 -080019using std::set;
Paul Stewartfdd16072011-09-16 12:41:35 -070020using std::string;
Paul Stewart1b253142012-01-26 14:05:52 -080021using std::vector;
Paul Stewartfdd16072011-09-16 12:41:35 -070022
23const char Technology::kUnknownName[] = "Unknown";
24
Gaurav Shah435de2c2011-11-17 19:01:07 -080025// static
Paul Stewart1b253142012-01-26 14:05:52 -080026Technology::Identifier Technology::IdentifierFromName(const string &name) {
Paul Stewartfdd16072011-09-16 12:41:35 -070027 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 Petkov33af05c2012-02-28 10:10:30 +010033 } else if (name == flimflam::kTypeVPN) {
34 return kVPN;
Paul Stewartfdd16072011-09-16 12:41:35 -070035 } else {
36 return kUnknown;
37 }
38}
39
Gaurav Shah435de2c2011-11-17 19:01:07 -080040// static
Paul Stewart1b253142012-01-26 14:05:52 -080041string Technology::NameFromIdentifier(Technology::Identifier id) {
Paul Stewartfdd16072011-09-16 12:41:35 -070042 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 Petkov33af05c2012-02-28 10:10:30 +010048 } else if (id == kVPN) {
49 return flimflam::kTypeVPN;
Paul Stewartfdd16072011-09-16 12:41:35 -070050 } else {
51 return kUnknownName;
52 }
53}
54
Paul Stewart1b253142012-01-26 14:05:52 -080055// static
56Technology::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 Stewart20088d82012-02-16 06:58:55 -080066// static
67bool 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 Stewartfdd16072011-09-16 12:41:35 -070098} // namespace shill