FTPclient.c File Reference

FTP client example. More...

#include <BoardSupport/inc/stdtypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <inttypes.h>
#include <time.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>

Macros

#define FTP_DOWNLOAD   UINT32_C(1)
 
#define FTP_UPLOAD   UINT32_C(2)
 

Functions

int32_t FTP_receive_file (void)
 
int32_t FTP_send_file (void)
 
int main (void)
 

Variables

char * program_name = "FTP-client"
 
char data_buffer [FTP_BUFFER_SIZE]
 
uint32_t action = UINT32_C(0)
 
static char ftp_server_ip_addr [] = "192.168.168.8"
 
user_type user []
 

Detailed Description

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

Macro Definition Documentation

#define FTP_DOWNLOAD   UINT32_C(1)
Examples:
FTPClient.c.
#define FTP_UPLOAD   UINT32_C(2)
Examples:
FTPClient.c.

Function Documentation

int32_t FTP_receive_file ( void  )
Examples:
FTPClient.c.
189 {
190  /***************************************************************************
191  locals
192  ***************************************************************************/
193  int32_t ret = FALSE; /* return code from server */
194  FTP_client_type *FTP_client; /* FTP client connection */
195 
196  /***************************************************************************
197  get FTP client. Parameter is FTP server IP address and login user
198  ***************************************************************************/
199  FTP_client = FTP_get_control (ftp_server_ip_addr, user);
200 
201  if ( FTP_client == NULL )
202  {
203  /***********************************************************************
204  out of memory
205  ***********************************************************************/
206  CPuts ( VT100_RED
207  "Out of memory error\r\n"
208  VT100_DEFAULT);
209  return (FALSE);
210  }
211 
212  /***************************************************************************
213  try to connect
214  ***************************************************************************/
215  CPuts ( "Connecting..." );
216  switch ( FTP_connect ( FTP_client, CONNECT_TIMEOUT) )
217  {
218  case TRUE: // success
219  /*******************************************************************
220  connection successful-> try to login
221  *******************************************************************/
222  if ( FTP_server_login (FTP_client) == FTP_LOGIN_OK )
223  {
224  /***************************************************************
225  Login successful
226  ***************************************************************/
228 
229  /***************************************************************
230  set file buffer for received data
231  ***************************************************************/
232  FTP_set_file_buffer ( FTP_client,
233  data_buffer,
235 
236  /***************************************************************
237  retrieve file
238  check syntax of file name, some servers need a slash
239  "/ftproot/readme.txt"
240  ***************************************************************/
241  ret = FTP_retrieve_file (FTP_client, "\\ftproot\\readme.txt", 'A');
242 
243  /***************************************************************
244  check return code
245  ***************************************************************/
246  switch ( ret )
247  {
248  case FTP_TRANSFER_OK:
249  CPrintf ( "Retrieving readme.txt ( %"PRId32" bytes) successful\r\n", FTP_client-> file_size );
250  break;
251 
253  CPuts ( "Error: Port in use\r\n");
254  break;
255 
257  CPuts ( "Error: Invalid path\r\n");
258  break;
259 
260  default :
261  CPuts ( "Retrieving readme.txt failure\r\n" );
262  break;
263  } // switch
264 
265  /***************************************************************
266  don't forget to logout
267  ***************************************************************/
268  FTP_server_logout (FTP_client);
269  }
270  else
271  {
272  CPuts( "FTP server login failed\r\n" );
273  }
274  break;
275 
276  default : // error
277  CPuts ("Error connecting FTP server:\r\n");
278  net_print_error (FTP_client-> control -> error_code, CPrintf);
279  break;
280  } // switch
281 
282  /***************************************************************************
283  free FTP client
284  ***************************************************************************/
285  FTP_free_control (FTP_client);
286 
287  return (ret);
288 }
static char ftp_server_ip_addr[]
Definition: FTPclient.c:157
#define FTP_BUFFER_SIZE
Definition: BoardSpecific.h:218
Used for information about current FTP connection.
Definition: ftplib.h:113
int32_t FTP_server_login(FTP_client_type *FTP_client)
Perform FTP server login.
FTP_client_type * FTP_get_control(char *ftp_server, user_type *user)
Allocate FTP client structure and control buffer.
int32_t FTP_retrieve_file(FTP_client_type *FTP_client, char *file_name, char type)
Retrieve a file from FTP server.
#define FTP_TRANSFER_INVALID
Definition: ftplib.h:90
#define NULL
Definition: net.h:126
int CPrintf(const char *_format,...)
Custom printf function.
Definition: cprintf.c:708
#define VT100_DEFAULT
Definition: cprintf.h:150
int32_t FTP_connect(FTP_client_type *FTP_client, int32_t connect_timeout)
Connect to FTP server.
user_type user[]
Definition: FTPclient.c:168
#define VT100_RED
Definition: cprintf.h:143
int32_t FTP_set_file_buffer(FTP_client_type *FTP_client, char *buffer, int32_t size)
Set user buffer for file transfer.
int32_t FTP_free_control(FTP_client_type *FTP_client)
Close control structure and free all associated buffers.
#define FTP_TRANSFER_PORT_IN_USE
Definition: ftplib.h:89
int int32_t
Definition: stdint.h:46
#define FTP_LOGIN_OK
Definition: ftplib.h:83
char data_buffer[FTP_BUFFER_SIZE]
Definition: FTPclient.c:145
int32_t FTP_server_logout(FTP_client_type *FTP_client)
Logout from FTP server.
int CPuts(const char *_ptr)
Definition: cprintf.c:399
#define CONNECT_TIMEOUT
Definition: BoardSpecific.h:240
#define CPrintfProgressSuccess()
Definition: cprintf.h:263
#define FTP_TRANSFER_OK
Definition: ftplib.h:88
int32_t FTP_send_file ( void  )
Examples:
FTPClient.c.
299 {
300  /***************************************************************************
301  locals
302  ***************************************************************************/
303  int32_t ret = FALSE; /* return code from server */
304  FTP_client_type *FTP_client; /* FTP client connection */
305 
306  /***************************************************************************
307  get FTP client. Parameter is FTP server IP address and login user
308  ***************************************************************************/
309  FTP_client = FTP_get_control(ftp_server_ip_addr, user);
310 
311  if ( FTP_client == NULL )
312  {
313  /***********************************************************************
314  out of memory
315  ***********************************************************************/
316  CPuts ( VT100_RED
317  "Out of memory error\r\n"
318  VT100_DEFAULT);
319  return (FALSE);
320  }
321 
322  /***************************************************************************
323  try to connect
324  ***************************************************************************/
325  CPuts( "Connecting..." );
326  switch ( FTP_connect ( FTP_client, CONNECT_TIMEOUT) )
327  {
328  case TRUE: // success
329  /*******************************************************************
330  connection successful-> try to login
331  *******************************************************************/
332  if ( FTP_server_login (FTP_client) == FTP_LOGIN_OK)
333  {
334  /***************************************************************
335  Login successful
336  ***************************************************************/
338 
339  /***************************************************************
340  set file buffer for received data
341  ***************************************************************/
342  FTP_set_file_buffer ( FTP_client,
343  data_buffer,
345 
346  /***************************************************************
347  send file
348  check syntax of file name, some servers need a slash
349  "/ftproot/logfile"
350  ***************************************************************/
351  ret = FTP_store_file (FTP_client, "\\ftproot\\logfile.bin", 'I');
352 
353  /***************************************************************
354  check return code
355  ***************************************************************/
356  switch ( ret )
357  {
358  case FTP_TRANSFER_OK:
359  CPrintf( "sending logfile.bin ( %"PRId32" bytes) successful\r\n", FTP_client-> file_size );
360  break;
361 
363  CPuts ( "Error: Port in use\r\n");
364  break;
365 
367  CPuts ( "Error: Invalid path\r\n");
368  break;
369 
370  default :
371  CPuts ( "sending logfile.bin failure\r\n" );
372  break;
373  } // switch
374 
375  /***************************************************************
376  don't forget to logout
377  ***************************************************************/
378  FTP_server_logout (FTP_client);
379  }
380  else
381  {
382  CPuts ( "FTP server login failed\r\n" );
383  }
384  break;
385 
386  default : // error
387  CPuts ( "Error connecting FTP server:\r\n");
388  net_print_error (FTP_client-> control -> error_code, CPrintf);
389  break;
390  } // switch
391 
392  /***************************************************************************
393  free FTP client
394  ***************************************************************************/
395  FTP_free_control (FTP_client);
396 
397  return (ret);
398 }
static char ftp_server_ip_addr[]
Definition: FTPclient.c:157
#define FTP_BUFFER_SIZE
Definition: BoardSpecific.h:218
Used for information about current FTP connection.
Definition: ftplib.h:113
int32_t FTP_server_login(FTP_client_type *FTP_client)
Perform FTP server login.
FTP_client_type * FTP_get_control(char *ftp_server, user_type *user)
Allocate FTP client structure and control buffer.
#define FTP_TRANSFER_INVALID
Definition: ftplib.h:90
#define NULL
Definition: net.h:126
int CPrintf(const char *_format,...)
Custom printf function.
Definition: cprintf.c:708
#define VT100_DEFAULT
Definition: cprintf.h:150
int32_t FTP_connect(FTP_client_type *FTP_client, int32_t connect_timeout)
Connect to FTP server.
user_type user[]
Definition: FTPclient.c:168
#define VT100_RED
Definition: cprintf.h:143
int32_t FTP_set_file_buffer(FTP_client_type *FTP_client, char *buffer, int32_t size)
Set user buffer for file transfer.
int32_t FTP_free_control(FTP_client_type *FTP_client)
Close control structure and free all associated buffers.
#define FTP_TRANSFER_PORT_IN_USE
Definition: ftplib.h:89
int int32_t
Definition: stdint.h:46
#define FTP_LOGIN_OK
Definition: ftplib.h:83
char data_buffer[FTP_BUFFER_SIZE]
Definition: FTPclient.c:145
int32_t FTP_server_logout(FTP_client_type *FTP_client)
Logout from FTP server.
int CPuts(const char *_ptr)
Definition: cprintf.c:399
int32_t FTP_store_file(FTP_client_type *FTP_client, char *file_name, char type)
Send file to FTP server.
#define CONNECT_TIMEOUT
Definition: BoardSpecific.h:240
#define CPrintfProgressSuccess()
Definition: cprintf.h:263
#define FTP_TRANSFER_OK
Definition: ftplib.h:88
int main ( void  )
Examples:
Blocksend.c, Chat.c, DHCPTest.c, DNSTest.c, Echo.c, FTPClient.c, Multicast.c, Ping.c, Ping2.c, PServer.c, Send.c, SNTPTest.c, and Telnet.c.
409 {
410  /***************************************************************************
411  locals
412  ***************************************************************************/
413  int main_loop = 1; /* main loop switch, set to 0 to exit */
414  volatile uint32_t ret; /* FTP server result */
415  timeval stamp1, stamp2, delta; /* used to determine startup time */
416 
417 #ifdef USE_UART_CONTROL
418  char c;
419 #endif
420 
421  /***************************************************************************
422  initialize application (e.g. timer clocks, PLL settings, EMIF etc.)
423  (ref. \Common\Common.c)
424  ***************************************************************************/
425  AppInit (GET_CLOCK);
426 
427  /***************************************************************************
428  select output device for CPrintf (ref. \Common\cprintf.c)
429  possible settings:
430  CPRINTF_UART_OUTPUT -> output to UART
431  CPRINTF_CCS_OUTPUT -> output to CCS
432  CPRINTF_UART_OUTPUT | CPRINTF_CCS_OUTPUT -> output to UART and CCS
433  ***************************************************************************/
434  CPrintf_select_output (CPRINTF_DEFAULT_OUTPUT); /* default outputs */
435 
436  /***************************************************************************
437  print a start up message
438  ***************************************************************************/
439  START_UP_MESSAGE (BLANK_REV NETLIB_REV);
440 
441  /**************************************************************************/
442  // CPrintfProgress (" Heap check ");
443  // at least 0x8000 bytes required for this app
444  /**************************************************************************/
445  ASSERT_HEAP (initial_heap_size, 0x8000);
446  // CPrintfProgressSuccess();
447 
448  /**************************************************************************/
449  CPrintfProgress (" Setup system time ");
450  // 1 milli seconds resolution
451  /**************************************************************************/
454  CPrintf (" *** timer %d mapped to CPU int %d ***\r\n",
456 
457  /**************************************************************************/
458  CPrintfProgress (" Enable interrupts ");
459  /**************************************************************************/
462 
463  /**************************************************************************/
464  CPrintfProgress (" Start system timer ");
465  /**************************************************************************/
466  StartSystemTimer ();
468  CPrintf (" *** timer %d running at %"PRId32" Hz ***\r\n", SystemTimerDev, RES_SECONDS/GetSystemTimerRes());
469 
470  /***************************************************************************
471  measure network initialization time
472  ***************************************************************************/
473  stamp1 = GetTimeStamp();
474 
475  /**************************************************************************/
476  CPrintfProgress (" Initialize network ");
477  /**************************************************************************/
478  InitializeNetwork (64); // 64 bytes for ping
480 
481  stamp2 = GetTimeStamp();
482 
483  tv_interval (&delta, &stamp1, &stamp2);
484  CPuts (" network startup time [sec]: ");
486  "%"PRId32".%03"PRId32"\r\n"
488  delta.tv_sec,
489  delta.tv_usec/UINT32_C(1000));
490 
491 #ifdef USE_UART_CONTROL
492  CPrintf (" press <"VT100_BLUE"1"VT100_DEFAULT"> to retrieve a file from FTP server (%s)\r\n", ftp_server_ip_addr);
493  CPrintf (" press <"VT100_BLUE"2"VT100_DEFAULT"> to send a file to FTP server (%s)\r\n", ftp_server_ip_addr);
494 #else
495  CPuts ("\r\n use test.gel to set action\r\n");
496  CPrintf (" enter <1> to retrieve a file from FTP server (%s)\r\n", ftp_server_ip_addr);
497  CPrintf (" enter <2> to send a file to FTP server (%s)\r\n", ftp_server_ip_addr);
498 #endif
499 
500  /***************************************************************************
501  main program loop: set main_loop to 0 to exit loop
502  ***************************************************************************/
503  CPrintf ("\r\n %s running...", program_name);
504  CPuts (" entering main loop ...\r\n");
505  while ( main_loop )
506  {
507  /***********************************************************************
508  try to detect IP assignment
509  if DHCP is used, the assigned IP address may change
510  ***********************************************************************/
512 
513  /***********************************************************************
514  process net_isq()
515  ***********************************************************************/
516  net_isq ();
517 
518  /***********************************************************************
519  monitor link status
520  ***********************************************************************/
522 
523  /***********************************************************************
524  handle FTP up/download (use test.gel to set action)
525  ***********************************************************************/
526  switch ( action )
527  {
528  case FTP_DOWNLOAD:
529  action = FALSE;
530  ret = FTP_receive_file();
531  break;
532 
533  case FTP_UPLOAD:
534  action = FALSE;
535  ret = FTP_send_file();
536  break;
537 
538  default :
539  action = FALSE;
540  break;
541  } // switch
542 
543 #ifdef USE_KEY_CONTROL
544  if ( KEY_pressed (SW0) ) action = FTP_DOWNLOAD;
545  if ( KEY_pressed (SW1) ) action = FTP_UPLOAD;
546 #endif
547 
548 #ifdef USE_UART_CONTROL
549  if (BoardUartReadChar(&c))
550  {
551  CPrintf("%c\r\n",c);
552  action = (uint32_t)(c - '0');
553  }
554 #endif
555 
556  /***********************************************************************
557  show that the program is running, perform symbol animation
558  ***********************************************************************/
560  }
561 
562  /***************************************************************************
563  exit program, shut down peripherals
564  ***************************************************************************/
565  return (0);
566 }
static char ftp_server_ip_addr[]
Definition: FTPclient.c:157
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
time_t GetSystemTimerRes(void)
Definition: timer.c:160
#define UINT32_C(value)
Definition: stdint.h:210
#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 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 VT100_BLUE
Definition: cprintf.h:146
uint32_t initial_heap_size
Definition: Common.c:140
#define FTP_UPLOAD
Definition: FTPclient.c:119
int32_t FTP_send_file(void)
Definition: FTPclient.c:298
int32_t BoardUartReadChar(char *c)
read one character from the UART
#define CPRINTF_DEFAULT_OUTPUT
Definition: BoardSpecific.h:129
#define VT100_RED
Definition: cprintf.h:143
int32_t FTP_receive_file(void)
Definition: FTPclient.c:188
uint16_t CPrintAnimatedSymbol(char *c, time_t period, size_t size)
Definition: cprintf.c:816
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
#define SYSTEM_TIMER_INT
Definition: BoardSpecific.h:174
#define SYSTEM_TIMER
Definition: BoardSpecific.h:167
int KEY_pressed(unsigned int keyNum)
Definition: BoardSpecific.c:1371
char * program_name
Definition: FTPclient.c:131
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 action
Definition: FTPclient.c:151
#define FTP_DOWNLOAD
Definition: FTPclient.c:118
#define START_UP_MESSAGE(rev)
Definition: BoardSpecific.h:192
#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 CPrintfProgress(s)
Definition: cprintf.h:230
time_t tv_usec
Definition: timer.h:78
#define CPrintfProgressSuccess()
Definition: cprintf.h:263

Variable Documentation

char* program_name = "FTP-client"
char data_buffer[FTP_BUFFER_SIZE]
Examples:
Blocksend.c, and FTPClient.c.
uint32_t action = UINT32_C(0)
Examples:
FTPClient.c.
char ftp_server_ip_addr[] = "192.168.168.8"
static
Examples:
FTPClient.c.
user_type user[]
Initial value:
=
{
{"anonymous", "*" , EFS_TERMINATION},
}
#define EFS_TERMINATION
Definition: net.h:783
Examples:
FTPClient.c, FTPServer.c, HTTPdynamic.c, HTTPjava.c, and HTTPsimple.c.