You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
3.3 KiB
126 lines
3.3 KiB
/*
|
|
* bsp_pb_decode_encode.c
|
|
*
|
|
* Created on: Jul 5, 2024
|
|
* Author: akeguo
|
|
*/
|
|
#include "bsp_pb_decode_encode.h"
|
|
#include "bsp_CV.pb.h"
|
|
#include "bsp_IAP.pb.h"
|
|
|
|
|
|
bool pb_encode_string_cb(pb_ostream_t *stream, const pb_field_t *field,
|
|
void *const*arg)
|
|
{
|
|
pb_buffer_arg *data = (pb_buffer_arg*) *arg;
|
|
if (pb_encode_tag_for_field(stream, field)
|
|
&& pb_encode_string(stream, data->buf, data->buf_len))
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
bool pb_decode_string_cb(pb_istream_t *stream, const pb_field_t *field,
|
|
void **arg)
|
|
{
|
|
pb_buffer_arg *data = (pb_buffer_arg*) *arg;
|
|
|
|
data->buf_len = stream->bytes_left;
|
|
data->buf = stream->state;
|
|
return pb_read(stream, (pb_byte_t*) NULL, stream->bytes_left);
|
|
}
|
|
void Test_CV()
|
|
{
|
|
uint8_t buf[64];
|
|
size_t len;
|
|
CV_struct_define o = CV_struct_define_init_default; // CV_struct_define_init_default this defination can only be used when initialize the struct;
|
|
CV_struct_define n = CV_struct_define_init_default;
|
|
|
|
pb_ostream_t o_stream =
|
|
{ 0 };
|
|
pb_istream_t i_stream =
|
|
{ 0 };
|
|
|
|
//strcat(o.Date_Time, "MikeJordan");
|
|
// o.Speed[0] = 11;
|
|
// o.Speed[1] = 22;
|
|
// o.Speed[2] = 33;
|
|
// encode
|
|
// char const date_string[] = "2024-7-5 10:19 pm";
|
|
// o.Date_Time.funcs.encode = pb_encode_string_cb;
|
|
// o.Date_Time.arg = &((pb_buffer_arg )
|
|
// { .buf = date_string, .buf_len = sizeof(date_string) } );
|
|
//
|
|
// char const id_string[] = "Robot-01-03-08";
|
|
// o.Robot_ID.funcs.encode = pb_encode_string_cb;
|
|
// o.Robot_ID.arg = &((pb_buffer_arg )
|
|
// { .buf = id_string, .buf_len = sizeof(id_string) } );
|
|
|
|
o_stream = pb_ostream_from_buffer(buf, sizeof(buf));
|
|
pb_encode(&o_stream, CV_struct_define_fields, &o);
|
|
|
|
len = o_stream.bytes_written; // send the [o_stream.bytes_written] bytes, startting from buf
|
|
|
|
// decode example
|
|
|
|
// i_stream = pb_istream_from_buffer(buf, len);
|
|
// //decode
|
|
// pb_buffer_arg decode_datetime_string = { .buf = NULL, .buf_len = 0 };
|
|
// n.Date_Time.arg = &decode_datetime_string;
|
|
// n.Date_Time.funcs.decode = pb_decode_string_cb;
|
|
//
|
|
// pb_buffer_arg decode_id_string = { .buf = NULL, .buf_len = 0 };
|
|
// n.Robot_ID.arg = &decode_id_string;
|
|
// n.Robot_ID.funcs.decode = pb_decode_string_cb;
|
|
|
|
pb_decode(&i_stream, CV_struct_define_fields, &n);
|
|
|
|
char decodestring[100];
|
|
//strncpy(decodestring, decode_datetime_string.buf,
|
|
// decode_datetime_string.buf_len);
|
|
|
|
decodestring[99] = 'a';
|
|
// printf("decode_string:%s , buf_len:%zu\n",decode_datetime_string.buf, decode_datetime_string.buf_len);
|
|
|
|
}
|
|
|
|
CV_struct_define pb_decode_CV(char *buf, size_t length)
|
|
{
|
|
CV_struct_define decoded_CV = CV_struct_define_init_default;
|
|
pb_istream_t i_stream =
|
|
{ 0 };
|
|
|
|
i_stream = pb_istream_from_buffer(buf, length);
|
|
pb_decode(&i_stream, CV_struct_define_fields, &decoded_CV);
|
|
return decoded_CV;
|
|
}
|
|
|
|
IAP_struct_define pb_decode_IAP(uint8_t *buf, size_t length)
|
|
{
|
|
|
|
|
|
IAP_struct_define decoded_IAP =IAP_struct_define_init_default;
|
|
pb_istream_t i_stream =
|
|
{ 0 };
|
|
|
|
i_stream = pb_istream_from_buffer(buf, length);
|
|
pb_decode(&i_stream, IAP_struct_define_fields, &decoded_IAP);
|
|
|
|
|
|
|
|
return decoded_IAP;
|
|
}
|
|
|
|
GV_struct_define* pb_decode_GV(char *buf, size_t length)
|
|
{
|
|
GV_struct_define decoded_struct = GV_struct_define_init_default;
|
|
pb_istream_t i_stream =
|
|
{ 0 };
|
|
|
|
i_stream = pb_istream_from_buffer(buf, length);
|
|
|
|
pb_decode(&i_stream, GV_struct_define_fields, &decoded_struct);
|
|
return &decoded_struct;
|
|
}
|
|
|
|
|
|
|