Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate dedicated bluez directory for linux BLE interfacing #4846

Merged
merged 23 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f293ffe
Add an adapter iterator class to bluez
andy31415 Feb 12, 2021
1f0b19f
code review updates
andy31415 Feb 12, 2021
df722ac
Fix typo in method
andy31415 Feb 12, 2021
433f869
Fix compilation and off-by-one error
andy31415 Feb 12, 2021
9182b58
Ensure we unref the adapter during listing
andy31415 Feb 12, 2021
02aeafe
Remove unused variable
andy31415 Feb 12, 2021
9ba960b
Fix typos in comments - I clobbered the fixes that Justin pushed
andy31415 Feb 12, 2021
2f8aa3c
Fix typos in comments - I clobbered the fixes that Justin pushed
andy31415 Feb 12, 2021
6bebebf
Merge branch 'master' into 01_list_bluez_interfaces
andy31415 Feb 12, 2021
86d7053
Add support for native adapter listing
andy31415 Feb 12, 2021
8981566
Restyle fixes
andy31415 Feb 12, 2021
d91dc50
Update the init logic
andy31415 Feb 12, 2021
d2e5116
Do not auto-import GetAdapters. ble is a stand alone package for now
andy31415 Feb 12, 2021
7279672
Update typing
andy31415 Feb 12, 2021
55827ba
Move iterator values to std::string and fix typo in linux impl
andy31415 Feb 12, 2021
780496d
Move Bluez files into a separate directory to be able to better split…
andy31415 Feb 12, 2021
21ed05d
Split out AdapterIterator from bluez
andy31415 Feb 12, 2021
7c88a1b
Restyle fixes
andy31415 Feb 12, 2021
0a78780
Remove adapteriterator from helper.h and only keep in adapteriterator.h
andy31415 Feb 12, 2021
9910f97
Switch reinterpret cast to static cast
andy31415 Feb 16, 2021
25870ce
Merge branch 'master' into 02_native_list_adapters
andy31415 Feb 16, 2021
53bd2c9
Merge branch '02_native_list_adapters' into 03_move_bluez_helpers
andy31415 Feb 16, 2021
4ac7a3a
Merge branch 'master' into 03_move_bluez_helpers
andy31415 Feb 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/controller/python/chip/ble/LinuxImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

#include <platform/CHIPDeviceLayer.h>
#include <platform/Linux/CHIPBluezHelper.h>
#include <platform/Linux/bluez/AdapterIterator.h>
#include <platform/internal/BLEManager.h>
#include <support/CHIPMem.h>
#include <support/ReturnMacros.h>
Expand Down
7 changes: 5 additions & 2 deletions src/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,6 @@ if (chip_device_platform != "none" && chip_device_platform != "external") {
"Linux/BLEManagerImpl.cpp",
"Linux/BLEManagerImpl.h",
"Linux/BlePlatformConfig.h",
"Linux/CHIPBluezHelper.cpp",
"Linux/CHIPBluezHelper.h",
"Linux/CHIPDevicePlatformConfig.h",
"Linux/CHIPDevicePlatformEvent.h",
"Linux/CHIPLinuxStorage.cpp",
Expand All @@ -466,6 +464,11 @@ if (chip_device_platform != "none" && chip_device_platform != "external") {
"Linux/PosixConfig.h",
"Linux/SystemPlatformConfig.h",
"Linux/SystemTimeSupport.cpp",
"Linux/bluez/AdapterIterator.cpp",
"Linux/bluez/AdapterIterator.h",
"Linux/bluez/Helper.cpp",
"Linux/bluez/Helper.h",
"Linux/bluez/Types.h",
]

if (chip_enable_mdns) {
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Linux/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE

#include "CHIPBluezHelper.h"
#include "bluez/Helper.h"

using namespace ::nl;
using namespace ::chip::Ble;
Expand Down
119 changes: 119 additions & 0 deletions src/platform/Linux/bluez/AdapterIterator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "AdapterIterator.h"

#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE

namespace chip {
namespace DeviceLayer {
namespace Internal {

AdapterIterator::~AdapterIterator()
{
if (mManager != nullptr)
{
g_object_unref(mManager);
}

if (mObjectList != nullptr)
{
g_list_free_full(mObjectList, g_object_unref);
}
}

void AdapterIterator::Initialize()
{
GError * error = nullptr;

mManager = g_dbus_object_manager_client_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
BLUEZ_INTERFACE, "/", bluez_object_manager_client_get_proxy_type,
nullptr /* unused user data in the Proxy Type Func */,
nullptr /*destroy notify */, nullptr /* cancellable */, &error);

VerifyOrExit(mManager != nullptr, ChipLogError(DeviceLayer, "Failed to get DBUS object manager for listing adapters."));

mObjectList = g_dbus_object_manager_get_objects(mManager);
mCurrentListItem = mObjectList;

exit:
if (error != nullptr)
{
ChipLogError(DeviceLayer, "DBus error: %s", error->message);
g_error_free(error);
}
}

bool AdapterIterator::Advance()
{
if (mCurrentListItem == nullptr)
{
return false;
}

while (mCurrentListItem != nullptr)
{
BluezAdapter1 * adapter = bluez_object_get_adapter1(BLUEZ_OBJECT(mCurrentListItem->data));
if (adapter == nullptr)
{
mCurrentListItem = mCurrentListItem->next;
continue;
}

// PATH is of the for BLUEZ_PATH / hci<nr>, i.e. like
// '/org/bluez/hci0'
// Index represents the number after hci
const char * path = g_dbus_proxy_get_object_path(G_DBUS_PROXY(adapter));
unsigned index = 0;

if (sscanf(path, BLUEZ_PATH "/hci%u", &index) != 1)
{
ChipLogError(DeviceLayer, "Failed to extract HCI index from '%s'", path);
index = 0;
}

mCurrent.index = index;
mCurrent.address = bluez_adapter1_get_address(adapter);
mCurrent.alias = bluez_adapter1_get_alias(adapter);
mCurrent.name = bluez_adapter1_get_name(adapter);
mCurrent.powered = bluez_adapter1_get_powered(adapter);

g_object_unref(adapter);

mCurrentListItem = mCurrentListItem->next;

return true;
}

return false;
}

bool AdapterIterator::Next()
{
if (mManager == nullptr)
{
Initialize();
}

return Advance();
}

} // namespace Internal
} // namespace DeviceLayer
} // namespace chip

#endif
92 changes: 92 additions & 0 deletions src/platform/Linux/bluez/AdapterIterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "Types.h"

#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE

namespace chip {
namespace DeviceLayer {
namespace Internal {

/// Iterates over available BlueZ adapters
///
/// Usage example:
///
/// AdapterIterator iterator;
/// while (iterator.Next()) {
/// std::cout << iterator.GetAddress() << std::endl;
/// }
///
/// Data is provided through the bluez dbus interface. You can view
/// this data in the commandline using commands such as:
///
/// busctl introspect org.bluez /org/bluez/hci0
class AdapterIterator
{
public:
~AdapterIterator();

/// Moves to the next DBUS interface.
///
/// MUST be called before any of the 'current value' methods are
/// used (iterator gets initialized on the first call of Next).
bool Next();

// Information about the current value. Safe to call only after
// "Next" has returned true.
uint32_t GetIndex() const { return mCurrent.index; }
const char * GetAddress() const { return mCurrent.address.c_str(); }
const char * GetAlias() const { return mCurrent.alias.c_str(); }
const char * GetName() const { return mCurrent.name.c_str(); }
bool IsPowered() const { return mCurrent.powered; }

private:
/// Sets up the DBUS manager and loads the list
void Initialize();

/// Loads the next value in the list.
///
/// Returns true if a value could be loaded, false if no more items to
/// iterate through.
bool Advance();

static constexpr size_t kMaxAddressLength = 19; // xx:xx:xx:xx:xx:xx
static constexpr size_t kMaxNameLength = 64;

GDBusObjectManager * mManager = nullptr; // DBus connection
GList * mObjectList = nullptr; // listing of objects on the bus
GList * mCurrentListItem = nullptr; // current item viewed in the list

// data valid only if Next() returns true
struct
{
uint32_t index;
std::string address;
std::string alias;
std::string name;
bool powered;
} mCurrent = { 0 };
};

} // namespace Internal
} // namespace DeviceLayer
} // namespace chip

#endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@
#include <unistd.h>
#include <utility>

#include "BLEManagerImpl.h"
#include "CHIPBluezHelper.h"
#include <platform/Linux/BLEManagerImpl.h>
#include <support/CodeUtils.h>
#include <support/ReturnMacros.h>
#include <system/TLVPacketBufferBackingStore.h>

#include "Helper.h"

using namespace ::nl;
using namespace chip::Protocols;
using chip::Platform::CopyString;
Expand Down Expand Up @@ -2011,95 +2012,6 @@ CHIP_ERROR ConnectDevice(BluezDevice1 * apDevice)
return error;
}

