Some Cleanup removed the example prefix from functions and structs

This commit is contained in:
simon 2024-10-04 13:11:54 +02:00
parent 72851b69c5
commit 6d383e43b4
2 changed files with 73 additions and 73 deletions

View File

@ -29,12 +29,12 @@
#define CONFIG_ESPNOW_SEND_LEN 250
#define CONFIG_ESPNOW_LMK "lmk1234567890123"
const char *tag = "Exam";
static QueueHandle_t s_example_espnow_queue;
static uint8_t s_example_broadcast_mac[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF,
const char *tag = "Alox";
static QueueHandle_t s_espnow_queue;
static uint8_t s_broadcast_mac[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF};
static uint16_t s_example_espnow_seq[2] = {0, 0};
static void example_espnow_deinit(example_espnow_send_param_t *send_param);
static uint16_t s_espnow_seq[2] = {0, 0};
static void espnow_deinit(espnow_send_param_t *send_param);
void wifi_init(void) {
esp_err_t ret = nvs_flash_init();
@ -60,39 +60,39 @@ void wifi_init(void) {
WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR));
}
static void example_espnow_deinit(example_espnow_send_param_t *send_param) {
static void espnow_deinit(espnow_send_param_t *send_param) {
free(send_param->buffer);
free(send_param);
vSemaphoreDelete(s_example_espnow_queue);
vSemaphoreDelete(s_espnow_queue);
esp_now_deinit();
}
void example_espnow_data_prepare(example_espnow_send_param_t *send_param) {
example_espnow_data_t *buf = (example_espnow_data_t *)send_param->buffer;
void espnow_data_prepare(espnow_send_param_t *send_param) {
espnow_data_t *buf = (espnow_data_t *)send_param->buffer;
ESP_LOGI(tag, "Example_Data_SIZE: %u, send_param_len: %d\n", sizeof(example_espnow_data_t), send_param->len);
ESP_LOGI(tag, "Example_Data_SIZE: %u, send_param_len: %d\n", sizeof(espnow_data_t), send_param->len);
assert(send_param->len >= sizeof(example_espnow_data_t));
assert(send_param->len >= sizeof(espnow_data_t));
buf->type = IS_BROADCAST_ADDR(send_param->dest_mac)
? EXAMPLE_ESPNOW_DATA_BROADCAST
: EXAMPLE_ESPNOW_DATA_UNICAST;
buf->state = send_param->state;
buf->seq_num = s_example_espnow_seq[buf->type]++;
buf->seq_num = s_espnow_seq[buf->type]++;
buf->crc = 0;
buf->magic = send_param->magic;
buf->payload.isMaster = isMaster;
/* Fill all remaining bytes after the data with random values */
/* esp_fill_random(buf->payload,
send_param->len - sizeof(example_espnow_data_t)); */ // wieso sollte ich das mit random daten füllen
send_param->len - sizeof(espnow_data_t)); */ // wieso sollte ich das mit random daten füllen
buf->crc = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, send_param->len);
}
static void example_espnow_send_cb(const uint8_t *mac_addr,
static void espnow_send_cb(const uint8_t *mac_addr,
esp_now_send_status_t status) {
example_espnow_event_t evt;
example_espnow_event_send_cb_t *send_cb = &evt.info.send_cb;
espnow_event_t evt;
espnow_event_send_cb_t *send_cb = &evt.info.send_cb;
if (mac_addr == NULL) {
ESP_LOGE(tag, "Send cb arg error");
@ -102,15 +102,15 @@ static void example_espnow_send_cb(const uint8_t *mac_addr,
evt.id = EXAMPLE_ESPNOW_SEND_CB;
memcpy(send_cb->mac_addr, mac_addr, ESP_NOW_ETH_ALEN);
send_cb->status = status;
if (xQueueSend(s_example_espnow_queue, &evt, ESPNOW_MAXDELAY) != pdTRUE) {
if (xQueueSend(s_espnow_queue, &evt, ESPNOW_MAXDELAY) != pdTRUE) {
ESP_LOGW(tag, "Send send queue fail");
}
}
static void example_espnow_recv_cb(const esp_now_recv_info_t *recv_info,
static void espnow_recv_cb(const esp_now_recv_info_t *recv_info,
const uint8_t *data, int len) {
example_espnow_event_t evt;
example_espnow_event_recv_cb_t *recv_cb = &evt.info.recv_cb;
espnow_event_t evt;
espnow_event_recv_cb_t *recv_cb = &evt.info.recv_cb;
uint8_t *mac_addr = recv_info->src_addr;
uint8_t *des_addr = recv_info->des_addr;
@ -138,18 +138,18 @@ static void example_espnow_recv_cb(const esp_now_recv_info_t *recv_info,
}
memcpy(recv_cb->data, data, len);
recv_cb->data_len = len;
if (xQueueSend(s_example_espnow_queue, &evt, ESPNOW_MAXDELAY) != pdTRUE) {
if (xQueueSend(s_espnow_queue, &evt, ESPNOW_MAXDELAY) != pdTRUE) {
ESP_LOGW(tag, "Send receive queue fail");
free(recv_cb->data);
}
}
int example_espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state,
int espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state,
uint16_t *seq, uint32_t *magic) {
example_espnow_data_t *buf = (example_espnow_data_t *)data;
espnow_data_t *buf = (espnow_data_t *)data;
uint16_t crc, crc_cal = 0;
if (data_len < sizeof(example_espnow_data_t)) {
if (data_len < sizeof(espnow_data_t)) {
ESP_LOGE(tag, "Receive ESPNOW data too short, len:%d", data_len);
return -1;
}
@ -162,9 +162,9 @@ int example_espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state,
crc_cal = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, data_len);
if(buf->payload.isMaster) {
ESP_LOGE(tag, "Recived Data from Master");
ESP_LOGI(tag, "Recived Data from Master");
} else {
ESP_LOGE(tag, "Recived Data from Slave");
ESP_LOGI(tag, "Recived Data from Slave");
}
if (crc_cal == crc) {
@ -174,8 +174,8 @@ int example_espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state,
return -1;
}
static void example_espnow_task(void *pvParameter) {
example_espnow_event_t evt;
static void espnow_task(void *pvParameter) {
espnow_event_t evt;
uint8_t recv_state = 0;
uint16_t recv_seq = 0;
uint32_t recv_magic = 0;
@ -186,19 +186,19 @@ static void example_espnow_task(void *pvParameter) {
ESP_LOGI(tag, "Start sending broadcast data");
/* Start sending broadcast ESPNOW data. */
example_espnow_send_param_t *send_param =
(example_espnow_send_param_t *)pvParameter;
espnow_send_param_t *send_param =
(espnow_send_param_t *)pvParameter;
if (esp_now_send(send_param->dest_mac, send_param->buffer, send_param->len) !=
ESP_OK) {
ESP_LOGE(tag, "Send error");
example_espnow_deinit(send_param);
espnow_deinit(send_param);
vTaskDelete(NULL);
}
while (xQueueReceive(s_example_espnow_queue, &evt, portMAX_DELAY) == pdTRUE) {
while (xQueueReceive(s_espnow_queue, &evt, portMAX_DELAY) == pdTRUE) {
switch (evt.id) {
case EXAMPLE_ESPNOW_SEND_CB: {
example_espnow_event_send_cb_t *send_cb = &evt.info.send_cb;
espnow_event_send_cb_t *send_cb = &evt.info.send_cb;
is_broadcast = IS_BROADCAST_ADDR(send_cb->mac_addr);
ESP_LOGD(tag, "Send data to " MACSTR ", status1: %d",
@ -212,7 +212,7 @@ static void example_espnow_task(void *pvParameter) {
send_param->count--;
if (send_param->count == 0) {
ESP_LOGI(tag, "Send done");
example_espnow_deinit(send_param);
espnow_deinit(send_param);
vTaskDelete(NULL);
}
}
@ -225,21 +225,21 @@ static void example_espnow_task(void *pvParameter) {
ESP_LOGI(tag, "send data to " MACSTR "", MAC2STR(send_cb->mac_addr));
memcpy(send_param->dest_mac, send_cb->mac_addr, ESP_NOW_ETH_ALEN);
example_espnow_data_prepare(send_param);
espnow_data_prepare(send_param);
/* Send the next data after the previous data is sent. */
if (esp_now_send(send_param->dest_mac, send_param->buffer,
send_param->len) != ESP_OK) {
ESP_LOGE(tag, "Send error");
example_espnow_deinit(send_param);
espnow_deinit(send_param);
vTaskDelete(NULL);
}
break;
}
case EXAMPLE_ESPNOW_RECV_CB: {
example_espnow_event_recv_cb_t *recv_cb = &evt.info.recv_cb;
espnow_event_recv_cb_t *recv_cb = &evt.info.recv_cb;
ret = example_espnow_data_parse(recv_cb->data, recv_cb->data_len,
ret = espnow_data_parse(recv_cb->data, recv_cb->data_len,
&recv_state, &recv_seq, &recv_magic);
free(recv_cb->data);
if (ret == EXAMPLE_ESPNOW_DATA_BROADCAST) {
@ -251,7 +251,7 @@ static void example_espnow_task(void *pvParameter) {
esp_now_peer_info_t *peer = malloc(sizeof(esp_now_peer_info_t));
if (peer == NULL) {
ESP_LOGE(tag, "Malloc peer information fail");
example_espnow_deinit(send_param);
espnow_deinit(send_param);
vTaskDelete(NULL);
}
memset(peer, 0, sizeof(esp_now_peer_info_t));
@ -285,11 +285,11 @@ static void example_espnow_task(void *pvParameter) {
/* Start sending unicast ESPNOW data. */
memcpy(send_param->dest_mac, recv_cb->mac_addr, ESP_NOW_ETH_ALEN);
example_espnow_data_prepare(send_param);
espnow_data_prepare(send_param);
if (esp_now_send(send_param->dest_mac, send_param->buffer,
send_param->len) != ESP_OK) {
ESP_LOGE(tag, "Send error");
example_espnow_deinit(send_param);
espnow_deinit(send_param);
vTaskDelete(NULL);
} else {
send_param->broadcast = false;
@ -317,20 +317,20 @@ static void example_espnow_task(void *pvParameter) {
}
}
static esp_err_t example_espnow_init(void) {
example_espnow_send_param_t *send_param;
static esp_err_t espnow_init(void) {
espnow_send_param_t *send_param;
s_example_espnow_queue =
xQueueCreate(ESPNOW_QUEUE_SIZE, sizeof(example_espnow_event_t));
if (s_example_espnow_queue == NULL) {
s_espnow_queue =
xQueueCreate(ESPNOW_QUEUE_SIZE, sizeof(espnow_event_t));
if (s_espnow_queue == NULL) {
ESP_LOGE(tag, "Create mutex fail");
return ESP_FAIL;
}
/* Initialize ESPNOW and register sending and receiving callback function. */
ESP_ERROR_CHECK(esp_now_init());
ESP_ERROR_CHECK(esp_now_register_send_cb(example_espnow_send_cb));
ESP_ERROR_CHECK(esp_now_register_recv_cb(example_espnow_recv_cb));
ESP_ERROR_CHECK(esp_now_register_send_cb(espnow_send_cb));
ESP_ERROR_CHECK(esp_now_register_recv_cb(espnow_recv_cb));
#if CONFIG_ESPNOW_ENABLE_POWER_SAVE
ESP_ERROR_CHECK(esp_now_set_wake_window(CONFIG_ESPNOW_WAKE_WINDOW));
ESP_ERROR_CHECK(esp_wifi_connectionless_module_set_wake_interval(
@ -343,7 +343,7 @@ static esp_err_t example_espnow_init(void) {
esp_now_peer_info_t *peer = malloc(sizeof(esp_now_peer_info_t));
if (peer == NULL) {
ESP_LOGE(tag, "Malloc peer information fail");
vSemaphoreDelete(s_example_espnow_queue);
vSemaphoreDelete(s_espnow_queue);
esp_now_deinit();
return ESP_FAIL;
}
@ -351,19 +351,19 @@ static esp_err_t example_espnow_init(void) {
peer->channel = CONFIG_ESPNOW_CHANNEL;
peer->ifidx = ESPNOW_WIFI_IF;
peer->encrypt = false;
memcpy(peer->peer_addr, s_example_broadcast_mac, ESP_NOW_ETH_ALEN);
memcpy(peer->peer_addr, s_broadcast_mac, ESP_NOW_ETH_ALEN);
ESP_ERROR_CHECK(esp_now_add_peer(peer));
free(peer);
/* Initialize sending parameters. */
send_param = malloc(sizeof(example_espnow_send_param_t));
send_param = malloc(sizeof(espnow_send_param_t));
if (send_param == NULL) {
ESP_LOGE(tag, "Malloc send parameter fail");
vSemaphoreDelete(s_example_espnow_queue);
vSemaphoreDelete(s_espnow_queue);
esp_now_deinit();
return ESP_FAIL;
}
memset(send_param, 0, sizeof(example_espnow_send_param_t));
memset(send_param, 0, sizeof(espnow_send_param_t));
send_param->unicast = false;
send_param->broadcast = true;
send_param->state = 0;
@ -375,14 +375,14 @@ static esp_err_t example_espnow_init(void) {
if (send_param->buffer == NULL) {
ESP_LOGE(tag, "Malloc send buffer fail");
free(send_param);
vSemaphoreDelete(s_example_espnow_queue);
vSemaphoreDelete(s_espnow_queue);
esp_now_deinit();
return ESP_FAIL;
}
memcpy(send_param->dest_mac, s_example_broadcast_mac, ESP_NOW_ETH_ALEN);
example_espnow_data_prepare(send_param);
memcpy(send_param->dest_mac, s_broadcast_mac, ESP_NOW_ETH_ALEN);
espnow_data_prepare(send_param);
xTaskCreate(example_espnow_task, "example_espnow_task", 2048, send_param, 4,
xTaskCreate(espnow_task, "espnow_task", 2048, send_param, 4,
NULL);
return ESP_OK;
@ -402,5 +402,5 @@ void app_main(void) {
vTaskDelay(500/portTICK_PERIOD_MS);
wifi_init();
example_espnow_init();
espnow_init();
}

View File

@ -6,35 +6,35 @@
#define ESPNOW_QUEUE_SIZE 6
#define IS_BROADCAST_ADDR(addr) \
(memcmp(addr, s_example_broadcast_mac, ESP_NOW_ETH_ALEN) == 0)
(memcmp(addr, s_broadcast_mac, ESP_NOW_ETH_ALEN) == 0)
static bool isMaster;
typedef enum {
EXAMPLE_ESPNOW_SEND_CB,
EXAMPLE_ESPNOW_RECV_CB,
} example_espnow_event_id_t;
} espnow_event_id_t;
typedef struct {
uint8_t mac_addr[ESP_NOW_ETH_ALEN];
esp_now_send_status_t status;
} example_espnow_event_send_cb_t;
} espnow_event_send_cb_t;
typedef struct {
uint8_t mac_addr[ESP_NOW_ETH_ALEN];
uint8_t *data;
int data_len;
} example_espnow_event_recv_cb_t;
} espnow_event_recv_cb_t;
typedef union {
example_espnow_event_send_cb_t send_cb;
example_espnow_event_recv_cb_t recv_cb;
} example_espnow_event_info_t;
espnow_event_send_cb_t send_cb;
espnow_event_recv_cb_t recv_cb;
} espnow_event_info_t;
typedef struct {
example_espnow_event_id_t id;
example_espnow_event_info_t info;
} example_espnow_event_t;
espnow_event_id_t id;
espnow_event_info_t info;
} espnow_event_t;
enum {
EXAMPLE_ESPNOW_DATA_BROADCAST,
@ -46,9 +46,6 @@ typedef struct {
bool isMaster;
} __attribute__((packed)) payloadData;
static_assert(sizeof(payloadData) <= MAX_PAYLOAD_SIZE, "payloadData struct is too big to be sent in one part, keep it under 250 Bytes!");
/* User defined field of ESPNOW data in this example. */
typedef struct {
uint8_t type; // Broadcast or unicast ESPNOW data.
@ -58,7 +55,10 @@ typedef struct {
uint32_t magic; // Magic number which is used to determine which device to
// send unicast ESPNOW data.
payloadData payload; // Real payload of ESPNOW data.
} __attribute__((packed)) example_espnow_data_t;
} __attribute__((packed)) espnow_data_t;
static_assert(sizeof(espnow_data_t) <= MAX_PAYLOAD_SIZE, "payloadData struct is too big to be sent in one part, keep it under 250 Bytes!");
typedef struct {
bool unicast; // Send unicast ESPNOW data.
@ -71,6 +71,6 @@ typedef struct {
int len; // Length of ESPNOW data to be sent, unit: byte.
uint8_t *buffer; // Buffer pointing to ESPNOW data.
uint8_t dest_mac[ESP_NOW_ETH_ALEN]; // MAC address of destination device.
} example_espnow_send_param_t;
} espnow_send_param_t;
#endif