Implemented bluetooth Adapter and atomic boolean.

This commit is contained in:
kidfromjupiter
2025-10-02 16:26:40 +00:00
parent cd24003e6e
commit 922ff58a3b
5 changed files with 277 additions and 0 deletions
@@ -0,0 +1,33 @@
//
// Created by root on 10/2/25.
//
#ifndef LINUX_ATOMIC_BOOLEAN_H
#define LINUX_ATOMIC_BOOLEAN_H
#include <atomic>
#include "internal/platform/implementation/atomic_boolean.h"
namespace nearby {
namespace linux {
// A boolean value that may be updated atomically.
class AtomicBoolean : public api::AtomicBoolean {
public:
explicit AtomicBoolean(bool value = false) : atomic_boolean_(value) {}
~AtomicBoolean() override = default;
// Atomically read and return current value.
bool Get() const override { return atomic_boolean_; };
// Atomically exchange original value with a new one. Return previous value.
bool Set(bool value) override { return atomic_boolean_.exchange(value); };
private:
std::atomic_bool atomic_boolean_ = false;
};
} // namespace api
} // namespace nearby
#endif //LINUX_ATOMIC_BOOLEAN_H
@@ -0,0 +1,90 @@
// Copyright 2020 Google LLC
//
// 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
//
// https://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 "absl/base/attributes.h"
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/linux/bluetooth_adapter.h"
#include "third_party/absl/absl/strings/str_format.h"
namespace nearby {
namespace linux {
bool BluetoothAdapter::SetStatus(Status status)
{
if (status == Status::kEnabled)
{
Powered(true);
}
Powered(false);
return true;
}
bool BluetoothAdapter::IsEnabled() const
{
return Powered();
}
api::BluetoothAdapter::ScanMode BluetoothAdapter::GetScanMode() const
{
if (!IsEnabled())
{
return ScanMode::kNone;
}
if (Discoverable())
{
return ScanMode::kConnectableDiscoverable;
}
return ScanMode::kConnectable;
}
bool BluetoothAdapter::SetScanMode(ScanMode scan_mode)
{
if (!IsEnabled()) return false;
switch (scan_mode)
{
case ScanMode::kConnectable:
Discoverable(false);
return true;
case ScanMode::kConnectableDiscoverable:
Discoverable(true);
return true;
default:
return false;
}
}
std::string BluetoothAdapter::GetMacAddress() const
{
return Address();
}
std::string BluetoothAdapter::GetName() const
{
return Alias();
}
bool BluetoothAdapter::SetName(absl::string_view name)
{
try {
Alias(std::string(name));
return true;
} catch (const sdbus::Error&) {return false;}
}
bool BluetoothAdapter::SetName(absl::string_view name,bool persist)
{
BluetoothAdapter::SetName(name);
}
} // namespace linux
} // namespace nearby
@@ -0,0 +1,104 @@
// Copyright 2020 Google LLC
//
// 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
//
// https://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.
#ifndef PLATFORM_LINUX_IMPL_BLUETOOTH_ADAPTER_H_
#define PLATFORM_LINUX_IMPL_BLUETOOTH_ADAPTER_H_
#include <string>
#include <sdbus-c++/AdaptorInterfaces.h>
#include <sdbus-c++/ProxyInterfaces.h>
#include "absl/base/attributes.h"
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/linux/generated/bluez_adapter_client_glue.h"
constexpr uint8_t kAndroidDiscoverableBluetoothNameMaxLength = 37; // bytes
namespace nearby {
namespace linux {
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html
class BluetoothAdapter : public api::BluetoothAdapter, public sdbus::ProxyInterfaces<org::bluez::Adapter1_proxy> {
public:
BluetoothAdapter(sdbus::IConnection& system_bus,
const sdbus::ObjectPath& object_path): ProxyInterfaces(
system_bus,
"org.bluez.Adap", object_path )
{
registerProxy();
};
~BluetoothAdapter() override
{
unregisterProxy();
};
//// Eligible statuses of the BluetoothAdapter.
//enum class Status {
// kDisabled,
// kEnabled,
//};
// Synchronously sets the status of the BluetoothAdapter to 'status', and
// returns true if the operation was a success.
bool SetStatus(Status status) = 0;
// Returns true if the BluetoothAdapter's current status is
// Status::Value::kEnabled.
bool IsEnabled() const = 0;
// Scan modes of a BluetoothAdapter, as described at
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getScanMode().
//enum class ScanMode {
// kUnknown,
// kNone,
// kConnectable,
// kConnectableDiscoverable,
//};
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getScanMode()
//
// Returns ScanMode::kUnknown on error.
ScanMode GetScanMode() const = 0;
// Synchronously sets the scan mode of the adapter, and returns true if the
// operation was a success.
bool SetScanMode(ScanMode scan_mode) = 0;
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getName()
// Returns an empty string on error
std::string GetName() const = 0;
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#setName(java.lang.String)
bool SetName(absl::string_view name) = 0;
bool SetName(absl::string_view name, bool persist) = 0;
// Returns BT MAC address assigned to this adapter.
ABSL_DEPRECATED("Use GetAddress() instead.")
std::string GetMacAddress() const = 0;
// Implementation for migration only. Once subclasses implement this, the
// above GetMacAddress() can be removed.
MacAddress GetAddress() const {
std::string mac_address = GetMacAddress();
if (mac_address.empty()) {
return MacAddress();
}
MacAddress address;
MacAddress::FromString(mac_address, address);
return address;
}
};
} // namespace linux
} // namespace nearby
#endif // PLATFORM_LINUX_IMPL_BLUETOOTH_ADAPTER_H_
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="org.bluez.Adapter1">
<method name="StartDiscovery" />
<method name="SetDiscoveryFilter">
<arg name="properties" type="a{sv}" direction="in" />
</method>
<method name="StopDiscovery" />
<method name="RemoveDevice">
<arg name="device" type="o" direction="in" />
</method>
<method name="GetDiscoveryFilters">
<arg name="filters" type="as" direction="out" />
</method>
<method name="ConnectDevice">
<arg name="properties" type="a{sv}" direction="in" />
</method>
<property name="Address" type="s" access="read" />
<property name="AddressType" type="s" access="read" />
<property name="Name" type="s" access="read" />
<property name="Alias" type="s" access="readwrite" />
<property name="Class" type="u" access="read" />
<property name="Powered" type="b" access="readwrite" />
<property name="PowerState" type="s" access="read" />
<property name="Discoverable" type="b" access="readwrite" />
<property name="DiscoverableTimeout" type="u" access="readwrite" />
<property name="Pairable" type="b" access="readwrite" />
<property name="PairableTimeout" type="u" access="readwrite" />
<property name="Discovering" type="b" access="read" />
<property name="UUIDs" type="as" access="read" />
<property name="Modalias" type="s" access="read" />
<property name="Roles" type="as" access="read" />
<property name="ExperimentalFeatures" type="as" access="read" />
</interface>
</node>
@@ -0,0 +1,14 @@
#ifndef WORKSPACE_PLATFORM_H
#define WORKSPACE_PLATFORM_H
#include "internal/platform/implementation/platform.h"
namespace nearby
{
namespace linux
{
}
}
#endif //WORKSPACE_PLATFORM_H