96 lines
2.8 KiB
C
96 lines
2.8 KiB
C
#include <string.h>
|
|
#include <sys/param.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "freertos/event_groups.h"
|
|
#include "esp_system.h"
|
|
#include "esp_event.h"
|
|
#include "esp_log.h"
|
|
#include "nvs_flash.h"
|
|
#include "esp_netif.h"
|
|
#include "protocol_examples_common.h"
|
|
|
|
#include "lwip/err.h"
|
|
#include "lwip/sockets.h"
|
|
#include "lwip/sys.h"
|
|
#include <lwip/netdb.h>
|
|
|
|
#define DEST_IP_ADDR "10.0.0.255"
|
|
// #define PORT CONFIG_EXAMPLE_PORT 4444 // placeholder
|
|
|
|
int err;
|
|
int errno;
|
|
int sockfd;
|
|
float loopSeconds = 0.25;
|
|
int timeoutSecs = 10;
|
|
|
|
static const char *payload = "LETS FUCKING GOOOOO LFGGGGGGGGG";
|
|
static const char* TAGERR = "PrincessPiError";
|
|
static const char* TAGSUCC = "PrincessPiSuccess";
|
|
|
|
static void raw_send_task(void *pvParameters) {
|
|
// configure the sockaddr
|
|
/*
|
|
struct sockaddr_in dest_addr;
|
|
dest_addr.sin_addr.s_addr = inet_addr(HOST_IP_ADDR);
|
|
dest_addr.sin_family = AF_INET;
|
|
dest_addr.sin_port = htons(PORT);
|
|
*/
|
|
struct sockaddr_in dest_addr;
|
|
dest_addr.sin_addr.s_addr = inet_addr(DEST_IP_ADDR);
|
|
dest_addr.sin_family = AF_INET;
|
|
//dest_addr.sin_port = htons(PORT);
|
|
|
|
if((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_IP)) < 0) {
|
|
ESP_LOGE(TAGERR, "Error creating socket: %d", errno);
|
|
} else {
|
|
ESP_LOGI(TAGSUCC, "Socket created");
|
|
}
|
|
|
|
// option lengths
|
|
int opt = 1;
|
|
int optlen = sizeof(int);
|
|
|
|
// timeouts
|
|
struct timeval timeout;
|
|
timeout.tv_sec = timeoutSecs;
|
|
timeout.tv_usec = 0;
|
|
|
|
// options
|
|
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
|
|
setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt));
|
|
setsockopt(sockfd, SOL_SOCKET, SO_ERROR, &err, optlen);
|
|
|
|
if(err < 0) {
|
|
ESP_LOGE(TAGERR, "Error setting socket options: %d", err);
|
|
} else {
|
|
ESP_LOGI(TAGSUCC, "Socket options set");
|
|
}
|
|
|
|
while(1) {
|
|
// sendto(sockefd, payloadf, strlen(payload), 0, IP_ADDR_BROADCAST, sizeof(IP_ADDR_BROADCAST));
|
|
int sendErr = sendto(sockfd, payload, strlen(payload), 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
|
|
|
|
if(sendErr < 0) {
|
|
ESP_LOGE(TAGERR, "Error sending: %d", err);
|
|
} else {
|
|
ESP_LOGI(TAGSUCC, "Sent!\n\tLooping every %f seconds\n\tDst: %s\n\tPayload: %s\n\tTimeout: %d\n", loopSeconds, DEST_IP_ADDR, payload, timeoutSecs);
|
|
}
|
|
|
|
// delay on loop
|
|
vTaskDelay((loopSeconds*1000) / portTICK_PERIOD_MS);
|
|
}
|
|
}
|
|
|
|
void app_main(void) {
|
|
// init shit
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
// wifi connect
|
|
ESP_ERROR_CHECK(example_connect());
|
|
|
|
// fork raw_send_task to FreeRTOS task
|
|
xTaskCreate(raw_send_task, "raw_send_task", 4096, NULL, 5, NULL);
|
|
} |