1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
|
/*
* Author: Sven Gothel <sgothel@jausoft.com>
* Copyright (c) 2021 Gothel Software e.K.
* Copyright (c) 2021 ZAFENA AB
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef DB_GATT_SERVER_HPP_
#define DB_GATT_SERVER_HPP_
#include <cstring>
#include <string>
#include <memory>
#include <cstdint>
#include <mutex>
#include <atomic>
#include <jau/java_uplink.hpp>
#include <jau/octets.hpp>
#include <jau/darray.hpp>
#include <jau/cow_darray.hpp>
#include <jau/uuid.hpp>
#include <jau/dfa_utf8_decode.hpp>
#include "BTTypes0.hpp"
#include "ATTPDUTypes.hpp"
#include "BTTypes1.hpp"
// #include "BTGattDesc.hpp"
#include "BTGattChar.hpp"
// #include "BTGattService.hpp"
// #define JAU_TRACE_DBGATT 1
#ifdef JAU_TRACE_DBGATT
#define JAU_TRACE_DBGATT_PRINT(...) { fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); fflush(stderr); }
#else
#define JAU_TRACE_DBGATT_PRINT(...)
#endif
/**
* - - - - - - - - - - - - - - -
*
* Module DBGattServer covering the GattServer elements:
*
* - BT Core Spec v5.2: Vol 3, Part G Generic Attribute Protocol (GATT)
* - BT Core Spec v5.2: Vol 3, Part G GATT: 2.6 GATT Profile Hierarchy
*/
namespace direct_bt {
/**
* Representing a Gatt Characteristic Descriptor object from the ::GATTRole::Server perspective.
*
* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3 Characteristic Descriptor
*
* @since 2.4.0
*/
class DBGattDesc {
public:
/**
* Characteristic Descriptor Handle
* <p>
* Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
* </p>
*/
uint16_t handle;
/** Type of descriptor */
std::shared_ptr<const jau::uuid_t> type;
/**
* Characteristic Descriptor's Value.
*
* Its capacity defines the maximum writable variable length
* and its size defines the maximum writable fixed length.
*/
jau::POctets value;
/**
* True if value is of variable length, otherwise fixed length.
*/
bool variable_length;
~DBGattDesc() {
JAU_TRACE_DBGATT_PRINT("DBGattDesc dtor0: %p", this);
}
/**
*
* @param type_
* @param value_ the data, which length defines the maximum writable fixed length if variable_length is false.
* If variable length is true, its capacity limits the maximum writable length.
* Forced to false if isExtendedProperties() or isClientCharConfig().
* @param variable_length_ defaults to false.
*/
DBGattDesc(const std::shared_ptr<const jau::uuid_t>& type_,
jau::POctets && value_, bool variable_length_=false) noexcept
: handle(0), type(type_), value( std::move( value_ ) ), variable_length(variable_length_)
{
if( variable_length && ( isExtendedProperties() || isClientCharConfig() ) ) {
variable_length = false;
}
JAU_TRACE_DBGATT_PRINT("DBGattDesc ctor0, value-move: %p", this);
}
/**
* Copy constructor preserving the capacity of the value
* @param o
*/
DBGattDesc(const DBGattDesc &o)
: handle(o.handle), type(o.type),
value(o.value, o.value.capacity()),
variable_length(o.variable_length)
{
JAU_TRACE_DBGATT_PRINT("DBGattDesc ctor-cpy0: %p -> %p", &o, this);
}
/**
* Move constructor
* @param o POctet source to be taken over
*/
DBGattDesc(DBGattDesc &&o) noexcept
: handle(std::move(o.handle)), type(std::move(o.type)),
value(std::move(o.value)),
variable_length(std::move(o.variable_length))
{
JAU_TRACE_DBGATT_PRINT("DBGattDesc ctor-move0: %p -> %p", &o, this);
}
/**
* Return a newly constructed Client Characteristic Configuration
* with a zero uint16_t value of fixed length.
* @see isClientCharConfig()
*/
static DBGattDesc createClientCharConfig() {
jau::POctets p( 2, jau::endian::little);
p.put_uint16_nc(0, 0);
return DBGattDesc( BTGattDesc::TYPE_CCC_DESC, std::move(p), false /* variable_length */ );
}
/** Fill value with zero bytes. */
void bzero() noexcept {
value.bzero();
}
/** Value is uint16_t bitfield */
bool isExtendedProperties() const noexcept { return *BTGattDesc::TYPE_EXT_PROP == *type; }
/* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration (Characteristic Descriptor, optional, single, uint16_t bitfield) */
bool isClientCharConfig() const noexcept{ return *BTGattDesc::TYPE_CCC_DESC == *type; }
/* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.2 Characteristic User Description */
bool isUserDescription() const noexcept{ return *BTGattDesc::TYPE_USER_DESC == *type; }
std::string toString() const noexcept {
const std::string len = variable_length ? "var" : "fixed";
return "Desc[type 0x"+type->toString()+", handle "+jau::to_hexstring(handle)+
", value[len "+len+
", "+value.toString()+
" '" + jau::dfa_utf8_decode( value.get_ptr(), value.size() ) + "'"+
"]]";
}
};
inline bool operator==(const DBGattDesc& lhs, const DBGattDesc& rhs) noexcept
{ return lhs.handle == rhs.handle; /** unique attribute handles */ }
inline bool operator!=(const DBGattDesc& lhs, const DBGattDesc& rhs) noexcept
{ return !(lhs == rhs); }
/**
* Representing a Gatt Characteristic object from the ::GATTRole::Server perspective.
*
* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3 Characteristic Definition
*
* handle -> CDAV value
*
* BT Core Spec v5.2: Vol 3, Part G GATT: 4.6.1 Discover All Characteristics of a Service
*
* The handle represents a service's characteristics-declaration
* and the value the Characteristics Property, Characteristics Value Handle _and_ Characteristics UUID.
*
* @since 2.4.0
*/
class DBGattChar {
private:
bool enabledNotifyState = false;
bool enabledIndicateState = false;
public:
/**
* Characteristic Handle of this instance.
* <p>
* Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
* </p>
*/
uint16_t handle;
/**
* Characteristic end handle, inclusive.
* <p>
* Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
* </p>
*/
uint16_t end_handle;
/**
* Characteristics Value Handle.
* <p>
* Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
* </p>
*/
uint16_t value_handle;
/* Characteristics Value Type UUID */
std::shared_ptr<const jau::uuid_t> value_type;
/* Characteristics Property */
BTGattChar::PropertyBitVal properties;
/** List of Characteristic Descriptions. */
jau::darray<DBGattDesc> descriptors;
/**
* Characteristics's Value.
*
* Its capacity defines the maximum writable variable length
* and its size defines the maximum writable fixed length.
*/
jau::POctets value;
/**
* True if value is of variable length, otherwise fixed length.
*/
bool variable_length;
/* Optional Client Characteristic Configuration index within descriptorList */
int clientCharConfigIndex;
/* Optional Characteristic User Description index within descriptorList */
int userDescriptionIndex;
~DBGattChar() {
JAU_TRACE_DBGATT_PRINT("DBGattChar dtor0: %p", this);
}
/**
*
* @param value_type_
* @param properties_
* @param descriptors_
* @param value_ the data, which length defines the maximum writable fixed length if variable_length is false.
* If variable length is true, its capacity limits the maximum writable length.
* @param variable_length_ defaults to false.
*
*/
DBGattChar(const std::shared_ptr<const jau::uuid_t>& value_type_,
const BTGattChar::PropertyBitVal properties_,
jau::darray<DBGattDesc> && descriptors_,
jau::POctets && value_, bool variable_length_=false) noexcept
: handle(0), end_handle(0), value_handle(0),
value_type(value_type_),
properties(properties_),
descriptors( std::move( descriptors_ ) ),
value( std::move( value_ ) ), variable_length(variable_length_),
clientCharConfigIndex(-1),
userDescriptionIndex(-1)
{
int i=0;
// C++11: Range-based for loop: [begin, end[
for(DBGattDesc& d : descriptors) {
if( 0 > clientCharConfigIndex && d.isClientCharConfig() ) {
clientCharConfigIndex=i;
} else if( 0 > userDescriptionIndex && d.isUserDescription() ) {
userDescriptionIndex=i;
}
++i;
}
JAU_TRACE_DBGATT_PRINT("DBGattChar: ctor0, descr-move: %p", this);
}
/**
* Copy constructor preserving the capacity of the value
* @param o
*/
DBGattChar(const DBGattChar &o)
: handle(o.handle), end_handle(o.end_handle),
value_handle(o.value_handle), value_type(o.value_type),
properties(o.properties),
descriptors(o.descriptors),
value(o.value, o.value.capacity()),
variable_length(o.variable_length),
clientCharConfigIndex(o.clientCharConfigIndex),
userDescriptionIndex(o.userDescriptionIndex)
{
JAU_TRACE_DBGATT_PRINT("DBGattChar: ctor-copy0: %p -> %p", &o, this);
}
/**
* Move constructor
* @param o POctet source to be taken over
*/
DBGattChar(DBGattChar &&o) noexcept
: handle(std::move(o.handle)), end_handle(std::move(o.end_handle)),
value_handle(std::move(o.value_handle)), value_type(std::move(o.value_type)),
properties(std::move(o.properties)),
descriptors(std::move(o.descriptors)),
value(std::move(o.value)),
variable_length(std::move(o.variable_length)),
clientCharConfigIndex(std::move(o.clientCharConfigIndex)),
userDescriptionIndex(std::move(o.userDescriptionIndex))
{
JAU_TRACE_DBGATT_PRINT("DBGattChar: ctor-move0: %p -> %p", &o, this);
}
bool hasProperties(const BTGattChar::PropertyBitVal v) const noexcept { return v == ( properties & v ); }
/** Fill value with zero bytes. */
void bzero() noexcept {
value.bzero();
}
const DBGattDesc* getClientCharConfig() const noexcept {
if( 0 > clientCharConfigIndex ) {
return nullptr;
}
return &descriptors.at(static_cast<size_t>(clientCharConfigIndex)); // abort if out of bounds
}
const DBGattDesc* getUserDescription() const noexcept {
if( 0 > userDescriptionIndex ) {
return nullptr;
}
return &descriptors.at(static_cast<size_t>(userDescriptionIndex)); // abort if out of bounds
}
DBGattDesc* getClientCharConfig() noexcept {
if( 0 > clientCharConfigIndex ) {
return nullptr;
}
return &descriptors.at(static_cast<size_t>(clientCharConfigIndex)); // abort if out of bounds
}
DBGattDesc* getUserDescription() noexcept {
if( 0 > userDescriptionIndex ) {
return nullptr;
}
return &descriptors.at(static_cast<size_t>(userDescriptionIndex)); // abort if out of bounds
}
std::string toString() const noexcept {
std::string char_name, notify_str;
{
const DBGattDesc* ud = getUserDescription();
if( nullptr != ud ) {
char_name = ", '" + jau::dfa_utf8_decode( ud->value.get_ptr(), ud->value.size() ) + "'";
}
}
if( hasProperties(BTGattChar::PropertyBitVal::Notify) || hasProperties(BTGattChar::PropertyBitVal::Indicate) ) {
notify_str = ", enabled[notify "+std::to_string(enabledNotifyState)+", indicate "+std::to_string(enabledIndicateState)+"]";
}
const std::string len = variable_length ? "var" : "fixed";
return "Char[handle ["+jau::to_hexstring(handle)+".."+jau::to_hexstring(end_handle)+
"], props "+jau::to_hexstring(properties)+" "+to_string(properties)+
char_name+", value[type 0x"+value_type->toString()+", handle "+jau::to_hexstring(value_handle)+", len "+len+
", "+value.toString()+
" '" + jau::dfa_utf8_decode( value.get_ptr(), value.size() ) + "'"+
"], ccd-idx "+std::to_string(clientCharConfigIndex)+notify_str+"]";
}
};
inline bool operator==(const DBGattChar& lhs, const DBGattChar& rhs) noexcept
{ return lhs.handle == rhs.handle; /** unique attribute handles */ }
inline bool operator!=(const DBGattChar& lhs, const DBGattChar& rhs) noexcept
{ return !(lhs == rhs); }
/**
* Representing a Gatt Service object from the ::GATTRole::Server perspective.
*
* BT Core Spec v5.2: Vol 3, Part G GATT: 3.1 Service Definition
*
* Includes a complete [Primary] Service Declaration
* including its list of Characteristic Declarations,
* which also may include its client config if available.
*
* @since 2.4.0
*/
class DBGattService {
public:
/**
* Indicate whether this service is a primary service.
*/
bool primary;
/**
* Service start handle
* <p>
* Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
* </p>
*/
uint16_t handle;
/**
* Service end handle, inclusive.
* <p>
* Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
* </p>
*/
uint16_t end_handle;
/** Service type UUID */
std::shared_ptr<const jau::uuid_t> type;
/** List of Characteristic Declarations. */
jau::darray<DBGattChar> characteristics;
~DBGattService() {
JAU_TRACE_DBGATT_PRINT("DBGattService dtor0: %p", this);
}
DBGattService(const bool primary_,
const std::shared_ptr<const jau::uuid_t>& type_,
jau::darray<DBGattChar> && characteristics_)
: primary(primary_), handle(0), end_handle(0),
type(type_),
characteristics( std::move( characteristics_ ) )
{ }
DBGattService(const DBGattService &o) = default;
DBGattService(DBGattService &&o) noexcept = default;
DBGattChar* findGattChar(const jau::uuid_t& char_uuid) noexcept {
for(DBGattChar& c : characteristics) {
if( char_uuid.equivalent( *c.value_type ) ) {
return &c;
}
}
return nullptr;
}
DBGattChar* findGattCharByValueHandle(const uint16_t char_value_handle) noexcept {
for(DBGattChar& c : characteristics) {
if( char_value_handle == c.value_handle ) {
return &c;
}
}
return nullptr;
}
/**
* Sets all handles of this service instance and all its owned childs,
* i.e. DBGattChars elements and its DBGattDesc elements.
*
* @param start_handle a valid and unique start handle number > 0
* @return number of set handles, i.e. `( end_handle - handle ) + 1`
*/
int setHandles(const uint16_t start_handle) {
if( 0 == start_handle ) {
handle = 0;
end_handle = 0;
return 0;
}
uint16_t h = start_handle;
handle = h++;
for(DBGattChar& c : characteristics) {
c.handle = h++;
c.value_handle = h++;
for(DBGattDesc& d : c.descriptors) {
d.handle = h++;
}
c.end_handle = h-1;
}
end_handle = h-1;
return ( end_handle - handle ) + 1;
}
std::string toString() const noexcept {
return "Srvc[type 0x"+type->toString()+", handle ["+jau::to_hexstring(handle)+".."+jau::to_hexstring(end_handle)+"], "+
std::to_string(characteristics.size())+" chars]";
}
};
inline bool operator==(const DBGattService& lhs, const DBGattService& rhs) noexcept
{ return lhs.handle == rhs.handle && lhs.end_handle == rhs.end_handle; /** unique attribute handles */ }
inline bool operator!=(const DBGattService& lhs, const DBGattService& rhs) noexcept
{ return !(lhs == rhs); }
/**
* Representing a complete list of Gatt Service objects from the ::GATTRole::Server perspective,
* i.e. the Gatt Server database.
*
* One instance shall be attached to BTAdapter and hence BTGattHandler
* when operating in Gatt Server mode, i.e. ::GATTRole::Server.
*
* This class is not thread safe and only intended to be prepared
* by the user at startup and processed by the Gatt Server facility.
*
* @since 2.4.0
*/
class DBGattServer {
public:
/**
* Listener to remote master device's operations on the local GATT-Server.
*
* All methods shall return as soon as possible to not block GATT event processing.
*/
class Listener {
public:
virtual ~Listener() {}
/**
* Notification that device got connected.
*
* Convenient user entry, allowing to setup resources.
*
* @param device the connected device
* @param initialMTU initial used minimum MTU until negotiated.
*/
virtual void connected(std::shared_ptr<BTDevice> device, const uint16_t initialMTU) = 0;
/**
* Notification that device got disconnected.
*
* Convenient user entry, allowing to clean up resources.
*
* @param device the disconnected device.
*/
virtual void disconnected(std::shared_ptr<BTDevice> device) = 0;
/**
* Notification that the MTU has changed.
*
* @param device the device for which the MTU has changed
* @param mtu the new negotiated MTU
*/
virtual void mtuChanged(std::shared_ptr<BTDevice> device, const uint16_t mtu) = 0;
/**
*
* @param device
* @param s
* @param c
* @return true if master read has been accepted by GATT-Server listener, otherwise false. Only if all listener return true, the read action will be allowed.
*/
virtual bool readCharValue(std::shared_ptr<BTDevice> device, DBGattService& s, DBGattChar& c) = 0;
/**
*
* @param device
* @param s
* @param c
* @param d
* @return true if master read has been accepted by GATT-Server listener, otherwise false. Only if all listener return true, the read action will be allowed.
*/
virtual bool readDescValue(std::shared_ptr<BTDevice> device, DBGattService& s, DBGattChar& c, DBGattDesc& d) = 0;
/**
*
* @param device
* @param s
* @param c
* @param value
* @param value_offset
* @return true if master write has been accepted by GATT-Server listener, otherwise false. Only if all listener return true, the write action will be allowed.
*/
virtual bool writeCharValue(std::shared_ptr<BTDevice> device, DBGattService& s, DBGattChar& c,
const jau::TROOctets & value, const uint16_t value_offset) = 0;
/**
*
* @param device
* @param s
* @param c
* @param d
* @param value
* @param value_offset
* @return true if master write has been accepted by GATT-Server listener, otherwise false. Only if all listener return true, the write action will be allowed.
*/
virtual bool writeDescValue(std::shared_ptr<BTDevice> device, DBGattService& s, DBGattChar& c, DBGattDesc& d,
const jau::TROOctets & value, const uint16_t value_offset) = 0;
/**
*
* @param device
* @param s
* @param c
* @param d
* @param notificationEnabled
* @param indicationEnabled
*/
virtual void clientCharConfigChanged(std::shared_ptr<BTDevice> device, DBGattService& s, DBGattChar& c, DBGattDesc& d,
const bool notificationEnabled, const bool indicationEnabled) = 0;
/**
* Default comparison operator, merely testing for same memory reference.
* <p>
* Specializations may override.
* </p>
*/
virtual bool operator==(const Listener& rhs) const
{ return this == &rhs; }
bool operator!=(const Listener& rhs) const
{ return !(*this == rhs); }
};
typedef std::shared_ptr<Listener> ListenerRef;
private:
typedef jau::cow_darray<ListenerRef> ListenerList_t;
ListenerList_t listenerList;
public:
/** Used maximum server Rx ATT_MTU */
uint16_t max_att_mtu = 512 + 1; // BTGattHandler::Defaults::MAX_ATT_MTU;
/** List of Services */
jau::darray<DBGattService> services;
DBGattServer()
: services()
{ }
DBGattServer(jau::darray<DBGattService> && services_)
: services( std::move( services_ ) )
{ }
DBGattService* findGattService(const jau::uuid_t& type) noexcept {
for(DBGattService& s : services) {
if( type.equivalent( *s.type ) ) {
return &s;
}
}
return nullptr;
}
DBGattChar* findGattChar(const jau::uuid_t& service_uuid, const jau::uuid_t& char_uuid) noexcept {
DBGattService* service = findGattService(service_uuid);
if( nullptr == service ) {
return nullptr;
}
return service->findGattChar(char_uuid);
}
DBGattDesc* findGattClientCharConfig(const jau::uuid_t& service_uuid, const jau::uuid_t& char_uuid) noexcept {
DBGattChar * c = findGattChar(service_uuid, char_uuid);
if( nullptr == c ) {
return nullptr;
}
return c->getClientCharConfig();
}
bool resetGattClientCharConfig(const jau::uuid_t& service_uuid, const jau::uuid_t& char_uuid) noexcept {
DBGattDesc* d = findGattClientCharConfig(service_uuid, char_uuid);
if( nullptr == d ) {
return false;
}
d->bzero();
return true;
}
DBGattChar* findGattCharByValueHandle(const uint16_t char_value_handle) noexcept {
for(DBGattService& s : services) {
DBGattChar* r = s.findGattCharByValueHandle(char_value_handle);
if( nullptr != r ) {
return r;
}
}
return nullptr;
}
/**
* Sets all handles of all service instances and all its owned childs,
* i.e. DBGattChars elements and its DBGattDesc elements.
*
* Start handle is `1`.
*
* Method is being called by BTAdapter when advertising is enabled
* via BTAdapter::startAdvertising().
*
* @return number of set handles, i.e. `( end_handle - handle ) + 1`
* @see BTAdapter::startAdvertising()
*/
int setServicesHandles() {
int c = 0;
uint16_t h = 1;
for(DBGattService& s : services) {
int l = s.setHandles(h);
c += l;
h += l; // end + 1 for next service
}
return c;
}
bool addListener(ListenerRef l);
bool removeListener(ListenerRef l);
bool removeListener(const Listener * l);
jau::cow_darray<ListenerRef>& listener() { return listenerList; }
std::string toFullString() {
std::string res = toString()+"\n";
for(DBGattService& s : services) {
res.append(" ").append(s.toString()).append("\n");
for(DBGattChar& c : s.characteristics) {
res.append(" ").append(c.toString()).append("\n");
for(DBGattDesc& d : c.descriptors) {
res.append(" ").append(d.toString()).append("\n");
}
}
}
return res;
}
std::string toString() const noexcept {
return "DBSrv[max mtu "+std::to_string(max_att_mtu)+", "+std::to_string(services.size())+" services]";
}
};
typedef std::shared_ptr<DBGattServer> DBGattServerRef;
} // namespace direct_bt
#endif /* DB_GATT_SERVER_HPP_ */
|