LCOV - code coverage report
Current view: top level - msgid - encode_msgid.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 33 37 89.2 %
Date: 2023-02-14 20:10:26 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /* Encode a message using msgid field as prefix */
       2             : 
       3             : #include <stdio.h>
       4             : #include <stdlib.h>
       5             : #include <string.h>
       6             : #include <pb_encode.h>
       7             : #include "msgid_example.pb.h"
       8             : #include "test_helpers.h"
       9             : 
      10             : /* This function writes the message id as a prefix to the message, allowing
      11             :  * the receiving side to identify message type. Here we use uint8_t to store
      12             :  * it, but e.g. varint or some custom header struct would work just as well.
      13             :  */
      14           3 : bool write_prefix(pb_ostream_t *stream, int msgid)
      15             : {
      16           3 :     uint8_t prefix = msgid;
      17           3 :     return pb_write(stream, &prefix, 1);
      18             : }
      19             : 
      20             : /* The main logic will call one of these functions.
      21             :  * Normally which function you call would be selected based on what message
      22             :  * you want to send, here it is decided based on command line parameter.
      23             :  */
      24             : 
      25           1 : bool encode_MyMessage1(pb_ostream_t *stream)
      26             : {
      27           1 :     MyMessage1 msg = MyMessage1_init_default;
      28           1 :     msg.intvalue = 1234;
      29           1 :     return write_prefix(stream, MyMessage1_msgid)
      30           1 :            && pb_encode(stream, MyMessage1_fields, &msg);
      31             : }
      32             : 
      33           1 : bool encode_MyMessage2(pb_ostream_t *stream)
      34             : {
      35           1 :     MyMessage2 msg = MyMessage2_init_default;
      36           1 :     msg.intvalue = 9999;
      37           1 :     strcpy(msg.strvalue, "Msg2");
      38           1 :     return write_prefix(stream, MyMessage2_msgid)
      39           1 :            && pb_encode(stream, MyMessage2_fields, &msg);
      40             : }
      41             : 
      42           1 : bool encode_MyMessage3(pb_ostream_t *stream)
      43             : {
      44           1 :     MyMessage3 msg = MyMessage3_init_default;
      45           1 :     msg.boolvalue = true;
      46           1 :     return write_prefix(stream, MyMessage3_msgid)
      47           1 :            && pb_encode(stream, MyMessage3_fields, &msg);
      48             : }
      49             : 
      50           3 : int main(int argc, char **argv)
      51             : {
      52             :     uint8_t buffer[128];
      53             :     pb_ostream_t stream;
      54           3 :     bool status = false;
      55             :     int option;
      56             : 
      57           3 :     if (argc != 2)
      58             :     {
      59           0 :         fprintf(stderr, "Usage: encode_msgid [number]\n");
      60           0 :         return 1;
      61             :     }
      62           3 :     option = atoi(argv[1]);
      63             : 
      64           3 :     stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
      65             : 
      66           3 :     if (option == 1)
      67             :     {
      68           1 :         status = encode_MyMessage1(&stream);
      69             :     }
      70           2 :     else if (option == 2)
      71             :     {
      72           1 :         status = encode_MyMessage2(&stream);
      73             :     }
      74           1 :     else if (option == 3)
      75             :     {
      76           1 :         status = encode_MyMessage3(&stream);
      77             :     }
      78             : 
      79           3 :     if (status)
      80             :     {
      81             :         SET_BINARY_MODE(stdout);
      82           3 :         fwrite(buffer, 1, stream.bytes_written, stdout);
      83           3 :         return 0;
      84             :     }
      85             :     else
      86             :     {
      87           0 :         fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
      88           0 :         return 1;
      89             :     }
      90             : }

Generated by: LCOV version 1.14