Skip to content

Commit e1371f8

Browse files
committed
BLE - Devirtualize pal::GattClient
The event handler has been taken out of GattClient declaration and an instantiation requires the actual implementation and the type that handle events.
1 parent 0749100 commit e1371f8

File tree

1 file changed

+138
-51
lines changed

1 file changed

+138
-51
lines changed

features/FEATURE_BLE/ble/pal/PalGattClient.h

Lines changed: 138 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef BLE_PAL_GATT_CLIENT_H_
1818
#define BLE_PAL_GATT_CLIENT_H_
1919

20+
#include "ble/common/StaticInterface.h"
2021
#include "ble/UUID.h"
2122
#include "ble/BLETypes.h"
2223
#include "ble/ArrayView.h"
@@ -29,6 +30,31 @@
2930
namespace ble {
3031
namespace pal {
3132

33+
/**
34+
* Definition of the general handler of GattClient related events.
35+
*/
36+
template<class Impl>
37+
struct GattClientEventHandler : StaticInterface<Impl, GattClientEventHandler> {
38+
39+
using StaticInterface<Impl, ble::pal::GattClientEventHandler>::impl;
40+
41+
/**
42+
* Function invoked when the connections changes the ATT_MTU which controls
43+
* the maximum size of an attribute that can be read in a single L2CAP packet
44+
* which might be fragmented across multiple packets.
45+
*
46+
* @param connectionHandle The handle of the connection that changed the size.
47+
* @param attMtuSize
48+
*/
49+
void on_att_mtu_change(
50+
ble::connection_handle_t connection_handle,
51+
uint16_t att_mtu_size
52+
) {
53+
impl()->on_att_mtu_change_(connection_handle, att_mtu_size);
54+
}
55+
};
56+
57+
3258
/**
3359
* Adaptation layer for a GATT client.
3460
*
@@ -54,25 +80,14 @@ namespace pal {
5480
* implementation for GattClient by subclassing the AttClient class and use
5581
* the class AttClientToGattClientAdapter
5682
*/
83+
template<class Impl, class EventHandler>
5784
class GattClient {
85+
86+
Impl* self() {
87+
return static_cast<Impl*>(this);
88+
}
89+
5890
public:
59-
/**
60-
* Definition of the general handler of GattClient related events.
61-
*/
62-
struct EventHandler {
63-
/**
64-
* Function invoked when the connections changes the ATT_MTU which controls
65-
* the maximum size of an attribute that can be read in a single L2CAP packet
66-
* which might be fragmented across multiple packets.
67-
*
68-
* @param connectionHandle The handle of the connection that changed the size.
69-
* @param attMtuSize
70-
*/
71-
virtual void on_att_mtu_change(
72-
ble::connection_handle_t connection_handle,
73-
uint16_t att_mtu_size
74-
) = 0;
75-
};
7691

7792
/**
7893
* Initialisation of the instance. An implementation can use this function
@@ -84,7 +99,9 @@ class GattClient {
8499
* @return BLE_ERROR_NONE if the request has been successfully sent or the
85100
* appropriate error otherwise.
86101
*/
87-
virtual ble_error_t initialize() = 0;
102+
ble_error_t initialize() {
103+
return self()->initialize_();
104+
}
88105

89106
/**
90107
* Termination of the instance. An implementation can use this function
@@ -97,7 +114,9 @@ class GattClient {
97114
* @return BLE_ERROR_NONE if the request has been successfully sent or the
98115
* appropriate error otherwise.
99116
*/
100-
virtual ble_error_t terminate() = 0;
117+
ble_error_t terminate() {
118+
return self()->terminate_();
119+
}
101120

102121
/**
103122
* Negotiate the mtu to use by this connection.
@@ -116,7 +135,9 @@ class GattClient {
116135
* @param connection The handle of the connection to send this request to.
117136
* @return BLE_ERROR_NONE or an appropriate error.
118137
*/
119-
virtual ble_error_t exchange_mtu(connection_handle_t connection) = 0;
138+
ble_error_t exchange_mtu(connection_handle_t connection) {
139+
return self()->exchange_mtu_(connection);
140+
}
120141

121142
/**
122143
* Acquire the size of the mtu for a given connection.
@@ -129,10 +150,12 @@ class GattClient {
129150
* @return BLE_ERROR_NONE if the MTU size has been acquired or the
130151
* appropriate error otherwise.
131152
*/
132-
virtual ble_error_t get_mtu_size(
153+
ble_error_t get_mtu_size(
133154
connection_handle_t connection_handle,
134155
uint16_t& mtu_size
135-
) = 0;
156+
) {
157+
return self()->get_mtu_size_(connection_handle, mtu_size);
158+
}
136159

137160
/**
138161
* Discover primary services in the range [begin - 0xFFFF].
@@ -165,10 +188,15 @@ class GattClient {
165188
*
166189
* @return BLE_ERROR_NONE or an appropriate error.
167190
*/
168-
virtual ble_error_t discover_primary_service(
191+
ble_error_t discover_primary_service(
169192
connection_handle_t connection,
170193
attribute_handle_t discovery_range_begining
171-
) = 0;
194+
) {
195+
return self()->discover_primary_service_(
196+
connection,
197+
discovery_range_begining
198+
);
199+
}
172200

173201
/**
174202
* Discover primary services by UUID in the range [discovery_range_begining - 0xFFFF].
@@ -200,11 +228,17 @@ class GattClient {
200228
*
201229
* @return BLE_ERROR_NONE or an appropriate error.
202230
*/
203-
virtual ble_error_t discover_primary_service_by_service_uuid(
231+
ble_error_t discover_primary_service_by_service_uuid(
204232
connection_handle_t connection_handle,
205233
attribute_handle_t discovery_range_beginning,
206234
const UUID& uuid
207-
) = 0;
235+
) {
236+
return self()->discover_primary_service_by_service_uuid_(
237+
connection_handle,
238+
discovery_range_beginning,
239+
uuid
240+
);
241+
}
208242

209243
/**
210244
* Find included services within a service.
@@ -242,10 +276,15 @@ class GattClient {
242276
*
243277
* @return BLE_ERROR_NONE or an appropriate error.
244278
*/
245-
virtual ble_error_t find_included_service(
279+
ble_error_t find_included_service(
246280
connection_handle_t connection_handle,
247281
attribute_handle_range_t service_range
248-
) = 0;
282+
) {
283+
return self()->find_included_service_(
284+
connection_handle,
285+
service_range
286+
);
287+
}
249288

250289
/**
251290
* Find characteristic declarations within a service definition.
@@ -283,10 +322,15 @@ class GattClient {
283322
*
284323
* @return BLE_ERROR_NONE or an appropriate error.
285324
*/
286-
virtual ble_error_t discover_characteristics_of_a_service(
325+
ble_error_t discover_characteristics_of_a_service(
287326
connection_handle_t connection_handle,
288327
attribute_handle_range_t discovery_range
289-
) = 0;
328+
) {
329+
return self()->discover_characteristics_of_a_service_(
330+
connection_handle,
331+
discovery_range
332+
);
333+
}
290334

291335
/**
292336
* Discover characteristic descriptors of a characteristic.
@@ -317,10 +361,15 @@ class GattClient {
317361
*
318362
* @return BLE_ERROR_NONE or an appropriate error.
319363
*/
320-
virtual ble_error_t discover_characteristics_descriptors(
364+
ble_error_t discover_characteristics_descriptors(
321365
connection_handle_t connection_handle,
322366
attribute_handle_range_t descriptors_discovery_range
323-
) = 0;
367+
) {
368+
return self()->discover_characteristics_descriptors_(
369+
connection_handle,
370+
descriptors_discovery_range
371+
);
372+
}
324373

325374
/**
326375
* Read the value of an attribute.
@@ -343,10 +392,12 @@ class GattClient {
343392
*
344393
* @return BLE_ERROR_NONE or an appropriate error.
345394
*/
346-
virtual ble_error_t read_attribute_value(
395+
ble_error_t read_attribute_value(
347396
connection_handle_t connection_handle,
348397
attribute_handle_t attribute_handle
349-
) = 0;
398+
) {
399+
return self()->read_attribute_value_(connection_handle, attribute_handle);
400+
}
350401

351402
/**
352403
* Read a characteristic value using its UUID (type).
@@ -377,11 +428,17 @@ class GattClient {
377428
*
378429
* @return BLE_ERROR_NONE or an appropriate error.
379430
*/
380-
virtual ble_error_t read_using_characteristic_uuid(
431+
ble_error_t read_using_characteristic_uuid(
381432
connection_handle_t connection_handle,
382433
attribute_handle_range_t read_range,
383434
const UUID& uuid
384-
) = 0;
435+
) {
436+
return self()->read_using_characteristic_uuid_(
437+
connection_handle,
438+
read_range,
439+
uuid
440+
);
441+
}
385442

386443
/**
387444
* Read a partial value of an attribute.
@@ -409,11 +466,13 @@ class GattClient {
409466
*
410467
* @return BLE_ERROR_NONE or an appropriate error.
411468
*/
412-
virtual ble_error_t read_attribute_blob(
469+
ble_error_t read_attribute_blob(
413470
connection_handle_t connection_handle,
414471
attribute_handle_t attribute_handle,
415472
uint16_t offset
416-
) = 0;
473+
) {
474+
return self()->read_attribute_blob_(connection_handle, attribute_handle, offset);
475+
}
417476

418477
/**
419478
* Read atomically multiple characteristics values.
@@ -435,10 +494,15 @@ class GattClient {
435494
*
436495
* @return BLE_ERROR_NONE or an appropriate error.
437496
*/
438-
virtual ble_error_t read_multiple_characteristic_values(
497+
ble_error_t read_multiple_characteristic_values(
439498
connection_handle_t connection_handle,
440499
const ArrayView<const attribute_handle_t>& characteristic_value_handles
441-
) = 0;
500+
) {
501+
return self()->read_multiple_characteristic_values_(
502+
connection_handle,
503+
characteristic_value_handles
504+
);
505+
}
442506

443507
/**
444508
* Send a write command to the server.
@@ -453,11 +517,17 @@ class GattClient {
453517
*
454518
* @return BLE_ERROR_NONE or an appropriate error.
455519
*/
456-
virtual ble_error_t write_without_response(
520+
ble_error_t write_without_response(
457521
connection_handle_t connection_handle,
458522
attribute_handle_t characteristic_value_handle,
459523
const ArrayView<const uint8_t>& value
460-
) = 0;
524+
) {
525+
return self()->write_without_response_(
526+
connection_handle,
527+
characteristic_value_handle,
528+
value
529+
);
530+
}
461531

462532
/**
463533
* Send a Signed Write without Response command to the server.
@@ -475,11 +545,17 @@ class GattClient {
475545
*
476546
* @return BLE_ERROR_NONE or an appropriate error.
477547
*/
478-
virtual ble_error_t signed_write_without_response(
548+
ble_error_t signed_write_without_response(
479549
connection_handle_t connection_handle,
480550
attribute_handle_t characteristic_value_handle,
481551
const ArrayView<const uint8_t>& value
482-
) = 0;
552+
) {
553+
return self()->signed_write_without_response_(
554+
connection_handle,
555+
characteristic_value_handle,
556+
value
557+
);
558+
}
483559

484560
/**
485561
* Send a write request to the server.
@@ -501,11 +577,13 @@ class GattClient {
501577
*
502578
* @return BLE_ERROR_NONE or an appropriate error.
503579
*/
504-
virtual ble_error_t write_attribute(
580+
ble_error_t write_attribute(
505581
connection_handle_t connection_handle,
506582
attribute_handle_t attribute_handle,
507583
const ArrayView<const uint8_t>& value
508-
) = 0;
584+
) {
585+
return self()->write_attribute_(connection_handle, attribute_handle, value);
586+
}
509587

510588
/**
511589
* Send a prepare write request to the server.
@@ -536,12 +614,19 @@ class GattClient {
536614
*
537615
* @return BLE_ERROR_NONE or an appropriate error.
538616
*/
539-
virtual ble_error_t queue_prepare_write(
617+
ble_error_t queue_prepare_write(
540618
connection_handle_t connection_handle,
541619
attribute_handle_t characteristic_value_handle,
542620
const ArrayView<const uint8_t>& value,
543621
uint16_t offset
544-
) = 0;
622+
) {
623+
return self()->queue_prepare_write_(
624+
connection_handle,
625+
characteristic_value_handle,
626+
value,
627+
offset
628+
);
629+
}
545630

546631
/**
547632
* Send a request to the server to execute the queue of prepared write
@@ -563,10 +648,12 @@ class GattClient {
563648
*
564649
* @return BLE_ERROR_NONE or an appropriate error.
565650
*/
566-
virtual ble_error_t execute_write_queue(
651+
ble_error_t execute_write_queue(
567652
connection_handle_t connection_handle,
568653
bool execute
569-
) = 0;
654+
) {
655+
return self()->execute_write_queue_(connection_handle, execute);
656+
}
570657

571658
/**
572659
* Register a callback which will handle messages from the server.
@@ -621,7 +708,7 @@ class GattClient {
621708
protected:
622709
GattClient() : _event_handler(NULL) { }
623710

624-
virtual ~GattClient() { }
711+
~GattClient() { }
625712

626713
/**
627714
* Upon server message reception an implementation shall call this function.

0 commit comments

Comments
 (0)