38
38
#include < lib/support/SafeInt.h>
39
39
#include < lib/support/Span.h>
40
40
#include < platform/CHIPDeviceConfig.h>
41
+ #include < platform/CHIPDeviceLayer.h>
41
42
#include < platform/GLibTypeDeleter.h>
42
43
#include < platform/PlatformManager.h>
43
44
@@ -276,25 +277,10 @@ void GetTextEntries(unsigned short txtLen, uint8_t * txtRecord, std::vector<chip
276
277
}
277
278
}
278
279
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
-
295
280
void OnResolve (dnssd_error_e result, dnssd_service_h service, void * userData)
296
281
{
297
- ChipLogDetail (DeviceLayer, " DNSsd %s" , __func__);
282
+ ChipLogProgress (DeviceLayer, " DNSsd %s" , __func__);
283
+
298
284
auto rCtx = reinterpret_cast <chip::Dnssd::ResolveContext *>(userData);
299
285
300
286
chip::GAutoPtr<char > name;
@@ -356,17 +342,14 @@ void OnResolve(dnssd_error_e result, dnssd_service_h service, void * userData)
356
342
357
343
rCtx->mResult .mAddress .emplace (ipAddr);
358
344
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 ()));
370
353
371
354
return ;
372
355
@@ -377,11 +360,38 @@ void OnResolve(dnssd_error_e result, dnssd_service_h service, void * userData)
377
360
378
361
CHIP_ERROR ResolveAsync (chip::Dnssd::ResolveContext * rCtx)
379
362
{
363
+ int ret;
364
+
380
365
ChipLogDetail (DeviceLayer, " DNSsd %s" , __func__);
381
366
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
+ }
385
395
386
396
rCtx->mIsResolving = true ;
387
397
return CHIP_NO_ERROR;
@@ -459,6 +469,7 @@ ResolveContext::~ResolveContext()
459
469
460
470
void ResolveContext::Finalize (CHIP_ERROR error)
461
471
{
472
+ ChipLogProgress (DeviceLayer, " DNSsd %s" , __func__);
462
473
// In case of error, run the callback function with nullptr as the result.
463
474
VerifyOrReturn (error == CHIP_NO_ERROR, mCallback (mCbContext , nullptr , chip::Span<chip::Inet::IPAddress>(), error));
464
475
@@ -628,30 +639,12 @@ CHIP_ERROR DnssdTizen::Resolve(const DnssdService & browseResult, chip::Inet::In
628
639
std::string fullType = GetFullType (browseResult.mType , browseResult.mProtocol );
629
640
auto interfaceId = interface.GetPlatformInterface ();
630
641
CHIP_ERROR err = CHIP_NO_ERROR;
631
- int ret;
632
642
633
643
ChipLogDetail (DeviceLayer, " DNSsd %s: name: %s, type: %s, interfaceId: %u" , __func__, browseResult.mName , fullType.c_str (),
634
644
interfaceId);
635
645
636
646
auto resolveCtx = CreateResolveContext (browseResult.mName , fullType.c_str (), interfaceId, callback, context);
637
647
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
-
655
648
err = DeviceLayer::PlatformMgrImpl ().GLibMatterContextInvokeSync (ResolveAsync, resolveCtx);
656
649
SuccessOrExit (err);
657
650
0 commit comments