Dawn Framework 1.0
Universal data acquisition framework for embedded systems
gatt_runtime.cxx
1// dawn/src/proto/nimble/gatt_runtime.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/proto/nimble/gatt_runtime.hxx"
7
8#include <cerrno>
9#include <cinttypes>
10#include <cstring>
11#include <new>
12
13using namespace dawn;
14
15void dawn::nimbleGattChrInit(struct ble_gatt_chr_def &chr, void *arg)
16{
17 chr.uuid = nullptr;
18 chr.arg = arg;
19 chr.descriptors = nullptr;
20 chr.flags = 0;
21 chr.min_key_size = 0;
22 chr.val_handle = nullptr;
23}
24
25int dawn::nimbleGattChrUuid128Set(struct ble_gatt_chr_def &chr, const uint32_t *uuid)
26{
27 if (uuid == nullptr)
28 {
29 return -EINVAL;
30 }
31
32 chr.uuid = new (std::nothrow) ble_uuid_t[16]();
33 if (chr.uuid == nullptr)
34 {
35 return -ENOMEM;
36 }
37
38 std::memcpy(const_cast<ble_uuid_t *>(chr.uuid), uuid, sizeof(uint8_t) * 16);
39 return OK;
40}
41
42void dawn::nimbleGattChrUuidFree(struct ble_gatt_chr_def &chr)
43{
44 if (chr.uuid != nullptr)
45 {
46 delete[] const_cast<ble_uuid_t *>(chr.uuid);
47 chr.uuid = nullptr;
48 }
49}
50
51void dawn::nimbleGattChrAccessSet(struct ble_gatt_chr_def &chr, CIOCommon &io)
52{
53 if (io.isRead())
54 {
55 chr.flags |= BLE_GATT_CHR_F_READ;
56 }
57
58 if (io.isWrite())
59 {
60 chr.flags |= BLE_GATT_CHR_F_WRITE;
61 }
62}
63
64#ifdef CONFIG_DAWN_IO_NOTIFY
65int dawn::nimbleGattChrNotifySet(struct ble_gatt_chr_def &chr,
66 CIOCommon &io,
68 int (*notifier)(void *priv, io_ddata_t *data),
70{
71 int ret;
72
73 if (!io.isNotify())
74 {
75 return OK;
76 }
77
78 ret = io.setNotifier(notifier, 0, &ncb);
79 if (ret < 0)
80 {
81 DAWNERR("ERROR: set notifier failed for objId = 0x%" PRIx32 "\n", objid);
82 return ret;
83 }
84
85 chr.flags |= BLE_GATT_CHR_F_NOTIFY;
86 chr.val_handle = &ncb.handle;
87 return OK;
88}
89#endif
90
91IProtoNimblePrphService::SPrphNotiferCb *dawn::nimbleGattNotifierCreate(CIOCommon *io)
92{
94
95 if (io == nullptr)
96 {
97 return nullptr;
98 }
99
100 ncb = new (std::nothrow) IProtoNimblePrphService::SPrphNotiferCb();
101 if (ncb == nullptr)
102 {
103 return nullptr;
104 }
105
106 ncb->io = io;
107 ncb->handle = 0;
108 ncb->data = io->ddata_alloc(1);
109 if (ncb->data == nullptr)
110 {
111 delete ncb;
112 return nullptr;
113 }
114
115 return ncb;
116}
117
118void dawn::nimbleGattNotifierFree(IProtoNimblePrphService::SPrphNotiferCb *ncb)
119{
120 if (ncb == nullptr)
121 {
122 return;
123 }
124
125#ifdef CONFIG_DAWN_IO_NOTIFY
126 if (ncb->io != nullptr && ncb->io->isNotify())
127 {
128 ncb->io->setNotifier(nullptr, 0, nullptr);
129 }
130#endif
131
132 if (ncb->data != nullptr)
133 {
134 delete ncb->data;
135 }
136 delete ncb;
137}
138
139void dawn::nimbleGattChrNotifierFree(struct ble_gatt_chr_def &chr)
140{
141 nimbleGattNotifierFree(static_cast<IProtoNimblePrphService::SPrphNotiferCb *>(chr.arg));
142 chr.arg = nullptr;
143}
Base class for all I/O objects.
Definition common.hxx:27
virtual bool isWrite() const =0
Check if IO supports write operations.
virtual bool isNotify() const =0
Check if IO supports notifications.
io_ddata_t * ddata_alloc(size_t batch, size_t chunk_size=0)
Allocate data buffer for this I/O.
Definition common.cxx:247
virtual bool isRead() const =0
Check if IO supports read operations.
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Context data for characteristic notification callbacks.
Definition iprph.hxx:476
io_ddata_t * data
GATT attribute handle for notification.
Definition iprph.hxx:481
uint16_t handle
Pointer to I/O object being monitored.
Definition iprph.hxx:479
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
Heap-allocated dynamic I/O data buffer.
Definition ddata.hxx:21