diff --git a/src/ble/BLEEndPoint.cpp b/src/ble/BLEEndPoint.cpp index dad2fcba03305a..4abe18b17129c1 100644 --- a/src/ble/BLEEndPoint.cpp +++ b/src/ble/BLEEndPoint.cpp @@ -530,26 +530,23 @@ void BLEEndPoint::FreeBtpEngine() BLE_ERROR BLEEndPoint::Init(BleLayer * bleLayer, BLE_CONNECTION_OBJECT connObj, BleRole role, bool autoClose) { - BLE_ERROR err = BLE_NO_ERROR; - bool expectInitialAck; - // Fail if already initialized. - VerifyOrExit(mBle == nullptr, err = BLE_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mBle == nullptr, BLE_ERROR_INCORRECT_STATE); // Validate args. - VerifyOrExit(bleLayer != nullptr, err = BLE_ERROR_BAD_ARGS); - VerifyOrExit(connObj != BLE_CONNECTION_UNINITIALIZED, err = BLE_ERROR_BAD_ARGS); - VerifyOrExit((role == kBleRole_Central || role == kBleRole_Peripheral), err = BLE_ERROR_BAD_ARGS); + VerifyOrReturnError(bleLayer != nullptr, BLE_ERROR_BAD_ARGS); + VerifyOrReturnError(connObj != BLE_CONNECTION_UNINITIALIZED, BLE_ERROR_BAD_ARGS); + VerifyOrReturnError((role == kBleRole_Central || role == kBleRole_Peripheral), BLE_ERROR_BAD_ARGS); // If end point plays peripheral role, expect ack for indication sent as last step of BTP handshake. // If central, periperal's handshake indication 'ack's write sent by central to kick off the BTP handshake. - expectInitialAck = (role == kBleRole_Peripheral); + bool expectInitialAck = (role == kBleRole_Peripheral); - err = mBtpEngine.Init(this, expectInitialAck); + BLE_ERROR err = mBtpEngine.Init(this, expectInitialAck); if (err != BLE_NO_ERROR) { ChipLogError(Ble, "BtpEngine init failed"); - ExitNow(); + return err; } #if CHIP_ENABLE_CHIPOBLE_TEST @@ -557,13 +554,13 @@ BLE_ERROR BLEEndPoint::Init(BleLayer * bleLayer, BLE_CONNECTION_OBJECT connObj, if (err != BLE_NO_ERROR) { ChipLogError(Ble, "%s: Mutex init failed", __FUNCTION__); - ExitNow(); + return err; } err = mBtpEngineTest.Init(this); if (err != BLE_NO_ERROR) { ChipLogError(Ble, "BTP test init failed"); - ExitNow(); + return err; } #endif @@ -586,8 +583,7 @@ BLE_ERROR BLEEndPoint::Init(BleLayer * bleLayer, BLE_CONNECTION_OBJECT connObj, // End point is ready to connect or receive a connection. mState = kState_Ready; -exit: - return err; + return BLE_NO_ERROR; } void BLEEndPoint::Release() @@ -725,9 +721,6 @@ bool BLEEndPoint::PrepareNextFragment(PacketBufferHandle && data, bool & sentAck BLE_ERROR BLEEndPoint::SendNextMessage() { - BLE_ERROR err = BLE_NO_ERROR; - bool sentAck; - // Get the first queued packet to send QueueTxLock(); #if CHIP_ENABLE_CHIPOBLE_TEST @@ -736,7 +729,7 @@ BLE_ERROR BLEEndPoint::SendNextMessage() if (mSendQueue.IsNull()) { QueueTxUnlock(); - return err; + return BLE_NO_ERROR; } #endif @@ -751,7 +744,8 @@ BLE_ERROR BLEEndPoint::SendNextMessage() #endif // Hand whole message payload to the fragmenter. - VerifyOrExit(PrepareNextFragment(std::move(data), sentAck), err = BLE_ERROR_CHIPOBLE_PROTOCOL_ABORT); + bool sentAck; + VerifyOrReturnError(PrepareNextFragment(std::move(data), sentAck), BLE_ERROR_CHIPOBLE_PROTOCOL_ABORT); /* // Todo: reenabled it after integrating fault injection @@ -768,8 +762,7 @@ BLE_ERROR BLEEndPoint::SendNextMessage() ExitNow(); }); */ - err = SendCharacteristic(mBtpEngine.BorrowTxPacket()); - SuccessOrExit(err); + ReturnErrorOnFailure(SendCharacteristic(mBtpEngine.BorrowTxPacket())); if (sentAck) { @@ -778,16 +771,11 @@ BLE_ERROR BLEEndPoint::SendNextMessage() } // Start ack received timer, if it's not already running. - err = StartAckReceivedTimer(); - SuccessOrExit(err); - -exit: - return err; + return StartAckReceivedTimer(); } BLE_ERROR BLEEndPoint::ContinueMessageSend() { - BLE_ERROR err; bool sentAck; if (!PrepareNextFragment(nullptr, sentAck)) @@ -796,12 +784,10 @@ BLE_ERROR BLEEndPoint::ContinueMessageSend() ChipLogError(Ble, "btp fragmenter error on send!"); mBtpEngine.LogState(); - err = BLE_ERROR_CHIPOBLE_PROTOCOL_ABORT; - ExitNow(); + return BLE_ERROR_CHIPOBLE_PROTOCOL_ABORT; } - err = SendCharacteristic(mBtpEngine.BorrowTxPacket()); - SuccessOrExit(err); + ReturnErrorOnFailure(SendCharacteristic(mBtpEngine.BorrowTxPacket())); if (sentAck) { @@ -810,11 +796,7 @@ BLE_ERROR BLEEndPoint::ContinueMessageSend() } // Start ack received timer, if it's not already running. - err = StartAckReceivedTimer(); - SuccessOrExit(err); - -exit: - return err; + return StartAckReceivedTimer(); } BLE_ERROR BLEEndPoint::HandleHandshakeConfirmationReceived() @@ -956,8 +938,6 @@ BLE_ERROR BLEEndPoint::HandleGattSendConfirmationReceived() BLE_ERROR BLEEndPoint::DriveStandAloneAck() { - BLE_ERROR err = BLE_NO_ERROR; - // Stop send-ack timer if running. StopSendAckTimer(); @@ -965,15 +945,11 @@ BLE_ERROR BLEEndPoint::DriveStandAloneAck() if (mAckToSend.IsNull()) { mAckToSend = System::PacketBufferHandle::New(kTransferProtocolStandaloneAckHeaderSize); - VerifyOrExit(!mAckToSend.IsNull(), err = BLE_ERROR_NO_MEMORY); + VerifyOrReturnError(!mAckToSend.IsNull(), BLE_ERROR_NO_MEMORY); } // Attempt to send stand-alone ack. - err = DriveSending(); - SuccessOrExit(err); - -exit: - return err; + return DriveSending(); } BLE_ERROR BLEEndPoint::DoSendStandAloneAck() @@ -982,8 +958,7 @@ BLE_ERROR BLEEndPoint::DoSendStandAloneAck() // Encode and transmit stand-alone ack. mBtpEngine.EncodeStandAloneAck(mAckToSend); - BLE_ERROR err = SendCharacteristic(mAckToSend.Retain()); - SuccessOrExit(err); + ReturnErrorOnFailure(SendCharacteristic(mAckToSend.Retain())); // Reset local receive window counter. mLocalReceiveWindowSize = mReceiveWindowMaxSize; @@ -992,17 +967,11 @@ BLE_ERROR BLEEndPoint::DoSendStandAloneAck() mConnStateFlags.Set(ConnectionStateFlag::kStandAloneAckInFlight); // Start ack received timer, if it's not already running. - err = StartAckReceivedTimer(); - SuccessOrExit(err); - -exit: - return err; + return StartAckReceivedTimer(); } BLE_ERROR BLEEndPoint::DriveSending() { - BLE_ERROR err = BLE_NO_ERROR; - ChipLogDebugBleEndPoint(Ble, "entered DriveSending"); // If receiver's window is almost closed and we don't have an ack to send, OR we do have an ack to send but @@ -1030,15 +999,14 @@ BLE_ERROR BLEEndPoint::DriveSending() #endif // Can't send anything. - ExitNow(); + return BLE_NO_ERROR; } // Otherwise, let's see what we can send. if (!mAckToSend.IsNull()) // If immediate, stand-alone ack is pending, send it. { - err = DoSendStandAloneAck(); - SuccessOrExit(err); + ReturnErrorOnFailure(DoSendStandAloneAck()); } else if (mBtpEngine.TxState() == BtpEngine::kState_Idle) // Else send next message fragment, if any. { @@ -1046,8 +1014,7 @@ BLE_ERROR BLEEndPoint::DriveSending() if (!mSendQueue.IsNull()) { // Transmit first fragment of next whole message in send queue. - err = SendNextMessage(); - SuccessOrExit(err); + ReturnErrorOnFailure(SendNextMessage()); } else { @@ -1057,8 +1024,7 @@ BLE_ERROR BLEEndPoint::DriveSending() else if (mBtpEngine.TxState() == BtpEngine::kState_InProgress) { // Send next fragment of message currently held by fragmenter. - err = ContinueMessageSend(); - SuccessOrExit(err); + ReturnErrorOnFailure(ContinueMessageSend()); } else if (mBtpEngine.TxState() == BtpEngine::kState_Complete) { @@ -1072,8 +1038,7 @@ BLE_ERROR BLEEndPoint::DriveSending() if (!mSendQueue.IsNull()) { // Transmit first fragment of next whole message in send queue. - err = SendNextMessage(); - SuccessOrExit(err); + ReturnErrorOnFailure(SendNextMessage()); } else if (mState == kState_Closing && !mBtpEngine.ExpectingAck()) // and mSendQueue is NULL, per above... { @@ -1086,28 +1051,24 @@ BLE_ERROR BLEEndPoint::DriveSending() } } -exit: - return err; + return BLE_NO_ERROR; } BLE_ERROR BLEEndPoint::HandleCapabilitiesRequestReceived(PacketBufferHandle && data) { - BLE_ERROR err = BLE_NO_ERROR; BleTransportCapabilitiesRequestMessage req; BleTransportCapabilitiesResponseMessage resp; - PacketBufferHandle responseBuf; uint16_t mtu; - VerifyOrExit(!data.IsNull(), err = BLE_ERROR_BAD_ARGS); + VerifyOrReturnError(!data.IsNull(), BLE_ERROR_BAD_ARGS); mState = kState_Connecting; // Decode BTP capabilities request. - err = BleTransportCapabilitiesRequestMessage::Decode(data, req); - SuccessOrExit(err); + ReturnErrorOnFailure(BleTransportCapabilitiesRequestMessage::Decode(data, req)); - responseBuf = System::PacketBufferHandle::New(kCapabilitiesResponseLength); - VerifyOrExit(!responseBuf.IsNull(), err = BLE_ERROR_NO_MEMORY); + PacketBufferHandle responseBuf = System::PacketBufferHandle::New(kCapabilitiesResponseLength); + VerifyOrReturnError(!responseBuf.IsNull(), BLE_ERROR_NO_MEMORY); // Determine BLE connection's negotiated ATT MTU, if possible. if (req.mMtu > 0) // If MTU was observed and provided by central... @@ -1165,32 +1126,25 @@ BLE_ERROR BLEEndPoint::HandleCapabilitiesRequestReceived(PacketBufferHandle && d } ChipLogProgress(Ble, "using BTP fragment sizes rx %d / tx %d.", mBtpEngine.GetRxFragmentSize(), mBtpEngine.GetTxFragmentSize()); - err = resp.Encode(responseBuf); - SuccessOrExit(err); + ReturnErrorOnFailure(resp.Encode(responseBuf)); // Stash capabilities response payload and wait for subscription from central. QueueTx(std::move(responseBuf), kType_Data); // Start receive timer. Canceled when end point freed or connection established. - err = StartReceiveConnectionTimer(); - SuccessOrExit(err); - -exit: - return err; + return StartReceiveConnectionTimer(); } BLE_ERROR BLEEndPoint::HandleCapabilitiesResponseReceived(PacketBufferHandle && data) { - BLE_ERROR err = BLE_NO_ERROR; BleTransportCapabilitiesResponseMessage resp; - VerifyOrExit(!data.IsNull(), err = BLE_ERROR_BAD_ARGS); + VerifyOrReturnError(!data.IsNull(), BLE_ERROR_BAD_ARGS); // Decode BTP capabilities response. - err = BleTransportCapabilitiesResponseMessage::Decode(data, resp); - SuccessOrExit(err); + ReturnErrorOnFailure(BleTransportCapabilitiesResponseMessage::Decode(data, resp)); - VerifyOrExit(resp.mFragmentSize > 0, err = BLE_ERROR_INVALID_FRAGMENT_SIZE); + VerifyOrReturnError(resp.mFragmentSize > 0, BLE_ERROR_INVALID_FRAGMENT_SIZE); ChipLogProgress(Ble, "peripheral chose BTP version %d; central expected between %d and %d", resp.mSelectedProtocolVersion, CHIP_BLE_TRANSPORT_PROTOCOL_MIN_SUPPORTED_VERSION, CHIP_BLE_TRANSPORT_PROTOCOL_MAX_SUPPORTED_VERSION); @@ -1198,8 +1152,7 @@ BLE_ERROR BLEEndPoint::HandleCapabilitiesResponseReceived(PacketBufferHandle && if ((resp.mSelectedProtocolVersion < CHIP_BLE_TRANSPORT_PROTOCOL_MIN_SUPPORTED_VERSION) || (resp.mSelectedProtocolVersion > CHIP_BLE_TRANSPORT_PROTOCOL_MAX_SUPPORTED_VERSION)) { - err = BLE_ERROR_INCOMPATIBLE_PROTOCOL_VERSIONS; - ExitNow(); + return BLE_ERROR_INCOMPATIBLE_PROTOCOL_VERSIONS; } // Set fragment size as minimum of (reported ATT MTU, BTP characteristic size) @@ -1231,16 +1184,11 @@ BLE_ERROR BLEEndPoint::HandleCapabilitiesResponseReceived(PacketBufferHandle && // Send ack for connection handshake indication when timer expires. Sequence numbers always start at 0, // and the reassembler's "last received seq num" is initialized to 0 and updated when new fragments are // received from the peripheral, so we don't need to explicitly mark the ack num to send here. - err = StartSendAckTimer(); - SuccessOrExit(err); + ReturnErrorOnFailure(StartSendAckTimer()); // We've sent a capabilities request write and received a compatible response, so the connect // operation has completed successfully. - err = HandleConnectComplete(); - SuccessOrExit(err); - -exit: - return err; + return HandleConnectComplete(); } // Returns number of open slots in remote receive window given the input values. @@ -1479,93 +1427,69 @@ bool BLEEndPoint::SendIndication(PacketBufferHandle && buf) BLE_ERROR BLEEndPoint::StartConnectTimer() { - BLE_ERROR err = BLE_NO_ERROR; - chip::System::Error timerErr; - - timerErr = mBle->mSystemLayer->StartTimer(BLE_CONNECT_TIMEOUT_MS, HandleConnectTimeout, this); - VerifyOrExit(timerErr == CHIP_SYSTEM_NO_ERROR, err = BLE_ERROR_START_TIMER_FAILED); + const chip::System::Error timerErr = mBle->mSystemLayer->StartTimer(BLE_CONNECT_TIMEOUT_MS, HandleConnectTimeout, this); + VerifyOrReturnError(timerErr == CHIP_SYSTEM_NO_ERROR, BLE_ERROR_START_TIMER_FAILED); mTimerStateFlags.Set(TimerStateFlag::kConnectTimerRunning); -exit: - return err; + return BLE_NO_ERROR; } BLE_ERROR BLEEndPoint::StartReceiveConnectionTimer() { - BLE_ERROR err = BLE_NO_ERROR; - chip::System::Error timerErr; - - timerErr = mBle->mSystemLayer->StartTimer(BLE_CONNECT_TIMEOUT_MS, HandleReceiveConnectionTimeout, this); - VerifyOrExit(timerErr == CHIP_SYSTEM_NO_ERROR, err = BLE_ERROR_START_TIMER_FAILED); + const chip::System::Error timerErr = + mBle->mSystemLayer->StartTimer(BLE_CONNECT_TIMEOUT_MS, HandleReceiveConnectionTimeout, this); + VerifyOrReturnError(timerErr == CHIP_SYSTEM_NO_ERROR, BLE_ERROR_START_TIMER_FAILED); mTimerStateFlags.Set(TimerStateFlag::kReceiveConnectionTimerRunning); -exit: - return err; + return BLE_NO_ERROR; } BLE_ERROR BLEEndPoint::StartAckReceivedTimer() { - BLE_ERROR err = BLE_NO_ERROR; - chip::System::Error timerErr; - if (!mTimerStateFlags.Has(TimerStateFlag::kAckReceivedTimerRunning)) { - timerErr = mBle->mSystemLayer->StartTimer(BTP_ACK_RECEIVED_TIMEOUT_MS, HandleAckReceivedTimeout, this); - VerifyOrExit(timerErr == CHIP_SYSTEM_NO_ERROR, err = BLE_ERROR_START_TIMER_FAILED); + const chip::System::Error timerErr = + mBle->mSystemLayer->StartTimer(BTP_ACK_RECEIVED_TIMEOUT_MS, HandleAckReceivedTimeout, this); + VerifyOrReturnError(timerErr == CHIP_SYSTEM_NO_ERROR, BLE_ERROR_START_TIMER_FAILED); mTimerStateFlags.Set(TimerStateFlag::kAckReceivedTimerRunning); } -exit: - return err; + return BLE_NO_ERROR; } BLE_ERROR BLEEndPoint::RestartAckReceivedTimer() { - BLE_ERROR err = BLE_NO_ERROR; - - VerifyOrExit(mTimerStateFlags.Has(TimerStateFlag::kAckReceivedTimerRunning), err = BLE_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mTimerStateFlags.Has(TimerStateFlag::kAckReceivedTimerRunning), BLE_ERROR_INCORRECT_STATE); StopAckReceivedTimer(); - err = StartAckReceivedTimer(); - SuccessOrExit(err); - -exit: - return err; + return StartAckReceivedTimer(); } BLE_ERROR BLEEndPoint::StartSendAckTimer() { - BLE_ERROR err = BLE_NO_ERROR; - chip::System::Error timerErr; - ChipLogDebugBleEndPoint(Ble, "entered StartSendAckTimer"); if (!mTimerStateFlags.Has(TimerStateFlag::kSendAckTimerRunning)) { ChipLogDebugBleEndPoint(Ble, "starting new SendAckTimer"); - timerErr = mBle->mSystemLayer->StartTimer(BTP_ACK_SEND_TIMEOUT_MS, HandleSendAckTimeout, this); - VerifyOrExit(timerErr == CHIP_SYSTEM_NO_ERROR, err = BLE_ERROR_START_TIMER_FAILED); + const chip::System::Error timerErr = mBle->mSystemLayer->StartTimer(BTP_ACK_SEND_TIMEOUT_MS, HandleSendAckTimeout, this); + VerifyOrReturnError(timerErr == CHIP_SYSTEM_NO_ERROR, BLE_ERROR_START_TIMER_FAILED); mTimerStateFlags.Set(TimerStateFlag::kSendAckTimerRunning); } -exit: - return err; + return BLE_NO_ERROR; } BLE_ERROR BLEEndPoint::StartUnsubscribeTimer() { - BLE_ERROR err = BLE_NO_ERROR; - chip::System::Error timerErr; - - timerErr = mBle->mSystemLayer->StartTimer(BLE_UNSUBSCRIBE_TIMEOUT_MS, HandleUnsubscribeTimeout, this); - VerifyOrExit(timerErr == CHIP_SYSTEM_NO_ERROR, err = BLE_ERROR_START_TIMER_FAILED); + const chip::System::Error timerErr = mBle->mSystemLayer->StartTimer(BLE_UNSUBSCRIBE_TIMEOUT_MS, HandleUnsubscribeTimeout, this); + VerifyOrReturnError(timerErr == CHIP_SYSTEM_NO_ERROR, BLE_ERROR_START_TIMER_FAILED); mTimerStateFlags.Set(TimerStateFlag::kUnsubscribeTimerRunning); -exit: - return err; + return BLE_NO_ERROR; } void BLEEndPoint::StopConnectTimer() diff --git a/src/ble/BleLayer.cpp b/src/ble/BleLayer.cpp index c68c89edc2cb80..851ad3da5e16f5 100644 --- a/src/ble/BleLayer.cpp +++ b/src/ble/BleLayer.cpp @@ -171,11 +171,10 @@ void BleTransportCapabilitiesRequestMessage::SetSupportedProtocolVersion(uint8_t BLE_ERROR BleTransportCapabilitiesRequestMessage::Encode(const PacketBufferHandle & msgBuf) const { - uint8_t * p = msgBuf->Start(); - BLE_ERROR err = BLE_NO_ERROR; + uint8_t * p = msgBuf->Start(); // Verify we can write the fixed-length request without running into the end of the buffer. - VerifyOrExit(msgBuf->MaxDataLength() >= kCapabilitiesRequestLength, err = BLE_ERROR_NO_MEMORY); + VerifyOrReturnError(msgBuf->MaxDataLength() >= kCapabilitiesRequestLength, BLE_ERROR_NO_MEMORY); chip::Encoding::Write8(p, CAPABILITIES_MSG_CHECK_BYTE_1); chip::Encoding::Write8(p, CAPABILITIES_MSG_CHECK_BYTE_2); @@ -190,21 +189,19 @@ BLE_ERROR BleTransportCapabilitiesRequestMessage::Encode(const PacketBufferHandl msgBuf->SetDataLength(kCapabilitiesRequestLength); -exit: - return err; + return BLE_NO_ERROR; } BLE_ERROR BleTransportCapabilitiesRequestMessage::Decode(const PacketBufferHandle & msgBuf, BleTransportCapabilitiesRequestMessage & msg) { const uint8_t * p = msgBuf->Start(); - BLE_ERROR err = BLE_NO_ERROR; // Verify we can read the fixed-length request without running into the end of the buffer. - VerifyOrExit(msgBuf->DataLength() >= kCapabilitiesRequestLength, err = BLE_ERROR_MESSAGE_INCOMPLETE); + VerifyOrReturnError(msgBuf->DataLength() >= kCapabilitiesRequestLength, BLE_ERROR_MESSAGE_INCOMPLETE); - VerifyOrExit(CAPABILITIES_MSG_CHECK_BYTE_1 == chip::Encoding::Read8(p), err = BLE_ERROR_INVALID_MESSAGE); - VerifyOrExit(CAPABILITIES_MSG_CHECK_BYTE_2 == chip::Encoding::Read8(p), err = BLE_ERROR_INVALID_MESSAGE); + VerifyOrReturnError(CAPABILITIES_MSG_CHECK_BYTE_1 == chip::Encoding::Read8(p), BLE_ERROR_INVALID_MESSAGE); + VerifyOrReturnError(CAPABILITIES_MSG_CHECK_BYTE_2 == chip::Encoding::Read8(p), BLE_ERROR_INVALID_MESSAGE); for (size_t i = 0; i < kCapabilitiesRequestSupportedVersionsLength; i++) { @@ -214,19 +211,17 @@ BLE_ERROR BleTransportCapabilitiesRequestMessage::Decode(const PacketBufferHandl msg.mMtu = chip::Encoding::LittleEndian::Read16(p); msg.mWindowSize = chip::Encoding::Read8(p); -exit: - return err; + return BLE_NO_ERROR; } // BleTransportCapabilitiesResponseMessage implementation: BLE_ERROR BleTransportCapabilitiesResponseMessage::Encode(const PacketBufferHandle & msgBuf) const { - uint8_t * p = msgBuf->Start(); - BLE_ERROR err = BLE_NO_ERROR; + uint8_t * p = msgBuf->Start(); // Verify we can write the fixed-length request without running into the end of the buffer. - VerifyOrExit(msgBuf->MaxDataLength() >= kCapabilitiesResponseLength, err = BLE_ERROR_NO_MEMORY); + VerifyOrReturnError(msgBuf->MaxDataLength() >= kCapabilitiesResponseLength, BLE_ERROR_NO_MEMORY); chip::Encoding::Write8(p, CAPABILITIES_MSG_CHECK_BYTE_1); chip::Encoding::Write8(p, CAPABILITIES_MSG_CHECK_BYTE_2); @@ -237,28 +232,25 @@ BLE_ERROR BleTransportCapabilitiesResponseMessage::Encode(const PacketBufferHand msgBuf->SetDataLength(kCapabilitiesResponseLength); -exit: - return err; + return BLE_NO_ERROR; } BLE_ERROR BleTransportCapabilitiesResponseMessage::Decode(const PacketBufferHandle & msgBuf, BleTransportCapabilitiesResponseMessage & msg) { const uint8_t * p = msgBuf->Start(); - BLE_ERROR err = BLE_NO_ERROR; // Verify we can read the fixed-length response without running into the end of the buffer. - VerifyOrExit(msgBuf->DataLength() >= kCapabilitiesResponseLength, err = BLE_ERROR_MESSAGE_INCOMPLETE); + VerifyOrReturnError(msgBuf->DataLength() >= kCapabilitiesResponseLength, BLE_ERROR_MESSAGE_INCOMPLETE); - VerifyOrExit(CAPABILITIES_MSG_CHECK_BYTE_1 == chip::Encoding::Read8(p), err = BLE_ERROR_INVALID_MESSAGE); - VerifyOrExit(CAPABILITIES_MSG_CHECK_BYTE_2 == chip::Encoding::Read8(p), err = BLE_ERROR_INVALID_MESSAGE); + VerifyOrReturnError(CAPABILITIES_MSG_CHECK_BYTE_1 == chip::Encoding::Read8(p), BLE_ERROR_INVALID_MESSAGE); + VerifyOrReturnError(CAPABILITIES_MSG_CHECK_BYTE_2 == chip::Encoding::Read8(p), BLE_ERROR_INVALID_MESSAGE); msg.mSelectedProtocolVersion = chip::Encoding::Read8(p); msg.mFragmentSize = chip::Encoding::LittleEndian::Read16(p); msg.mWindowSize = chip::Encoding::Read8(p); -exit: - return err; + return BLE_NO_ERROR; } // BleLayer implementation: @@ -271,15 +263,13 @@ BleLayer::BleLayer() BLE_ERROR BleLayer::Init(BlePlatformDelegate * platformDelegate, BleConnectionDelegate * connDelegate, BleApplicationDelegate * appDelegate, chip::System::Layer * systemLayer) { - BLE_ERROR err = BLE_NO_ERROR; - Ble::RegisterLayerErrorFormatter(); // It is totally valid to not have a connDelegate. In this case the client application // will take care of the connection steps. - VerifyOrExit(platformDelegate != nullptr, err = BLE_ERROR_BAD_ARGS); - VerifyOrExit(appDelegate != nullptr, err = BLE_ERROR_BAD_ARGS); - VerifyOrExit(systemLayer != nullptr, err = BLE_ERROR_BAD_ARGS); + VerifyOrReturnError(platformDelegate != nullptr, BLE_ERROR_BAD_ARGS); + VerifyOrReturnError(appDelegate != nullptr, BLE_ERROR_BAD_ARGS); + VerifyOrReturnError(systemLayer != nullptr, BLE_ERROR_BAD_ARGS); if (mState != kState_NotInitialized) { @@ -299,8 +289,7 @@ BLE_ERROR BleLayer::Init(BlePlatformDelegate * platformDelegate, BleConnectionDe mTestBleEndPoint = NULL; #endif -exit: - return err; + return BLE_NO_ERROR; } BLE_ERROR BleLayer::Init(BlePlatformDelegate * platformDelegate, BleApplicationDelegate * appDelegate, @@ -373,18 +362,14 @@ BLE_ERROR BleLayer::CloseBleConnection(BLE_CONNECTION_OBJECT connObj) BLE_ERROR BleLayer::CancelBleIncompleteConnection() { - BLE_ERROR err = BLE_NO_ERROR; - - VerifyOrExit(mState == kState_Initialized, err = BLE_ERROR_INCORRECT_STATE); - VerifyOrExit(mConnectionDelegate != nullptr, err = BLE_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mState == kState_Initialized, BLE_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mConnectionDelegate != nullptr, BLE_ERROR_INCORRECT_STATE); - err = mConnectionDelegate->CancelConnection(); + BLE_ERROR err = mConnectionDelegate->CancelConnection(); if (err == BLE_ERROR_NOT_IMPLEMENTED) { ChipLogError(Ble, "BleConnectionDelegate::CancelConnection is not implemented."); } - -exit: return err; } @@ -482,7 +467,7 @@ bool BleLayer::HandleWriteReceived(BLE_CONNECTION_OBJECT connObj, const ChipBleU if (!UUIDsMatch(&CHIP_BLE_SVC_ID, svcId)) { ChipLogError(Ble, "ble write rcvd on unknown svc id"); - ExitNow(); + return true; } if (UUIDsMatch(&CHIP_BLE_CHAR_1_ID, charId)) @@ -490,7 +475,7 @@ bool BleLayer::HandleWriteReceived(BLE_CONNECTION_OBJECT connObj, const ChipBleU if (pBuf.IsNull()) { ChipLogError(Ble, "rcvd null ble write"); - ExitNow(); + return true; } // Find matching connection end point. @@ -518,7 +503,6 @@ bool BleLayer::HandleWriteReceived(BLE_CONNECTION_OBJECT connObj, const ChipBleU ChipLogError(Ble, "ble write rcvd on unknown char"); } -exit: return true; } @@ -535,7 +519,7 @@ bool BleLayer::HandleIndicationReceived(BLE_CONNECTION_OBJECT connObj, const Chi if (pBuf.IsNull()) { ChipLogError(Ble, "rcvd null ble indication"); - ExitNow(); + return true; } // find matching connection end point. @@ -559,7 +543,6 @@ bool BleLayer::HandleIndicationReceived(BLE_CONNECTION_OBJECT connObj, const Chi ChipLogError(Ble, "ble ind rcvd on unknown char"); } -exit: return true; } diff --git a/src/ble/BtpEngine.cpp b/src/ble/BtpEngine.cpp index b59f951756c5a4..58c9efc57aab80 100644 --- a/src/ble/BtpEngine.cpp +++ b/src/ble/BtpEngine.cpp @@ -173,12 +173,10 @@ bool BtpEngine::IsValidAck(SequenceNumber_t ack_num) const BLE_ERROR BtpEngine::HandleAckReceived(SequenceNumber_t ack_num) { - BLE_ERROR err = BLE_NO_ERROR; - ChipLogDebugBtpEngine(Ble, "entered HandleAckReceived, ack_num = %u", ack_num); // Ensure ack_num falls within range of ack values we're expecting. - VerifyOrExit(IsValidAck(ack_num), err = BLE_ERROR_INVALID_ACK); + VerifyOrReturnError(IsValidAck(ack_num), BLE_ERROR_INVALID_ACK); if (mTxNewestUnackedSeqNum == ack_num) // If ack is for newest outstanding unacknowledged fragment... { @@ -194,23 +192,19 @@ BLE_ERROR BtpEngine::HandleAckReceived(SequenceNumber_t ack_num) IncSeqNum(mTxOldestUnackedSeqNum); } -exit: - return err; + return BLE_NO_ERROR; } // Calling convention: // EncodeStandAloneAck may only be called if data arg is commited for immediate, synchronous subsequent transmission. BLE_ERROR BtpEngine::EncodeStandAloneAck(const PacketBufferHandle & data) { - BLE_ERROR err = BLE_NO_ERROR; - uint8_t * characteristic; - // Ensure enough headroom exists for the lower BLE layers. - VerifyOrExit(data->EnsureReservedSize(CHIP_CONFIG_BLE_PKT_RESERVED_SIZE), err = BLE_ERROR_NO_MEMORY); + VerifyOrReturnError(data->EnsureReservedSize(CHIP_CONFIG_BLE_PKT_RESERVED_SIZE), BLE_ERROR_NO_MEMORY); // Ensure enough space for standalone ack payload. - VerifyOrExit(data->MaxDataLength() >= kTransferProtocolStandaloneAckHeaderSize, err = BLE_ERROR_NO_MEMORY); - characteristic = data->Start(); + VerifyOrReturnError(data->MaxDataLength() >= kTransferProtocolStandaloneAckHeaderSize, BLE_ERROR_NO_MEMORY); + uint8_t * characteristic = data->Start(); // Since there's no preexisting message payload, we can write BTP header without adjusting data start pointer. characteristic[0] = static_cast(HeaderFlags::kFragmentAck); @@ -225,8 +219,7 @@ BLE_ERROR BtpEngine::EncodeStandAloneAck(const PacketBufferHandle & data) // Set ack payload data length. data->SetDataLength(kTransferProtocolStandaloneAckHeaderSize); -exit: - return err; + return BLE_NO_ERROR; } // Calling convention: