Skip to content

Commit

Permalink
Merge branch 'master' into human_format
Browse files Browse the repository at this point in the history
  • Loading branch information
andy31415 authored Jul 7, 2023
2 parents df64905 + 2f875c7 commit 9f95ab8
Show file tree
Hide file tree
Showing 16 changed files with 404 additions and 60 deletions.
9 changes: 6 additions & 3 deletions .github/actions/checkout-submodules/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ inputs:
runs:
using: "composite"
steps:
- name: Checkout submodules
shell: bash
run: scripts/checkout_submodules.py --allow-changing-global-git-config --shallow --platform ${{ inputs.platform }} ${{ inputs.extra-parameters }}
- uses: Wandalen/[email protected]
name: Checkout submodules
with:
command: scripts/checkout_submodules.py --allow-changing-global-git-config --shallow --platform ${{ inputs.platform }} ${{ inputs.extra-parameters }}
attempt_limit: 3
attempt_delay: 2000
7 changes: 6 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ jobs:
"
- name: Run Tests using the python parser sending commands to chip-tool
# https://github.com/project-chip/connectedhomeip/issues/27673
if: matrix.build_variant != 'no-ble-tsan-clang'
run: |
./scripts/run_in_build_env.sh \
"./scripts/tests/run_test_suite.py \
Expand Down Expand Up @@ -286,7 +288,8 @@ jobs:

