libGulliBLE
controller.hpp
Go to the documentation of this file.
1//
2// Created by spak on 10/2/21.
3//
4
5#ifndef LIBGULLIBLE_CONTROLLER_HPP
6#define LIBGULLIBLE_CONTROLLER_HPP
7
8#include <ble/service.hpp>
9#include <vector>
10
11namespace ble {
12 namespace {
13 using namespace std::chrono_literals;
14 }
15
19 enum struct device_appearance : std::uint16_t {
20 none = 0x0,
22 };
23
26
27 protected:
28 [[nodiscard]] retcode do_read(mlab::bin_data &data, std::uint16_t, std::uint16_t) override;
29
30 [[nodiscard]] retcode do_write(mlab::bin_data const &data, std::uint16_t, std::uint16_t) override;
31 };
32
33 class controller : public mlab::uniquely_tracked {
34
35 template <class T>
36 struct dyn_c_vec {
37 std::unique_ptr<T[]> v = nullptr;
38 std::size_t size = 0;
39
40 void resize(std::size_t new_size) {
41 v = std::unique_ptr<T[]>(new T[new_size]);
42 size = new_size;
43 }
44 };
45
46 std::vector<service::snapshot> _svc_snapshots;
47 std::unique_ptr<ble_gatt_svc_def[]> _nimble_svc_defs;
51 std::uint8_t _own_address;
53
54 static void _nimble_main_task(void *);
55
56 static void _nimble_reset_cbk(int reason);
57
58 static void _nimble_sync_cbk();
59
60 static int _nimble_on_gap_event_cbk(ble_gap_event *event, void *tracker);
61
62 protected:
63 void ensure_own_address();
64
65 void configure_services(bool start_gatts_manually = false);
66
67 public:
68 explicit controller(std::vector<service> const &svcs);
69
71
72 noexcept = delete;
73
74 controller(controller const &) = delete;
75
76 controller &operator=(controller &&)
77
78 noexcept = delete;
79
80 controller &operator=(controller const &) = delete;
81
82 void start();
83
84 void stop();
85
89 void advertise_start(std::chrono::milliseconds duration = 0ms);
90
91 void advertise_stop();
92
93 [[nodiscard]] bool is_running() const;
94
95 [[nodiscard]] bool is_advertising() const;
96
97 [[nodiscard]] inline bool auto_advertise() const;
98
99 inline void set_auto_advertise(bool aa);
100
101 [[nodiscard]] std::string get_device_name() const;
102
103 [[nodiscard]] std::uint16_t get_device_appearance() const;
104
105 void set_device_name(std::string const &new_name);
106
107 void set_device_appearance(std::uint16_t new_appearance);
108
109 inline void set_device_appearance(device_appearance new_appearance);
110
111 [[nodiscard]] inline std::vector<service::snapshot> const &services() const;
112
113 ~controller();
114 };
115
116 enum struct gap_event : std::uint8_t {
117 connect = BLE_GAP_EVENT_CONNECT,
118 disconnect = BLE_GAP_EVENT_DISCONNECT,
119 conn_update = BLE_GAP_EVENT_CONN_UPDATE,
120 conn_update_req = BLE_GAP_EVENT_CONN_UPDATE_REQ,
121 l2cap_update_req = BLE_GAP_EVENT_L2CAP_UPDATE_REQ,
122 term_failure = BLE_GAP_EVENT_TERM_FAILURE,
123 disc = BLE_GAP_EVENT_DISC,
124 disc_complete = BLE_GAP_EVENT_DISC_COMPLETE,
125 adv_complete = BLE_GAP_EVENT_ADV_COMPLETE,
126 enc_change = BLE_GAP_EVENT_ENC_CHANGE,
127 passkey_action = BLE_GAP_EVENT_PASSKEY_ACTION,
128 notify_rx = BLE_GAP_EVENT_NOTIFY_RX,
129 notify_tx = BLE_GAP_EVENT_NOTIFY_TX,
130 subscribe = BLE_GAP_EVENT_SUBSCRIBE,
131 mtu = BLE_GAP_EVENT_MTU,
132 identity_resolved = BLE_GAP_EVENT_IDENTITY_RESOLVED,
133 repeat_pairing = BLE_GAP_EVENT_REPEAT_PAIRING,
134 phy_update_complete = BLE_GAP_EVENT_PHY_UPDATE_COMPLETE,
135 ext_disc = BLE_GAP_EVENT_EXT_DISC,
136 periodic_sync = BLE_GAP_EVENT_PERIODIC_SYNC,
137 periodic_report = BLE_GAP_EVENT_PERIODIC_REPORT,
138 periodic_sync_lost = BLE_GAP_EVENT_PERIODIC_SYNC_LOST,
139 scan_req_rcvd = BLE_GAP_EVENT_SCAN_REQ_RCVD
140 };
141
142 [[nodiscard]] const char *to_string(gap_event evt);
143}// namespace ble
144
145namespace ble {
146
147 std::vector<service::snapshot> const &controller::services() const {
148 return _svc_snapshots;
149 }
150
152 set_device_appearance(static_cast<std::uint16_t>(new_appearance));
153 }
154
156 return _auto_advertise;
157 }
158
160 _auto_advertise = aa;
161 }
162
163
164}// namespace ble
165
166#endif//LIBGULLIBLE_CONTROLLER_HPP
mlab::bin_data & data
Definition: characteristic.cpp:49
Definition: characteristic.hpp:52
Definition: controller.hpp:33
void advertise_stop()
Definition: controller.cpp:256
std::vector< service::snapshot > const & services() const
Definition: controller.hpp:147
std::uint16_t get_device_appearance() const
Definition: controller.cpp:164
void set_auto_advertise(bool aa)
Definition: controller.hpp:159
static void _nimble_main_task(void *)
Definition: controller.cpp:268
std::string get_device_name() const
Definition: controller.cpp:160
std::unique_ptr< ble_gatt_svc_def[]> _nimble_svc_defs
Definition: controller.hpp:47
bool auto_advertise() const
Definition: controller.hpp:155
static void _nimble_sync_cbk()
Definition: controller.cpp:332
dyn_c_vec< ble_uuid128_t > _nimble_svc_uuid128
Definition: controller.hpp:50
void set_device_appearance(std::uint16_t new_appearance)
Definition: controller.cpp:168
static void _nimble_reset_cbk(int reason)
Definition: controller.cpp:281
std::vector< service::snapshot > _svc_snapshots
Definition: controller.hpp:46
void ensure_own_address()
Definition: controller.cpp:318
void start()
Definition: controller.cpp:345
dyn_c_vec< ble_uuid32_t > _nimble_svc_uuid32
Definition: controller.hpp:49
controller(std::vector< service > const &svcs)
Definition: controller.cpp:50
controller(controller &&) noexcept=delete
void set_device_name(std::string const &new_name)
Definition: controller.cpp:153
void advertise_start(std::chrono::milliseconds duration=0ms)
Definition: controller.cpp:175
std::uint8_t _own_address
Definition: controller.hpp:51
void stop()
Definition: controller.cpp:359
bool _auto_advertise
Definition: controller.hpp:52
void configure_services(bool start_gatts_manually=false)
Definition: controller.cpp:134
dyn_c_vec< ble_uuid16_t > _nimble_svc_uuid16
Definition: controller.hpp:48
static int _nimble_on_gap_event_cbk(ble_gap_event *event, void *tracker)
Definition: controller.cpp:285
bool is_advertising() const
Definition: controller.cpp:264
bool is_running() const
Definition: controller.cpp:355
Definition: service.hpp:17
Definition: uuid.hpp:72
Definition: characteristic.hpp:14
device_appearance
Definition: controller.hpp:19
retcode
Definition: characteristic.hpp:16
gap_event
Definition: controller.hpp:116
const char * to_string(gap_event evt)
Definition: controller.cpp:365
Definition: controller.hpp:36
void resize(std::size_t new_size)
Definition: controller.hpp:40
std::size_t size
Definition: controller.hpp:38
std::unique_ptr< T[]> v
Definition: controller.hpp:37
Definition: controller.hpp:24
retcode do_write(mlab::bin_data const &data, std::uint16_t, std::uint16_t) override
Definition: controller.cpp:44
retcode do_read(mlab::bin_data &data, std::uint16_t, std::uint16_t) override
Definition: controller.cpp:37
device_name_characteristic(uuid u)
Definition: controller.cpp:35