Skip to content

Commit

Permalink
Removes misguided attempt to implement the sending inside this class.
Browse files Browse the repository at this point in the history
Adds accessor for an abstract protocol engine instead.
  • Loading branch information
balazsracz committed Jul 8, 2024
1 parent 550ab81 commit 0b91ddd
Showing 1 changed file with 31 additions and 53 deletions.
84 changes: 31 additions & 53 deletions src/openlcb/BLEGattClient.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,23 @@
#ifndef _OPENLCB_BLEGATTCLIENT_HXX_
#define _OPENLCB_BLEGATTCLIENT_HXX_

#include <memory>

#include "ble/Connection.hxx"
#include "utils/Singleton.hxx"
#include "openlcb/BLEDefs.hxx"

namespace openlcb
{

// Forward declaration.
class BLEGattClients;

/// Metadata for a BLE Gatt Client instance.
class BLEGattClient
{
public:
/// Destructor.
~BLEGattClient()
{ }
{
}

/// Get the out bound data characteristic handle.
/// @return out bound data characteristic handle
Expand All @@ -66,54 +67,40 @@ public:
return conn_;
}

/// Save a protocol engine into this object's ownership. The new protocol
/// engine will be deleted when this client is disconnected. One
/// connection can set only one protocol engine. Any previous one will be
/// deleted in this call.
/// @param protocol the newly created protocol engine. Will take ownership
/// and delete it via abandon_and_delete.
void set_protocol_engine(BLEProtocolEnginePtr protocol)
{
protocol_ = std::move(protocol);
}

/// @return the protocol engine
BLEProtocolEngine* protocol_engine() {
return protocol_.get();
}

private:
/// Constructor.
/// @param conn BLE device connection
/// @param out_bound handle to out bound data characteristic
/// @param in_bound handle to in bound data characteristic
BLEGattClient(ble::Connection *conn, ble::Defs::AttHandle out_bound,
ble::Defs::AttHandle in_bound, ByteDirectHubInterface *hub,
Service *service)
ble::Defs::AttHandle in_bound)
: conn_(conn)
, outBound_(out_bound)
, inBound_(in_bound)
, port_(new BLEGattCHubPort(hub, service, this))
{ }

~BLEGattClient() {
/// @TODO: destroy hub port properly.
}

class BLEGattCHubPort : public BLEHubPort
{
public:
BLEGattCHubPort(ByteDirectHubInterface *hub, BLEGattClient *client,
Service *service)
: BLEHubPort(hub,
std::unique_ptr<MessageSegmenter>(
create_gc_message_segmenter()),
service)
, client_(client)
{ }

void do_send(const uint8_t *data, size_t len) override
{
esp_err_t result = esp_ble_gattc_write_char(
parent_->gattcProfileTable_[PROFILE_OLCB].interface_,
client->connection()->get_handle(), client->get_out_bound(),
200, data, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
ESP_LOGI("GATTC_DEMO", "write char status: 0x%x", result);
}

private:
/// Reference to the client object holding the various handles.
BLEGattClient *client_;
};

ble::Connection *conn_; ///< BLE device connection
ble::Defs::AttHandle outBound_; ///< handle to out bound data characteristic
ble::Defs::AttHandle inBound_; ///< handle to in bound data characteristic
std::unique_ptr<BLEHubPort> port_; ///< Direct hub I/O object.
/// This object implements the protocol over this connection. It is used
/// only for onwership and notifying it upon disconnect.
BLEProtocolEnginePtr protocol_;

/// Allow private access from BLEGattClients container object.
friend class BLEGattClients;
Expand All @@ -130,23 +117,19 @@ public:
&BLEGattClients::disconnect_callback, this, std::placeholders::_1));
}

void set_stack(ByteDirectHubInterface *hub, Service *send_service)
{
hub_ = hub;
sendService_ = send_service;
}

/// Register an active client.
/// @param conn BLE device connection
/// @param out_bound handle to out bound data characteristic
/// @param in_bound handle to in bound data characteristic
/// @return newly created client instance. Ownership is retained.
/// @return newly created client instance. Ownership is retained, and this
/// object will be deleted at an unspecified time.
BLEGattClient *register_client(ble::Connection *conn,
ble::Defs::AttHandle out_bound, ble::Defs::AttHandle in_bound)
{
auto *c =
new BLEGattClient(conn, out_bound, in_bound, hub, sendService_);
new BLEGattClient(conn, out_bound, in_bound);
clients_.emplace_back(c);
return c;
}

/// Invoke a callback method on each of the registered clients.
Expand All @@ -155,7 +138,7 @@ public:
{
for (auto it = clients_.begin(); it != clients_.end(); ++it)
{
callback(&(*it));
callback(it->get());
}
}

Expand All @@ -167,7 +150,7 @@ private:
/// Remove any clients using the given connection from the container.
for (auto it = clients_.begin(); it != clients_.end(); ++it)
{
if (it->conn_ == conn)
if ((*it)->conn_ == conn)
{
clients_.erase(it);
}
Expand All @@ -176,11 +159,6 @@ private:

/// All the BLE Gatt clients managed by the container.
std::vector<std::unique_ptr<BLEGattClient>> clients_;
/// Direct hub to interface with for traffic. Externally owned.
ByteDirectHubInterface *hub_;
/// This executor willbe used by the hub ports to perform the send calls on
/// the BLE stack.
Service *sendService_;
};

} // namespace openlcb
Expand Down

0 comments on commit 0b91ddd

Please sign in to comment.