Skip to content

Commit

Permalink
Advertising demo with manufacturer data works.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdewald committed Oct 6, 2024
1 parent 55c7454 commit aaf1525
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 14 deletions.
4 changes: 3 additions & 1 deletion examples/simplebluez/advertise/advertise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ int main(int argc, char* argv[]) {
adapter->register_advertisement(advertisement->path());

// Sleep for a bit to allow the adapter to stop discovering.
millisecond_delay(3000);
millisecond_delay(8000);

adapter->unregister_advertisement(advertisement->path());

async_thread_active = false;
while (!async_thread->joinable()) {
Expand Down
3 changes: 3 additions & 0 deletions simplebluez/include/simplebluez/CustomAdvertisement.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include <simpledbus/advanced/Proxy.h>

#include <simplebluez/interfaces/LEAdvertisement1.h>
#include <simpledbus/interfaces/ObjectManager.h>

namespace SimpleBluez {

Expand All @@ -13,6 +15,7 @@ class CustomAdvertisement : public SimpleDBus::Proxy {

private:
std::shared_ptr<LEAdvertisement1> le_advertisement1();
std::shared_ptr<SimpleDBus::ObjectManager> object_manager();
};

} // namespace SimpleBluez
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CustomAdvertisementManager : public SimpleDBus::Proxy {
void remove_advertisement(const std::string& path);

private:
std::shared_ptr<SimpleDBus::ObjectManager> object_manager();

};

} // namespace SimpleBluez
1 change: 1 addition & 0 deletions simplebluez/src/Adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void Adapter::register_advertisement(const std::string& advertisement_path) {
}

void Adapter::unregister_advertisement(const std::string& advertisement_path) {
// TODO: We should keep track of all registered advertisements and unregister them when the adapter is destroyed.
le_advertising_manager1()->UnregisterAdvertisement(advertisement_path);
}

Expand Down
5 changes: 5 additions & 0 deletions simplebluez/src/CustomAdvertisement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ CustomAdvertisement::CustomAdvertisement(std::shared_ptr<SimpleDBus::Connection>
: Proxy(conn, bus_name, path) {

_interfaces.emplace(std::make_pair("org.bluez.LEAdvertisement1", std::make_shared<LEAdvertisement1>(conn, this)));
_interfaces.emplace(std::make_pair("org.freedesktop.DBus.ObjectManager", std::make_shared<SimpleDBus::ObjectManager>(conn, this)));
}

std::shared_ptr<LEAdvertisement1> CustomAdvertisement::le_advertisement1() {
return std::dynamic_pointer_cast<LEAdvertisement1>(interface_get("org.bluez.LEAdvertisement1"));
}

std::shared_ptr<SimpleDBus::ObjectManager> CustomAdvertisement::object_manager() {
return std::dynamic_pointer_cast<SimpleDBus::ObjectManager>(interface_get("org.freedesktop.DBus.ObjectManager"));
}
6 changes: 0 additions & 6 deletions simplebluez/src/CustomAdvertisementManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ using namespace SimpleBluez;

CustomAdvertisementManager::CustomAdvertisementManager(std::shared_ptr<SimpleDBus::Connection> conn, const std::string& bus_name, const std::string& path)
: Proxy(conn, bus_name, path) {

_interfaces.emplace(std::make_pair("org.freedesktop.DBus.ObjectManager", std::make_shared<SimpleDBus::ObjectManager>(conn, this)));
}

std::shared_ptr<SimpleBluez::CustomAdvertisement> CustomAdvertisementManager::create_advertisement(const std::string& path) {
Expand All @@ -20,7 +18,3 @@ void CustomAdvertisementManager::remove_advertisement(const std::string& path) {
const std::string advertisement_path = _path + "/" + path;
path_remove_child(advertisement_path);
}

std::shared_ptr<SimpleDBus::ObjectManager> CustomAdvertisementManager::object_manager() {
return std::dynamic_pointer_cast<SimpleDBus::ObjectManager>(interface_get("org.freedesktop.DBus.ObjectManager"));
}
5 changes: 4 additions & 1 deletion simplebluez/src/interfaces/LEAdvertisement1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ LEAdvertisement1::LEAdvertisement1(std::shared_ptr<SimpleDBus::Connection> conn,
// Set all fields to null or empty values
SetType("peripheral"); // This is the only required field, so we set a default
// SetServiceUUIDs({}); // Empty vector
// SetManufacturerData({}); // Empty map

std::map<uint16_t, std::vector<uint8_t>> data;
data[0x1024] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
SetManufacturerData(data); // Empty map
// SetServiceData({}); // Empty map
// SetSolicitUUIDs({}); // Empty vector
// SetData({}); // Empty map
Expand Down
8 changes: 4 additions & 4 deletions simpledbus/include/simpledbus/base/Holder.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <any>
#include <optional>
#include <cstdint>
#include <map>
#include <string>
Expand Down Expand Up @@ -61,6 +62,7 @@ class Holder {
Type type() const;
std::string represent();
std::string signature();
void signature_override(const std::string& signature);

// TODO: Deprecate these functions in favor of templated version.
static Holder create_boolean(bool value);
Expand Down Expand Up @@ -120,16 +122,14 @@ class Holder {

private:
Type _type = NONE;
std::optional<std::string> _signature;

bool holder_boolean = false;
uint64_t holder_integer = 0;
double holder_double = 0;

std::string holder_string;
std::vector<Holder> holder_array;

// Dictionaries are stored within a vector as a tuple of <key_type, key, holder>
std::vector<std::tuple<Type, std::any, Holder>> holder_dict;
std::vector<std::tuple<Type, std::any, Holder>> holder_dict; // Dictionaries are stored within a vector as a tuple of <key_type, key, holder>

std::vector<std::string> _represent_container();
std::string _represent_simple();
Expand Down
11 changes: 10 additions & 1 deletion simpledbus/src/base/Holder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,16 @@ std::string Holder::_represent_type(Type type, std::any value) {
return output.str();
}

void Holder::signature_override(const std::string& signature) {
// TODO: Check that the signature is valid for the Holder type and contents.
_signature = signature;
}

std::string Holder::signature() {
if (_signature) {
return *_signature;
}

std::string output;
switch (_type) {
case BOOLEAN:
Expand Down Expand Up @@ -343,7 +352,7 @@ std::string Holder::signature() {
}
}

if (all_same_value_type) {
if (all_same_value_type && first_value_type != ARRAY && first_value_type != DICT) {
output += std::get<2>(holder_dict[0])._signature_simple();
} else {
output += DBUS_TYPE_VARIANT_AS_STRING;
Expand Down

0 comments on commit aaf1525

Please sign in to comment.