blob: 3d54465aeaee292ee71a7316979e4a0b157d216d [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
Ben Chanc12cf662012-04-05 14:47:18 -070011#include <base/logging.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050012#include <base/stl_util.h>
Paul Stewart1b253142012-01-26 14:05:52 -080013#include <base/string_split.h>
Paul Stewartfdd16072011-09-16 12:41:35 -070014#include <chromeos/dbus/service_constants.h>
15
Paul Stewart20088d82012-02-16 06:58:55 -080016#include "shill/error.h"
17
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 Stewarte81eb702012-04-11 15:04:53 -070024const char Technology::kLoopbackName[] = "Loopback";
Paul Stewartcba0f7f2012-02-29 16:33:05 -080025const char Technology::kTunnelName[] = "Tunnel";
Paul Stewartfdd16072011-09-16 12:41:35 -070026const char Technology::kUnknownName[] = "Unknown";
27
Gaurav Shah435de2c2011-11-17 19:01:07 -080028// static
Paul Stewart1b253142012-01-26 14:05:52 -080029Technology::Identifier Technology::IdentifierFromName(const string &name) {
Paul Stewartfdd16072011-09-16 12:41:35 -070030 if (name == flimflam::kTypeEthernet) {
31 return kEthernet;
32 } else if (name == flimflam::kTypeWifi) {
33 return kWifi;
34 } else if (name == flimflam::kTypeCellular) {
35 return kCellular;
Darin Petkov33af05c2012-02-28 10:10:30 +010036 } else if (name == flimflam::kTypeVPN) {
37 return kVPN;
Paul Stewarte81eb702012-04-11 15:04:53 -070038 } else if (name == kLoopbackName) {
39 return kLoopback;
Paul Stewartcba0f7f2012-02-29 16:33:05 -080040 } else if (name == kTunnelName) {
41 return kTunnel;
Paul Stewartfdd16072011-09-16 12:41:35 -070042 } else {
43 return kUnknown;
44 }
45}
46
Gaurav Shah435de2c2011-11-17 19:01:07 -080047// static
Paul Stewart1b253142012-01-26 14:05:52 -080048string Technology::NameFromIdentifier(Technology::Identifier id) {
Paul Stewartfdd16072011-09-16 12:41:35 -070049 if (id == kEthernet) {
50 return flimflam::kTypeEthernet;
51 } else if (id == kWifi) {
52 return flimflam::kTypeWifi;
53 } else if (id == kCellular) {
54 return flimflam::kTypeCellular;
Darin Petkov33af05c2012-02-28 10:10:30 +010055 } else if (id == kVPN) {
56 return flimflam::kTypeVPN;
Paul Stewarte81eb702012-04-11 15:04:53 -070057 } else if (id == kLoopback) {
58 return kLoopbackName;
Paul Stewartcba0f7f2012-02-29 16:33:05 -080059 } else if (id == kTunnel) {
60 return kTunnelName;
Paul Stewartfdd16072011-09-16 12:41:35 -070061 } else {
62 return kUnknownName;
63 }
64}
65
Paul Stewart1b253142012-01-26 14:05:52 -080066// static
67Technology::Identifier Technology::IdentifierFromStorageGroup(
68 const string &group) {
69 vector<string> group_parts;
70 base::SplitString(group, '_', &group_parts);
71 if (group_parts.empty()) {
72 return kUnknown;
73 }
74 return IdentifierFromName(group_parts[0]);
75}
76
Paul Stewart20088d82012-02-16 06:58:55 -080077// static
78bool Technology::GetTechnologyVectorFromString(
79 const string &technologies_string,
80 vector<Identifier> *technologies_vector,
81 Error *error) {
Ben Chanc12cf662012-04-05 14:47:18 -070082 CHECK(technologies_vector);
83 CHECK(error);
84
Paul Stewart20088d82012-02-16 06:58:55 -080085 vector<string> technology_parts;
Paul Stewart20088d82012-02-16 06:58:55 -080086 set<Technology::Identifier> seen;
Ben Chanc12cf662012-04-05 14:47:18 -070087 technologies_vector->clear();
88
89 // Check if |technologies_string| is empty as base::SplitString returns
90 // a vector with one empty string when given an empty string.
91 if (!technologies_string.empty())
92 base::SplitString(technologies_string, ',', &technology_parts);
Paul Stewart20088d82012-02-16 06:58:55 -080093
94 for (vector<string>::iterator it = technology_parts.begin();
95 it != technology_parts.end();
96 ++it) {
97 Technology::Identifier identifier = Technology::IdentifierFromName(*it);
98
99 if (identifier == Technology::kUnknown) {
100 Error::PopulateAndLog(error, Error::kInvalidArguments,
101 *it + " is an unknown technology name");
102 return false;
103 }
104
105 if (ContainsKey(seen, identifier)) {
106 Error::PopulateAndLog(error, Error::kInvalidArguments,
107 *it + " is duplicated in the list");
108 return false;
109 }
110 seen.insert(identifier);
111 technologies_vector->push_back(identifier);
112 }
113
114 return true;
115}
116
Paul Stewartfdd16072011-09-16 12:41:35 -0700117} // namespace shill