pkt_unpack_struct

isolation_bkp/dynres
Dominik Madarász 2021-05-04 01:38:47 +02:00
parent b135b62a2b
commit 93c68ee3c8
3 changed files with 58 additions and 12 deletions

View File

@ -36,3 +36,22 @@ int32_t pkt_header_decode(pkt_header *table, void *data, size_t datalen) {
return pkt_validate_eof_msg(&uc);
}
int32_t pkt_unpack_struct(cw_unpack_context *uc, pkt_desc *desc, void *raw_blob, uint32_t blob_size) {
uint8_t *blob = (uint8_t*)raw_blob;
for (pkt_desc *field = desc; field->type != 0; ++field) {
cw_unpack_next(uc);
if (uc->item.type != field->type) return -1; // unexpected field
switch (field->type) {
case CWP_ITEM_POSITIVE_INTEGER: {
zpl_memcopy(blob + field->offset, (uint8_t*)uc->item.as.u64, field->size);
}break;
default: {
zpl_printf("[WARN] unsupported pkt field type %lld !\n", field->type);
return -1; // unsupported field
}break;
}
}
return 0;
}

View File

@ -1,4 +1,5 @@
#pragma once
#include "zpl.h"
#include "packet.h"
#include "cwpack/cwpack.h"
@ -29,3 +30,32 @@ inline int32_t pkt_validate_eof_msg(cw_unpack_context *uc) {
if (uc->return_code != CWP_RC_END_OF_INPUT) return -1; // not finished yet but should be?
return 0;
}
#ifndef PKT_OFFSETOF
#if defined(_MSC_VER) || defined(ZPL_COMPILER_TINYC)
# define PKT_OFFSETOF(Type, element) ((size_t) & (((Type *)0)->element))
#else
# define PKT_OFFSETOF(Type, element) __builtin_offsetof(Type, element)
#endif
# define PKT_FIELD_SIZEOF(type, member) sizeof(((type *)0)->member)
#endif
#ifndef PKT_STRUCT_PTR
#define PKT_STRUCT_PTR(a) (void*)a, (uint32_t)sizeof(*a)
#endif
#ifndef PKT_FIELD
#define PKT_FIELD(k, t, a) .type = k, .offset = PKT_OFFSETOF(t, a), .size = PKT_FIELD_SIZEOF(t,a)
#endif
#ifndef PKT_END
#define PKT_END .type = CWP_NOT_AN_ITEM
#endif
typedef struct pkt_desc {
cwpack_item_types type;
size_t offset;
size_t size;
} pkt_desc;
int32_t pkt_unpack_struct(cw_unpack_context *uc, pkt_desc *desc, void *raw_blob, uint32_t blob_size);

View File

@ -4,6 +4,12 @@
#define PKT_01_WELCOME_ARGS 2
pkt_desc pkt_01_welcome_desc[3] = {
{ PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, chunk_size) },
{ PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, chunk_amount) },
{ PKT_END },
};
size_t pkt_01_welcome_encode(pkt_01_welcome *table) {
cw_pack_context pc = {0};
pkt_pack_msg(&pc, PKT_01_WELCOME_ARGS);
@ -17,26 +23,17 @@ size_t pkt_01_welcome_encode(pkt_01_welcome *table) {
int32_t pkt_01_welcome_decode(pkt_01_welcome *table, pkt_header *header) {
cw_unpack_context uc = {0};
pkt_unpack_msg(&uc, header, PKT_01_WELCOME_ARGS);
pkt_unpack_struct(&uc, header, PKT_STRUCT_PTR(table));
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER) return -1; // expected chunk size
table->chunk_size = (uint16_t)uc.item.as.u64;
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER) return -1; // expected chunk amount
table->chunk_amount = (uint16_t)uc.item.as.u64;
return pkt_validate_eof_msg(&uc);;
return pkt_validate_eof_msg(&uc);
}
int32_t pkt_01_welcome_handler(pkt_header *header) {
#if 0
#if 1
pkt_01_welcome table;
pkt_01_welcome_decode(&table, header);
zpl_printf("we received: chunk_size: %d and chunk_amount: %d\n", table.chunk_size, table.chunk_amount);
do STUFF
#endif
return 0;
}