#include <Message.pb.h>#include <pb.h>#include <pb_encode.h>#include <pb_decode.h>
int test_nanopb(void)
{
/* This isthe buffer where we will store our message. */
uint8_t buffer[128];
size_t message_length;
bool status;
uint8_t i;
/* Encode our message */
{
/* Allocate spaceonthe stack to store the message data.
*
* Nanopb generates simple struct definitions for all the messages.
* - check out thecontentsof simple.pb.h!
* It is a good idea to always initialize your structures
* so that you do not have garbage data from RAM in there.
*/
Loginrequst login= Loginrequst_init_zero;
memset(buffer, 0, 128);
/* Create a stream that will writeto our buffer. */
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
strcpy((char *)&login.username, "devin");
strcpy((char *)&login.password,"123456");
/* Now we are ready to encode the message! */
status = pb_encode(&stream, Loginrequst_fields, &login);
message_length = stream.bytes_written;
/* Then just check for any errors.. */
if (!status)
{
printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
return1;
}
}
/* Now we could transmit the message over network, store itin a fileor
* wrap itto a pigeon's leg.
*/
/* But because we are lazy, we will just decode it immediately. */
{
/* Allocate spaceforthe decoded message. */
Loginrequst login = Loginrequst_init_zero;
/* Create a stream that reads fromthe buffer. */
pb_istream_t stream = pb_istream_from_buffer(buffer, message_length);
/* Now we are ready to decode the message. */
status = pb_decode(&stream, Loginrequst_fields, &login);
/* Check for errors... */
if (!status)
{
printf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
return -1;
}
/* Print the data contained inthe message. */
printf("username was [%s].\n", login.username);
printf("password was [%s].\n", login.password);
}
return0;
}
int main(int argc,char *argv[])
{
test_nanopb();
return0;
}