blob: d383b190ff9d8f9883e2f6149937a2d1ca1a8684 [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
Eric Shienbrood3e20a232012-02-16 11:35:56 -050011#include <base/stl_util.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"
Christopher Wileyb691efd2012-08-09 13:51:51 -070016#include "shill/logging.h"
Paul Stewart20088d82012-02-16 06:58:55 -080017
Paul Stewartfdd16072011-09-16 12:41:35 -070018namespace shill {
19
Paul Stewart20088d82012-02-16 06:58:55 -080020using std::set;
Paul Stewartfdd16072011-09-16 12:41:35 -070021using std::string;
Paul Stewart1b253142012-01-26 14:05:52 -080022using std::vector;
Paul Stewartfdd16072011-09-16 12:41:35 -070023
Paul Stewartca876ee2012-04-21 08:55:58 -070024const char Technology::kLoopbackName[] = "loopback";
25const char Technology::kTunnelName[] = "tunnel";
26const char Technology::kPPPName[] = "ppp";
27const char Technology::kUnknownName[] = "unknown";
Paul Stewartfdd16072011-09-16 12:41:35 -070028
Gaurav Shah435de2c2011-11-17 19:01:07 -080029// static
Paul Stewart1b253142012-01-26 14:05:52 -080030Technology::Identifier Technology::IdentifierFromName(const string &name) {
Paul Stewartfdd16072011-09-16 12:41:35 -070031 if (name == flimflam::kTypeEthernet) {
32 return kEthernet;
Paul Stewart44dba7b2013-04-16 09:43:42 -070033 } else if (name == kTypeEthernetEap) {
34 return kEthernetEap;
Paul Stewartfdd16072011-09-16 12:41:35 -070035 } else if (name == flimflam::kTypeWifi) {
36 return kWifi;
Ben Chan46af1272012-05-01 10:34:00 -070037 } else if (name == flimflam::kTypeWimax) {
38 return kWiMax;
Paul Stewartfdd16072011-09-16 12:41:35 -070039 } else if (name == flimflam::kTypeCellular) {
40 return kCellular;
Darin Petkov33af05c2012-02-28 10:10:30 +010041 } else if (name == flimflam::kTypeVPN) {
42 return kVPN;
Paul Stewarte81eb702012-04-11 15:04:53 -070043 } else if (name == kLoopbackName) {
44 return kLoopback;
Paul Stewartcba0f7f2012-02-29 16:33:05 -080045 } else if (name == kTunnelName) {
46 return kTunnel;
Paul Stewartca876ee2012-04-21 08:55:58 -070047 } else if (name == kPPPName) {
48 return kPPP;
Paul Stewartfdd16072011-09-16 12:41:35 -070049 } else {
50 return kUnknown;
51 }
52}
53
Gaurav Shah435de2c2011-11-17 19:01:07 -080054// static
Paul Stewart1b253142012-01-26 14:05:52 -080055string Technology::NameFromIdentifier(Technology::Identifier id) {
Paul Stewartfdd16072011-09-16 12:41:35 -070056 if (id == kEthernet) {
57 return flimflam::kTypeEthernet;
Paul Stewart44dba7b2013-04-16 09:43:42 -070058 } else if (id == kEthernetEap) {
59 return kTypeEthernetEap;
Paul Stewartfdd16072011-09-16 12:41:35 -070060 } else if (id == kWifi) {
61 return flimflam::kTypeWifi;
Ben Chan46af1272012-05-01 10:34:00 -070062 } else if (id == kWiMax) {
63 return flimflam::kTypeWimax;
Paul Stewartfdd16072011-09-16 12:41:35 -070064 } else if (id == kCellular) {
65 return flimflam::kTypeCellular;
Darin Petkov33af05c2012-02-28 10:10:30 +010066 } else if (id == kVPN) {
67 return flimflam::kTypeVPN;
Paul Stewarte81eb702012-04-11 15:04:53 -070068 } else if (id == kLoopback) {
69 return kLoopbackName;
Paul Stewartcba0f7f2012-02-29 16:33:05 -080070 } else if (id == kTunnel) {
71 return kTunnelName;
Paul Stewartca876ee2012-04-21 08:55:58 -070072 } else if (id == kPPP) {
73 return kPPPName;
Paul Stewartfdd16072011-09-16 12:41:35 -070074 } else {
75 return kUnknownName;
76 }
77}
78
Paul Stewart1b253142012-01-26 14:05:52 -080079// static
80Technology::Identifier Technology::IdentifierFromStorageGroup(
81 const string &group) {
82 vector<string> group_parts;
83 base::SplitString(group, '_', &group_parts);
84 if (group_parts.empty()) {
85 return kUnknown;
86 }
87 return IdentifierFromName(group_parts[0]);
88}
89
Paul Stewart20088d82012-02-16 06:58:55 -080090// static
91bool Technology::GetTechnologyVectorFromString(
92 const string &technologies_string,
93 vector<Identifier> *technologies_vector,
94 Error *error) {
Ben Chanc12cf662012-04-05 14:47:18 -070095 CHECK(technologies_vector);
96 CHECK(error);
97
Paul Stewart20088d82012-02-16 06:58:55 -080098 vector<string> technology_parts;
Paul Stewart20088d82012-02-16 06:58:55 -080099 set<Technology::Identifier> seen;
Ben Chanc12cf662012-04-05 14:47:18 -0700100 technologies_vector->clear();
101
Paul Stewart5ad16062013-02-21 18:10:48 -0800102 // Check if |technologies_string| is empty as some versions of
103 // base::SplitString return a vector with one empty string when given an
104 // empty string.
Ben Chanc12cf662012-04-05 14:47:18 -0700105 if (!technologies_string.empty())
106 base::SplitString(technologies_string, ',', &technology_parts);
Paul Stewart20088d82012-02-16 06:58:55 -0800107
108 for (vector<string>::iterator it = technology_parts.begin();
109 it != technology_parts.end();
110 ++it) {
111 Technology::Identifier identifier = Technology::IdentifierFromName(*it);
112
113 if (identifier == Technology::kUnknown) {
114 Error::PopulateAndLog(error, Error::kInvalidArguments,
115 *it + " is an unknown technology name");
116 return false;
117 }
118
119 if (ContainsKey(seen, identifier)) {
120 Error::PopulateAndLog(error, Error::kInvalidArguments,
121 *it + " is duplicated in the list");
122 return false;
123 }
124 seen.insert(identifier);
125 technologies_vector->push_back(identifier);
126 }
127
128 return true;
129}
130
Ben Chan5086b972013-01-15 21:51:38 -0800131// static
132bool Technology::IsPrimaryConnectivityTechnology(Identifier technology) {
133 return (technology == kCellular ||
134 technology == kEthernet ||
135 technology == kWifi ||
136 technology == kWiMax);
137}
138
Paul Stewartfdd16072011-09-16 12:41:35 -0700139} // namespace shill