From 22e3f4cd9118a19bb74b37c1a63a3f330181141d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20Michalak-Szmaci=C5=84ski?= Date: Fri, 29 Sep 2023 16:46:11 +0200 Subject: [PATCH] [Tizen] Convert possible glib objects to GAutoPtr<> (#29488) * Replace glib char with GAutoPtr in BLEManagerImpl.cpp * Replace glib char with GAutoPtr in DnssdImpl.cpp * Replace glib char with GAutoPtr in WiFiManager.cpp * Add GSource to GLibTypeDeleter.h * Replace glib GSource with GAutoPtr in ChipDeviceScanner.cpp * Replace glib GSource with GAutoPtr in DnssdImpl.cpp * Replace glib GSource with GAutoPtr in PlatformManagerImpl.cpp * Review update --- src/platform/GLibTypeDeleter.h | 6 +++ src/platform/Tizen/BLEManagerImpl.cpp | 58 ++++++++++----------- src/platform/Tizen/ChipDeviceScanner.cpp | 20 ++++---- src/platform/Tizen/DnssdImpl.cpp | 58 +++++++++------------ src/platform/Tizen/PlatformManagerImpl.cpp | 8 +-- src/platform/Tizen/WiFiManager.cpp | 59 ++++++++++------------ 6 files changed, 97 insertions(+), 112 deletions(-) diff --git a/src/platform/GLibTypeDeleter.h b/src/platform/GLibTypeDeleter.h index bd93be76203cd2..1e187aa275adb3 100644 --- a/src/platform/GLibTypeDeleter.h +++ b/src/platform/GLibTypeDeleter.h @@ -104,6 +104,12 @@ struct GAutoPtrDeleter using deleter = GErrorDeleter; }; +template <> +struct GAutoPtrDeleter +{ + using deleter = GObjectDeleter; +}; + template <> struct GAutoPtrDeleter { diff --git a/src/platform/Tizen/BLEManagerImpl.cpp b/src/platform/Tizen/BLEManagerImpl.cpp index 6483920d410ac9..f4426cde2119de 100644 --- a/src/platform/Tizen/BLEManagerImpl.cpp +++ b/src/platform/Tizen/BLEManagerImpl.cpp @@ -58,6 +58,7 @@ #include #include #include +#include #include #include #include @@ -187,21 +188,19 @@ void BLEManagerImpl::ReadValueRequestedCb(const char * remoteAddress, int reques { int ret, len = 0; bt_gatt_type_e type; - char * uuid = nullptr; - char * value = nullptr; + GAutoPtr uuid; + GAutoPtr value; - VerifyOrReturn(__GetAttInfo(gattHandle, &uuid, &type) == BT_ERROR_NONE, + VerifyOrReturn(__GetAttInfo(gattHandle, &MakeUniquePointerReceiver(uuid).Get(), &type) == BT_ERROR_NONE, ChipLogError(DeviceLayer, "Failed to fetch GATT Attribute from GATT handle")); - ChipLogProgress(DeviceLayer, "Gatt read requested on %s: uuid=%s", __ConvertAttTypeToStr(type), StringOrNullMarker(uuid)); - g_free(uuid); + ChipLogProgress(DeviceLayer, "Gatt read requested on %s: uuid=%s", __ConvertAttTypeToStr(type), StringOrNullMarker(uuid.get())); - ret = bt_gatt_get_value(gattHandle, &value, &len); + ret = bt_gatt_get_value(gattHandle, &MakeUniquePointerReceiver(value).Get(), &len); VerifyOrReturn(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_get_value() failed. ret: %d", ret)); - ChipLogByteSpan(DeviceLayer, ByteSpan(Uint8::from_const_char(value), len)); + ChipLogByteSpan(DeviceLayer, ByteSpan(Uint8::from_const_char(value.get()), len)); - ret = bt_gatt_server_send_response(requestId, BT_GATT_REQUEST_TYPE_READ, offset, 0x00, value, len); - g_free(value); + ret = bt_gatt_server_send_response(requestId, BT_GATT_REQUEST_TYPE_READ, offset, 0x00, value.get(), len); VerifyOrReturn(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_server_send_response() failed. ret: %d", ret)); } @@ -209,19 +208,18 @@ void BLEManagerImpl::WriteValueRequestedCb(const char * remoteAddress, int reque bool responseNeeded, int offset, const char * value, int len, void * userData) { int ret; - char * uuid = nullptr; + GAutoPtr uuid; BLEConnection * conn = nullptr; bt_gatt_type_e type; conn = static_cast(g_hash_table_lookup(sInstance.mConnectionMap, remoteAddress)); VerifyOrReturn(conn != nullptr, ChipLogError(DeviceLayer, "Failed to find connection info")); - VerifyOrReturn(__GetAttInfo(gattHandle, &uuid, &type) == BT_ERROR_NONE, + VerifyOrReturn(__GetAttInfo(gattHandle, &MakeUniquePointerReceiver(uuid).Get(), &type) == BT_ERROR_NONE, ChipLogError(DeviceLayer, "Failed to fetch GATT Attribute from GATT handle")); ChipLogProgress(DeviceLayer, "Gatt write requested on %s: uuid=%s len=%d", __ConvertAttTypeToStr(type), - StringOrNullMarker(uuid), len); + StringOrNullMarker(uuid.get()), len); ChipLogByteSpan(DeviceLayer, ByteSpan(Uint8::from_const_char(value), len)); - g_free(uuid); ret = bt_gatt_set_value(gattHandle, value, len); VerifyOrReturn(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_set_value() failed. ret: %d", ret)); @@ -234,7 +232,7 @@ void BLEManagerImpl::WriteValueRequestedCb(const char * remoteAddress, int reque void BLEManagerImpl::NotificationStateChangedCb(bool notify, bt_gatt_server_h server, bt_gatt_h gattHandle, void * userData) { - char * uuid = nullptr; + GAutoPtr uuid; BLEConnection * conn = nullptr; bt_gatt_type_e type; GHashTableIter iter; @@ -250,12 +248,11 @@ void BLEManagerImpl::NotificationStateChangedCb(bool notify, bt_gatt_server_h se } VerifyOrReturn(conn != nullptr, ChipLogError(DeviceLayer, "Failed to find connection info")); - VerifyOrReturn(__GetAttInfo(gattHandle, &uuid, &type) == BT_ERROR_NONE, + VerifyOrReturn(__GetAttInfo(gattHandle, &MakeUniquePointerReceiver(uuid).Get(), &type) == BT_ERROR_NONE, ChipLogError(DeviceLayer, "Failed to fetch GATT Attribute from GATT handle")); ChipLogProgress(DeviceLayer, "Notification State Changed %d on %s: %s", notify, __ConvertAttTypeToStr(type), - StringOrNullMarker(uuid)); - g_free(uuid); + StringOrNullMarker(uuid.get())); sInstance.NotifyBLESubscribed(notify ? true : false, conn); } @@ -728,23 +725,22 @@ int BLEManagerImpl::StopBLEAdvertising() static bool __GattClientForeachCharCb(int total, int index, bt_gatt_h charHandle, void * data) { bt_gatt_type_e type; - char * uuid = nullptr; - auto conn = static_cast(data); + GAutoPtr uuid; + auto conn = static_cast(data); - VerifyOrExit(__GetAttInfo(charHandle, &uuid, &type) == BT_ERROR_NONE, + VerifyOrExit(__GetAttInfo(charHandle, &MakeUniquePointerReceiver(uuid).Get(), &type) == BT_ERROR_NONE, ChipLogError(DeviceLayer, "Failed to fetch GATT Attribute from CHAR handle")); - if (strcasecmp(uuid, chip_ble_char_c1_tx_uuid) == 0) + if (strcasecmp(uuid.get(), chip_ble_char_c1_tx_uuid) == 0) { - ChipLogProgress(DeviceLayer, "CHIP Char C1 TX Found [%s]", StringOrNullMarker(uuid)); + ChipLogProgress(DeviceLayer, "CHIP Char C1 TX Found [%s]", StringOrNullMarker(uuid.get())); conn->gattCharC1Handle = charHandle; } - else if (strcasecmp(uuid, chip_ble_char_c2_rx_uuid) == 0) + else if (strcasecmp(uuid.get(), chip_ble_char_c2_rx_uuid) == 0) { - ChipLogProgress(DeviceLayer, "CHIP Char C2 RX Found [%s]", StringOrNullMarker(uuid)); + ChipLogProgress(DeviceLayer, "CHIP Char C2 RX Found [%s]", StringOrNullMarker(uuid.get())); conn->gattCharC2Handle = charHandle; } - g_free(uuid); exit: /* Try next Char UUID */ @@ -754,25 +750,23 @@ static bool __GattClientForeachCharCb(int total, int index, bt_gatt_h charHandle static bool __GattClientForeachServiceCb(int total, int index, bt_gatt_h svcHandle, void * data) { bt_gatt_type_e type; - char * uuid = nullptr; - auto conn = static_cast(data); + GAutoPtr uuid; + auto conn = static_cast(data); ChipLogProgress(DeviceLayer, "__GattClientForeachServiceCb"); - VerifyOrExit(__GetAttInfo(svcHandle, &uuid, &type) == BT_ERROR_NONE, + VerifyOrExit(__GetAttInfo(svcHandle, &MakeUniquePointerReceiver(uuid).Get(), &type) == BT_ERROR_NONE, ChipLogError(DeviceLayer, "Failed to fetch GATT Attribute from SVC handle")); - if (strcasecmp(uuid, chip_ble_service_uuid) == 0) + if (strcasecmp(uuid.get(), chip_ble_service_uuid) == 0) { - ChipLogProgress(DeviceLayer, "CHIP Service UUID Found [%s]", StringOrNullMarker(uuid)); + ChipLogProgress(DeviceLayer, "CHIP Service UUID Found [%s]", StringOrNullMarker(uuid.get())); if (bt_gatt_service_foreach_characteristics(svcHandle, __GattClientForeachCharCb, conn) == BT_ERROR_NONE) conn->isChipDevice = true; /* Got CHIP Device, no need to process further service */ - g_free(uuid); return false; } - g_free(uuid); exit: /* Try next Service UUID */ diff --git a/src/platform/Tizen/ChipDeviceScanner.cpp b/src/platform/Tizen/ChipDeviceScanner.cpp index 5dcf154f13b984..d69db7bf24263a 100644 --- a/src/platform/Tizen/ChipDeviceScanner.cpp +++ b/src/platform/Tizen/ChipDeviceScanner.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include namespace chip { @@ -145,25 +146,22 @@ gboolean ChipDeviceScanner::TimerExpiredCb(gpointer userData) CHIP_ERROR ChipDeviceScanner::TriggerScan(ChipDeviceScanner * self) { - CHIP_ERROR err = CHIP_NO_ERROR; - GSource * idleSource; + GAutoPtr idleSource; int ret; // Trigger LE Scan ret = bt_adapter_le_start_scan(LeScanResultCb, self); - VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_adapter_le_start_scan() failed: %s", get_error_message(ret)); - err = CHIP_ERROR_INTERNAL); + VerifyOrReturnValue(ret == BT_ERROR_NONE, CHIP_ERROR_INTERNAL, + ChipLogError(DeviceLayer, "bt_adapter_le_start_scan() failed: %s", get_error_message(ret))); self->mIsScanning = true; // Setup timer for scan timeout - idleSource = g_timeout_source_new(self->mScanTimeoutMs); - g_source_set_callback(idleSource, TimerExpiredCb, self, nullptr); - g_source_set_priority(idleSource, G_PRIORITY_HIGH_IDLE); - g_source_attach(idleSource, g_main_context_get_thread_default()); - g_source_unref(idleSource); + idleSource = GAutoPtr(g_timeout_source_new(self->mScanTimeoutMs)); + g_source_set_callback(idleSource.get(), TimerExpiredCb, self, nullptr); + g_source_set_priority(idleSource.get(), G_PRIORITY_HIGH_IDLE); + g_source_attach(idleSource.get(), g_main_context_get_thread_default()); -exit: - return err; + return CHIP_NO_ERROR; } static bool __IsScanFilterSupported() diff --git a/src/platform/Tizen/DnssdImpl.cpp b/src/platform/Tizen/DnssdImpl.cpp index 0c68bd67694a03..646d40af5a8f53 100644 --- a/src/platform/Tizen/DnssdImpl.cpp +++ b/src/platform/Tizen/DnssdImpl.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #if CHIP_DEVICE_CONFIG_ENABLE_THREAD @@ -186,31 +187,31 @@ void OnBrowse(dnssd_service_state_e state, dnssd_service_h service, void * data) g_source_attach(source, g_main_context_get_thread_default()); bCtx->mTimeoutSource = source; - char * type = nullptr; - char * name = nullptr; - char * ifaceName = nullptr; + chip::GAutoPtr type; + chip::GAutoPtr name; + chip::GAutoPtr ifaceName; uint32_t interfaceId = 0; - ret = dnssd_service_get_type(service, &type); + ret = dnssd_service_get_type(service, &MakeUniquePointerReceiver(type).Get()); VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_get_type() failed. ret: %d", ret)); - ret = dnssd_service_get_name(service, &name); + ret = dnssd_service_get_name(service, &MakeUniquePointerReceiver(name).Get()); VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_get_name() failed. ret: %d", ret)); - ret = dnssd_service_get_interface(service, &ifaceName); + ret = dnssd_service_get_interface(service, &MakeUniquePointerReceiver(ifaceName).Get()); VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_get_interface() failed. ret: %d", ret)); - interfaceId = if_nametoindex(ifaceName); + interfaceId = if_nametoindex(ifaceName.get()); VerifyOrExit(interfaceId > 0, ChipLogError(DeviceLayer, "if_nametoindex() failed. errno: %d", errno); ret = DNSSD_ERROR_OPERATION_FAILED); if (state == DNSSD_SERVICE_STATE_AVAILABLE) { - OnBrowseAdd(bCtx, type, name, interfaceId); + OnBrowseAdd(bCtx, type.get(), name.get(), interfaceId); } else { - OnBrowseRemove(bCtx, type, name, interfaceId); + OnBrowseRemove(bCtx, type.get(), name.get(), interfaceId); } exit: @@ -223,10 +224,6 @@ void OnBrowse(dnssd_service_state_e state, dnssd_service_h service, void * data) // After this point the context might be no longer valid bCtx->mInstance->RemoveContext(bCtx); } - - g_free(type); - g_free(name); - g_free(ifaceName); } CHIP_ERROR BrowseAsync(chip::Dnssd::BrowseContext * bCtx) @@ -310,46 +307,42 @@ void OnResolve(dnssd_error_e result, dnssd_service_h service, void * userData) ChipLogDetail(DeviceLayer, "DNSsd %s", __func__); auto rCtx = reinterpret_cast(userData); - char * name = nullptr; - char * ipv4 = nullptr; - char * ipv6 = nullptr; + chip::GAutoPtr name; + chip::GAutoPtr ipv4; + chip::GAutoPtr ipv6; int port = 0; char * interface = nullptr; chip::Inet::IPAddress ipAddr; CHIP_ERROR err = CHIP_NO_ERROR; - int ret = dnssd_service_get_name(service, &name); + int ret = dnssd_service_get_name(service, &MakeUniquePointerReceiver(name).Get()); VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_get_name() failed. ret: %d", ret)); - chip::Platform::CopyString(rCtx->mResult.mName, name); - g_free(name); + chip::Platform::CopyString(rCtx->mResult.mName, name.get()); - ret = dnssd_service_get_ip(service, &ipv4, &ipv6); + ret = dnssd_service_get_ip(service, &MakeUniquePointerReceiver(ipv4).Get(), &MakeUniquePointerReceiver(ipv6).Get()); VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_get_ip() failed. ret: %d", ret)); // If both IPv4 and IPv6 are set, IPv6 address has higher priority. - if (ipv6 != nullptr && strcmp(ipv6, kEmptyAddressIpv6) != 0) + if (ipv6.get() != nullptr && strcmp(ipv6.get(), kEmptyAddressIpv6) != 0) { - if (!chip::Inet::IPAddress::FromString(ipv6, ipAddr) || ipAddr.Type() != chip::Inet::IPAddressType::kIPv6) + if (!chip::Inet::IPAddress::FromString(ipv6.get(), ipAddr) || ipAddr.Type() != chip::Inet::IPAddressType::kIPv6) { ret = DNSSD_ERROR_OPERATION_FAILED; } } #if INET_CONFIG_ENABLE_IPV4 - else if (ipv4 != nullptr) + else if (ipv4.get() != nullptr) { - if (!chip::Inet::IPAddress::FromString(ipv4, ipAddr) || ipAddr.Type() != chip::Inet::IPAddressType::kIPv4) + if (!chip::Inet::IPAddress::FromString(ipv4.get(), ipAddr) || ipAddr.Type() != chip::Inet::IPAddressType::kIPv4) { ret = DNSSD_ERROR_OPERATION_FAILED; } } #endif - ChipLogDetail(DeviceLayer, "DNSsd %s: IPv4: %s, IPv6: %s, ret: %d", __func__, StringOrNullMarker(ipv4), - StringOrNullMarker(ipv6), ret); - - g_free(ipv4); - g_free(ipv6); + ChipLogDetail(DeviceLayer, "DNSsd %s: IPv4: %s, IPv6: %s, ret: %d", __func__, StringOrNullMarker(ipv4.get()), + StringOrNullMarker(ipv6.get()), ret); VerifyOrExit(ret == DNSSD_ERROR_NONE, ); @@ -378,10 +371,9 @@ void OnResolve(dnssd_error_e result, dnssd_service_h service, void * userData) // with the NSD internal mutex locked, which is also locked by the // dnssd_create_remote_service() function called in the Resolve(), and // the Resolve() itself is called with the stack mutex locked. - auto * sourceIdle = g_idle_source_new(); - g_source_set_callback(sourceIdle, OnResolveFinalize, rCtx, NULL); - g_source_attach(sourceIdle, g_main_context_get_thread_default()); - g_source_unref(sourceIdle); + chip::GAutoPtr sourceIdle(g_idle_source_new()); + g_source_set_callback(sourceIdle.get(), OnResolveFinalize, rCtx, NULL); + g_source_attach(sourceIdle.get(), g_main_context_get_thread_default()); } return; diff --git a/src/platform/Tizen/PlatformManagerImpl.cpp b/src/platform/Tizen/PlatformManagerImpl.cpp index 27174a6c4c74ee..58e1e014f41e28 100644 --- a/src/platform/Tizen/PlatformManagerImpl.cpp +++ b/src/platform/Tizen/PlatformManagerImpl.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include "PosixConfig.h" @@ -92,9 +93,9 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack() GLibMatterContextInvokeData invokeData{}; - auto * idleSource = g_idle_source_new(); + GAutoPtr idleSource(g_idle_source_new()); g_source_set_callback( - idleSource, + idleSource.get(), [](void * userData_) { auto * data = reinterpret_cast(userData_); std::unique_lock lock(data->mDoneMutex); @@ -103,8 +104,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack() return G_SOURCE_REMOVE; }, &invokeData, nullptr); - g_source_attach(idleSource, g_main_loop_get_context(mGLibMainLoop)); - g_source_unref(idleSource); + g_source_attach(idleSource.get(), g_main_loop_get_context(mGLibMainLoop)); std::unique_lock lock(invokeData.mDoneMutex); invokeData.mDoneCond.wait(lock, [&invokeData]() { return invokeData.mDone; }); diff --git a/src/platform/Tizen/WiFiManager.cpp b/src/platform/Tizen/WiFiManager.cpp index cc0369120b11b0..bd0907f5531ab2 100644 --- a/src/platform/Tizen/WiFiManager.cpp +++ b/src/platform/Tizen/WiFiManager.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -384,12 +385,12 @@ void WiFiManager::_FullScanFinishedCb(wifi_manager_error_e wifiErr, void * userD bool WiFiManager::_FoundAPOnScanCb(wifi_manager_ap_h ap, void * userData) { - bool cbRet = true; - int wifiErr = WIFI_MANAGER_ERROR_NONE; - char * essid = nullptr; - char * bssid = nullptr; - int rssi = 0; - int freq = 0; + bool cbRet = true; + int wifiErr = WIFI_MANAGER_ERROR_NONE; + GAutoPtr essid; + GAutoPtr bssid; + int rssi = 0; + int freq = 0; auto networkScanned = static_cast *>(userData); std::pair bandInfo; @@ -397,17 +398,17 @@ bool WiFiManager::_FoundAPOnScanCb(wifi_manager_ap_h ap, void * userData) wifi_manager_security_type_e type; WiFiScanResponse scannedAP; - wifiErr = wifi_manager_ap_get_essid(ap, &essid); + wifiErr = wifi_manager_ap_get_essid(ap, &MakeUniquePointerReceiver(essid).Get()); VerifyOrExit(wifiErr == WIFI_MANAGER_ERROR_NONE, ChipLogError(DeviceLayer, "FAIL: get AP essid [%s]", get_error_message(wifiErr))); - ChipLogProgress(DeviceLayer, "Essid Found: %s\n", essid); - scannedAP.ssidLen = static_cast(std::min(strlen(essid), sizeof(scannedAP.ssid))); - memcpy(scannedAP.ssid, essid, scannedAP.ssidLen); + ChipLogProgress(DeviceLayer, "Essid Found: %s\n", essid.get()); + scannedAP.ssidLen = static_cast(std::min(strlen(essid.get()), sizeof(scannedAP.ssid))); + memcpy(scannedAP.ssid, essid.get(), scannedAP.ssidLen); - wifiErr = wifi_manager_ap_get_bssid(ap, &bssid); + wifiErr = wifi_manager_ap_get_bssid(ap, &MakeUniquePointerReceiver(bssid).Get()); VerifyOrExit(wifiErr == WIFI_MANAGER_ERROR_NONE, ChipLogError(DeviceLayer, "Fail: get AP bssid [%s]", get_error_message(wifiErr))); - memcpy(scannedAP.bssid, bssid, std::min(strlen(bssid), sizeof(scannedAP.bssid))); + memcpy(scannedAP.bssid, bssid.get(), std::min(strlen(bssid.get()), sizeof(scannedAP.bssid))); wifiErr = wifi_manager_ap_get_rssi(ap, &rssi); VerifyOrExit(wifiErr == WIFI_MANAGER_ERROR_NONE, @@ -428,26 +429,23 @@ bool WiFiManager::_FoundAPOnScanCb(wifi_manager_ap_h ap, void * userData) networkScanned->push_back(scannedAP); -exit : { - g_free(essid); - g_free(bssid); -} +exit: return cbRet; } bool WiFiManager::_FoundAPCb(wifi_manager_ap_h ap, void * userData) { - bool cbRet = true; - int wifiErr = WIFI_MANAGER_ERROR_NONE; - char * essid = nullptr; + bool cbRet = true; + int wifiErr = WIFI_MANAGER_ERROR_NONE; + GAutoPtr essid; bool isPassphraseRequired = false; auto clonedAp = reinterpret_cast(userData); - wifiErr = wifi_manager_ap_get_essid(ap, &essid); + wifiErr = wifi_manager_ap_get_essid(ap, &MakeUniquePointerReceiver(essid).Get()); VerifyOrExit(wifiErr == WIFI_MANAGER_ERROR_NONE, ChipLogError(DeviceLayer, "FAIL: get AP essid [%s]", get_error_message(wifiErr))); - VerifyOrExit(strcmp(sInstance.mWiFiSSID, essid) == 0, ); + VerifyOrExit(strcmp(sInstance.mWiFiSSID, essid.get()) == 0, ); wifiErr = wifi_manager_ap_is_passphrase_required(ap, &isPassphraseRequired); VerifyOrExit(wifiErr == WIFI_MANAGER_ERROR_NONE, @@ -468,7 +466,6 @@ bool WiFiManager::_FoundAPCb(wifi_manager_ap_h ap, void * userData) cbRet = false; exit: - g_free(essid); return cbRet; } @@ -500,24 +497,23 @@ void WiFiManager::_ConnectedCb(wifi_manager_error_e wifiErr, void * userData) bool WiFiManager::_ConfigListCb(const wifi_manager_config_h config, void * userData) { - int wifiErr = WIFI_MANAGER_ERROR_NONE; - char * name = nullptr; + int wifiErr = WIFI_MANAGER_ERROR_NONE; + GAutoPtr name; wifi_manager_security_type_e securityType = WIFI_MANAGER_SECURITY_TYPE_NONE; - wifi_manager_config_get_name(config, &name); + wifi_manager_config_get_name(config, &MakeUniquePointerReceiver(name).Get()); wifi_manager_config_get_security_type(config, &securityType); wifiErr = wifi_manager_config_remove(sInstance.mWiFiManagerHandle, config); if (wifiErr == WIFI_MANAGER_ERROR_NONE) { - ChipLogProgress(DeviceLayer, "Remove config [%s:%s]", name, __WiFiSecurityTypeToStr(securityType)); + ChipLogProgress(DeviceLayer, "Remove config [%s:%s]", name.get(), __WiFiSecurityTypeToStr(securityType)); } else { ChipLogError(DeviceLayer, "FAIL: remove config [%s]", get_error_message(wifiErr)); } - g_free(name); return true; } @@ -1156,13 +1152,12 @@ CHIP_ERROR WiFiManager::GetConfiguredNetwork(NetworkCommissioning::Network & net { return CHIP_ERROR_INCORRECT_STATE; } - char * essid = nullptr; - int wifiErr = wifi_manager_ap_get_essid(connectedAp, &essid); + GAutoPtr essid; + int wifiErr = wifi_manager_ap_get_essid(connectedAp, &MakeUniquePointerReceiver(essid).Get()); VerifyOrReturnError(wifiErr == WIFI_MANAGER_ERROR_NONE, CHIP_ERROR_INTERNAL, ChipLogError(DeviceLayer, "FAIL: get essid [%s]", get_error_message(wifiErr))); - network.networkIDLen = static_cast(std::min(strlen(essid), sizeof(network.networkID))); - memcpy(network.networkID, essid, network.networkIDLen); - g_free(essid); + network.networkIDLen = static_cast(std::min(strlen(essid.get()), sizeof(network.networkID))); + memcpy(network.networkID, essid.get(), network.networkIDLen); return CHIP_NO_ERROR; }