Skip to content

Commit 79180a9

Browse files
committed
Launcher: Added wifi test code for ftp/web server
But it adds 475K to the binary, we'd have to bump the firmware size to 3.5-4MB :(
1 parent 21940f2 commit 79180a9

File tree

6 files changed

+145
-3
lines changed

6 files changed

+145
-3
lines changed

base.sdkconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,4 @@ CONFIG_LOG_COLORS=y
145145
#
146146
# ODROID-GO partition table doesn't use any checksum
147147
CONFIG_PARTITION_TABLE_MD5=n
148+
CONFIG_APP_BUILD_BOOTLOADER=n

launcher/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
cmake_minimum_required(VERSION 3.5)
2-
set(COMPONENTS "main retro-go app_trace bootloader esptool_py")
2+
set(COMPONENTS "main retro-go wifi esp_http_server app_trace bootloader esptool_py")
33
include(../base.cmake)
44
project(launcher)

launcher/main/ftp_server.c

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#if 0
2+
#include <freertos/FreeRTOS.h>
3+
#include <tcpip_adapter.h>
4+
#include <esp_http_server.h>
5+
#include <esp_system.h>
6+
#include <esp_event.h>
7+
// #include <esp_netif.h>
8+
#include <esp_wifi.h>
9+
#include <esp_log.h>
10+
#include <string.h>
11+
#include <unistd.h>
12+
#include <assert.h>
13+
#include <netdb.h>
14+
#include <rg_system.h>
15+
16+
tcpip_adapter_ip_info_t ip_info;
17+
static char local_ipv4[16];
18+
static bool wifi_ready = false;
19+
20+
#define WIFI_SSID "RETRO-GO"
21+
#define WIFI_CHANNEL 12
22+
23+
24+
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
25+
{
26+
if (event_id == IP_EVENT_AP_STAIPASSIGNED) // A client connected to us
27+
{
28+
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ip_info);
29+
sprintf(local_ipv4, IPSTR, IP2STR(&ip_info.ip));
30+
wifi_ready = true;
31+
}
32+
else if (event_id == IP_EVENT_STA_GOT_IP) // We connected to an AP
33+
{
34+
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info);
35+
sprintf(local_ipv4, IPSTR, IP2STR(&ip_info.ip));
36+
wifi_ready = true;
37+
}
38+
}
39+
40+
41+
static esp_err_t http_handler(httpd_req_t *req)
42+
{
43+
if (req->method == HTTP_POST)
44+
{
45+
char content[100];
46+
size_t bytes = 0;
47+
48+
while (bytes < req->content_len)
49+
{
50+
int ret = httpd_req_recv(req, content, sizeof(content));
51+
if (ret < 0)
52+
{
53+
return ESP_FAIL;
54+
}
55+
bytes += ret;
56+
}
57+
}
58+
59+
httpd_resp_send(req, "Hello World!", HTTPD_RESP_USE_STRLEN);
60+
return ESP_OK;
61+
}
62+
63+
64+
static void ftp_server_task(void *arg)
65+
{
66+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
67+
wifi_mode_t mode = arg ? WIFI_MODE_STA : WIFI_MODE_AP;
68+
httpd_handle_t http_server = NULL;
69+
httpd_config_t http_config = HTTPD_DEFAULT_CONFIG();
70+
71+
wifi_ready = false;
72+
73+
tcpip_adapter_init();
74+
esp_event_loop_create_default();
75+
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
76+
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
77+
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
78+
ESP_ERROR_CHECK(esp_wifi_set_config(mode, &(wifi_config_t){
79+
.sta.ssid = WIFI_SSID,
80+
.sta.channel = WIFI_CHANNEL,
81+
}));
82+
ESP_ERROR_CHECK(esp_wifi_set_config(mode, &(wifi_config_t){
83+
.ap.ssid = WIFI_SSID,
84+
.ap.authmode = WIFI_AUTH_OPEN,
85+
.ap.channel = WIFI_CHANNEL,
86+
.ap.max_connection = 2,
87+
}));
88+
ESP_ERROR_CHECK(esp_wifi_set_mode(mode));
89+
ESP_ERROR_CHECK(esp_wifi_start());
90+
if (mode == WIFI_MODE_STA)
91+
ESP_ERROR_CHECK(esp_wifi_connect());
92+
93+
// Wait for Wifi to connect/start
94+
for (int i = 0; i < 1000 && !wifi_ready; i++)
95+
{
96+
vTaskDelay(pdMS_TO_TICKS(10));
97+
}
98+
99+
if (!wifi_ready)
100+
{
101+
RG_LOGE("Wifi init failure.\n");
102+
return;
103+
}
104+
105+
RG_LOGI("Wifi connected. Local IP = %s\n", local_ipv4);
106+
107+
if (httpd_start(&http_server, &http_config) == ESP_OK)
108+
{
109+
httpd_register_uri_handler(http_server, &(httpd_uri_t){"/", HTTP_GET, http_handler, 0});
110+
httpd_register_uri_handler(http_server, &(httpd_uri_t){"/", HTTP_POST, http_handler, 0});
111+
RG_LOGI("Web Server started!\n");
112+
}
113+
114+
while (1)
115+
{
116+
vTaskDelay(10);
117+
}
118+
119+
httpd_stop(http_server);
120+
}
121+
122+
void ftp_server_start(void)
123+
{
124+
ftp_server_task(1);
125+
}
126+
127+
void ftp_server_stop(void)
128+
{
129+
130+
}
131+
#else
132+
void ftp_server_start(void) {}
133+
void ftp_server_stop(void) {}
134+
#endif

launcher/main/ftp_server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
void ftp_server_start(void);
2+
void ftp_server_stop(void);

launcher/main/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <string.h>
66
#include <unistd.h>
77

8+
#include "ftp_server.h"
89
#include "emulators.h"
910
#include "bookmarks.h"
1011
#include "gui.h"
@@ -252,5 +253,7 @@ void app_main(void)
252253
emulators_init();
253254
bookmarks_init();
254255

256+
ftp_server_start();
257+
255258
retro_loop();
256259
}

retro-go.code-workspace

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"IS_LITTLE_ENDIAN",
4040
"INLINE=",
4141
"uint=unsigned int",
42-
"CONFIG_FREERTOS_HZ=100"
42+
"CONFIG_FREERTOS_HZ=100",
43+
"CONFIG_TCPIP_LWIP=1"
4344
],
4445
"C_Cpp.default.includePath": [
4546
"${workspaceFolder}/components/**",
@@ -60,7 +61,8 @@
6061
"gnuboy",
6162
"newfrendo",
6263
"newboy",
63-
"smsplusgx"
64+
"smsplusgx",
65+
"TCPIP"
6466
],
6567
"files.trimTrailingWhitespace": true,
6668
"files.exclude": {

0 commit comments

Comments
 (0)