blob: 5cef34711df2490e0dd43459d2ddbf88d4745620 [file] [log] [blame]
Paul Stewart20088d82012-02-16 06:58:55 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartfdd16072011-09-16 12:41:35 -07002// 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
Paul Stewart20088d82012-02-16 06:58:55 -08007#include <set>
Paul Stewartfdd16072011-09-16 12:41:35 -07008#include <string>
Paul Stewart1b253142012-01-26 14:05:52 -08009#include <vector>
Paul Stewartfdd16072011-09-16 12:41:35 -070010
Paul Stewart20088d82012-02-16 06:58:55 -080011#include <base/stl_util-inl.h>
Paul Stewart1b253142012-01-26 14:05:52 -080012#include <base/string_split.h>
Paul Stewartfdd16072011-09-16 12:41:35 -070013#include <chromeos/dbus/service_constants.h>
14
Paul Stewart20088d82012-02-16 06:58:55 -080015#include "shill/error.h"
16
Paul Stewartfdd16072011-09-16 12:41:35 -070017namespace shill {
18
Paul Stewart20088d82012-02-16 06:58:55 -080019using std::set;
Paul Stewartfdd16072011-09-16 12:41:35 -070020using std::string;
Paul Stewart1b253142012-01-26 14:05:52 -080021using std::vector;
Paul Stewartfdd16072011-09-16 12:41:35 -070022
23const char Technology::kUnknownName[] = "Unknown";
24
Gaurav Shah435de2c2011-11-17 19:01:07 -080025// static
Paul Stewart1b253142012-01-26 14:05:52 -080026Technology::Identifier Technology::IdentifierFromName(const string &name) {
Paul Stewartfdd16072011-09-16 12:41:35 -070027 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
Gaurav Shah435de2c2011-11-17 19:01:07 -080038// static
Paul Stewart1b253142012-01-26 14:05:52 -080039string Technology::NameFromIdentifier(Technology::Identifier id) {
Paul Stewartfdd16072011-09-16 12:41:35 -070040 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
Paul Stewart1b253142012-01-26 14:05:52 -080051// 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
Paul Stewart20088d82012-02-16 06:58:55 -080062// 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
Paul Stewartfdd16072011-09-16 12:41:35 -070094} // namespace shill