Skip to content

Commit

Permalink
Merge branch 'protocol_changes' into protocol_cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Mallets committed Mar 19, 2024
2 parents 557e665 + 8e8c678 commit 5bca588
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 205 deletions.
12 changes: 3 additions & 9 deletions include/zenoh-pico/protocol/codec/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,19 @@ int8_t _z_query_target_decode(z_query_target_t *en, _z_zbuf_t *zbf);
int8_t _z_whatami_encode(_z_wbuf_t *wbf, z_whatami_t en);
int8_t _z_whatami_decode(z_whatami_t *en, _z_zbuf_t *zbf);

int8_t _z_uint_encode(_z_wbuf_t *buf, unsigned int v);
int8_t _z_uint_decode(unsigned int *u, _z_zbuf_t *buf);

int8_t _z_uint8_encode(_z_wbuf_t *buf, uint8_t v);
int8_t _z_uint8_decode(uint8_t *u8, _z_zbuf_t *buf);

int8_t _z_uint16_encode(_z_wbuf_t *buf, uint16_t v);
int8_t _z_uint16_decode(uint16_t *u16, _z_zbuf_t *buf);

int8_t _z_uint64_encode(_z_wbuf_t *buf, uint64_t v);
int8_t _z_uint64_decode(uint64_t *u64, _z_zbuf_t *buf);

uint8_t _z_zint_len(_z_zint_t v);
int8_t _z_zint_encode(_z_wbuf_t *buf, _z_zint_t v);
uint8_t _z_zint_len(uint64_t v);
int8_t _z_zsize_encode(_z_wbuf_t *buf, _z_zint_t v);
int8_t _z_zint64_encode(_z_wbuf_t *buf, uint64_t v);
int8_t _z_zint16_decode(uint16_t *zint, _z_zbuf_t *buf);
int8_t _z_zint32_decode(uint32_t *zint, _z_zbuf_t *buf);
int8_t _z_zint64_decode(uint64_t *zint, _z_zbuf_t *buf);
int8_t _z_zint_decode(_z_zint_t *zint, _z_zbuf_t *buf);
int8_t _z_zsize_decode(_z_zint_t *zint, _z_zbuf_t *buf);

int8_t _z_bytes_val_encode(_z_wbuf_t *buf, const _z_bytes_t *bs);
int8_t _z_bytes_val_decode(_z_bytes_t *bs, _z_zbuf_t *buf);
Expand Down
203 changes: 75 additions & 128 deletions src/protocol/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,85 +21,54 @@
#include "zenoh-pico/utils/result.h"

/*------------------ uint8 -------------------*/
int8_t _z_encoding_prefix_encode(_z_wbuf_t *wbf, z_encoding_prefix_t en) { return _z_zint_encode(wbf, en); }
int8_t _z_encoding_prefix_encode(_z_wbuf_t *wbf, z_encoding_prefix_t en) { return _z_zsize_encode(wbf, en); }

int8_t _z_encoding_prefix_decode(z_encoding_prefix_t *en, _z_zbuf_t *zbf) {
int8_t ret = _Z_RES_OK;

_z_zint_t tmp;
ret |= _z_zint_decode(&tmp, zbf);
ret |= _z_zsize_decode(&tmp, zbf);
*en = tmp;

return ret;
}

int8_t _z_consolidation_mode_encode(_z_wbuf_t *wbf, z_consolidation_mode_t en) { return _z_zint_encode(wbf, en); }
int8_t _z_consolidation_mode_encode(_z_wbuf_t *wbf, z_consolidation_mode_t en) { return _z_zsize_encode(wbf, en); }

int8_t _z_consolidation_mode_decode(z_consolidation_mode_t *en, _z_zbuf_t *zbf) {
int8_t ret = _Z_RES_OK;

_z_zint_t tmp;
ret |= _z_zint_decode(&tmp, zbf);
ret |= _z_zsize_decode(&tmp, zbf);
*en = tmp;

return ret;
}

