2020-04-18 20:07:23 -07:00
|
|
|
#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;
|
2020-05-12 22:37:12 -07:00
|
|
|
collar.oob = 1;
|
2020-04-18 20:07:23 -07:00
|
|
|
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);
|
2020-05-12 22:37:12 -07:00
|
|
|
assert(collar0.oob == collar.oob);
|
2020-04-18 20:07:23 -07:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
return 0;
|
|
|
|
}
|