3

Question

I'm currently working with ESP-NOW with the esp-idf. Below is a snippet from their espnow examples. I need some help in identifying what does this line means, but I'm not quite sure what to google. Can someone point me in the correct direction?

example_espnow_data_t *buf = (example_espnow_data_t *)send_param->buffer;

What I tried so far

I can't seem to find any guide online since I'm not sure what to google. Based on what I could find, my guess is that the send_param buffer parameter is parsed to the buf pointer of example_espnow_data_t. Is my understanding correct?

Sample code

example_espnow_send_param_t is a typdef struct with buffer as one of the parameters. The send parameters are then allocated and filled to the send_param memory block. This then get passed to the data prepare function.

// code is truncated

typedef struct { // from header files
    bool unicast;                         //Send unicast ESPNOW data.
    bool broadcast;                       //Send broadcast ESPNOW data.
    .
    .
} example_espnow_send_param_t;

typedef struct { // from header files
    uint8_t type;                         //Broadcast or unicast ESPNOW data.
    .
    .
} __attribute__((packed)) example_espnow_data_t;

send_param = malloc(sizeof(example_espnow_send_param_t));
memset(send_param, 0, sizeof(example_espnow_send_param_t));
send_param->unicast = false;
send_param->broadcast = false;
.
.
example_espnow_data_prepare(send_param);

void example_espnow_data_prepare(example_espnow_send_param_t *send_param)
{
    example_espnow_data_t *buf = (example_espnow_data_t *)send_param->buffer;
    assert(send_param->len >= sizeof(example_espnow_data_t));
    .
    .
}

ESP32 repo

Wb10
  • 89
  • 4

1 Answers1

1

You've truncated the relevant part from the definition of struct example_espnow_send_param_t - the field buffer :)

/* Parameters of sending ESPNOW data. */
typedef struct {
    ...
    uint8_t *buffer;                      //Buffer pointing to ESPNOW data.
    ...
} example_espnow_send_param_t;

Anyway, the function receives a pointer to struct example_espnow_send_param_t as input variable send_param. The line in question picks the field buffer from that struct. buffer just holds a pointer to some raw data as far as example_espnow_send_param_t knows.

It then converts this pointer to raw data into a pointer to struct example_espnow_data_t - thereby assuming that this is what the raw data actually holds. Finally, it allocates a new variable of the correct pointer type and assigns the result of the conversion to it.

Tarmo
  • 3,728
  • 1
  • 8
  • 25