blob: 4891731b3b7d0bf398df861d8f8cf8fdbc78eaee [file]
/*
*
* Copyright (c) 2020-2026 Project CHIP Authors
* Copyright (c) 2019 Nest Labs, Inc.
* Copyright (c) 2025 NXP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <algorithm>
#include <mutex>
#include <stdint.h>
#include <glib.h>
#include <lib/support/BytesToHex.h>
#include <lib/support/CHIPMemString.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/CommissionableDataProvider.h>
#include <platform/ConnectivityManager.h>
#include <platform/DeviceInstanceInfoProvider.h>
#include <platform/GLibTypeDeleter.h>
#include <platform/PlatformManager.h>
#include "ConnectivityManagerImpl.h"
#include "WpaSupplicantClient.h"
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
using namespace ::chip::WiFiPAF;
#endif
namespace chip {
namespace DeviceLayer {
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
static const char srv_name[] = "_matterc._udp";
// Parses a MAC address in the "aa:bb:cc:dd:ee:ff" form used by wpa_supplicant.
static bool ParseMacAddress(const char * str, uint8_t (&addr)[kMACAddressLength])
{
VerifyOrReturnValue(strlen(str) == kMACAddressLength * 3 - 1, false);
for (size_t i = 0; i < kMACAddressLength; i++)
{
VerifyOrReturnValue(i == 0 || str[i * 3 - 1] == ':', false);
VerifyOrReturnValue(Encoding::HexToBytes(&str[i * 3], 2, &addr[i], 1) == 1, false);
}
return true;
}
/*
NAN-USD Service Protocol Type: ref: Table 58 of Wi-Fi Aware Specificaiton
*/
#define NAN_PUBLISH_SSI_TAG " ssi="
#pragma pack(push, 1)
struct PAFPublishSSI
{
uint8_t DevOpCode;
uint16_t DevInfo;
uint16_t ProductId;
uint16_t VendorId;
};
enum nan_service_protocol_type
{
NAN_SRV_PROTO_BONJOUR = 1,
NAN_SRV_PROTO_GENERIC = 2,
NAN_SRV_PROTO_CSA_MATTER = 3,
};
#pragma pack(pop)
CHIP_ERROR ConnectivityManagerImpl::_WiFiPAFPublish(ConnectivityManager::WiFiPAFAdvertiseParam & InArgs)
{
CHIP_ERROR result = StartWiFiManagementSync();
VerifyOrReturnError(result == CHIP_NO_ERROR, result);
GAutoPtr<GError> err;
guint publish_id;
enum nan_service_protocol_type srv_proto_type = nan_service_protocol_type::NAN_SRV_PROTO_CSA_MATTER;
unsigned int ttl = CHIP_DEVICE_CONFIG_WIFIPAF_MAX_ADVERTISING_TIMEOUT_SECS;
unsigned int freq = CHIP_DEVICE_CONFIG_WIFIPAF_24G_DEFAUTL_CHNL;
unsigned int ssi_len = sizeof(struct PAFPublishSSI);
// Add the freq_list:
GVariant * freq_array_variant =
g_variant_new_fixed_array(G_VARIANT_TYPE_UINT16, InArgs.freq_list.get(), InArgs.freq_list_len, sizeof(guint16));
if (freq_array_variant == nullptr)
{
ChipLogError(DeviceLayer, "WiFi-PAF: freq_array_variant is NULL ");
return CHIP_ERROR_INTERNAL;
}
// Construct the SSI
struct PAFPublishSSI PafPublish_ssi;
PafPublish_ssi.DevOpCode = 0;
SuccessOrDie(DeviceLayer::GetCommissionableDataProvider()->GetSetupDiscriminator(PafPublish_ssi.DevInfo));
if (DeviceLayer::GetDeviceInstanceInfoProvider()->GetProductId(PafPublish_ssi.ProductId) != CHIP_NO_ERROR)
{
PafPublish_ssi.ProductId = 0;
}
if (DeviceLayer::GetDeviceInstanceInfoProvider()->GetVendorId(PafPublish_ssi.VendorId) != CHIP_NO_ERROR)
{
PafPublish_ssi.VendorId = 0;
}
GVariant * ssi_array_variant = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, &PafPublish_ssi, ssi_len, sizeof(guint8));
if (ssi_array_variant == nullptr)
{
ChipLogProgress(DeviceLayer, "WiFi-PAF: ssi_array_variant is NULL ");
return CHIP_ERROR_INTERNAL;
}
GVariantBuilder builder;
GVariant * args = nullptr;
g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add(&builder, "{sv}", "srv_name", g_variant_new_string(srv_name));
g_variant_builder_add(&builder, "{sv}", "srv_proto_type", g_variant_new_byte(srv_proto_type));
g_variant_builder_add(&builder, "{sv}", "ttl", g_variant_new_uint16(ttl));
g_variant_builder_add(&builder, "{sv}", "freq", g_variant_new_uint16(freq));
g_variant_builder_add(&builder, "{sv}", "ssi", ssi_array_variant);
g_variant_builder_add(&builder, "{sv}", "freq_list", freq_array_variant);
args = g_variant_builder_end(&builder);
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
wpa_supplicant_1_interface_call_nanpublish_sync(mWpaSupplicant.iface.get(), args, &publish_id, nullptr, &err.GetReceiver());
ChipLogProgress(DeviceLayer, "WiFi-PAF: publish_id: %u ! ", publish_id);
WiFiPAFSession sessionInfo = { .role = WiFiPafRole::kWiFiPafRole_Publisher, .id = publish_id };
WiFiPAFLayer & WiFiPafLayer = WiFiPAFLayer::GetWiFiPAFLayer();
ReturnErrorOnFailure(WiFiPafLayer.AddPafSession(PafInfoAccess::kAccSessionId, sessionInfo));
InArgs.publish_id = publish_id;
return CHIP_NO_ERROR;
}
CHIP_ERROR ConnectivityManagerImpl::_WiFiPAFCancelPublish(uint32_t PublishId)
{
GAutoPtr<GError> err;
ChipLogProgress(DeviceLayer, "WiFi-PAF: cancel publish_id: %d ! ", PublishId);
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
VerifyOrReturnError(mWpaSupplicant.iface, CHIP_ERROR_INTERNAL,
ChipLogError(DeviceLayer, "WiFi-PAF: Skip D-Bus 'cancel publish' call since wpa_supplicant is not ready"));
gboolean result =
wpa_supplicant_1_interface_call_nancancel_publish_sync(mWpaSupplicant.iface.get(), PublishId, nullptr, &err.GetReceiver());
// TODO #40814: make sure that the callers do check the return values. This doesn't seem to be happening now.
VerifyOrReturnError(
result, CHIP_ERROR_INTERNAL,
ChipLogError(DeviceLayer, "WiFi-PAF: Failed to Cancel Publish with Error: %s", err ? err->message : "unknown error"));
return CHIP_NO_ERROR;
}
/*
NAN-USD Service Protocol Type: ref: Table 58 of Wi-Fi Aware Specificaiton
*/
void ConnectivityManagerImpl::OnDiscoveryResult(GVariant * discov_info)
{
ChipLogProgress(Controller, "WiFi-PAF: OnDiscoveryResult");
uint32_t subscribe_id;
uint32_t peer_publish_id;
uint8_t peer_addr[kMACAddressLength];
uint32_t srv_proto_type;
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
if (g_variant_n_children(discov_info) == 0)
{
return;
}
/*
Read the data from dbus
*/
GAutoPtr<GVariant> dataValue;
GVariant * value;
value = g_variant_lookup_value(discov_info, "subscribe_id", G_VARIANT_TYPE_UINT32);
dataValue.reset(value);
g_variant_get(dataValue.get(), "u", &subscribe_id);
value = g_variant_lookup_value(discov_info, "publish_id", G_VARIANT_TYPE_UINT32);
dataValue.reset(value);
g_variant_get(dataValue.get(), "u", &peer_publish_id);
char addr_str[20];
char * paddr;
value = g_variant_lookup_value(discov_info, "peer_addr", G_VARIANT_TYPE_STRING);
dataValue.reset(value);
g_variant_get(dataValue.get(), "&s", &paddr);
chip::Platform::CopyString(addr_str, paddr);
VerifyOrReturn(ParseMacAddress(addr_str, peer_addr),
ChipLogError(DeviceLayer, "WiFi-PAF: OnDiscoveryResult: malformed peer_addr '%s'", addr_str));
value = g_variant_lookup_value(discov_info, "srv_proto_type", G_VARIANT_TYPE_UINT32);
dataValue.reset(value);
g_variant_get(dataValue.get(), "u", &srv_proto_type);
// Read the ssi
gsize bufferLen;
value = g_variant_lookup_value(discov_info, "ssi", G_VARIANT_TYPE_BYTESTRING);
dataValue.reset(value);
auto ssibuf = g_variant_get_fixed_array(dataValue.get(), &bufferLen, sizeof(uint8_t));
VerifyOrReturn(bufferLen >= sizeof(PAFPublishSSI), ChipLogError(DeviceLayer, "WiFi-PAF: DiscoveryResult SSI too short"));
auto pPublishSSI = reinterpret_cast<const PAFPublishSSI *>(ssibuf);
GetWiFiPAF()->SetWiFiPAFState(WiFiPAF::State::kConnected);
/*
Error Checking
*/
WiFiPAFSession sessionInfo = { .discriminator = pPublishSSI->DevInfo };
WiFiPAFLayer & WiFiPafLayer = WiFiPAFLayer::GetWiFiPAFLayer();
auto pPafInfo = WiFiPafLayer.GetPAFInfo(PafInfoAccess::kAccDisc, sessionInfo);
if (pPafInfo == nullptr)
{
ChipLogError(DeviceLayer, "WiFi-PAF: DiscoveryResult, no valid session with discriminator: %u", pPublishSSI->DevInfo);
return;
}
if (pPafInfo->role != WiFiPAF::WiFiPafRole::kWiFiPafRole_Subscriber)
{
ChipLogError(DeviceLayer, "WiFi-PAF: DiscoveryResult received for non-subscriber session");
return;
}
// The connect and background-scan discovery handlers are both connected to the
// shared "nandiscovery-result" signal, so GLib delivers every discovery to both.
// Act only on discovery for THIS session's own subscribe (pPafInfo->id was set to
// the connect subscribe_id in _WiFiPAFSubscribe); a (possibly stale) background-
// scan subscribe cross-fires here and must not hijack or overwrite the connect
// session — before or after it is established. Matching on subscribe_id rather
// than on "peer_id already set" keeps a legitimate re-discovery on the connect's
// own (re)subscribe working for the controller path.
if (pPafInfo->id != subscribe_id)
{
ChipLogDetail(DeviceLayer, "WiFi-PAF: DiscoveryResult, subscribe_id=%u not for this session (id=%u), ignoring",
subscribe_id, pPafInfo->id);
return;
}
if (pPafInfo->peer_id != UINT32_MAX)
{
// Reentrance: a duplicate discovery for our own, already-established session.
ChipLogError(DeviceLayer, "WiFi-PAF: DiscoveryResult, reentrance, subscribe_id: %u", subscribe_id);
return;
}
if (srv_proto_type != nan_service_protocol_type::NAN_SRV_PROTO_CSA_MATTER)
{
ChipLogError(DeviceLayer, "WiFi-PAF: DiscoveryResult, Incorrect Protocol Type: %u, exp: %u", srv_proto_type,
nan_service_protocol_type::NAN_SRV_PROTO_CSA_MATTER);
return;
}
/*
Set the PAF session information
*/
ChipLogProgress(DeviceLayer, "WiFi-PAF: DiscoveryResult, set PafInfo, whose nodeId: %" PRIu64, pPafInfo->nodeId);
ChipLogProgress(DeviceLayer, "\t (subscribe_id, peer_publish_id): (%u, %u)", subscribe_id, peer_publish_id);
ChipLogProgress(DeviceLayer, "\t peer_addr: [%02x:%02x:%02x:%02x:%02x:%02x]", peer_addr[0], peer_addr[1], peer_addr[2],
peer_addr[3], peer_addr[4], peer_addr[5]);
ChipLogProgress(DeviceLayer, "\t DevInfo: %x", pPublishSSI->DevInfo);
pPafInfo->role = WiFiPAF::WiFiPafRole::kWiFiPafRole_Subscriber;
pPafInfo->id = subscribe_id;
pPafInfo->peer_id = peer_publish_id;
memcpy(pPafInfo->peer_addr, peer_addr, kMACAddressLength);
/*
Indicate the connection event
*/
ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoWiFiPAFConnected };
memcpy(&event.CHIPoWiFiPAFReceived.SessionInfo, pPafInfo, sizeof(chip::WiFiPAF::WiFiPAFSession));
PlatformMgr().PostEventOrDie(&event);
}
void ConnectivityManagerImpl::OnReplied(GVariant * reply_info)
{
ChipLogProgress(Controller, "WiFi-PAF: OnReplied");
// Seeing a peer again is the first evidence that NAN survived a station association.
PafChannelNoteNanActivity();
uint32_t publish_id;
uint32_t peer_subscribe_id;
uint8_t peer_addr[kMACAddressLength];
uint32_t srv_proto_type;
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
if (g_variant_n_children(reply_info) == 0)
{
return;
}
/*
Read the data from dbus
*/
GAutoPtr<GVariant> dataValue;
GVariant * value;
value = g_variant_lookup_value(reply_info, "publish_id", G_VARIANT_TYPE_UINT32);
dataValue.reset(value);
g_variant_get(dataValue.get(), "u", &publish_id);
value = g_variant_lookup_value(reply_info, "subscribe_id", G_VARIANT_TYPE_UINT32);
dataValue.reset(value);
g_variant_get(dataValue.get(), "u", &peer_subscribe_id);
char addr_str[20];
char * paddr;
value = g_variant_lookup_value(reply_info, "peer_addr", G_VARIANT_TYPE_STRING);
dataValue.reset(value);
g_variant_get(dataValue.get(), "&s", &paddr);
chip::Platform::CopyString(addr_str, paddr);
VerifyOrReturn(ParseMacAddress(addr_str, peer_addr),
ChipLogError(DeviceLayer, "WiFi-PAF: OnReplied: malformed peer_addr '%s'", addr_str));
value = g_variant_lookup_value(reply_info, "srv_proto_type", G_VARIANT_TYPE_UINT32);
dataValue.reset(value);
g_variant_get(dataValue.get(), "u", &srv_proto_type);
// Read the ssi
gsize bufferLen;
value = g_variant_lookup_value(reply_info, "ssi", G_VARIANT_TYPE_BYTESTRING);
dataValue.reset(value);
auto ssibuf = g_variant_get_fixed_array(dataValue.get(), &bufferLen, sizeof(uint8_t));
VerifyOrReturn(bufferLen >= sizeof(PAFPublishSSI), ChipLogError(DeviceLayer, "WiFi-PAF: OnReplied SSI too short"));
auto pPublishSSI = reinterpret_cast<const PAFPublishSSI *>(ssibuf);
/*
Error Checking
*/
uint16_t SetupDiscriminator;
TEMPORARY_RETURN_IGNORED DeviceLayer::GetCommissionableDataProvider()->GetSetupDiscriminator(SetupDiscriminator);
if ((pPublishSSI->DevInfo != SetupDiscriminator) || (srv_proto_type != nan_service_protocol_type::NAN_SRV_PROTO_CSA_MATTER))
{
ChipLogProgress(DeviceLayer, "WiFi-PAF: OnReplied, mismatched discriminator, got %u, ours: %u", pPublishSSI->DevInfo,
SetupDiscriminator);
return;
}
WiFiPAFSession sessionInfo = { .id = publish_id };
WiFiPAFLayer & WiFiPafLayer = WiFiPAFLayer::GetWiFiPAFLayer();
auto pPafInfo = WiFiPafLayer.GetPAFInfo(PafInfoAccess::kAccSessionId, sessionInfo);
if (pPafInfo == nullptr)
{
ChipLogError(DeviceLayer, "WiFi-PAF: OnReplied, no valid session with publish_id: %u", publish_id);
return;
}
if (pPafInfo->role != WiFiPAF::WiFiPafRole::kWiFiPafRole_Publisher)
{
ChipLogError(DeviceLayer, "WiFi-PAF: OnReplied received for non-publisher session");
return;
}
if ((pPafInfo->peer_id == peer_subscribe_id) && !memcmp(pPafInfo->peer_addr, peer_addr, kMACAddressLength))
{
ChipLogError(DeviceLayer, "WiFi-PAF: OnReplied, reentrance, publish_id: %u ", publish_id);
return;
}
/*
Set the PAF session information
*/
ChipLogProgress(DeviceLayer, "WiFi-PAF: OnReplied, set PafInfo, whose nodeId: %" PRIu64, pPafInfo->nodeId);
ChipLogProgress(DeviceLayer, "\t (publish_id, peer_subscribe_id): (%u, %u)", publish_id, peer_subscribe_id);
ChipLogProgress(DeviceLayer, "\t peer_addr: [%02x:%02x:%02x:%02x:%02x:%02x]", peer_addr[0], peer_addr[1], peer_addr[2],
peer_addr[3], peer_addr[4], peer_addr[5]);
ChipLogProgress(DeviceLayer, "\t DevInfo: %x", pPublishSSI->DevInfo);
pPafInfo->role = WiFiPAF::WiFiPafRole::kWiFiPafRole_Publisher;
pPafInfo->id = publish_id;
pPafInfo->peer_id = peer_subscribe_id;
memcpy(pPafInfo->peer_addr, peer_addr, kMACAddressLength);
auto handleInitiated = [](intptr_t arg) {
WiFiPAF::WiFiPAFSession * pInfo = reinterpret_cast<WiFiPAF::WiFiPAFSession *>(arg);
LogErrorOnFailure(WiFiPAFLayer::GetWiFiPAFLayer().HandleTransportConnectionInitiated(*pInfo));
};
LogErrorOnFailure(PlatformMgr().ScheduleWork(handleInitiated, reinterpret_cast<intptr_t>(pPafInfo)));
}
void ConnectivityManagerImpl::OnNanReceive(GVariant * obj)
{
// An inbound frame proves the NAN path is usable, so it counts as recovery evidence too.
PafChannelNoteNanActivity();
if (g_variant_n_children(obj) == 0)
{
return;
}
// Read the rx_info
WiFiPAF::WiFiPAFSession RxInfo;
GAutoPtr<GVariant> dataValue;
GVariant * value;
value = g_variant_lookup_value(obj, "id", G_VARIANT_TYPE_UINT32);
dataValue.reset(value);
g_variant_get(dataValue.get(), "u", &RxInfo.id);
value = g_variant_lookup_value(obj, "peer_id", G_VARIANT_TYPE_UINT32);
dataValue.reset(value);
g_variant_get(dataValue.get(), "u", &RxInfo.peer_id);
char addr_str[20];
char * paddr;
value = g_variant_lookup_value(obj, "peer_addr", G_VARIANT_TYPE_STRING);
dataValue.reset(value);
g_variant_get(dataValue.get(), "&s", &paddr);
chip::Platform::CopyString(addr_str, paddr);
VerifyOrReturn(ParseMacAddress(addr_str, RxInfo.peer_addr),
ChipLogError(DeviceLayer, "WiFi-PAF: OnNanReceive: malformed peer_addr '%s'", addr_str));
// Read the rx_data
gsize bufferLen;
System::PacketBufferHandle buf;
value = g_variant_lookup_value(obj, "ssi", G_VARIANT_TYPE_BYTESTRING);
dataValue.reset(value);
auto rxbuf = g_variant_get_fixed_array(dataValue.get(), &bufferLen, sizeof(uint8_t));
ChipLogProgress(DeviceLayer, "WiFi-PAF: " WPA_SUPPLICANT_CLIENT_LOG_PREFIX "nan-rx: [len: %" G_GSIZE_FORMAT "]", bufferLen);
buf = System::PacketBufferHandle::NewWithData(rxbuf, bufferLen);
// Post an event to the Chip queue to deliver the data into the Chip stack.
ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoWiFiPAFReceived,
.CHIPoWiFiPAFReceived = { .Data = std::move(buf).UnsafeRelease() } };
memcpy(&event.CHIPoWiFiPAFReceived.SessionInfo, &RxInfo, sizeof(WiFiPAF::WiFiPAFSession));
PlatformMgr().PostEventOrDie(&event);
}
void ConnectivityManagerImpl::OnNanPublishTerminated(guint public_id, gchar * reason)
{
ChipLogProgress(Controller, "WiFi-PAF: Publish terminated (%u, %s)", public_id, reason);
WiFiPAFSession sessionInfo = { .id = public_id };
WiFiPAFLayer & WiFiPafLayer = WiFiPAFLayer::GetWiFiPAFLayer();
auto pPafInfo = WiFiPafLayer.GetPAFInfo(PafInfoAccess::kAccSessionId, sessionInfo);
if (pPafInfo == nullptr)
{
ChipLogError(DeviceLayer, "WiFi-PAF: OnNanPublishTerminated, no valid session with publish_id: %u", public_id);
return;
}
if (pPafInfo->role != WiFiPAF::WiFiPafRole::kWiFiPafRole_Publisher)
{
ChipLogError(DeviceLayer, "WiFi-PAF: OnNanPublishTerminated received for non-publisher session");
return;
}
TEMPORARY_RETURN_IGNORED WiFiPafLayer.RmPafSession(PafInfoAccess::kAccSessionId, sessionInfo);
}
void ConnectivityManagerImpl::OnNanSubscribeTerminated(guint subscribe_id, gchar * reason)
{
ChipLogProgress(Controller, "WiFi-PAF: Subscription terminated (%u, %s)", subscribe_id, reason);
WiFiPAFSession sessionInfo = { .id = subscribe_id };
WiFiPAFLayer & WiFiPafLayer = WiFiPAFLayer::GetWiFiPAFLayer();
auto pPafInfo = WiFiPafLayer.GetPAFInfo(PafInfoAccess::kAccSessionId, sessionInfo);
if (pPafInfo == nullptr)
{
ChipLogError(DeviceLayer, "WiFi-PAF: OnNanSubscribeTerminated, no valid session with subscribe_id: %u", subscribe_id);
return;
}
if (pPafInfo->role != WiFiPAF::WiFiPafRole::kWiFiPafRole_Subscriber)
{
ChipLogError(DeviceLayer, "WiFi-PAF: OnNanSubscribeTerminated received for non-subscriber session");
return;
}
TEMPORARY_RETURN_IGNORED WiFiPafLayer.RmPafSession(PafInfoAccess::kAccSessionId, sessionInfo);
/*
Indicate the connection event
*/
ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoWiFiPAFCancelConnect };
PlatformMgr().PostEventOrDie(&event);
}
void ConnectivityManagerImpl::_WiFiPAFSetParam(const WiFiPAFAdvertiseParam & pafAdvParam)
{
mPafAdvParam.freq_list_len = pafAdvParam.freq_list_len;
mPafAdvParam.freq_list = std::make_unique<uint16_t[]>(mPafAdvParam.freq_list_len);
for (size_t i = 0; i < mPafAdvParam.freq_list_len; i++)
{
mPafAdvParam.freq_list[i] = pafAdvParam.freq_list[i];
}
}
CHIP_ERROR ConnectivityManagerImpl::_SetWiFiPAFAdvertisingEnabled(bool enabled, uint32_t & publishId)
{
if (enabled)
{
VerifyOrReturnError(mPafAdvParam.freq_list_len > 0, CHIP_ERROR_INCORRECT_STATE);
auto res = WiFiPAFPublish(mPafAdvParam);
if ((res == CHIP_NO_ERROR) && (mPafAdvParam.publish_id != kUndefinedWiFiPafSessionId))
{
publishId = mPafAdvParam.publish_id;
}
return res;
}
// Cancel paf_publish, publish_id should be valid
VerifyOrReturnError((publishId != 0) && (publishId != WiFiPAF::kUndefinedWiFiPafSessionId), CHIP_ERROR_INCORRECT_STATE);
return WiFiPAFCancelPublish(publishId);
}
CHIP_ERROR ConnectivityManagerImpl::_WiFiPAFSubscribe(const uint16_t & connDiscriminator, void * appState,
OnConnectionCompleteFunct onSuccess, OnConnectionErrorFunct onError)
{
CHIP_ERROR result = StartWiFiManagementSync();
VerifyOrReturnError(result == CHIP_NO_ERROR, result);
ChipLogProgress(Controller, "WiFi-PAF: Try to subscribe the NAN-USD devices");
guint subscribe_id;
GAutoPtr<GError> err;
enum nan_service_protocol_type srv_proto_type = nan_service_protocol_type::NAN_SRV_PROTO_CSA_MATTER;
uint8_t is_active = 1;
unsigned int ttl = CHIP_DEVICE_CONFIG_WIFIPAF_DISCOVERY_TIMEOUT_SECS;
unsigned int freq = (mApFreq == 0) ? CHIP_DEVICE_CONFIG_WIFIPAF_24G_DEFAUTL_CHNL : mApFreq;
unsigned int ssi_len = sizeof(struct PAFPublishSSI);
struct PAFPublishSSI PafPublish_ssi;
mAppState = appState;
PafPublish_ssi.DevOpCode = 0;
PafPublish_ssi.DevInfo = connDiscriminator;
if (DeviceLayer::GetDeviceInstanceInfoProvider()->GetProductId(PafPublish_ssi.ProductId) != CHIP_NO_ERROR)
{
PafPublish_ssi.ProductId = 0;
}
if (DeviceLayer::GetDeviceInstanceInfoProvider()->GetVendorId(PafPublish_ssi.VendorId) != CHIP_NO_ERROR)
{
PafPublish_ssi.VendorId = 0;
}
GVariant * ssi_array_variant = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, &PafPublish_ssi, ssi_len, sizeof(guint8));
if (ssi_array_variant == nullptr)
{
ChipLogProgress(DeviceLayer, "WiFi-PAF: ssi_array_variant is NULL ");
return CHIP_ERROR_INTERNAL;
}
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
GVariantBuilder builder;
GVariant * args = nullptr;
g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add(&builder, "{sv}", "srv_name", g_variant_new_string(srv_name));
g_variant_builder_add(&builder, "{sv}", "srv_proto_type", g_variant_new_byte(srv_proto_type));
g_variant_builder_add(&builder, "{sv}", "active", g_variant_new_boolean(is_active));
g_variant_builder_add(&builder, "{sv}", "ttl", g_variant_new_uint16(ttl));
g_variant_builder_add(&builder, "{sv}", "freq", g_variant_new_uint16(freq));
g_variant_builder_add(&builder, "{sv}", "ssi", ssi_array_variant);
args = g_variant_builder_end(&builder);
wpa_supplicant_1_interface_call_nansubscribe_sync(mWpaSupplicant.iface.get(), args, &subscribe_id, nullptr, &err.GetReceiver());
ChipLogProgress(DeviceLayer, "WiFi-PAF: subscribe_id: [%u], freq: %u", subscribe_id, freq);
mPendingConnectSubscribeId = subscribe_id;
mOnPafSubscribeComplete = onSuccess;
mOnPafSubscribeError = onError;
WiFiPAFSession sessionInfo = { .discriminator = PafPublish_ssi.DevInfo };
WiFiPAFLayer & WiFiPafLayer = WiFiPAFLayer::GetWiFiPAFLayer();
auto pPafInfo = WiFiPafLayer.GetPAFInfo(PafInfoAccess::kAccDisc, sessionInfo);
if (pPafInfo != nullptr)
{
pPafInfo->id = subscribe_id;
pPafInfo->role = WiFiPAF::WiFiPafRole::kWiFiPafRole_Subscriber;
}
return CHIP_NO_ERROR;
}
CHIP_ERROR ConnectivityManagerImpl::_WiFiPAFCancelSubscribe(uint32_t SubscribeId)
{
GAutoPtr<GError> err;
ChipLogProgress(DeviceLayer, "WiFi-PAF: cancel subscribe_id: %d ! ", SubscribeId);
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
if (!mWpaSupplicant.iface)
{
ChipLogError(DeviceLayer, "WiFi-PAF: Skip D-Bus 'cancel subscribe' call since wpa_supplicant is not ready");
return CHIP_ERROR_INTERNAL;
}
gboolean result = wpa_supplicant_1_interface_call_nancancel_subscribe_sync(mWpaSupplicant.iface.get(), SubscribeId, nullptr,
&err.GetReceiver());
if (!result)
{
ChipLogError(DeviceLayer, "WiFi-PAF: Failed to Cancel Subscribe with Error: %s", err ? err->message : "unknown error");
return CHIP_ERROR_INTERNAL;
}
return CHIP_NO_ERROR;
}
CHIP_ERROR ConnectivityManagerImpl::_WiFiPAFCancelIncompleteSubscribe()
{
mOnPafSubscribeComplete = nullptr;
mOnPafSubscribeError = nullptr;
return CHIP_NO_ERROR;
}
CHIP_ERROR ConnectivityManagerImpl::_WiFiPAFSend(const WiFiPAF::WiFiPAFSession & TxInfo, System::PacketBufferHandle && msgBuf)
{
ChipLogProgress(Controller, "WiFi-PAF: sending PAF Follow-up packets, (%zu)", msgBuf->DataLength());
CHIP_ERROR ret = CHIP_NO_ERROR;
if (msgBuf.IsNull())
{
ChipLogError(Controller, "WiFi-PAF: Invalid Packet (%zu)", msgBuf->DataLength());
return CHIP_ERROR_INVALID_ARGUMENT;
}
// Ensure outgoing message fits in a single contiguous packet buffer, as currently required by the
// message fragmentation and reassembly engine.
if (msgBuf->HasChainedBuffer())
{
msgBuf->CompactHead();
if (msgBuf->HasChainedBuffer())
{
ret = CHIP_ERROR_OUTBOUND_MESSAGE_TOO_BIG;
ChipLogError(Controller, "WiFi-PAF: Outbound message too big (%zu), skip temporally", msgBuf->DataLength());
return ret;
}
}
// Send the packets
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
// The interface can go away between a send being queued and it being driven.
VerifyOrReturnError(mWpaSupplicant.iface, CHIP_ERROR_INCORRECT_STATE);
GAutoPtr<GError> err;
gchar peer_mac[18];
snprintf(peer_mac, sizeof(peer_mac), "%02x:%02x:%02x:%02x:%02x:%02x", TxInfo.peer_addr[0], TxInfo.peer_addr[1],
TxInfo.peer_addr[2], TxInfo.peer_addr[3], TxInfo.peer_addr[4], TxInfo.peer_addr[5]);
GVariant * ssi_array_variant =
g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, msgBuf->Start(), msgBuf->DataLength(), sizeof(guint8));
if (ssi_array_variant == nullptr)
{
ChipLogProgress(DeviceLayer, "WiFi-PAF: ssi_array_variant is NULL ");
return CHIP_ERROR_INTERNAL;
}
GVariantBuilder builder;
GVariant * args = nullptr;
g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add(&builder, "{sv}", "handle", g_variant_new_uint32(TxInfo.id));
g_variant_builder_add(&builder, "{sv}", "req_instance_id", g_variant_new_uint32(TxInfo.peer_id));
g_variant_builder_add(&builder, "{sv}", "peer_addr", g_variant_new_string(peer_mac));
g_variant_builder_add(&builder, "{sv}", "ssi", ssi_array_variant);
args = g_variant_builder_end(&builder);
gboolean result =
wpa_supplicant_1_interface_call_nantransmit_sync(mWpaSupplicant.iface.get(), args, nullptr, &err.GetReceiver());
if (!result)
{
ChipLogError(DeviceLayer, "WiFi-PAF: Failed to send message: %s", err == nullptr ? "unknown error" : err->message);
}
ChipLogProgress(Controller, "WiFi-PAF: Outbound message (%zu) done", msgBuf->DataLength());
// Post an event to the Chip queue to deliver the data into the Chip stack.
ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoWiFiPAFWriteDone };
memcpy(&event.CHIPoWiFiPAFReceived.SessionInfo, &TxInfo, sizeof(chip::WiFiPAF::WiFiPAFSession));
event.CHIPoWiFiPAFReceived.result = result;
PlatformMgr().PostEventOrDie(&event);
return ret;
}
CHIP_ERROR ConnectivityManagerImpl::_WiFiPAFShutdown(uint32_t id, WiFiPAF::WiFiPafRole role)
{
VerifyOrReturnError(((id != kUndefinedWiFiPafSessionId) && (id != 0)), CHIP_ERROR_INTERNAL);
switch (role)
{
case WiFiPAF::WiFiPafRole::kWiFiPafRole_Publisher:
return _WiFiPAFCancelPublish(id);
case WiFiPAF::WiFiPafRole::kWiFiPafRole_Subscriber:
return _WiFiPAFCancelSubscribe(id);
}
return CHIP_ERROR_INTERNAL;
}
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONING_PROXY
static uint16_t FreqToBand(uint32_t freq)
{
// 2.4 GHz: channels 1–13 (2412–2472 MHz) plus channel 14 (2484 MHz).
if ((freq >= 2412 && freq <= 2472) || freq == 2484)
return static_cast<uint16_t>(0x0001u); // k2g4
// 5 GHz: main range plus the two additional channels above the main range.
if ((freq >= 5035 && freq <= 5945) || freq == 5960 || freq == 5980)
return static_cast<uint16_t>(0x0004u); // k5g
return 0;
}
// Context for dispatching background-scan discovery results onto the CHIP event loop.
namespace {
struct BgScanWorkCtx
{
chip::DeviceLayer::ConnectivityManagerImpl::BgScanDiscoveryCallback cb;
void * cbCtx;
chip::DeviceLayer::NanPeerInfo peer;
};
} // namespace
void ConnectivityManagerImpl::ConnectScanSignals()
{
// Connected before NANSubscribe, not after: discovery is dispatched on the GLib
// main-loop thread, which runs while this thread blocks inside the synchronous
// NANSubscribe call, so a fast responder can emit nandiscovery-result before that
// call returns and a handler connected afterwards would never see it. The caller
// holds mWpaSupplicantMutex across the subscribe and ScanDiscoveryResult takes the
// same mutex, so an early result waits until the new subscribe_id has been stored.
if (!mWpaSupplicant.iface)
return;
mScanSignalIds[0] = g_signal_connect(mWpaSupplicant.iface.get(), "nandiscovery-result",
G_CALLBACK(+[](WpaSupplicant1Interface * proxy, GVariant * obj,
ConnectivityManagerImpl * self) { return self->ScanDiscoveryResult(obj); }),
this);
mScanSignalIds[1] = g_signal_connect(mWpaSupplicant.iface.get(), "nanreceive",
G_CALLBACK(+[](WpaSupplicant1Interface * proxy, GVariant * obj,
ConnectivityManagerImpl * self) { return self->ScanNanReceive(obj); }),
this);
mScanSignalIds[2] = g_signal_connect(
mWpaSupplicant.iface.get(), "nansubscribe-terminated",
G_CALLBACK(+[](WpaSupplicant1Interface * proxy, guint term_subscribe_id, gchar * reason, ConnectivityManagerImpl * self) {
return self->ScanNanSubscribeTerminated(term_subscribe_id, reason);
}),
this);
}
void ConnectivityManagerImpl::DisconnectScanSignalsLocked()
{
if (!mWpaSupplicant.iface)
return;
GObject * obj = G_OBJECT(mWpaSupplicant.iface.get());
for (gulong & id : mScanSignalIds)
{
if (id != 0)
{
g_signal_handler_disconnect(obj, id);
id = 0;
}
}
}
void ConnectivityManagerImpl::DisconnectScanSignals()
{
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
DisconnectScanSignalsLocked();
}
// Scan for Matter PAF devices, but don't connect
void ConnectivityManagerImpl::ScanDiscoveryResult(GVariant * discov_info)
{
uint32_t peer_publish_id;
uint8_t peer_addr[6];
uint32_t srv_proto_type;
uint32_t subscribe_id;
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
if (g_variant_n_children(discov_info) == 0)
{
return;
}
// Read the data from dbus
GAutoPtr<GVariant> dataValue;
GVariant * value;
value = g_variant_lookup_value(discov_info, "subscribe_id", G_VARIANT_TYPE_UINT32);
dataValue.reset(value);
g_variant_get(dataValue.get(), "u", &subscribe_id);
ChipLogDetail(DeviceLayer, "ScanDiscoveryResult: subscribe_id=%u", subscribe_id);
// Ignore results from orphaned wpa_supplicant subscriptions (e.g. from
// previous process runs that weren't cancelled). Only accept results from
// the currently active scan subscription.
{
uint32_t expected = (mBgScanCb != nullptr) ? mBgScanSubscribeId : mActiveScanSubscribeId;
if (expected == 0 || subscribe_id != expected)
{
ChipLogProgress(DeviceLayer, "ScanDiscoveryResult: ignoring stale subscribe_id=%u (expected=%u)", subscribe_id,
expected);
return;
}
}
value = g_variant_lookup_value(discov_info, "publish_id", G_VARIANT_TYPE_UINT32);
dataValue.reset(value);
g_variant_get(dataValue.get(), "u", &peer_publish_id);
char addr_str[20];
char * paddr;
value = g_variant_lookup_value(discov_info, "peer_addr", G_VARIANT_TYPE_STRING);
dataValue.reset(value);
g_variant_get(dataValue.get(), "&s", &paddr);
chip::Platform::CopyString(addr_str, paddr);
VerifyOrReturn(ParseMacAddress(addr_str, peer_addr),
ChipLogError(DeviceLayer, "WiFi-PAF: ScanDiscoveryResult: malformed peer_addr '%s'", addr_str));
value = g_variant_lookup_value(discov_info, "srv_proto_type", G_VARIANT_TYPE_UINT32);
dataValue.reset(value);
g_variant_get(dataValue.get(), "u", &srv_proto_type);
// Read the variable length Extended data from the SSI, as per R1.5 Section 5.4.2.6.3
size_t bufferLen;
value = g_variant_lookup_value(discov_info, "ssi", G_VARIANT_TYPE_BYTESTRING);
dataValue.reset(value);
auto ssibuf = g_variant_get_fixed_array(dataValue.get(), &bufferLen, sizeof(uint8_t));
VerifyOrReturn(bufferLen >= sizeof(PAFPublishSSI), ChipLogError(DeviceLayer, "WiFi-PAF: ScanDiscoveryResult SSI too short"));
auto pPublishSSI = reinterpret_cast<const PAFPublishSSI *>(ssibuf);
NanPeerInfo peer;
peer.vid = pPublishSSI->VendorId;
peer.pid = pPublishSSI->ProductId;
peer.opcode = pPublishSSI->DevOpCode;
peer.discriminator = pPublishSSI->DevInfo;
std::memcpy(peer.mac, peer_addr, sizeof(peer_addr));
// ExtendedData (optional, core R1.4.2 5.4.2.6.3) follows the mandatory
// PAFPublishSSI struct.
if (bufferLen > sizeof(PAFPublishSSI))
{
const auto * bytes = static_cast<const uint8_t *>(ssibuf);
peer.storage.assign(bytes + sizeof(PAFPublishSSI), bytes + bufferLen);
peer.hasExtendedData = true;
}
// Set WiFiBand from the frequency used when the scan was started.
peer.band = FreqToBand(mScanFreq);
// The bg-scan callback uses SystemLayer and cluster APIs which require
// the CHIP stack lock. ScanDiscoveryResult runs on a GLib/dbus thread,
// so schedule the callback on the CHIP event loop instead of calling directly.
if (mBgScanCb != nullptr)
{
auto * workCtx = new BgScanWorkCtx{ mBgScanCb, mBgScanCbCtx, peer };
CHIP_ERROR schedErr = chip::DeviceLayer::PlatformMgr().ScheduleWork(
+[](intptr_t arg) {
auto * w = reinterpret_cast<BgScanWorkCtx *>(arg);
w->cb(w->cbCtx, w->peer);
delete w;
},
reinterpret_cast<intptr_t>(workCtx));
if (schedErr != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "ScanDiscoveryResult: ScheduleWork failed: %" CHIP_ERROR_FORMAT, schedErr.Format());
delete workCtx;
}
}
// A peer already in the window is not logged again: a single device produces a
// discovery event every few hundred ms
if (std::find(mNanScanPeers.begin(), mNanScanPeers.end(), peer) != mNanScanPeers.end())
{
return;
}
// Rolling window. A background scan runs with no timeout, so the window is bounded.
// Once full, a new peer overwrites the oldest rather than being dropped, so a device.
if (mNanScanPeers.size() < kMaxScanPeers)
{
mNanScanPeers.push_back(peer);
}
else
{
mNanScanPeers[mNanScanPeersNext] = peer;
mNanScanPeersNext = (mNanScanPeersNext + 1) % kMaxScanPeers;
}
ChipLogProgress(DeviceLayer, "Discovered Device: %s() Subscribe_id:%u peer_publish_id:%u srv_proto_type:%u", __func__,
subscribe_id, peer_publish_id, srv_proto_type);
ChipLogProgress(DeviceLayer, "\tDiscriminator:%u Opcode:%u PID:0x%X VID:0x%X", pPublishSSI->DevInfo, pPublishSSI->DevOpCode,
pPublishSSI->ProductId, pPublishSSI->VendorId);
ChipLogProgress(DeviceLayer, "\tpeer_addr: [%02x:%02x:%02x:%02x:%02x:%02x]", peer_addr[0], peer_addr[1], peer_addr[2],
peer_addr[3], peer_addr[4], peer_addr[5]);
ChipLogProgress(DeviceLayer, "\tSSI Buffer Len:%lu", bufferLen);
if (peer.hasExtendedData && !peer.storage.empty())
{
chip::ByteSpan s(peer.storage.data(), peer.storage.size());
constexpr size_t kMaxBytesToPrint = 20;
const size_t bytesToPrint = (s.size() > kMaxBytesToPrint) ? kMaxBytesToPrint : s.size();
char hexBuf[2 * kMaxBytesToPrint + 1] = { 0 };
CHIP_ERROR err =
chip::Encoding::BytesToHex(s.data(), bytesToPrint, hexBuf, sizeof(hexBuf), chip::Encoding::HexFlags::kUppercase);
if (err == CHIP_NO_ERROR)
{
ChipLogProgress(DeviceLayer, "\tExtendedData (%lu bytes, showing %lu): %s", static_cast<unsigned long>(s.size()),
static_cast<unsigned long>(bytesToPrint), hexBuf);
}
else
{
ChipLogError(DeviceLayer, "\tExtendedData, BytesToHex failed: %" CHIP_ERROR_FORMAT, err.Format());
}
}
else
{
ChipLogProgress(DeviceLayer, "\tNo Extended Data");
}
}
void ConnectivityManagerImpl::ScanNanReceive(GVariant * obj)
{
ChipLogError(DeviceLayer, "Commissioning Proxy: Unexpected ScanNanReceive");
}
void ConnectivityManagerImpl::ScanNanSubscribeTerminated(guint subscribe_id, gchar * reason)
{
ChipLogProgress(DeviceLayer, "Commissioning Proxy: Subscription terminated (%u, %s)", subscribe_id, reason);
// This handler shares the "nansubscribe-terminated" signal with the connect
// path (OnNanSubscribeTerminated). Act only on a subscribe_id that belongs to
// a scan; otherwise a terminating connect subscribe would remove the connect
// session's PAF entry out from under an in-flight ProxyConnect.
{
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
if (subscribe_id != mActiveScanSubscribeId && subscribe_id != mBgScanSubscribeId)
{
return;
}
}
WiFiPAFSession sessionInfo = { .id = subscribe_id };
WiFiPAFLayer & WiFiPafLayer = WiFiPAFLayer::GetWiFiPAFLayer();
TEMPORARY_RETURN_IGNORED WiFiPafLayer.RmPafSession(PafInfoAccess::kAccSessionId, sessionInfo);
}
CHIP_ERROR ConnectivityManagerImpl::WiFiPAFScan(uint8_t scanMaxTime, PafScanResultsCallback cb, void * cbContext)
{
// Read/modify the shared scan-callback state under the lock; ScanDiscoveryResult
// reads these members on the GLib/D-Bus thread under the same lock.
{
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
// Cannot start a one-shot scan while a background scan is running.
VerifyOrReturnError(mBgScanCb == nullptr, CHIP_ERROR_BUSY);
mScanCb = cb;
mScanCbContext = cbContext;
// Drop peers left over from a previous scan so this one-shot response
// reports only devices discovered within this scan window.
mNanScanPeers.clear();
mNanScanPeersNext = 0;
}
CHIP_ERROR result = StartWiFiManagementSync();
if (result != CHIP_NO_ERROR)
{
// Reset the scan-callback state set above so a transient WiFi-management
// startup failure does not leave mScanCb non-null forever, which would wedge
// every subsequent background scan into CHIP_ERROR_BUSY for the process life.
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
mScanCb = nullptr;
mScanCbContext = nullptr;
return result;
}
ChipLogProgress(DeviceLayer, "WiFiPAFScan: starting one-shot NAN discovery (maxTime=%us)", scanMaxTime);
guint subscribe_id;
GAutoPtr<GError> err;
enum nan_service_protocol_type srv_proto_type = nan_service_protocol_type::NAN_SRV_PROTO_CSA_MATTER;
uint8_t is_active = false; // passive subscribe = discovery mode
// Set the NAN TTL to match the chip-level scan window. A longer TTL leaves
// subscribe_id alive in wpa_supplicant after FinishWiFiPAFScan cancels it,
// causing stale nandiscovery-result events during the next scan that mask
// results from the new subscribe_id (wpa_supplicant is edge-triggered).
unsigned int ttl = scanMaxTime;
unsigned int freq = (mApFreq == 0) ? CHIP_DEVICE_CONFIG_WIFIPAF_24G_DEFAUTL_CHNL : mApFreq;
// Scoped so the failure handling after the block can call DisconnectScanSignals() /
// _WiFiPAFCancelSubscribe(), which take mWpaSupplicantMutex themselves.
CHIP_ERROR addErr = CHIP_NO_ERROR;
{
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
mScanFreq = static_cast<uint32_t>(freq);
ConnectScanSignals();
GVariantBuilder builder;
GVariant * args = nullptr;
g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add(&builder, "{sv}", "srv_name", g_variant_new_string(srv_name));
g_variant_builder_add(&builder, "{sv}", "srv_proto_type", g_variant_new_byte(srv_proto_type));
g_variant_builder_add(&builder, "{sv}", "active", g_variant_new_boolean(is_active));
g_variant_builder_add(&builder, "{sv}", "ttl", g_variant_new_uint16(ttl));
g_variant_builder_add(&builder, "{sv}", "freq", g_variant_new_uint16(freq));
g_variant_builder_add(&builder, "{sv}", "discovery_only", g_variant_new_boolean(TRUE));
args = g_variant_builder_end(&builder);
wpa_supplicant_1_interface_call_nansubscribe_sync(mWpaSupplicant.iface.get(), args, &subscribe_id, nullptr,
&err.GetReceiver());
if (err.get() != nullptr)
{
ChipLogError(DeviceLayer, "WiFiPAFScan: nansubscribe failed: %s", err->message);
ChipLogError(DeviceLayer, "WiFiPAFScan: Check wpa_supplicant supports discovery_only flag");
DisconnectScanSignalsLocked();
mScanCb = nullptr;
mScanCbContext = nullptr;
return CHIP_ERROR_INTERNAL;
}
ChipLogProgress(DeviceLayer, "WiFiPAFScan: subscribe_id=%u freq=%u", subscribe_id, freq);
mActiveScanSubscribeId = static_cast<uint32_t>(subscribe_id);
WiFiPAFSession sessionInfo = { .role = WiFiPafRole::kWiFiPafRole_Subscriber, .id = subscribe_id };
WiFiPAFLayer & WiFiPafLayer = WiFiPAFLayer::GetWiFiPAFLayer();
addErr = WiFiPafLayer.AddPafSession(PafInfoAccess::kAccSessionId, sessionInfo);
if (addErr != CHIP_NO_ERROR)
{
// The subscribe is already live in wpa_supplicant. Reset the scan state here
// and cancel the subscribe after the lock is released; leaving mScanCb set
// would wedge every later background scan into CHIP_ERROR_BUSY for the life of
// the process, and the orphaned subscribe would keep emitting
// nandiscovery-result events.
ChipLogError(DeviceLayer, "WiFiPAFScan: AddPafSession failed: %" CHIP_ERROR_FORMAT, addErr.Format());
DisconnectScanSignalsLocked();
mActiveScanSubscribeId = 0;
mScanFreq = 0;
mScanCb = nullptr;
mScanCbContext = nullptr;
}
else
{
auto pPafInfo = WiFiPafLayer.GetPAFInfo(PafInfoAccess::kAccSessionId, sessionInfo);
if (pPafInfo != nullptr)
{
pPafInfo->id = subscribe_id;
pPafInfo->role = WiFiPAF::WiFiPafRole::kWiFiPafRole_Subscriber;
}
}
}
if (addErr != CHIP_NO_ERROR)
{
CHIP_ERROR cancelErr = _WiFiPAFCancelSubscribe(subscribe_id);
if (cancelErr != CHIP_NO_ERROR)
{
ChipLogDetail(DeviceLayer, "WiFiPAFScan: CancelSubscribe(%u): %" CHIP_ERROR_FORMAT, subscribe_id, cancelErr.Format());
}
return addErr;
}
ChipLogProgress(DeviceLayer, "WiFiPAFScan: timeout in %d seconds on subscribe_id %u", scanMaxTime, subscribe_id);
auto * ctx = new ScanTimerCtx{ this, subscribe_id };
CHIP_ERROR timerErr = SystemLayer().StartTimer(
System::Clock::Milliseconds32(scanMaxTime * 1000),
+[](System::Layer *, void * context) {
auto * timerCtx = static_cast<ScanTimerCtx *>(context);
timerCtx->self->FinishWiFiPAFScan(timerCtx);
delete timerCtx;
},
ctx);
if (timerErr != CHIP_NO_ERROR)
{
// Nothing would ever call FinishWiFiPAFScan, so the subscribe, the signal handlers
// and mScanCb would all outlive this call. Tear the scan down here and free the
// context the timer would have owned.
ChipLogError(DeviceLayer, "WiFiPAFScan: StartTimer failed: %" CHIP_ERROR_FORMAT, timerErr.Format());
delete ctx;
{
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
mActiveScanSubscribeId = 0;
mScanFreq = 0;
mScanCb = nullptr;
mScanCbContext = nullptr;
}
DisconnectScanSignals();
CHIP_ERROR cancelErr = _WiFiPAFCancelSubscribe(subscribe_id);
if (cancelErr != CHIP_NO_ERROR)
{
ChipLogDetail(DeviceLayer, "WiFiPAFScan: CancelSubscribe(%u): %" CHIP_ERROR_FORMAT, subscribe_id, cancelErr.Format());
}
WiFiPAFSession failedSession = { .role = WiFiPafRole::kWiFiPafRole_Subscriber, .id = subscribe_id };
CHIP_ERROR rmErr = WiFiPAFLayer::GetWiFiPAFLayer().RmPafSession(PafInfoAccess::kAccSessionId, failedSession);
if (rmErr != CHIP_NO_ERROR)
{
ChipLogDetail(DeviceLayer, "WiFiPAFScan: RmPafSession: %" CHIP_ERROR_FORMAT, rmErr.Format());
}
return timerErr;
}
return CHIP_NO_ERROR;
}
void ConnectivityManagerImpl::FinishWiFiPAFScan(ScanTimerCtx * ctx)
{
ChipLogProgress(DeviceLayer, "FinishWiFiPAFScan: subscribe_id %u", ctx->subscribe_id);
DisconnectScanSignals();
// Hold the mutex while collecting and clearing mNanScanPeers so that a
// ScanDiscoveryResult running concurrently on the GLib/D-Bus thread cannot
// write to the set at the same time. Moving mActiveScanSubscribeId = 0
// inside the lock ensures any ScanDiscoveryResult that acquires the lock
// after us sees expected == 0 and correctly discards the late event.
std::vector<NanPeerInfo> results;
{
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
results.reserve(mNanScanPeers.size());
for (auto & p : mNanScanPeers)
results.push_back(p);
mNanScanPeers.clear();
mNanScanPeersNext = 0;
mActiveScanSubscribeId = 0;
mScanFreq = 0;
}
// Remove the session before the cancel below: the cancel emits nansubscribe-terminated,
// and OnNanSubscribeTerminated fails an in-flight connect for a session it still finds.
WiFiPAFSession sessionInfo = { .role = WiFiPafRole::kWiFiPafRole_Subscriber, .id = ctx->subscribe_id };
WiFiPAFLayer & WiFiPafLayer = WiFiPAFLayer::GetWiFiPAFLayer();
LogErrorOnFailure(WiFiPafLayer.RmPafSession(PafInfoAccess::kAccSessionId, sessionInfo));
if (mScanCb != nullptr)
mScanCb(mScanCbContext, results);
mScanCb = nullptr;
mScanCbContext = nullptr;
// The cancel is a synchronous D-Bus call that can wait behind other radio work, so it
// runs after the results are delivered rather than delaying them. A background scan
// started from the callback above gets CHIP_ERROR_BUSY, because mScanCb is still set there.
LogErrorOnFailure(_WiFiPAFCancelSubscribe(ctx->subscribe_id));
// NOTE: do not call _WiFiPAFCancelIncompleteSubscribe() here. That clears the
// connect-path subscribe-complete/error callbacks (mOnPafSubscribe*), which may
// belong to a ProxyConnectRequest subscribe still in flight. Cancelling this
// scan's subscribe_id above is sufficient to tear down the scan.
}
CHIP_ERROR ConnectivityManagerImpl::WiFiPAFStartBackgroundScan(BgScanDiscoveryCallback cb, void * cbCtx)
{
VerifyOrReturnError(cb != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
// Read/modify the shared scan-callback state under the lock; ScanDiscoveryResult
// reads these members on the GLib/D-Bus thread under the same lock.
{
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
VerifyOrReturnError(mScanCb == nullptr, CHIP_ERROR_BUSY);
// Already running — update callback and return success.
if (mBgScanCb != nullptr)
{
mBgScanCb = cb;
mBgScanCbCtx = cbCtx;
return CHIP_NO_ERROR;
}
}
CHIP_ERROR result = StartWiFiManagementSync();
VerifyOrReturnError(result == CHIP_NO_ERROR, result);
ChipLogProgress(DeviceLayer, "WiFiPAFStartBackgroundScan: starting continuous NAN discovery");
guint subscribe_id;
GAutoPtr<GError> err;
enum nan_service_protocol_type srv_proto_type = nan_service_protocol_type::NAN_SRV_PROTO_CSA_MATTER;
uint8_t is_active = false; // passive subscribe = discovery mode
unsigned int ttl = 0; // 0 = infinite in wpa_supplicant
unsigned int freq = (mApFreq == 0) ? CHIP_DEVICE_CONFIG_WIFIPAF_24G_DEFAUTL_CHNL : mApFreq;
// Scoped so the failure handling after the block can call _WiFiPAFCancelSubscribe(),
// which takes mWpaSupplicantMutex itself.
CHIP_ERROR addErr = CHIP_NO_ERROR;
{
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
mScanFreq = static_cast<uint32_t>(freq);
ConnectScanSignals();
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add(&builder, "{sv}", "srv_name", g_variant_new_string(srv_name));
g_variant_builder_add(&builder, "{sv}", "srv_proto_type", g_variant_new_byte(srv_proto_type));
g_variant_builder_add(&builder, "{sv}", "active", g_variant_new_boolean(is_active));
g_variant_builder_add(&builder, "{sv}", "ttl", g_variant_new_uint16(ttl));
g_variant_builder_add(&builder, "{sv}", "freq", g_variant_new_uint16(freq));
g_variant_builder_add(&builder, "{sv}", "discovery_only", g_variant_new_boolean(TRUE));
GVariant * args = g_variant_builder_end(&builder);
wpa_supplicant_1_interface_call_nansubscribe_sync(mWpaSupplicant.iface.get(), args, &subscribe_id, nullptr,
&err.GetReceiver());
if (err.get() != nullptr)
{
ChipLogError(DeviceLayer, "WiFiPAFStartBackgroundScan: nansubscribe failed: %s", err->message);
DisconnectScanSignalsLocked();
return CHIP_ERROR_INTERNAL;
}
ChipLogProgress(DeviceLayer, "WiFiPAFStartBackgroundScan: subscribe_id=%u freq=%u", subscribe_id, freq);
WiFiPAFSession sessionInfo = { .role = WiFiPafRole::kWiFiPafRole_Subscriber, .id = subscribe_id };
WiFiPAFLayer & WiFiPafLayer = WiFiPAFLayer::GetWiFiPAFLayer();
addErr = WiFiPafLayer.AddPafSession(PafInfoAccess::kAccSessionId, sessionInfo);
if (addErr != CHIP_NO_ERROR)
{
// mBgScanCb is still null here, so WiFiPAFStopBackgroundScan() would early-return
// and could never cancel this subscribe. Cancel it after the lock is released or
// it is orphaned in wpa_supplicant, where its stale nandiscovery-result events go
// on to mask results from the next subscribe_id (wpa_supplicant is edge-triggered).
ChipLogError(DeviceLayer, "WiFiPAFStartBackgroundScan: AddPafSession failed: %" CHIP_ERROR_FORMAT, addErr.Format());
DisconnectScanSignalsLocked();
mScanFreq = 0;
}
else
{
mBgScanCb = cb;
mBgScanCbCtx = cbCtx;
mBgScanSubscribeId = static_cast<uint32_t>(subscribe_id);
}
}
if (addErr != CHIP_NO_ERROR)
{
CHIP_ERROR cancelErr = _WiFiPAFCancelSubscribe(subscribe_id);
if (cancelErr != CHIP_NO_ERROR)
{
ChipLogDetail(DeviceLayer, "WiFiPAFStartBackgroundScan: CancelSubscribe(%u): %" CHIP_ERROR_FORMAT, subscribe_id,
cancelErr.Format());
}
return addErr;
}
return CHIP_NO_ERROR;
}
void ConnectivityManagerImpl::WiFiPAFStopBackgroundScan()
{
// Snapshot and clear the shared bg-scan state under the lock so a
// ScanDiscoveryResult running on the GLib/D-Bus thread (which reads these
// members under the same lock) cannot observe a half-torn-down scan. Once
// mBgScanCb/mBgScanSubscribeId are cleared, a late discovery is discarded.
uint32_t subscribeId = 0;
{
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
if (mBgScanCb == nullptr)
return;
subscribeId = mBgScanSubscribeId;
mBgScanCb = nullptr;
mBgScanCbCtx = nullptr;
mBgScanSubscribeId = 0;
mScanFreq = 0;
// Clear accumulated peers so they cannot leak into a subsequent one-shot
// scan's ProxyScanResponse (and so a long-lived bg scan does not grow the
// set without bound).
mNanScanPeers.clear();
mNanScanPeersNext = 0;
}
ChipLogProgress(DeviceLayer, "WiFiPAFStopBackgroundScan: stopping subscribe_id=%u", subscribeId);
// DisconnectScanSignals() and _WiFiPAFCancelSubscribe() each take
// mWpaSupplicantMutex internally, so they MUST be called without holding it.
DisconnectScanSignals();
CHIP_ERROR cancelErr = _WiFiPAFCancelSubscribe(subscribeId);
if (cancelErr != CHIP_NO_ERROR)
ChipLogDetail(DeviceLayer, "WiFiPAFStopBackgroundScan: CancelSubscribe: %" CHIP_ERROR_FORMAT, cancelErr.Format());
WiFiPAFSession sessionInfo = { .role = WiFiPafRole::kWiFiPafRole_Subscriber, .id = subscribeId };
WiFiPAFLayer & WiFiPafLayer = WiFiPAFLayer::GetWiFiPAFLayer();
CHIP_ERROR rmErr = WiFiPafLayer.RmPafSession(PafInfoAccess::kAccSessionId, sessionInfo);
if (rmErr != CHIP_NO_ERROR)
ChipLogDetail(DeviceLayer, "WiFiPAFStopBackgroundScan: RmPafSession: %" CHIP_ERROR_FORMAT, rmErr.Format());
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONING_PROXY
void ConnectivityManagerImpl::PostWpaInterfaceProxyReady()
{
g_signal_connect(mWpaSupplicant.iface.get(), "nanreplied",
G_CALLBACK(+[](WpaSupplicant1Interface * proxy, GVariant * obj, ConnectivityManagerImpl * self) {
return self->OnReplied(obj);
}),
this);
g_signal_connect(mWpaSupplicant.iface.get(), "nanreceive",
G_CALLBACK(+[](WpaSupplicant1Interface * proxy, GVariant * obj, ConnectivityManagerImpl * self) {
return self->OnNanReceive(obj);
}),
this);
g_signal_connect(
mWpaSupplicant.iface.get(), "nanpublish-terminated",
G_CALLBACK(+[](WpaSupplicant1Interface * proxy, guint term_publish_id, gchar * reason, ConnectivityManagerImpl * self) {
return self->OnNanPublishTerminated(term_publish_id, reason);
}),
this);
g_signal_connect(mWpaSupplicant.iface.get(), "nandiscovery-result",
G_CALLBACK(+[](WpaSupplicant1Interface * proxy, GVariant * obj, ConnectivityManagerImpl * self) {
return self->OnDiscoveryResult(obj);
}),
this);
g_signal_connect(
mWpaSupplicant.iface.get(), "nansubscribe-terminated",
G_CALLBACK(+[](WpaSupplicant1Interface * proxy, guint term_subscribe_id, gchar * reason, ConnectivityManagerImpl * self) {
return self->OnNanSubscribeTerminated(term_subscribe_id, reason);
}),
this);
// Interface available, remove the block added in OnInterfaceRemoved().
PafChannelState expected = PafChannelState::kNoInterface;
mPafChannelState.compare_exchange_strong(expected, PafChannelState::kAvailable);
}
void ConnectivityManagerImpl::OnAssociationRequested()
{
WiFiPAF::WiFiPAFLayer::GetWiFiPAFLayer().FlushPendingAcks();
}
void ConnectivityManagerImpl::OnAssociationStarting()
{
mPafChannelState.store(PafChannelState::kConnecting);
}
void ConnectivityManagerImpl::OnAssociationFailed()
{
// PAF does not need the station link, so wake the queued sends rather than waiting on the
// endpoint's resource-wait timer.
mPafChannelState.store(PafChannelState::kAvailable);
LogErrorOnFailure(
DeviceLayer::SystemLayer().ScheduleLambda([]() { WiFiPAF::WiFiPAFLayer::GetWiFiPAFLayer().DrivePendingSends(); }));
}
void ConnectivityManagerImpl::OnAssociationCompleted()
{
// NAN needs a few hundred milliseconds more than the link, so hold until it is seen working.
// New id first, so a timer left over from the previous wait cannot release this one.
mNanRecoveryId++;
mPafChannelState.store(PafChannelState::kAwaitingNan);
LogErrorOnFailure(DeviceLayer::SystemLayer().ScheduleLambda([this]() { ArmNanRecoveryTimer(); }));
}
void ConnectivityManagerImpl::OnInterfaceRemoved()
{
// The wpa_supplicant interface removed, nothing to send on.
mPafChannelState.store(PafChannelState::kNoInterface);
}
bool ConnectivityManagerImpl::TryReleaseNanRecoveryWait()
{
PafChannelState expected = PafChannelState::kAwaitingNan;
return mPafChannelState.compare_exchange_strong(expected, PafChannelState::kAvailable);
}
void ConnectivityManagerImpl::HandleNanRecoveryTimeout(chip::System::Layer * layer, void * context)
{
auto * self = static_cast<ConnectivityManagerImpl *>(context);
// Only release the Wait this timer was armed for.
VerifyOrReturn(self->mArmedNanRecoveryId.load() == self->mNanRecoveryId.load());
VerifyOrReturn(self->TryReleaseNanRecoveryWait());
ChipLogProgress(DeviceLayer, "WiFi-PAF: no NAN activity after association; releasing the transport anyway");
WiFiPAF::WiFiPAFLayer::GetWiFiPAFLayer().DrivePendingSends();
}
void ConnectivityManagerImpl::PafChannelNoteNanActivity()
{
// False on most frames: only the first after an association has a wait to release.
VerifyOrReturn(TryReleaseNanRecoveryWait());
ChipLogProgress(DeviceLayer, "WiFi-PAF: NAN active again after association; releasing the transport");
// Called from a NAN D-Bus signal on the glib thread, so the sends cannot be driven here.
LogErrorOnFailure(
DeviceLayer::SystemLayer().ScheduleLambda([]() { WiFiPAF::WiFiPAFLayer::GetWiFiPAFLayer().DrivePendingSends(); }));
}
void ConnectivityManagerImpl::ArmNanRecoveryTimer()
{
VerifyOrReturn(mPafChannelState.load() == PafChannelState::kAwaitingNan);
// Latest wait, so two associations in quick succession settle on the later one.
mArmedNanRecoveryId.store(mNanRecoveryId.load());
// StartTimer() cancels any timer already registered for this handler and context, so arming
// more than once per association just restarts it.
LogErrorOnFailure(DeviceLayer::SystemLayer().StartTimer(
System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_WIFIPAF_NAN_RECOVERY_TIMEOUT_MS), HandleNanRecoveryTimeout, this));
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
} // namespace DeviceLayer
} // namespace chip