Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
BLEGattClient.hxx
Go to the documentation of this file.
1
28#ifndef _OPENLCB_BLEGATTCLIENT_HXX_
29#define _OPENLCB_BLEGATTCLIENT_HXX_
30
31#include <memory>
32
33#include "ble/Connection.hxx"
34#include "utils/Singleton.hxx"
35#include "openlcb/BLEDefs.hxx"
36
37namespace openlcb
38{
39
42{
43public:
46 {
47 }
48
51 ble::Defs::AttHandle get_out_bound()
52 {
53 return outBound_;
54 }
55
58 ble::Defs::AttHandle get_in_bound()
59 {
60 return inBound_;
61 }
62
66 {
67 return conn_;
68 }
69
76 void set_protocol_engine(BLEProtocolEnginePtr protocol)
77 {
78 protocol_ = std::move(protocol);
79 }
80
83 return protocol_.get();
84 }
85
86private:
91 BLEGattClient(ble::Connection *conn, ble::Defs::AttHandle out_bound,
92 ble::Defs::AttHandle in_bound)
93 : conn_(conn)
94 , outBound_(out_bound)
95 , inBound_(in_bound)
96 { }
97
99 ble::Defs::AttHandle outBound_;
100 ble::Defs::AttHandle inBound_;
103 BLEProtocolEnginePtr protocol_;
104
106 friend class BLEGattClients;
107};
108
110class BLEGattClients : public Singleton<BLEGattClients>
111{
112public:
115 {
116 ble::Connections::instance()->register_disconnect_callback(std::bind(
117 &BLEGattClients::disconnect_callback, this, std::placeholders::_1));
118 }
119
127 ble::Defs::AttHandle out_bound, ble::Defs::AttHandle in_bound)
128 {
129 auto *c =
130 new BLEGattClient(conn, out_bound, in_bound);
131 clients_.emplace_back(c);
132 return c;
133 }
134
137 ble::Connection *conn, ble::Defs::AttHandle out_handle)
138 {
139 for (auto it = clients_.begin(); it != clients_.end(); ++it)
140 {
141 if (it->get()->connection() == conn &&
142 it->get()->get_out_bound() == out_handle)
143 {
144 return it->get();
145 }
146 }
147 return nullptr;
148 }
149
152 ble::Connection *conn, ble::Defs::AttHandle in_handle)
153 {
154 for (auto it = clients_.begin(); it != clients_.end(); ++it)
155 {
156 if (it->get()->connection() == conn &&
157 it->get()->get_in_bound() == in_handle)
158 {
159 return it->get();
160 }
161 }
162 return nullptr;
163 }
164
167 void for_each_call(std::function<void(BLEGattClient *)> callback)
168 {
169 for (auto it = clients_.begin(); it != clients_.end(); ++it)
170 {
171 callback(it->get());
172 }
173 }
174
175private:
179 {
181 for (int i = 0; i < (int)clients_.size(); ++i)
182 {
183 if (clients_[i]->conn_ == conn)
184 {
185 clients_.erase(clients_.begin() + i);
186 --i;
187 continue;
188 }
189 }
190 }
191
193 std::vector<std::unique_ptr<BLEGattClient>> clients_;
194};
195
196} // namespace openlcb
197
198#endif // _OPENLCB_BLEGATTCLIENT_HXX_
Singleton class.
Definition Singleton.hxx:65
static Connections * instance()
Definition Singleton.hxx:77
Connection instance metadata.
Metadata for a BLE Gatt Client instance.
ble::Defs::AttHandle get_out_bound()
Get the out bound data characteristic handle.
ble::Defs::AttHandle inBound_
handle to in bound data characteristic
void set_protocol_engine(BLEProtocolEnginePtr protocol)
Save a protocol engine into this object's ownership.
ble::Connection * conn_
BLE device connection.
ble::Defs::AttHandle outBound_
handle to out bound data characteristic
BLEGattClient(ble::Connection *conn, ble::Defs::AttHandle out_bound, ble::Defs::AttHandle in_bound)
Constructor.
ble::Defs::AttHandle get_in_bound()
Get the in bound data characteristic handle.
BLEProtocolEngine * protocol_engine()
ble::Connection * connection()
Get the client connection.
BLEProtocolEnginePtr protocol_
This object implements the protocol over this connection.
Singleton container of all the BLE Gatt Clients.
void disconnect_callback(ble::Connection *conn)
Callback for when a connection disconnect occurs.
BLEGattClient * find_client_by_out(ble::Connection *conn, ble::Defs::AttHandle out_handle)
BLEGattClient * register_client(ble::Connection *conn, ble::Defs::AttHandle out_bound, ble::Defs::AttHandle in_bound)
Register an active client.
BLEGattClient * find_client_by_in(ble::Connection *conn, ble::Defs::AttHandle in_handle)
void for_each_call(std::function< void(BLEGattClient *)> callback)
Invoke a callback method on each of the registered clients.
std::vector< std::unique_ptr< BLEGattClient > > clients_
All the BLE Gatt clients managed by the container.
Shared base class for protocol implementation on a per-BLE-connection basis.
Definition BLEDefs.hxx:42