Skip to content

Commit 30bed7a

Browse files
[Tizen] Modify dnssd resolve operation process (#37003)
* [Tizen] Modify dnssd resolve operation process Signed-off-by: hyunuk.tak <hyunuk.tak@samsung.com> * Restyled by clang-format * Modify the return method * Restyled by clang-format * Fix a segfault * Readability improvement * Add ScheduleWork for HandleResolveTask * Check to lock before calling context invoke sync function * Restyled by clang-format * Replace gsource idle with scheduleWork * Restyled by clang-format * Replace scheduleWork with scheduleLambda * Restyled by clang-format * Use CHIP_ERROR_FORMAT * Restyled by clang-format --------- Signed-off-by: hyunuk.tak <hyunuk.tak@samsung.com> Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 75b5405 commit 30bed7a

File tree

1 file changed

+42
-49
lines changed

1 file changed

+42
-49
lines changed

src/platform/Tizen/DnssdImpl.cpp

+42-49
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <lib/support/SafeInt.h>
3939
#include <lib/support/Span.h>
4040
#include <platform/CHIPDeviceConfig.h>
41+
#include <platform/CHIPDeviceLayer.h>
4142
#include <platform/GLibTypeDeleter.h>
4243
#include <platform/PlatformManager.h>
4344

@@ -276,25 +277,10 @@ void GetTextEntries(unsigned short txtLen, uint8_t * txtRecord, std::vector<chip
276277
}
277278
}
278279

