Skip to content

Commit

Permalink
Merge branch 'develop' into feature/updateClangConfig
Browse files Browse the repository at this point in the history
Signed-off-by: Cervenka Dusan <[email protected]>
  • Loading branch information
Hadatko committed Oct 24, 2023
2 parents 714f037 + 958569b commit e593dd6
Show file tree
Hide file tree
Showing 50 changed files with 849 additions and 764 deletions.
2 changes: 1 addition & 1 deletion erpc_c/infra/erpc_arbitrated_client_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void ArbitratedClientManager::performClientRequest(RequestContext &request)
// Send the request.
if (request.getCodec()->isStatusOk() == true)
{
err = m_arbitrator->send(request.getCodec()->getBuffer());
err = m_arbitrator->send(&request.getCodec()->getBufferRef());
request.getCodec()->updateStatus(err);
}

Expand Down
8 changes: 8 additions & 0 deletions erpc_c/infra/erpc_basic_codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ using namespace erpc;

const uint32_t BasicCodec::kBasicCodecVersion = 1UL;

BasicCodec::BasicCodec(void) : Codec() {}

BasicCodec::~BasicCodec(void) {}

void BasicCodec::startWriteMessage(message_type_t type, uint32_t service, uint32_t request, uint32_t sequence)
{
uint32_t header =
Expand Down Expand Up @@ -367,6 +371,10 @@ void BasicCodec::readNullFlag(bool &isNull)

ERPC_MANUALLY_CONSTRUCTED_ARRAY_STATIC(BasicCodec, s_basicCodecManual, ERPC_CODEC_COUNT);

BasicCodecFactory::BasicCodecFactory(void) : CodecFactory() {}

BasicCodecFactory::~BasicCodecFactory(void) {}

Codec *BasicCodecFactory::create(void)
{
ERPC_CREATE_NEW_OBJECT(BasicCodec, s_basicCodecManual, ERPC_CODEC_COUNT)
Expand Down
20 changes: 19 additions & 1 deletion erpc_c/infra/erpc_basic_codec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ class BasicCodec : public Codec
public:
static const uint32_t kBasicCodecVersion; /*!< Codec version. */

BasicCodec(void) : Codec() {}
/*!
* @brief Constructor.
*/
BasicCodec(void);

/*!
* @brief CodecFactory destructor
*/
virtual ~BasicCodec(void);

//! @name Encoding
//@{
Expand Down Expand Up @@ -341,6 +349,16 @@ class BasicCodec : public Codec
class BasicCodecFactory : public CodecFactory
{
public:
/*!
* @brief Constructor.
*/
BasicCodecFactory(void);

/*!
* @brief CodecFactory destructor
*/
virtual ~BasicCodecFactory(void);

/*!
* @brief Return created codec.
*
Expand Down
15 changes: 8 additions & 7 deletions erpc_c/infra/erpc_client_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void ClientManager::performClientRequest(RequestContext &request)
// Send invocation request to server.
if (request.getCodec()->isStatusOk() == true)
{
err = m_transport->send(request.getCodec()->getBuffer());
err = m_transport->send(&request.getCodec()->getBufferRef());
request.getCodec()->updateStatus(err);
}

Expand All @@ -88,7 +88,7 @@ void ClientManager::performClientRequest(RequestContext &request)
if (request.getCodec()->isStatusOk() == true)
{
// Receive reply.
err = m_transport->receive(request.getCodec()->getBuffer());
err = m_transport->receive(&request.getCodec()->getBufferRef());
request.getCodec()->updateStatus(err);
}

Expand Down Expand Up @@ -126,7 +126,7 @@ void ClientManager::performNestedClientRequest(RequestContext &request)
// Send invocation request to server.
if (request.getCodec()->isStatusOk() == true)
{
err = m_transport->send(request.getCodec()->getBuffer());
err = m_transport->send(&request.getCodec()->getBufferRef());
request.getCodec()->updateStatus(err);
}

Expand Down Expand Up @@ -167,7 +167,7 @@ void ClientManager::verifyReply(RequestContext &request)

// Some transport layers change the request's message buffer pointer (for things like zero
// copy support), so inCodec must be reset to work with correct buffer.
request.getCodec()->reset();
request.getCodec()->reset(m_transport->reserveHeaderSize());

// Extract the reply header.
request.getCodec()->startReadMessage(msgType, service, requestNumber, sequence);
Expand All @@ -186,13 +186,14 @@ Codec *ClientManager::createBufferAndCodec(void)
{
Codec *codec = m_codecFactory->create();
MessageBuffer message;
uint8_t reservedMessageSpace = m_transport->reserveHeaderSize();

if (codec != NULL)
{
message = m_messageFactory->create();
message = m_messageFactory->create(reservedMessageSpace);
if (NULL != message.get())
{
codec->setBuffer(message);
codec->setBuffer(message, reservedMessageSpace);
}
else
{
Expand All @@ -209,7 +210,7 @@ void ClientManager::releaseRequest(RequestContext &request)
{
if (request.getCodec() != NULL)
{
m_messageFactory->dispose(request.getCodec()->getBuffer());
m_messageFactory->dispose(&request.getCodec()->getBufferRef());
m_codecFactory->dispose(request.getCodec());
}
}
Expand Down
28 changes: 17 additions & 11 deletions erpc_c/infra/erpc_codec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Codec
*
* This function initializes object attributes.
*/
Codec(void) : m_buffer(), m_cursor(), m_status(kErpcStatus_Success) {}
Codec(void) : m_cursor(), m_status(kErpcStatus_Success) {}

/*!
* @brief Codec destructor
Expand All @@ -70,24 +70,31 @@ class Codec
*
* @return Pointer to used message buffer.
*/
MessageBuffer *getBuffer(void) { return &m_buffer; }
MessageBuffer getBuffer(void) { return m_cursor.getBuffer(); }

MessageBuffer &getBufferRef(void) { return m_cursor.getBufferRef(); }

/*!
* @brief Prototype for set message buffer used for read and write data.
*
* @param[in] buf Message buffer to set.
* @param[in] skip How many bytes to skip from reading.
*/
virtual void setBuffer(MessageBuffer &buf)
virtual void setBuffer(MessageBuffer &buf, uint8_t skip = 0)
{
m_buffer = buf;
m_cursor.set(&m_buffer);
m_cursor.setBuffer(buf, skip);
m_status = kErpcStatus_Success;
}

/*! @brief Reset the codec to initial state. */
virtual void reset(void)
/*!
* @brief Reset the codec to initial state.
*
* @param[in] skip How many bytes to skip from reading.
*/
virtual void reset(uint8_t skip = 0)
{
m_cursor.set(&m_buffer);
MessageBuffer buffer = m_cursor.getBuffer();
m_cursor.setBuffer(buffer, skip);
m_status = kErpcStatus_Success;
}

Expand Down Expand Up @@ -389,9 +396,8 @@ class Codec
virtual void readNullFlag(bool &isNull) = 0;

protected:
MessageBuffer m_buffer; /*!< Message buffer object */
MessageBuffer::Cursor m_cursor; /*!< Copy data to message buffers. */
erpc_status_t m_status; /*!< Status of serialized data. */
Cursor m_cursor; /*!< Copy data to message buffers. */
erpc_status_t m_status; /*!< Status of serialized data. */
};

/*!
Expand Down
103 changes: 77 additions & 26 deletions erpc_c/infra/erpc_framed_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ m_sendLock(), m_receiveLock()

FramedTransport::~FramedTransport(void) {}

uint8_t FramedTransport::reserveHeaderSize(void)
{
return sizeof(FramedTransport::Header::m_crcHeader) + sizeof(FramedTransport::Header::m_messageSize) +
sizeof(FramedTransport::Header::m_crcBody);
}

void FramedTransport::setCrc16(Crc16 *crcImpl)
{
erpc_assert(crcImpl);
Expand All @@ -46,57 +52,78 @@ Crc16 *FramedTransport::getCrc16(void)

erpc_status_t FramedTransport::receive(MessageBuffer *message)
{
Header h;
Header h = { 0, 0, 0 };
erpc_status_t retVal;
uint16_t computedCrc;
uint8_t offset = 0;

erpc_assert((m_crcImpl != NULL) && ("Uninitialized Crc16 object." != NULL));

// e.g. rpmsg tty may have nullptr and buffer is assigned in receive function.
if ((message->get() != nullptr) && (message->getLength() < reserveHeaderSize()))
{
retVal = kErpcStatus_MemoryError;
}
else
{
#if !ERPC_THREADS_IS(NONE)
Mutex::Guard lock(m_receiveLock);
#endif

// Receive header first.
retVal = underlyingReceive((uint8_t *)&h, sizeof(h));
retVal = underlyingReceive(message, reserveHeaderSize(), 0);
if ((retVal == kErpcStatus_Success) && (message->getLength() < reserveHeaderSize()))
{
retVal = kErpcStatus_MemoryError;
}

if (retVal == kErpcStatus_Success)
{
static_cast<void>(memcpy(&h.m_crcHeader, message->get(), sizeof(h.m_crcHeader)));
offset = sizeof(h.m_crcHeader);
static_cast<void>(memcpy(&h.m_messageSize, &message->get()[offset], sizeof(h.m_messageSize)));
offset += sizeof(h.m_messageSize);
static_cast<void>(memcpy(&h.m_crcBody, &message->get()[offset], sizeof(h.m_crcBody)));
offset += sizeof(h.m_crcBody);

ERPC_READ_AGNOSTIC_16(h.m_crcHeader);
ERPC_READ_AGNOSTIC_16(h.m_messageSize);
ERPC_READ_AGNOSTIC_16(h.m_crc);
ERPC_READ_AGNOSTIC_16(h.m_crcBody);

// received size can't be zero.
if (h.m_messageSize == 0U)
computedCrc =
m_crcImpl->computeCRC16(reinterpret_cast<const uint8_t *>(&h.m_messageSize), sizeof(h.m_messageSize)) +
m_crcImpl->computeCRC16(reinterpret_cast<const uint8_t *>(&h.m_crcBody), sizeof(h.m_crcBody));
if (computedCrc != h.m_crcHeader)
{
retVal = kErpcStatus_ReceiveFailed;
retVal = kErpcStatus_CrcCheckFailed;
}
}

if (retVal == kErpcStatus_Success)
{
// received size can't be larger then buffer length.
if (h.m_messageSize > message->getLength())
if ((h.m_messageSize + reserveHeaderSize()) > message->getLength())
{
retVal = kErpcStatus_ReceiveFailed;
}
}

if (retVal == kErpcStatus_Success)
{
// Receive rest of the message now we know its size.
retVal = underlyingReceive(message->get(), h.m_messageSize);
// rpmsg tty can receive all data in one buffer, others need second call.
if (message->getUsed() < (h.m_messageSize + reserveHeaderSize()))
{
// Receive rest of the message now we know its size.
retVal = underlyingReceive(message, h.m_messageSize, offset);
}
}
}

if (retVal == kErpcStatus_Success)
{
// Verify CRC.
computedCrc = m_crcImpl->computeCRC16(message->get(), h.m_messageSize);
if (computedCrc == h.m_crc)
{
message->setUsed(h.m_messageSize);
}
else
computedCrc = m_crcImpl->computeCRC16(&message->get()[offset], h.m_messageSize);
if (computedCrc != h.m_crcBody)
{
retVal = kErpcStatus_CrcCheckFailed;
}
Expand All @@ -110,27 +137,51 @@ erpc_status_t FramedTransport::send(MessageBuffer *message)
erpc_status_t ret;
uint16_t messageLength;
Header h;
uint8_t offset;

erpc_assert((m_crcImpl != NULL) && ("Uninitialized Crc16 object." != NULL));

#if !ERPC_THREADS_IS(NONE)
Mutex::Guard lock(m_sendLock);
#endif

messageLength = message->getUsed();
messageLength = message->getUsed() - reserveHeaderSize();

// Send header first.
h.m_messageSize = messageLength;
h.m_crc = m_crcImpl->computeCRC16(message->get(), messageLength);
h.m_crcBody = m_crcImpl->computeCRC16(&message->get()[reserveHeaderSize()], messageLength);
h.m_crcHeader =
m_crcImpl->computeCRC16(reinterpret_cast<const uint8_t *>(&h.m_messageSize), sizeof(h.m_messageSize)) +
m_crcImpl->computeCRC16(reinterpret_cast<const uint8_t *>(&h.m_crcBody), sizeof(h.m_crcBody));

ERPC_WRITE_AGNOSTIC_16(h.m_crcHeader);
ERPC_WRITE_AGNOSTIC_16(h.m_messageSize);
ERPC_WRITE_AGNOSTIC_16(h.m_crc);
ERPC_WRITE_AGNOSTIC_16(h.m_crcBody);

ret = underlyingSend((uint8_t *)&h, sizeof(h));
if (ret == kErpcStatus_Success)
static_cast<void>(memcpy(message->get(), reinterpret_cast<const uint8_t *>(&h.m_crcHeader), sizeof(h.m_crcHeader)));
offset = sizeof(h.m_crcHeader);
static_cast<void>(
memcpy(&message->get()[offset], reinterpret_cast<const uint8_t *>(&h.m_messageSize), sizeof(h.m_messageSize)));
offset += sizeof(h.m_messageSize);
static_cast<void>(
memcpy(&message->get()[offset], reinterpret_cast<const uint8_t *>(&h.m_crcBody), sizeof(h.m_crcBody)));

ret = underlyingSend(message, message->getUsed(), 0);

return ret;
}

erpc_status_t FramedTransport::underlyingSend(MessageBuffer *message, uint32_t size, uint32_t offset)
{
erpc_status_t retVal = underlyingSend(&message->get()[offset], size);

return retVal;
}

erpc_status_t FramedTransport::underlyingReceive(MessageBuffer *message, uint32_t size, uint32_t offset)
{
erpc_status_t retVal = underlyingReceive(&message->get()[offset], size);

if (retVal == kErpcStatus_Success)
{
ret = underlyingSend(message->get(), messageLength);
message->setUsed(size + offset);
}

return ret;
return retVal;
}
Loading

0 comments on commit e593dd6

Please sign in to comment.