encode_oneof.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Encode a message using callbacks inside oneof fields.
  2. * For encoding, callbacks inside oneofs require nothing special
  3. * so this is just normal callback usage.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <pb_encode.h>
  8. #include "oneof.pb.h"
  9. #include "test_helpers.h"
  10. /* This is a nanopb-0.4 style global callback, that is referred by function name
  11. * and does not have to be bound separately to the message. It also allows defining
  12. * a custom data type for the field in the structure.
  13. */
  14. bool SubMsg3_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field)
  15. {
  16. if (ostream && field->tag == SubMsg3_strvalue_tag)
  17. {
  18. /* Our custom data type is char* */
  19. const char *str = *(const char**)field->pData;
  20. if (!pb_encode_tag_for_field(ostream, field))
  21. return false;
  22. return pb_encode_string(ostream, (const uint8_t*)str, strlen(str));
  23. }
  24. return true;
  25. }
  26. /* The two callbacks below are traditional callbacks that use function pointers
  27. * defined in pb_callback_t.
  28. */
  29. bool encode_int32_array(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
  30. {
  31. int i;
  32. for (i = 0; i < 15; i++)
  33. {
  34. if (!pb_encode_tag_for_field(stream, field))
  35. return false;
  36. if (!pb_encode_varint(stream, i))
  37. return false;
  38. }
  39. return true;
  40. }
  41. bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
  42. {
  43. const char *str = "mystring";
  44. if (!pb_encode_tag_for_field(stream, field))
  45. return false;
  46. return pb_encode_string(stream, (const uint8_t*)str, strlen(str));
  47. }
  48. int main(int argc, char **argv)
  49. {
  50. uint8_t buffer[256];
  51. OneOfMessage msg = OneOfMessage_init_zero;
  52. pb_ostream_t stream;
  53. int option;
  54. if (argc != 2)
  55. {
  56. fprintf(stderr, "Usage: encode_oneof [number]\n");
  57. return 1;
  58. }
  59. option = atoi(argv[1]);
  60. /* Prefix and suffix are used to test that the union does not disturb
  61. * other fields in the same message. */
  62. msg.prefix = 123;
  63. /* We encode one of the 'values' fields based on command line argument */
  64. if (option == 1)
  65. {
  66. msg.which_values = OneOfMessage_intvalue_tag;
  67. msg.values.intvalue = 999;
  68. }
  69. else if (option == 2)
  70. {
  71. msg.which_values = OneOfMessage_strvalue_tag;
  72. strcpy(msg.values.strvalue, "abcd");
  73. }
  74. else if (option == 3)
  75. {
  76. msg.which_values = OneOfMessage_submsg1_tag;
  77. msg.values.submsg1.array.funcs.encode = encode_int32_array;
  78. }
  79. else if (option == 4)
  80. {
  81. msg.which_values = OneOfMessage_submsg2_tag;
  82. msg.values.submsg2.strvalue.funcs.encode = encode_string;
  83. }
  84. else if (option == 5)
  85. {
  86. msg.which_values = OneOfMessage_submsg3_tag;
  87. msg.values.submsg3.which_values = SubMsg3_intvalue_tag;
  88. msg.values.submsg3.values.intvalue = 1234;
  89. }
  90. else if (option == 6)
  91. {
  92. msg.which_values = OneOfMessage_submsg3_tag;
  93. msg.values.submsg3.which_values = SubMsg3_strvalue_tag;
  94. msg.values.submsg3.values.strvalue = "efgh";
  95. }
  96. msg.suffix = 321;
  97. stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
  98. if (pb_encode(&stream, OneOfMessage_fields, &msg))
  99. {
  100. SET_BINARY_MODE(stdout);
  101. fwrite(buffer, 1, stream.bytes_written, stdout);
  102. return 0;
  103. }
  104. else
  105. {
  106. fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
  107. return 1;
  108. }
  109. }