1 // Copyright 2013 The Chromium 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 "chromeos/network/network_device_handler.h" 6 7 #include "base/bind.h" 8 #include "base/values.h" 9 #include "chromeos/dbus/dbus_thread_manager.h" 10 #include "chromeos/dbus/shill_device_client.h" 11 #include "chromeos/dbus/shill_ipconfig_client.h" 12 #include "chromeos/network/device_state.h" 13 #include "chromeos/network/network_event_log.h" 14 #include "dbus/object_path.h" 15 #include "third_party/cros_system_api/dbus/service_constants.h" 16 17 namespace chromeos { 18 19 namespace { 20 21 const char kDevicePath[] = "devicePath"; 22 23 // TODO(armansito): Add bindings for these to service_constants.h 24 // (crbug.com/256889) 25 const char kShillErrorFailure[] = "org.chromium.flimflam.Error.Failure"; 26 const char kShillErrorNotSupported[] = 27 "org.chromium.flimflam.Error.NotSupported"; 28 29 std::string GetErrorNameForShillError(const std::string& shill_error_name) { 30 // TODO(armansito): Use the new SIM error names once the ones below get 31 // deprecated (crbug.com/256855) 32 if (shill_error_name == kShillErrorFailure) 33 return NetworkDeviceHandler::kErrorFailure; 34 if (shill_error_name == kShillErrorNotSupported) 35 return NetworkDeviceHandler::kErrorNotSupported; 36 if (shill_error_name == flimflam::kErrorIncorrectPinMsg) 37 return NetworkDeviceHandler::kErrorIncorrectPin; 38 if (shill_error_name == flimflam::kErrorPinBlockedMsg) 39 return NetworkDeviceHandler::kErrorPinBlocked; 40 if (shill_error_name == flimflam::kErrorPinRequiredMsg) 41 return NetworkDeviceHandler::kErrorPinRequired; 42 return NetworkDeviceHandler::kErrorUnknown; 43 } 44 45 void HandleShillCallFailure( 46 const std::string& device_path, 47 const network_handler::ErrorCallback& error_callback, 48 const std::string& shill_error_name, 49 const std::string& shill_error_message) { 50 network_handler::ShillErrorCallbackFunction( 51 GetErrorNameForShillError(shill_error_name), 52 device_path, 53 error_callback, 54 shill_error_name, 55 shill_error_message); 56 } 57 58 void IPConfigRefreshCallback(const std::string& ipconfig_path, 59 DBusMethodCallStatus call_status) { 60 if (call_status != DBUS_METHOD_CALL_SUCCESS) { 61 NET_LOG_ERROR( 62 base::StringPrintf("IPConfigs.Refresh Failed: %d", call_status), 63 ipconfig_path); 64 } else { 65 NET_LOG_EVENT("IPConfigs.Refresh Succeeded", ipconfig_path); 66 } 67 } 68 69 void RefreshIPConfigsCallback( 70 const base::Closure& callback, 71 const network_handler::ErrorCallback& error_callback, 72 const std::string& device_path, 73 const base::DictionaryValue& properties) { 74 const ListValue* ip_configs; 75 if (!properties.GetListWithoutPathExpansion( 76 flimflam::kIPConfigsProperty, &ip_configs)) { 77 NET_LOG_ERROR("RequestRefreshIPConfigs Failed", device_path); 78 network_handler::ShillErrorCallbackFunction( 79 "RequestRefreshIPConfigs Failed", 80 device_path, 81 error_callback, 82 std::string("Missing ") + flimflam::kIPConfigsProperty, ""); 83 return; 84 } 85 86 for (size_t i = 0; i < ip_configs->GetSize(); i++) { 87 std::string ipconfig_path; 88 if (!ip_configs->GetString(i, &ipconfig_path)) 89 continue; 90 DBusThreadManager::Get()->GetShillIPConfigClient()->Refresh( 91 dbus::ObjectPath(ipconfig_path), 92 base::Bind(&IPConfigRefreshCallback, ipconfig_path)); 93 } 94 // It is safe to invoke |callback| here instead of waiting for the 95 // IPConfig.Refresh callbacks to complete because the Refresh DBus calls will 96 // be executed in order and thus before any further DBus requests that 97 // |callback| may issue. 98 if (!callback.is_null()) 99 callback.Run(); 100 } 101 102 void ProposeScanCallback( 103 const std::string& device_path, 104 const base::Closure& callback, 105 const network_handler::ErrorCallback& error_callback, 106 DBusMethodCallStatus call_status) { 107 if (call_status != DBUS_METHOD_CALL_SUCCESS) { 108 NET_LOG_ERROR( 109 base::StringPrintf("Device.ProposeScan failed: %d", call_status), 110 device_path); 111 network_handler::ShillErrorCallbackFunction( 112 "Device.ProposeScan Failed", 113 device_path, 114 error_callback, 115 base::StringPrintf("DBus call failed: %d", call_status), ""); 116 return; 117 } 118 NET_LOG_EVENT("Device.ProposeScan succeeded.", device_path); 119 if (!callback.is_null()) 120 callback.Run(); 121 } 122 123 } // namespace 124 125 const char NetworkDeviceHandler::kErrorFailure[] = "failure"; 126 const char NetworkDeviceHandler::kErrorIncorrectPin[] = "incorrect-pin"; 127 const char NetworkDeviceHandler::kErrorNotSupported[] = "not-supported"; 128 const char NetworkDeviceHandler::kErrorPinBlocked[] = "pin-blocked"; 129 const char NetworkDeviceHandler::kErrorPinRequired[] = "pin-required"; 130 const char NetworkDeviceHandler::kErrorUnknown[] = "unknown"; 131 132 NetworkDeviceHandler::NetworkDeviceHandler() { 133 } 134 135 NetworkDeviceHandler::~NetworkDeviceHandler() { 136 } 137 138 void NetworkDeviceHandler::GetDeviceProperties( 139 const std::string& device_path, 140 const network_handler::DictionaryResultCallback& callback, 141 const network_handler::ErrorCallback& error_callback) const { 142 DBusThreadManager::Get()->GetShillDeviceClient()->GetProperties( 143 dbus::ObjectPath(device_path), 144 base::Bind(&network_handler::GetPropertiesCallback, 145 callback, error_callback, device_path)); 146 } 147 148 void NetworkDeviceHandler::SetDeviceProperty( 149 const std::string& device_path, 150 const std::string& name, 151 const base::Value& value, 152 const base::Closure& callback, 153 const network_handler::ErrorCallback& error_callback) { 154 DBusThreadManager::Get()->GetShillDeviceClient()->SetProperty( 155 dbus::ObjectPath(device_path), 156 name, 157 value, 158 callback, 159 base::Bind(&HandleShillCallFailure, device_path, error_callback)); 160 } 161 162 void NetworkDeviceHandler::RequestRefreshIPConfigs( 163 const std::string& device_path, 164 const base::Closure& callback, 165 const network_handler::ErrorCallback& error_callback) { 166 GetDeviceProperties(device_path, 167 base::Bind(&RefreshIPConfigsCallback, 168 callback, error_callback), 169 error_callback); 170 } 171 172 void NetworkDeviceHandler::ProposeScan( 173 const std::string& device_path, 174 const base::Closure& callback, 175 const network_handler::ErrorCallback& error_callback) { 176 DBusThreadManager::Get()->GetShillDeviceClient()->ProposeScan( 177 dbus::ObjectPath(device_path), 178 base::Bind(&ProposeScanCallback, device_path, callback, error_callback)); 179 } 180 181 void NetworkDeviceHandler::RegisterCellularNetwork( 182 const std::string& device_path, 183 const std::string& network_id, 184 const base::Closure& callback, 185 const network_handler::ErrorCallback& error_callback) { 186 DBusThreadManager::Get()->GetShillDeviceClient()->Register( 187 dbus::ObjectPath(device_path), 188 network_id, 189 callback, 190 base::Bind(&HandleShillCallFailure, device_path, error_callback)); 191 } 192 193 void NetworkDeviceHandler::SetCarrier( 194 const std::string& device_path, 195 const std::string& carrier, 196 const base::Closure& callback, 197 const network_handler::ErrorCallback& error_callback) { 198 DBusThreadManager::Get()->GetShillDeviceClient()->SetCarrier( 199 dbus::ObjectPath(device_path), 200 carrier, 201 callback, 202 base::Bind(&HandleShillCallFailure, device_path, error_callback)); 203 } 204 205 void NetworkDeviceHandler::RequirePin( 206 const std::string& device_path, 207 bool require_pin, 208 const std::string& pin, 209 const base::Closure& callback, 210 const network_handler::ErrorCallback& error_callback) { 211 DBusThreadManager::Get()->GetShillDeviceClient()->RequirePin( 212 dbus::ObjectPath(device_path), 213 pin, 214 require_pin, 215 callback, 216 base::Bind(&HandleShillCallFailure, device_path, error_callback)); 217 } 218 219 void NetworkDeviceHandler::EnterPin( 220 const std::string& device_path, 221 const std::string& pin, 222 const base::Closure& callback, 223 const network_handler::ErrorCallback& error_callback) { 224 DBusThreadManager::Get()->GetShillDeviceClient()->EnterPin( 225 dbus::ObjectPath(device_path), 226 pin, 227 callback, 228 base::Bind(&HandleShillCallFailure, device_path, error_callback)); 229 } 230 231 void NetworkDeviceHandler::UnblockPin( 232 const std::string& device_path, 233 const std::string& puk, 234 const std::string& new_pin, 235 const base::Closure& callback, 236 const network_handler::ErrorCallback& error_callback) { 237 DBusThreadManager::Get()->GetShillDeviceClient()->UnblockPin( 238 dbus::ObjectPath(device_path), 239 puk, 240 new_pin, 241 callback, 242 base::Bind(&HandleShillCallFailure, device_path, error_callback)); 243 } 244 245 void NetworkDeviceHandler::ChangePin( 246 const std::string& device_path, 247 const std::string& old_pin, 248 const std::string& new_pin, 249 const base::Closure& callback, 250 const network_handler::ErrorCallback& error_callback) { 251 DBusThreadManager::Get()->GetShillDeviceClient()->ChangePin( 252 dbus::ObjectPath(device_path), 253 old_pin, 254 new_pin, 255 callback, 256 base::Bind(&HandleShillCallFailure, device_path, error_callback)); 257 } 258 259 void NetworkDeviceHandler::HandleShillCallFailureForTest( 260 const std::string& device_path, 261 const network_handler::ErrorCallback& error_callback, 262 const std::string& shill_error_name, 263 const std::string& shill_error_message) { 264 HandleShillCallFailure( 265 device_path, error_callback, shill_error_name, shill_error_message); 266 } 267 268 } // namespace chromeos 269