@@ -26,69 +26,38 @@ namespace chip {
26
26
namespace DeviceLayer {
27
27
namespace Internal {
28
28
29
- AdapterIterator::~AdapterIterator ()
30
- {
31
- if (mManager != nullptr )
32
- {
33
- g_object_unref (mManager );
34
- }
35
-
36
- if (mObjectList != nullptr )
37
- {
38
- g_list_free_full (mObjectList , g_object_unref);
39
- }
40
- }
41
-
42
- CHIP_ERROR AdapterIterator::Initialize (AdapterIterator * self)
29
+ CHIP_ERROR AdapterIterator::Initialize ()
43
30
{
44
31
// When creating D-Bus proxy object, the thread default context must be initialized. Otherwise,
45
32
// all D-Bus signals will be delivered to the GLib global default main context.
46
33
VerifyOrDie (g_main_context_get_thread_default () != nullptr );
47
34
48
- CHIP_ERROR err = CHIP_NO_ERROR;
49
35
GAutoPtr<GError> error;
50
-
51
- self->mManager = g_dbus_object_manager_client_new_for_bus_sync (
36
+ mManager .reset (g_dbus_object_manager_client_new_for_bus_sync (
52
37
G_BUS_TYPE_SYSTEM, G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE, BLUEZ_INTERFACE, " /" ,
53
38
bluez_object_manager_client_get_proxy_type, nullptr /* unused user data in the Proxy Type Func */ ,
54
- nullptr /* destroy notify */ , nullptr /* cancellable */ , &error.GetReceiver ());
39
+ nullptr /* destroy notify */ , nullptr /* cancellable */ , &error.GetReceiver ())) ;
55
40
56
- VerifyOrExit (self-> mManager != nullptr , ChipLogError (DeviceLayer, " Failed to get DBUS object manager for listing adapters. " );
57
- err = CHIP_ERROR_INTERNAL );
41
+ VerifyOrReturnError ( mManager , CHIP_ERROR_INTERNAL,
42
+ ChipLogError (DeviceLayer, " Failed to get D-Bus object manager for listing adapters: %s " , error-> message ) );
58
43
59
- self-> mObjectList = g_dbus_object_manager_get_objects (self-> mManager );
60
- self-> mCurrentListItem = self-> mObjectList ;
44
+ mObjectList . Init ( mManager . get () );
45
+ mIterator = mObjectList . begin () ;
61
46
62
- exit :
63
- if (error != nullptr )
64
- {
65
- ChipLogError (DeviceLayer, " DBus error: %s" , error->message );
66
- }
67
-
68
- return err;
47
+ return CHIP_NO_ERROR;
69
48
}
70
49
71
50
bool AdapterIterator::Advance ()
72
51
{
73
- if ( mCurrentListItem == nullptr )
52
+ for (; mIterator != BluezObjectList::end (); ++ mIterator )
74
53
{
75
- return false ;
76
- }
77
-
78
- while (mCurrentListItem != nullptr )
79
- {
80
- BluezAdapter1 * adapter = bluez_object_get_adapter1 (BLUEZ_OBJECT (mCurrentListItem ->data ));
81
- if (adapter == nullptr )
54
+ BluezAdapter1 * adapter = bluez_object_get_adapter1 (&(*mIterator ));
55
+ if (adapter != nullptr )
82
56
{
83
- mCurrentListItem = mCurrentListItem ->next ;
84
- continue ;
57
+ mCurrentAdapter .reset (adapter);
58
+ ++mIterator ;
59
+ return true ;
85
60
}
86
-
87
- mCurrentAdapter .reset (adapter);
88
-
89
- mCurrentListItem = mCurrentListItem ->next ;
90
-
91
- return true ;
92
61
}
93
62
94
63
return false ;
@@ -112,9 +81,10 @@ uint32_t AdapterIterator::GetIndex() const
112
81
113
82
bool AdapterIterator::Next ()
114
83
{
115
- if (mManager == nullptr )
84
+ if (! mManager )
116
85
{
117
- CHIP_ERROR err = PlatformMgrImpl ().GLibMatterContextInvokeSync (Initialize, this );
86
+ CHIP_ERROR err = PlatformMgrImpl ().GLibMatterContextInvokeSync (
87
+ +[](AdapterIterator * self) { return self->Initialize (); }, this );
118
88
VerifyOrReturnError (err == CHIP_NO_ERROR, false , ChipLogError (DeviceLayer, " Failed to initialize adapter iterator" ));
119
89
}
120
90
0 commit comments