Skip to content

Commit

Permalink
Merge pull request #276 from amgross/endianness_agnostic
Browse files Browse the repository at this point in the history
Add defines for changing transport to endianness agnostic
  • Loading branch information
MichalPrincNXP authored Jun 7, 2022
2 parents de940fe + 9f152a2 commit 3abb86d
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 2 deletions.
18 changes: 17 additions & 1 deletion erpc_c/config/erpc_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,26 @@

//! @name Assert function definition
//@{
//! User custom asser defition. Include header file if needed before bellow line. If assert is not enabled, default will be used.
//! User custom asser defition. Include header file if needed before bellow line. If assert is not enabled, default will
//! be used.
// #define erpc_assert(condition)
//@}

//! @def ENDIANES_HEADER
//!
//! Include header file that controls the communication endianness
//!
//! Uncomment for example behaviour for endianness agnostic with:
//! 1. communication in little endian.
//! 2. current processor is big endian.
//! 3. pointer size is 32 bit.
//! 4. float+double scheme not defined, so throws assert if passes.
//! #define ERPC_PROCESSOR_ENDIANNESS_LITTLE 0
//! #define ERPC_COMMUNICATION_LITTLE 1
//! #define ERPC_POINTER_SIZE_16 0
//! #define ERPC_POINTER_SIZE_32 1
//! #define ERPC_POINTER_SIZE_64 0
//! #define ENDIANNESS_HEADER "erpc_endianness_agnostic_example.h"

/*! @} */
#endif // _ERPC_CONFIG_H_
Expand Down
59 changes: 58 additions & 1 deletion erpc_c/infra/erpc_basic_codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/

#include "erpc_basic_codec.h"
#include "erpc_config_internal.h"
#include ENDIANNESS_HEADER
#include "erpc_manually_constructed.h"

#if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_DYNAMIC
Expand All @@ -26,7 +28,8 @@ const uint32_t BasicCodec::kBasicCodecVersion = 1UL;

void BasicCodec::startWriteMessage(message_type_t type, uint32_t service, uint32_t request, uint32_t sequence)
{
uint32_t header = (kBasicCodecVersion << 24u) | ((service & 0xffu) << 16u) | ((request & 0xffu) << 8u) | ((uint32_t)type & 0xffu);
uint32_t header =
(kBasicCodecVersion << 24u) | ((service & 0xffu) << 16u) | ((request & 0xffu) << 8u) | ((uint32_t)type & 0xffu);

write(header);

Expand Down Expand Up @@ -56,16 +59,22 @@ void BasicCodec::write(int8_t value)

void BasicCodec::write(int16_t value)
{
ERPC_WRITE_AGNOSTIC_16(value);

writeData(&value, sizeof(value));
}

void BasicCodec::write(int32_t value)
{
ERPC_WRITE_AGNOSTIC_32(value);

writeData(&value, sizeof(value));
}

void BasicCodec::write(int64_t value)
{
ERPC_WRITE_AGNOSTIC_64(value);

writeData(&value, sizeof(value));
}

Expand All @@ -76,26 +85,36 @@ void BasicCodec::write(uint8_t value)

void BasicCodec::write(uint16_t value)
{
ERPC_WRITE_AGNOSTIC_16(value);

writeData(&value, sizeof(value));
}

void BasicCodec::write(uint32_t value)
{
ERPC_WRITE_AGNOSTIC_32(value);

writeData(&value, sizeof(value));
}

void BasicCodec::write(uint64_t value)
{
ERPC_WRITE_AGNOSTIC_64(value);

writeData(&value, sizeof(value));
}

void BasicCodec::write(float value)
{
ERPC_WRITE_AGNOSTIC_FLOAT(value);

writeData(&value, sizeof(value));
}

void BasicCodec::write(double value)
{
ERPC_WRITE_AGNOSTIC_DOUBLE(value);

writeData(&value, sizeof(value));
}

Expand All @@ -105,6 +124,8 @@ void BasicCodec::writePtr(uintptr_t value)

write(ptrSize);

ERPC_WRITE_AGNOSTIC_PTR(value);

writeData(&value, ptrSize);
}

