technology.cc revision aab63499fccbd5b94fcfd2fa88e5fda83a8b5da8
1// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
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
7#include <set>
8#include <string>
9#include <vector>
10
11#include <base/stl_util.h>
12#include <base/strings/string_split.h>
13#include <chromeos/dbus/service_constants.h>
14
15#include "shill/error.h"
16#include "shill/logging.h"
17
18namespace shill {
19
20using std::set;
21using std::string;
22using std::vector;
23
24const char Technology::kLoopbackName[] = "loopback";
25const char Technology::kTunnelName[] = "tunnel";
26const char Technology::kPPPName[] = "ppp";
27const char Technology::kUnknownName[] = "unknown";
28
29// static
30Technology::Identifier Technology::IdentifierFromName(const string &name) {
31  if (name == kTypeEthernet) {
32    return kEthernet;
33  } else if (name == kTypeEthernetEap) {
34    return kEthernetEap;
35  } else if (name == kTypeWifi) {
36    return kWifi;
37  } else if (name == kTypeWimax) {
38    return kWiMax;
39  } else if (name == kTypeCellular) {
40    return kCellular;
41  } else if (name == kTypeVPN) {
42    return kVPN;
43  } else if (name == kTypePPPoE) {
44    return kPPPoE;
45  } else if (name == kLoopbackName) {
46    return kLoopback;
47  } else if (name == kTunnelName) {
48    return kTunnel;
49  } else if (name == kPPPName) {
50    return kPPP;
51  } else {
52    return kUnknown;
53  }
54}
55
56// static
57string Technology::NameFromIdentifier(Technology::Identifier id) {
58  if (id == kEthernet) {
59    return kTypeEthernet;
60  } else if (id == kEthernetEap) {
61    return kTypeEthernetEap;
62  } else if (id == kWifi) {
63    return kTypeWifi;
64  } else if (id == kWiMax) {
65    return kTypeWimax;
66  } else if (id == kCellular) {
67    return kTypeCellular;
68  } else if (id == kVPN) {
69    return kTypeVPN;
70  } else if (id == kLoopback) {
71    return kLoopbackName;
72  } else if (id == kTunnel) {
73    return kTunnelName;
74  } else if (id == kPPP) {
75    return kPPPName;
76  } else if (id == kPPPoE) {
77    return kTypePPPoE;
78  } else {
79    return kUnknownName;
80  }
81}
82
83// static
84Technology::Identifier Technology::IdentifierFromStorageGroup(
85    const string &group) {
86  vector<string> group_parts;
87  base::SplitString(group, '_', &group_parts);
88  if (group_parts.empty()) {
89    return kUnknown;
90  }
91  return IdentifierFromName(group_parts[0]);
92}
93
94// static
95bool Technology::GetTechnologyVectorFromString(
96    const string &technologies_string,
97    vector<Identifier> *technologies_vector,
98    Error *error) {
99  CHECK(technologies_vector);
100  CHECK(error);
101
102  vector<string> technology_parts;
103  set<Technology::Identifier> seen;
104  technologies_vector->clear();
105
106  // Check if |technologies_string| is empty as some versions of
107  // base::SplitString return a vector with one empty string when given an
108  // empty string.
109  if (!technologies_string.empty())
110    base::SplitString(technologies_string, ',', &technology_parts);
111
112  for (const auto &name : technology_parts) {
113    Technology::Identifier identifier = Technology::IdentifierFromName(name);
114
115    if (identifier == Technology::kUnknown) {
116      Error::PopulateAndLog(FROM_HERE, error, Error::kInvalidArguments,
117                            name + " is an unknown technology name");
118      return false;
119    }
120
121    if (ContainsKey(seen, identifier)) {
122      Error::PopulateAndLog(FROM_HERE, error, Error::kInvalidArguments,
123                            name + " is duplicated in the list");
124      return false;
125    }
126    seen.insert(identifier);
127    technologies_vector->push_back(identifier);
128  }
129
130  return true;
131}
132
133// static
134bool Technology::IsPrimaryConnectivityTechnology(Identifier technology) {
135  return (technology == kCellular ||
136          technology == kEthernet ||
137          technology == kWifi ||
138          technology == kWiMax ||
139          technology == kPPPoE);
140}
141
142}  // namespace shill
143