Skip to content

Commit

Permalink
[msg] Flatten SuccessOrExit calls in MessageHeader.cpp.
Browse files Browse the repository at this point in the history
  • Loading branch information
turon committed Oct 25, 2021
1 parent 13d34c2 commit cfe2451
Showing 1 changed file with 15 additions and 30 deletions.
45 changes: 15 additions & 30 deletions src/transport/raw/MessageHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,23 @@ CHIP_ERROR PacketHeader::Decode(const uint8_t * const data, uint16_t size, uint1
uint16_t octets_read;

uint8_t msgFlags;
err = reader.Read8(&msgFlags).StatusCode();
SuccessOrExit(err);
SuccessOrExit(err = reader.Read8(&msgFlags).StatusCode());
version = ((msgFlags & kVersionMask) >> kVersionShift);
VerifyOrExit(version == kMsgHeaderVersion, err = CHIP_ERROR_VERSION_MISMATCH);
SetMessageFlags(msgFlags);

uint8_t securityFlags;
err = reader.Read8(&securityFlags).StatusCode();
SuccessOrExit(err);
SuccessOrExit(err = reader.Read8(&securityFlags).StatusCode());
SetSecurityFlags(securityFlags);

err = reader.Read16(&mSessionId).StatusCode();
SuccessOrExit(err);
SuccessOrExit(err = reader.Read16(&mSessionId).StatusCode());

err = reader.Read32(&mMessageCounter).StatusCode();
SuccessOrExit(err);
SuccessOrExit(err = reader.Read32(&mMessageCounter).StatusCode());

if (mMsgFlags.Has(Header::MsgFlagValues::kSourceNodeIdPresent))
{
uint64_t sourceNodeId;
err = reader.Read64(&sourceNodeId).StatusCode();
SuccessOrExit(err);
SuccessOrExit(err = reader.Read64(&sourceNodeId).StatusCode());
mSourceNodeId.SetValue(sourceNodeId);
}
else
Expand All @@ -172,39 +167,33 @@ CHIP_ERROR PacketHeader::Decode(const uint8_t * const data, uint16_t size, uint1
if (!IsSessionTypeValid())
{
// Reserved.
err = CHIP_ERROR_INTERNAL;
SuccessOrExit(err);
SuccessOrExit(err = CHIP_ERROR_INTERNAL);
}

if (mMsgFlags.HasAll(Header::MsgFlagValues::kDestinationNodeIdPresent, Header::MsgFlagValues::kDestinationGroupIdPresent))
{
// Reserved.
err = CHIP_ERROR_INTERNAL;
SuccessOrExit(err);
SuccessOrExit(err = CHIP_ERROR_INTERNAL);
}
else if (mMsgFlags.Has(Header::MsgFlagValues::kDestinationNodeIdPresent))
{
if (mSessionType != Header::SessionType::kUnicastSession)
{
err = CHIP_ERROR_INTERNAL;
SuccessOrExit(err);
SuccessOrExit(err = CHIP_ERROR_INTERNAL);
}
uint64_t destinationNodeId;
err = reader.Read64(&destinationNodeId).StatusCode();
SuccessOrExit(err);
SuccessOrExit(err = reader.Read64(&destinationNodeId).StatusCode());
mDestinationNodeId.SetValue(destinationNodeId);
mDestinationGroupId.ClearValue();
}
else if (mMsgFlags.Has(Header::MsgFlagValues::kDestinationGroupIdPresent))
{
if (mSessionType != Header::SessionType::kGroupSession)
{
err = CHIP_ERROR_INTERNAL;
SuccessOrExit(err);
SuccessOrExit(err = CHIP_ERROR_INTERNAL);
}
uint16_t destinationGroupId;
err = reader.Read16(&destinationGroupId).StatusCode();
SuccessOrExit(err);
SuccessOrExit(err = reader.Read16(&destinationGroupId).StatusCode());
mDestinationGroupId.SetValue(destinationGroupId);
mDestinationNodeId.ClearValue();
}
Expand Down Expand Up @@ -238,17 +227,15 @@ CHIP_ERROR PayloadHeader::Decode(const uint8_t * const data, uint16_t size, uint
uint8_t header;
uint16_t octets_read;

err = reader.Read8(&header).Read8(&mMessageType).Read16(&mExchangeID).StatusCode();
SuccessOrExit(err);
SuccessOrExit(err = reader.Read8(&header).Read8(&mMessageType).Read16(&mExchangeID).StatusCode());

mExchangeFlags.SetRaw(header);

VendorId vendor_id;
if (HaveVendorId())
{
uint16_t vendor_id_raw;
err = reader.Read16(&vendor_id_raw).StatusCode();
SuccessOrExit(err);
SuccessOrExit(err = reader.Read16(&vendor_id_raw).StatusCode());
vendor_id = static_cast<VendorId>(vendor_id_raw);
}
else
Expand All @@ -257,16 +244,14 @@ CHIP_ERROR PayloadHeader::Decode(const uint8_t * const data, uint16_t size, uint
}

uint16_t protocol_id;
err = reader.Read16(&protocol_id).StatusCode();
SuccessOrExit(err);
SuccessOrExit(err = reader.Read16(&protocol_id).StatusCode());

mProtocolID = Protocols::Id(vendor_id, protocol_id);

if (mExchangeFlags.Has(Header::ExFlagValues::kExchangeFlag_AckMsg))
{
uint32_t ack_message_counter;
err = reader.Read32(&ack_message_counter).StatusCode();
SuccessOrExit(err);
SuccessOrExit(err = reader.Read32(&ack_message_counter).StatusCode());
mAckMessageCounter.SetValue(ack_message_counter);
}
else
Expand Down

0 comments on commit cfe2451

Please sign in to comment.