783 lines
22 KiB
C
783 lines
22 KiB
C
|
/****************************************************************************
|
||
|
|
||
|
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
|
||
|
|
||
|
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
|
||
|
be copied by any method or incorporated into another program without
|
||
|
the express written consent of Aerospace C.Power. This Information or any portion
|
||
|
thereof remains the property of Aerospace C.Power. The Information contained herein
|
||
|
is believed to be accurate and Aerospace C.Power assumes no responsibility or
|
||
|
liability for its use in any way and conveys no license or title under
|
||
|
any patent or copyright and makes no representation or warranty that this
|
||
|
Information is free from patent or copyright infringement.
|
||
|
|
||
|
****************************************************************************/
|
||
|
|
||
|
#include "os_task.h"
|
||
|
#include "os_utils.h"
|
||
|
#include "os_mem.h"
|
||
|
|
||
|
#include "communicator.h"
|
||
|
|
||
|
#include "iot_errno.h"
|
||
|
#include "iot_dbglog_api.h"
|
||
|
#include "iot_io.h"
|
||
|
#include "iot_uart_h.h"
|
||
|
#include "iot_frame_parse.h"
|
||
|
#include "iot_config.h"
|
||
|
|
||
|
#include "iot_cli_module_config.h"
|
||
|
|
||
|
#define MAX_IP_LIST_NUM 100
|
||
|
#define MAX_IP_LEN 64
|
||
|
|
||
|
#ifdef CLI_MINI_MODE_ENABLE
|
||
|
#define COMMUNI_UART_BUF_SIZE 512
|
||
|
#else
|
||
|
#ifdef CLI_FULL_FEATURE_ENABLE
|
||
|
#if RUN_IN_PSRAM
|
||
|
#define COMMUNI_UART_BUF_SIZE 4096
|
||
|
#else /* RUN_IN_PSRAM */
|
||
|
#define COMMUNI_UART_BUF_SIZE 2048
|
||
|
#endif /* RUN_IN_PSRAM */
|
||
|
#else
|
||
|
#define COMMUNI_UART_BUF_SIZE 2048
|
||
|
#endif
|
||
|
#endif // CLI_MINI_MODE_ENABLE
|
||
|
|
||
|
#if HW_PLATFORM == HW_PLATFORM_SIMU
|
||
|
#include "simulator_txrx.h"
|
||
|
#else
|
||
|
#include "iot_uart.h"
|
||
|
int cli_uart_fd;
|
||
|
#define CLI_UART_PORT 1
|
||
|
os_mutex_h cli_uart_mutex;
|
||
|
|
||
|
#endif
|
||
|
extern uint8_t cli_ftm_mode;
|
||
|
commnucator_t communicator;
|
||
|
static void communicator_rx_task(void *pvParameters);
|
||
|
|
||
|
#if HW_PLATFORM == HW_PLATFORM_SIMU
|
||
|
uint8_t cli_sock_recv_buf[1024 * 3];
|
||
|
#elif (IOT_CLI_INF_MODE == IOT_CLI_INF_MODE_LWIP_SOCKET)
|
||
|
uint8_t cli_sock_recv_buf[COMMUNI_UART_BUF_SIZE];
|
||
|
#endif
|
||
|
|
||
|
#if ((HW_PLATFORM == HW_PLATFORM_SIMU) || (IOT_CLI_INF_MODE == IOT_CLI_INF_MODE_LWIP_SOCKET))
|
||
|
static void communicator_rx_cli_task(void *lpparameters);
|
||
|
#endif
|
||
|
|
||
|
#if HW_PLATFORM == HW_PLATFORM_SIMU
|
||
|
static int8_t is_ipaddress_accept(char* ipaddr);
|
||
|
extern uint32_t g_ip_addr;
|
||
|
|
||
|
int socket_init(commnuicator_handle pcommnu, caller_type_t caller, uint8_t work_mode)
|
||
|
{
|
||
|
uint32_t bind_port = (uint32_t)UDP_BROADCAST_PORT_NUMBER;
|
||
|
if (work_mode == WORKMODE_RF)
|
||
|
{
|
||
|
bind_port = (uint32_t)UDP_BROADCAST_PORT_NUMBER_RF;
|
||
|
}
|
||
|
os_sock_addr bind_address;
|
||
|
uint32_t recv_timeout = OS_TICK(999000);
|
||
|
if (caller == CALLER_HW_SIMULATOR)
|
||
|
{
|
||
|
pcommnu->udprecvsocket = os_udpsocket();
|
||
|
if (pcommnu->udprecvsocket == OS_INVALID_SOCKET)
|
||
|
{
|
||
|
return -1;
|
||
|
}
|
||
|
os_setsock_recvtimeout_opt(pcommnu->udprecvsocket, recv_timeout);
|
||
|
|
||
|
bind_address.sin_port =
|
||
|
(uint16_t)bind_port & 0xffffUL;
|
||
|
bind_address.sin_port = os_htons(bind_address.sin_port);
|
||
|
|
||
|
/* Bind the socket to the port that the client task will send to. */
|
||
|
os_sock_bind(pcommnu->udprecvsocket, bind_address);
|
||
|
}
|
||
|
#if 0
|
||
|
if (!pcommnu->csrecvsock)
|
||
|
{
|
||
|
pcommnu->csrecvsock = os_udpsocket();
|
||
|
if (pcommnu->csrecvsock == OS_INVALID_SOCKET)
|
||
|
{
|
||
|
return -1;
|
||
|
}
|
||
|
os_setsock_recvtimeout_opt(pcommnu->csrecvsock, recv_timeout);
|
||
|
|
||
|
bind_address.sin_port =
|
||
|
(uint16_t)((uint32_t)UDP_CHANNEL_SIMULATION_RECV_PORT) & 0xffffUL;
|
||
|
bind_address.sin_port = os_htons(bind_address.sin_port);
|
||
|
|
||
|
/* Bind the socket to the port that the client task will send to. */
|
||
|
os_sock_bind(pcommnu->csrecvsock, bind_address);
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
if (!pcommnu->udpsendsocket)
|
||
|
{
|
||
|
pcommnu->udpsendsocket = os_udpsocket();
|
||
|
if (pcommnu->udpsendsocket == OS_INVALID_SOCKET)
|
||
|
{
|
||
|
return -1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (caller == CALLER_PLC_CLI)
|
||
|
{
|
||
|
pcommnu->clirecvsocket = os_udpsocket();
|
||
|
if (pcommnu->clirecvsocket == OS_INVALID_SOCKET) {
|
||
|
return -1;
|
||
|
}
|
||
|
os_setsock_recvtimeout_opt(pcommnu->clirecvsocket, recv_timeout);
|
||
|
|
||
|
bind_address.sin_port =
|
||
|
(uint16_t)((uint32_t)UDP_CLI_RECV_PORT) & 0xffffUL;
|
||
|
bind_address.sin_port = os_htons(bind_address.sin_port);
|
||
|
/* Bind the socket to the port that the client task will send to. */
|
||
|
os_sock_bind(pcommnu->clirecvsocket, bind_address);
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
int socket_deinit(commnuicator_handle pcommnu)
|
||
|
{
|
||
|
#if HW_PLATFORM == HW_PLATFORM_SIMU
|
||
|
if (pcommnu->udpsendsocket)
|
||
|
{
|
||
|
os_close_socket(pcommnu->udpsendsocket);
|
||
|
}
|
||
|
if (pcommnu->udprecvsocket)
|
||
|
{
|
||
|
os_close_socket(pcommnu->udprecvsocket);
|
||
|
}
|
||
|
if (pcommnu->csrecvsock)
|
||
|
{
|
||
|
os_close_socket(pcommnu->csrecvsock);
|
||
|
}
|
||
|
if (pcommnu->clirecvsocket)
|
||
|
{
|
||
|
os_close_socket(pcommnu->clirecvsocket);
|
||
|
}
|
||
|
#endif
|
||
|
return 0;
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
void* Find(const uint8_t *pSour, const uint8_t *pDest, int SourLen, int DestLen)
|
||
|
{
|
||
|
int i = 0, j = 0;
|
||
|
while (i < SourLen && j < DestLen)
|
||
|
{
|
||
|
if (*(pSour + i) == *(pDest + j))
|
||
|
{
|
||
|
i++;
|
||
|
j++;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
i = i - j + 1;
|
||
|
j = 0;
|
||
|
}
|
||
|
}
|
||
|
if (j == DestLen)
|
||
|
{
|
||
|
return (void*)(pSour + (i - DestLen));
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
bool_t IsFullFrame(commnuicator_handle pcommnu, uint8_t* pdata_buffer, uint32_t data_buf_len, uint8_t* pread_buffer, uint32_t* pread_buf_len)
|
||
|
{
|
||
|
//static char Buf[MAX_SERIAL_BUFFER * 2];
|
||
|
static unsigned long nFrameStart = 0;
|
||
|
static unsigned long nFrameEnd = 0;
|
||
|
static unsigned long nCurrectPos = 0;
|
||
|
uint8_t *pdest = NULL;
|
||
|
|
||
|
//printf("**************nCurrentPos=(%d)***********\n", nCurrectPos);
|
||
|
if (!pread_buf_len)
|
||
|
return false;
|
||
|
if (pread_buffer && (pread_buf_len != 0))
|
||
|
{
|
||
|
if (nCurrectPos + *pread_buf_len > data_buf_len)
|
||
|
{
|
||
|
//zero the memory
|
||
|
os_mem_set((void*)pdata_buffer, 0, data_buf_len);
|
||
|
nFrameStart = 0;
|
||
|
nCurrectPos = 0;
|
||
|
return false;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
os_mem_cpy((void*)(pdata_buffer+nCurrectPos), pread_buffer, *pread_buf_len);
|
||
|
nCurrectPos = nCurrectPos + *pread_buf_len;
|
||
|
os_mem_set(pdata_buffer+nCurrectPos, 0, data_buf_len - nCurrectPos);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
framebegin:
|
||
|
pdest = (uint8_t*)Find(pdata_buffer, (uint8_t*)"##", data_buf_len, 2);
|
||
|
if (pdest)
|
||
|
{
|
||
|
nFrameStart = (unsigned long)(pdest - pdata_buffer);
|
||
|
|
||
|
//printf("found ##, (nCurrectPos=%d, dwRealRead=%d)\n", nCurrectPos, *pread_buf_len);
|
||
|
}
|
||
|
else//didn't find the start frame"##"
|
||
|
{
|
||
|
pdest = (uint8_t*)Find(pdata_buffer, (uint8_t*)"#", data_buf_len, 2);
|
||
|
if (pdest) //fount the # wait for next package
|
||
|
{
|
||
|
os_mem_set(pdata_buffer, 0, data_buf_len);//zero the memory
|
||
|
os_mem_cpy(pdata_buffer, "#", 1);
|
||
|
nFrameStart = 0;
|
||
|
nCurrectPos = 1;
|
||
|
//printf("found #, (nCurrectPos=%d, dwRealRead=%d)\n", nCurrectPos, *pread_buf_len);
|
||
|
return false;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
os_mem_set(pdata_buffer, 0, data_buf_len);//zero the memory
|
||
|
nFrameStart = 0;
|
||
|
nCurrectPos = 0;
|
||
|
//printf("no ## drop the data, (nCurrectPos=%d, dwRealRead=%d)\n", nCurrectPos, *pread_buf_len);
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
frameend:
|
||
|
|
||
|
pdest = (uint8_t*)Find(pdata_buffer, (uint8_t*)"@@", data_buf_len, 2);
|
||
|
if (pdest)
|
||
|
{
|
||
|
nFrameEnd = (unsigned long)(pdest - pdata_buffer + 2);
|
||
|
//printf("found @@, (nCurrectPos=%d, dwRealRead=%d)\n", nCurrectPos, dwRealRead);
|
||
|
|
||
|
if (nFrameStart == nFrameEnd) //"@@ is ahaead of ## move ## to head of buffer
|
||
|
{
|
||
|
|
||
|
os_mem_cpy(pdata_buffer, pdata_buffer+nFrameStart, nCurrectPos - nFrameStart);
|
||
|
|
||
|
|
||
|
nCurrectPos = nCurrectPos - nFrameStart;
|
||
|
*pread_buf_len = *pread_buf_len - nFrameStart;
|
||
|
nFrameStart = 0;
|
||
|
os_mem_set(pdata_buffer, 0, data_buf_len - nCurrectPos);
|
||
|
goto frameend;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
*pread_buf_len = nFrameEnd - nFrameStart;
|
||
|
//memcpy(pReadBuf, &Buf[nFrameStart], dwRealRead);
|
||
|
|
||
|
//memcpy(Buf1, &Buf[nFrameStart], nFrameEnd - nFrameStart);
|
||
|
//printf("**************get a full command, size is %d ***********, (nCurrectPos=%d, dwRealRead=%d)\n", nFrameEnd - nFrameStart, nCurrectPos, dwRealRead);
|
||
|
//ParseCliCommand(pdata_buffer + 2, nFrameEnd - nFrameStart - 2); //+2 is for removing the ## -2 is for removing the @@
|
||
|
iot_uart_head* head = (iot_uart_head*)(pdata_buffer + 2);
|
||
|
pcommnu->rx_ctrl_cb(pcommnu->ref_caller_ptr, pdata_buffer + 2 + sizeof(head), head->data_len, 0);
|
||
|
}
|
||
|
|
||
|
|
||
|
nFrameStart = nFrameEnd;
|
||
|
nCurrectPos = nCurrectPos - *pread_buf_len;
|
||
|
if (nCurrectPos > 0)
|
||
|
{
|
||
|
|
||
|
*pread_buf_len = nCurrectPos;
|
||
|
|
||
|
//printf("(nCurrectPos > 0) goto begin, (nCurrectPos=%d, dwRealRead=%d)\n", nCurrectPos, dwRealRead);
|
||
|
os_mem_cpy(pdata_buffer, pdata_buffer+nFrameEnd, nCurrectPos);
|
||
|
os_mem_set(pdata_buffer+nCurrectPos, 0, data_buf_len - nCurrectPos);
|
||
|
goto framebegin;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
commnuicator_handle com_handle;
|
||
|
|
||
|
void receive_func(uint8_t* buffer, uint32_t buffer_len, bool_t is_full_frame,
|
||
|
uint32_t invalid_data_len)
|
||
|
{
|
||
|
iot_printf("communicate receive_func(len=%d, is full%d, invalid_data%d)\n",
|
||
|
buffer_len, is_full_frame, invalid_data_len);
|
||
|
if (com_handle && com_handle->rx_ctrl_cb && is_full_frame)
|
||
|
com_handle->rx_ctrl_cb(com_handle->ref_caller_ptr,
|
||
|
iot_frame_data(buffer, com_handle->uart_frame)+ invalid_data_len,
|
||
|
iot_frame_data_len(buffer_len, com_handle->uart_frame)- invalid_data_len, NULL);
|
||
|
}
|
||
|
|
||
|
#if HW_PLATFORM == HW_PLATFORM_SIMU
|
||
|
int uart_win_init(commnuicator_handle pcommnu)
|
||
|
{
|
||
|
if (!pcommnu)
|
||
|
return -1;
|
||
|
|
||
|
pcommnu->uart_handle = (void*)iot_uart_open(pcommnu->com_num, receive_func,
|
||
|
COMMUNI_UART_BUF_SIZE, &(pcommnu->uart_frame));
|
||
|
|
||
|
com_handle = pcommnu;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
uint8_t uart_win_deinit(commnuicator_handle pcommnu)
|
||
|
{
|
||
|
if (!pcommnu)
|
||
|
return 0;
|
||
|
|
||
|
CloseHandle(((iot_uart_h_t*)(pcommnu->uart_handle))->uart_handle);
|
||
|
((iot_uart_h_t*)(pcommnu->uart_handle))->uart_handle = INVALID_HANDLE_VALUE;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
uint32_t uart_win_send(commnuicator_handle pcommnu, iot_pkt_t *pkt)
|
||
|
{
|
||
|
return iot_uart_send(pcommnu->uart_handle, pkt,
|
||
|
&(pcommnu->uart_frame));
|
||
|
}
|
||
|
|
||
|
#else
|
||
|
bool_t uart_init(commnuicator_handle pcommnu)
|
||
|
{
|
||
|
bool_t ret = false;
|
||
|
|
||
|
if (!pcommnu)
|
||
|
return ret;
|
||
|
|
||
|
pcommnu->uart_handle = (void*)iot_uart_open(pcommnu->com_num, receive_func,
|
||
|
COMMUNI_UART_BUF_SIZE, &(pcommnu->uart_frame));
|
||
|
|
||
|
if (pcommnu->uart_handle)
|
||
|
{
|
||
|
ret = true;
|
||
|
com_handle = pcommnu;
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
uint8_t uart_deinit(commnuicator_handle pcommnu)
|
||
|
{
|
||
|
iot_uart_close(pcommnu->uart_handle);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
uint32_t uart_receive_bytes(uint8_t *buf, uint32_t len)
|
||
|
{
|
||
|
//return 0;
|
||
|
return uart_read(cli_uart_fd, buf, len);
|
||
|
}
|
||
|
|
||
|
uint8_t uart_receive(commnuicator_handle pcommnu, uint8_t* buffer_read, uint32_t buffer_len)
|
||
|
{
|
||
|
//return 0;
|
||
|
uint32_t dwWantRead = 500;
|
||
|
uint32_t dwRealRead = 0;
|
||
|
uint8_t* pReadBuf = (uint8_t *)os_mem_malloc(IOT_COMMUNICATOR_MID, dwWantRead);
|
||
|
|
||
|
if (!pReadBuf)
|
||
|
return 1;
|
||
|
|
||
|
if (!pcommnu || !buffer_read)
|
||
|
return 0;
|
||
|
os_mem_set(buffer_read, 0, buffer_len);
|
||
|
|
||
|
while(1){
|
||
|
os_mem_set(pReadBuf, 0, dwWantRead);
|
||
|
dwRealRead = uart_receive_bytes(pReadBuf, dwWantRead);
|
||
|
if (dwRealRead>0){
|
||
|
iot_printf("12321(%d)\n", dwRealRead);
|
||
|
if (IsFullFrame(pcommnu, buffer_read, buffer_len, pReadBuf, (uint32_t*)&dwRealRead))
|
||
|
{
|
||
|
dwWantRead = 500;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
os_mem_free(pReadBuf);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
uint32_t uart_send(commnuicator_handle pcommnu, iot_pkt_t *pkt)
|
||
|
{
|
||
|
|
||
|
return iot_uart_send((iot_uart_h_t*)pcommnu->uart_handle, pkt,
|
||
|
&(pcommnu->uart_frame));
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
#if (IOT_CLI_INF_MODE == IOT_CLI_INF_MODE_LWIP_SOCKET)
|
||
|
void iot_lwip_socket_init(commnuicator_handle pcommnu)
|
||
|
{
|
||
|
pcommnu->lwip_tcp_raw_socket = cli_open_lwip_tcp_socket(UDP_CLI_RECV_PORT);
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
uint8_t interfaces_init(commnuicator_handle pcommnu, caller_type_t caller, uint8_t work_mode)
|
||
|
{
|
||
|
(void)caller;
|
||
|
#if HW_PLATFORM == HW_PLATFORM_SIMU
|
||
|
if (pcommnu->type == COMMUNICATOR_SOCKET)
|
||
|
socket_init(pcommnu, caller, work_mode);
|
||
|
else if (pcommnu->type == COMMUNICATOR_UART)
|
||
|
uart_win_init(pcommnu);
|
||
|
#else
|
||
|
if (cli_ftm_mode == 1) {
|
||
|
pcommnu->type = COMMUNICATOR_UART;
|
||
|
uart_init(pcommnu);
|
||
|
}
|
||
|
else {
|
||
|
#if (IOT_CLI_INF_MODE == IOT_CLI_INF_MODE_LWIP_SOCKET)
|
||
|
pcommnu->type = COMMUNICATOR_LWIP_SOCKET;
|
||
|
iot_lwip_socket_init(pcommnu);
|
||
|
#elif (IOT_CLI_INF_MODE == IOT_CLI_INF_MODE_APP)
|
||
|
pcommnu->type = COMMUNICATOR_APP;
|
||
|
#else
|
||
|
pcommnu->type = COMMUNICATOR_UART;
|
||
|
uart_init(pcommnu);
|
||
|
#endif
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
uint8_t interfaces_deinit(commnuicator_handle pcommnu)
|
||
|
{
|
||
|
#if HW_PLATFORM == HW_PLATFORM_SIMU
|
||
|
if (pcommnu->type == COMMUNICATOR_SOCKET)
|
||
|
socket_deinit(pcommnu);
|
||
|
else if (pcommnu->type == COMMUNICATOR_UART)
|
||
|
uart_win_deinit(pcommnu);
|
||
|
#else
|
||
|
if (pcommnu->type == COMMUNICATOR_UART)
|
||
|
uart_deinit(pcommnu);
|
||
|
#endif
|
||
|
return 0;
|
||
|
}
|
||
|
uint8_t commnunicator_init(commnuicator_handle pcommnu,
|
||
|
void *caller_module, caller_type_t caller, uint8_t work_mode)
|
||
|
{
|
||
|
if (pcommnu->is_initiated == 0) {
|
||
|
interfaces_init(pcommnu, caller, work_mode);
|
||
|
}
|
||
|
pcommnu->is_initiated = 1;
|
||
|
pcommnu->ref_caller_ptr = caller_module;
|
||
|
|
||
|
if (caller == CALLER_HW_SIMULATOR) {
|
||
|
os_create_task(communicator_rx_task, pcommnu,
|
||
|
COMMUNICATOR_RX_TASK_PRIO);
|
||
|
}
|
||
|
#if ((HW_PLATFORM == HW_PLATFORM_SIMU) || (IOT_CLI_INF_MODE == IOT_CLI_INF_MODE_LWIP_SOCKET))
|
||
|
if (caller == CALLER_PLC_CLI) {
|
||
|
os_create_task(communicator_rx_cli_task, pcommnu,
|
||
|
COMMUNICATOR_RX_CLI_TASK_PRIO);
|
||
|
}
|
||
|
#endif
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
uint8_t commnunicator_deinit(commnuicator_handle pcommnu)
|
||
|
{
|
||
|
if (pcommnu->is_initiated == 1) {
|
||
|
interfaces_deinit(pcommnu);
|
||
|
}
|
||
|
pcommnu->is_initiated = 0;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int commnuicator_register_callback_packet_recv(commnuicator_handle pcommnu,
|
||
|
communicator_rx_cb rx_cb)
|
||
|
{
|
||
|
if (NULL == pcommnu)
|
||
|
{
|
||
|
iot_printf("%s: invalid parameter\n", __FUNCTION__);
|
||
|
return -1;
|
||
|
}
|
||
|
pcommnu->rx_ctrl_cb = rx_cb;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int commnuicator_register_callback_tx_comp(commnuicator_handle pcommnu,
|
||
|
communicator_tx_comp_cb tx_comp_cb)
|
||
|
{
|
||
|
if (NULL == pcommnu)
|
||
|
{
|
||
|
iot_printf("%s: invalid parameter\n", __FUNCTION__);
|
||
|
return -1;
|
||
|
}
|
||
|
pcommnu->tx_comp_cb = tx_comp_cb;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int commnuicator_register_cb_ext_tx_comp(commnuicator_handle pcommnu,
|
||
|
communicator_tx_comp_cb tx_comp_cb)
|
||
|
{
|
||
|
if (NULL == pcommnu)
|
||
|
{
|
||
|
iot_printf("%s: invalid parameter\n", __FUNCTION__);
|
||
|
return -1;
|
||
|
}
|
||
|
pcommnu->ext_tx_cb = tx_comp_cb;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int commnuicator_deregister_callback_packet_recv(commnuicator_handle pcommnu)
|
||
|
{
|
||
|
if (NULL == pcommnu)
|
||
|
{
|
||
|
iot_printf("%s: invalid parameter\n", __FUNCTION__);
|
||
|
return -1;
|
||
|
}
|
||
|
pcommnu->rx_ctrl_cb = NULL;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/* return - the num of bytes txed */
|
||
|
int package_send(commnuicator_handle pcommnu, uint32_t port,
|
||
|
iot_pkt_t *pkt)
|
||
|
{
|
||
|
if (!pcommnu || !pkt) {
|
||
|
iot_printf("%s: invalid parameter\n", __FUNCTION__);
|
||
|
if (pkt)
|
||
|
iot_pkt_free(pkt);
|
||
|
return 0;
|
||
|
}
|
||
|
#if HW_PLATFORM == HW_PLATFORM_SIMU
|
||
|
if (pcommnu->type == COMMUNICATOR_SOCKET)
|
||
|
{
|
||
|
uint32_t cur_sent_len = 0;
|
||
|
uint32_t sent_len_all = 0;
|
||
|
uint32_t len_to_send = 0;
|
||
|
os_sock_addr dest_addr;
|
||
|
uint32_t buffer_len = iot_pkt_data_len(pkt);
|
||
|
uint8_t *buffer_to_send = iot_pkt_data(pkt);
|
||
|
dest_addr.sin_addr = 0xffffffffUL;
|
||
|
dest_addr.sin_port =
|
||
|
(uint16_t)((uint32_t)port) & 0xffffUL;
|
||
|
dest_addr.sin_port = os_htons(dest_addr.sin_port);
|
||
|
IOT_ASSERT(buffer_len <= RTOS_MAX_UDP_PAYLOAD);
|
||
|
|
||
|
while (sent_len_all < buffer_len)
|
||
|
{
|
||
|
len_to_send = buffer_len - sent_len_all;
|
||
|
|
||
|
cur_sent_len = os_sendto(pcommnu->udpsendsocket,
|
||
|
(void *)&buffer_to_send[sent_len_all], len_to_send, 0,
|
||
|
&dest_addr, sizeof(dest_addr));
|
||
|
sent_len_all += cur_sent_len;
|
||
|
}
|
||
|
if (pcommnu->tx_comp_cb)
|
||
|
{
|
||
|
pcommnu->tx_comp_cb(pcommnu->ref_caller_ptr,
|
||
|
(void *)buffer_to_send, buffer_len);
|
||
|
}
|
||
|
iot_pkt_free(pkt);
|
||
|
return sent_len_all;
|
||
|
}
|
||
|
else if (pcommnu->type == COMMUNICATOR_UART)
|
||
|
{
|
||
|
return uart_win_send(pcommnu, pkt);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
iot_pkt_free(pkt);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
#else
|
||
|
if (pcommnu->type == COMMUNICATOR_UART)
|
||
|
{
|
||
|
return uart_send(pcommnu, pkt);
|
||
|
}
|
||
|
#if (IOT_CLI_INF_MODE == IOT_CLI_INF_MODE_LWIP_SOCKET)
|
||
|
else if (pcommnu->type == COMMUNICATOR_LWIP_SOCKET)
|
||
|
{
|
||
|
uint32_t ret = 0;
|
||
|
uint32_t buffer_len = iot_pkt_data_len(pkt);
|
||
|
uint8_t *buffer_to_send = iot_pkt_data(pkt);
|
||
|
|
||
|
ret = cli_lwip_send(pcommnu->lwip_tcp_con_socket,
|
||
|
buffer_to_send, buffer_len);
|
||
|
iot_pkt_free(pkt);
|
||
|
return ret;
|
||
|
}
|
||
|
#endif
|
||
|
#if (IOT_CLI_INF_MODE == IOT_CLI_INF_MODE_APP)
|
||
|
else if (pcommnu->type == COMMUNICATOR_APP)
|
||
|
{
|
||
|
if (NULL != pcommnu->ext_tx_cb) {
|
||
|
pcommnu->ext_tx_cb(NULL, iot_pkt_data(pkt),
|
||
|
iot_pkt_data_len(pkt));
|
||
|
}
|
||
|
iot_pkt_free(pkt);
|
||
|
return 0;
|
||
|
}
|
||
|
#endif
|
||
|
else
|
||
|
{
|
||
|
iot_pkt_free(pkt);
|
||
|
return 0;
|
||
|
}
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
int package_send_with_rawdata(commnuicator_handle pcommnu, uint32_t port,
|
||
|
uint8_t* data, uint32_t data_len)
|
||
|
{
|
||
|
if (!pcommnu || !data) {
|
||
|
iot_printf("%s: invalid parameter\n", __FUNCTION__);
|
||
|
return 0;
|
||
|
}
|
||
|
#if HW_PLATFORM == HW_PLATFORM_SIMU
|
||
|
if (pcommnu->type == COMMUNICATOR_SOCKET)
|
||
|
{
|
||
|
uint32_t cur_sent_len = 0;
|
||
|
uint32_t sent_len_all = 0;
|
||
|
uint32_t len_to_send = 0;
|
||
|
os_sock_addr dest_addr;
|
||
|
dest_addr.sin_addr = 0xffffffffUL;
|
||
|
dest_addr.sin_port =
|
||
|
(uint16_t)((uint32_t)port) & 0xffffUL;
|
||
|
dest_addr.sin_port = os_htons(dest_addr.sin_port);
|
||
|
IOT_ASSERT(data_len <= RTOS_MAX_UDP_PAYLOAD);
|
||
|
|
||
|
while (sent_len_all < data_len)
|
||
|
{
|
||
|
len_to_send = data_len - sent_len_all;
|
||
|
|
||
|
cur_sent_len = os_sendto(pcommnu->udpsendsocket,
|
||
|
(void *)&data[sent_len_all], len_to_send, 0,
|
||
|
&dest_addr, sizeof(dest_addr));
|
||
|
sent_len_all += cur_sent_len;
|
||
|
}
|
||
|
if (pcommnu->tx_comp_cb)
|
||
|
{
|
||
|
pcommnu->tx_comp_cb(pcommnu->ref_caller_ptr,
|
||
|
(void *)data, data_len);
|
||
|
}
|
||
|
return sent_len_all;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
#else
|
||
|
return 0;
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
static void communicator_rx_task(void *lpparameters)
|
||
|
{
|
||
|
#if HW_PLATFORM == HW_PLATFORM_SIMU
|
||
|
int32_t lBytes;
|
||
|
os_sock_addr client_addr;
|
||
|
uint32_t client_len = sizeof(client_addr);
|
||
|
uint8_t recvbuff[1024 * 3];
|
||
|
int8_t buffer[MAX_IP_ADDR_LEN] = { 0 };
|
||
|
commnuicator_handle commu = (commnuicator_handle)lpparameters;
|
||
|
if (!commu)
|
||
|
return;
|
||
|
|
||
|
for (;; )
|
||
|
{
|
||
|
os_mem_set(recvbuff, 0, sizeof(recvbuff));
|
||
|
lBytes = os_recv_from(commu->udprecvsocket,
|
||
|
recvbuff, sizeof(recvbuff), 0,
|
||
|
&client_addr, &client_len);
|
||
|
if (lBytes < 0)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
os_mem_set(buffer, 0, sizeof(buffer));
|
||
|
os_ntoa(client_addr.sin_addr, buffer);
|
||
|
|
||
|
commu->rx_ctrl_cb(commu->ref_caller_ptr, recvbuff,
|
||
|
lBytes, buffer);
|
||
|
}
|
||
|
|
||
|
#else
|
||
|
(void)lpparameters;
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
#if ((HW_PLATFORM == HW_PLATFORM_SIMU) || (IOT_CLI_INF_MODE == IOT_CLI_INF_MODE_LWIP_SOCKET))
|
||
|
|
||
|
static void communicator_rx_cli_task(void *lpparameters)
|
||
|
{
|
||
|
(void)lpparameters;
|
||
|
#if HW_PLATFORM == HW_PLATFORM_SIMU
|
||
|
if (((commnuicator_handle)lpparameters)->type == COMMUNICATOR_SOCKET)
|
||
|
{
|
||
|
commnuicator_handle commu = (commnuicator_handle)lpparameters;
|
||
|
int8_t buffer[64] = { 0 };
|
||
|
os_sock_addr client_addr;
|
||
|
uint32_t client_len = sizeof(client_addr);
|
||
|
for (;; )
|
||
|
{
|
||
|
os_mem_set(cli_sock_recv_buf, 0, sizeof(cli_sock_recv_buf));
|
||
|
int32_t lBytes = os_recv_from(commu->clirecvsocket,
|
||
|
cli_sock_recv_buf, sizeof(cli_sock_recv_buf), 0,
|
||
|
&client_addr, &client_len);
|
||
|
|
||
|
if (lBytes < 0)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
os_mem_set(buffer, 0, sizeof(buffer));
|
||
|
os_ntoa(client_addr.sin_addr, buffer);
|
||
|
if (g_ip_addr == client_addr.sin_addr)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
if (commu->rx_ctrl_cb)
|
||
|
{
|
||
|
commu->rx_ctrl_cb(commu->ref_caller_ptr, cli_sock_recv_buf,
|
||
|
lBytes, NULL);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
#endif /*#if HW_PLATFORM == HW_PLATFORM_SIMU*/
|
||
|
#if (IOT_CLI_INF_MODE == IOT_CLI_INF_MODE_LWIP_SOCKET)
|
||
|
if (((commnuicator_handle)lpparameters)->type == COMMUNICATOR_LWIP_SOCKET)
|
||
|
{
|
||
|
commnuicator_handle commu = (commnuicator_handle)lpparameters;
|
||
|
for (;; )
|
||
|
{
|
||
|
int sock = cli_lwip_accept(commu->lwip_tcp_raw_socket);
|
||
|
if (sock < 0) {
|
||
|
continue;
|
||
|
}
|
||
|
commu->lwip_tcp_con_socket = sock;
|
||
|
for (;;)
|
||
|
{
|
||
|
os_mem_set(cli_sock_recv_buf, 0, sizeof(cli_sock_recv_buf));
|
||
|
int32_t lBytes = cli_lwip_read(commu->lwip_tcp_con_socket,
|
||
|
cli_sock_recv_buf, sizeof(cli_sock_recv_buf));
|
||
|
|
||
|
if (lBytes <= 0)
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
if (commu->rx_ctrl_cb)
|
||
|
{
|
||
|
commu->rx_ctrl_cb(commu->ref_caller_ptr, cli_sock_recv_buf,
|
||
|
lBytes, NULL);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
#endif /*IOT_CLI_INF_MODE == IOT_CLI_INF_MODE_LWIP_SOCKET*/
|
||
|
}
|
||
|
#endif
|
||
|
|