Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend GAutoPtr to conventionally get receiver #32115

Merged
merged 6 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/platform/GLibTypeDeleter.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class UniquePointerReceiver
{
public:
UniquePointerReceiver(std::unique_ptr<T, Deleter> & target) : mTarget(target) {}

~UniquePointerReceiver() { mTarget.reset(mValue); }

T *& Get() { return mValue; }
T ** operator&() { return &mValue; }

private:
std::unique_ptr<T, Deleter> & mTarget;
Expand Down Expand Up @@ -151,6 +151,14 @@ struct GAutoPtrDeleter<GVariantIter>
};

template <typename T>
using GAutoPtr = std::unique_ptr<T, typename GAutoPtrDeleter<T>::deleter>;
class GAutoPtr : public std::unique_ptr<T, typename GAutoPtrDeleter<T>::deleter>
{
public:
using deleter = typename GAutoPtrDeleter<T>::deleter;
using std::unique_ptr<T, deleter>::unique_ptr;

// Convenience method to get a UniquePointerReceiver for this object.
UniquePointerReceiver<T, deleter> GetReceiver() { return MakeUniquePointerReceiver(*this); }
};

} // namespace chip
55 changes: 23 additions & 32 deletions src/platform/Linux/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ void ConnectivityManagerImpl::_ClearWiFiStationProvision()
if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled)
{
GAutoPtr<GError> err;
wpa_fi_w1_wpa_supplicant1_interface_call_remove_all_networks_sync(mWpaSupplicant.iface, nullptr,
&MakeUniquePointerReceiver(err).Get());
wpa_fi_w1_wpa_supplicant1_interface_call_remove_all_networks_sync(mWpaSupplicant.iface, nullptr, &err.GetReceiver());

if (err != nullptr)
{
Expand Down Expand Up @@ -388,7 +387,7 @@ void ConnectivityManagerImpl::_OnWpaPropertiesChanged(WpaFiW1Wpa_supplicant1Inte

WiFiDiagnosticsDelegate * delegate = GetDiagnosticDataProvider().GetWiFiDiagnosticsDelegate();

g_variant_get(changedProperties, "a{sv}", &MakeUniquePointerReceiver(iter).Get());
g_variant_get(changedProperties, "a{sv}", &iter.GetReceiver());

while (g_variant_iter_loop(iter.get(), "{&sv}", &key, &value))
{
Expand Down Expand Up @@ -497,8 +496,7 @@ void ConnectivityManagerImpl::_OnWpaInterfaceProxyReady(GObject * sourceObject,

std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);

WpaFiW1Wpa_supplicant1Interface * iface =
wpa_fi_w1_wpa_supplicant1_interface_proxy_new_for_bus_finish(res, &MakeUniquePointerReceiver(err).Get());
WpaFiW1Wpa_supplicant1Interface * iface = wpa_fi_w1_wpa_supplicant1_interface_proxy_new_for_bus_finish(res, &err.GetReceiver());

if (mWpaSupplicant.iface)
{
Expand Down Expand Up @@ -552,8 +550,7 @@ void ConnectivityManagerImpl::_OnWpaBssProxyReady(GObject * sourceObject, GAsync

std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);

WpaFiW1Wpa_supplicant1BSS * bss =
wpa_fi_w1_wpa_supplicant1_bss_proxy_new_for_bus_finish(res, &MakeUniquePointerReceiver(err).Get());
WpaFiW1Wpa_supplicant1BSS * bss = wpa_fi_w1_wpa_supplicant1_bss_proxy_new_for_bus_finish(res, &err.GetReceiver());

if (mWpaSupplicant.bss)
{
Expand Down Expand Up @@ -584,7 +581,7 @@ void ConnectivityManagerImpl::_OnWpaInterfaceReady(GObject * sourceObject, GAsyn
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);

gboolean result = wpa_fi_w1_wpa_supplicant1_call_get_interface_finish(mWpaSupplicant.proxy, &mWpaSupplicant.interfacePath, res,
&MakeUniquePointerReceiver(err).Get());
&err.GetReceiver());
if (result)
{
mWpaSupplicant.state = GDBusWpaSupplicant::WPA_GOT_INTERFACE_PATH;
Expand Down Expand Up @@ -622,7 +619,7 @@ void ConnectivityManagerImpl::_OnWpaInterfaceReady(GObject * sourceObject, GAsyn
args = g_variant_builder_end(&builder);

result = wpa_fi_w1_wpa_supplicant1_call_create_interface_sync(mWpaSupplicant.proxy, args, &mWpaSupplicant.interfacePath,
nullptr, &MakeUniquePointerReceiver(error).Get());
nullptr, &error.GetReceiver());

if (result)
{
Expand Down Expand Up @@ -747,7 +744,7 @@ void ConnectivityManagerImpl::_OnWpaProxyReady(GObject * sourceObject, GAsyncRes

std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);

mWpaSupplicant.proxy = wpa_fi_w1_wpa_supplicant1_proxy_new_for_bus_finish(res, &MakeUniquePointerReceiver(err).Get());
mWpaSupplicant.proxy = wpa_fi_w1_wpa_supplicant1_proxy_new_for_bus_finish(res, &err.GetReceiver());
if (mWpaSupplicant.proxy != nullptr && err.get() == nullptr)
{
mWpaSupplicant.state = GDBusWpaSupplicant::WPA_CONNECTED;
Expand Down Expand Up @@ -898,7 +895,7 @@ void ConnectivityManagerImpl::DriveAPState()
GAutoPtr<GError> error(nullptr);

gboolean result = wpa_fi_w1_wpa_supplicant1_interface_call_remove_network_sync(
mWpaSupplicant.iface, mWpaSupplicant.networkPath, nullptr, &MakeUniquePointerReceiver(error).Get());
mWpaSupplicant.iface, mWpaSupplicant.networkPath, nullptr, &error.GetReceiver());

if (result)
{
Expand Down Expand Up @@ -963,7 +960,7 @@ CHIP_ERROR ConnectivityManagerImpl::ConfigureWiFiAP()
args = g_variant_builder_end(&builder);

gboolean result = wpa_fi_w1_wpa_supplicant1_interface_call_add_network_sync(
mWpaSupplicant.iface, args, &mWpaSupplicant.networkPath, nullptr, &MakeUniquePointerReceiver(err).Get());
mWpaSupplicant.iface, args, &mWpaSupplicant.networkPath, nullptr, &err.GetReceiver());

if (result)
{
Expand All @@ -972,7 +969,7 @@ CHIP_ERROR ConnectivityManagerImpl::ConfigureWiFiAP()
ChipLogProgress(DeviceLayer, "wpa_supplicant: added network: SSID: %s: %s", ssid, mWpaSupplicant.networkPath);

result = wpa_fi_w1_wpa_supplicant1_interface_call_select_network_sync(mWpaSupplicant.iface, mWpaSupplicant.networkPath,
nullptr, &MakeUniquePointerReceiver(error).Get());
nullptr, &error.GetReceiver());
if (result)
{
ChipLogProgress(DeviceLayer, "wpa_supplicant: succeeded to start softAP: SSID: %s", ssid);
Expand Down Expand Up @@ -1033,7 +1030,7 @@ ConnectivityManagerImpl::_ConnectWiFiNetworkAsync(GVariant * args,
GAutoPtr<GError> error;

result = wpa_fi_w1_wpa_supplicant1_interface_call_remove_network_sync(mWpaSupplicant.iface, networkPath, nullptr,
&MakeUniquePointerReceiver(error).Get());
&error.GetReceiver());

if (result)
{
Expand All @@ -1055,7 +1052,7 @@ ConnectivityManagerImpl::_ConnectWiFiNetworkAsync(GVariant * args,
}

result = wpa_fi_w1_wpa_supplicant1_interface_call_add_network_sync(mWpaSupplicant.iface, args, &mWpaSupplicant.networkPath,
nullptr, &MakeUniquePointerReceiver(err).Get());
nullptr, &err.GetReceiver());

if (result)
{
Expand Down Expand Up @@ -1127,7 +1124,7 @@ static CHIP_ERROR AddOrReplaceBlob(WpaFiW1Wpa_supplicant1Interface * iface, cons
const char * name = (strncmp(nameOrRef, refPrefix.data(), refPrefix.size()) == 0) ? nameOrRef + refPrefix.size() : nameOrRef;

GAutoPtr<GError> err;
if (!wpa_fi_w1_wpa_supplicant1_interface_call_remove_blob_sync(iface, name, nullptr, &MakeUniquePointerReceiver(err).Get()))
if (!wpa_fi_w1_wpa_supplicant1_interface_call_remove_blob_sync(iface, name, nullptr, &err.GetReceiver()))
{
GAutoPtr<char> remoteError(g_dbus_error_get_remote_error(err.get()));
if (!(remoteError && strcmp(remoteError.get(), kWpaSupplicantBlobUnknown) == 0))
Expand All @@ -1138,8 +1135,7 @@ static CHIP_ERROR AddOrReplaceBlob(WpaFiW1Wpa_supplicant1Interface * iface, cons
err.reset();
}
if (!wpa_fi_w1_wpa_supplicant1_interface_call_add_blob_sync(
iface, name, g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, data.data(), data.size(), 1), nullptr,
&MakeUniquePointerReceiver(err).Get()))
iface, name, g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, data.data(), data.size(), 1), nullptr, &err.GetReceiver()))
{
ChipLogError(DeviceLayer, "wpa_supplicant: failed to add blob: %s", err ? err->message : "unknown error");
return CHIP_ERROR_INTERNAL;
Expand Down Expand Up @@ -1239,8 +1235,8 @@ void ConnectivityManagerImpl::_ConnectWiFiNetworkAsyncCallback(GObject * sourceO
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);

{
gboolean result = wpa_fi_w1_wpa_supplicant1_interface_call_select_network_finish(mWpaSupplicant.iface, res,
&MakeUniquePointerReceiver(err).Get());
gboolean result =
wpa_fi_w1_wpa_supplicant1_interface_call_select_network_finish(mWpaSupplicant.iface, res, &err.GetReceiver());
if (!result)
{
ChipLogError(DeviceLayer, "Failed to perform connect network: %s", err == nullptr ? "unknown error" : err->message);
Expand All @@ -1257,8 +1253,7 @@ void ConnectivityManagerImpl::_ConnectWiFiNetworkAsyncCallback(GObject * sourceO
return;
}

result = wpa_fi_w1_wpa_supplicant1_interface_call_save_config_sync(mWpaSupplicant.iface, nullptr,
&MakeUniquePointerReceiver(err).Get());
result = wpa_fi_w1_wpa_supplicant1_interface_call_save_config_sync(mWpaSupplicant.iface, nullptr, &err.GetReceiver());
if (result)
{
ChipLogProgress(DeviceLayer, "wpa_supplicant: save config succeeded!");
Expand Down Expand Up @@ -1335,8 +1330,7 @@ CHIP_ERROR ConnectivityManagerImpl::CommitConfig()

ChipLogProgress(DeviceLayer, "wpa_supplicant: save config");

result = wpa_fi_w1_wpa_supplicant1_interface_call_save_config_sync(mWpaSupplicant.iface, nullptr,
&MakeUniquePointerReceiver(err).Get());
result = wpa_fi_w1_wpa_supplicant1_interface_call_save_config_sync(mWpaSupplicant.iface, nullptr, &err.GetReceiver());

if (!result)
{
Expand Down Expand Up @@ -1486,8 +1480,7 @@ CHIP_ERROR ConnectivityManagerImpl::GetConfiguredNetwork(NetworkCommissioning::N
}

GAutoPtr<WpaFiW1Wpa_supplicant1Network> networkInfo(wpa_fi_w1_wpa_supplicant1_network_proxy_new_for_bus_sync(
G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, kWpaSupplicantServiceName, networkPath, nullptr,
&MakeUniquePointerReceiver(err).Get()));
G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, kWpaSupplicantServiceName, networkPath, nullptr, &err.GetReceiver()));
if (networkInfo == nullptr)
{
return CHIP_ERROR_INTERNAL;
Expand Down Expand Up @@ -1518,7 +1511,7 @@ CHIP_ERROR ConnectivityManagerImpl::StopAutoScan()
ChipLogDetail(DeviceLayer, "wpa_supplicant: disabling auto scan");

result = wpa_fi_w1_wpa_supplicant1_interface_call_auto_scan_sync(
mWpaSupplicant.iface, "" /* empty string means disabling auto scan */, nullptr, &MakeUniquePointerReceiver(err).Get());
mWpaSupplicant.iface, "" /* empty string means disabling auto scan */, nullptr, &err.GetReceiver());
if (!result)
{
ChipLogError(DeviceLayer, "wpa_supplicant: Failed to stop auto network scan: %s", err ? err->message : "unknown");
Expand Down Expand Up @@ -1548,8 +1541,7 @@ CHIP_ERROR ConnectivityManagerImpl::StartWiFiScan(ByteSpan ssid, WiFiDriver::Sca
g_variant_builder_add(&builder, "{sv}", "Type", g_variant_new_string("active"));
args = g_variant_builder_end(&builder);

result = wpa_fi_w1_wpa_supplicant1_interface_call_scan_sync(mWpaSupplicant.iface, args, nullptr,
&MakeUniquePointerReceiver(err).Get());
result = wpa_fi_w1_wpa_supplicant1_interface_call_scan_sync(mWpaSupplicant.iface, args, nullptr, &err.GetReceiver());

if (result)
{
Expand Down Expand Up @@ -1676,9 +1668,8 @@ bool ConnectivityManagerImpl::_GetBssInfo(const gchar * bssPath, NetworkCommissi
// with the proxy object.

GAutoPtr<GError> err;
GAutoPtr<WpaFiW1Wpa_supplicant1BSS> bss(
wpa_fi_w1_wpa_supplicant1_bss_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, kWpaSupplicantServiceName,
bssPath, nullptr, &MakeUniquePointerReceiver(err).Get()));
GAutoPtr<WpaFiW1Wpa_supplicant1BSS> bss(wpa_fi_w1_wpa_supplicant1_bss_proxy_new_for_bus_sync(
G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, kWpaSupplicantServiceName, bssPath, nullptr, &err.GetReceiver()));

if (bss == nullptr)
{
Expand Down
28 changes: 12 additions & 16 deletions src/platform/Linux/ThreadStackManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ CHIP_ERROR ThreadStackManagerImpl::GLibMatterContextInitThreadStack(ThreadStackM
GAutoPtr<GError> err;
self->mProxy.reset(openthread_io_openthread_border_router_proxy_new_for_bus_sync(
G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, kDBusOpenThreadService, kDBusOpenThreadObjectPath, nullptr,
&MakeUniquePointerReceiver(err).Get()));
&err.GetReceiver()));
VerifyOrReturnError(
self->mProxy != nullptr, CHIP_ERROR_INTERNAL,
ChipLogError(DeviceLayer, "openthread: failed to create openthread dbus proxy %s", err ? err->message : "unknown error"));
Expand Down Expand Up @@ -123,7 +123,7 @@ void ThreadStackManagerImpl::OnDbusPropertiesChanged(OpenthreadIoOpenthreadBorde
GVariant * value;

GAutoPtr<GVariantIter> iter;
g_variant_get(changed_properties, "a{sv}", &MakeUniquePointerReceiver(iter).Get());
g_variant_get(changed_properties, "a{sv}", &iter.GetReceiver());
if (!iter)
return;
while (g_variant_iter_loop(iter.get(), "{&sv}", &key, &value))
Expand Down Expand Up @@ -193,7 +193,7 @@ bool ThreadStackManagerImpl::_HaveRouteToAddress(const Inet::IPAddress & destAdd
if (g_variant_n_children(routes.get()) > 0)
{
GAutoPtr<GVariantIter> iter;
g_variant_get(routes.get(), "av", &MakeUniquePointerReceiver(iter).Get());
g_variant_get(routes.get(), "av", &iter.GetReceiver());
if (!iter)
return false;

Expand All @@ -207,14 +207,13 @@ bool ThreadStackManagerImpl::_HaveRouteToAddress(const Inet::IPAddress & destAdd
guchar preference;
gboolean stable;
gboolean nextHopIsThisDevice;
g_variant_get(route, "(&vqybb)", &MakeUniquePointerReceiver(prefix).Get(), &rloc16, &preference, &stable,
&nextHopIsThisDevice);
g_variant_get(route, "(&vqybb)", &prefix.GetReceiver(), &rloc16, &preference, &stable, &nextHopIsThisDevice);
if (!prefix)
continue;

GAutoPtr<GVariant> address;
guchar prefixLength;
g_variant_get(prefix.get(), "(&vy)", &MakeUniquePointerReceiver(address).Get(), &prefixLength);
g_variant_get(prefix.get(), "(&vy)", &address.GetReceiver(), &prefixLength);
if (!address)
continue;

Expand Down Expand Up @@ -273,8 +272,7 @@ CHIP_ERROR ThreadStackManagerImpl::_GetThreadProvision(Thread::OperationalDatase
GAutoPtr<GError> err;
GAutoPtr<GVariant> response(g_dbus_proxy_call_sync(G_DBUS_PROXY(mProxy.get()), "org.freedesktop.DBus.Properties.Get",
g_variant_new("(ss)", "io.openthread.BorderRouter", "ActiveDatasetTlvs"),
G_DBUS_CALL_FLAGS_NONE, -1, nullptr,
&MakeUniquePointerReceiver(err).Get()));
G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &err.GetReceiver()));

if (err)
{
Expand Down Expand Up @@ -330,7 +328,7 @@ bool ThreadStackManagerImpl::_IsThreadEnabled()
GAutoPtr<GError> err;
GAutoPtr<GVariant> response(g_dbus_proxy_call_sync(G_DBUS_PROXY(mProxy.get()), "org.freedesktop.DBus.Properties.Get",
g_variant_new("(ss)", "io.openthread.BorderRouter", "DeviceRole"),
G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &MakeUniquePointerReceiver(err).Get()));
G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &err.GetReceiver()));

if (err)
{
Expand Down Expand Up @@ -390,8 +388,7 @@ CHIP_ERROR ThreadStackManagerImpl::_SetThreadEnabled(bool val)
else
{
GAutoPtr<GError> err;
gboolean result =
openthread_io_openthread_border_router_call_reset_sync(mProxy.get(), nullptr, &MakeUniquePointerReceiver(err).Get());
gboolean result = openthread_io_openthread_border_router_call_reset_sync(mProxy.get(), nullptr, &err.GetReceiver());
if (err)
{
ChipLogError(DeviceLayer, "openthread: _SetThreadEnabled calling %s failed: %s", "Reset", err->message);
Expand All @@ -413,8 +410,7 @@ void ThreadStackManagerImpl::_OnThreadBrAttachFinished(GObject * source_object,
GAutoPtr<GVariant> attachRes;
GAutoPtr<GError> err;
{
gboolean result = openthread_io_openthread_border_router_call_attach_finish(this_->mProxy.get(), res,
&MakeUniquePointerReceiver(err).Get());
gboolean result = openthread_io_openthread_border_router_call_attach_finish(this_->mProxy.get(), res, &err.GetReceiver());
if (!result)
{
ChipLogError(DeviceLayer, "Failed to perform finish Thread network scan: %s",
Expand Down Expand Up @@ -612,8 +608,8 @@ void ThreadStackManagerImpl::_OnNetworkScanFinished(GAsyncResult * res)
GAutoPtr<GVariant> scan_result;
GAutoPtr<GError> err;
{
gboolean result = openthread_io_openthread_border_router_call_scan_finish(
mProxy.get(), &MakeUniquePointerReceiver(scan_result).Get(), res, &MakeUniquePointerReceiver(err).Get());
gboolean result = openthread_io_openthread_border_router_call_scan_finish(mProxy.get(), &scan_result.GetReceiver(), res,
&err.GetReceiver());
if (!result)
{
ChipLogError(DeviceLayer, "Failed to perform finish Thread network scan: %s",
Expand All @@ -635,7 +631,7 @@ void ThreadStackManagerImpl::_OnNetworkScanFinished(GAsyncResult * res)
if (g_variant_n_children(scan_result.get()) > 0)
{
GAutoPtr<GVariantIter> iter;
g_variant_get(scan_result.get(), "a(tstayqqynyybb)", &MakeUniquePointerReceiver(iter).Get());
g_variant_get(scan_result.get(), "a(tstayqqynyybb)", &iter.GetReceiver());
if (!iter)
{
delete scanResult;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Linux/bluez/AdapterIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ CHIP_ERROR AdapterIterator::Initialize(AdapterIterator * self)
self->mManager = g_dbus_object_manager_client_new_for_bus_sync(
G_BUS_TYPE_SYSTEM, G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE, BLUEZ_INTERFACE, "/",
bluez_object_manager_client_get_proxy_type, nullptr /* unused user data in the Proxy Type Func */,
nullptr /*destroy notify */, nullptr /* cancellable */, &MakeUniquePointerReceiver(error).Get());
nullptr /* destroy notify */, nullptr /* cancellable */, &error.GetReceiver());

VerifyOrExit(self->mManager != nullptr, ChipLogError(DeviceLayer, "Failed to get DBUS object manager for listing adapters.");
err = CHIP_ERROR_INTERNAL);
Expand Down
Loading
Loading