1 Star 1 Fork 0

tangtao/latserver

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
lxz_spcomm.c 13.73 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
#include "lxz_config.h"
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#ifdef LXZAT_WIN32
#include <windows.h>
#endif /* LXZAT_WIN32 */
#ifdef LXZAT_LINUX
#include <sys/types.h>
#include <sys/time.h>
#endif /* LXZAT_LINUX */
#include "lxz_types.h"
#include "os_port.h"
#include "lxz_runlog.h"
#include "lxz_sysloger.h"
#include "lxz_debug.h"
#include "lxz_ring.h"
#include "lxz_string.h"
#include "lxz_dstring.h"
#include "lxz_atc_req.h"
#include "lxz_atc_rsp.h"
#include "lxz_spcomm.h"
#include "lxz_dbg_vmem.h"
/* 每一路虚拟串口的RX/TX缓存的大小 */
#define LXZAT_MAX_BUF_SIZE 16384
/* 每一路虚拟串口的urc缓存的大小 */
#define LXZAT_URC_BUF_SIZE 1024
/* 自动flush机制,最短数据长度 */
#define LXZAT_AUTO_FLUSH_LEN 1024
/* 每一路连接拥有一份独立的配置表 */
static lxzat_spcfg_t gs_vsp_cfg[LXZAT_MAX_VSP_NUM]={0};
/*
* Description:
* Get the descriptor of serial-port.
* Param: i_vsp_id, the ID of serial-port;
* Return: the descriptor of serial-port.
* History:
*/
lxzat_spcfg_t * lxzat_spcomm_f_get(sint32 i_vsp_id)
{
lxzat_spcfg_t * p_tmp_spcfg = NULL;
if ((0 <= i_vsp_id) && (i_vsp_id < LXZAT_MAX_VSP_NUM))
{
p_tmp_spcfg = &(gs_vsp_cfg[i_vsp_id]);
}
return p_tmp_spcfg;
}
/*
* Description:
* find an usable serial-port.
* Param: none
* Return: i_vsp_id, the ID of serial-port;
*/
sint32 lxzat_spcomm_f_find(void)
{
sint32 i = 0;
sint32 i_vsp_id = -1;
lxzat_spcfg_t * p_tmp_spcfg = NULL;
i = 0;
while (i < LXZAT_MAX_VSP_NUM)
{
p_tmp_spcfg = lxzat_spcomm_f_get(i);
if (p_tmp_spcfg->i_opened_flag == 0)
{
i_vsp_id = i;
break;
}
i++;
}
return i_vsp_id;
}
/*
* Description:
* open the specified serial-port.
* Param: i_vsp_id, the ID of serial-port;
* Return: the descriptor of serial-port.
* History:
*/
lxzat_spcfg_t * lxzat_spcomm_f_open(sint32 i_vsp_id)
{
lxzat_spcfg_t * p_cur_spcfg = NULL;
lxz_ring_t * p_tmp_ring = NULL;
lxzat_spcfg_t * p_tmp_spcfg = NULL;
if (i_vsp_id < LXZAT_MAX_VSP_NUM)
{
p_tmp_spcfg = lxzat_spcomm_f_get(i_vsp_id);
if ( (p_tmp_spcfg != NULL)
&& (p_tmp_spcfg->i_opened_flag == 0))
{
p_tmp_ring = lxz_ring_f_create(LXZAT_MAX_BUF_SIZE);
if (p_tmp_ring == NULL)
{
return NULL;
}
p_tmp_spcfg->pt_rx_ring = p_tmp_ring;
p_tmp_ring = lxz_ring_f_create(LXZAT_MAX_BUF_SIZE);
if (p_tmp_ring == NULL)
{
lxz_ring_f_delete(p_tmp_spcfg->pt_rx_ring);
p_tmp_spcfg->pt_rx_ring = NULL;
return NULL;
}
p_tmp_spcfg->pt_tx_ring = p_tmp_ring;
p_tmp_ring = lxz_ring_f_create(LXZAT_URC_BUF_SIZE);
if (p_tmp_ring == NULL)
{
lxz_ring_f_delete(p_tmp_spcfg->pt_rx_ring);
lxz_ring_f_delete(p_tmp_spcfg->pt_tx_ring);
p_tmp_spcfg->pt_rx_ring = NULL;
p_tmp_spcfg->pt_tx_ring = NULL;
return NULL;
}
p_tmp_spcfg->pt_urc_cache = p_tmp_ring;
p_tmp_spcfg->i_vsp_id = i_vsp_id;
p_tmp_spcfg->i_opened_flag = 1;
p_tmp_spcfg->i_resp_format = LXZAT_RSP_MODE_P1N1;
p_tmp_spcfg->i_link_mode = E_LSMX_ATC;
p_tmp_spcfg->u_end_char1 = LXZAT_END_CHAR_CR;
p_tmp_spcfg->u_end_char2 = LXZAT_END_CHAR_LF;
p_tmp_spcfg->i_echo_auxiliary = LXZAT_TRUE;
p_cur_spcfg = p_tmp_spcfg;
}
}
return p_cur_spcfg;
}
/*
* Description:
* release all resource related to serial-port.
* Param: i_vsp_id, the ID of serial-port;
* Return: i_op_status, 0, fail;1, success.
* History:
*/
sint32 lxzat_spcomm_f_close(sint32 i_vsp_id)
{
sint32 i_op_status = 0;
lxzat_spcfg_t * p_cur_spcfg = NULL;
if (i_vsp_id < LXZAT_MAX_VSP_NUM)
{
p_cur_spcfg = lxzat_spcomm_f_get(i_vsp_id);
p_cur_spcfg->i_opened_flag = 0;
if (p_cur_spcfg->pt_rx_ring != NULL)
{
lxz_ring_f_delete(p_cur_spcfg->pt_rx_ring);
p_cur_spcfg->pt_rx_ring = NULL;
}
if (p_cur_spcfg->pt_tx_ring != NULL)
{
lxz_ring_f_delete(p_cur_spcfg->pt_tx_ring);
p_cur_spcfg->pt_tx_ring = NULL;
}
if (p_cur_spcfg->pt_urc_cache != NULL)
{
lxz_ring_f_delete(p_cur_spcfg->pt_urc_cache);
p_cur_spcfg->pt_urc_cache = NULL;
}
i_op_status = 1;
}
return i_op_status;
}
/*
* Description:
* send data through the serial-port.
* Param: i_vsp_id, the ID of serial-port;
* Param: p_data_buf, data buffer;
* Param: i_data_len, length of data;
* Return: i_len_write, length of data has been written.
* History:
*/
sint32 lxzat_spcomm_f_writebyid(sint32 i_vsp_id, uint08 * p_data_buf, sint32 i_data_len)
{
sint32 i_len_write = 0;
sint32 i_len_left = 0;
sint32 i_len_unit = LXZAT_AUTO_FLUSH_LEN;
sint32 i_len_temp = 0;
uint08 * p_ptr_temp = NULL;
sint32 i_len_cache = 0;
lxzat_spcfg_t * p_cur_spcfg = NULL;
if ((p_data_buf == NULL) || (i_data_len == 0))
{
return i_len_write;
}
p_cur_spcfg = lxzat_spcomm_f_get(i_vsp_id);
if ( (p_cur_spcfg != NULL)
&& (p_cur_spcfg->i_opened_flag == 1))
{
i_len_left = i_data_len;
p_ptr_temp = p_data_buf;
while (i_len_left > 0)
{
sint32 i_len_write0 = 0;
i_len_temp = i_len_left;
if (i_len_left >= i_len_unit)
{
i_len_temp = i_len_unit;
}
i_len_write0 = lxz_ring_f_write(p_cur_spcfg->pt_tx_ring, p_ptr_temp, i_len_temp);
i_len_left = i_len_left - i_len_write0;
p_ptr_temp = p_ptr_temp + i_len_write0;
i_len_write = i_len_write + i_len_write0;
i_len_cache = lxz_ring_f_getoption(p_cur_spcfg->pt_tx_ring, E_LRO_DATA_SIZE);
if (i_len_cache >= LXZAT_AUTO_FLUSH_LEN)
{
lxzat_spcomm_f_flush(i_vsp_id);
if (i_len_left > 0)
{
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 100*1000; /* 100 ms */
select(0, NULL, NULL, NULL, &tv);
}
}
}
}
return i_len_write;
}
/*
* Description:
* send data through the serial-port.
* Param: u_vsp_fd, the descriptor of serial-port;
* Param: p_data_buf, data buffer;
* Param: i_data_len, length of data;
* Return: i_len_write, length of data has been written.
* History:
*/
sint32 lxzat_spcomm_f_writebyfd(uint32 u_vsp_fd, uint08 * p_data_buf, sint32 i_data_len)
{
sint32 i_len_write = 0;
if ((p_data_buf == NULL) || (i_data_len == 0))
{
return i_len_write;
}
if (u_vsp_fd != 0)
{
#ifdef LXZAT_SIMU
i_len_write = osp_socket_f_write(u_vsp_fd, p_data_buf, i_data_len);
#else /* LXZAT_SIMU */
i_len_write = write(u_vsp_fd, p_data_buf, i_data_len);
#endif /* LXZAT_SIMU */
}
return i_len_write;
}
/*
* Description:
* send data through the serial-port.
* Param: u_vsp_fd, the descriptor of serial-port;
* Param: p_data_buf, data buffer;
* Param: i_data_len, length of data;
* Return: i_len_write, length of data has been written.
* History:
*/
sint32 lxzat_spcomm_f_flush(uint32 i_vsp_id)
{
sint32 i_total_len = 0;
sint32 i_write_len = 0;
sint32 i_data_len = 0;
sint32 i_read_len = 0;
uint08 p_read_buf[256];
lxzat_spcfg_t * p_cur_spcfg = NULL;
p_cur_spcfg = lxzat_spcomm_f_get(i_vsp_id);
if (p_cur_spcfg == NULL)
{
return i_write_len;
}
i_data_len = lxz_ring_f_getoption(p_cur_spcfg->pt_tx_ring, E_LRO_DATA_SIZE);
while (i_data_len > 0)
{
i_read_len = lxz_ring_f_read(p_cur_spcfg->pt_tx_ring, p_read_buf, sizeof(p_read_buf));
if (i_read_len > 0)
{
#ifdef LXZAT_SIMU
i_write_len = osp_socket_f_write(p_cur_spcfg->u_vsp_fd, p_read_buf, i_read_len);
#else /* LXZAT_SIMU */
i_write_len = write(p_cur_spcfg->u_vsp_fd, p_read_buf, i_read_len);
#endif /* LXZAT_SIMU */
if(i_write_len!=i_read_len)
{
OS_DBG_ERR(("lxzat_spcomm_f_flush i_write_len!=i_read_len"));
}
i_total_len = i_total_len + i_write_len;
}
i_data_len = i_data_len - i_read_len;
}
lxz_ring_f_clear(p_cur_spcfg->pt_tx_ring);
return i_total_len;
}
/*
* Description:
* clear all data that stored in ring buffer.
* Param: p_lxzat_ctxt, the context of current AT-Command;
* Return: i_op_status, 0, fail;1, success.
* History:
*/
sint32 lxzat_spcomm_f_clear(sint32 i_vsp_id)
{
sint32 i_op_status = 0;
lxzat_spcfg_t * p_cur_spcfg = NULL;
p_cur_spcfg = lxzat_spcomm_f_get(i_vsp_id);
if (p_cur_spcfg == NULL)
{
return i_op_status;
}
if (p_cur_spcfg->pt_rx_ring != NULL)
{
lxz_ring_f_clear(p_cur_spcfg->pt_rx_ring);
i_op_status = 1;
}
return i_op_status;
}
/*
* Description:
* Change the work mode of the serial-port and related conditions.
* Param: i_vsp_id, the ID of serial-port;
* Param: i_link_mode, the new work-mode of serial-port;
* Param: u_end_char1, the primary end-character;
* Param: u_end_char1, the auxiliary end-character;
* Return: i_op_status, 0, fail;1, success.
* History:
*/
sint32 lxzat_spcomm_f_ChangeWorkMode(sint32 i_vsp_id, sint32 i_link_mode,
uint08 u_end_char1, uint08 u_end_char2)
{
sint32 i_op_status = 0;
lxzat_spcfg_t * p_cur_spcfg = NULL;
if (i_vsp_id < LXZAT_MAX_VSP_NUM)
{
OS_DBG_LOG(("lxzat_spcomm_f_ChangeWorkMode,i_vsp_id=%d,i_new_mode=%d!\r\n", i_vsp_id, i_link_mode));
p_cur_spcfg = lxzat_spcomm_f_get(i_vsp_id);
p_cur_spcfg->i_link_mode = i_link_mode;
p_cur_spcfg->u_end_char1 = u_end_char1;
p_cur_spcfg->u_end_char2 = u_end_char2;
if (p_cur_spcfg->i_link_mode == E_LSMX_ATC)
{
p_cur_spcfg->i_echo_auxiliary = LXZAT_TRUE;
}
else
{
p_cur_spcfg->i_echo_auxiliary = LXZAT_FALSE;
}
i_op_status = 1;
}
return i_op_status;
}
/*
* Description:
* Get the status of a specified serial-port.
* Param: i_vsp_id, the ID of serial-port;
* Return: i_spcomm_isopen, 1, opened;0, closed.
* History:
*/
sint32 lxzat_spcomm_f_isopen(sint32 i_vsp_id)
{
sint32 i_spcomm_isopen = 0;
lxzat_spcfg_t * p_cur_spcfg = NULL;
if (i_vsp_id < LXZAT_MAX_VSP_NUM)
{
p_cur_spcfg = &(gs_vsp_cfg[i_vsp_id]);
i_spcomm_isopen = p_cur_spcfg->i_opened_flag;
}
return i_spcomm_isopen;
}
/*
* Description:
* send data through the serial-port.
* Param: i_vsp_id, the ID of serial-port;
* Param: p_data_buf, data buffer;
* Param: i_data_len, length of data;
* Return: i_op_status, 0, fail;1, success.
* History:
*/
sint32 lxzurc_cache_f_write(sint32 i_vsp_id, sint32 i_data_len, uint08 * p_data_buf)
{
sint32 i_op_status = 0;
sint32 i_write_len = 0;
sint32 i_spvcomm_open = 0;
lxzat_spcfg_t * p_cur_spcfg = NULL;
p_cur_spcfg = lxzat_spcomm_f_get(i_vsp_id);
if (p_cur_spcfg == NULL)
{
return i_op_status;
}
i_spvcomm_open = lxzat_spcomm_f_isopen(i_vsp_id);
if (i_spvcomm_open != 1)
{
return i_op_status;
}
if ((i_data_len > 0) && (p_data_buf != NULL))
{
lxz_ring_f_write(p_cur_spcfg->pt_urc_cache, "\r\n", 2);
lxz_ring_f_write(p_cur_spcfg->pt_urc_cache, p_data_buf, i_data_len);
lxz_ring_f_write(p_cur_spcfg->pt_urc_cache, "\r\n", 2);
OS_DBG_LOG(("lxzat:ATCommand-urc:%s\r\n", p_data_buf));
lxzpcap_sysloger_printf("\r\n%s\r\n", p_data_buf);
}
return 1;
}
/*
* Description:
* Get the baudrate of a specified serial-port.
* Param: i_vsp_id, the ID of serial-port;
* Return: the descriptor of serial-port.
* History:
*/
sint32 lxzat_spcomm_baudrate_f_get(sint32 i_vsp_id)
{
sint32 i_got_baudrate = 0;
sint32 i_vsp_openflag = 0;
lxzat_spcfg_t * p_cur_spcfg = NULL;
p_cur_spcfg = lxzat_spcomm_f_get(i_vsp_id);
if (p_cur_spcfg == NULL)
{
return i_got_baudrate;
}
i_vsp_openflag = lxzat_spcomm_f_isopen(i_vsp_id);
if (i_vsp_openflag != LXZAT_TRUE)
{
return i_got_baudrate;
}
i_got_baudrate = 115200;
return i_got_baudrate;
}
/*
* Description:
* Set the baudrate of a specified serial-port.
* Param: i_vsp_id, the ID of serial-port;
* Return: the descriptor of serial-port.
* History:
*/
sint32 lxzat_spcomm_baudrate_f_set(sint32 i_vsp_id, sint32 i_new_baudrate)
{
sint32 i_op_status = 0;
sint32 i_vsp_openflag = 0;
lxzat_spcfg_t * p_cur_spcfg = NULL;
p_cur_spcfg = lxzat_spcomm_f_get(i_vsp_id);
if (p_cur_spcfg == NULL)
{
return i_op_status;
}
i_vsp_openflag = lxzat_spcomm_f_isopen(i_vsp_id);
if (i_vsp_openflag != LXZAT_TRUE)
{
return i_op_status;
}
i_op_status = 1;
return i_op_status;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/cyctspace/latserver.git
git@gitee.com:cyctspace/latserver.git
cyctspace
latserver
latserver
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385