Skip to content

Commit

Permalink
Decouple BLE observers from nordic
Browse files Browse the repository at this point in the history
Issue #36

ble::profile::connectable: add gatts, gattc classes

     struct ble::profile::connectable::gatts
     Aggregates ble::gatts::event_observer
                ble::gatts::operations.

     struct ble::profile::connectable::gattc
     Aggregates ble::gatts::event_observer,
                ble::gatts::operations,
                ble::gattc::service_builder.

Instead of registering each observer, wrapped in a
nordic observer template, with each nodic BLE event
observable register just the object
ble::profile::connectable with the BLE stack events.
  • Loading branch information
natersoz authored and nersoz-impinj committed May 22, 2022
1 parent 03d0368 commit c2e4e15
Show file tree
Hide file tree
Showing 18 changed files with 1,414 additions and 1,381 deletions.
4 changes: 2 additions & 2 deletions ble/gap_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ namespace gap
uint8_t peer_address_id)
{
this->set_handle(connection_handle);
this->get_connecteable()->connection().get_negotiation_state().clear_all_pending();
this->get_connecteable()->gap.get_negotiation_state().clear_all_pending();
}

void connection::disconnect(uint16_t connection_handle,
ble::hci::error_code error_code)
{
this->set_handle(ble::gap::handle_invalid);
this->get_connecteable()->connection().get_negotiation_state().clear_all_pending();
this->get_connecteable()->gap.get_negotiation_state().clear_all_pending();
}

} // namespace gap
Expand Down
2 changes: 1 addition & 1 deletion ble/gap_event_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class event_logger: public ble::gap::event_observer
public:
logger::level log_level;

virtual ~event_logger() override = default;
virtual ~event_logger() override = default;

event_logger(event_logger const&) = delete;
event_logger(event_logger &&) = delete;
Expand Down
65 changes: 65 additions & 0 deletions ble/gap_event_observable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @file ble/gap_event_observable.h
* @copyright (c) 2018, natersoz. Distributed under the Apache 2.0 license.
*/

#pragma once

#include "ble/gap_event_observer.h"

namespace ble
{
namespace gap
{

class event_observable
{
virtual ~event_observable() = default;

event_observable() = default;
event_observable(event_observable const&) = delete;
event_observable(event_observable &&) = delete;
event_observable& operator=(event_observable const&) = delete;
event_observable& operator=(event_observable&&) = delete;

void attach(event_observer& observer)
{
if (not observer.hook.is_linked())
{
this->observer_list.push_back(observer);
}
}

void attach_first(event_observer& observer)
{
if (not observer.hook.is_linked())
{
this->observer_list.push_front(observer);
}
}

void detach(event_observer& observer)
{
if (observer.hook.is_linked())
{
observer.hook.unlink();
}
}

private:
using list_type = boost::intrusive::list<
event_observer,
boost::intrusive::constant_time_size<false>,
boost::intrusive::member_hook<
event_observer,
typename event_observer::list_hook_type,
&event_observer::hook>
>;

list_type observer_list;
};

} // namespace gap
} // namespace ble


40 changes: 21 additions & 19 deletions ble/gatts_event_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void event_observer::write(uint16_t conection_handle,
return;
}

