107 lines
1.9 KiB
C
107 lines
1.9 KiB
C
|
|
#include <rtthread.h>
|
|
|
|
#include "chrg_south.h"
|
|
|
|
#include "chrg_utils.h"
|
|
|
|
#define LOG_TAG "chrg.sou"
|
|
#define DBG_LEVEL DBG_LOG
|
|
#include <rtdbg.h>
|
|
|
|
struct chrg_south_t chrgsouth = {
|
|
.name = THR_NAME_SOUTH,
|
|
.devname = DEV_NAME_SOUTH,
|
|
.tid = RT_NULL,
|
|
.tty = RT_NULL,
|
|
.rx_len = 0,
|
|
};
|
|
|
|
int chrg_south_send (rt_uint8_t *data, rt_size_t length)
|
|
{
|
|
rt_ssize_t ret = -1;
|
|
struct chrg_tty_t *pTTY = chrgsouth.tty;
|
|
|
|
if (RT_NULL != pTTY) {
|
|
ret = chrg_tty_send(pTTY, data, length);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static void chrg_south_thread_entry (void *data)
|
|
{
|
|
struct chrg_south_t *pSOU = (struct chrg_south_t *)data;
|
|
|
|
if ((RT_NULL == pSOU)||(RT_NULL == pSOU->tty)) {
|
|
return ;
|
|
}
|
|
|
|
int len = 0;
|
|
struct chrg_tty_t *pTTY = pSOU->tty;
|
|
|
|
chrg_tty_set_recv_tmo(pTTY, 100);
|
|
if (chrg_tty_connect(pTTY) != RT_EOK) {
|
|
chrg_tty_destory(pTTY);
|
|
return;
|
|
}
|
|
|
|
while (1) {
|
|
len = chrg_tty_recv(pTTY, pSOU->rx_buf, sizeof(pSOU->rx_buf));
|
|
if (len <= 0) {
|
|
rt_thread_mdelay(1);
|
|
continue;
|
|
}
|
|
|
|
rt_kprintf("%s\r\n", (char *)pSOU->rx_buf);
|
|
chrg_tty_send(pTTY, pSOU->rx_buf, len);
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static int chrg_south_init (void)
|
|
{
|
|
rt_err_t ret;
|
|
struct chrg_south_t *pSOU = &chrgsouth;
|
|
|
|
pSOU->tty = chrg_tty_create(pSOU->devname, 2000000, 0, -1, 1);
|
|
if (RT_NULL == pSOU->tty) {
|
|
LOG_E("tty can't create.");
|
|
return -1;
|
|
}
|
|
|
|
rt_memset(pSOU->rx_buf, 0, SIZE_BUF_TTY);
|
|
|
|
ret = rt_mutex_init(&pSOU->lock, pSOU->name, RT_IPC_FLAG_FIFO);
|
|
if (ret != RT_EOK) {
|
|
LOG_E("init mutex '%s' failed.", pSOU->name);
|
|
return ret;
|
|
}
|
|
|
|
pSOU->tid = rt_thread_create(pSOU->name, chrg_south_thread_entry, pSOU, 4096,
|
|
RT_MAIN_THREAD_PRIORITY, 20);
|
|
if (RT_NULL != pSOU->tid) {
|
|
rt_thread_startup(pSOU->tid);
|
|
} else {
|
|
LOG_E("create thread '%s' failed.", pSOU->name);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
INIT_APP_EXPORT(chrg_south_init);
|
|
|
|
static int south_send(rt_uint8_t argc, char **argv)
|
|
{
|
|
return chrg_south_send("1234567890abcdef", 16);
|
|
}
|
|
|
|
|
|
MSH_CMD_EXPORT(south_send, south send test);
|
|
|
|
|
|
|