Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
BLEAPI.hpp
Go to the documentation of this file.
1#pragma once
2
9#include <Settings/NamespaceSettings.hpp>
10
11#include "Util/compat.hpp"
12
13#include <cstddef>
14#include <cstdint>
15
17
20 uint16_t conn = 0xFFFF;
21 explicit operator bool() const { return conn != 0xFFFF; }
22
23#if __cplusplus < 201402L
26#endif
27};
28
31 uint16_t characteristic = 0xFFFF;
32 explicit operator bool() const { return characteristic != 0xFFFF; }
33
34#if __cplusplus < 201402L
38#endif
39};
40
43 const uint8_t *data = nullptr;
44 uint16_t length = 0;
45 explicit operator bool() const { return length > 0; }
46
47#if __cplusplus < 201402L
48 BLEDataView() = default;
49 BLEDataView(const uint8_t *data, uint16_t length)
50 : data {data}, length {length} {}
51#endif
52};
53
58enum class BLEDataType : uint8_t {
59 None = 0,
60 Packet,
62};
63
67 public:
75 void clear();
77 explicit operator bool() const { return instance; }
78
80 BLEDataGenerator() = default;
82 template <class T, class... Args>
86 template <class T>
93
94 private:
96 struct Iface;
99 template <class T>
100 struct Impl;
102 using buffer_align_t = max_align_t;
104 static constexpr size_t capacity = 4 * sizeof(void *) - sizeof(Iface *);
108 Iface *instance = nullptr;
109};
110
113enum class BLEDataLifetime {
118 Managed,
119};
120
128 public:
129 virtual ~MIDIBLEInstance() = default;
131 virtual void handleConnect(BLEConnectionHandle conn_handle) = 0;
133 virtual void handleDisconnect(BLEConnectionHandle conn_handle) = 0;
136 virtual void handleMTU(BLEConnectionHandle conn_handle, uint16_t mtu) = 0;
139 virtual void handleSubscribe(BLEConnectionHandle conn_handle,
140 BLECharacteristicHandle char_handle,
141 bool notify) = 0;
144 virtual void handleData(BLEConnectionHandle conn_handle,
145 BLEDataGenerator &&data,
146 BLEDataLifetime lifetime) = 0;
147};
148
152 const char *device_name = "Control Surface MIDI";
155 struct {
156 uint16_t minimum = 0x000C;
157 uint16_t maximum = 0x000C;
159};
160
162
163#include "BLEAPI.ipp"
BLEDataType
Describes a byte buffer containing (part of) a BLE packet.
Definition BLEAPI.hpp:58
@ None
No buffers available.
@ Continuation
Buffer contains a chunk of a BLE packet.
@ Packet
Buffer contains the start of a BLE packet.
BLEDataLifetime
Should a buffer of BLEData be consumed immediately inside of the callback, or can we hold on to it an...
Definition BLEAPI.hpp:113
@ ConsumeImmediately
Buffer is valid only during the callback. Do not keep any pointers to it.
@ Managed
Buffer is valid for as long as the owning BLEDataGenerator is not resumed or destroyed.
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Callable that returns the next chunk of data from a BLE packet when called.
Definition BLEAPI.hpp:66
Iface * instance
Definition BLEAPI.hpp:108
BLEDataGenerator(compat::in_place_type_t< T >, Args &&...args)
Store a callable of type T and initialize it by args.
BLEDataGenerator & operator=(BLEDataGenerator &&other) noexcept
BLEDataGenerator(BLEDataGenerator &&other) noexcept
BLEDataGenerator()=default
Create an empty BLEDataGenerator.
BLEDataGenerator & operator=(const BLEDataGenerator &)=delete
static constexpr size_t capacity
Size of the buffer to allocate the underlying data generator.
Definition BLEAPI.hpp:104
BLEDataGenerator(compat::in_place_t, T &&t)
Store a callable of type T (with cv qualifiers and references removed) and initialize it by forwardin...
void clear()
Release the resources of the underlying data generator.
max_align_t buffer_align_t
Alignment of the buffer to allocate the underlying data generator.
Definition BLEAPI.hpp:102
compat::byte storage[capacity]
Buffer used for allocation of the underlying data generator.
Definition BLEAPI.hpp:106
BLEDataView operator()()
Get the next chunk of data from the BLE packet.
BLEDataGenerator(const BLEDataGenerator &)=delete
Defines the interface for callback functions registered by the low-level BLE code.
Definition BLEAPI.hpp:127
virtual void handleData(BLEConnectionHandle conn_handle, BLEDataGenerator &&data, BLEDataLifetime lifetime)=0
Called by the BLE stack when the central writes data to the MIDI GATT characteristic.
virtual void handleConnect(BLEConnectionHandle conn_handle)=0
Called by the BLE stack when a connection is established.
virtual void handleSubscribe(BLEConnectionHandle conn_handle, BLECharacteristicHandle char_handle, bool notify)=0
Called by the BLE stack when the central subscribes to receive notifications for the MIDI GATT charac...
virtual ~MIDIBLEInstance()=default
virtual void handleDisconnect(BLEConnectionHandle conn_handle)=0
Called by the BLE stack when a connection is terminated.
virtual void handleMTU(BLEConnectionHandle conn_handle, uint16_t mtu)=0
Called by the BLE stack when the maximum transmission unit for the connection changes.
Represents a handle to a local GATT characteristic.
Definition BLEAPI.hpp:30
BLECharacteristicHandle(uint16_t characteristic)
Definition BLEAPI.hpp:36
BLECharacteristicHandle()=default
Represents a handle to the connection to another device.
Definition BLEAPI.hpp:19
BLEConnectionHandle()=default
BLEConnectionHandle(uint16_t conn)
Definition BLEAPI.hpp:25
Specific class that implements the type-erased interface, wrapping the type T.
Definition BLEAPI.hpp:100
Non-owning, std::span-style read-only view of BLE data.
Definition BLEAPI.hpp:42
uint16_t length
Definition BLEAPI.hpp:44
BLEDataView()=default
const uint8_t * data
Definition BLEAPI.hpp:43
BLEDataView(const uint8_t *data, uint16_t length)
Definition BLEAPI.hpp:49
Configuration options for the low-level BLE code.
Definition BLEAPI.hpp:150
struct BLESettings::@4 connection_interval
Connection intervals as multiples of 1.25 milliseconds (e.g.0x000C = 15 ms).
uint16_t maximum
Definition BLEAPI.hpp:157
const char * device_name
Device name (used for advertising)
Definition BLEAPI.hpp:152
uint16_t minimum
Definition BLEAPI.hpp:156