1

I have various sensor types running, creating periodic events and publishing the data online. There's an overall event structure containing information about the sensor and a submessage containing data depending on what type of sensor it is. The event structure looks like this:

typedef struct _Event {
    PayloadType type;
    pb_callback_t address;
    pb_size_t which_payload;
    union {
        UnknownEvent unknown_event;
        Sensor1Event sensor1_event;
    } payload;
} Event;

And the specific Sensor1Event for sensor nr 1 structure:

typedef struct _Sensor1Event {
    uint64_t device_id;
    uint64_t time;
    Sensor1EventType type;
    uint16_t lux;
    uint16_t co2;
    uint16_t voc;
    uint8_t mic;
    uint8_t temp;
    uint8_t humidity;
    uint16_t in_count;
    uint16_t out_count;
    int16_t aggregated_count;
} Sensor1Event;

I created an encode pb_callback_t callback function that aims to encode the submessage.

Here's my approach:

Event SEvent = Event_init_zero;
Sensor1Event s1evt = {0,0,0, 0, 0,0,temp_processed, 0,0,0,0}; //fill with dummy vals for now
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));

... ...

SEvent.address = adr; //predetermined address assigned here
SEvent.type = PayloadType_Sensor1; // predefined value here
SEvent.sensor1_event.arg = &s1evt;
SEvent.sensor1_event.funcs.encode = &encode_sensor_data;
pb_encode(&stream, Event_fields, &SEvent);

The encode_sensor_data function:

bool encode_data(pb_ostream_t *stream, const pb_field_iter_t *field, void * const *arg)
{
  
    Sensor1Event * evt = (Sensor1Event *)(*arg);
 
  

    if (!pb_encode_tag_for_field(stream, field))
        return false;

    
    return pb_encode_submessage(stream, Sensor1Event_fields, &evt);
   
    
}

Once the data gets sent and decoded at the receiver, the submessage data is garbage. The encoding of the type and address is always correct however. I'm completely lost as to what the cause might be. Any help or advise would be much appreciated!

4

1 回答 1

1
bool encode_data(pb_ostream_t *stream, const pb_field_iter_t *field, void * const *arg)
{  
    Sensor1Event * evt = (Sensor1Event *)(*arg);
    .....
    return pb_encode_submessage(stream, Sensor1Event_fields, &evt);    
}

我想你的问题可能在这里。该参数&evt将创建一个指向消息指针的指针。但是,pb_encode_submessage()期望接收指向子消息的指针,而不是双指针。

因此,请尝试pb_encode_submessage(stream, Sensor1Event_fields, evt)代替。

不幸的是,C 语言的类型系统不允许在编译时检测到这样的错误。

于 2020-11-26T17:46:39.903 回答