if (connectable->connection().get_connection_handle() != conection_handle)
if (connectable->gap.get_connection_handle() != conection_handle)
{
/// This GATTS request is from a different connection.
/// @todo Is it normal for this connection to get notified of other
Expand All @@ -52,7 +52,7 @@ void event_observer::write(uint16_t conection_handle,
}

ble::gatt::characteristic *characteristic =
connectable->service_container().find_characteristic(attribute_handle);
connectable->service_container.find_characteristic(attribute_handle);
if (not characteristic)
{
logger.warn("GATTS write(0x%04x, 0x%04x): invalid handle",
Expand Down Expand Up @@ -92,7 +92,7 @@ void event_observer::write_cancel(uint16_t conection_handle,
return;
}

if (connectable->connection().get_connection_handle() != conection_handle)
if (connectable->gap.get_connection_handle() != conection_handle)
{
// This GATTS request is from a different connection.
return;
Expand All @@ -113,7 +113,7 @@ void event_observer::read_authorization_request(uint16_t conection_handle,
return;
}

if (connectable->connection().get_connection_handle() != conection_handle)
if (connectable->gap.get_connection_handle() != conection_handle)
{
// This GATTS request is from a different connection.
return;
Expand All @@ -123,13 +123,14 @@ void event_observer::read_authorization_request(uint16_t conection_handle,
}

// BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST
void event_observer::write_authorization_request(uint16_t conection_handle,
uint16_t attribute_handle,
att::op_code write_operation_type,
bool authorization_required,
att::length_t offset,
att::length_t length,
void const* data)
void event_observer::write_authorization_request(
uint16_t conection_handle,
uint16_t attribute_handle,
att::op_code write_operation_type,
bool authorization_required,
att::length_t offset,
att::length_t length,
void const* data)
{
ble::profile::connectable* connectable = this->get_connecteable();
if (not connectable)
Expand All @@ -138,7 +139,7 @@ void event_observer::write_authorization_request(uint16_t conection_handle,
return;
}

if (connectable->connection().get_connection_handle() != conection_handle)
if (connectable->gap.get_connection_handle() != conection_handle)
{
// This GATTS request is from a different connection.
return;
Expand Down Expand Up @@ -175,19 +176,20 @@ void event_observer::exchange_mtu_request(uint16_t conection_handle,
return;
}

if (connectable->connection().get_connection_handle() != conection_handle)
if (connectable->gap.get_connection_handle() != conection_handle)
{
// This GATTS request is from a different connection.
return;
}

ble::att::length_t const att_mtu_length_maximum =
connectable->ble_stack().get_constraints().att_mtu_maximum_length;
connectable->stack.get_constraints().att_mtu_maximum_length;

client_rx_mtu_size = std::min(client_rx_mtu_size, att_mtu_length_maximum);
client_rx_mtu_size = std::max(client_rx_mtu_size, att::mtu_length_minimum);

connectable->gatts()->exchange_mtu_reply(conection_handle, client_rx_mtu_size);
connectable->gatts.operations->exchange_mtu_reply(conection_handle,
client_rx_mtu_size);
}

// BLE_GATTS_EVT_TIMEOUT, // always BLE_GATT_TIMEOUT_SRC_PROTOCOL (0)
Expand All @@ -200,7 +202,7 @@ void event_observer::timeout(uint16_t conection_handle, uint8_t timeout_source)
return;
}

if (connectable->connection().get_connection_handle() != conection_handle)
if (connectable->gap.get_connection_handle() != conection_handle)
{
// This GATTS request is from a different connection.
return;
Expand All @@ -211,8 +213,8 @@ void event_observer::timeout(uint16_t conection_handle, uint8_t timeout_source)
}

// BLE_GATTS_EVT_HVN_TX_COMPLETE
void event_observer::handle_value_notifications_tx_completed(uint16_t conection_handle,
uint8_t count)
void event_observer::handle_value_notifications_tx_completed(
uint16_t conection_handle, uint8_t count)
{
ble::profile::connectable* connectable = this->get_connecteable();
if (not connectable)
Expand All @@ -221,7 +223,7 @@ void event_observer::handle_value_notifications_tx_completed(uint16_t conection_
return;
}

if (connectable->connection().get_connection_handle() != conection_handle)
if (connectable->gap.get_connection_handle() != conection_handle)
{
// This GATTS request is from a different connection.
return;
Expand Down
59 changes: 19 additions & 40 deletions ble/nordic_ble_common_event_observable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,35 @@
* @copyright (c) 2018, natersoz. Distributed under the Apache 2.0 license.
*/

#include "ble/nordic_ble_event_observable.h"
#include "ble/nordic_ble_event_observer.h"

#include "logger.h"

namespace nordic
{

template<>
void ble_event_observable<ble_common_event_observer>::notify(
ble_common_event_observer::event_enum_t event_type,
ble_common_event_observer::event_data_t const& event_data)
void ble_common_event_notify(ble::common::event_observer& observer,
enum BLE_COMMON_EVTS event_type,
ble_common_evt_t const& event_data)
{
for (auto observer_iter = this->observer_list_.begin();
observer_iter != this->observer_list_.end(); )
auto const memory_type = static_cast<ble::common::memory_type>(
event_data.params.user_mem_request.type);

switch (event_type)
{
logger &logger = logger::instance();
case BLE_EVT_USER_MEM_REQUEST:
observer.memory_request(event_data.conn_handle, memory_type, 0u, 1u);
break;

// Increment the iterator prior to using it.
// If the client removes itself during the completion callback
// then the iterator will be valid and can continue.
auto &observer = *observer_iter;
++observer_iter;
case BLE_EVT_USER_MEM_RELEASE:
observer.memory_release(event_data.conn_handle,
memory_type,
event_data.params.user_mem_release.mem_block.p_mem,
event_data.params.user_mem_release.mem_block.len);
break;

switch (event_type)
{
case BLE_EVT_USER_MEM_REQUEST:
// User Memory request. @ref ble_evt_user_mem_request_t
{
observer.interface_reference.memory_request(
event_data.conn_handle,
static_cast<ble::common::memory_type>(event_data.params.user_mem_request.type),
0u,
1u);
}
break;
case BLE_EVT_USER_MEM_RELEASE:
// User Memory release. @ref ble_evt_user_mem_release_t
{
observer.interface_reference.memory_release(
event_data.conn_handle,
static_cast<ble::common::memory_type>(event_data.params.user_mem_release.type),
event_data.params.user_mem_release.mem_block.p_mem,
event_data.params.user_mem_release.mem_block.len);
}
break;
default:
logger.warn("unhandled Nordic common event: %u", event_type);
break;
}
default:
logger::instance().warn("unhandled Nordic common event: %u", event_type);
break;
}
}

Expand Down
111 changes: 0 additions & 111 deletions ble/nordic_ble_event_observable.h

This file was deleted.

Loading

0 comments on commit c2e4e15

Please sign in to comment.