core/core.c (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include "core.h"
#include "cbor.h"
#include "packet.h"
#include <stdbool.h>
#define CIRCE_CORE_ASSERT(x, err) \
do \
{ \
if (!(x)) \
{ \
return err; \
} \
} \
while (false)
enum circe_result
circe_handle_packet (struct circe_ctx *ctx, const uint8_t *in, uint32_t len,
uint64_t now_ms, uint8_t *out, uint32_t *out_len,
struct circe_event *events, uint32_t *n_events)
{
struct circe_cbor_decoder cd = {
.buf_start = in,
.buf_len = len,
.cursor = in,
};
enum circe_cbor_major_type major_type;
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (&cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_MAPPING,
CIRCE_RESULT_INVALID_PACKET);
uint64_t num_fields;
CIRCE_CORE_ASSERT (circe_cbor_extract_unsigned (&cd, &num_fields),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (num_fields > 0, CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (&cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_UNSIGNED,
CIRCE_RESULT_INVALID_PACKET);
uint64_t field;
CIRCE_CORE_ASSERT (circe_cbor_extract_unsigned (&cd, &field),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (field < CIRCE_PACKET_FIELD_OVERMAX,
CIRCE_RESULT_INVALID_PACKET);
return CIRCE_RESULT_SUCCESS;
}
|