diff --git a/apps/examples/aft/.gitignore b/apps/examples/aft/.gitignore new file mode 100644 index 0000000000..fa1ec75792 --- /dev/null +++ b/apps/examples/aft/.gitignore @@ -0,0 +1,11 @@ +/Make.dep +/.depend +/.built +/*.asm +/*.obj +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src diff --git a/apps/examples/aft/Kconfig b/apps/examples/aft/Kconfig new file mode 100644 index 0000000000..eb19c4056f --- /dev/null +++ b/apps/examples/aft/Kconfig @@ -0,0 +1,15 @@ +# +# 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_AFT + bool "Audio File Transfer application" + default n + ---help--- + Enable the Audio File Transfer application + +config USER_ENTRYPOINT + string + default "aft_main" if ENTRY_AFT + diff --git a/apps/examples/aft/Kconfig_ENTRY b/apps/examples/aft/Kconfig_ENTRY new file mode 100644 index 0000000000..c2390eb8f6 --- /dev/null +++ b/apps/examples/aft/Kconfig_ENTRY @@ -0,0 +1,3 @@ +config ENTRY_AFT + bool "\"Audio File Transfer!\" example" + depends on EXAMPLES_AFT diff --git a/apps/examples/aft/Make.defs b/apps/examples/aft/Make.defs new file mode 100644 index 0000000000..cecf4b3f19 --- /dev/null +++ b/apps/examples/aft/Make.defs @@ -0,0 +1,21 @@ +########################################################################### +# +# Copyright 2020 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. +# +########################################################################### + +ifeq ($(CONFIG_EXAMPLES_AFT),y) +CONFIGURED_APPS += examples/aft +endif diff --git a/apps/examples/aft/Makefile b/apps/examples/aft/Makefile new file mode 100644 index 0000000000..63b88474b7 --- /dev/null +++ b/apps/examples/aft/Makefile @@ -0,0 +1,172 @@ +########################################################################### +# +# Copyright 2018 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/aft/Makefile +# +# Copyright (C) 2009-2012 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# 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 + +CXXEXT ?= .cpp +# C++ Test Example + +APPNAME = aft +FUNCNAME = $(APPNAME)_main +THREADEXEC = TASH_EXECMD_ASYNC + +# C++ Test Example +ASRCS = +CSRCS = +CXXSRCS = +MAINSRC = $(FUNCNAME)$(CXXEXT) + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) +CXXOBJS = $(CXXSRCS:$(CXXEXT)=$(OBJEXT)) +ifeq ($(suffix $(MAINSRC)),$(CXXEXT)) +MAINOBJ = $(MAINSRC:$(CXXEXT)=$(OBJEXT)) +else +MAINOBJ = $(MAINSRC:.c=$(OBJEXT)) +endif + +SRCS = $(ASRCS) $(CSRCS) $(CXXSRCS) $(MAINSRC) +OBJS = $(AOBJS) $(COBJS) $(CXXOBJS) + +ifneq ($(CONFIG_BUILD_KERNEL),y) +OBJS += $(MAINOBJ) +endif + +ifeq ($(CONFIG_WINDOWS_NATIVE),y) +BIN = $(APPDIR)\libapps$(LIBEXT) +else +ifeq ($(WINTOOL),y) + BIN = $(APPDIR)\\libapps$(LIBEXT) +else + BIN = $(APPDIR)/libapps$(LIBEXT) +endif +endif + +CONFIG_EXAMPLES_AFT ?= aft$(EXEEXT) +PROGNAME = $(CONFIG_EXAMPLES_AFT) + +ROOTDEPPATH = --dep-path . + +# Common build + +VPATH = + +all: .built +.PHONY: clean depend distclean + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +$(CXXOBJS): %$(OBJEXT): %$(CXXEXT) + $(call COMPILEXX, $<, $@) + +ifeq ($(suffix $(MAINSRC)),$(CXXEXT)) +$(MAINOBJ): %$(OBJEXT): %$(CXXEXT) + $(call COMPILEXX, $<, $@) +else +$(MAINOBJ): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) +endif + +.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_AFT),yy) +$(BUILTIN_REGISTRY)$(DELIM)$(FUNCNAME).bdat: $(DEPCONFIG) Makefile + $(Q) $(call REGISTER,$(APPNAME),$(FUNCNAME),$(THREADEXEC),$(PRIORITY),$(STACKSIZE)) + +context: $(BUILTIN_REGISTRY)$(DELIM)$(FUNCNAME).bdat + +else +context: + +endif + +.depend: Makefile $(SRCS) +ifeq ($(filter %$(CXXEXT),$(SRCS)),) + @$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep +else + @$(MKDEP) $(ROOTDEPPATH) "$(CXX)" -- $(CXXFLAGS) -- $(SRCS) >Make.dep +endif + @touch $@ + +depend: .depend + +clean: + $(call DELFILE, .built) + $(call CLEAN) + +distclean: clean + $(call DELFILE, Make.dep) + $(call DELFILE, .depend) + +-include Make.dep +.PHONY: preconfig +preconfig: diff --git a/apps/examples/aft/README.txt b/apps/examples/aft/README.txt new file mode 100644 index 0000000000..9e745a5edb --- /dev/null +++ b/apps/examples/aft/README.txt @@ -0,0 +1,9 @@ +This is guided to use AFT (Audio File Transfer) sample app to transfer file from RTL8730E board to local PC + +1. We need to connect the local pc and RTL8730E board on same network. +2. We need to install python on local pc and save the minimal_file_transfer.py file in the local pc (or destination). +3. Run the Aft app on RTL8730E board. +4. Run the below command to transfer the file from RTL8730E board to local PC: + python minimal_file_transfer.py -a -s -t + +i.e. : python minimal_file_transfer.py -a "192.168.1.248" -s D:\check_audio\record2.pcm -t /mnt/record2.pcm diff --git a/apps/examples/aft/aft_main.cpp b/apps/examples/aft/aft_main.cpp new file mode 100644 index 0000000000..1489a1421b --- /dev/null +++ b/apps/examples/aft/aft_main.cpp @@ -0,0 +1,244 @@ +/**************************************************************************** + * + * Copyright 2024 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. + * + ****************************************************************************/ +//*************************************************************************** +// Included Files +//*************************************************************************** + +/**************************************************************************** + * + * Copyright 2020 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define SFT_PORT 5555 +#define BUF_SIZE 2048 +static char g_buf[BUF_SIZE] = {0,}; +static char g_name_buf[128]; + +static int send_data(int sd, char *buf, int buf_size) +{ + int sent = 0; + while (sent < buf_size) { + int ret = send(sd, buf + sent, buf_size - sent, 0); + if (ret <= 0) { + if (ret == 0) { + printf("[AFT] connection closed\n"); + return -1; + } + if (errno == EWOULDBLOCK) { + continue; + } + } + sent += ret; + } + return 0; +} + +static int read_data(int sd, char *buf, int buf_size, int *readed) +{ + int nbytes; + int read_size = 0; + while (read_size < buf_size) { + nbytes = recv(sd, buf + read_size, buf_size - read_size, 0); + if (nbytes == 0) { + printf("[AFT] connection closed\n"); + return -1; + } else if (nbytes < 0) { + printf("[AFT] unknown error(%d)\n", errno); + return -1; + } + read_size += nbytes; + *readed = read_size; + } + return 0; +} + +static int get_file_size(FILE *fp) +{ + fseek(fp, 0, SEEK_END); + int file_size = ftell(fp); + + printf("[AFT] file_size (%d)\n", file_size); + fseek(fp, 0, SEEK_SET); + + return file_size; +} + +static void send_file(int connfd) +{ + int ret; + int readed = 0; + int net_file_name_size = 0; + int file_name_size = 0; + ret = read_data(connfd, (char *)&net_file_name_size, sizeof(int), &readed); + if (ret < 0) { + printf("[AFT] readed %d %d\n", readed, __LINE__); + return; + } + file_name_size = ntohl(net_file_name_size); + printf("[AFT] file name length(%d) %d\n", file_name_size, readed); + + ret = read_data(connfd, g_name_buf, file_name_size, &readed); + if (ret < 0) { + printf("[AFT] fail %d\n", ret); + return; + } + g_name_buf[readed] = 0; + printf("[AFT] file name: %s\n", g_name_buf); + + FILE *fp = fopen(g_name_buf, "rb"); + if (!fp) { + printf("[AFT] fail %d\n", __LINE__); + return; + } + + //get file size + int file_size = get_file_size(fp); + printf("[AFT] send file size %d\n", file_size); + int net_file_size = htonl(file_size); + ret = send_data(connfd, (char *)&net_file_size, sizeof(int)); + + if (ret < 0) { + printf("[AFT] error %d\n", errno); + fclose(fp); + return; + } + printf("[AFT] file size: %d\n", file_size); + + readed = 0; + while (readed < file_size) { + ret = fread(g_buf, sizeof(char), BUF_SIZE, fp); + if (ret < 0) { + fclose(fp); + return; + } + send_data(connfd, g_buf, ret); + readed += ret; + printf("[AFT] Successfully Sent %d bytes until now.\n", readed); + } + + printf("[AFT] <--send : %d bytes\n", readed); + fclose(fp); + return; +} + +/**************************************************************************** + * aft_main + ****************************************************************************/ +extern "C" +{ +int aft_main(int argc, char *argv[]) +{ + printf("[AFT] Main called"); + struct sockaddr_in servaddr; + struct sockaddr_in cliaddr; + int listenfd = -1; + int connfd = -1; + socklen_t clilen; + int ret = 0; + int readed = 0; + + listenfd = socket(PF_INET, SOCK_STREAM, 0); + if (listenfd < 0) { + printf("[AFT] TCP socket failure %d\n", errno); + return -1; + } + + /* + * Enable SO_REUSEADDR to allow multiple instances of this + * application to receive copies of the multicast datagrams. + */ + + int reuse = 1; + ret = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)); + if (ret < 0) { + printf("[AFT] ERR: setting SO_REUSEADDR\n"); + close(listenfd); + return 0 ; + } + printf("[AFT] set reusable success\n"); + + /* Connect the socket to the server */ + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = PF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(SFT_PORT); + + ret = bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); + if (ret < 0) { + perror("[AFT] bind fail\n"); + close(listenfd); + return 0; + } + + printf("[AFT] Listening... port %d\n", SFT_PORT); + + ret = listen(listenfd, 1024); + if (ret < 0) { + perror("[AFT] listen fail\n"); + close(listenfd); + return 0; + } + + clilen = sizeof(cliaddr); + + connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen); + if (connfd < 0) { + perror("[AFT] accept fail\n"); + close(listenfd); + return 0; + } + printf("[AFT] Accepted\n"); + send_file(connfd); + close(connfd); + close(listenfd); + printf("[AFT] Stopping Perfectly\n"); + return 0; +} +} diff --git a/apps/examples/aft/minimal_file_transfer.py b/apps/examples/aft/minimal_file_transfer.py new file mode 100644 index 0000000000..56add1a37d --- /dev/null +++ b/apps/examples/aft/minimal_file_transfer.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +############################################################################ +# +# Copyright 2020 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. +# +############################################################################ + +import socket +import os +import argparse +import logging +import sys + +LOG = logging.getLogger(__name__) +PORT = 5555 # The port used by the server +buffer_len = 2048 + +def receive_file(target_addr, send_file_name, target_file_name): + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.connect((target_addr, PORT)) + s.sendall(len(target_file_name).to_bytes(4, byteorder='big')) + s.sendall(target_file_name.encode()) + + data = s.recv(4) + file_size = socket.ntohl(int.from_bytes(data, sys.byteorder)) + LOG.info("file size: %d", file_size) + tot_recv = 0 + with open(send_file_name, 'wb') as infile: + while tot_recv < file_size: + data = s.recv(min(buffer_len, file_size-tot_recv)) + tot_recv += len(data) + print(f"Total size received till now: {tot_recv}") + infile.write(data) + + LOG.info("File receiving is done") + + return + + +if __name__ == '__main__': + logging.basicConfig( + format="%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s", + level=logging.DEBUG, + datefmt="%Y-%m-%d %H:%M:%S", + ) + + parser = argparse.ArgumentParser( + description='Simple File Transfer -a\ + target_addr -s file to send -t file path of target') + parser.add_argument('-a', help='address', required=True) + parser.add_argument('-s', help='source file', required=True) + parser.add_argument('-t', help='target path', required=True) + + args = parser.parse_args() + LOG.info("Target address: %s", args.a) + LOG.info("File name(source): %s", args.s) + LOG.info("File name(target): %s", args.t) + + receive_file(target_addr=args.a, send_file_name=args.s, + target_file_name=args.t) + \ No newline at end of file diff --git a/build/configs/rtl8730e/audio/defconfig b/build/configs/rtl8730e/audio/defconfig index d802e7c473..992fda253e 100644 --- a/build/configs/rtl8730e/audio/defconfig +++ b/build/configs/rtl8730e/audio/defconfig @@ -1520,7 +1520,7 @@ CONFIG_EXAMPLES_HELLO=y CONFIG_EXAMPLES_SOUNDPLAYER=y # CONFIG_EXAMPLES_MEMORY_FRAGMENTATION_TEST is not set # CONFIG_EXAMPLES_NETTEST is not set - +#CONFIG_EXAMPLES_AFT is not set # # Performance # @@ -1579,7 +1579,6 @@ CONFIG_EXAMPLES_WAKEREC=y # # CONFIG_EXAMPLES_TAHI is not set # CONFIG_EXAMPLES_WIFIMANAGER_TEST is not set - # # Platform-specific Support #