--- /dev/null
+#
+# For a description of the syntax of this configuration file,
+# see kconfig-language at https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt
+#
+
+config EXAMPLES_FTPC
+ bool "FTP client example"
+ depends on NETUTILS_FTPC
+ default n
+ ---help---
+ Enable the FTP client example
+
+config USER_ENTRYPOINT
+ string
+ default "ftpc_main" if ENTRY_FTPC
--- /dev/null
+config ENTRY_FTPC
+ bool "FTP client example"
+ depends on EXAMPLES_FTPC
--- /dev/null
+###########################################################################
+#
+# Copyright 2017 Samsung Electronics All Rights Reserved.
+#
+# 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.
+#
+###########################################################################
+############################################################################
+# apps/examples/ftpc/Make.defs
+# Adds selected applications to apps/ build
+#
+# Copyright (C) 2015 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <gnutt@nuttx.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name NuttX nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################################
+
+ifeq ($(CONFIG_EXAMPLES_FTPC),y)
+CONFIGURED_APPS += examples/ftpc
+endif
--- /dev/null
+###########################################################################
+#
+# Copyright 2017 Samsung Electronics All Rights Reserved.
+#
+# 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.
+#
+###########################################################################
+############################################################################
+# apps/examples/ftpc/Makefile
+#
+# Copyright (C) 2008, 2010-2013 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <gnutt@nuttx.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name NuttX nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################################
+
+-include $(TOPDIR)/.config
+-include $(TOPDIR)/Make.defs
+include $(APPDIR)/Make.defs
+
+# built-in application info
+
+APPNAME = ftpc
+THREADEXEC = TASH_EXECMD_ASYNC
+FUNCNAME = ftpc_main
+
+# ftp client example
+
+ASRCS =
+CSRCS = ftpc_cmds.c
+MAINSRC = ftpc_main.c
+
+AOBJS = $(ASRCS:.S=$(OBJEXT))
+COBJS = $(CSRCS:.c=$(OBJEXT))
+MAINOBJ = $(MAINSRC:.c=$(OBJEXT))
+
+SRCS = $(ASRCS) $(CSRCS) $(MAINSRC)
+OBJS = $(AOBJS) $(COBJS)
+
+ifneq ($(CONFIG_BUILD_KERNEL),y)
+ OBJS += $(MAINOBJ)
+endif
+
+ifeq ($(CONFIG_WINDOWS_NATIVE),y)
+ BIN = ..\..\libapps$(LIBEXT)
+else
+ifeq ($(WINTOOL),y)
+ BIN = ..\\..\\libapps$(LIBEXT)
+else
+ BIN = ../../libapps$(LIBEXT)
+endif
+endif
+
+ifeq ($(WINTOOL),y)
+ INSTALL_DIR = "${shell cygpath -w $(BIN_DIR)}"
+else
+ INSTALL_DIR = $(BIN_DIR)
+endif
+
+CONFIG_EXAMPLES_FTPC_PROGNAME ?= ftpc$(EXEEXT)
+PROGNAME = $(CONFIG_EXAMPLES_FTPC_PROGNAME)
+
+ROOTDEPPATH = --dep-path .
+
+
+# Common build
+
+VPATH =
+
+all: .built
+.PHONY: clean depend distclean
+
+$(AOBJS): %$(OBJEXT): %.S
+ $(call ASSEMBLE, $<, $@)
+
+$(COBJS) $(MAINOBJ): %$(OBJEXT): %.c
+ $(call COMPILE, $<, $@)
+
+.built: $(OBJS)
+ $(call ARCHIVE, $(BIN), $(OBJS))
+ @touch .built
+
+ifeq ($(CONFIG_BUILD_KERNEL),y)
+$(BIN_DIR)$(DELIM)$(PROGNAME): $(OBJS) $(MAINOBJ)
+ @echo "LD: $(PROGNAME)"
+ $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME) $(ARCHCRT0OBJ) $(MAINOBJ) $(LDLIBS)
+ $(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(PROGNAME)
+
+install: $(BIN_DIR)$(DELIM)$(PROGNAME)
+
+else
+install:
+
+endif
+
+ifeq ($(CONFIG_BUILTIN_APPS)$(CONFIG_EXAMPLES_FTPC),yy)
+$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat: $(DEPCONFIG) Makefile
+ $(Q) $(call REGISTER,$(APPNAME),$(APPNAME)_main,$(THREADEXEC),$(PRIORITY),$(STACKSIZE))
+
+context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat
+
+else
+context:
+
+endif
+
+.depend: Makefile $(SRCS)
+ @$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep
+ @touch $@
+
+depend: .depend
+
+clean:
+ $(call DELFILE, .built)
+ $(call CLEAN)
+
+distclean: clean
+ $(call DELFILE, Make.dep)
+ $(call DELFILE, .depend)
+
+-include Make.dep
+.PHONY: preconfig
+preconfig:
--- /dev/null
+/****************************************************************************
+ *
+ * Copyright 2017 Samsung Electronics All Rights Reserved.
+ *
+ * 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.
+ *
+ ****************************************************************************/
+/****************************************************************************
+ * apps/examples/ftpc/ftpc.h
+ *
+ * Copyright (C) 2011 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+#ifndef __APPS_EXAMPLES_FTPC_FTPC_H
+#define __APPS_EXAMPLES_FTPC_FTPC_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+#include <stdio.h>
+#include <tinyara/config.h>
+#include <sys/types.h>
+#include <apps/ftpc.h>
+
+/****************************************************************************
+ * Definitions
+ ****************************************************************************/
+
+/* Maximum size of one command line */
+#define FTPC_LINELEN 80
+
+/* If CONFIG_STDIO_LINEBUFFER is defined, the STDIO buffer will be flushed
+ * on each new line. Otherwise, STDIO needs to be explicitly flushed to
+ * see the output in context.
+ */
+
+#if CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NFILE_STREAMS > 0 && CONFIG_STDIO_BUFFER_SIZE > 0 && !defined(CONFIG_STDIO_LINEBUFFER)
+#define FFLUSH() fflush(stdout)
+#else
+#define FFLUSH()
+#endif
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+typedef int (*cmd_t)(SESSION handle, int argc, char **argv);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/* FTP command handlers */
+
+extern int cmd_rlogin(SESSION handle, int argc, char **argv);
+extern int cmd_rquit(SESSION handle, int argc, char **argv);
+extern int cmd_rchdir(SESSION handle, int argc, char **argv);
+extern int cmd_rpwd(SESSION handle, int argc, char **argv);
+extern int cmd_rcdup(SESSION handle, int argc, char **argv);
+extern int cmd_rmkdir(SESSION handle, int argc, char **argv);
+
+extern int cmd_rrmdir(SESSION handle, int argc, char **argv);
+extern int cmd_runlink(SESSION handle, int argc, char **argv);
+extern int cmd_rchmod(SESSION handle, int argc, char **argv);
+extern int cmd_rrename(SESSION handle, int argc, char **argv);
+extern int cmd_rsize(SESSION handle, int argc, char **argv);
+extern int cmd_rtime(SESSION handle, int argc, char **argv);
+extern int cmd_ridle(SESSION handle, int argc, char **argv);
+extern int cmd_rnoop(SESSION handle, int argc, char **argv);
+extern int cmd_rhelp(SESSION handle, int argc, char **argv);
+extern int cmd_rls(SESSION handle, int argc, char **argv);
+extern int cmd_rget(SESSION handle, int argc, char **argv);
+extern int cmd_rput(SESSION handle, int argc, char **argv);
+
+#endif /* __APPS_EXAMPLES_FTPC_FTPC_H */
--- /dev/null
+/****************************************************************************
+ *
+ * Copyright 2017 Samsung Electronics All Rights Reserved.
+ *
+ * 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.
+ *
+ ****************************************************************************/
+/****************************************************************************
+ * examples/ftpc/ftpc_cmds.c
+ *
+ * Copyright (C) 2011 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <apps/ftpc.h>
+
+#include "ftpc.h"
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: cmd_rlogin
+ ****************************************************************************/
+
+int cmd_rlogin(SESSION handle, int argc, char **argv)
+{
+ struct ftpc_login_s login = { NULL, NULL, NULL, true };
+
+ login.uname = argv[1];
+ if (argc > 2) {
+ login.pwd = argv[2];
+ }
+
+ return ftpc_login(handle, &login);
+}
+
+/****************************************************************************
+ * Name: cmd_rquit
+ ****************************************************************************/
+
+int cmd_rquit(SESSION handle, int argc, char **argv)
+{
+ int ret = ftpc_quit(handle);
+ if (ret < 0) {
+ printf("quit failed: %d\n", errno);
+ return ERROR;
+ }
+ printf("Exitting...\n");
+ return OK;
+}
+
+/****************************************************************************
+ * Name: cmd_rchdir
+ ****************************************************************************/
+
+int cmd_rchdir(SESSION handle, int argc, char **argv)
+{
+ return ftpc_chdir(handle, argv[1]);
+}
+
+/****************************************************************************
+ * Name: cmd_rpwd
+ ****************************************************************************/
+
+int cmd_rpwd(SESSION handle, int argc, char **argv)
+{
+ FAR char *pwd = ftpc_rpwd(handle);
+ if (pwd) {
+ printf("PWD: %s\n", pwd);
+ free(pwd);
+ return OK;
+ }
+
+ return ERROR;
+}
+
+/****************************************************************************
+ * Name: cmd_rcdup
+ ****************************************************************************/
+
+int cmd_rcdup(SESSION handle, int argc, char **argv)
+{
+ return ftpc_cdup(handle);
+}
+
+/****************************************************************************
+ * Name: cmd_rmkdir
+ ****************************************************************************/
+
+int cmd_rmkdir(SESSION handle, int argc, char **argv)
+{
+ return ftpc_mkdir(handle, argv[1]);
+}
+
+/****************************************************************************
+ * Name: cmd_rrmdir
+ ****************************************************************************/
+
+int cmd_rrmdir(SESSION handle, int argc, char **argv)
+{
+ return ftpc_rmdir(handle, argv[1]);
+}
+
+/****************************************************************************
+ * Name: cmd_runlink
+ ****************************************************************************/
+
+int cmd_runlink(SESSION handle, int argc, char **argv)
+{
+ return ftpc_unlink(handle, argv[1]);
+}
+
+/****************************************************************************
+ * Name: cmd_rchmod
+ ****************************************************************************/
+
+int cmd_rchmod(SESSION handle, int argc, char **argv)
+{
+ return ftpc_chmod(handle, argv[1], argv[2]);
+}
+
+/****************************************************************************
+ * Name: cmd_rrename
+ ****************************************************************************/
+
+int cmd_rrename(SESSION handle, int argc, char **argv)
+{
+ return ftpc_rename(handle, argv[1], argv[2]);
+}
+
+/****************************************************************************
+ * Name: cmd_rsize
+ ****************************************************************************/
+
+int cmd_rsize(SESSION handle, int argc, char **argv)
+{
+ off_t size = ftpc_filesize(handle, argv[1]);
+ printf("SIZE: %lu\n", size);
+ return OK;
+}
+
+/****************************************************************************
+ * Name: cmd_rtime
+ ****************************************************************************/
+
+int cmd_rtime(SESSION handle, int argc, char **argv)
+{
+ time_t filetime = ftpc_filetime(handle, argv[1]);
+ printf("TIME: %lu\n", (long)filetime);
+ return OK;
+}
+
+/****************************************************************************
+ * Name: cmd_ridle
+ ****************************************************************************/
+
+int cmd_ridle(SESSION handle, int argc, char **argv)
+{
+ unsigned int idletime = 0;
+
+ if (argc > 1) {
+ idletime = atoi(argv[1]);
+ }
+
+ return ftpc_idle(handle, idletime);
+}
+
+/****************************************************************************
+ * Name: cmd_rnoop
+ ****************************************************************************/
+
+int cmd_rnoop(SESSION handle, int argc, char **argv)
+{
+ return ftpc_noop(handle);
+}
+
+/****************************************************************************
+ * Name: cmd_rhelp
+ ****************************************************************************/
+
+int cmd_rhelp(SESSION handle, int argc, char **argv)
+{
+ FAR const char *cmd = NULL;
+ int ret;
+
+ if (argc > 1) {
+ cmd = argv[1];
+ }
+
+ ret = ftpc_help(handle, cmd);
+ if (ret == OK) {
+ FAR char *msg = ftpc_response(handle);
+ puts(msg);
+ free(msg);
+ }
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: cmd_rls
+ ****************************************************************************/
+
+int cmd_rls(SESSION handle, int argc, char **argv)
+{
+ FAR struct ftpc_dirlist_s *dirlist;
+ FAR char *dirname = NULL;
+ int i;
+
+ /* Get the directory listing */
+
+ if (argc > 1) {
+ dirname = argv[1];
+ }
+
+ dirlist = ftpc_listdir(handle, dirname);
+ if (!dirlist) {
+ return ERROR;
+ }
+
+ /* Print the directory listing */
+
+ printf("%s/\n", dirname ? dirname : ".");
+ for (i = 0; i < dirlist->nnames; i++) {
+ printf(" %s\n", dirlist->name[i]);
+ }
+
+ FFLUSH();
+
+ /* We are responsible for freeing the directory structure allocated by
+ * ftpc_listdir().
+ */
+
+ ftpc_dirfree(dirlist);
+ return OK;
+}
+
+/****************************************************************************
+ * Name: cmd_rget
+ ****************************************************************************/
+
+int cmd_rget(SESSION handle, int argc, char **argv)
+{
+ FAR const char *rname;
+ FAR const char *lname = NULL;
+ int xfrmode = FTPC_XFRMODE_ASCII;
+ int option;
+
+ while ((option = getopt(argc, argv, "ab")) != ERROR) {
+ if (option == 'a') {
+ xfrmode = FTPC_XFRMODE_ASCII;
+ } else if (option == 'b') {
+ xfrmode = FTPC_XFRMODE_BINARY;
+ } else {
+ printf("%s: Unrecognized option: '%c'\n", "rget", option);
+ return ERROR;
+ }
+ }
+
+ /* There should be one or two parameters remaining on the command line */
+
+ if (optind >= argc) {
+ printf("%s: Missing required arguments\n", "rget");
+ return ERROR;
+ }
+
+ rname = argv[optind];
+ optind++;
+
+ if (optind < argc) {
+ lname = argv[optind];
+ optind++;
+ }
+
+ if (optind != argc) {
+ printf("%s: Too many arguments\n", "rget");
+ return ERROR;
+ }
+
+ /* Perform the transfer */
+
+ return ftpc_getfile(handle, rname, lname, FTPC_GET_NORMAL, xfrmode);
+}
+
+/****************************************************************************
+ * Name: cmd_rput
+ ****************************************************************************/
+
+int cmd_rput(SESSION handle, int argc, char **argv)
+{
+ FAR const char *lname;
+ FAR const char *rname = NULL;
+ int xfrmode = FTPC_XFRMODE_ASCII;
+ int option;
+
+ while ((option = getopt(argc, argv, "ab")) != ERROR) {
+ if (option == 'a') {
+ xfrmode = FTPC_XFRMODE_ASCII;
+ } else if (option == 'b') {
+ xfrmode = FTPC_XFRMODE_BINARY;
+ } else {
+ printf("%s: Unrecognized option: '%c'\n", "rput", option);
+ return ERROR;
+ }
+ }
+
+ /* There should be one or two parameters remaining on the command line */
+
+ if (optind >= argc) {
+ printf("%s: Missing required arguments\n", "rput");
+ return ERROR;
+ }
+
+ lname = argv[optind];
+ optind++;
+
+ if (optind < argc) {
+ rname = argv[optind];
+ optind++;
+ }
+
+ if (optind != argc) {
+ printf("%s: Too many arguments\n", "rput");
+ return ERROR;
+ }
+
+ /* Perform the transfer */
+
+ return ftp_putfile(handle, lname, rname, FTPC_PUT_NORMAL, xfrmode);
+}
--- /dev/null
+/****************************************************************************\r
+ *\r
+ * Copyright 2017 Samsung Electronics All Rights Reserved.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing,\r
+ * software distributed under the License is distributed on an\r
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
+ * either express or implied. See the License for the specific\r
+ * language governing permissions and limitations under the License.\r
+ *\r
+ ****************************************************************************/\r
+/****************************************************************************\r
+ * examples/ftpc/ftpc_main.c\r
+ *\r
+ * Copyright (C) 2011-2013 Gregory Nutt. All rights reserved.\r
+ * Author: Gregory Nutt <gnutt@nuttx.org>\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ *\r
+ * 1. Redistributions of source code must retain the above copyright\r
+ * notice, this list of conditions and the following disclaimer.\r
+ * 2. Redistributions in binary form must reproduce the above copyright\r
+ * notice, this list of conditions and the following disclaimer in\r
+ * the documentation and/or other materials provided with the\r
+ * distribution.\r
+ * 3. Neither the name NuttX nor the names of its contributors may be\r
+ * used to endorse or promote products derived from this software\r
+ * without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE\r
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS\r
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED\r
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
+ * POSSIBILITY OF SUCH DAMAGE.\r
+ *\r
+ ****************************************************************************/\r
+\r
+/****************************************************************************\r
+ * Included Files\r
+ ****************************************************************************/\r
+\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <fcntl.h>\r
+#include "ftpc.h"\r
+\r
+/****************************************************************************\r
+ * Pre-processor Definitions\r
+ ****************************************************************************/\r
+\r
+#define FTPC_MAX_ARGUMENTS 4\r
+\r
+/****************************************************************************\r
+ * Public Types\r
+ ****************************************************************************/\r
+\r
+struct cmdmap_s {\r
+ const char *cmd; /* Name of the command */\r
+ cmd_t handler; /* Function that handles the command */\r
+ uint8_t minargs; /* Minimum number of arguments */\r
+ uint8_t maxargs; /* Maximum number of arguments */\r
+ const char *usage; /* Usage instructions for 'help' command */\r
+};\r
+\r
+/****************************************************************************\r
+ * Private Data\r
+ ****************************************************************************/\r
+\r
+static const char g_delim[] = " \t\n";\r
+\r
+static int cmd_lhelp(SESSION handle, int argc, char **argv);\r
+static int cmd_lunrecognized(SESSION handle, int argc, char **argv);\r
+\r
+static pthread_t pth = -1;\r
+static mqd_t g_send_mqfd;\r
+static mqd_t g_recv_mqfd;\r
+\r
+static const struct cmdmap_s g_cmdmap[] =\r
+{\r
+ { "cd", cmd_rchdir, 2, 2, "<directory>" },\r
+ { "chmod", cmd_rchmod, 3, 3, "<permissions> <path>" },\r
+ { "get", cmd_rget, 2, 4, "[-a|b] <rname> [<lname>]" },\r
+ { "help", cmd_lhelp, 1, 1, "" },\r
+ { "idle", cmd_ridle, 1, 2, "[<idletime>]" },\r
+ { "login", cmd_rlogin, 2, 3, "<uname> [<password>]" },\r
+ { "ls", cmd_rls, 1, 2, "[<dirpath>]" },\r
+ { "mkdir", cmd_rmkdir, 2, 2, "<directory>" },\r
+ { "noop", cmd_rnoop, 1, 1, "" },\r
+ { "put", cmd_rput, 2, 4, "[-a|b] <lname> [<rname>]" },\r
+ { "pwd", cmd_rpwd, 1, 1, "" },\r
+ { "quit", cmd_rquit, 1, 1, "" },\r
+ { "rename", cmd_rrename, 3, 3, "<oldname> <newname>" },\r
+ { "rhelp", cmd_rhelp, 1, 2, "[<command>]" },\r
+ { "rm", cmd_runlink, 2, 2, "<filepath>" },\r
+ { "rmdir", cmd_rrmdir, 2, 2, "<directory>" },\r
+ { "size", cmd_rsize, 2, 2, "<filepath>" },\r
+ { "time", cmd_rtime, 2, 2, "<filepath>" },\r
+ { "up", cmd_rcdup, 1, 1, "" },\r
+ { NULL, NULL, 1, 1, NULL }\r
+};\r
+\r
+/****************************************************************************\r
+ * Private Functions\r
+ ****************************************************************************/\r
+\r
+/****************************************************************************\r
+ * Name: cmd_lhelp\r
+ ****************************************************************************/\r
+\r
+static int cmd_lhelp(SESSION handle, int argc, char **argv)\r
+{\r
+ const struct cmdmap_s *ptr;\r
+\r
+ printf("Local FTPC commands:\n");\r
+ for (ptr = g_cmdmap; ptr->cmd; ptr++) {\r
+ if (ptr->usage) {\r
+ printf(" %s %s\n", ptr->cmd, ptr->usage);\r
+ } else {\r
+ printf(" %s\n", ptr->cmd);\r
+ }\r
+ }\r
+ return OK;\r
+}\r
+\r
+/****************************************************************************\r
+ * Name: cmd_lunrecognized\r
+ ****************************************************************************/\r
+\r
+static int cmd_lunrecognized(SESSION handle, int argc, char **argv)\r
+{\r
+ printf("Command %s unrecognized\n", argv[0]);\r
+ return ERROR;\r
+}\r
+\r
+/****************************************************************************\r
+ * Name: ftpc_argument\r
+ ****************************************************************************/\r
+\r
+char *ftpc_argument(char **saveptr)\r
+{\r
+ char *pbegin = *saveptr;\r
+ char *pend = NULL;\r
+ const char *term;\r
+\r
+ /* Find the beginning of the next token */\r
+\r
+ for (; *pbegin && strchr(g_delim, *pbegin) != NULL; pbegin++);\r
+\r
+ /* If we are at the end of the string with nothing\r
+ * but delimiters found, then return NULL.\r
+ */\r
+\r
+ if (!*pbegin) {\r
+ return NULL;\r
+ } else if (*pbegin == '#') {\r
+ /* Does the token begin with '#' -- comment */\r
+ /* Return NULL meaning that we are at the end of the line */\r
+\r
+ *saveptr = pbegin;\r
+ pbegin = NULL;\r
+ } else {\r
+ /* Otherwise, we are going to have to parse to find the end of\r
+ * the token. Does the token begin with '"'?\r
+ */\r
+\r
+ if (*pbegin == '"') {\r
+ /* Yes.. then only another '"' can terminate the string */\r
+\r
+ pbegin++;\r
+ term = "\"";\r
+ } else {\r
+ /* No, then any of the usual terminators will terminate the argument */\r
+\r
+ term = g_delim;\r
+ }\r
+\r
+ /* Find the end of the string */\r
+\r
+ for (pend = pbegin + 1; *pend && strchr(term, *pend) == NULL; pend++);\r
+\r
+ /* pend either points to the end of the string or to\r
+ * the first delimiter after the string.\r
+ */\r
+\r
+ if (*pend) {\r
+ /* Turn the delimiter into a null terminator */\r
+\r
+ *pend++ = '\0';\r
+ }\r
+\r
+ /* Save the pointer where we left off */\r
+\r
+ *saveptr = pend;\r
+\r
+ }\r
+\r
+ /* Return the beginning of the token. */\r
+\r
+ return pbegin;\r
+}\r
+\r
+/****************************************************************************\r
+ * Name: ftpc_execute\r
+ ****************************************************************************/\r
+\r
+static int ftpc_execute(SESSION handle, int argc, char *argv[])\r
+{\r
+ const struct cmdmap_s *cmdmap;\r
+ const char *cmd;\r
+ cmd_t handler = cmd_lunrecognized;\r
+ int ret;\r
+\r
+ /* The form of argv is:\r
+ *\r
+ * argv[0]: The command name. This is argv[0] when the arguments\r
+ * are, finally, received by the command handler\r
+ * argv[1]: The beginning of argument (up to FTPC_MAX_ARGUMENTS)\r
+ * argv[argc]: NULL terminating pointer\r
+ */\r
+\r
+ cmd = argv[0];\r
+\r
+ /* See if the command is one that we understand */\r
+\r
+ for (cmdmap = g_cmdmap; cmdmap->cmd; cmdmap++)\r
+ {\r
+ if (strcmp(cmdmap->cmd, cmd) == 0) {\r
+ /* Check if a valid number of arguments was provided. We\r
+ * do this simple, imperfect checking here so that it does\r
+ * not have to be performed in each command.\r
+ */\r
+\r
+ if (argc < cmdmap->minargs) {\r
+ /* Fewer than the minimum number were provided */\r
+\r
+ printf("Too few arguments for '%s'\n", cmd);\r
+ return ERROR;\r
+ } else if (argc > cmdmap->maxargs) {\r
+ /* More than the maximum number were provided */\r
+\r
+ printf("Too many arguments for '%s'\n", cmd);\r
+ return ERROR;\r
+ } else {\r
+ /* A valid number of arguments were provided (this does\r
+ * not mean they are right).\r
+ */\r
+\r
+ handler = cmdmap->handler;\r
+ break;\r
+ }\r
+ }\r
+ }\r
+\r
+ ret = handler(handle, argc, argv);\r
+ if (ret < 0) {\r
+ printf("%s failed: %d\n", cmd, errno);\r
+ } else {\r
+ printf("Command %s successed\n", cmd);\r
+ }\r
+ return ret;\r
+}\r
+\r
+/****************************************************************************\r
+ * Name: ftpc_parse\r
+ ****************************************************************************/\r
+\r
+int ftpc_parse(SESSION handle, char *cmdline)\r
+{\r
+ FAR char *argv[FTPC_MAX_ARGUMENTS+1];\r
+ FAR char *saveptr;\r
+ FAR char *cmd;\r
+ int argc;\r
+ int ret;\r
+\r
+ /* Initialize parser state */\r
+\r
+ memset(argv, 0, FTPC_MAX_ARGUMENTS*sizeof(FAR char *));\r
+\r
+ /* Parse out the command at the beginning of the line */\r
+\r
+ saveptr = cmdline;\r
+ cmd = ftpc_argument(&saveptr);\r
+\r
+ /* Check if any command was provided -OR- if command processing is\r
+ * currently disabled.\r
+ */\r
+\r
+ if (!cmd) {\r
+ /* An empty line is not an error */\r
+\r
+ return OK;\r
+ }\r
+\r
+ /* Parse all of the arguments following the command name. */\r
+\r
+ argv[0] = cmd;\r
+ for (argc = 1; argc < FTPC_MAX_ARGUMENTS; argc++) {\r
+ argv[argc] = ftpc_argument(&saveptr);\r
+ if (!argv[argc]) {\r
+ break;\r
+ }\r
+ }\r
+ argv[argc] = NULL;\r
+\r
+ /* Check if the maximum number of arguments was exceeded */\r
+\r
+ if (argc > FTPC_MAX_ARGUMENTS) {\r
+ printf("Too many arguments\n");\r
+ ret = -EINVAL;\r
+ } else {\r
+ /* Then execute the command */\r
+\r
+ ret = ftpc_execute(handle, argc, argv);\r
+ }\r
+\r
+ return ret;\r
+}\r
+\r
+void* ftpc_thread(void* argv)\r
+{\r
+ struct ftpc_connect_s connect = { { 0 }, 0 };\r
+ SESSION g_handle = NULL;\r
+ FAR char *ptr = NULL;\r
+\r
+ /* Check if the argument includes a port number */\r
+ ptr = strchr((char*)argv, ':');\r
+ if (ptr) {\r
+ *ptr = '\0';\r
+ connect.port = atoi(ptr + 1);\r
+ }\r
+\r
+ /* In any event, we can now extract the IP address from the comman-line */\r
+\r
+ connect.addr.s_addr = inet_addr((char*)argv);\r
+\r
+ /* Connect to the FTP server */\r
+\r
+ g_handle = ftpc_connect(&connect);\r
+ if (!g_handle) {\r
+ printf("Failed to connect to the server: %d\n", errno);\r
+ return NULL;\r
+ } else {\r
+ printf("Connected successful\n");\r
+ }\r
+\r
+ char msg_buffer[FTPC_LINELEN];\r
+ struct mq_attr attr;\r
+ int nbytes;\r
+\r
+ /* Fill in attributes for message queue */\r
+\r
+ attr.mq_maxmsg = 20;\r
+ attr.mq_msgsize = FTPC_LINELEN;\r
+ attr.mq_flags = 0;\r
+\r
+ /* Set the flags for the open of the queue.\r
+ * Make it a blocking open on the queue, meaning it will block if\r
+ * this task tries to read from the queue when the queue is empty\r
+ *\r
+ * O_CREAT - the queue will get created if it does not already exist.\r
+ * O_RDONLY - we are only planning to read from the queue.\r
+ *\r
+ * Open the queue, and create it if the sending process hasn't\r
+ * already created it.\r
+ */\r
+\r
+ g_recv_mqfd = mq_open("mqueue", O_RDONLY | O_CREAT, 0666, &attr);\r
+ if (g_recv_mqfd == (mqd_t)ERROR) {\r
+ printf("tc_mqueue_mq_open FAIL\n");\r
+ pthread_exit((pthread_addr_t)1);\r
+ }\r
+\r
+ while (1) {\r
+ memset(msg_buffer, 0x00, FTPC_LINELEN);\r
+ nbytes = mq_receive(g_recv_mqfd, msg_buffer, FTPC_LINELEN, 0);\r
+ if (nbytes > 0) {\r
+ (void)ftpc_parse(g_handle, msg_buffer);\r
+ if (strncmp(msg_buffer, "quit", sizeof("quit") + 1) == 0) { //exit thread\r
+ printf("Disconnected\n");\r
+ pth = -1;\r
+ return NULL;\r
+ }\r
+ }\r
+ }\r
+ return NULL;\r
+}\r
+\r
+/****************************************************************************\r
+ * Public Functions\r
+ ****************************************************************************/\r
+\r
+int ftpc_main(int argc, char **argv, char **envp)\r
+{\r
+ const struct cmdmap_s *cmdmap;\r
+ const char *cmd;\r
+ int i;\r
+\r
+ if (argc < 2) {\r
+ printf("Usage:\n");\r
+ printf(" %s open xx.xx.xx.xx[:pp]\n", argv[0]);\r
+ printf("Where\n");\r
+ printf(" xx.xx.xx.xx is the IP address of the FTP server\n");\r
+ printf(" pp is option port to use with the FTP server\n\n");\r
+ printf("*****************************************************************\n");\r
+ printf("* FTP client sample application *\n");\r
+ printf("*****************************************************************\n");\r
+ printf("Available commands:\n");\r
+ printf(" cd\n");\r
+ printf(" cdmod\n");\r
+ printf(" get\n");\r
+ printf(" help\n");\r
+ printf(" login\n");\r
+ printf(" ls\n");\r
+ printf(" mkdir\n");\r
+ printf(" noop\n");\r
+ printf(" open\n");\r
+ printf(" put\n");\r
+ printf(" quit\n");\r
+ printf(" rename\n");\r
+ printf(" rhelp\n");\r
+ printf(" rm\n");\r
+ printf(" rmdir\n");\r
+ printf(" size\n");\r
+ printf(" time\n");\r
+ printf(" up\n");\r
+ printf("use the command help to get extended help about arguments for the\n");\r
+ printf("different commands\n");\r
+ printf("*****************************************************************\n");\r
+ return 0;\r
+ }\r
+\r
+ cmd = argv[1];\r
+\r
+ if (strncmp("open", cmd, sizeof("open") + 1) == 0) {\r
+ int ret = pthread_create(&pth, NULL, ftpc_thread, (void*)argv[2]);\r
+ if (ret) {\r
+ printf("Thread creation Failed %d\n", errno);\r
+ return 0;\r
+ }\r
+ pthread_join(pth, NULL);\r
+ } else {\r
+ if (pth == -1) {\r
+ printf("Not connected\n");\r
+ return 0;\r
+ }\r
+ for (cmdmap = g_cmdmap; cmdmap->cmd; cmdmap++) {\r
+ if (strncmp(cmdmap->cmd, cmd, strlen(cmdmap->cmd) + 1) == 0) {\r
+ if (argc < cmdmap->minargs + 1) {\r
+ /* Fewer than the minimum number were provided */\r
+ printf("Too few arguments for '%s'\n", cmd);\r
+ return ERROR;\r
+ } else if (argc > cmdmap->maxargs + 1) {\r
+ /* More than the maximum number were provided */\r
+ printf("Too many arguments for '%s'\n", cmd);\r
+ return ERROR;\r
+ } else {\r
+ /* A valid number of arguments were provided (this does\r
+ * not mean they are right).\r
+ */\r
+ char msg_buffer[FTPC_LINELEN];\r
+ struct mq_attr attr;\r
+ int status = 0;\r
+\r
+ /* Fill in attributes for message queue */\r
+\r
+ attr.mq_maxmsg = 20;\r
+ attr.mq_msgsize = FTPC_LINELEN;\r
+ attr.mq_flags = 0;\r
+\r
+ /* Set the flags for the open of the queue.\r
+ * Make it a blocking open on the queue, meaning it will block if\r
+ * this process tries to send to the queue and the queue is full.\r
+ *\r
+ * O_CREAT - the queue will get created if it does not already exist.\r
+ * O_WRONLY - we are only planning to write to the queue.\r
+ *\r
+ * Open the queue, and create it if the receiving process hasn't\r
+ * already created it.\r
+ */\r
+\r
+ g_send_mqfd = mq_open("mqueue", O_WRONLY | O_CREAT, 0666, &attr);\r
+ if (g_send_mqfd == (mqd_t)-1) {\r
+ printf("tc_mqueue_mq_open FAIL\n");\r
+ }\r
+\r
+ msg_buffer[0] = '\0';\r
+ strcat(msg_buffer, argv[1]);\r
+ for (i = 2; i < argc; i++) {\r
+ strcat(msg_buffer, " ");\r
+ strcat(msg_buffer, argv[i]);\r
+ }\r
+\r
+ status = mq_send(g_send_mqfd, msg_buffer, strlen(msg_buffer), 0);\r
+ if (status < 0) {\r
+ printf("tc_mqueue_mq_send FAIL : failure=%d\n", status);\r
+ }\r
+\r
+ /* Close the queue and return success */\r
+\r
+ if (mq_close(g_send_mqfd) < 0) {\r
+ printf("tc_mqueue_mq_close FAIL\n");\r
+ } else {\r
+ g_send_mqfd = NULL;\r
+ }\r
+\r
+ return 0;\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ return 0;\r
+}\r