migrating
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
build
|
||||
.vscode
|
||||
*.old
|
||||
sdkconfig
|
||||
*.lock
|
||||
@@ -0,0 +1,8 @@
|
||||
# The following five lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
set(COMPONENTS main esp_netif lwip protocol_examples_tapif_io startup esp_hw_support esp_system nvs_flash)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(socket-play)
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "socket-play.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,9 @@
|
||||
dependencies:
|
||||
protocol_examples_tapif_io:
|
||||
path: ${IDF_PATH}/examples/common_components/protocol_examples_tapif_io
|
||||
rules:
|
||||
- if: "target in [linux]"
|
||||
protocol_examples_common:
|
||||
path: ${IDF_PATH}/examples/common_components/protocol_examples_common
|
||||
rules:
|
||||
- if: "target not in [linux]"
|
||||
@@ -0,0 +1,112 @@
|
||||
https://pubs.opengroup.org/onlinepubs/007908799/xns/socket.html
|
||||
|
||||
https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/lwip.html#bsd-sockets-api
|
||||
|
||||
setsockopt()
|
||||
SO_REUSEADDR
|
||||
SO_BROADCAST
|
||||
SO_NO_CHECK ?
|
||||
|
||||
|
||||
/* Socket protocol types (TCP/UDP/RAW) */
|
||||
SOCK_RAW 3
|
||||
|
||||
struct raw_pcb * raw_new (u8_t proto);
|
||||
err_t raw_connect (struct raw_pcb *pcb, const ip_addr_t *ipaddr);
|
||||
void raw_disconnect (struct raw_pcb *pcb);
|
||||
err_t raw_bind (struct raw_pcb *pcb, const ip_addr_t *ipaddr);
|
||||
err_t raw_sendto (struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr);
|
||||
err_t raw_send (struct raw_pcb *pcb, struct pbuf *p);\
|
||||
void raw_recv (struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg);
|
||||
|
||||
|
||||
|
||||
int addr_family = AF_INET;
|
||||
int ip_protocol = 0;
|
||||
|
||||
https://lwip.fandom.com/wiki/Netconn_send
|
||||
|
||||
|
||||
|
||||
|
||||
// socket(addr type (v6/v4) (AF_INET/AF_INET6),
|
||||
// proto type (SOCK_STREAM (tcp)/SOCK_DGRAM (udp)/SOCK_RAW (raw)),
|
||||
// ip protocol (IPPROTO_IP/IPPROTO_IPV6));
|
||||
|
||||
#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 HOST_IP_ADDR "10.0.0.255"
|
||||
#define PORT CONFIG_EXAMPLE_PORT 4444 // placeholder
|
||||
|
||||
int err;
|
||||
int errno;
|
||||
int sockfd;
|
||||
|
||||
static const char *payload = "Message from ESP32 ";
|
||||
static const char* TAG = "PrincessPiError";
|
||||
|
||||
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);
|
||||
int opt = 1;
|
||||
|
||||
// https://pubs.opengroup.org/onlinepubs/007908799/xns/socket.html
|
||||
// https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/lwip.html#bsd-sockets-api
|
||||
if(sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_IP) < 0) {
|
||||
ESP_LOGE(TAG, "%d", errno);
|
||||
}
|
||||
|
||||
setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt));
|
||||
setsockopt(sockfd, SOL_SOCKET, SO_ERROR, &err, &optlen);
|
||||
|
||||
if(err < 0) {
|
||||
ESP_LOGE(TAG, "%d", err);
|
||||
}
|
||||
|
||||
// sendto(sockefd, payloadf, strlen(payload), 0, IP_ADDR_BROADCAST, sizeof(IP_ADDR_BROADCAST));
|
||||
sendto(sockfd, payload, strlen(payload), 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
|
||||
|
||||
// setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
||||
/*
|
||||
|
||||
#define TNetConn struct netconn *
|
||||
#define TNetBuf struct netbuf *
|
||||
|
||||
TNetConn netConn = netconn_new(NETCONN_RAW);
|
||||
|
||||
struct TIpAddr xIp;
|
||||
xIp.addr = 10.0.0.1;
|
||||
|
||||
TNetBuf xNetBuf = netbuf_new();
|
||||
xBuf = netbuf_alloc(xNetBuf, 0x100);
|
||||
netconn_sendto(netConn, xBuf, IP_ADDR_BROADCAST, 4444);
|
||||
|
||||
//Pointer xBuf = mem_malloc(0x100); // Pointer type? huh I'm too high for this
|
||||
|
||||
// netbuf_alloc (newNetBuf, 0x100);
|
||||
// memset(xBuf, 0xAA, 0x100);
|
||||
// xBuf = "\x00\x00\xFF" // raw payload. need memcpy?
|
||||
|
||||
|
||||
//netbuf_ref()
|
||||
// netconn_sendto(connection, payload, dst ip, port)
|
||||
// netconn_sendto(newNetconn, xBuf, xIP, );
|
||||
// netconn_sendto(newNetconn, &newNetbuff);
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,96 @@
|
||||
#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);
|
||||
}
|
||||
Reference in New Issue
Block a user