1 // 2 // Copyright (C) 2012 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #include "shill/technology.h" 18 19 #include <set> 20 #include <string> 21 #include <vector> 22 23 #include <base/stl_util.h> 24 #include <base/strings/string_split.h> 25 #if defined(__ANDROID__) 26 #include <dbus/service_constants.h> 27 #else 28 #include <chromeos/dbus/service_constants.h> 29 #endif // __ANDROID__ 30 31 #include "shill/error.h" 32 #include "shill/logging.h" 33 34 namespace shill { 35 36 using std::set; 37 using std::string; 38 using std::vector; 39 40 const char Technology::kLoopbackName[] = "loopback"; 41 const char Technology::kTunnelName[] = "tunnel"; 42 const char Technology::kPPPName[] = "ppp"; 43 const char Technology::kUnknownName[] = "unknown"; 44 45 // static 46 Technology::Identifier Technology::IdentifierFromName(const string& name) { 47 if (name == kTypeEthernet) { 48 return kEthernet; 49 } else if (name == kTypeEthernetEap) { 50 return kEthernetEap; 51 } else if (name == kTypeWifi) { 52 return kWifi; 53 } else if (name == kTypeWimax) { 54 return kWiMax; 55 } else if (name == kTypeCellular) { 56 return kCellular; 57 } else if (name == kTypeVPN) { 58 return kVPN; 59 } else if (name == kTypePPPoE) { 60 return kPPPoE; 61 } else if (name == kLoopbackName) { 62 return kLoopback; 63 } else if (name == kTunnelName) { 64 return kTunnel; 65 } else if (name == kPPPName) { 66 return kPPP; 67 } else { 68 return kUnknown; 69 } 70 } 71 72 // static 73 string Technology::NameFromIdentifier(Technology::Identifier id) { 74 if (id == kEthernet) { 75 return kTypeEthernet; 76 } else if (id == kEthernetEap) { 77 return kTypeEthernetEap; 78 } else if (id == kWifi) { 79 return kTypeWifi; 80 } else if (id == kWiMax) { 81 return kTypeWimax; 82 } else if (id == kCellular) { 83 return kTypeCellular; 84 } else if (id == kVPN) { 85 return kTypeVPN; 86 } else if (id == kLoopback) { 87 return kLoopbackName; 88 } else if (id == kTunnel) { 89 return kTunnelName; 90 } else if (id == kPPP) { 91 return kPPPName; 92 } else if (id == kPPPoE) { 93 return kTypePPPoE; 94 } else { 95 return kUnknownName; 96 } 97 } 98 99 // static 100 Technology::Identifier Technology::IdentifierFromStorageGroup( 101 const string& group) { 102 vector<string> group_parts = base::SplitString( 103 group, "_", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 104 if (group_parts.empty()) { 105 return kUnknown; 106 } 107 return IdentifierFromName(group_parts[0]); 108 } 109 110 // static 111 bool Technology::GetTechnologyVectorFromString( 112 const string& technologies_string, 113 vector<Identifier>* technologies_vector, 114 Error* error) { 115 CHECK(technologies_vector); 116 CHECK(error); 117 118 vector<string> technology_parts; 119 set<Technology::Identifier> seen; 120 technologies_vector->clear(); 121 122 // Check if |technologies_string| is empty as some versions of 123 // base::SplitString return a vector with one empty string when given an 124 // empty string. 125 if (!technologies_string.empty()) { 126 technology_parts = base::SplitString( 127 technologies_string, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 128 } 129 130 for (const auto& name : technology_parts) { 131 Technology::Identifier identifier = Technology::IdentifierFromName(name); 132 133 if (identifier == Technology::kUnknown) { 134 Error::PopulateAndLog(FROM_HERE, error, Error::kInvalidArguments, 135 name + " is an unknown technology name"); 136 return false; 137 } 138 139 if (ContainsKey(seen, identifier)) { 140 Error::PopulateAndLog(FROM_HERE, error, Error::kInvalidArguments, 141 name + " is duplicated in the list"); 142 return false; 143 } 144 seen.insert(identifier); 145 technologies_vector->push_back(identifier); 146 } 147 148 return true; 149 } 150 151 // static 152 bool Technology::IsPrimaryConnectivityTechnology(Identifier technology) { 153 return (technology == kCellular || 154 technology == kEthernet || 155 technology == kWifi || 156 technology == kWiMax || 157 technology == kPPPoE); 158 } 159 160 } // namespace shill 161