279-
gboolean OnResolveFinalize(gpointer userData)
280-
{
281-
ChipLogDetail(DeviceLayer, "DNSsd %s", __func__);
282-
auto rCtx = reinterpret_cast<chip::Dnssd::ResolveContext *>(userData);
283-
284-
{
285-
// Lock the stack mutex when calling the callback function, so that the callback
286-
// function could safely perform message exchange (e.g. PASE session pairing).
287-
chip::DeviceLayer::StackLock lock;
288-
rCtx->Finalize(CHIP_NO_ERROR);
289-
}
290-
291-
rCtx->mInstance->RemoveContext(rCtx);
292-
return G_SOURCE_REMOVE;
293-
}
294-
295280
void OnResolve(dnssd_error_e result, dnssd_service_h service, void * userData)
296281
{
297-
ChipLogDetail(DeviceLayer, "DNSsd %s", __func__);
282+
ChipLogProgress(DeviceLayer, "DNSsd %s", __func__);
283+
298284
auto rCtx = reinterpret_cast<chip::Dnssd::ResolveContext *>(userData);
299285

300286
chip::GAutoPtr<char> name;
@@ -356,17 +342,14 @@ void OnResolve(dnssd_error_e result, dnssd_service_h service, void * userData)
356342

357343
rCtx->mResult.mAddress.emplace(ipAddr);
358344

359-
{
360-
// Before calling the Resolve() callback, we need to lock stack mutex.
361-
// However, we cannot lock the stack mutex from here, because we might
362-
// face lock inversion problem. This callback (OnResolve()) is called
363-
// with the NSD internal mutex locked, which is also locked by the
364-
// dnssd_create_remote_service() function called in the Resolve(), and
365-
// the Resolve() itself is called with the stack mutex locked.
366-
chip::GAutoPtr<GSource> sourceIdle(g_idle_source_new());
367-
g_source_set_callback(sourceIdle.get(), OnResolveFinalize, rCtx, NULL);
368-
g_source_attach(sourceIdle.get(), g_main_context_get_thread_default());
369-
}
345+
err = chip::DeviceLayer::SystemLayer().ScheduleLambda([rCtx] {
346+
ChipLogProgress(DeviceLayer, "DNSsd Handle resolve task on schedule lambda");
347+
348+
rCtx->Finalize(CHIP_NO_ERROR);
349+
rCtx->mInstance->RemoveContext(rCtx);
350+
});
351+
VerifyOrExit(err == CHIP_NO_ERROR,
352+
ChipLogError(DeviceLayer, "Failed to schedule resolve task: %" CHIP_ERROR_FORMAT, err.Format()));
370353

371354
return;
372355

@@ -377,11 +360,38 @@ void OnResolve(dnssd_error_e result, dnssd_service_h service, void * userData)
377360

378361
CHIP_ERROR ResolveAsync(chip::Dnssd::ResolveContext * rCtx)
379362
{
363+
int ret;
364+
380365
ChipLogDetail(DeviceLayer, "DNSsd %s", __func__);
381366

382-
int ret = dnssd_resolve_service(rCtx->mServiceHandle, OnResolve, rCtx);
383-
VerifyOrReturnValue(ret == DNSSD_ERROR_NONE, TizenToChipError(ret),
384-
ChipLogError(DeviceLayer, "dnssd_resolve_service() failed: %s", get_error_message(ret)));
367+
if (rCtx->mInterfaceId == 0)
368+
{
369+
ret = dnssd_create_remote_service(rCtx->mType, rCtx->mName, nullptr, &rCtx->mServiceHandle);
370+
}
371+
else
372+
{
373+
char iface[IF_NAMESIZE + 1] = "";
374+
if (if_indextoname(rCtx->mInterfaceId, iface) == nullptr)
375+
{
376+
ChipLogError(DeviceLayer, "if_indextoname() failed: %s", strerror(errno));
377+
return CHIP_ERROR_POSIX(errno);
378+
}
379+
380+
ret = dnssd_create_remote_service(rCtx->mType, rCtx->mName, iface, &rCtx->mServiceHandle);
381+
}
382+
383+
if (ret != DNSSD_ERROR_NONE)
384+
{
385+
ChipLogError(DeviceLayer, "dnssd_create_remote_service() failed: %s", get_error_message(ret));
386+
return TizenToChipError(ret);
387+
}
388+
389+
ret = dnssd_resolve_service(rCtx->mServiceHandle, OnResolve, rCtx);
390+
if (ret != DNSSD_ERROR_NONE)
391+
{
392+
ChipLogError(DeviceLayer, "dnssd_resolve_service() failed: %s", get_error_message(ret));
393+
return TizenToChipError(ret);
394+
}
385395

386396
rCtx->mIsResolving = true;
387397
return CHIP_NO_ERROR;
@@ -459,6 +469,7 @@ ResolveContext::~ResolveContext()
459469

460470
void ResolveContext::Finalize(CHIP_ERROR error)
461471
{
472+
ChipLogProgress(DeviceLayer, "DNSsd %s", __func__);
462473
// In case of error, run the callback function with nullptr as the result.
463474
VerifyOrReturn(error == CHIP_NO_ERROR, mCallback(mCbContext, nullptr, chip::Span<chip::Inet::IPAddress>(), error));
464475

@@ -628,30 +639,12 @@ CHIP_ERROR DnssdTizen::Resolve(const DnssdService & browseResult, chip::Inet::In
628639
std::string fullType = GetFullType(browseResult.mType, browseResult.mProtocol);
629640
auto interfaceId = interface.GetPlatformInterface();
630641
CHIP_ERROR err = CHIP_NO_ERROR;
631-
int ret;
632642

633643
ChipLogDetail(DeviceLayer, "DNSsd %s: name: %s, type: %s, interfaceId: %u", __func__, browseResult.mName, fullType.c_str(),
634644
interfaceId);
635645

636646
auto resolveCtx = CreateResolveContext(browseResult.mName, fullType.c_str(), interfaceId, callback, context);
637647

638-
if (interfaceId == 0)
639-
{
640-
ret = dnssd_create_remote_service(fullType.c_str(), browseResult.mName, nullptr, &resolveCtx->mServiceHandle);
641-
}
642-
else
643-
{
644-
char iface[IF_NAMESIZE + 1] = "";
645-
VerifyOrExit(if_indextoname(interfaceId, iface) != nullptr,
646-
ChipLogError(DeviceLayer, "if_indextoname() failed: %s", strerror(errno));
647-
err = CHIP_ERROR_POSIX(errno));
648-
ret = dnssd_create_remote_service(fullType.c_str(), browseResult.mName, iface, &resolveCtx->mServiceHandle);
649-
}
650-
651-
VerifyOrExit(ret == DNSSD_ERROR_NONE,
652-
ChipLogError(DeviceLayer, "dnssd_create_remote_service() failed: %s", get_error_message(ret));
653-
err = TizenToChipError(ret));
654-
655648
err = DeviceLayer::PlatformMgrImpl().GLibMatterContextInvokeSync(ResolveAsync, resolveCtx);
656649
SuccessOrExit(err);
657650

0 commit comments

Comments
 (0)