1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/dbus/shill/modem_3gpp_client.h"
#include <map>
#include <memory>
#include <utility>
#include "ash/constants/ash_features.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "chromeos/ash/components/dbus/shill/fake_modem_3gpp_client.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace ash {
namespace {
Modem3gppClient* g_instance = nullptr;
// A class which makes method calls for 3gpp services via the
// org.freedesktop.ModemManager1.3gpp object.
class Modem3gppProxy {
public:
Modem3gppProxy(dbus::Bus* bus,
const std::string& service_name,
const dbus::ObjectPath& object_path)
: proxy_(bus->GetObjectProxy(service_name, object_path)),
service_name_(service_name) {}
Modem3gppProxy(const Modem3gppProxy&) = delete;
Modem3gppProxy& operator=(const Modem3gppProxy&) = delete;
virtual ~Modem3gppProxy() = default;
// Calls SetCarrierLock method.
void SetCarrierLock(const std::string& config,
Modem3gppClient::CarrierLockCallback callback) {
dbus::MethodCall method_call(modemmanager::kModemManager13gppInterface,
modemmanager::kModem3gppSetCarrierLock);
dbus::MessageWriter writer(&method_call);
writer.AppendArrayOfBytes(base::as_byte_span(config));
proxy_->CallMethodWithErrorResponse(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&Modem3gppProxy::OnSetCarrierLock,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
private:
// Handles responses of SetCarrierLock method calls.
void OnSetCarrierLock(Modem3gppClient::CarrierLockCallback callback,
dbus::Response* response,
dbus::ErrorResponse* error_response) {
if (response &&
response->GetMessageType() == dbus::Message::MESSAGE_METHOD_RETURN) {
std::move(callback).Run(CarrierLockResult::kSuccess);
return;
}
if (!error_response) {
std::move(callback).Run(CarrierLockResult::kUnknownError);
return;
}
std::string error_name;
error_name = error_response->GetErrorName();
if (error_name.ends_with("InvalidSignature")) {
std::move(callback).Run(CarrierLockResult::kInvalidSignature);
} else if (error_name.ends_with("InvalidImei")) {
std::move(callback).Run(CarrierLockResult::kInvalidImei);
} else if (error_name.ends_with("InvalidTimestamp")) {
std::move(callback).Run(CarrierLockResult::kInvalidTimeStamp);
} else if (error_name.ends_with("NetworkListTooLarge")) {
std::move(callback).Run(CarrierLockResult::kNetworkListTooLarge);
} else if (error_name.ends_with("AlgorithmNotSupported")) {
std::move(callback).Run(CarrierLockResult::kAlgorithmNotSupported);
} else if (error_name.ends_with("FeatureNotSupported")) {
std::move(callback).Run(CarrierLockResult::kFeatureNotSupported);
} else if (error_name.ends_with("DecodeOrParsingError")) {
std::move(callback).Run(CarrierLockResult::kDecodeOrParsingError);
} else if (error_name.ends_with("Unsupported")) {
std::move(callback).Run(CarrierLockResult::kOperationNotSupported);
} else {
std::move(callback).Run(CarrierLockResult::kUnknownError);
}
}
raw_ptr<dbus::ObjectProxy> proxy_;
std::string service_name_;
// Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed.
base::WeakPtrFactory<Modem3gppProxy> weak_ptr_factory_{this};
};
class COMPONENT_EXPORT(CHROMEOS_DBUS) Modem3gppClientImpl
: public Modem3gppClient {
public:
explicit Modem3gppClientImpl(dbus::Bus* bus) : bus_(bus) {}
Modem3gppClientImpl(const Modem3gppClientImpl&) = delete;
Modem3gppClientImpl& operator=(const Modem3gppClientImpl&) = delete;
~Modem3gppClientImpl() override = default;
void SetCarrierLock(const std::string& service_name,
const dbus::ObjectPath& object_path,
const std::string& config,
CarrierLockCallback callback) override {
GetProxy(service_name, object_path)
->SetCarrierLock(config, std::move(callback));
}
private:
// Map of 3gppProxies where Key is a pair of service name and object path
using ProxyMap = std::map<std::pair<std::string, std::string>,
std::unique_ptr<Modem3gppProxy>>;
// Returns a 3gppProxy for the given service name and object path.
Modem3gppProxy* GetProxy(const std::string& service_name,
const dbus::ObjectPath& object_path) {
const ProxyMap::key_type key(service_name, object_path.value());
ProxyMap::const_iterator it = proxies_.find(key);
if (it != proxies_.end()) {
return it->second.get();
}
// There is no proxy for the service_name and object_path, create it.
std::unique_ptr<Modem3gppProxy> proxy =
std::make_unique<Modem3gppProxy>(bus_, service_name, object_path);
Modem3gppProxy* proxy_ptr = proxy.get();
proxies_[key] = std::move(proxy);
return proxy_ptr;
}
raw_ptr<dbus::Bus> bus_;
ProxyMap proxies_;
};
} // namespace
////////////////////////////////////////////////////////////////////////////////
// Modem3gppClient
Modem3gppClient::Modem3gppClient() {
DCHECK(!g_instance);
g_instance = this;
}
Modem3gppClient::~Modem3gppClient() {
DCHECK_EQ(this, g_instance);
g_instance = nullptr;
}
// static
void Modem3gppClient::Initialize(dbus::Bus* bus) {
DCHECK(bus);
DCHECK(!g_instance);
new Modem3gppClientImpl(bus);
}
// static
void Modem3gppClient::InitializeFake() {
new FakeModem3gppClient();
}
// static
void Modem3gppClient::Shutdown() {
DCHECK(g_instance);
delete g_instance;
}
// static
Modem3gppClient* Modem3gppClient::Get() {
return g_instance;
}
} // namespace ash
|