Some optimization, starting to add a way to disconnect from remote access on the server side
This commit is contained in:
parent
775ef945bf
commit
3f0fc680ab
@ -11,6 +11,7 @@
|
|||||||
extern int shell_printf(const char *format, ...);
|
extern int shell_printf(const char *format, ...);
|
||||||
extern int wm_printf(const char *fmt,...);
|
extern int wm_printf(const char *fmt,...);
|
||||||
extern u32 tls_mem_get_avail_heapsize(void);
|
extern u32 tls_mem_get_avail_heapsize(void);
|
||||||
|
extern bool disconnect_client(void);
|
||||||
|
|
||||||
extern int demo_bt_enable();
|
extern int demo_bt_enable();
|
||||||
extern int demo_bt_destroy();
|
extern int demo_bt_destroy();
|
||||||
@ -486,6 +487,10 @@ int _telnet(const shell_cmd_t *pcmd, int argc, char *const argv[])
|
|||||||
{
|
{
|
||||||
shell_printf("Disabling client echo"NEW_LINE"%c%c%c"NEW_LINE, 0xFF, 0xFB, 0x01);
|
shell_printf("Disabling client echo"NEW_LINE"%c%c%c"NEW_LINE, 0xFF, 0xFB, 0x01);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
shell_printf("Unknown %s action"NEW_LINE, argv[0]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -494,6 +499,20 @@ int _telnet(const shell_cmd_t *pcmd, int argc, char *const argv[])
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int _exit_remote_access(const shell_cmd_t *pcmd, int argc, char *const argv[])
|
||||||
|
{
|
||||||
|
if(disconnect_client())
|
||||||
|
{
|
||||||
|
shell_printf("Disconnected !"NEW_LINE);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
shell_printf("Not using remote access !"NEW_LINE);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
NANO_SHELL_ADD_CMD(system,
|
NANO_SHELL_ADD_CMD(system,
|
||||||
_system,
|
_system,
|
||||||
"Query system information",
|
"Query system information",
|
||||||
@ -537,4 +556,8 @@ NANO_SHELL_ADD_CMD(bluetooth,
|
|||||||
NANO_SHELL_ADD_CMD(telnet,
|
NANO_SHELL_ADD_CMD(telnet,
|
||||||
_telnet,
|
_telnet,
|
||||||
"Command to set the telnet session up",
|
"Command to set the telnet session up",
|
||||||
" USe this command to set telnet session parameters"NEW_LINE);
|
" Use this command to set telnet session parameters"NEW_LINE);
|
||||||
|
NANO_SHELL_ADD_CMD(exit,
|
||||||
|
_exit_remote_access,
|
||||||
|
"Disconnect from Nano-Shell remote access",
|
||||||
|
" Use this command to disconnect from Nano-Shell remote access"NEW_LINE);
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
#include "wm_include.h"
|
#include "wm_include.h"
|
||||||
|
#include "shell_config.h"
|
||||||
#include "shell_io/static_fifo.h"
|
#include "shell_io/static_fifo.h"
|
||||||
|
|
||||||
static_fifo_declare(uart_char_fifo, 256, unsigned char, char);
|
static_fifo_declare(uart_char_fifo, 256, unsigned char, char);
|
||||||
extern int sendchar(int ch);
|
extern int sendchar(int ch);
|
||||||
extern void network_write(char c);
|
extern bool network_write_char(const char c);
|
||||||
|
extern bool network_write_string(const char *str, size_t size);
|
||||||
extern tls_os_task_t nano_shell_task_handle;
|
extern tls_os_task_t nano_shell_task_handle;
|
||||||
|
|
||||||
s16 uart0_rx_callback(u16 len, void *user_data)
|
s16 uart0_rx_callback(u16 len, void *user_data)
|
||||||
@ -64,11 +66,38 @@ int shell_getc(char *ch)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int shell_printf(const char *format, ...)
|
||||||
|
{
|
||||||
|
static char shell_printf_buffer[CONFIG_SHELL_PRINTF_BUFFER_SIZE];
|
||||||
|
|
||||||
|
int length = 0;
|
||||||
|
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, format);
|
||||||
|
length = vsnprintf(shell_printf_buffer, CONFIG_SHELL_PRINTF_BUFFER_SIZE, format, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
(void)tls_uart_write(TLS_UART_0, shell_printf_buffer, length);
|
||||||
|
(void)tls_uart_write(TLS_UART_1, shell_printf_buffer, length);
|
||||||
|
(void)network_write_string(shell_printf_buffer, length);
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
void shell_puts(const char *str)
|
||||||
|
{
|
||||||
|
(void)shell_printf(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
void shell_putc(char ch)
|
||||||
|
{
|
||||||
|
(void)shell_printf("%c", ch);
|
||||||
|
}
|
||||||
|
|
||||||
bool got_linefeed = false;
|
bool got_linefeed = false;
|
||||||
|
|
||||||
void low_level_write_char(char ch)
|
void low_level_write_char(char ch)
|
||||||
{
|
{
|
||||||
(void)sendchar((int)ch);
|
(void)sendchar((int)ch);
|
||||||
(void)tls_uart_write(TLS_UART_1, &ch, 1);
|
(void)tls_uart_write(TLS_UART_1, &ch, 1);
|
||||||
network_write(ch);
|
(void)network_write_char(ch);
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
extern int shell_printf(const char *format, ...);
|
extern int shell_printf(const char *format, ...);
|
||||||
extern void network_rx_callback(u16 len, char *data);
|
extern void network_rx_callback(u16 len, char *data);
|
||||||
|
|
||||||
|
tls_os_mutex_t *socket_mutex = NULL;
|
||||||
int nano_shell_srv_sock = 0, client_sock = 0;
|
int nano_shell_srv_sock = 0, client_sock = 0;
|
||||||
const char greeting_buffer[] = "\r\n"
|
const char greeting_buffer[] = "\r\n"
|
||||||
" _ _ ____ _ _ _\r\n"
|
" _ _ ____ _ _ _\r\n"
|
||||||
@ -22,17 +23,73 @@ const char greeting_buffer[] = "\r\n"
|
|||||||
" * Source: https://github.com/lebinlv/nano-shell\r\n"
|
" * Source: https://github.com/lebinlv/nano-shell\r\n"
|
||||||
" * Copyright: (c) Liber 2020\r\n"
|
" * Copyright: (c) Liber 2020\r\n"
|
||||||
"\r\n";
|
"\r\n";
|
||||||
|
char identity_buffer[250] = "";
|
||||||
|
|
||||||
void network_write(char c)
|
bool network_write_char(const char c)
|
||||||
{
|
{
|
||||||
|
bool toReturn = false;
|
||||||
|
|
||||||
|
tls_os_mutex_acquire(socket_mutex, 0);
|
||||||
if(client_sock > 0)
|
if(client_sock > 0)
|
||||||
{
|
{
|
||||||
|
|
||||||
if(send(client_sock, &c, 1, 0) < 0)
|
if(send(client_sock, &c, 1, 0) < 0)
|
||||||
{
|
{
|
||||||
// Failed to send data to client because he probably disconnected
|
// Failed to send data to client because he probably disconnected
|
||||||
// or the connection got broken
|
// or the connection broke
|
||||||
|
client_sock = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
toReturn = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
tls_os_mutex_release(socket_mutex);
|
||||||
|
|
||||||
|
return toReturn;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool network_write_string(const char *str, size_t size)
|
||||||
|
{
|
||||||
|
bool toReturn = false;
|
||||||
|
|
||||||
|
if(client_sock > 0)
|
||||||
|
{
|
||||||
|
tls_os_mutex_acquire(socket_mutex, 0);
|
||||||
|
if(send(client_sock, str, size, 0) < 0)
|
||||||
|
{
|
||||||
|
// Failed to send data to client because he probably disconnected
|
||||||
|
// or the connection broke
|
||||||
|
client_sock = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
toReturn = true;
|
||||||
|
}
|
||||||
|
tls_os_mutex_release(socket_mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
return toReturn;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool disconnect_client(void)
|
||||||
|
{
|
||||||
|
bool toReturn = false;
|
||||||
|
|
||||||
|
if(client_sock > 0)
|
||||||
|
{
|
||||||
|
if(shutdown(client_sock, SHUT_WR) < 0)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
toReturn = true;
|
||||||
|
}
|
||||||
|
client_sock = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return toReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
void nano_shell_server_task(void* param)
|
void nano_shell_server_task(void* param)
|
||||||
@ -41,10 +98,18 @@ void nano_shell_server_task(void* param)
|
|||||||
|
|
||||||
bool setup_error = false;
|
bool setup_error = false;
|
||||||
char recv_buffer[256] = "";
|
char recv_buffer[256] = "";
|
||||||
//We setup the listening socket :
|
|
||||||
struct sockaddr_in nano_shell_srv_addr = { .sin_family = AF_INET, .sin_addr.s_addr = INADDR_ANY, .sin_port = htons(NANO_SHELL_SERVER_PORT)}, client_addr;
|
struct sockaddr_in nano_shell_srv_addr = { .sin_family = AF_INET, .sin_addr.s_addr = INADDR_ANY, .sin_port = htons(NANO_SHELL_SERVER_PORT)}, client_addr;
|
||||||
socklen_t sockaddr_in_len = sizeof(struct sockaddr_in);
|
socklen_t sockaddr_in_len = sizeof(struct sockaddr_in);
|
||||||
|
|
||||||
|
//We initialize the mutex
|
||||||
|
if(tls_os_mutex_create(0, &socket_mutex) != TLS_OS_SUCCESS)
|
||||||
|
{
|
||||||
|
shell_printf("Failed to create the mutex."NEW_LINE);
|
||||||
|
setup_error = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//We setup the listening socket :
|
||||||
if((nano_shell_srv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
|
if((nano_shell_srv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
|
||||||
{
|
{
|
||||||
shell_printf("Failed to create nano_shell listening socket."NEW_LINE);
|
shell_printf("Failed to create nano_shell listening socket."NEW_LINE);
|
||||||
@ -72,20 +137,33 @@ void nano_shell_server_task(void* param)
|
|||||||
|
|
||||||
for(;;)
|
for(;;)
|
||||||
{
|
{
|
||||||
if((client_sock = accept(nano_shell_srv_sock, (struct sockaddr *)&client_addr, &sockaddr_in_len)) < 0)
|
client_sock = accept(nano_shell_srv_sock, (struct sockaddr *)&client_addr, &sockaddr_in_len);
|
||||||
|
|
||||||
|
if(client_sock < 0)
|
||||||
{
|
{
|
||||||
shell_printf("Failed to accept incoming connection."NEW_LINE);
|
shell_printf("Failed to accept incoming connection."NEW_LINE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(send(client_sock, greeting_buffer, sizeof greeting_buffer, 0) < 0)
|
if(!network_write_string(greeting_buffer, sizeof greeting_buffer))
|
||||||
{
|
{
|
||||||
shell_printf("Failed to send greetings to client - errno(%d)."NEW_LINE, errno);
|
shell_printf("Failed to send greetings to client - errno(%d)."NEW_LINE, errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
sprintf(identity_buffer, "Connected from : %u.%u.%u.%u:%u"NEW_LINE NEW_LINE,((u8 *)&client_addr.sin_addr)[0],
|
||||||
|
((u8 *)&client_addr.sin_addr)[1],
|
||||||
|
((u8 *)&client_addr.sin_addr)[2],
|
||||||
|
((u8 *)&client_addr.sin_addr)[3],
|
||||||
|
ntohs(client_addr.sin_port));
|
||||||
|
|
||||||
|
if(!network_write_string(identity_buffer, strlen(identity_buffer)))
|
||||||
|
{
|
||||||
|
shell_printf("Failed to send greetings to client - errno(%d)."NEW_LINE, errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(;client_sock > 0;)
|
for(;client_sock > 0;)
|
||||||
{
|
{
|
||||||
int result = recv(client_sock, recv_buffer, 255, 0);
|
int result = recv(client_sock, recv_buffer, 255, 0);
|
||||||
|
|
||||||
if(result < 0)
|
if(result < 0)
|
||||||
{
|
{
|
||||||
shell_printf("Failed to receive data from client - errno(%d)."NEW_LINE"Closing connection."NEW_LINE, errno);
|
shell_printf("Failed to receive data from client - errno(%d)."NEW_LINE"Closing connection."NEW_LINE, errno);
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
#define NANO_SHELL_SERVER_PORT 21
|
#define NANO_SHELL_SERVER_PORT 23
|
||||||
|
|
||||||
void nano_shell_server_task(void* param);
|
void nano_shell_server_task(void* param);
|
Loading…
Reference in New Issue
Block a user