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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
|
/*
* 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 {
class BTDevice; // forward
typedef std::shared_ptr<BTDevice> BTDeviceRef;
class DBGattService; // fwd
/**
* 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 jau::JavaUplink {
private:
friend DBGattService;
uint16_t handle;
std::shared_ptr<const jau::uuid_t> type;
jau::POctets value;
bool variable_length;
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 getHandle() const noexcept { return handle; }
/** Type of descriptor */
std::shared_ptr<const jau::uuid_t>& getType() noexcept { return type; }
/** Type of descriptor */
std::shared_ptr<const jau::uuid_t> getType() const noexcept { return type; }
/**
* Characteristic Descriptor's Value.
*
* Its capacity defines the maximum writable variable length
* and its size defines the maximum writable fixed length.
*/
jau::POctets& getValue() noexcept { return value; }
/**
* True if value is of variable length, otherwise fixed length.
*/
bool hasVariableLength() const noexcept { return variable_length; }
static std::string java_class() noexcept {
return std::string(JAVA_MAIN_PACKAGE "DBGattDesc");
}
std::string get_java_class() const noexcept override {
return java_class();
}
~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 std::shared_ptr<DBGattDesc> createClientCharConfig() {
jau::POctets p( 2, jau::endian::little);
p.put_uint16_nc(0, 0);
return std::make_shared<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 override {
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() ) + "'"+
"], "+javaObjectToString()+"]";
}
};
inline bool operator==(const DBGattDesc& lhs, const DBGattDesc& rhs) noexcept
{ return lhs.getHandle() == rhs.getHandle(); /** unique attribute handles */ }
inline bool operator!=(const DBGattDesc& lhs, const DBGattDesc& rhs) noexcept
{ return !(lhs == rhs); }
typedef std::shared_ptr<DBGattDesc> DBGattDescRef;
/**
* 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 : public jau::JavaUplink {
private:
friend DBGattService;
bool enabledNotifyState = false;
bool enabledIndicateState = false;
uint16_t handle;
uint16_t end_handle;
uint16_t value_handle;
std::shared_ptr<const jau::uuid_t> value_type;
BTGattChar::PropertyBitVal properties;
jau::darray<DBGattDescRef> descriptors;
jau::POctets value;
bool variable_length;
int clientCharConfigIndex;
int userDescriptionIndex;
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 getHandle() const noexcept { return 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 getEndHandle() const noexcept { return 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 getValueHandle() const noexcept { return value_handle; }
/* Characteristics Value Type UUID */
std::shared_ptr<const jau::uuid_t>& getValueType() noexcept { return value_type; }
/* Characteristics Value Type UUID */
std::shared_ptr<const jau::uuid_t> getValueType() const noexcept { return value_type; }
/* Characteristics Property */
BTGattChar::PropertyBitVal getProperties() const noexcept { return properties; }
/** List of Characteristic Descriptions. */
jau::darray<DBGattDescRef>& getDescriptors() noexcept { return descriptors; }
/**
* Characteristics's Value.
*
* Its capacity defines the maximum writable variable length
* and its size defines the maximum writable fixed length.
*/
jau::POctets& getValue() noexcept { return value; }
/**
* True if value is of variable length, otherwise fixed length.
*/
bool hasVariableLength() const noexcept { return variable_length; }
/* Optional Client Characteristic Configuration index within descriptorList */
int getClientCharConfigIndex() const noexcept { return clientCharConfigIndex; }
/* Optional Characteristic User Description index within descriptorList */
int getUserDescriptionIndex() const noexcept { return userDescriptionIndex; }
static std::string java_class() noexcept {
return std::string(JAVA_MAIN_PACKAGE "DBGattChar");
}
std::string get_java_class() const noexcept override {
return java_class();
}
~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<DBGattDescRef> && 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(const DBGattDescRef& 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 DBGattDescRef getClientCharConfig() const noexcept {
if( 0 > clientCharConfigIndex ) {
return nullptr;
}
return descriptors.at(static_cast<size_t>(clientCharConfigIndex)); // abort if out of bounds
}
const DBGattDescRef getUserDescription() const noexcept {
if( 0 > userDescriptionIndex ) {
return nullptr;
}
return descriptors.at(static_cast<size_t>(userDescriptionIndex)); // abort if out of bounds
}
DBGattDescRef getClientCharConfig() noexcept {
if( 0 > clientCharConfigIndex ) {
return nullptr;
}
return descriptors.at(static_cast<size_t>(clientCharConfigIndex)); // abort if out of bounds
}
DBGattDescRef 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 override {
std::string char_name, notify_str;
{
const DBGattDescRef ud = getUserDescription();
if( nullptr != ud ) {
char_name = ", '" + jau::dfa_utf8_decode( ud->getValue().get_ptr(), ud->getValue().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+
", "+javaObjectToString()+"]";
}
};
inline bool operator==(const DBGattChar& lhs, const DBGattChar& rhs) noexcept
{ return lhs.getHandle() == rhs.getHandle() && lhs.getEndHandle() == rhs.getEndHandle(); /** unique attribute handles */ }
inline bool operator!=(const DBGattChar& lhs, const DBGattChar& rhs) noexcept
{ return !(lhs == rhs); }
typedef std::shared_ptr<DBGattChar> DBGattCharRef;
/**
* 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 jau::JavaUplink {
private:
bool primary;
uint16_t handle;
uint16_t end_handle;
std::shared_ptr<const jau::uuid_t> type;
jau::darray<DBGattCharRef> characteristics;
public:
/**
* Indicate whether this service is a primary service.
*/
bool isPrimary() const noexcept { return 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 getHandle() const noexcept { return 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 getEndHandle() const noexcept { return end_handle; }
/** Service type UUID */
std::shared_ptr<const jau::uuid_t>& getType() noexcept { return type; }
/** Service type UUID */
std::shared_ptr<const jau::uuid_t> getType() const noexcept { return type; }
/** List of Characteristic Declarations. */
jau::darray<DBGattCharRef>& getCharacteristics() noexcept { return characteristics; }
static std::string java_class() noexcept {
return std::string(JAVA_MAIN_PACKAGE "DBGattService");
}
std::string get_java_class() const noexcept override {
return java_class();
}
~DBGattService() {
JAU_TRACE_DBGATT_PRINT("DBGattService dtor0: %p", this);
}
DBGattService(const bool primary_,
const std::shared_ptr<const jau::uuid_t>& type_,
jau::darray<DBGattCharRef> && characteristics_)
: primary(primary_), handle(0), end_handle(0),
type(type_),
characteristics( std::move( characteristics_ ) )
{ }
DBGattService(const DBGattService &o) = default;
DBGattService(DBGattService &&o) noexcept = default;
DBGattCharRef findGattChar(const jau::uuid_t& char_uuid) noexcept {
for(DBGattCharRef& c : characteristics) {
if( char_uuid.equivalent( *c->value_type ) ) {
return c;
}
}
return nullptr;
}
DBGattCharRef findGattCharByValueHandle(const uint16_t char_value_handle) noexcept {
for(DBGattCharRef& 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(DBGattCharRef& c : characteristics) {
c->handle = h++;
c->value_handle = h++;
for(DBGattDescRef& 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 override {
return "Srvc[type 0x"+type->toString()+", handle ["+jau::to_hexstring(handle)+".."+jau::to_hexstring(end_handle)+"], "+
std::to_string(characteristics.size())+" chars, "+javaObjectToString()+"]";
}
};
inline bool operator==(const DBGattService& lhs, const DBGattService& rhs) noexcept
{ return lhs.getHandle() == rhs.getHandle() && lhs.getEndHandle() == rhs.getEndHandle(); /** unique attribute handles */ }
inline bool operator!=(const DBGattService& lhs, const DBGattService& rhs) noexcept
{ return !(lhs == rhs); }
typedef std::shared_ptr<DBGattService> DBGattServiceRef;
/**
* 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 jau::JavaUplink {
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(BTDeviceRef 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(BTDeviceRef 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(BTDeviceRef 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(BTDeviceRef device, DBGattServiceRef s, DBGattCharRef 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(BTDeviceRef device, DBGattServiceRef s, DBGattCharRef c, DBGattDescRef 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(BTDeviceRef device, DBGattServiceRef s, DBGattCharRef 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(BTDeviceRef device, DBGattServiceRef s, DBGattCharRef c, DBGattDescRef 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(BTDeviceRef device, DBGattServiceRef s, DBGattCharRef c, DBGattDescRef 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;
uint16_t max_att_mtu;
jau::darray<DBGattServiceRef> services;
public:
/** Used maximum server Rx ATT_MTU, defaults to 512+1. */
uint16_t getMaxAttMTU() const noexcept { return max_att_mtu; }
/**
* Set maximum server Rx ATT_MTU, defaults to 512+1 limit.
*
* Method can only be issued before passing instance to BTAdapter::startAdvertising()
* @see BTAdapter::startAdvertising()
*/
void setMaxAttMTU(const uint16_t v) noexcept { max_att_mtu = std::min<uint16_t>(512+1, v); }
/** List of Services */
jau::darray<DBGattServiceRef>& getServices() noexcept { return services; }
static std::string java_class() noexcept {
return std::string(JAVA_MAIN_PACKAGE "DBGattServer");
}
std::string get_java_class() const noexcept override {
return java_class();
}
~DBGattServer() {
#ifdef JAU_TRACE_DBGATT
JAU_TRACE_DBGATT_PRINT("DBGattServer dtor0: %p", this);
jau::print_backtrace(true);
#endif
}
DBGattServer()
: max_att_mtu(512+1), services()
{ }
DBGattServer(uint16_t max_att_mtu_, jau::darray<DBGattServiceRef> && services_)
: max_att_mtu(std::min<uint16_t>(512+1, max_att_mtu_)), services( std::move( services_ ) )
{ }
/**
* Ctor using default maximum ATT_MTU of 512+1
* @param services_
*/
DBGattServer(jau::darray<DBGattServiceRef> && services_)
: max_att_mtu(512+1), services( std::move( services_ ) )
{ }
DBGattServiceRef findGattService(const jau::uuid_t& type) noexcept {
for(DBGattServiceRef& s : services) {
if( type.equivalent( *s->getType() ) ) {
return s;
}
}
return nullptr;
}
DBGattCharRef findGattChar(const jau::uuid_t& service_uuid, const jau::uuid_t& char_uuid) noexcept {
DBGattServiceRef service = findGattService(service_uuid);
if( nullptr == service ) {
return nullptr;
}
return service->findGattChar(char_uuid);
}
DBGattDescRef findGattClientCharConfig(const jau::uuid_t& service_uuid, const jau::uuid_t& char_uuid) noexcept {
DBGattCharRef 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 {
DBGattDescRef d = findGattClientCharConfig(service_uuid, char_uuid);
if( nullptr == d ) {
return false;
}
d->bzero();
return true;
}
DBGattCharRef findGattCharByValueHandle(const uint16_t char_value_handle) noexcept {
for(DBGattServiceRef& s : services) {
DBGattCharRef 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(DBGattServiceRef& 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);
jau::cow_darray<ListenerRef>& listener() { return listenerList; }
std::string toFullString() {
std::string res = toString()+"\n";
for(DBGattServiceRef& s : services) {
res.append(" ").append(s->toString()).append("\n");
for(DBGattCharRef& c : s->getCharacteristics()) {
res.append(" ").append(c->toString()).append("\n");
for(DBGattDescRef& d : c->getDescriptors()) {
res.append(" ").append(d->toString()).append("\n");
}
}
}
return res;
}
std::string toString() const noexcept override {
return "DBSrv[max mtu "+std::to_string(max_att_mtu)+", "+std::to_string(services.size())+" services, "+javaObjectToString()+"]";
}
};
typedef std::shared_ptr<DBGattServer> DBGattServerRef;
} // namespace direct_bt
#endif /* DB_GATT_SERVER_HPP_ */
|