Skip to content

Commit 20a2db5

Browse files
committed
add indicate API, add getCccd to BLECharacteristic
1 parent a70d157 commit 20a2db5

File tree

4 files changed

+137
-7
lines changed

4 files changed

+137
-7
lines changed

libraries/Bluefruit52Lib/src/BLECharacteristic.cpp

Lines changed: 98 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,10 @@ uint32_t BLECharacteristic::read32(void)
496496
return read(&num, sizeof(num)) ? num : 0;
497497
}
498498

499-
/*------------------------------------------------------------------*/
500-
/* NOTIFY
501-
*------------------------------------------------------------------*/
502-
bool BLECharacteristic::notifyEnabled(void)
499+
500+
uint16_t BLECharacteristic::getCccd(void)
503501
{
504-
VERIFY( _properties.notify && Bluefruit.connected() );
502+
VERIFY( Bluefruit.connected() && (_handles.cccd_handle != BLE_GATT_HANDLE_INVALID), 0 );
505503

506504
uint16_t cccd;
507505
ble_gatts_value_t value =
@@ -522,7 +520,16 @@ bool BLECharacteristic::notifyEnabled(void)
522520
VERIFY_STATUS(err);
523521
}
524522

525-
return (cccd & BLE_GATT_HVX_NOTIFICATION);
523+
return cccd;
524+
}
525+
526+
/*------------------------------------------------------------------*/
527+
/* NOTIFY
528+
*------------------------------------------------------------------*/
529+
bool BLECharacteristic::notifyEnabled(void)
530+
{
531+
VERIFY( _properties.notify );
532+
return (getCccd() & BLE_GATT_HVX_NOTIFICATION);
526533
}
527534

528535
bool BLECharacteristic::notify(const void* data, uint16_t len)
@@ -557,7 +564,7 @@ bool BLECharacteristic::notify(const void* data, uint16_t len)
557564
VERIFY_STATUS( sd_ble_gatts_hvx(Bluefruit.connHandle(), &hvx_params), false );
558565

559566
remaining -= packet_len;
560-
u8data += packet_len;
567+
u8data += packet_len;
561568
}
562569
}
563570
else
@@ -593,3 +600,87 @@ bool BLECharacteristic::notify32(int num)
593600
{
594601
return notify32( (uint32_t) num);
595602
}
603+
604+
/*------------------------------------------------------------------*/
605+
/* NOTIFY
606+
*------------------------------------------------------------------*/
607+
bool BLECharacteristic::indicateEnabled(void)
608+
{
609+
VERIFY( _properties.indicate );
610+
return (getCccd() & BLE_GATT_HVX_INDICATION);
611+
}
612+
613+
bool BLECharacteristic::indicate(const void* data, uint16_t len)
614+
{
615+
VERIFY( _properties.indicate );
616+
617+
// could not exceed max len
618+
uint16_t remaining = min16(len, _max_len);
619+
620+
if ( indicateEnabled() )
621+
{
622+
uint16_t const max_payload = Bluefruit.Gap.getMTU( Bluefruit.connHandle() ) - 3;
623+
const uint8_t* u8data = (const uint8_t*) data;
624+
625+
while ( remaining )
626+
{
627+
uint16_t packet_len = min16(max_payload, remaining);
628+
629+
ble_gatts_hvx_params_t hvx_params =
630+
{
631+
.handle = _handles.value_handle,
632+
.type = BLE_GATT_HVX_INDICATION,
633+
.offset = 0,
634+
.p_len = &packet_len,
635+
.p_data = (uint8_t*) u8data,
636+
};
637+
638+
LOG_LV2("CHR", "Indicate %d bytes", packet_len);
639+
err_t err;
640+
641+
// If previous indicate is still in progress, wait for a while and try again
642+
while ( NRF_ERROR_BUSY == (err = sd_ble_gatts_hvx(Bluefruit.connHandle(), &hvx_params)) )
643+
{
644+
// TODO should wait for BLE_GATTS_EVT_HVC event
645+
delay( Bluefruit.connInterval() );
646+
}
647+
648+
VERIFY_STATUS( err, false );
649+
650+
remaining -= packet_len;
651+
u8data += packet_len;
652+
}
653+
}
654+
else
655+
{
656+
write(data, remaining);
657+
return false;
658+
}
659+
660+
return true;
661+
}
662+
663+
bool BLECharacteristic::indicate(const char * str)
664+
{
665+
return indicate( (const uint8_t*) str, strlen(str) );
666+
}
667+
668+
bool BLECharacteristic::indicate8(uint8_t num)
669+
{
670+
return indicate( (uint8_t*) &num, sizeof(num));
671+
}
672+
673+
bool BLECharacteristic::indicate16(uint16_t num)
674+
{
675+
return indicate( (uint8_t*) &num, sizeof(num));
676+
}
677+
678+
bool BLECharacteristic::indicate32(uint32_t num)
679+
{
680+
return indicate( (uint8_t*) &num, sizeof(num));
681+
}
682+
683+
bool BLECharacteristic::indicate32(int num)
684+
{
685+
return indicate32( (uint32_t) num);
686+
}

libraries/Bluefruit52Lib/src/BLECharacteristic.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ class BLECharacteristic
135135
uint32_t read32(void);
136136

137137
/*------------- Notify -------------*/
138+
uint16_t getCccd(void);
139+
138140
bool notifyEnabled(void);
139141

140142
bool notify(const void* data, uint16_t len);
@@ -145,6 +147,17 @@ class BLECharacteristic
145147
bool notify32 (uint32_t num);
146148
bool notify32 (int num);
147149

150+
/*------------- Indicate -------------*/
151+
bool indicateEnabled(void);
152+
153+
bool indicate(const void* data, uint16_t len);
154+
bool indicate(const char* str);
155+
156+
bool indicate8 (uint8_t num);
157+
bool indicate16 (uint16_t num);
158+
bool indicate32 (uint32_t num);
159+
bool indicate32 (int num);
160+
148161
/*------------- Internal Functions -------------*/
149162
virtual void _eventHandler(ble_evt_t* event);
150163

libraries/Bluefruit52Lib/src/BLEGatt.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ uint16_t BLEGatt::readCharByUuid(uint16_t conn_hdl, BLEUuid bleuuid, void* buffe
4949
int32_t count = 0;
5050
ble_gattc_handle_range_t hdl_range = { .start_handle = start_hdl, .end_handle = end_hdl };
5151

52+
while( _adamsg.isWaiting() )
53+
{
54+
// TODO multiple peripherals
55+
delay( Bluefruit.connInterval() );
56+
}
57+
5258
_adamsg.begin(true);
5359
_adamsg.prepare(buffer, bufsize);
5460

@@ -68,6 +74,25 @@ uint16_t BLEGatt::readCharByUuid(uint16_t conn_hdl, BLEUuid bleuuid, void* buffe
6874
return (count < 0) ? 0 : count;
6975
}
7076

77+
//bool BLEGatt::waitForIndicateConfirm(uint16_t conn_hdl)
78+
//{
79+
// while( _adamsg.isWaiting() )
80+
// {
81+
// // TODO multiple peripherals
82+
// delay( Bluefruit.connInterval() );
83+
// }
84+
//
85+
// _adamsg.begin(true);
86+
// _adamsg.prepare(NULL, 0);
87+
//
88+
// // Wait for HVC event
89+
// int32_t result = _adamsg.waitUntilComplete(BLE_GENERIC_TIMEOUT);
90+
//
91+
// _adamsg.stop();
92+
//
93+
// return (result == 0);
94+
//}
95+
7196
void BLEGatt::_eventHandler(ble_evt_t* evt)
7297
{
7398
// conn handle has fixed offset regardless of event type

libraries/Bluefruit52Lib/src/BLEGatt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class BLEGatt
5959
BLEGatt(void);
6060

6161
uint16_t readCharByUuid(uint16_t conn_hdl, BLEUuid bleuuid, void* buffer, uint16_t bufsize, uint16_t start_hdl = 1, uint16_t end_hdl = 0xffff);
62+
// bool waitForIndicateConfirm(uint16_t conn_hdl);
6263

6364
/*------------------------------------------------------------------*/
6465
/* INTERNAL USAGE ONLY

0 commit comments

Comments
 (0)