@@ -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
528535bool 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+ }
0 commit comments