Skip to content

Commit

Permalink
network: Add FTP Server
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryzee119 authored Apr 19, 2022
1 parent b9651a4 commit 4ef7682
Show file tree
Hide file tree
Showing 22 changed files with 4,099 additions and 382 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ file(GLOB PROJECT_FILES
"${PROJECT_DIR}/main.c"
"${PROJECT_DIR}/helpers/nano_debug.c"
"${PROJECT_DIR}/helpers/fileio.c"
"${PROJECT_DIR}/platform/platform_generic.c"
"${PROJECT_DIR}/platform/generic/platform.c"
"${PROJECT_DIR}/platform/generic/network.c"
"${PROJECT_DIR}/dash_resources/default_tbn.c"
"${PROJECT_DIR}/lvgl_drivers/lv_sdl_indev.c"
"${PROJECT_DIR}/lvgl_drivers/lv_sdl_disp.c"
Expand Down
15 changes: 12 additions & 3 deletions Makefile.nxdk
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
XBE_TITLE = LithiumX
GEN_XISO = $(XBE_TITLE).iso
NXDK_SDL = y
NXDK_NET = y

LVGL_DIR = src/lib/lvgl
LV_CONF_PATH = "lv_conf.h"
Expand All @@ -16,12 +17,18 @@ SRCS += \
$(CURDIR)/src/dash_titlelist.c \
$(CURDIR)/src/helpers/fileio.c \
$(CURDIR)/src/helpers/nano_debug.c \
$(CURDIR)/src/platform/platform_xbox.c \
$(CURDIR)/src/platform/xbox/platform.c \
$(CURDIR)/src/platform/xbox/network.c \
$(CURDIR)/src/dash_resources/default_tbn.c \
$(CURDIR)/src/lib/jpg_decoder/jpg_decoder.c \
$(CURDIR)/src/lib/xml/src/xml.c \
$(CURDIR)/src/lvgl_drivers/lv_sdl_disp.c) \
$(CURDIR)/src/lvgl_drivers/lv_sdl_indev.c)
$(CURDIR)/src/lvgl_drivers/lv_sdl_indev.c) \
$(CURDIR)/src/lib/nvnetdrv/nvnetdrv.c \
$(CURDIR)/src/lib/nvnetdrv/nvnetdrv_lwip.c \
$(CURDIR)/src/lib/ftpd/ftp_file.c \
$(CURDIR)/src/lib/ftpd/ftp_server.c \
$(CURDIR)/src/lib/ftpd/ftp.c \

CFLAGS += \
-I$(CURDIR)/src \
Expand All @@ -36,7 +43,9 @@ CFLAGS += \
-DLVGL_USE_CUSTOM_CONTROLLER_MAP \
-DDEBUG_CONSOLE \
-DBUILD_VERSION=\"$(GIT_COMMIT_HASH)\" \
-DQRURL=\"https://github.com/Ryzee119/LithiumX\"
-DQRURL=\"https://github.com/Ryzee119/LithiumX\" \
-DFTP_CUSTOM_ROOT_PATH=\"X:\\\\LithiumX\\\\ftp_root\" \
-DDBG
#-DDBG_I2C
#-DDBG

Expand Down
147 changes: 147 additions & 0 deletions src/lib/ftpd/ftp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
FTP Server for STM32-E407 and ChibiOS
Copyright (C) 2015 Jean-Michel Gallego
See readme.txt for information
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "ftp.h"
#include "ftp_server.h"
#include "ftp_file.h"
#include "lwip/opt.h"
#include "lwip/api.h"
#include <string.h>
#include <stdlib.h>

// static variables
static const char *no_conn_allowed = "421 No more connections allowed\r\n";
static server_stru_t ftp_links[FTP_NBR_CLIENTS];

// single ftp connection loop
static void ftp_task(void *param)
{
// sanity check
if (param == NULL)
return;

// parse parameter
server_stru_t *ftp = (server_stru_t *)param;

// save the instance number
ftp->ftp_data.ftp_con_num = ftp->number;

// feedback
FTP_PRINTF("FTP %d connected\r\n", ftp->number);

// service FTP server
ftp_service(ftp->ftp_connection, &ftp->ftp_data);

// delete the connection.
netconn_delete(ftp->ftp_connection);

// reset the socket to be sure
ftp->ftp_connection = NULL;

// feedback
FTP_PRINTF("FTP %d disconnected\r\n", ftp->number);

// clear handle
ftp->task_handle = NULL;
}

