Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / Linux / bluez / AdapterIterator.h
1 /*
2  *
3  *    Copyright (c) 2021 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 #pragma once
19
20 #include "Types.h"
21
22 #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
23
24 namespace chip {
25 namespace DeviceLayer {
26 namespace Internal {
27
28 /// Iterates over available BlueZ adapters
29 ///
30 /// Usage example:
31 ///
32 ///  AdapterIterator iterator;
33 ///  while (iterator.Next()) {
34 ///      std::cout << iterator.GetAddress() << std::endl;
35 ///  }
36 ///
37 /// Data is provided through the bluez dbus interface. You can view
38 /// this data in the commandline using commands such as:
39 ///
40 ///    busctl introspect org.bluez /org/bluez/hci0
41 class AdapterIterator
42 {
43 public:
44     ~AdapterIterator();
45
46     /// Moves to the next DBUS interface.
47     ///
48     /// MUST be called before any of the 'current value' methods are
49     /// used (iterator gets initialized on the first call of Next).
50     bool Next();
51
52     // Information about the current value. Safe to call only after
53     // "Next" has returned true.
54     uint32_t GetIndex() const { return mCurrent.index; }
55     const char * GetAddress() const { return mCurrent.address.c_str(); }
56     const char * GetAlias() const { return mCurrent.alias.c_str(); }
57     const char * GetName() const { return mCurrent.name.c_str(); }
58     bool IsPowered() const { return mCurrent.powered; }
59     BluezAdapter1 * GetAdapter() const { return mCurrent.adapter; }
60
61 private:
62     /// Sets up the DBUS manager and loads the list
63     void Initialize();
64
65     /// Loads the next value in the list.
66     ///
67     /// Returns true if a value could be loaded, false if no more items to
68     /// iterate through.
69     bool Advance();
70
71     static constexpr size_t kMaxAddressLength = 19; // xx:xx:xx:xx:xx:xx
72     static constexpr size_t kMaxNameLength    = 64;
73
74     GDBusObjectManager * mManager = nullptr; // DBus connection
75     GList * mObjectList           = nullptr; // listing of objects on the bus
76     GList * mCurrentListItem      = nullptr; // current item viewed in the list
77
78     // data valid only if Next() returns true
79     struct
80     {
81         uint32_t index;
82         std::string address;
83         std::string alias;
84         std::string name;
85         bool powered;
86         BluezAdapter1 * adapter;
87     } mCurrent = { 0 };
88 };
89
90 } // namespace Internal
91 } // namespace DeviceLayer
92 } // namespace chip
93
94 #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE