Skip to content

Commit

Permalink
Add and enable Simple File Transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
s1-vivek authored and abhishek-samsung committed Nov 27, 2024
1 parent 6293996 commit d13cf76
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 151 deletions.
204 changes: 55 additions & 149 deletions apps/examples/simple_file_transfer/sft_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,18 @@
#include <pthread.h>

#define SFT_PORT 5555
#define BUF_SIZE 512
#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)
{
printf("Sending Data %d\n", 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("connection closed\n");
printf("[AFT] connection closed\n");
return -1;
}
if (errno == EWOULDBLOCK) {
Expand All @@ -62,15 +61,15 @@ static int send_data(int sd, char *buf, int buf_size)

static int read_data(int sd, char *buf, int buf_size, int *readed)
{
printf("Receiving Data %d\n", buf_size);
int nbytes;
int read_size = 0;
while (read_size < buf_size) {
int nbytes = recv(sd, buf + read_size, buf_size - read_size, 0);
nbytes = recv(sd, buf + read_size, buf_size - read_size, 0);
if (nbytes == 0) {
printf("connection closed\n");
printf("[AFT] connection closed\n");
return -1;
} else if (nbytes < 0) {
printf("unknown error(%d)\n", errno);
printf("[AFT] unknown error(%d)\n", errno);
return -1;
}
read_size += nbytes;
Expand All @@ -79,146 +78,85 @@ static int read_data(int sd, char *buf, int buf_size, int *readed)
return 0;
}

static int write_data(FILE *fp, char *buf, int buf_size)
{
int written = 0;
while (written < buf_size) {
int res = fwrite(buf + written, sizeof(char), buf_size - written, fp);
if (res < 0) {
printf("file write fail %d\n", errno);
return -1;
}
written += res;
}
return 0;
}

static int get_file_size(FILE *fp)
{
fseek(fp, 0, SEEK_END);
int file_size = ftell(fp);

printf("file_size (%d)\n", file_size);
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;
int ret = read_data(connfd, (char *)&file_name_size, sizeof(int), &readed);
ret = read_data(connfd, (char *)&net_file_name_size, sizeof(int), &readed);
if (ret < 0) {
printf("readed %d %d\n", readed, __LINE__);
printf("[AFT] readed %d %d\n", readed, __LINE__);
return;
}
file_name_size = ntohl(file_name_size);
printf("file name length(%d) %d\n", file_name_size, readed);
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("fail %d\n", __LINE__);
printf("[AFT] fail %d\n", ret);
return;
}
g_name_buf[file_name_size] = 0;
printf("file name: %s\n", g_name_buf);

FILE *fp = fopen(g_name_buf, "r");
g_name_buf[readed] = 0;
printf("[AFT] file name: %s\n", g_name_buf);
FILE *fp = fopen(g_name_buf, "rb");
if (!fp) {
printf("fail %d\n", __LINE__);
printf("[AFT] fail %d\n", __LINE__);
return;
}

//get file size
int file_size = get_file_size(fp);
printf("send file size %d\n", file_size);
printf("[AFT] send file size %d\n", file_size);
int net_file_size = htonl(file_size);
ret = send_data(connfd, (void *)&net_file_size, sizeof(int));
ret = send_data(connfd, (char *)&net_file_size, sizeof(int));

if (ret < 0) {
printf("error %d\n", __LINE__);
printf("[AFT] error %d\n", errno);
fclose(fp);
return;
}
printf("file size: %d\n", file_size);
printf("[AFT] file size: %d\n", file_size);

readed = 0;
while (readed < file_size) {
ret = fread(g_buf, sizeof(char), file_size - readed, fp);
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("[TCPCLIENT] <--send\t%d bytes\n\n", ret);
printf("[AFT] <--send : %d bytes\n", readed);
fclose(fp);

return;
}

static void receive_file(int connfd)
{
int readed = 0;
int file_name_size = 0;
int ret = read_data(connfd, (char *)&file_name_size, sizeof(int), &readed);
if (ret < 0) {
printf("readed %d %d\n", readed, __LINE__);
return;
}
file_name_size = ntohl(file_name_size);
printf("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("fail %d\n", __LINE__);
return;
}
g_name_buf[file_name_size] = 0;
printf("file name: %s\n", g_name_buf);

int file_size = 0;
ret = read_data(connfd, (char *)&file_size, sizeof(int), &readed);
if (ret < 0) {
printf("error %d\n", __LINE__);
return;
}
file_size = ntohl(file_size);
printf("file size = %d\n", file_size);

FILE *fp = fopen(g_name_buf, "w");
if (!fp) {
printf("fail %d\n", __LINE__);
return;
}

int remain = file_size;
while (remain > 0) {
readed = 0;
int buf_size = remain < BUF_SIZE ? remain : BUF_SIZE;
ret = read_data(connfd, g_buf, buf_size, &readed);
if (ret < 0) {
printf("error %d\n", __LINE__);
break;
}
ret = write_data(fp, g_buf, readed);
if (ret < 0) {
printf("error %d\n", __LINE__);
break;
}
remain -= buf_size;
}
if (remain == 0) {
printf("read complete\n");
}

fclose(fp);
}

static int tcp_server(void)
/****************************************************************************
* sft_main
****************************************************************************/
#ifdef CONFIG_BUILD_KERNEL
int main(int argc, FAR char *argv[])
#else
int sft_main(int argc, char *argv[])
#endif
{
printf("[AFT] Main called");
struct sockaddr_in servaddr;
struct sockaddr_in cliaddr;
int listenfd = -1;
Expand All @@ -229,7 +167,7 @@ static int tcp_server(void)

listenfd = socket(PF_INET, SOCK_STREAM, 0);
if (listenfd < 0) {
printf("[TCPSERV] TCP socket failure %d\n", errno);
printf("[AFT] TCP socket failure %d\n", errno);
return -1;
}

Expand All @@ -241,10 +179,11 @@ static int tcp_server(void)
int reuse = 1;
ret = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));
if (ret < 0) {
printf("[TCPSERV] ERR: setting SO_REUSEADDR\n");
goto out_with_socket;
printf("[AFT] ERR: setting SO_REUSEADDR\n");
close(listenfd);
return 0 ;
}
printf("[TCPSERV] set reusable success\n");
printf("[AFT] set reusable success\n");

/* Connect the socket to the server */
memset(&servaddr, 0, sizeof(servaddr));
Expand All @@ -254,65 +193,32 @@ static int tcp_server(void)

ret = bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
if (ret < 0) {
perror("[TCPSERV] bind fail\n");
goto out_with_socket;
perror("[AFT] bind fail\n");
close(listenfd);
return 0;
}

printf("[TCPSERV] Listening... port %d\n", SFT_PORT);
printf("[AFT] Listening... port %d\n", SFT_PORT);

ret = listen(listenfd, 1024);
if (ret < 0) {
perror("[TCPSERV] listen fail\n");
goto out_with_socket;
perror("[AFT] listen fail\n");
close(listenfd);
return 0;
}

clilen = sizeof(cliaddr);

connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
if (connfd < 0) {
perror("[TCPSERV] accept fail\n");
goto out_with_socket;
perror("[AFT] accept fail\n");
close(listenfd);
return 0;
}
printf("[TCPSERV] Accepted\n");

int direction = 0;
ret = read_data(connfd, (char *)&direction, sizeof(int), &readed);
if (ret < 0) {
printf("read %d %d\n", readed, __LINE__);
close(connfd);
goto out_with_socket;
}
direction = ntohl(direction);
printf("direction %d\n", direction);

if (direction == 0) {
printf("send file\n");
send_file(connfd);
} else if (direction == 1) {
printf("receive file\n");
receive_file(connfd);
} else {
printf("invalid direction\n");
}

printf("[AFT] Accepted\n");
send_file(connfd);
close(connfd);

out_with_socket:
close(listenfd);
return 0 ;
}

/****************************************************************************
* sft_main
****************************************************************************/
#ifdef CONFIG_BUILD_KERNEL
int main(int argc, FAR char *argv[])
#else
int sft_main(int argc, char *argv[])
#endif
{
while (1) {
tcp_server();
}
return 0;
printf("[AFT] Stopping Perfectly\n");
return 0;
}
6 changes: 4 additions & 2 deletions build/configs/rtl8730e/audio/defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1544,7 +1544,7 @@ CONFIG_EXAMPLES_SOUNDPLAYER=y
# CONFIG_EXAMPLES_SELECT_TEST is not set
# CONFIG_EXAMPLES_SENSORBOARD is not set
# CONFIG_EXAMPLES_SETJMP_TEST is not set
# CONFIG_EXAMPLES_SIMPLE_FILE_TRANSFER is not set
CONFIG_EXAMPLES_SIMPLE_FILE_TRANSFER=y

#
# SmartFs Test Applications
Expand Down Expand Up @@ -1573,7 +1573,9 @@ CONFIG_EXAMPLES_WAKEREC=y
# Wifi Manager
#
# CONFIG_EXAMPLES_TAHI is not set
# CONFIG_EXAMPLES_WIFIMANAGER_TEST is not set
CONFIG_EXAMPLES_WIFIMANAGER_TEST=y
CONFIG_WIFIMANAGER_TEST_TRIAL=5
CONFIG_EXAMPLES_WIFIMANAGER_AP_LIST_ITEMS_COUNT=10

#
# Platform-specific Support
Expand Down
19 changes: 19 additions & 0 deletions tools/sft/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Simple File transfer

(I) Connecting board to network of PC:-
Use below commands to connect board to router to which PC is connected.

1. wm_test start

2. wm_test join <SSID> <security> <password> (ex. - wm_test join TPWIFI wpa2_aes 20202020)



(II) Running the sft application to transfer audio from the RTL8730E board to PC:-

For transfering each file use following command on RTL8730E board.
sft
On the PC terminal use minimal_file_transfer.py application present in attachment. Command to be used is below:

python minimal_file_transfer.py -a <IP of RTL8730E board> -s <location at PC> -t <location at board>
python minimal_file_transfer.py -a "192.168.1.248" -s D:\\check_audio\record2.pcm -t /mnt/record2.pcm
Loading

0 comments on commit d13cf76

Please sign in to comment.