Skip to content

Commit

Permalink
Log the instance name when publishing dns-sd services. (#22191)
Browse files Browse the repository at this point in the history
Makes it easier to correlate server logs against what clients are seeing.

This does not work on all platforms, since some platform impls of
ChipDnssdPublishService never call the callback...
  • Loading branch information
bzbarsky-apple authored Aug 29, 2022
1 parent 0bc1578 commit 0abfab0
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 24 deletions.
8 changes: 5 additions & 3 deletions src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ CHIP_ERROR AdvertiserMinMdns::Advertise(const OperationalAdvertisingParameters &
return CHIP_ERROR_NO_MEMORY;
}

ChipLogProgress(Discovery, "CHIP minimal mDNS configured as 'Operational device'.");
ChipLogProgress(Discovery, "CHIP minimal mDNS configured as 'Operational device'; instance name: %s.", instanceName.names[0]);

AdvertiseRecords(BroadcastAdvertiseType::kStarted);

Expand Down Expand Up @@ -724,11 +724,13 @@ CHIP_ERROR AdvertiserMinMdns::Advertise(const CommissionAdvertisingParameters &

if (params.GetCommissionAdvertiseMode() == CommssionAdvertiseMode::kCommissionableNode)
{
ChipLogProgress(Discovery, "CHIP minimal mDNS configured as 'Commissionable node device'.");
ChipLogProgress(Discovery, "CHIP minimal mDNS configured as 'Commissionable node device'; instance name: %s.",
instanceName.names[0]);
}
else
{
ChipLogProgress(Discovery, "CHIP minimal mDNS configured as 'Commissioner device'.");
ChipLogProgress(Discovery, "CHIP minimal mDNS configured as 'Commissioner device'; instance name: %s.",
instanceName.names[0]);
}

AdvertiseRecords(BroadcastAdvertiseType::kStarted);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/dnssd/Discovery_ImplPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,11 @@ CHIP_ERROR DiscoveryImplPlatform::UpdateCommissionableInstanceName()
return CHIP_NO_ERROR;
}

void DiscoveryImplPlatform::HandleDnssdPublish(void * context, const char * type, CHIP_ERROR error)
void DiscoveryImplPlatform::HandleDnssdPublish(void * context, const char * type, const char * instanceName, CHIP_ERROR error)
{
if (CHIP_NO_ERROR == error)
{
ChipLogProgress(Discovery, "mDNS service published: %s", type);
ChipLogProgress(Discovery, "mDNS service published: %s; instance name: %s", type, instanceName);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/dnssd/Discovery_ImplPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class DiscoveryImplPlatform : public ServiceAdvertiser, public Resolver

static void HandleDnssdInit(void * context, CHIP_ERROR initError);
static void HandleDnssdError(void * context, CHIP_ERROR initError);
static void HandleDnssdPublish(void * context, const char * type, CHIP_ERROR error);
static void HandleDnssdPublish(void * context, const char * type, const char * instanceName, CHIP_ERROR error);
static CHIP_ERROR GenerateRotatingDeviceId(char rotatingDeviceIdHexBuffer[], size_t & rotatingDeviceIdHexBufferSize);
CHIP_ERROR PublishService(const char * serviceType, TextEntry * textEntries, size_t textEntrySize, const char ** subTypes,
size_t subTypeSize, const OperationalAdvertisingParameters & params);
Expand Down
7 changes: 4 additions & 3 deletions src/lib/dnssd/platform/Dnssd.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ using DnssdBrowseCallback = void (*)(void * context, DnssdService * services, si
* The callback function SHALL NOT take the ownership of the service pointer or
* any pointer inside this structure.
*
* @param[in] context The context passed to ChipDnssdPublish.
* @param[in] type The published type if no errors has occured, nullptr otherwise.
* @param[in] context The context passed to ChipDnssdPublishService.
* @param[in] type The published type if no errors have occured, nullptr otherwise.
* @param[in] instanceName The published instance name if no errors have occured, nullptr otherwise.
* @param[in] error The error code.
*
*/
using DnssdPublishCallback = void (*)(void * context, const char * type, CHIP_ERROR error);
using DnssdPublishCallback = void (*)(void * context, const char * type, const char * instanceName, CHIP_ERROR error);

using DnssdAsyncReturnCallback = void (*)(void * context, CHIP_ERROR error);

Expand Down
9 changes: 5 additions & 4 deletions src/platform/Darwin/DnssdContexts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,26 +251,27 @@ CHIP_ERROR MdnsContexts::GetRegisterContextOfType(const char * type, RegisterCon
return found ? CHIP_NO_ERROR : CHIP_ERROR_KEY_NOT_FOUND;
}

RegisterContext::RegisterContext(const char * sType, DnssdPublishCallback cb, void * cbContext)
RegisterContext::RegisterContext(const char * sType, const char * instanceName, DnssdPublishCallback cb, void * cbContext)
{
type = ContextType::Register;
context = cbContext;
callback = cb;

mType = sType;
mType = sType;
mInstanceName = instanceName;
}

void RegisterContext::DispatchFailure(DNSServiceErrorType err)
{
ChipLogError(Discovery, "Mdns: Register failure (%s)", Error::ToString(err));
callback(context, nullptr, CHIP_ERROR_INTERNAL);
callback(context, nullptr, nullptr, CHIP_ERROR_INTERNAL);
MdnsContexts::GetInstance().Remove(this);
}

void RegisterContext::DispatchSuccess()
{
std::string typeWithoutSubTypes = GetFullTypeWithoutSubTypes(mType);
callback(context, typeWithoutSubTypes.c_str(), CHIP_NO_ERROR);
callback(context, typeWithoutSubTypes.c_str(), mInstanceName.c_str(), CHIP_NO_ERROR);
}

BrowseContext::BrowseContext(void * cbContext, DnssdBrowseCallback cb, DnssdServiceProtocol cbContextProtocol)
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Darwin/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ CHIP_ERROR Register(void * context, DnssdPublishCallback callback, uint32_t inte
return CHIP_NO_ERROR;
}

sdCtx = chip::Platform::New<RegisterContext>(type, callback, context);
sdCtx = chip::Platform::New<RegisterContext>(type, name, callback, context);
VerifyOrReturnError(nullptr != sdCtx, CHIP_ERROR_NO_MEMORY);

DNSServiceRef sdRef;
Expand Down
3 changes: 2 additions & 1 deletion src/platform/Darwin/DnssdImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ struct RegisterContext : public GenericContext
{
DnssdPublishCallback callback;
std::string mType;
std::string mInstanceName;

RegisterContext(const char * sType, DnssdPublishCallback cb, void * cbContext);
RegisterContext(const char * sType, const char * instanceName, DnssdPublishCallback cb, void * cbContext);
virtual ~RegisterContext() {}

void DispatchFailure(DNSServiceErrorType err) override;
Expand Down
4 changes: 2 additions & 2 deletions src/platform/Linux/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,11 @@ CHIP_ERROR MdnsAvahi::PublishService(const DnssdService & service, DnssdPublishC
// The code needs to be updated to support that callback properly.
if (CHIP_NO_ERROR == error)
{
callback(context, type.c_str(), CHIP_NO_ERROR);
callback(context, type.c_str(), service.mName, CHIP_NO_ERROR);
}
else
{
callback(context, nullptr, error);
callback(context, nullptr, nullptr, error);
}

return error;
Expand Down
8 changes: 4 additions & 4 deletions src/platform/Tizen/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ void OnRegister(dnssd_error_e result, dnssd_service_h service, void * data)
if (result != DNSSD_ERROR_NONE)
{
ChipLogError(DeviceLayer, "DNSsd %s: Error: %d", __func__, result);
rCtx->mCallback(rCtx->mCbContext, nullptr, GetChipError(result));
rCtx->mCallback(rCtx->mCbContext, nullptr, nullptr, GetChipError(result));
// After this point, the context might be no longer valid
rCtx->mInstance->RemoveContext(rCtx);
return;
}

rCtx->mCallback(rCtx->mCbContext, rCtx->mType, CHIP_NO_ERROR);
rCtx->mCallback(rCtx->mCbContext, rCtx->mType, rCtx->mName, CHIP_NO_ERROR);
}

gboolean RegisterAsync(GMainLoop * mainLoop, gpointer userData)
Expand Down Expand Up @@ -524,7 +524,7 @@ CHIP_ERROR DnssdTizen::RegisterService(const DnssdService & service, DnssdPublis
if (ret != DNSSD_ERROR_NONE)
{
ChipLogError(DeviceLayer, "dnssd_service_add_txt_record() failed. ret: %d", ret);
callback(context, nullptr, err = GetChipError(ret));
callback(context, nullptr, nullptr, err = GetChipError(ret));
}
}

Expand Down Expand Up @@ -573,7 +573,7 @@ CHIP_ERROR DnssdTizen::RegisterService(const DnssdService & service, DnssdPublis
exit:
if (err != CHIP_NO_ERROR)
{ // Notify caller about error
callback(context, nullptr, err);
callback(context, nullptr, nullptr, err);
RemoveContext(serviceCtx);
}
return err;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/tests/TestDnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static void HandleBrowse(void * context, DnssdService * services, size_t service
}
}

static void HandlePublish(void * context, const char * type, CHIP_ERROR error) {}
static void HandlePublish(void * context, const char * type, const char * instanceName, CHIP_ERROR error) {}

static void InitCallback(void * context, CHIP_ERROR error)
{
Expand Down
4 changes: 2 additions & 2 deletions src/platform/webos/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,11 @@ CHIP_ERROR MdnsAvahi::PublishService(const DnssdService & service, DnssdPublishC
// The code needs to be updated to support that callback properly.
if (CHIP_NO_ERROR == error)
{
callback(context, type.c_str(), CHIP_NO_ERROR);
callback(context, type.c_str(), service.mName, CHIP_NO_ERROR);
}
else
{
callback(context, nullptr, error);
callback(context, nullptr, nullptr, error);
}

return error;
Expand Down

0 comments on commit 0abfab0

Please sign in to comment.