Expand Down Expand Up @@ -218,16 +239,28 @@ void BasicCodec::read(int8_t *value)
void BasicCodec::read(int16_t *value)
{
readData(value, sizeof(*value));
if (isStatusOk())
{
ERPC_READ_AGNOSTIC_16(*value);
}
}

void BasicCodec::read(int32_t *value)
{
readData(value, sizeof(*value));
if (isStatusOk())
{
ERPC_READ_AGNOSTIC_32(*value);
}
}

void BasicCodec::read(int64_t *value)
{
readData(value, sizeof(*value));
if (isStatusOk())
{
ERPC_READ_AGNOSTIC_64(*value);
}
}

void BasicCodec::read(uint8_t *value)
Expand All @@ -238,26 +271,46 @@ void BasicCodec::read(uint8_t *value)
void BasicCodec::read(uint16_t *value)
{
readData(value, sizeof(*value));
if (isStatusOk())
{
ERPC_READ_AGNOSTIC_16(*value);
}
}

void BasicCodec::read(uint32_t *value)
{
readData(value, sizeof(*value));
if (isStatusOk())
{
ERPC_READ_AGNOSTIC_32(*value);
}
}

void BasicCodec::read(uint64_t *value)
{
readData(value, sizeof(*value));
if (isStatusOk())
{
ERPC_READ_AGNOSTIC_64(*value);
}
}

void BasicCodec::read(float *value)
{
readData(value, sizeof(*value));
if (isStatusOk())
{
ERPC_READ_AGNOSTIC_FLOAT(*value);
}
}

void BasicCodec::read(double *value)
{
readData(value, sizeof(*value));
if (isStatusOk())
{
ERPC_READ_AGNOSTIC_DOUBLE(*value);
}
}

void BasicCodec::readPtr(uintptr_t *value)
Expand All @@ -272,6 +325,10 @@ void BasicCodec::readPtr(uintptr_t *value)
}

readData(value, ptrSize);
if (isStatusOk())
{
ERPC_READ_AGNOSTIC_PTR(*value);
}
}

void BasicCodec::readString(uint32_t *length, char **value)
Expand Down
10 changes: 10 additions & 0 deletions erpc_c/infra/erpc_framed_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/

#include "erpc_framed_transport.h"

#include "erpc_config_internal.h"
#include ENDIANNESS_HEADER
#include "erpc_message_buffer.h"

#include <cstdio>
Expand Down Expand Up @@ -55,6 +58,9 @@ erpc_status_t FramedTransport::receive(MessageBuffer *message)

