Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Connection.hxx
Go to the documentation of this file.
1
28#ifndef _BLE_CONNECTION_HXX_
29#define _BLE_CONNECTION_HXX_
30
31#include <functional>
32#include <vector>
33#include <cstring>
34
35#include "ble/Defs.hxx"
36#include "utils/Singleton.hxx"
37
38namespace ble
39{
40
41// Forward declaration.
42class Connections;
43
46{
47public:
51 {
52 return handle_;
53 }
54
57 void get_addr(Defs::Addr addr, Defs::AddrType *addr_type)
58 {
59 memcpy(addr, addr_, Defs::ADDR_LEN);
60 if (addr_type)
61 {
62 *addr_type = addrType_;
63 }
64 }
65
68 {
69 }
70
71private:
78 Defs::AddrType addr_type, bool central)
79 : handle_(handle)
80 , addrType_(addr_type)
81 , central_(central)
82 {
83 memcpy(addr_, addr, Defs::ADDR_LEN);
84 }
85
89 bool central_;
90
92 friend class Connections;
93};
94
96class Connections : public Singleton<Connections>
97{
98public:
101 {
102 }
103
110 Defs::AddrType addr_type, bool central)
111 {
112 connections_.emplace_back(Connection(handle, addr, addr_type, central));
113 }
114
118 std::function<void(Connection *connection)> callback)
119 {
120 callbacks_.emplace_back(callback);
121 }
122
127 {
128 // Execute all of the registered disconnect callbacks.
129 for (auto &e : callbacks_)
130 {
131 e(conn);
132 }
133 // Remove the connection instance from the container.
134 for (auto it = connections_.begin(); it != connections_.end(); ++it)
135 {
136 if (&(*it) == conn)
137 {
138 connections_.erase(it);
139 // Should only be one entry per connection in the container.
140 return;
141 }
142 }
143 }
144
149 {
150 // Remove the connection instance from the container.
151 for (auto it = connections_.begin(); it != connections_.end(); ++it)
152 {
153 if (it->get_handle() == handle)
154 {
155 // Execute all of the registered disconnect callbacks.
156 for (auto &e : callbacks_)
157 {
158 e(&(*it));
159 }
160 connections_.erase(it);
161 // Should only be one entry per connection in the container.
162 return;
163 }
164 }
165 }
166
172 {
173 for (auto &e : connections_)
174 {
175 if (!memcmp(e.addr_, addr, Defs::ADDR_LEN) &&
176 e.addrType_ == addr_type)
177 {
178 // Match.
179 return &e;
180 }
181 }
182 return nullptr;
183 }
184
189 {
190 for (auto &e : connections_)
191 {
192 if (e.handle_ == handle)
193 {
194 // Match.
195 return &e;
196 }
197 }
198 return nullptr;
199 }
200
204 {
205 return connections_.size();
206 }
207
208private:
210 std::vector<Connection> connections_;
212 std::vector<std::function<void(Connection *connection)>> callbacks_;
213};
214
215} // namespace ble
216
217#endif // _BLE_CONNECTION_HXX_
Singleton class.
Definition Singleton.hxx:65
Connection instance metadata.
bool central_
true if central role, else peripheral role
Defs::ConnHandle get_handle()
Get the connection handle.
Defs::Addr addr_
peer address
void get_addr(Defs::Addr addr, Defs::AddrType *addr_type)
Get the connection address.
Defs::AddrType addrType_
peer address type
Defs::ConnHandle handle_
handle to the connection
Connection(Defs::ConnHandle handle, Defs::Addr addr, Defs::AddrType addr_type, bool central)
Constructor.
~Connection()
Destructor. This happens when there is a disconnect.
Singleton container of all the active connections.
std::vector< Connection > connections_
All the connections managed by the container.
std::vector< std::function< void(Connection *connection)> > callbacks_
Callbacks to be called upon disconnection.
void register_connection(Defs::ConnHandle handle, Defs::Addr addr, Defs::AddrType addr_type, bool central)
Register an active connection.
void disconnect(Defs::ConnHandle handle)
Called when a connection is disconnected.
void register_disconnect_callback(std::function< void(Connection *connection)> callback)
Register a callback at disconnection.
size_t get_active_count()
Get the number of active connections being tracked by this object.
void disconnect(Connection *conn)
Called when a connection is disconnected.
Connection * lookup_by_handle(Defs::ConnHandle handle)
Find a connection by its connection handle.
Connection * lookup_by_address(Defs::Addr addr, Defs::AddrType addr_type)
Find a connection by its peer address.
Connections()
Constructor.
uint8_t AddrType
Address Type.
Definition ble/Defs.hxx:76
uint16_t ConnHandle
Connection handle.
Definition ble/Defs.hxx:79
uint8_t Addr[ADDR_LEN]
BLE address.
Definition ble/Defs.hxx:73
static constexpr uint8_t ADDR_LEN
The length of an address.
Definition ble/Defs.hxx:49