Skip to content

Commit

Permalink
move keeping track of encoding to codec a la TLV (#898)
Browse files Browse the repository at this point in the history
* move keeping track of encoding to codec a la TLV

* missed some consumers

* missed some consumers

* fixup DecodeEnd
  • Loading branch information
Rob Walker authored May 28, 2020
1 parent c965d8b commit ef2b681
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 115 deletions.
6 changes: 3 additions & 3 deletions examples/chip-tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ void DoOnOff(DeviceController::ChipDeviceController * controller, Command comman
static const size_t bufferSize = 1024;
auto * buffer = System::PacketBuffer::NewWithAvailableSize(bufferSize);

ChipZclBuffer_t zcl_buffer = { buffer->Start(), 0, 0, bufferSize };
ChipZclBuffer_t zcl_buffer = { buffer->Start(), bufferSize, 0 };
ChipZclCommandContext_t ctx = {
1, // endpointId
CHIP_ZCL_CLUSTER_ON_OFF, // clusterId
Expand All @@ -219,8 +219,8 @@ void DoOnOff(DeviceController::ChipDeviceController * controller, Command comman
nullptr // response
};
chipZclEncodeZclHeader(&zcl_buffer, &ctx);
chipZclBufferFinishWriting(&zcl_buffer);
const size_t data_len = chipZclBufferUsedLength(&zcl_buffer);

const size_t data_len = chipZclBufferDataLength(&zcl_buffer);

#ifdef DEBUG
fprintf(stderr, "SENDING: %zu ", data_len);
Expand Down
32 changes: 19 additions & 13 deletions src/app/chip-zcl/chip-zcl-buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ typedef struct
* The data storage for our buffer.
*/
uint8_t * buffer;

/**
* The size of our data storage.
*/
uint16_t totalLength;
/**
* The current read/write position.
* The size of our buffer above.
*/
uint16_t currentPosition;
uint16_t bufferLength;

/**
* The length of the data that can be read from this buffer; nonzero only
* when it's ready to be read from.
Expand Down Expand Up @@ -85,10 +83,8 @@ uint8_t * chipZclBufferPointer(ChipZclBuffer_t * buffer);
void chipZclBufferFree(ChipZclBuffer_t * buffer);

/**
* Function that resets a buffer.
*
* After this call, the buffer is ready for reading or writing from the
* beginning again, depending on whether it was in reading more or writing mode.
* Function that resets a buffer to have its entire allocated length
* available for writing.
*
* @param[in] buffer the buffer to reset.
*/
Expand All @@ -100,15 +96,25 @@ void chipZclBufferReset(ChipZclBuffer_t * buffer);
* being written to.
*
* @param[in] buffer the buffer whose used length we want.
* @return The number of bytes the given buffer holds.
* @return The number of bytes of data the given buffer holds.
*/
uint16_t chipZclBufferUsedLength(ChipZclBuffer_t * buffer);
uint16_t chipZclBufferDataLength(ChipZclBuffer_t * buffer);

/**
* Indicates that we are done writing to a buffer and prepares it for reading.
*
* @param[in] buffer the buffer we are done writing to.
* @param[in] newLength the length of the written data
*/
void chipZclBufferSetDataLength(ChipZclBuffer_t * buffer, uint16_t newLength);

/**
* Function that returns available space remaining in the buffer after any
* data in the buffer.
*
* @param[in] buffer the buffer we are interested in
* @return The number of bytes left available for writing after any data
*/
void chipZclBufferFinishWriting(ChipZclBuffer_t * buffer);
uint16_t chipZclBufferAvailableLength(ChipZclBuffer_t * buffer);

#endif // CHIP_ZCL_BUFFER
37 changes: 30 additions & 7 deletions src/app/chip-zcl/chip-zcl-codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,61 @@

#include "chip-zcl-struct.h"
#include "chip-zcl.h"

#include <stdbool.h>
#include <stdint.h>

/**
* Codec keeps track of an ongoing encode/decode session of a Buffer
*/
typedef struct ChipZclCodec_t
{
/**
* Buffer into/out of which this codec is co-dec'ing
*/
ChipZclBuffer_t * buffer;

/**
* The current read/write position.
*/
uint16_t cursor;

/**
* space available in the buffer for writing
*/
uint16_t available;

} ChipZclCodec_t;

/**
* @brief Starts the encoding process. if there is any kind of preamble of anything, this function is responsible for putting it
* there.
*/
ChipZclStatus_t chipZclCodecEncodeStart(ChipZclBuffer_t * buffer);
ChipZclStatus_t chipZclCodecEncodeStart(ChipZclCodec_t * me, ChipZclBuffer_t * buffer);

/**
* @brief Encodes a single value of a given type.
*/
ChipZclStatus_t chipZclCodecEncode(ChipZclBuffer_t * buffer, ChipZclType_t type, void * ptr, uint16_t ptrLen);
ChipZclStatus_t chipZclCodecEncode(ChipZclCodec_t * codec, ChipZclType_t type, void * ptr, uint16_t ptrLen);

/**
* @brief Ends the encoding process. After this call the buffer is ready to go back to the lower layers.
*/
ChipZclStatus_t chipZclCodecEncodeEnd(ChipZclBuffer_t * buffer);
ChipZclStatus_t chipZclCodecEncodeEnd(ChipZclCodec_t * codec);

/**
* @brief Starts the decoding process. if there is any kind of preamble of anything, this function is responsible for decoding it.
*/
ChipZclStatus_t chipZclCodecDecodeStart(ChipZclBuffer_t * buffer);
ChipZclStatus_t chipZclCodecDecodeStart(ChipZclCodec_t * codec, ChipZclBuffer_t * buffer);

/**
* @brief Decodes a single value and puts it into the pointer. If retLen is not NULL, the size of decoded value is put there.
*/
ChipZclStatus_t chipZclCodecDecode(ChipZclBuffer_t * buffer, ChipZclType_t type, void * ptr, uint16_t ptrLen, uint16_t * retLen);
ChipZclStatus_t chipZclCodecDecode(ChipZclCodec_t * codec, ChipZclType_t type, void * ptr, uint16_t ptrLen, uint16_t * retLen);

/**
* @brief Ends the decoding process. After this call, buffer should no longer be used for further decoding.
* @brief Call after decoding to verify that everything has been decoded
*/
ChipZclStatus_t chipZclCodecDecodeEnd(ChipZclBuffer_t * buffer);
ChipZclStatus_t chipZclCodecDecodeEnd(ChipZclCodec_t * me);

#endif // CHIP_ZCL_CODEC
5 changes: 0 additions & 5 deletions src/app/chip-zcl/chip-zcl.h
Original file line number Diff line number Diff line change
Expand Up @@ -976,9 +976,6 @@ void chipZclEventSetDelayMs(Event * event, uint32_t delay);
// Endpoint Management
ChipZclEndpointId_t chipZclEndpointIndexToId(ChipZclEndpointIndex_t index, const ChipZclClusterSpec_t * clusterSpec);

// Toplevel functions
ChipZclStatus_t chipZclDecode(ChipZclBuffer_t * buffer);

// Some platform CHIP_ZCL_STATUS_INSUFFICIENT_SPACE
#define MEMSET(d, v, l) memset(d, v, l)
#define MEMCOPY(d, s, l) memcpy(d, s, l)
Expand Down Expand Up @@ -1116,8 +1113,6 @@ void chipZclEncodeZclHeader(ChipZclBuffer_t * buffer, ChipZclCommandContext_t *
*/
void chipZclDecodeZclHeader(ChipZclBuffer_t * buffer, ChipZclCommandContext_t * context);

ChipZclCommandContext_t * createCommandContext(ChipZclEndpointId_t endpointId, ChipZclClusterId_t clusterId);

ChipZclStatus_t chipZclProcessIncoming(uint8_t * buffer, uint16_t bufferLength);

#endif // CHIP_ZCL_MASTER_HEADER
10 changes: 6 additions & 4 deletions src/app/plugin/binding-mock/mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ void chipZclReverseClusterSpec(const ChipZclClusterSpec_t * s1, ChipZclClusterSp
ChipZclBuffer_t * chipZclBufferAlloc(uint16_t allocatedLength)
{
ChipZclBuffer_t * buffer = (ChipZclBuffer_t *) malloc(sizeof(ChipZclBuffer_t) + allocatedLength);
buffer->buffer = (uint8_t *) (buffer + 1);
buffer->dataLength = 0;
buffer->currentPosition = 0;
buffer->totalLength = allocatedLength;
if (NULL != buffer)
{
buffer->buffer = (uint8_t *) (buffer + 1);
buffer->dataLength = 0;
buffer->bufferLength = allocatedLength;
}
return buffer;
}

Expand Down
Loading

0 comments on commit ef2b681

Please sign in to comment.