FTPServer.c File Reference

FTP server example. More...

#include <BoardSupport/inc/stdtypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <inttypes.h>
#include <time.h>
#include <string.h>
#include <Libs/NETlib/net.h>
#include <Libs/NETlib/ftplib.h>
#include <BoardSupport/inc/BoardSpecific.h>
#include <Common/Common.h>
#include <Common/uartio.h>
#include <Common/timer.h>
#include <Common/CPrintf.h>
#include <BoardSupport/config/netconfig.c>
#include "efs.def"

Functions

uint16_t FTP_CheckClients (void)
 
void FTP_PrintResult (ftp_return_type *ftp_ret)
 
int main (void)
 

Variables

char * program_name = "FTP-server"
 
char * _datetime = __DATE__" "__TIME__
 
uint32_t boot_address = 0
 

Detailed Description

                          _         _             _
                       __| |    ___(_) ____ _ __ | |_
                      / _` |   / __| |/ _` | '_ \| __|
                     | (_| | _ \__ \ | (_| | | | | |_
                      \__,_|(_) ___/_|\__, |_| |_|\__|
                     Signalprocessing |___/ Technology
Author
D.SignT GmbH & Co. KG, Claus Hermbusche
Date
2019-06-03

This program acts as a FTP server (port 21) with either fix IP 192.168.168.200 (configurable in netconfig.c) or DHCP assigned address (e.g. 192.168.168.143, host name mydemo).

To verify this program, first 'ping' the DSP:

>ping 192.168.168.200

or, if configured for DHCP

>ping mydemo

If the settings and network connections are correct, the pings will be replied.

In this application 3 users are configured for FTP access:

user: admin     password: xxxx
user: guest     password: -
user: anonymous password: -

Use a FTP client to access the configured directories. Examples for defining users, directories and files are shown below.

Dedicated FTP clients (like FileZilla or WsFTP) allow to configure different users and different passwords. Opposed to that kind of clients the browser built-in clients allow to configure only one account (usually the anonymous or guest account). Use the following address in your browser to access this server as admin:

ftp://admin@192.168.168.200

or

ftp://admin@mydemo

enter in the new opened dialogue window the admin password 'xxxx' to access the FTP server.

Important note for F28xx users: activate compiler define "USE_FLASH_API" for this application, otherwise FLASH uploads wont be possible.

Function Documentation

uint16_t FTP_CheckClients ( void  )
Examples:
FTPServer.c.
193 {
194  /***************************************************************************
195  locals
196  ***************************************************************************/
197  static uint32_t clients;
198  uint32_t ret;
199 
200  /***************************************************************************
201  get current connections
202  ***************************************************************************/
203  ret = ftp_is_server_connected();
204 
205  if (ret != clients)
206  {
207  /***********************************************************************
208  connection status changed
209  ***********************************************************************/
210  clients = ret;
211  CPrintf ("\r\n connection status changed: %"PRId32" client(s) connected\r\n", clients);
212 
213 
214  /***********************************************************************
215  activate to monitor dynamic memory utilization
216  ***********************************************************************/
217 // memmap (initial_heap_size);
218  return (1);
219  }
220  return (0);
221 }
int CPrintf(const char *_format,...)
Custom printf function.
Definition: cprintf.c:708
unsigned int uint32_t
Definition: stdint.h:47
uint32_t ftp_is_server_connected(void)
Test if clients are connected.
void FTP_PrintResult ( ftp_return_type ftp_ret)
Examples:
FTPServer.c.
231 {
232  /***************************************************************************
233  locals
234  ***************************************************************************/
235  char buffer[25]; /* small buffer for conversion */
236  direntry_type *fp; /* EFS file pointer */
237 
238  switch ( ftp_ret-> reason )
239  {
240  case FTP_STOR_CMD :
241  CPuts (" File upload... ");
242  break;
243 
244  case FTP_BOOT_CMD :
245  CPuts (" Boot command... ");
246  // check boot address
247  if (ftp_ret-> offset >= 0x30000)
248  boot_address = ftp_ret-> offset;
249  else
250  ftp_ret-> error_code = FSYS_OUT_OF_RANGE_ERROR;
251  return;
252 
253  case FTP_DELE_CMD :
254  CPuts (" Delete command... ");
255  break;
256 
257  case FTP_PASS_CMD :
258  CPrintf (" New connection established from %s\r\n", inet_ntoa(ftp_ret-> offset, buffer));
259  return;
260 
261  case FTP_QUIT_CMD :
262  return;
263 
264  // case TELNET_DOWNLOAD :
265  default:
266  break;
267  }
268 
269  /***************************************************************************
270  further details on event
271  ***************************************************************************/
272  switch ( ftp_ret-> error_code )
273  {
274  case FTP_NO_ERROR:
275  /*******************************************************************
276  success
277  *******************************************************************/
278  fp = ftp_ret-> fp;
279  if (fp==NULL) break; /* no details available */
280  CPrintf (VT100_GREEN" successful\r\n"VT100_DEFAULT);
281  if (ftp_ret-> offset==0)
282  {
283  if (!strncmp(fp->dir,"0:",2))
284  {
285  CPuts (" RAM-disc:\r\n");
286  }
287  if (!strncmp(fp->dir,"1:",2))
288  {
289  CPuts (" NAND-disc:\r\n");
290  }
291 #ifdef USE_SPIDRIVE
292  if (!strncmp(fp->dir,"2:",2))
293  {
294  CPuts (" SD-card:\r\n");
295  }
296 #endif
297  }
298  else
299  {
300  CPrintf (" EFS offset: 0x%08"PRIx32"\r\n", ftp_ret-> offset);
301  }
302  CPrintf (" file name: %s\r\n", fp-> name);
303  CPrintf (" file size: %"PRId32"\r\n", ftp_ret-> fp-> size);
304  break;
305 
306  default:
307  /*******************************************************************
308  print error
309  *******************************************************************/
310  net_print_error (ftp_ret-> error_code, CPrintf);
311  break;
312  }
313 }
#define FSYS_OUT_OF_RANGE_ERROR
Definition: net.h:896
char * inet_ntoa(uint32_t i_addr, char *s)
Convert IP-address from 0xbbaaddcc to "aaa.bbb.ccc.ddd".
#define FTP_DELE_CMD
Definition: ftplib.h:138
char dir[MAX_NAME_LEN]
Definition: net.h:798
#define NULL
Definition: net.h:126
Embedded File System directory.
Definition: net.h:796
int CPrintf(const char *_format,...)
Custom printf function.
Definition: cprintf.c:708
#define VT100_DEFAULT
Definition: cprintf.h:150
#define FTP_BOOT_CMD
Definition: ftplib.h:141
#define FTP_PASS_CMD
Definition: ftplib.h:137
static char buffer[100]
Definition: blocksend.c:158
#define VT100_GREEN
Definition: cprintf.h:144
#define FTP_STOR_CMD
Definition: ftplib.h:140
#define FTP_QUIT_CMD
Definition: ftplib.h:139
uint32_t boot_address
Definition: FTPServer.c:175
int CPuts(const char *_ptr)
Definition: cprintf.c:399
#define FTP_NO_ERROR
Definition: ftplib.h:73
int main ( void  )
Examples:
FTPServer.c.
324 {
325  /***************************************************************************
326  locals
327  ***************************************************************************/
328  ftp_return_type *ftp_ret; /* FTP server result */
329  int main_loop = 1; /* main loop switch, set to 0 to exit */
330  timeval stamp1, stamp2, delta; /* used to determine startup time */
331  uint32_t ftp_serv;
332 
333 
334  /***************************************************************************
335  initialize application (e.g. timer clocks, PLL settings, EMIF etc.)
336  (ref. \Common\Common.c)
337  ***************************************************************************/
338  AppInit (GET_CLOCK);
339 
340  /***************************************************************************
341  select output device for CPrintf (ref. \Common\cprintf.c)
342  possible settings:
343  CPRINTF_UART_OUTPUT -> output to UART
344  CPRINTF_CCS_OUTPUT -> output to CCS
345  CPRINTF_UART_OUTPUT | CPRINTF_CCS_OUTPUT -> output to UART and CCS
346  ***************************************************************************/
347  CPrintf_select_output (CPRINTF_DEFAULT_OUTPUT); /* default outputs */
348 
349  /***************************************************************************
350  print a start up message
351  ***************************************************************************/
352  START_UP_MESSAGE (BLANK_REV NETLIB_REV);
353 
354  /**************************************************************************/
355  // CPrintfProgress (" Heap check ");
356  // at least 0x8000 bytes required for this app
357  /**************************************************************************/
358  ASSERT_HEAP (initial_heap_size, 0x8000);
359  // CPrintfProgressSuccess();
360 
361  /**************************************************************************/
362  CPrintfProgress (" Setup system time ");
363  // 1 milli seconds resolution
364  /**************************************************************************/
367  CPrintf (" *** timer %d mapped to CPU int %d ***\r\n",
369 
370  /**************************************************************************/
371  CPrintfProgress (" Enable interrupts ");
372  /**************************************************************************/
375 
376  /**************************************************************************/
377  CPrintfProgress (" Start system timer ");
378  /**************************************************************************/
379  StartSystemTimer ();
381  CPrintf (" *** timer %d running at %"PRId32" Hz ***\r\n", SystemTimerDev, RES_SECONDS/GetSystemTimerRes());
382 
383  /***************************************************************************
384  measure network initialization time
385  ***************************************************************************/
386  stamp1 = GetTimeStamp();
387 
388  /**************************************************************************/
389  CPrintfProgress (" Initialize network ");
390  /**************************************************************************/
391  InitializeNetwork (64); // 64 bytes for ping
392 
393  /***************************************************************************
394  initialize ftp server; parameter is the specified user list
395  ***************************************************************************/
396  ftp_serv = ftp_server_init ( user, /* specified user list */
397  NULL, /* mmc support */
398  FTP_NO_LIMIT); /* max parallel connections */
399  /* 0 == no limit (heap limit only) */
400 
401  if ( ftp_serv == FALSE )
402  {
403  prg_exit ("ftp_server_init() failed"); /* possibly insufficient heap */
404  }
405 
406  /***************************************************************************
407  activate passive transfer mode. useful for clients behind a firewall.
408  this may be a security issue. if your network administrator does not
409  allow the passive mode, deactivate the following line.
410  ***************************************************************************/
411  // ftp_set_passive_mode (1);
412 
413  /***************************************************************************
414  define a connection time-out for FTP server
415  ***************************************************************************/
416  ftp_server_timeout (FTP_SERVER_TIMEOUT); /* appr. 5 minutes */
418 
419  stamp2 = GetTimeStamp();
420 
421  tv_interval (&delta, &stamp1, &stamp2);
422  CPuts (" network startup time [sec]: ");
424  "%"PRId32".%03"PRId32"\r\n"
426  delta.tv_sec,
427  delta.tv_usec/1000);
428 
429  CPuts ("\r\n 3 different users have FTP access:\r\n\n");
430  CPuts (" user: admin password: xxxx\r\n");
431  CPuts (" user: guest password: -\r\n");
432  CPuts (" user: anonymous password: -\r\n\n");
433 
434  /***************************************************************************
435  entering endless loop...
436  ***************************************************************************/
437  CPrintf (" %s running: waiting for connection...\r\n", program_name);
438 
439  /***************************************************************************
440  main program loop: set main_loop to 0 to exit loop
441  ***************************************************************************/
442  while ( main_loop )
443  {
444  /***********************************************************************
445  try to detect IP assignment
446  if DHCP is used, the assigned IP address may change
447  ***********************************************************************/
449 
450  /***********************************************************************
451  process net_isq()
452  ***********************************************************************/
453  net_isq ();
454 
455  /***********************************************************************
456  monitor link status
457  ***********************************************************************/
459 
460  /***********************************************************************
461  check client connections
462  ***********************************************************************/
464 
465  /***********************************************************************
466  process ftp_server()
467  ***********************************************************************/
468  ftp_ret = ftp_server ();
469 
470  if ((uint32_t)ftp_ret > FTP_NO_ERROR)
471  {
472  /*******************************************************************
473  print result
474  *******************************************************************/
475  CPuts (CRLF);
476  FTP_PrintResult (ftp_ret);
477  if (boot_address)
478  {
479  main_loop = 0;
480  }
481  }
482 
483  /***********************************************************************
484  show that the program is running, perform symbol animation
485  ***********************************************************************/
487  }
488 
489  if (boot_address)
490  {
491  AppCleanup();
492  // DM2_delay (1000);
493  // DM2_bootload (boot_address);
494  }
495 
496  /***************************************************************************
497  exit program, shut down peripherals
498  ***************************************************************************/
499  return (0);
500 }
#define prg_exit(s)
Definition: Common.h:267
void BoardEnableInterrupts(void)
global enable interrupts
Definition: BoardSpecific.c:365
#define ANIMATE_SYMBOLS_COUNT
Definition: BoardSpecific.h:268
void StartSystemTimer(void)
start system timer
Definition: timer.c:447
#define RES_MSECONDS
Definition: timer.h:67
uint16_t CPrintf_select_output(uint16_t device)
Definition: cprintf.c:206
#define ASSERT_HEAP(i, h)
Definition: Common.h:262
#define FTP_NO_LIMIT
Definition: ftplib.h:68
FTP server return structure.
Definition: ftplib.h:134
#define NULL
Definition: net.h:126
ftp_return_type * ftp_server(void)
The main polling function for concurrent FTP server.
time_t GetSystemTimerRes(void)
Definition: timer.c:160
#define GET_CLOCK
Definition: BoardSpecific.h:258
int CPrintf(const char *_format,...)
Custom printf function.
Definition: cprintf.c:708
#define VT100_DEFAULT
Definition: cprintf.h:150
void FTP_PrintResult(ftp_return_type *ftp_ret)
Definition: FTPServer.c:230
void SetupSystemTime(int32_t cpuint, int port, time_t resolution)
Setup System Time.
Definition: timer.c:392
void AppInit(uint32_t dsp_clock)
Initialize application.
Definition: Common.c:230
#define CRLF
Definition: cprintf.h:214
uint32_t initial_heap_size
Definition: Common.c:140
uint16_t FTP_CheckClients(void)
Definition: FTPServer.c:192
void ftp_server_timeout(uint32_t timeout)
Set a FTP server timeout for inactive connection.
#define CPRINTF_DEFAULT_OUTPUT
Definition: BoardSpecific.h:129
user_type user[]
Definition: FTPclient.c:168
#define VT100_RED
Definition: cprintf.h:143
uint16_t CPrintAnimatedSymbol(char *c, time_t period, size_t size)
Definition: cprintf.c:816
int32_t ftp_server_init(user_type *user, void *(*fat_init_ftp)(void), int32_t max_connections)
Initialize FTP server.
int InitializeNetwork(uint16_t icmp_size)
Initialize MAC, sockets and protocols.
Definition: BoardSpecific.c:597
unsigned int uint32_t
Definition: stdint.h:47
char sym_animate[]
Definition: BoardSpecific.c:129
Definition: timer.h:75
#define GetTimeStamp()
Definition: timer.h:81
void tv_interval(timeval *e, timeval *t1, timeval *t2)
compute elapsed time
Definition: timer.c:336
char * program_name
Definition: FTPServer.c:168
#define SYSTEM_TIMER_INT
Definition: BoardSpecific.h:174
#define SYSTEM_TIMER
Definition: BoardSpecific.h:167
void net_isq(void)
The main polling function for processing sockets, must be periodically called in the main application...
#define BLANK_REV
Definition: BoardSpecific.h:191
Uint16 SystemTimerDev
uint16_t monitor_ip_address(tpOutputFunc pLog)
monitor IP assignment
Definition: BoardSpecific.c:545
uint32_t boot_address
Definition: FTPServer.c:175
#define START_UP_MESSAGE(rev)
Definition: BoardSpecific.h:192
void AppCleanup(void)
Disable interrupts, DMA, timer etc.
Definition: Common.c:290
#define RES_SECONDS
Definition: timer.h:70
uint16_t monitor_link_status(tpOutputFunc pLog)
monitor link status change
Definition: BoardSpecific.c:497
int CPuts(const char *_ptr)
Definition: cprintf.c:399
time_t tv_sec
Definition: timer.h:77
#define FTP_NO_ERROR
Definition: ftplib.h:73
#define CPrintfProgress(s)
Definition: cprintf.h:230
#define FTP_SERVER_TIMEOUT
Definition: BoardSpecific.h:235
time_t tv_usec
Definition: timer.h:78
#define CPrintfProgressSuccess()
Definition: cprintf.h:263

Variable Documentation

char* program_name = "FTP-server"
Examples:
FTPServer.c.
char* _datetime = __DATE__" "__TIME__
Examples:
FTPServer.c.
uint32_t boot_address = 0
Examples:
FTPServer.c.