strategy:
matrix:
build_variant: [no-ble-tsan-clang, no-ble-asan-clang]
build_variant: [no-ble-asan-clang]
# Since no-ble-tsan-clang doesn't run any tests, this is just wasted CI time for now
chip_tool: [""]
env:
BUILD_VARIANT: ${{matrix.build_variant}}
Expand Down Expand Up @@ -350,6 +353,8 @@ jobs:
"
- name: Run Tests using the python parser sending commands to chip-tool
# https://github.com/project-chip/connectedhomeip/issues/27673
if: matrix.build_variant != 'no-ble-tsan-clang'
run: |
./scripts/run_in_build_env.sh \
"./scripts/tests/run_test_suite.py \
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/esp32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ example on ESP32 series of SoCs
- [Flash and NVS encryption for securing factory data](flash_nvs_encryption.md)
- [RPC Console and Device Tracing](rpc_console.md)
- [Matter OTA](ota.md)
- [Generating and Using ESP Secure Cert Parttiton](secure_cert_partition.md)
- [Generating and Using ESP Secure Cert Partition](secure_cert_partition.md)
2 changes: 1 addition & 1 deletion src/app/MessageDef/CommandPathIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ CHIP_ERROR CommandPathIB::Parser::PrettyPrint() const
{
chip::CommandId commandId;
ReturnErrorOnFailure(reader.Get(commandId));
PRETTY_PRINT("\tCommandId = 0x%x,", commandId);
PRETTY_PRINT("\tCommandId = 0x%" PRIx32 ",", commandId);
}
#endif // CHIP_DETAIL_LOGGING
break;
Expand Down
2 changes: 1 addition & 1 deletion src/app/MessageDef/EventPathIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ CHIP_ERROR EventPathIB::Parser::PrettyPrint() const
{
EventId event;
reader.Get(event);
PRETTY_PRINT("\tEvent = 0x%x,", event);
PRETTY_PRINT("\tEvent = 0x%" PRIx32 ",", event);
}
#endif // CHIP_DETAIL_LOGGING
break;
Expand Down
7 changes: 7 additions & 0 deletions src/lib/core/CHIPEncoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ namespace LittleEndian {
template <typename T>
inline T HostSwap(T v);

// For completeness of the set, we have identity.
template <>
inline uint8_t HostSwap<uint8_t>(uint8_t v)
{
return v;
}

/**
* This conditionally performs, as necessary for the target system, a
* byte order swap by value of the specified 16-bit value, presumed to
Expand Down
51 changes: 32 additions & 19 deletions src/lib/support/BufferReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,46 @@

#include "BufferReader.h"

#include <lib/core/CHIPEncoding.h>

#include <string.h>
#include <type_traits>

namespace chip {
namespace Encoding {
namespace LittleEndian {

namespace {
// These helper methods return void and put the value being read into an

// This helper methods return void and put the value being read into an
// outparam because that allows us to easily overload on the type of the
// thing being read.
void ReadHelper(const uint8_t *& p, uint8_t * dest)
{
*dest = Read8(p);
}
void ReadHelper(const uint8_t *& p, uint16_t * dest)
{
*dest = Read16(p);
}
void ReadHelper(const uint8_t *& p, uint32_t * dest)
void ReadHelper(const uint8_t * p, bool * dest)
{
*dest = Read32(p);
*dest = (*p != 0);
}
void ReadHelper(const uint8_t *& p, uint64_t * dest)

template <typename T>
void ReadHelper(const uint8_t * p, T * dest)
{
*dest = Read64(p);
std::make_unsigned_t<T> result;
memcpy(&result, p, sizeof(result));
result = chip::Encoding::LittleEndian::HostSwap(result);

*dest = static_cast<T>(result);
}

} // anonymous namespace

template <typename T>
void Reader::RawRead(T * retval)
void Reader::RawReadLowLevelBeCareful(T * retval)
{
static_assert(CHAR_BIT == 8, "Our various sizeof checks rely on bytes and octets being the same thing");
static_assert((-1 & 3) == 3, "LittleEndian::BufferReader only works with 2's complement architectures.");

VerifyOrReturn(IsSuccess());

static constexpr size_t data_size = sizeof(T);
constexpr size_t data_size = sizeof(T);

if (mAvailable < data_size)
{
Expand All @@ -61,6 +67,8 @@ void Reader::RawRead(T * retval)
}

ReadHelper(mReadPtr, retval);
mReadPtr += data_size;

mAvailable = static_cast<uint16_t>(mAvailable - data_size);
}

Expand All @@ -84,10 +92,15 @@ Reader & Reader::ReadBytes(uint8_t * dest, size_t size)
}

// Explicit Read instantiations for the data types we want to support.
template void Reader::RawRead(uint8_t *);
template void Reader::RawRead(uint16_t *);
template void Reader::RawRead(uint32_t *);
template void Reader::RawRead(uint64_t *);
template void Reader::RawReadLowLevelBeCareful(bool *);
template void Reader::RawReadLowLevelBeCareful(int8_t *);
template void Reader::RawReadLowLevelBeCareful(int16_t *);
template void Reader::RawReadLowLevelBeCareful(int32_t *);
template void Reader::RawReadLowLevelBeCareful(int64_t *);
template void Reader::RawReadLowLevelBeCareful(uint8_t *);
template void Reader::RawReadLowLevelBeCareful(uint16_t *);
template void Reader::RawReadLowLevelBeCareful(uint32_t *);
template void Reader::RawReadLowLevelBeCareful(uint64_t *);

} // namespace LittleEndian
} // namespace Encoding
Expand Down
100 changes: 95 additions & 5 deletions src/lib/support/BufferReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ class Reader
CHECK_RETURN_VALUE
CHIP_ERROR StatusCode() const { return mStatus; }

/**
* @return false if the reader is in error, true if the reader is OK.
*/
bool IsSuccess() const { return StatusCode() == CHIP_NO_ERROR; }

/**
* Read a bool, assuming single byte storage.
*
* @param [out] dest Where the 8-bit integer goes.
*
* @note The read can put the reader in a failed-status state if there are
* not enough octets available. Callers must either continue to do
* more reads on the return value or check its status to see whether
* the sequence of reads that has been performed succeeded.
*/
CHECK_RETURN_VALUE
Reader & ReadBool(bool * dest)
{
RawReadLowLevelBeCareful(dest);
return *this;
}

/**
* Read a single 8-bit unsigned integer.
*
Expand All @@ -103,7 +125,7 @@ class Reader
CHECK_RETURN_VALUE
Reader & Read8(uint8_t * dest)
{
RawRead(dest);
RawReadLowLevelBeCareful(dest);
return *this;
}

Expand All @@ -120,7 +142,7 @@ class Reader
CHECK_RETURN_VALUE
Reader & Read16(uint16_t * dest)
{
RawRead(dest);
RawReadLowLevelBeCareful(dest);
return *this;
}

Expand All @@ -137,7 +159,7 @@ class Reader
CHECK_RETURN_VALUE
Reader & Read32(uint32_t * dest)
{
RawRead(dest);
RawReadLowLevelBeCareful(dest);
return *this;
}

Expand All @@ -154,7 +176,75 @@ class Reader
CHECK_RETURN_VALUE
Reader & Read64(uint64_t * dest)
{
RawRead(dest);
RawReadLowLevelBeCareful(dest);
return *this;
}

/**
* Read a single 8-bit signed integer.
*
* @param [out] dest Where the 8-bit integer goes.
*
* @note The read can put the reader in a failed-status state if there are
* not enough octets available. Callers must either continue to do
* more reads on the return value or check its status to see whether
* the sequence of reads that has been performed succeeded.
*/
CHECK_RETURN_VALUE
Reader & ReadSigned8(int8_t * dest)
{
RawReadLowLevelBeCareful(dest);
return *this;
}

/**
* Read a single 16-bit signed integer.
*
* @param [out] dest Where the 16-bit integer goes.
*
* @note The read can put the reader in a failed-status state if there are
* not enough octets available. Callers must either continue to do
* more reads on the return value or check its status to see whether
* the sequence of reads that has been performed succeeded.
*/
CHECK_RETURN_VALUE
Reader & ReadSigned16(int16_t * dest)
{
RawReadLowLevelBeCareful(dest);
return *this;
}

/**
* Read a single 32-bit signed integer.
*
* @param [out] dest Where the 32-bit integer goes.
*
* @note The read can put the reader in a failed-status state if there are
* not enough octets available. Callers must either continue to do
* more reads on the return value or check its status to see whether
* the sequence of reads that has been performed succeeded.
*/
CHECK_RETURN_VALUE
Reader & ReadSigned32(int32_t * dest)
{
RawReadLowLevelBeCareful(dest);
return *this;
}

/**
* Read a single 64-bit signed integer.
*
* @param [out] dest Where the 64-bit integer goes.
*
* @note The read can put the reader in a failed-status state if there are
* not enough octets available. Callers must either continue to do
* more reads on the return value or check its status to see whether
* the sequence of reads that has been performed succeeded.
*/
CHECK_RETURN_VALUE
Reader & ReadSigned64(int64_t * dest)
{
RawReadLowLevelBeCareful(dest);
return *this;
}

Expand All @@ -180,7 +270,7 @@ class Reader
* delegate to this one.
*/
template <typename T>
void RawRead(T * retval);
void RawReadLowLevelBeCareful(T * retval);

/**
* Advance the Reader forward by the specified number of octets.
Expand Down
20 changes: 20 additions & 0 deletions src/lib/support/BufferWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ LittleEndian::BufferWriter & LittleEndian::BufferWriter::EndianPut(uint64_t x, s
return *this;
}

LittleEndian::BufferWriter & LittleEndian::BufferWriter::EndianPutSigned(int64_t x, size_t size)
{
while (size > 0)
{
Put(static_cast<uint8_t>(x & 0xff));
x >>= 8;
size--;
}
return *this;
}

BigEndian::BufferWriter & BigEndian::BufferWriter::EndianPut(uint64_t x, size_t size)
{
while (size-- > 0)
Expand All @@ -69,5 +80,14 @@ BigEndian::BufferWriter & BigEndian::BufferWriter::EndianPut(uint64_t x, size_t
return *this;
}

BigEndian::BufferWriter & BigEndian::BufferWriter::EndianPutSigned(int64_t x, size_t size)
{
while (size-- > 0)
{
Put(static_cast<uint8_t>((x >> (size * 8)) & 0xff));
}
return *this;
}

} // namespace Encoding
} // namespace chip
19 changes: 16 additions & 3 deletions src/lib/support/BufferWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,18 @@ class EndianBufferWriterBase : public BufferWriter
Derived & Put(uint8_t c) { return static_cast<Derived &>(BufferWriter::Put(c)); }
Derived & Skip(size_t len) { return static_cast<Derived &>(BufferWriter::Skip(len)); }

// write an integer into a buffer, in an endian specific way
// write an integer into a buffer, in an endian-specific way

Derived & Put8(uint8_t c) { return static_cast<Derived *>(this)->Put(c); }
Derived & Put16(uint16_t x) { return static_cast<Derived *>(this)->EndianPut(x, sizeof(x)); }
Derived & Put32(uint32_t x) { return static_cast<Derived *>(this)->EndianPut(x, sizeof(x)); }
Derived & Put64(uint64_t x) { return static_cast<Derived *>(this)->EndianPut(x, sizeof(x)); }

Derived & PutSigned8(int8_t x) { return static_cast<Derived *>(this)->EndianPutSigned(x, sizeof(x)); }
Derived & PutSigned16(int16_t x) { return static_cast<Derived *>(this)->EndianPutSigned(x, sizeof(x)); }
Derived & PutSigned32(int32_t x) { return static_cast<Derived *>(this)->EndianPutSigned(x, sizeof(x)); }
Derived & PutSigned64(int64_t x) { return static_cast<Derived *>(this)->EndianPutSigned(x, sizeof(x)); }

protected:
EndianBufferWriterBase(uint8_t * buf, size_t len) : BufferWriter(buf, len) {}
EndianBufferWriterBase(MutableByteSpan buf) : BufferWriter(buf.data(), buf.size()) {}
Expand All @@ -145,11 +150,15 @@ namespace LittleEndian {
class BufferWriter : public EndianBufferWriterBase<BufferWriter>
{
public:
BufferWriter(uint8_t * buf, size_t len) : EndianBufferWriterBase<BufferWriter>(buf, len) {}
BufferWriter(uint8_t * buf, size_t len) : EndianBufferWriterBase<BufferWriter>(buf, len)
{
static_assert((-1 & 3) == 3, "LittleEndian::BufferWriter only works with 2's complement architectures.");
}
BufferWriter(MutableByteSpan buf) : EndianBufferWriterBase<BufferWriter>(buf) {}
BufferWriter(const BufferWriter & other) = default;
BufferWriter & operator=(const BufferWriter & other) = default;
BufferWriter & EndianPut(uint64_t x, size_t size);
BufferWriter & EndianPutSigned(int64_t x, size_t size);
};

} // namespace LittleEndian
Expand All @@ -159,11 +168,15 @@ namespace BigEndian {
class BufferWriter : public EndianBufferWriterBase<BufferWriter>
{
public:
BufferWriter(uint8_t * buf, size_t len) : EndianBufferWriterBase<BufferWriter>(buf, len) {}
BufferWriter(uint8_t * buf, size_t len) : EndianBufferWriterBase<BufferWriter>(buf, len)
{
static_assert((-1 & 3) == 3, "BigEndian::BufferWriter only works with 2's complement architectures.");
}
BufferWriter(MutableByteSpan buf) : EndianBufferWriterBase<BufferWriter>(buf) {}
BufferWriter(const BufferWriter & other) = default;
BufferWriter & operator=(const BufferWriter & other) = default;
BufferWriter & EndianPut(uint64_t x, size_t size);
BufferWriter & EndianPutSigned(int64_t x, size_t size);
};

} // namespace BigEndian
Expand Down
Loading

0 comments on commit 9f95ab8

Please sign in to comment.