technology.cc revision 46af1272d6eab6e6a955266c33c8fa7de161880b
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/logging.h>
12#include <base/stl_util.h>
13#include <base/string_split.h>
14#include <chromeos/dbus/service_constants.h>
15
16#include "shill/error.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 == flimflam::kTypeEthernet) {
32    return kEthernet;
33  } else if (name == flimflam::kTypeWifi) {
34    return kWifi;
35  } else if (name == flimflam::kTypeWimax) {
36    return kWiMax;
37  } else if (name == flimflam::kTypeCellular) {
38    return kCellular;
39  } else if (name == flimflam::kTypeVPN) {
40    return kVPN;
41  } else if (name == kLoopbackName) {
42    return kLoopback;
43  } else if (name == kTunnelName) {
44    return kTunnel;
45  } else if (name == kPPPName) {
46    return kPPP;
47  } else {
48    return kUnknown;
49  }
50}
51
52// static
53string Technology::NameFromIdentifier(Technology::Identifier id) {
54  if (id == kEthernet) {
55    return flimflam::kTypeEthernet;
56  } else if (id == kWifi) {
57    return flimflam::kTypeWifi;
58  } else if (id == kWiMax) {
59    return flimflam::kTypeWimax;
60  } else if (id == kCellular) {
61    return flimflam::kTypeCellular;
62  } else if (id == kVPN) {
63    return flimflam::kTypeVPN;
64  } else if (id == kLoopback) {
65    return kLoopbackName;
66  } else if (id == kTunnel) {
67    return kTunnelName;
68  } else if (id == kPPP) {
69    return kPPPName;
70  } else {
71    return kUnknownName;
72  }
73}
74
75// static
76Technology::Identifier Technology::IdentifierFromStorageGroup(
77    const string &group) {
78  vector<string> group_parts;
79  base::SplitString(group, '_', &group_parts);
80  if (group_parts.empty()) {
81    return kUnknown;
82  }
83  return IdentifierFromName(group_parts[0]);
84}
85
86// static
87bool Technology::GetTechnologyVectorFromString(
88    const string &technologies_string,
89    vector<Identifier> *technologies_vector,
90    Error *error) {
91  CHECK(technologies_vector);
92  CHECK(error);
93
94  vector<string> technology_parts;
95  set<Technology::Identifier> seen;
96  technologies_vector->clear();
97
98  // Check if |technologies_string| is empty as base::SplitString returns
99  // a vector with one empty string when given an empty string.
100  if (!technologies_string.empty())
101    base::SplitString(technologies_string, ',', &technology_parts);
102
103  for (vector<string>::iterator it = technology_parts.begin();
104       it != technology_parts.end();
105       ++it) {
106    Technology::Identifier identifier = Technology::IdentifierFromName(*it);
107
108    if (identifier == Technology::kUnknown) {
109      Error::PopulateAndLog(error, Error::kInvalidArguments,
110                            *it + " is an unknown technology name");
111      return false;
112    }
113
114    if (ContainsKey(seen, identifier)) {
115      Error::PopulateAndLog(error, Error::kInvalidArguments,
116                            *it + " is duplicated in the list");
117      return false;
118    }
119    seen.insert(identifier);
120    technologies_vector->push_back(identifier);
121  }
122
123  return true;
124}
125
126}  // namespace shill
127