5#ifndef LIBGULLIBLE_CHARACTERISTIC_HPP
6#define LIBGULLIBLE_CHARACTERISTIC_HPP
9#include <mlab/bin_data.hpp>
10#include <mlab/observable.hpp>
11#include <mlab/unique_tracker.hpp>
51 :
public mlab::uniquely_tracked,
52 public std::enable_shared_from_this<characteristic_base> {
60 ble_gatt_access_ctxt *ctxt,
void *tracker);
63 [[nodiscard]]
virtual retcode on_access(std::uint16_t conn_handle, std::uint16_t attr_handle,
64 ble_gatt_access_ctxt &ctxt);
66 [[nodiscard]]
virtual retcode on_read(std::uint16_t conn_handle, std::uint16_t attr_handle,
67 ble_gatt_access_ctxt &ctxt);
69 [[nodiscard]]
virtual retcode on_write(std::uint16_t conn_handle, std::uint16_t attr_handle,
70 ble_gatt_access_ctxt &ctxt);
73 std::uint16_t attr_handle) = 0;
76 std::uint16_t attr_handle) = 0;
83 .descriptors =
nullptr,
86 .val_handle =
nullptr};
101 template <
class T,
bool>
103 std::shared_ptr<mlab::observable<T>>
_value;
107 std::shared_ptr<mlab::observable<T>> init =
nullptr);
111 [[nodiscard]]
inline mlab::observable_cref<T>
value()
const;
113 [[nodiscard]]
inline mlab::observable_ref<T>
value();
115 [[nodiscard]]
inline std::shared_ptr<mlab::observable<T>>
const &
value_ptr()
const;
122 std::uint16_t attr_handle)
override;
125 std::uint16_t attr_handle)
override;
138 T, mlab::bin_data::is_injectable<T>::value and mlab::bin_stream::is_extractable<T>::value> {
146 mlab::bin_stream::is_extractable<T>::value;
157 template <
class T,
bool B>
159 uuid const &u,
access_type access, std::shared_ptr<mlab::observable<T>> init)
161 _value{init != nullptr ? std::move(init) : std::make_shared<mlab::observable<T>>()} {}
163 template <
class T,
bool B>
167 _value{std::make_shared<mlab::observable<T>>(std::forward<T>(init))} {}
170 template <
class T,
bool B>
172 return _value->cref();
175 template <
class T,
bool B>
180 template <
class T,
bool B>
182 return _value->ref();
187 data << static_cast<T>(value());
194 mlab::bin_stream s{
data};
200 value() = std::forward<T>(obj);
215 return BLE_GATT_CHR_F_READ;
217 return BLE_GATT_CHR_F_WRITE;
219 return BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE;
228 return (
static_cast<std::uint8_t
>(l) &
static_cast<std::uint8_t
>(
r)) != 0;
232 return static_cast<access_type>(
static_cast<std::uint8_t
>(l) |
static_cast<std::uint8_t
>(
r));
mlab::bin_data & data
Definition: characteristic.cpp:49
Definition: characteristic.hpp:52
virtual retcode do_read(mlab::bin_data &data, std::uint16_t conn_handle, std::uint16_t attr_handle)=0
access_type access() const
Definition: characteristic.hpp:204
virtual ~characteristic_base()=default
virtual retcode on_write(std::uint16_t conn_handle, std::uint16_t attr_handle, ble_gatt_access_ctxt &ctxt)
Definition: characteristic.cpp:71
uuid _uuid
Definition: characteristic.hpp:54
virtual retcode do_write(mlab::bin_data const &data, std::uint16_t conn_handle, std::uint16_t attr_handle)=0
virtual retcode on_read(std::uint16_t conn_handle, std::uint16_t attr_handle, ble_gatt_access_ctxt &ctxt)
Definition: characteristic.cpp:58
virtual retcode on_access(std::uint16_t conn_handle, std::uint16_t attr_handle, ble_gatt_access_ctxt &ctxt)
Definition: characteristic.cpp:28
static int _nimble_characteristic_access_callback(std::uint16_t conn_handle, std::uint16_t attr_handle, ble_gatt_access_ctxt *ctxt, void *tracker)
Definition: characteristic.cpp:17
characteristic_base(uuid u, access_type access)
Definition: characteristic.cpp:13
access_type _access
Definition: characteristic.hpp:56
ble_gatt_chr_def get_definition() const
Definition: characteristic.cpp:85
uuid const & chr_uuid() const
Definition: characteristic.hpp:208
std::unique_ptr< ble_uuid_any_t > _nimble_uuid
Definition: characteristic.hpp:55
mlab::bin_data _buffer
Definition: characteristic.hpp:57
static constexpr ble_gatt_chr_def void_definition
Definition: characteristic.hpp:79
Definition: characteristic.hpp:102
std::shared_ptr< mlab::observable< T > > const & value_ptr() const
Definition: characteristic.hpp:176
stored_characteristic_base(uuid const &u, access_type access, std::shared_ptr< mlab::observable< T > > init=nullptr)
Definition: characteristic.hpp:158
mlab::observable_ref< T > value()
Definition: characteristic.hpp:181
std::shared_ptr< mlab::observable< T > > _value
Definition: characteristic.hpp:103
mlab::observable_cref< T > value() const
Definition: characteristic.hpp:171
stored_characteristic_base(uuid const &u, access_type access, T init)
Definition: characteristic.hpp:164
Definition: characteristic.hpp:138
static constexpr bool has_synthetic_rw_methods
True iff the do_read and do_write methods have already been automatically synthesized....
Definition: characteristic.hpp:145
Definition: characteristic.hpp:14
bool operator&(access_type l, access_type r)
Definition: characteristic.hpp:227
std::uint16_t to_nimble(access_type a)
Definition: characteristic.hpp:212
retcode
Definition: characteristic.hpp:16
access_type
Definition: characteristic.hpp:37
access_type operator|(access_type l, access_type r)
Definition: characteristic.hpp:231