technology.cc revision 6db7b24348e69639e19cd6c408388b10d6ee54fe
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 == kLoopbackName) {
44    return kLoopback;
45  } else if (name == kTunnelName) {
46    return kTunnel;
47  } else if (name == kPPPName) {
48    return kPPP;
49  } else {
50    return kUnknown;
51  }
52}
53
54// static
55string Technology::NameFromIdentifier(Technology::Identifier id) {
56  if (id == kEthernet) {
57    return kTypeEthernet;
58  } else if (id == kEthernetEap) {
59    return kTypeEthernetEap;
60  } else if (id == kWifi) {
61    return kTypeWifi;
62  } else if (id == kWiMax) {
63    return kTypeWimax;
64  } else if (id == kCellular) {
65    return kTypeCellular;
66  } else if (id == kVPN) {
67    return kTypeVPN;
68  } else if (id == kLoopback) {
69    return kLoopbackName;
70  } else if (id == kTunnel) {
71    return kTunnelName;
72  } else if (id == kPPP) {
73    return kPPPName;
74  } else {
75    return kUnknownName;
76  }
77}
78
79// 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
90// static
91bool Technology::GetTechnologyVectorFromString(
92    const string &technologies_string,
93    vector<Identifier> *technologies_vector,
94    Error *error) {
95  CHECK(technologies_vector);
96  CHECK(error);
97
98  vector<string> technology_parts;
99  set<Technology::Identifier> seen;
100  technologies_vector->clear();
101
102  // 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.
105  if (!technologies_string.empty())
106    base::SplitString(technologies_string, ',', &technology_parts);
107
108  for (const auto &name : technology_parts) {
109    Technology::Identifier identifier = Technology::IdentifierFromName(name);
110
111    if (identifier == Technology::kUnknown) {
112      Error::PopulateAndLog(error, Error::kInvalidArguments,
113                            name + " is an unknown technology name");
114      return false;
115    }
116
117    if (ContainsKey(seen, identifier)) {
118      Error::PopulateAndLog(error, Error::kInvalidArguments,
119                            name + " is duplicated in the list");
120      return false;
121    }
122    seen.insert(identifier);
123    technologies_vector->push_back(identifier);
124  }
125
126  return true;
127}
128
129// static
130bool Technology::IsPrimaryConnectivityTechnology(Identifier technology) {
131  return (technology == kCellular ||
132          technology == kEthernet ||
133          technology == kWifi ||
134          technology == kWiMax);
135}
136
137}  // namespace shill
138