198 lines
6.1 KiB
C++
198 lines
6.1 KiB
C++
#include <iostream>
|
|
#include <cassert>
|
|
|
|
#include "pb_common.h"
|
|
#include "pb.h"
|
|
#include "pb_encode.h"
|
|
#include "pb_decode.h"
|
|
|
|
#include "gateway/message.pb.h"
|
|
|
|
/*
|
|
* Try to get repeating element to work with nanopb
|
|
* for coordinates
|
|
*/
|
|
using namespace std;
|
|
void print_buffer(uint8_t *buffer, int size) {
|
|
for(int i=0;i<size;i++) {
|
|
printf("%02x ", buffer[i]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
int main() {
|
|
uint8_t buffer[1000] = {0};
|
|
pb_ostream_t stream;
|
|
pb_istream_t stream0;
|
|
int err;
|
|
|
|
// encode a coordinate
|
|
Fenceless_Coordinate coord = Fenceless_Coordinate_init_zero;
|
|
coord.x = 789;
|
|
coord.y = 123;
|
|
stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
|
|
|
err = pb_encode(&stream, Fenceless_Coordinate_fields, &coord);
|
|
assert(err);
|
|
print_buffer(buffer, stream.bytes_written);
|
|
|
|
// decode a coordinate from buffer
|
|
Fenceless_Coordinate coord0 = Fenceless_Coordinate_init_zero;
|
|
stream0 = pb_istream_from_buffer(buffer, stream.bytes_written);
|
|
err = pb_decode(&stream0, Fenceless_Coordinate_fields, &coord0);
|
|
assert(err);
|
|
|
|
// check result
|
|
assert(coord0.x == coord.x);
|
|
assert(coord0.y == coord.y);
|
|
|
|
// encode a coller response
|
|
Fenceless_CollarResponse collar = Fenceless_CollarResponse_init_zero;
|
|
collar.loc.x = 200;
|
|
collar.loc.y = 100;
|
|
collar.oob = 1;
|
|
stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
|
|
|
err = pb_encode(&stream, Fenceless_CollarResponse_fields, &collar);
|
|
assert(err);
|
|
print_buffer(buffer, stream.bytes_written);
|
|
|
|
Fenceless_CollarResponse collar0 = Fenceless_CollarResponse_init_zero;
|
|
stream0 = pb_istream_from_buffer(buffer, stream.bytes_written);
|
|
err = pb_decode(&stream0, Fenceless_CollarResponse_fields, &collar0);
|
|
assert(err);
|
|
|
|
// check result
|
|
assert(collar0.loc.x == collar.loc.x);
|
|
assert(collar0.loc.y == collar.loc.y);
|
|
assert(collar0.oob == collar.oob);
|
|
|
|
// encode 10 coordinates
|
|
Fenceless_Coordinates coords;
|
|
coords.coord0.x = 1; coords.coord0.y = 2;
|
|
coords.coord1.x = 1; coords.coord1.y = 2;
|
|
coords.coord2.x = 1; coords.coord2.y = 2;
|
|
coords.coord3.x = 1; coords.coord3.y = 2;
|
|
coords.coord4.x = 1; coords.coord4.y = 2;
|
|
coords.coord5.x = 1; coords.coord5.y = 2;
|
|
coords.coord6.x = 1; coords.coord6.y = 2;
|
|
coords.coord7.x = 1; coords.coord7.y = 2;
|
|
coords.coord8.x = 1; coords.coord8.y = 2;
|
|
coords.coord9.x = 1; coords.coord9.y = 2;
|
|
|
|
coords.isr = 10;
|
|
|
|
stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
|
err = pb_encode(&stream, Fenceless_Coordinates_fields, &coords);
|
|
assert(err);
|
|
|
|
print_buffer(buffer, stream.bytes_written);
|
|
|
|
// encode multiple coordinates
|
|
//Fenceless_Coordinates coords = Fenceless_Coordinates_init_zero;
|
|
//coords.loc.funcs.encode = [](pb_ostream_t *stream, const pb_field_t *field, void * const * arg){
|
|
// cout << "encode called" << endl;
|
|
// int err;
|
|
// err = pb_encode_tag_for_field(stream , field);
|
|
// assert(err);
|
|
|
|
// Fenceless_Coordinate coord = Fenceless_Coordinate_init_zero;
|
|
// coord.x = 123;
|
|
// coord.y = 456;
|
|
// err = pb_encode_submessage(stream, Fenceless_Coordinate_fields, &coord);
|
|
// assert(err);
|
|
|
|
|
|
// err = pb_encode_tag_for_field(stream , field);
|
|
// assert(err);
|
|
|
|
// coord = Fenceless_Coordinate_init_zero;
|
|
// coord.x = 789;
|
|
// coord.y = 987;
|
|
// err = pb_encode_submessage(stream, Fenceless_Coordinate_fields, &coord);
|
|
// assert(err);
|
|
|
|
// return true;
|
|
//};
|
|
//memset(buffer, 0, sizeof(buffer)-1);
|
|
//stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
|
|
|
//err = pb_encode(&stream, Fenceless_Coordinates_fields, &coords);
|
|
//assert(err);
|
|
|
|
//print_buffer(buffer, stream.bytes_written);
|
|
|
|
//Fenceless_Coordinates coords0 = Fenceless_Coordinates_init_zero;
|
|
//coords0.loc.funcs.decode = [](pb_istream_t *stream, const pb_field_iter_t *field, void ** arg){
|
|
// cout << "decode called" << endl;
|
|
// printf("Array size: %d\n", field->array_size);
|
|
// bool eof;
|
|
// uint32_t tag;
|
|
// pb_wire_type_t t = PB_WT_STRING;
|
|
// pb_decode_tag(stream, &t, &tag, &eof);
|
|
// Fenceless_Coordinate coord;
|
|
// int err = pb_decode(stream, Fenceless_Coordinate_fields, &coord);
|
|
// assert(err);
|
|
// printf("%d %d", coord.x,coord.y);
|
|
|
|
// return false;
|
|
//};
|
|
//stream0 = pb_istream_from_buffer(buffer, stream.bytes_written);
|
|
//err = pb_decode(&stream0, Fenceless_Coordinates_fields, &coords0);
|
|
//assert(err);
|
|
|
|
|
|
/* Reference Protobuff data
|
|
#define TYPE_STRING 0x0A
|
|
#define TYPE_VARIANT 0x10
|
|
#define PROTO_LEN 0x0A
|
|
#define FIELD_ONE_FLOAT 0x0D
|
|
#define FIELD_TWO_FLOAT 0x15
|
|
#define FIELD_TWO_VARIANT 0x10
|
|
#define FIELD_ONE_VARIANT 0x08
|
|
#define FIELD_TWO_STRING 0x12
|
|
#define FIELD_THREE_STRING 0x1A
|
|
#define FIELD_FOUR_STRING 0x22
|
|
#define FIELD_FIVE_STRING 0x2A
|
|
#define FIELD_SIX_STRING 0x32
|
|
#define FIELD_SEVEN_STRING 0x3A
|
|
#define FIELD_EIGHT_STRING 0x42
|
|
#define FIELD_NINE_STRING 0x4A
|
|
#define FIELD_TEN_STRING 0x52
|
|
#define FIELD_ELEVEN_STRING 0x5A
|
|
uint8_t buffer0[] {
|
|
FIELD_ONE_VARIANT, 0x01,
|
|
FIELD_TWO_STRING, 0x0A,
|
|
FIELD_ONE_FLOAT, 0x1B, 0x91, 0xF6, 0xC2,
|
|
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
|
|
FIELD_THREE_STRING, 0x0A,
|
|
FIELD_ONE_FLOAT, 0xB5, 0x3B, 0x32, 0x42,
|
|
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
|
|
FIELD_FOUR_STRING, 0x0A,
|
|
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
|
|
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
|
|
FIELD_FIVE_STRING, 0x0A,
|
|
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
|
|
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
|
|
FIELD_SIX_STRING, 0x0A,
|
|
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
|
|
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
|
|
FIELD_SEVEN_STRING, 0x0A,
|
|
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
|
|
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
|
|
FIELD_EIGHT_STRING, 0x0A,
|
|
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
|
|
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
|
|
FIELD_NINE_STRING, 0x0A,
|
|
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
|
|
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
|
|
FIELD_TEN_STRING, 0x0A,
|
|
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
|
|
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
|
|
FIELD_ELEVEN_STRING, 0x0A,
|
|
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
|
|
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40
|
|
};*/
|
|
|
|
return 0;
|
|
}
|