static void ftp_start_task(server_stru_t *data, uint8_t index)
{
// set number
data->number = index;

// start task with parameter
sys_thread_new(data->task_name, ftp_task, data, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
FTP_PRINTF("%s started\r\n\n\n", data->task_name);
}

// ftp server task
void ftp_server(void)
{
struct netconn *ftp_srv_conn;
struct netconn *ftp_client_conn;
uint8_t index = 0;

// Create the TCP connection handle
ftp_srv_conn = netconn_new(NETCONN_TCP);

// feedback
if (ftp_srv_conn == NULL)
{
// error
FTP_PRINTF("Failed to create socket\r\n");

// go back
return;
}

// Bind to port 21 (FTP) with default IP address
netconn_bind(ftp_srv_conn, NULL, FTP_SERVER_PORT);

// put the connection into LISTEN state
netconn_listen(ftp_srv_conn);

while (1)
{
// Wait for incoming connections
if (netconn_accept(ftp_srv_conn, &ftp_client_conn) == ERR_OK)
{
// Look for the first unused connection
for (index = 0; index < FTP_NBR_CLIENTS; index++)
{
if (ftp_links[index].ftp_connection == NULL && ftp_links[index].task_handle == NULL)
break;
}

// all connections in use?
if (index >= FTP_NBR_CLIENTS)
{
// tell that no connections are allowed
netconn_write(ftp_client_conn, no_conn_allowed, strlen(no_conn_allowed), NETCONN_COPY);

// delete the connection.
netconn_delete(ftp_client_conn);

// reset the socket to be sure
ftp_client_conn = NULL;

// feedback
FTP_PRINTF("FTP connection denied, all connections in use\r\n");

}
// not all connections in use
else
{
// copy client connection
ftp_links[index].ftp_connection = ftp_client_conn;

// zero out client connection
ftp_client_conn = NULL;

snprintf(ftp_links[index].task_name, 12, "ftp_task_%d", index);

// try and start the FTP task for this connection
ftp_start_task(&ftp_links[index], index);
}
}
}

// delete the connection.
netconn_delete(ftp_srv_conn);
}
72 changes: 72 additions & 0 deletions src/lib/ftpd/ftp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
FTP Server for STM32-E407 and ChibiOS
Copyright (C) 2015 Jean-Michel Gallego
See readme.txt for information
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef _FTPS_H_
#define _FTPS_H_

#include "lwip/opt.h"
#include "lwip/api.h"
#include "ftp_server.h"

// stack size for ftp task
#define FTP_TASK_STACK_SIZE 1536

// initial FTP port
#define FTP_SERVER_PORT 21

// Data port in passive mode
#define FTP_DATA_PORT 55600

// number of clients we want to serve simultaneously, same as netbuf limit
#define FTP_NBR_CLIENTS 2

#define FTP_CONN_DEBUG(ftp, f, ...) printf("[%d] " f, ftp->ftp_con_num, ##__VA_ARGS__)
#define FTP_PRINTF printf

// define a structure of parameters for a ftp thread
typedef struct
{
uint8_t number;
struct netconn *ftp_connection;
sys_thread_t *task_handle;
ftp_data_t ftp_data;
char task_name[12];
} server_stru_t;

/**
* Start the FTP server.
*
* This code creates a socket on port 21 to listen for incoming
* FTP client connections. If this creation fails the code returns
* Immediately. If the socket is created the task continues.
*
* The task loops indefinitely and waits for connections. When a
* connection is found a port is assigned to the incoming client.
* A separate task is started for each connection which handles
* The FTP commands. When the client disconnects the task is
* stopped.
*
* An incoming connection is denied when:
* - The memory on the CMS is not available
* - The maximum number of clients is connected
* - The application is running
*/
void ftp_server(void);

#endif // _FTPS_H_
Loading

0 comments on commit 4ef7682

Please sign in to comment.