]> vcs.slashdirt.org Git - sw/tic2json.git/commitdiff

vcs.slashdirt.org Git - sw/tic2json.git/commitdiff

git git / sw / tic2json.git / commitdiff
? search:
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 1c524e3)
embedded: esptic: implement an UDP client
2021年9月27日 10:32:42 +0000 (12:32 +0200)
2021年9月28日 13:53:00 +0000 (15:53 +0200)
This implements a barebone UDP client: it reads TIC frames from the UART
and pushes JSON-formatted UDP packets to a remote party.

embedded/ESP-RTOS/esptic/main/wifista.c [new file with mode: 0644] patch | blob

diff --git a/embedded/ESP-RTOS/esptic/components/tic2json/CMakeLists.txt b/embedded/ESP-RTOS/esptic/components/tic2json/CMakeLists.txt
index 630d9f01c2a3222ee13c5895c3cf0b54aeb79486..ca71a30f3955c8e1d8972f43d272c907b815e99f 100644 (file)
--- a/embedded/ESP-RTOS/esptic/components/tic2json/CMakeLists.txt
+++ b/embedded/ESP-RTOS/esptic/components/tic2json/CMakeLists.txt
@@ -1,6 +1,6 @@
execute_process(COMMAND make -C ${CMAKE_CURRENT_LIST_DIR}/src csources)
idf_component_register(SRC_DIRS "src")
-target_compile_options(${COMPONENT_LIB} PRIVATE -DBAREBUILD)
+target_compile_options(${COMPONENT_LIB} PRIVATE -DBAREBUILD -DPRINT2BUF)
if(CONFIG_TIC2JSON_TICV01)
add_definitions(-DTICV01)
diff --git a/embedded/ESP-RTOS/esptic/components/tic2json/component.mk b/embedded/ESP-RTOS/esptic/components/tic2json/component.mk
index e47d39a44ab539aa15875395099dbc353f8ddabd..2a2a79fafa758056a6d527c08ee4400b12938944 100644 (file)
--- a/embedded/ESP-RTOS/esptic/components/tic2json/component.mk
+++ b/embedded/ESP-RTOS/esptic/components/tic2json/component.mk
@@ -1,5 +1,5 @@
COMPONENT_SRCDIRS := src
-CFLAGS += -DBAREBUILD
+CFLAGS += -DBAREBUILD -DPRINT2BUF
ifdef CONFIG_TIC2JSON_TICV01
CFLAGS += -DTICV01
diff --git a/embedded/ESP-RTOS/esptic/main/CMakeLists.txt b/embedded/ESP-RTOS/esptic/main/CMakeLists.txt
index 383a2d6d0b7ffea56beaea0c18c5fe24df3a1975..afe29ee5b43adf7d5a16efe1a72d7a9f942f4fb6 100644 (file)
--- a/embedded/ESP-RTOS/esptic/main/CMakeLists.txt
+++ b/embedded/ESP-RTOS/esptic/main/CMakeLists.txt
@@ -1,3 +1,3 @@
-set(COMPONENT_SRCS "main.c")
+set(COMPONENT_SRCS "main.c wifista.c")
register_component()
diff --git a/embedded/ESP-RTOS/esptic/main/Kconfig b/embedded/ESP-RTOS/esptic/main/Kconfig
index 6bfe446af00b57c3adc891e2fb68ae3b51c94fc2..97a7c9204b6667a577ad19ee02f540d1c5834ff3 100644 (file)
--- a/embedded/ESP-RTOS/esptic/main/Kconfig
+++ b/embedded/ESP-RTOS/esptic/main/Kconfig
@@ -1,5 +1,17 @@
menu "esptic"
+config ESPTIC_WIFI_SSID
+ string "WiFi SSID"
+ default "myssid"
+ help
+ SSID (network name) to connect to.
+
+config ESPTIC_WIFI_PASSWORD
+ string "WiFi Password"
+ default "mypassword"
+ help
+ WiFi password (WPA2) to use.
+
config ESPTIC_BAUDRATE
int "TIC baudrate"
default 9600
@@ -11,7 +23,15 @@ config ESPTIC_UART_NUM
int "TIC UART"
default 0
help
- UART peripheral to use for TIC input and JSON output
+ UART peripheral to use for TIC input
+
+config ESPTIC_UDP_HOST
+ string "UDP remote hostname"
+ default "myhost"
+
+config ESPTIC_UDP_PORT
+ string "UDP remote port"
+ default "8094"
endmenu
diff --git a/embedded/ESP-RTOS/esptic/main/main.c b/embedded/ESP-RTOS/esptic/main/main.c
index 3ff4e9ed4c95a6a499770b47a0de29f48c8bdbdc..6e78e073f90f5e1c3048ea48a308bef7a936a43b 100644 (file)
--- a/embedded/ESP-RTOS/esptic/main/main.c
+++ b/embedded/ESP-RTOS/esptic/main/main.c
@@ -8,23 +8,92 @@
/**
* @file
- * Receives TIC on RX, outputs JSON on TX.
* @note: Memory usage detailed below has been tested on ESP8266 in "Release" (-Os) build:
+ * Receives TIC on RX, outputs JSON via UDP.
* - TICV01: max stack 5400, max heap: 3764+80
* - TICV02: max stack 5816, max heap: 3764+80
*/
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_vfs_dev.h"
+#include "esp_log.h"
#include "driver/uart.h"
-void tic2json_main(FILE * yyin);
+#include "lwip/err.h"
+#include "lwip/sockets.h"
+#include "lwip/sys.h"
+#include "lwip/netdb.h"
+
+#define XSTR(s) STR(s)
+#define STR(s) #s
+
+#define UDPBUFSIZE 1432 // avoid fragmentation
+
+static const char * TAG = "esptic";
+static struct sockaddr Gai_addr;
+static socklen_t Gai_addrlen;
+static int Gsockfd;
+
+typedef void (*tic2json_framecb_t)(char * buf, size_t size);
+void tic2json_main(FILE * yyin, char * buf, size_t size, tic2json_framecb_t cb);
+void wifista_main(void);
+
+static int udp_setup(void)
+{
+ struct addrinfo hints, *result, *rp;
+ int ret;
+
+ // obtain address(es) matching host/port
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_DGRAM;
+ hints.ai_protocol = IPPROTO_UDP;
+
+ ret = getaddrinfo(CONFIG_ESPTIC_UDP_HOST, CONFIG_ESPTIC_UDP_PORT, &hints, &result);
+ if (ret) {
+ ESP_LOGE(TAG, "getaddrinfo: %d", ret);
+ return ESP_FAIL;
+ }
+
+ // try each address until one succeeds
+ for (rp = result; rp; rp = rp->ai_next) {
+ Gsockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
+ if (-1 != Gsockfd)
+ break; // success
+ }
+
+ if (!rp) {
+ ESP_LOGE(TAG, "Could not reach server");
+ ret = ESP_FAIL;
+ goto cleanup;
+ }
+
+ memcpy(&Gai_addr, rp->ai_addr, sizeof(Gai_addr));
+ Gai_addrlen = rp->ai_addrlen;
+
+ ret = ESP_OK;
+
+cleanup:
+ freeaddrinfo(result);
+ return (ret);
+
+}
+
+static void ticframecb(char * buf, size_t size)
+{
+ sendto(Gsockfd, buf, size, 0, &Gai_addr, Gai_addrlen);
+}
void app_main(void)
{
+ FILE *yyin;
+ static char buf[UDPBUFSIZE];
+
uart_config_t uart_config = {
.baud_rate = CONFIG_ESPTIC_BAUDRATE,
.data_bits = UART_DATA_7_BITS,
@@ -40,6 +109,20 @@ void app_main(void)
/* Tell VFS to use UART driver */
esp_vfs_dev_uart_use_driver(CONFIG_ESPTIC_UART_NUM);
+ /* start wifi */
+ wifista_main();
+
+ /* setup UDP client */
+ ESP_ERROR_CHECK(udp_setup());
+
+ yyin = fopen("/dev/uart/" XSTR(CONFIG_ESPTIC_UART_NUM), "r");
+ if (!yyin) {
+ ESP_LOGE(TAG, "Cannot open UART");
+ abort();
+ }
+
+ ESP_LOGI(TAG, "Rock'n'roll");
+
while (1)
- tic2json_main(stdin);
+ tic2json_main(yyin, buf, UDPBUFSIZE, ticframecb);
}
diff --git a/embedded/ESP-RTOS/esptic/main/wifista.c b/embedded/ESP-RTOS/esptic/main/wifista.c
new file mode 100644 (file)
index 0000000..109c0cb
--- /dev/null
+++ b/embedded/ESP-RTOS/esptic/main/wifista.c
@@ -0,0 +1,110 @@
+/* WiFi station Example - amended for unlimited retries
+
+ This example code is in the Public Domain (or CC0 licensed, at your option.)
+
+ Unless required by applicable law or agreed to in writing, this
+ software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+ CONDITIONS OF ANY KIND, either express or implied.
+*/
+#include <string.h>
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "freertos/event_groups.h"
+#include "esp_system.h"
+#include "esp_log.h"
+#include "esp_netif.h"
+#include "esp_event.h"
+#include "esp_wifi.h"
+#include "nvs.h"
+#include "nvs_flash.h"
+
+#include "lwip/err.h"
+#include "lwip/sys.h"
+
+/* The examples use WiFi configuration that you can set via project configuration menu
+
+ If you'd rather not, just change the below entries to strings with
+ the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
+*/
+#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESPTIC_WIFI_SSID
+#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESPTIC_WIFI_PASSWORD
+
+/* FreeRTOS event group to signal when we are connected*/
+static EventGroupHandle_t s_wifi_event_group;
+
+/* The event group allows multiple bits for each event, but we only care being connected to the AP with an IP */
+#define WIFI_CONNECTED_BIT BIT0
+
+static const char *TAG = "wifi station";
+
+static void event_handler(void* arg, esp_event_base_t event_base,
+ int32_t event_id, void* event_data)
+{
+ if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
+ esp_wifi_connect();
+ } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
+ esp_wifi_connect();
+ ESP_LOGI(TAG, "retry to connect to the AP");
+ } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
+ ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
+ ESP_LOGI(TAG, "got ip:%s", ip4addr_ntoa(&event->ip_info.ip));
+ xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
+ }
+}
+
+static void wifi_init_sta(void)
+{
+ s_wifi_event_group = xEventGroupCreate();
+
+ tcpip_adapter_init();
+
+ ESP_ERROR_CHECK(esp_event_loop_create_default());
+
+ wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
+ ESP_ERROR_CHECK(esp_wifi_init(&cfg));
+
+ ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
+ ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
+
+ wifi_config_t wifi_config = {
+ .sta = {
+ .ssid = EXAMPLE_ESP_WIFI_SSID,
+ .password = EXAMPLE_ESP_WIFI_PASS
+ },
+ };
+
+ /* Setting a password implies station will connect to all security modes including WEP/WPA.
+ * However these modes are deprecated and not advisable to be used. Incase your Access point
+ * doesn't support WPA2, these mode can be enabled by commenting below line */
+
+ if (strlen((char *)wifi_config.sta.password))
+ wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
+
+ ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
+ ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
+ ESP_ERROR_CHECK(esp_wifi_start() );
+
+ ESP_LOGI(TAG, "wifi_init_sta finished.");
+
+ /* Waiting until either the connection is established (WIFI_CONNECTED_BIT). */
+ EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
+
+ /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
+ * happened. */
+ if (bits & WIFI_CONNECTED_BIT)
+ ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
+ else
+ ESP_LOGE(TAG, "UNEXPECTED EVENT");
+
+ ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler));
+ ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler));
+ vEventGroupDelete(s_wifi_event_group);
+}
+
+void wifista_main(void)
+{
+ ESP_ERROR_CHECK(nvs_flash_init());
+
+ ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
+ wifi_init_sta();
+}
tic2json TIC parser/converter
RSS Atom

AltStyle によって変換されたページ (->オリジナル) /