if (retVal == kErpcStatus_Success)
{
ERPC_READ_AGNOSTIC_16(h.m_messageSize);
ERPC_READ_AGNOSTIC_16(h.m_crc);

// received size can't be zero.
if (h.m_messageSize == 0U)
{
Expand Down Expand Up @@ -112,6 +118,10 @@ erpc_status_t FramedTransport::send(MessageBuffer *message)
// Send header first.
h.m_messageSize = messageLength;
h.m_crc = m_crcImpl->computeCRC16(message->get(), messageLength);

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

ret = underlyingSend((uint8_t *)&h, sizeof(h));
if (ret == kErpcStatus_Success)
{
Expand Down
5 changes: 5 additions & 0 deletions erpc_c/port/erpc_config_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@
#endif
#endif

// Disabling endianness agnostic feature.
#ifndef ENDIANNESS_HEADER
#define ENDIANNESS_HEADER "erpc_endianness_undefined.h"
#endif

/* clang-format on */
#endif // _ERPC_DETECT_H_
////////////////////////////////////////////////////////////////////////////////
Expand Down
51 changes: 51 additions & 0 deletions erpc_c/port/erpc_endianness_agnostic_example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* This is example file for endianness agnostic communication based on byteswap.h.
* Other approach can be done with htons(), htonl(), ntohs(), ntohl() and such.
*/

#ifndef _ERPC_ENDIANNESS_AGNOSTIC_EXAMPLE_H_
#define _ERPC_ENDIANNESS_AGNOSTIC_EXAMPLE_H_

#if ERPC_PROCESSOR_ENDIANNESS_LITTLE != ERPC_COMMUNICATION_LITTLE
#include <byteswap.h>

#define ERPC_WRITE_AGNOSTIC_16(value) (value) = __bswap_16(value);
#define ERPC_WRITE_AGNOSTIC_32(value) (value) = __bswap_32(value);
#define ERPC_WRITE_AGNOSTIC_64(value) (value) = __bswap_64(value);

#define ERPC_READ_AGNOSTIC_16(value) (value) = __bswap_16(value);
#define ERPC_READ_AGNOSTIC_32(value) (value) = __bswap_32(value);
#define ERPC_READ_AGNOSTIC_64(value) (value) = __bswap_64(value);

#if ERPC_POINTER_SIZE_16
#define ERPC_WRITE_AGNOSTIC_PTR(value) ERPC_WRITE_AGNOSTIC_16(value)
#define ERPC_READ_AGNOSTIC_PTR(value) ERPC_READ_AGNOSTIC_16(value)
#elif ERPC_POINTER_SIZE_32
#define ERPC_WRITE_AGNOSTIC_PTR(value) ERPC_WRITE_AGNOSTIC_32(value)
#define ERPC_READ_AGNOSTIC_PTR(value) ERPC_READ_AGNOSTIC_32(value)
#elif ERPC_POINTER_SIZE_64
#define ERPC_WRITE_AGNOSTIC_PTR(value) ERPC_WRITE_AGNOSTIC_64(value)
#define ERPC_READ_AGNOSTIC_PTR(value) ERPC_READ_AGNOSTIC_64(value)
#else
#error unknown pointer size
#endif

#else

#define ERPC_WRITE_AGNOSTIC_16(value)
#define ERPC_WRITE_AGNOSTIC_32(value)
#define ERPC_WRITE_AGNOSTIC_64(value)
#define ERPC_WRITE_AGNOSTIC_PTR(value)

#define ERPC_READ_AGNOSTIC_16(value)
#define ERPC_READ_AGNOSTIC_32(value)
#define ERPC_READ_AGNOSTIC_64(value)
#define ERPC_READ_AGNOSTIC_PTR(value)
#endif

#define ERPC_WRITE_AGNOSTIC_FLOAT(value) erpc_assert(0);
#define ERPC_WRITE_AGNOSTIC_DOUBLE(value) erpc_assert(0);
#define ERPC_READ_AGNOSTIC_FLOAT(value) erpc_assert(0);
#define ERPC_READ_AGNOSTIC_DOUBLE(value) erpc_assert(0);

#endif
20 changes: 20 additions & 0 deletions erpc_c/port/erpc_endianness_undefined.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

#ifndef _ERPC_ENDIANNESS_UNDEFINED_H_
#define _ERPC_ENDIANNESS_UNDEFINED_H_

// Disabling endianness agnostic feature.
#define ERPC_WRITE_AGNOSTIC_16(value)
#define ERPC_WRITE_AGNOSTIC_32(value)
#define ERPC_WRITE_AGNOSTIC_64(value)
#define ERPC_WRITE_AGNOSTIC_FLOAT(value)
#define ERPC_WRITE_AGNOSTIC_DOUBLE(value)
#define ERPC_WRITE_AGNOSTIC_PTR(value)

#define ERPC_READ_AGNOSTIC_16(value)
#define ERPC_READ_AGNOSTIC_32(value)
#define ERPC_READ_AGNOSTIC_64(value)
#define ERPC_READ_AGNOSTIC_FLOAT(value)
#define ERPC_READ_AGNOSTIC_DOUBLE(value)
#define ERPC_READ_AGNOSTIC_PTR(value)

#endif

0 comments on commit 3abb86d

Please sign in to comment.