AdapterIterator::~AdapterIterator()
{
if (mManager != nullptr)
{
g_object_unref(mManager);
}

if (mObjectList != nullptr)
{
g_list_free_full(mObjectList, g_object_unref);
}
}

void AdapterIterator::Initialize()
{
GError * error = nullptr;

mManager = g_dbus_object_manager_client_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
BLUEZ_INTERFACE, "/", bluez_object_manager_client_get_proxy_type,
nullptr /* unused user data in the Proxy Type Func */,
nullptr /*destroy notify */, nullptr /* cancellable */, &error);

VerifyOrExit(mManager != nullptr, ChipLogError(DeviceLayer, "Failed to get DBUS object manager for listing adapters."));

mObjectList = g_dbus_object_manager_get_objects(mManager);
mCurrentListItem = mObjectList;

exit:
if (error != nullptr)
{
ChipLogError(DeviceLayer, "DBus error: %s", error->message);
g_error_free(error);
}
}

bool AdapterIterator::Advance()
{
if (mCurrentListItem == nullptr)
{
return false;
}

while (mCurrentListItem != nullptr)
{
BluezAdapter1 * adapter = bluez_object_get_adapter1(BLUEZ_OBJECT(mCurrentListItem->data));
if (adapter == nullptr)
{
mCurrentListItem = mCurrentListItem->next;
continue;
}

// PATH is of the for BLUEZ_PATH / hci<nr>, i.e. like
// '/org/bluez/hci0'
// Index represents the number after hci
const char * path = g_dbus_proxy_get_object_path(G_DBUS_PROXY(adapter));
unsigned index = 0;

if (sscanf(path, BLUEZ_PATH "/hci%u", &index) != 1)
{
ChipLogError(DeviceLayer, "Failed to extract HCI index from '%s'", path);
index = 0;
}

mCurrent.index = index;
mCurrent.address = bluez_adapter1_get_address(adapter);
mCurrent.alias = bluez_adapter1_get_alias(adapter);
mCurrent.name = bluez_adapter1_get_name(adapter);
mCurrent.powered = bluez_adapter1_get_powered(adapter);

g_object_unref(adapter);

mCurrentListItem = mCurrentListItem->next;

return true;
}

return false;
}

bool AdapterIterator::Next()
{
if (mManager == nullptr)
{
Initialize();
}

return Advance();
}

} // namespace Internal
} // namespace DeviceLayer
} // namespace chip
Expand Down
Loading