technology.cc revision 08a4ffb4ecf5893eb55c523d528bf3e52c66facf
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/string_split.h>
13#include <chromeos/dbus/service_constants.h>
14
15#include "shill/error.h"
16
17namespace shill {
18
19using std::set;
20using std::string;
21using std::vector;
22
23const char Technology::kTunnelName[] = "Tunnel";
24const char Technology::kUnknownName[] = "Unknown";
25
26// static
27Technology::Identifier Technology::IdentifierFromName(const string &name) {
28  if (name == flimflam::kTypeEthernet) {
29    return kEthernet;
30  } else if (name == flimflam::kTypeWifi) {
31    return kWifi;
32  } else if (name == flimflam::kTypeCellular) {
33    return kCellular;
34  } else if (name == flimflam::kTypeVPN) {
35    return kVPN;
36  } else if (name == kTunnelName) {
37    return kTunnel;
38  } else {
39    return kUnknown;
40  }
41}
42
43// static
44string Technology::NameFromIdentifier(Technology::Identifier id) {
45  if (id == kEthernet) {
46    return flimflam::kTypeEthernet;
47  } else if (id == kWifi) {
48    return flimflam::kTypeWifi;
49  } else if (id == kCellular) {
50    return flimflam::kTypeCellular;
51  } else if (id == kVPN) {
52    return flimflam::kTypeVPN;
53  } else if (id == kTunnel) {
54    return kTunnelName;
55  } else {
56    return kUnknownName;
57  }
58}
59
60// static
61Technology::Identifier Technology::IdentifierFromStorageGroup(
62    const string &group) {
63  vector<string> group_parts;
64  base::SplitString(group, '_', &group_parts);
65  if (group_parts.empty()) {
66    return kUnknown;
67  }
68  return IdentifierFromName(group_parts[0]);
69}
70
71// static
72bool Technology::GetTechnologyVectorFromString(
73    const string &technologies_string,
74    vector<Identifier> *technologies_vector,
75    Error *error) {
76  vector<string> technology_parts;
77  base::SplitString(technologies_string, ',', &technology_parts);
78  set<Technology::Identifier> seen;
79
80  for (vector<string>::iterator it = technology_parts.begin();
81       it != technology_parts.end();
82       ++it) {
83    Technology::Identifier identifier = Technology::IdentifierFromName(*it);
84
85    if (identifier == Technology::kUnknown) {
86      Error::PopulateAndLog(error, Error::kInvalidArguments,
87                            *it + " is an unknown technology name");
88      return false;
89    }
90
91    if (ContainsKey(seen, identifier)) {
92      Error::PopulateAndLog(error, Error::kInvalidArguments,
93                            *it + " is duplicated in the list");
94      return false;
95    }
96    seen.insert(identifier);
97    technologies_vector->push_back(identifier);
98  }
99
100  return true;
101}
102
103}  // namespace shill
104