int8_t _z_query_target_encode(_z_wbuf_t *wbf, z_query_target_t en) { return _z_zint_encode(wbf, en); }
int8_t _z_query_target_encode(_z_wbuf_t *wbf, z_query_target_t en) { return _z_zsize_encode(wbf, en); }

int8_t _z_query_target_decode(z_query_target_t *en, _z_zbuf_t *zbf) {
int8_t ret = _Z_RES_OK;

_z_zint_t tmp;
ret |= _z_zint_decode(&tmp, zbf);
ret |= _z_zsize_decode(&tmp, zbf);
*en = tmp;

return ret;
}

int8_t _z_whatami_encode(_z_wbuf_t *wbf, z_whatami_t en) { return _z_zint_encode(wbf, en); }
int8_t _z_whatami_encode(_z_wbuf_t *wbf, z_whatami_t en) { return _z_zsize_encode(wbf, en); }

int8_t _z_whatami_decode(z_whatami_t *en, _z_zbuf_t *zbf) {
int8_t ret = _Z_RES_OK;

_z_zint_t tmp;
ret |= _z_zint_decode(&tmp, zbf);
ret |= _z_zsize_decode(&tmp, zbf);
*en = tmp;

return ret;
}

int8_t _z_uint_encode(_z_wbuf_t *wbf, unsigned int uint) {
unsigned int lv = uint;

while (lv > (unsigned int)0x7f) {
uint8_t c = (uint8_t)(lv & (unsigned int)0x7f) | (uint8_t)0x80;
_Z_RETURN_IF_ERR(_z_wbuf_write(wbf, c))
lv = lv >> (unsigned int)7;
}

uint8_t c = (uint8_t)(lv & (unsigned int)0xff);
return _z_wbuf_write(wbf, c);
}

int8_t _z_uint_decode(unsigned int *uint, _z_zbuf_t *zbf) {
int8_t ret = _Z_RES_OK;
*uint = 0;

uint8_t i = 0;
uint8_t u8 = 0;
do {
if (_z_uint8_decode(&u8, zbf) == _Z_RES_OK) {
*uint = *uint | (((unsigned int)u8 & (unsigned int)0x7f) << (unsigned int)i);
i = i + (uint8_t)7;
} else {
ret |= _Z_ERR_MESSAGE_DESERIALIZATION_FAILED;
}
} while (u8 > (uint8_t)0x7f);

return ret;
}

int8_t _z_uint8_encode(_z_wbuf_t *wbf, uint8_t u8) { return _z_wbuf_write(wbf, u8); }

