#pragma once

#include <Arduino.h>

struct BatchInput {
  uint16_t sender_id;
  uint16_t batch_id;
  uint32_t t_last;
  uint8_t dt_s;
  uint8_t n;
  uint16_t battery_mV;
  uint8_t err_m;
  uint8_t err_d;
  uint8_t err_tx;
  uint8_t err_last;
  uint8_t err_rx_reject;
  uint32_t energy_wh[30];
  int16_t p1_w[30];
  int16_t p2_w[30];
  int16_t p3_w[30];
};

bool encode_batch(const BatchInput &in, uint8_t *out, size_t out_cap, size_t *out_len);
bool decode_batch(const uint8_t *buf, size_t len, BatchInput *out);

size_t uleb128_encode(uint32_t v, uint8_t *out, size_t cap);
bool uleb128_decode(const uint8_t *in, size_t len, size_t *pos, uint32_t *v);

uint32_t zigzag32(int32_t x);
int32_t unzigzag32(uint32_t u);

size_t svarint_encode(int32_t x, uint8_t *out, size_t cap);
bool svarint_decode(const uint8_t *in, size_t len, size_t *pos, int32_t *x);

#ifdef PAYLOAD_CODEC_TEST
bool payload_codec_self_test();
#endif
