msgbuf: Define a separate msgbuf structure for encode/decode function
Define separate msgbuf structures to avoid casting away const-qualifiers
in the msgbuf constructor function:
* pldm_msgbuf_rw: for encode functions with non const-qualified buffer
* pldm_msgbuf_ro: for decode functions with const-qualified buffer
Further, use _Generic() to keep the API ergonomic while still yielding a
compile error when wrong msgbuf type is passed.
Change-Id: I71dbcb7996e9fb402b49870fce539a939c1497e5
Signed-off-by: John Chung <john.chung@arm.com>
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
diff --git a/tests/msgbuf_generic.c b/tests/msgbuf_generic.c
index 62160f3..28c8a53 100644
--- a/tests/msgbuf_generic.c
+++ b/tests/msgbuf_generic.c
@@ -28,8 +28,8 @@
static void test_msgbuf_extract_generic_uint8(void)
{
- struct pldm_msgbuf _ctx;
- struct pldm_msgbuf* ctx = &_ctx;
+ struct pldm_msgbuf_ro _ctx;
+ struct pldm_msgbuf_ro* ctx = &_ctx;
uint8_t buf[1] = {0xa5};
uint8_t val;
@@ -41,8 +41,8 @@
static void test_msgbuf_extract_generic_int8(void)
{
- struct pldm_msgbuf _ctx;
- struct pldm_msgbuf* ctx = &_ctx;
+ struct pldm_msgbuf_ro _ctx;
+ struct pldm_msgbuf_ro* ctx = &_ctx;
int8_t buf[1] = {-1};
int8_t val;
@@ -54,8 +54,8 @@
static void test_msgbuf_extract_generic_uint16(void)
{
- struct pldm_msgbuf _ctx;
- struct pldm_msgbuf* ctx = &_ctx;
+ struct pldm_msgbuf_ro _ctx;
+ struct pldm_msgbuf_ro* ctx = &_ctx;
uint16_t buf[1] = {0x5aa5};
uint16_t val;
@@ -67,8 +67,8 @@
static void test_msgbuf_extract_generic_int16(void)
{
- struct pldm_msgbuf _ctx;
- struct pldm_msgbuf* ctx = &_ctx;
+ struct pldm_msgbuf_ro _ctx;
+ struct pldm_msgbuf_ro* ctx = &_ctx;
int16_t buf[1] = {(int16_t)(htole16((uint16_t)INT16_MIN))};
int16_t val;
@@ -80,8 +80,8 @@
static void test_msgbuf_extract_generic_uint32(void)
{
- struct pldm_msgbuf _ctx;
- struct pldm_msgbuf* ctx = &_ctx;
+ struct pldm_msgbuf_ro _ctx;
+ struct pldm_msgbuf_ro* ctx = &_ctx;
uint32_t buf[1] = {0x5a00ffa5};
uint32_t val;
@@ -93,8 +93,8 @@
static void test_msgbuf_extract_generic_int32(void)
{
- struct pldm_msgbuf _ctx;
- struct pldm_msgbuf* ctx = &_ctx;
+ struct pldm_msgbuf_ro _ctx;
+ struct pldm_msgbuf_ro* ctx = &_ctx;
int32_t buf[1] = {(int32_t)(htole32((uint32_t)INT32_MIN))};
int32_t val;
@@ -106,8 +106,8 @@
static void test_msgbuf_extract_generic_real32(void)
{
- struct pldm_msgbuf _ctx;
- struct pldm_msgbuf* ctx = &_ctx;
+ struct pldm_msgbuf_ro _ctx;
+ struct pldm_msgbuf_ro* ctx = &_ctx;
uint32_t buf[1];
uint32_t xform;
real32_t val;
@@ -125,8 +125,8 @@
static void test_msgbuf_extract_array_generic_uint8(void)
{
- struct pldm_msgbuf _ctx;
- struct pldm_msgbuf* ctx = &_ctx;
+ struct pldm_msgbuf_ro _ctx;
+ struct pldm_msgbuf_ro* ctx = &_ctx;
uint32_t buf[1] = {0};
uint8_t arr[1];
@@ -138,8 +138,8 @@
static void test_msgbuf_insert_generic_int32(void)
{
- struct pldm_msgbuf _ctx;
- struct pldm_msgbuf* ctx = &_ctx;
+ struct pldm_msgbuf_rw _ctx;
+ struct pldm_msgbuf_rw* ctx = &_ctx;
int32_t src = -12345;
int32_t checkVal = 0;
uint8_t buf[sizeof(int32_t)] = {0};
@@ -147,8 +147,8 @@
expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)) == 0);
expect(pldm_msgbuf_insert(ctx, src) == 0);
- struct pldm_msgbuf _ctxExtract;
- struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+ struct pldm_msgbuf_ro _ctxExtract;
+ struct pldm_msgbuf_ro* ctxExtract = &_ctxExtract;
expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
@@ -160,8 +160,8 @@
static void test_msgbuf_insert_generic_uint32(void)
{
- struct pldm_msgbuf _ctx;
- struct pldm_msgbuf* ctx = &_ctx;
+ struct pldm_msgbuf_rw _ctx;
+ struct pldm_msgbuf_rw* ctx = &_ctx;
uint32_t src = 0xf1223344;
uint32_t checkVal = 0;
uint8_t buf[sizeof(uint32_t)] = {0};
@@ -169,8 +169,8 @@
expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)) == 0);
expect(pldm_msgbuf_insert(ctx, src) == 0);
- struct pldm_msgbuf _ctxExtract;
- struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+ struct pldm_msgbuf_ro _ctxExtract;
+ struct pldm_msgbuf_ro* ctxExtract = &_ctxExtract;
expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
@@ -182,8 +182,8 @@
static void test_msgbuf_insert_generic_uint16(void)
{
- struct pldm_msgbuf _ctx;
- struct pldm_msgbuf* ctx = &_ctx;
+ struct pldm_msgbuf_rw _ctx;
+ struct pldm_msgbuf_rw* ctx = &_ctx;
uint16_t src = 0xf344;
uint16_t checkVal = 0;
uint8_t buf[sizeof(uint16_t)] = {0};
@@ -191,8 +191,8 @@
expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(uint16_t)) == 0);
expect(pldm_msgbuf_insert(ctx, src) == 0);
- struct pldm_msgbuf _ctxExtract;
- struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+ struct pldm_msgbuf_ro _ctxExtract;
+ struct pldm_msgbuf_ro* ctxExtract = &_ctxExtract;
expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
@@ -204,8 +204,8 @@
static void test_msgbuf_insert_generic_int16(void)
{
- struct pldm_msgbuf _ctx;
- struct pldm_msgbuf* ctx = &_ctx;
+ struct pldm_msgbuf_rw _ctx;
+ struct pldm_msgbuf_rw* ctx = &_ctx;
int16_t src = -12;
int16_t checkVal = 0;
uint8_t buf[sizeof(int16_t)] = {0};
@@ -213,8 +213,8 @@
expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(uint16_t)) == 0);
expect(pldm_msgbuf_insert(ctx, src) == 0);
- struct pldm_msgbuf _ctxExtract;
- struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+ struct pldm_msgbuf_ro _ctxExtract;
+ struct pldm_msgbuf_ro* ctxExtract = &_ctxExtract;
expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
@@ -226,8 +226,8 @@
static void test_msgbuf_insert_generic_uint8(void)
{
- struct pldm_msgbuf _ctx;
- struct pldm_msgbuf* ctx = &_ctx;
+ struct pldm_msgbuf_rw _ctx;
+ struct pldm_msgbuf_rw* ctx = &_ctx;
uint8_t src = 0xf4;
uint8_t checkVal = 0;
uint8_t buf[sizeof(uint8_t)] = {0};
@@ -235,8 +235,8 @@
expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)) == 0);
expect(pldm_msgbuf_insert(ctx, src) == 0);
- struct pldm_msgbuf _ctxExtract;
- struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+ struct pldm_msgbuf_ro _ctxExtract;
+ struct pldm_msgbuf_ro* ctxExtract = &_ctxExtract;
expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
@@ -248,8 +248,8 @@
static void test_msgbuf_insert_generic_int8(void)
{
- struct pldm_msgbuf _ctx;
- struct pldm_msgbuf* ctx = &_ctx;
+ struct pldm_msgbuf_rw _ctx;
+ struct pldm_msgbuf_rw* ctx = &_ctx;
int8_t src = -4;
int8_t checkVal = 0;
uint8_t buf[sizeof(int8_t)] = {0};
@@ -257,8 +257,8 @@
expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)) == 0);
expect(pldm_msgbuf_insert(ctx, src) == 0);
- struct pldm_msgbuf _ctxExtract;
- struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+ struct pldm_msgbuf_ro _ctxExtract;
+ struct pldm_msgbuf_ro* ctxExtract = &_ctxExtract;
expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
@@ -270,8 +270,8 @@
static void test_msgbuf_insert_array_generic_uint8(void)
{
- struct pldm_msgbuf _ctx;
- struct pldm_msgbuf* ctx = &_ctx;
+ struct pldm_msgbuf_rw _ctx;
+ struct pldm_msgbuf_rw* ctx = &_ctx;
uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
uint8_t buf[6] = {0};
uint8_t retBuff[6] = {0};
@@ -279,8 +279,8 @@
expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)) == 0);
expect(pldm_msgbuf_insert_array(ctx, sizeof(src), src, sizeof(src)) == 0);
- struct pldm_msgbuf _ctxExtract;
- struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+ struct pldm_msgbuf_ro _ctxExtract;
+ struct pldm_msgbuf_ro* ctxExtract = &_ctxExtract;
expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
expect(pldm_msgbuf_extract_array(ctxExtract, sizeof(retBuff), retBuff,