91 lines
2.2 KiB
C
91 lines
2.2 KiB
C
#ifndef COMMUNICATION_HANDLER_H
|
|
#define COMMUNICATION_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>
|
|
|
|
#define BROADCAST_INTERVAL_MS 500
|
|
|
|
#define CLIENT_TIMEOUT_MS 5000 // 5 Sekunden Timeout
|
|
#define CHECK_INTERVAL_MS 1000 // Jede Sekunde überprüfen
|
|
static uint8_t broadcast_address[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF,
|
|
0xFF, 0xFF, 0xFF};
|
|
#define IS_BROADCAST_ADDR(addr) \
|
|
(memcmp(addr, broadcast_address, ESP_NOW_ETH_ALEN) == 0)
|
|
|
|
#define MAX_CLIENTS 19
|
|
#define MESSAGE_QUEUE_SIZE 10
|
|
|
|
typedef enum {
|
|
BroadCastPage,
|
|
StatusPage,
|
|
PingPage,
|
|
RegisterPage,
|
|
} CommandPages;
|
|
|
|
typedef struct {
|
|
uint32_t uptime;
|
|
uint8_t status;
|
|
} StatusPayload;
|
|
|
|
typedef struct {
|
|
uint32_t timestamp;
|
|
} PingPayload;
|
|
|
|
typedef struct {
|
|
} BroadCastPayload;
|
|
|
|
typedef struct {
|
|
bool familierClient;
|
|
} RegisterPayload;
|
|
|
|
typedef union {
|
|
StatusPayload status_payload;
|
|
PingPayload ping_payload;
|
|
BroadCastPayload broadcast_payload;
|
|
RegisterPayload register_payload;
|
|
} PayloadUnion;
|
|
|
|
typedef struct {
|
|
uint16_t version;
|
|
CommandPages commandPage;
|
|
uint16_t length;
|
|
PayloadUnion payload;
|
|
} BaseMessage;
|
|
|
|
static_assert(sizeof(BaseMessage) <= 255,
|
|
"BaseMessage darf nicht größer als 255 sein");
|
|
|
|
typedef struct {
|
|
uint8_t macAddr[ESP_NOW_ETH_ALEN];
|
|
int rssi;
|
|
bool isAvailable;
|
|
TickType_t lastSuccessfullPing;
|
|
} ClientInfo;
|
|
|
|
void init_com();
|
|
int getNextFreeClientId();
|
|
void add_peer(uint8_t *macAddr);
|
|
const char *MACtoString(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_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_monitor_task(void *pvParameters);
|
|
|
|
#endif
|