22
22
* a platform's Bluetooth Low Energy (BLE) implementation and the CHIP
23
23
* stack.
24
24
*
25
- * The BleLayer obect accepts BLE data and control input from the
25
+ * The BleLayer object accepts BLE data and control input from the
26
26
* application via a functional interface. It performs the fragmentation
27
27
* and reassembly required to transmit CHIP message via a BLE GATT
28
28
* characteristic interface, and drives incoming messages up the CHIP
@@ -485,43 +485,24 @@ CHIP_ERROR BleLayer::HandleBleTransportConnectionInitiated(BLE_CONNECTION_OBJECT
485
485
bool BleLayer::HandleWriteReceived (BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId,
486
486
PacketBufferHandle && pBuf)
487
487
{
488
- if (!UUIDsMatch (&CHIP_BLE_SVC_ID, svcId))
489
- {
490
- ChipLogError (Ble, " ble write rcvd on unknown svc id" );
491
- return true ;
492
- }
488
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_SVC_ID, svcId), false , ChipLogError (Ble, " Write received on unknown svc" ));
489
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_CHAR_1_ID, charId), false , ChipLogError (Ble, " Write received on unknown char" ));
490
+ VerifyOrReturnError (!pBuf.IsNull (), false , ChipLogError (Ble, " Write received null buffer" ));
493
491
494
- if (UUIDsMatch (&CHIP_BLE_CHAR_1_ID, charId))
495
- {
496
- if (pBuf.IsNull ())
497
- {
498
- ChipLogError (Ble, " rcvd null ble write" );
499
- return true ;
500
- }
501
-
502
- // Find matching connection end point.
503
- BLEEndPoint * endPoint = sBLEEndPointPool .Find (connObj);
492
+ // Find matching connection end point.
493
+ BLEEndPoint * endPoint = sBLEEndPointPool .Find (connObj);
504
494
505
- if (endPoint != nullptr )
506
- {
507
- CHIP_ERROR status = endPoint->Receive (std::move (pBuf));
508
- if (status != CHIP_NO_ERROR)
509
- {
510
- ChipLogError (Ble, " BLEEndPoint rcv failed, err = %" CHIP_ERROR_FORMAT, status.Format ());
511
- }
512
- }
513
- else
514
- {
515
- CHIP_ERROR status = HandleBleTransportConnectionInitiated (connObj, std::move (pBuf));
516
- if (status != CHIP_NO_ERROR)
517
- {
518
- ChipLogError (Ble, " failed handle new chip BLE connection, status = %" CHIP_ERROR_FORMAT, status.Format ());
519
- }
520
- }
495
+ if (endPoint != nullptr )
496
+ {
497
+ CHIP_ERROR err = endPoint->Receive (std::move (pBuf));
498
+ VerifyOrReturnError (err == CHIP_NO_ERROR, false ,
499
+ ChipLogError (Ble, " Receive failed, err = %" CHIP_ERROR_FORMAT, err.Format ()));
521
500
}
522
501
else
523
502
{
524
- ChipLogError (Ble, " ble write rcvd on unknown char" );
503
+ CHIP_ERROR err = HandleBleTransportConnectionInitiated (connObj, std::move (pBuf));
504
+ VerifyOrReturnError (err == CHIP_NO_ERROR, false ,
505
+ ChipLogError (Ble, " Handle new BLE connection failed, err = %" CHIP_ERROR_FORMAT, err.Format ()));
525
506
}
526
507
527
508
return true ;
@@ -530,217 +511,120 @@ bool BleLayer::HandleWriteReceived(BLE_CONNECTION_OBJECT connObj, const ChipBleU
530
511
bool BleLayer::HandleIndicationReceived (BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId,
531
512
PacketBufferHandle && pBuf)
532
513
{
533
- if (!UUIDsMatch (&CHIP_BLE_SVC_ID, svcId))
534
- {
535
- return false ;
536
- }
514
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_SVC_ID, svcId), false , ChipLogError (Ble, " Indication received on unknown svc" ));
515
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_CHAR_2_ID, charId), false , ChipLogError (Ble, " Indication received on unknown char" ));
516
+ VerifyOrReturnError (!pBuf.IsNull (), false , ChipLogError (Ble, " Indication received null buffer" ));
537
517
538
- if (UUIDsMatch (&CHIP_BLE_CHAR_2_ID, charId))
539
- {
540
- if (pBuf.IsNull ())
541
- {
542
- ChipLogError (Ble, " rcvd null ble indication" );
543
- return true ;
544
- }
545
-
546
- // find matching connection end point.
547
- BLEEndPoint * endPoint = sBLEEndPointPool .Find (connObj);
518
+ // Find matching connection end point.
519
+ BLEEndPoint * endPoint = sBLEEndPointPool .Find (connObj);
520
+ VerifyOrReturnError (endPoint != nullptr , false , ChipLogDetail (Ble, " No endpoint for received indication" ));
548
521
549
- if (endPoint != nullptr )
550
- {
551
- CHIP_ERROR status = endPoint->Receive (std::move (pBuf));
552
- if (status != CHIP_NO_ERROR)
553
- {
554
- ChipLogError (Ble, " BLEEndPoint rcv failed, err = %" CHIP_ERROR_FORMAT, status.Format ());
555
- }
556
- }
557
- else
558
- {
559
- ChipLogDetail (Ble, " no endpoint for rcvd indication" );
560
- }
561
- }
562
- else
563
- {
564
- ChipLogError (Ble, " ble ind rcvd on unknown char" );
565
- }
522
+ CHIP_ERROR err = endPoint->Receive (std::move (pBuf));
523
+ VerifyOrReturnError (err == CHIP_NO_ERROR, false , ChipLogError (Ble, " Receive failed, err = %" CHIP_ERROR_FORMAT, err.Format ()));
566
524
567
525
return true ;
568
526
}
569
527
570
528
bool BleLayer::HandleWriteConfirmation (BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId)
571
529
{
572
- if (!UUIDsMatch (&CHIP_BLE_SVC_ID, svcId))
573
- {
574
- return false ;
575
- }
576
-
577
- if (UUIDsMatch (&CHIP_BLE_CHAR_1_ID, charId))
578
- {
579
- HandleAckReceived (connObj);
580
- }
581
- else
582
- {
583
- ChipLogError (Ble, " ble write con rcvd on unknown char" );
584
- }
530
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_SVC_ID, svcId), false , ChipLogError (Ble, " Write confirmation on unknown svc" ));
531
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_CHAR_1_ID, charId), false , ChipLogError (Ble, " Write confirmation on unknown char" ));
585
532
533
+ HandleAckReceived (connObj);
586
534
return true ;
587
535
}
588
536
589
537
bool BleLayer::HandleIndicationConfirmation (BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId)
590
538
{
591
- if (!UUIDsMatch (&CHIP_BLE_SVC_ID, svcId))
592
- {
593
- return false ;
594
- }
595
-
596
- if (UUIDsMatch (&CHIP_BLE_CHAR_2_ID, charId))
597
- {
598
- HandleAckReceived (connObj);
599
- }
600
- else
601
- {
602
- ChipLogError (Ble, " ble ind con rcvd on unknown char" );
603
- }
539
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_SVC_ID, svcId), false , ChipLogError (Ble, " Indication confirmation on unknown svc" ));
540
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_CHAR_2_ID, charId), false ,
541
+ ChipLogError (Ble, " Indication confirmation on unknown char" ));
604
542
543
+ HandleAckReceived (connObj);
605
544
return true ;
606
545
}
607
546
608
547
void BleLayer::HandleAckReceived (BLE_CONNECTION_OBJECT connObj)
609
548
{
610
- // find matching connection end point.
549
+ // Find matching connection end point.
611
550
BLEEndPoint * endPoint = sBLEEndPointPool .Find (connObj);
551
+ VerifyOrReturn (endPoint != nullptr , ChipLogDetail (Ble, " No endpoint for received ack" ));
612
552
613
- if (endPoint != nullptr )
614
- {
615
- CHIP_ERROR status = endPoint->HandleGattSendConfirmationReceived ();
616
-
617
- if (status != CHIP_NO_ERROR)
618
- {
619
- ChipLogError (Ble, " endpoint conf recvd failed, err = %" CHIP_ERROR_FORMAT, status.Format ());
620
- }
621
- }
622
- else
623
- {
624
- ChipLogError (Ble, " no endpoint for BLE sent data ack" );
625
- }
553
+ CHIP_ERROR err = endPoint->HandleGattSendConfirmationReceived ();
554
+ VerifyOrReturn (err == CHIP_NO_ERROR,
555
+ ChipLogError (Ble, " Send ack confirmation failed, err = %" CHIP_ERROR_FORMAT, err.Format ()));
626
556
}
627
557
628
558
bool BleLayer::HandleSubscribeReceived (BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId)
629
559
{
630
- if (!UUIDsMatch (&CHIP_BLE_SVC_ID, svcId))
631
- {
632
- return false ;
633
- }
560
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_SVC_ID, svcId), false , ChipLogError (Ble, " Subscribe received on unknown svc" ));
561
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch (&CHIP_BLE_CHAR_3_ID, charId), false ,
562
+ ChipLogError (Ble, " Subscribe received on unknown char" ));
634
563
635
- if (UUIDsMatch (&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch (&CHIP_BLE_CHAR_3_ID, charId))
636
- {
637
- // Find end point already associated with BLE connection, if any.
638
- BLEEndPoint * endPoint = sBLEEndPointPool .Find (connObj);
639
-
640
- if (endPoint != nullptr )
641
- {
642
- endPoint->HandleSubscribeReceived ();
643
- }
644
- else
645
- {
646
- ChipLogError (Ble, " no endpoint for sub recvd" );
647
- }
648
- }
564
+ // Find end point already associated with BLE connection, if any.
565
+ BLEEndPoint * endPoint = sBLEEndPointPool .Find (connObj);
566
+ VerifyOrReturnError (endPoint != nullptr , false , ChipLogDetail (Ble, " No endpoint for received subscribe" ));
649
567
568
+ endPoint->HandleSubscribeReceived ();
650
569
return true ;
651
570
}
652
571
653
572
bool BleLayer::HandleSubscribeComplete (BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId)
654
573
{
655
- if (!UUIDsMatch (&CHIP_BLE_SVC_ID, svcId))
656
- {
657
- return false ;
658
- }
574
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_SVC_ID, svcId), false , ChipLogError (Ble, " Subscribe complete on unknown svc" ));
575
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch (&CHIP_BLE_CHAR_3_ID, charId), false ,
576
+ ChipLogError (Ble, " Subscribe complete on unknown char" ));
659
577
660
- if (UUIDsMatch (&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch (&CHIP_BLE_CHAR_3_ID, charId))
661
- {
662
- BLEEndPoint * endPoint = sBLEEndPointPool .Find (connObj);
663
-
664
- if (endPoint != nullptr )
665
- {
666
- endPoint->HandleSubscribeComplete ();
667
- }
668
- else
669
- {
670
- ChipLogError (Ble, " no endpoint for sub complete" );
671
- }
672
- }
578
+ BLEEndPoint * endPoint = sBLEEndPointPool .Find (connObj);
579
+ VerifyOrReturnError (endPoint != nullptr , false , ChipLogDetail (Ble, " No endpoint for subscribe complete" ));
673
580
581
+ endPoint->HandleSubscribeComplete ();
674
582
return true ;
675
583
}
676
584
677
585
bool BleLayer::HandleUnsubscribeReceived (BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId)
678
586
{
679
- if (!UUIDsMatch (&CHIP_BLE_SVC_ID, svcId))
680
- {
681
- return false ;
682
- }
587
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_SVC_ID, svcId), false , ChipLogError (Ble, " Unsubscribe received on unknown svc" ));
588
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch (&CHIP_BLE_CHAR_3_ID, charId), false ,
589
+ ChipLogError (Ble, " Unsubscribe received on unknown char" ));
683
590
684
- if (UUIDsMatch (&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch (&CHIP_BLE_CHAR_3_ID, charId))
685
- {
686
- // Find end point already associated with BLE connection, if any.
687
- BLEEndPoint * endPoint = sBLEEndPointPool .Find (connObj);
688
-
689
- if (endPoint != nullptr )
690
- {
691
- endPoint->DoClose (kBleCloseFlag_AbortTransmission , BLE_ERROR_CENTRAL_UNSUBSCRIBED);
692
- }
693
- else
694
- {
695
- ChipLogError (Ble, " no endpoint for unsub recvd" );
696
- }
697
- }
591
+ // Find end point already associated with BLE connection, if any.
592
+ BLEEndPoint * endPoint = sBLEEndPointPool .Find (connObj);
593
+ VerifyOrReturnError (endPoint != nullptr , false , ChipLogDetail (Ble, " No endpoint for unsubscribe received" ));
698
594
595
+ endPoint->DoClose (kBleCloseFlag_AbortTransmission , BLE_ERROR_CENTRAL_UNSUBSCRIBED);
699
596
return true ;
700
597
}
701
598
702
599
bool BleLayer::HandleUnsubscribeComplete (BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId)
703
600
{
704
- if (!UUIDsMatch (&CHIP_BLE_SVC_ID, svcId))
705
- {
706
- return false ;
707
- }
601
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_SVC_ID, svcId), false , ChipLogError (Ble, " Unsubscribe complete on unknown svc" ));
602
+ VerifyOrReturnError (UUIDsMatch (&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch (&CHIP_BLE_CHAR_3_ID, charId), false ,
603
+ ChipLogError (Ble, " Unsubscribe complete on unknown char" ));
708
604
709
- if (UUIDsMatch (&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch (&CHIP_BLE_CHAR_3_ID, charId))
710
- {
711
- // Find end point already associated with BLE connection, if any.
712
- BLEEndPoint * endPoint = sBLEEndPointPool .Find (connObj);
713
-
714
- if (endPoint != nullptr )
715
- {
716
- endPoint->HandleUnsubscribeComplete ();
717
- }
718
- else
719
- {
720
- ChipLogError (Ble, " no endpoint for unsub complete" );
721
- }
722
- }
605
+ // Find end point already associated with BLE connection, if any.
606
+ BLEEndPoint * endPoint = sBLEEndPointPool .Find (connObj);
607
+ VerifyOrReturnError (endPoint != nullptr , false , ChipLogDetail (Ble, " No endpoint for unsubscribe complete" ));
723
608
609
+ endPoint->HandleUnsubscribeComplete ();
724
610
return true ;
725
611
}
726
612
727
613
void BleLayer::HandleConnectionError (BLE_CONNECTION_OBJECT connObj, CHIP_ERROR err)
728
614
{
729
615
// BLE connection has failed somehow, we must find and abort matching connection end point.
730
616
BLEEndPoint * endPoint = sBLEEndPointPool .Find (connObj);
617
+ VerifyOrReturn (endPoint != nullptr , ChipLogDetail (Ble, " No endpoint for connection error" ));
731
618
732
- if (endPoint != nullptr )
619
+ if (err == BLE_ERROR_GATT_UNSUBSCRIBE_FAILED && endPoint-> IsUnsubscribePending () )
733
620
{
734
- if (err == BLE_ERROR_GATT_UNSUBSCRIBE_FAILED && endPoint->IsUnsubscribePending ())
735
- {
736
- // If end point was already closed and just waiting for unsubscribe to complete, free it. Call to Free()
737
- // stops unsubscribe timer.
738
- endPoint->Free ();
739
- }
740
- else
741
- {
742
- endPoint->DoClose (kBleCloseFlag_AbortTransmission , err);
743
- }
621
+ // If end point was already closed and just waiting for unsubscribe to complete, free it. Call to Free()
622
+ // stops unsubscribe timer.
623
+ endPoint->Free ();
624
+ }
625
+ else
626
+ {
627
+ endPoint->DoClose (kBleCloseFlag_AbortTransmission , err);
744
628
}
745
629
}
746
630
0 commit comments