Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

esp32 - C Buffer pointer

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

question from:https://stackoverflow.com/questions/66061238/c-buffer-pointer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...