int8_t _z_uint8_decode(uint8_t *u8, _z_zbuf_t *zbf) {
Expand Down Expand Up @@ -138,124 +107,102 @@ int8_t _z_uint16_decode(uint16_t *u16, _z_zbuf_t *zbf) {
return ret;
}

int8_t _z_uint64_encode(_z_wbuf_t *wbf, uint64_t u64) {
uint64_t lv = u64;

while (lv > (uint64_t)0x7f) {
uint8_t c = (uint8_t)(lv & (uint64_t)0x7f) | (uint8_t)0x80;
_Z_RETURN_IF_ERR(_z_wbuf_write(wbf, c))
lv = lv >> (_z_zint_t)7;
}

uint8_t c = lv & (uint64_t)0xff;
return _z_wbuf_write(wbf, c);
}

int8_t _z_uint64_decode(uint64_t *u64, _z_zbuf_t *zbf) {
int8_t ret = _Z_RES_OK;
*u64 = 0;

uint8_t i = 0;
uint8_t u8 = 0;
do {
if (_z_uint8_decode(&u8, zbf) == _Z_RES_OK) {
*u64 = *u64 | (((_z_zint_t)u8 & 0x7f) << i);
i = i + (uint8_t)7;
} else {
ret |= _Z_ERR_MESSAGE_DESERIALIZATION_FAILED;
}
} while (u8 > (uint8_t)0x7f);

return ret;
}

/*------------------ z_zint ------------------*/
uint8_t _z_zint_len(_z_zint_t v) {
uint8_t len = 1;
while (v > 0x7f) {
v >>= 7;
len++;
#define VLE_LEN 9

uint8_t _z_zint_len(uint64_t v) {
if ((v & (UINT64_MAX << (7 * 1))) == 0) {
return 1;
} else if ((v & (UINT64_MAX << (7 * 2))) == 0) {
return 2;
} else if ((v & (UINT64_MAX << (7 * 3))) == 0) {
return 3;
} else if ((v & (UINT64_MAX << (7 * 4))) == 0) {
return 4;
} else if ((v & (UINT64_MAX << (7 * 5))) == 0) {
return 5;
} else if ((v & (UINT64_MAX << (7 * 6))) == 0) {
return 6;
} else if ((v & (UINT64_MAX << (7 * 7))) == 0) {
return 7;
} else if ((v & (UINT64_MAX << (7 * 8))) == 0) {
return 8;
} else {
return 9;
}
return len;
}
int8_t _z_zint_encode(_z_wbuf_t *wbf, _z_zint_t v) {
_z_zint_t lv = v;

while (lv > 0x7f) {
int8_t _z_zint64_encode(_z_wbuf_t *wbf, uint64_t v) {
uint64_t lv = v;
uint8_t len = 0;
while ((lv & (uint64_t)~0x7f) != 0) {
uint8_t c = (lv & 0x7f) | 0x80;
_Z_RETURN_IF_ERR(_z_wbuf_write(wbf, c))
lv = lv >> (_z_zint_t)7;
len++;
lv = lv >> (uint64_t)7;
}
if (len != VLE_LEN) {
uint8_t c = (lv & 0xff);
_Z_RETURN_IF_ERR(_z_wbuf_write(wbf, c))
}

uint8_t c = lv & 0xff;
return _z_wbuf_write(wbf, c);
return _Z_RES_OK;
}
int8_t _z_zint64_encode(_z_wbuf_t *wbf, uint64_t v) {
uint64_t lv = v;

while (lv > 0x7f) {
uint8_t c = (lv & 0x7f) | 0x80;
_Z_RETURN_IF_ERR(_z_wbuf_write(wbf, c))
lv = lv >> (uint64_t)7;
int8_t _z_zint16_encode(_z_wbuf_t *wbf, uint16_t v) { return _z_zint64_encode(wbf, (uint64_t)v); }
int8_t _z_zint32_encode(_z_wbuf_t *wbf, uint32_t v) { return _z_zint64_encode(wbf, (uint64_t)v); }
int8_t _z_zsize_encode(_z_wbuf_t *wbf, _z_zint_t v) { return _z_zint64_encode(wbf, (uint64_t)v); }

int8_t _z_zint64_decode(uint64_t *zint, _z_zbuf_t *zbf) {
*zint = 0;

uint8_t b = 0;
_Z_RETURN_IF_ERR(_z_uint8_decode(&b, zbf));

uint8_t i = 0;
while (((b & 0x80) != 0) && (i != 7 * (VLE_LEN - 1))) {
*zint = *zint | ((uint64_t)(b & 0x7f)) << i;
_Z_RETURN_IF_ERR(_z_uint8_decode(&b, zbf));
i = i + (uint8_t)7;
}
*zint = *zint | ((uint64_t)b << i);

uint8_t c = lv & 0xff;
return _z_wbuf_write(wbf, c);
return _Z_RES_OK;
}

int8_t _z_zint16_decode(uint16_t *zint, _z_zbuf_t *zbf) {
int8_t ret = _Z_RES_OK;
_z_zint_t buf;
_Z_RETURN_IF_ERR(_z_zint_decode(&buf, zbf));
uint64_t buf;
_Z_RETURN_IF_ERR(_z_zint64_decode(&buf, zbf));
if (buf <= UINT16_MAX) {
*zint = (uint16_t)buf;
} else {
ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED;
}
return ret;
}

int8_t _z_zint32_decode(uint32_t *zint, _z_zbuf_t *zbf) {
int8_t ret = _Z_RES_OK;
_z_zint_t buf;
_Z_RETURN_IF_ERR(_z_zint_decode(&buf, zbf));
uint64_t buf;
_Z_RETURN_IF_ERR(_z_zint64_decode(&buf, zbf));
if (buf <= UINT32_MAX) {
*zint = (uint32_t)buf;
} else {
ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED;
}
return ret;
}
int8_t _z_zint_decode(_z_zint_t *zint, _z_zbuf_t *zbf) {
int8_t ret = _Z_RES_OK;
*zint = 0;

uint8_t i = 0;
uint8_t u8 = 0;
do {
if (_z_uint8_decode(&u8, zbf) == _Z_RES_OK) {
*zint = *zint | (((_z_zint_t)u8 & 0x7f) << i);
i = i + (uint8_t)7;
} else {
ret |= _Z_ERR_MESSAGE_DESERIALIZATION_FAILED;
}
} while (u8 > (uint8_t)0x7f);

return ret;
}
int8_t _z_zint64_decode(uint64_t *zint, _z_zbuf_t *zbf) {
int8_t _z_zsize_decode(_z_zint_t *zint, _z_zbuf_t *zbf) {
int8_t ret = _Z_RES_OK;
*zint = 0;

uint8_t i = 0;
uint8_t u8 = 0;
do {
if (_z_uint8_decode(&u8, zbf) == _Z_RES_OK) {
*zint = *zint | (((uint64_t)u8 & 0x7f) << i);
i = i + (uint8_t)7;
} else {
ret |= _Z_ERR_MESSAGE_DESERIALIZATION_FAILED;
}
} while (u8 > (uint8_t)0x7f);

uint64_t buf;
_Z_RETURN_IF_ERR(_z_zint64_decode(&buf, zbf));
if (buf <= SIZE_MAX) {
*zint = (_z_zint_t)buf;
} else {
ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED;
}
return ret;
}

Expand All @@ -275,7 +222,7 @@ int8_t _z_bytes_val_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) {
int8_t _z_bytes_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) {
int8_t ret = _Z_RES_OK;

_Z_RETURN_IF_ERR(_z_zint_encode(wbf, bs->len))
_Z_RETURN_IF_ERR(_z_zsize_encode(wbf, bs->len))
_Z_RETURN_IF_ERR(_z_bytes_val_encode(wbf, bs))

return ret;
Expand Down Expand Up @@ -306,7 +253,7 @@ int8_t _z_bytes_val_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf) {
int8_t _z_bytes_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf) {
int8_t ret = _Z_RES_OK;

ret |= _z_zint_decode(&bs->len, zbf);
ret |= _z_zsize_decode(&bs->len, zbf);
ret |= _z_bytes_val_decode_na(bs, zbf);

return ret;
Expand All @@ -319,7 +266,7 @@ int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *zbf) { return _z_bytes_decode_
/*------------------ string with null terminator ------------------*/
int8_t _z_str_encode(_z_wbuf_t *wbf, const char *s) {
size_t len = strlen(s);
_Z_RETURN_IF_ERR(_z_zint_encode(wbf, len))
_Z_RETURN_IF_ERR(_z_zsize_encode(wbf, len))
// Note that this does not put the string terminator on the wire.
return _z_wbuf_write_bytes(wbf, (const uint8_t *)s, 0, len);
}
Expand All @@ -328,7 +275,7 @@ int8_t _z_str_decode(char **str, _z_zbuf_t *zbf) {
int8_t ret = _Z_RES_OK;

_z_zint_t len = 0;
ret |= _z_zint_decode(&len, zbf);
ret |= _z_zsize_decode(&len, zbf);
if (ret == _Z_RES_OK) {
if (_z_zbuf_len(zbf) >= len) { // Check if we have enough bytes to read
char *tmp = (char *)zp_malloc(len + (size_t)1); // Allocate space for the string terminator
Expand Down
Loading

0 comments on commit 5bca588

Please sign in to comment.