Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AEC support with Syntiant 112.3.6 SDK #6540

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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;
}
7 changes: 5 additions & 2 deletions build/configs/rtl8730e/audio/defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ CONFIG_AUDIO_NDP120=y
#
CONFIG_NDP120=y
CONFIG_NDP120_AEC_SUPPORT=y
CONFIG_NDP120_ALIVE_CHECK=y

#
# LCD Driver Support
Expand Down Expand Up @@ -1544,7 +1545,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 +1574,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
Loading