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