Using no Broadcast logic for speed but its working now. There is to much Acks going on but for the prototyp that is okay
123 lines
3.9 KiB
C
123 lines
3.9 KiB
C
#ifndef COMMUNICATION_HANDLER_H
|
|
#define COMMUNICATION_HANDLER_H
|
|
|
|
#include "client_handler.h"
|
|
#include <esp_now.h>
|
|
#include <esp_wifi.h>
|
|
#include <freertos/FreeRTOS.h>
|
|
#include <freertos/queue.h>
|
|
#include <freertos/task.h>
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include "esp_partition.h"
|
|
#include "message_structs.h"
|
|
|
|
#define BROADCAST_INTERVAL_MS 500
|
|
|
|
#define CLIENT_TIMEOUT_MS 5000 // 5 Sekunden Timeout
|
|
#define CHECK_INTERVAL_MS 1000 // Jede Sekunde überprüfen
|
|
|
|
extern uint8_t broadcast_address[ESP_NOW_ETH_ALEN];
|
|
#define IS_BROADCAST_ADDR(addr) (memcmp(addr, broadcast_address, ESP_NOW_ETH_ALEN) == 0)
|
|
|
|
#define MESSAGE_QUEUE_SIZE 10
|
|
|
|
typedef union __attribute__((packed)) {
|
|
OTA_PREPARE_FOR_UPDATE_Payload ota_prepare_for_update_payload;
|
|
OTA_PREPARE_ACKNOWLEDGED_Payload ota_prepare_acknowledged_payload;
|
|
OTA_READY_TO_RECEIVE_Payload ota_ready_to_receive_payload;
|
|
OTA_CHUNK_Payload ota_chunk_payload;
|
|
OTA_REQUEST_BLOCK_STATUS_Payload ota_request_block_status_payload;
|
|
OTA_BLOCK_STATUS_REPORT_Payload ota_block_status_report_payload;
|
|
OTA_COMMIT_BLOCK_Payload ota_commit_block_payload;
|
|
OTA_BLOCK_COMMITTED_Payload ota_block_committed_payload;
|
|
OTA_FINISH_UPDATE_Payload ota_finish_update_payload;
|
|
OTA_UPDATE_STATUS_Payload ota_update_status_payload;
|
|
StatusPayload status_payload;
|
|
ConfigPayload config_payload;
|
|
PingPayload ping_payload;
|
|
BroadCastPayload broadcast_payload;
|
|
RegisterPayload register_payload;
|
|
FirmwarePrepPayload firmware_prep_payload;
|
|
FirmwarePayload firmware_payload;
|
|
} PayloadUnion;
|
|
|
|
typedef struct __attribute__((packed)) {
|
|
uint16_t version; // protcol version
|
|
CommandPages commandPage;
|
|
uint16_t length;
|
|
PayloadUnion payload;
|
|
} BaseMessage;
|
|
|
|
static_assert(sizeof(BaseMessage) <= 255,
|
|
"BaseMessage darf nicht größer als 255 sein");
|
|
|
|
typedef void (*ESP_RegisterFunctionCallback)(
|
|
const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len);
|
|
typedef void (*ESP_RegisterTaskCallback)(
|
|
const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len);
|
|
|
|
struct ESP_RegisterdFunction {
|
|
CommandPages MSGID;
|
|
ESP_RegisterFunctionCallback callback;
|
|
};
|
|
|
|
struct ESP_RegisterdTask {
|
|
CommandPages MSGID;
|
|
ESP_RegisterTaskCallback task;
|
|
};
|
|
|
|
struct ESP_MessageBroker {
|
|
struct ESP_RegisterdFunction FunctionList[64];
|
|
uint8_t num_direct_callbacks;
|
|
struct ESP_RegisterdTask TaskList[64];
|
|
uint8_t num_task_callbacks;
|
|
};
|
|
|
|
typedef struct {
|
|
QueueHandle_t message_queue;
|
|
} ESP_MessageBrokerTaskParams_t;
|
|
|
|
typedef struct {
|
|
esp_now_recv_info_t esp_now_info;
|
|
uint8_t *data;
|
|
int data_len;
|
|
} ESPNOW_MessageInfo;
|
|
|
|
void ESP_InitMessageBroker(QueueHandle_t msg_queue_handle);
|
|
void ESP_RegisterFunction(CommandPages command,
|
|
ESP_RegisterFunctionCallback callback);
|
|
void ESP_RegisterTask(CommandPages command, ESP_RegisterTaskCallback callback);
|
|
void ESP_MessageBrokerTask(void *param);
|
|
|
|
void ESPNOW_RegisterMasterCallbacks();
|
|
void ESPNOW_RegisterSlaveCallbacks();
|
|
void ESPNOW_RegisterOTAMaster();
|
|
void ESPNOW_RegisterOTASlave();
|
|
|
|
int init_com(ClientList *clients, uint8_t wifi_channel);
|
|
int getNextFreeClientId();
|
|
int add_peer(uint8_t *macAddr);
|
|
BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload,
|
|
size_t payload_size);
|
|
|
|
void master_broadcast_task(void *param);
|
|
void master_ping_task(void *param);
|
|
void master_broadcast_ping(void *param);
|
|
void master_receive_callback(const esp_now_recv_info_t *esp_now_info,
|
|
const uint8_t *data, int data_len);
|
|
|
|
void client_receive_callback(const esp_now_recv_info_t *esp_now_info,
|
|
const uint8_t *data, int data_len);
|
|
void client_data_sending_task(void *param);
|
|
void client_send_random_data_task(void *param);
|
|
|
|
void client_monitor_task(void *pvParameters);
|
|
|
|
void send_ota_block_chunks(uint8_t client_id, uint16_t block_id);
|
|
|
|
#endif
|