9ceb218f80
Co-authored-by: Cursor <cursoragent@cursor.com>
106 lines
3.1 KiB
C
106 lines
3.1 KiB
C
#include "tcp_ser/tcp_ser_task.h"
|
||
|
||
#include "app_init/app_init.h"
|
||
#include "app_init/task_lock.h"
|
||
#include "mylog/mylog.h"
|
||
#include "publicdata/publicdata.h"
|
||
#include "tcp_ser/tcp_server.h"
|
||
#include "debug_link.h"
|
||
#include "wdt_task/wdt_task.h"
|
||
|
||
#if TCP_DEBUG_EN
|
||
#define TCPSER_TASK_ID (TASK_ID_TcpSer)
|
||
#define TCPSER_SLEEP_TIME_MS() (MY_GET_SLEEP_TIME(TCPSER_TASK_ID))
|
||
#define TCPSER_LOCK_INIT() (u8_task_lock_ctrl(TCPSER_TASK_ID, TASK_LOCK_OP_INIT))
|
||
#define TCPSER_LOCK_TAKE() (u8_task_lock_ctrl(TCPSER_TASK_ID, TASK_LOCK_OP_TAKE))
|
||
#define TCPSER_LOCK_GIVE() (u8_task_lock_ctrl(TCPSER_TASK_ID, TASK_LOCK_OP_GIVE))
|
||
#define TCPSER_FEED_DOG() (v_wdt_setFlag(TCPSER_TASK_ID))
|
||
#endif
|
||
#define TCPSER_LOG(fmt, ...) MYLOG_MSG(TCPSER_TASK_ID, (fmt), ##__VA_ARGS__)
|
||
|
||
#if TCP_DEBUG_EN && BLE_DEBUG_EN
|
||
#include "ble_link/fc41d_ble.h"
|
||
#else
|
||
#include "sub_comm/sub_comm_task.h"
|
||
#endif
|
||
|
||
static const char *tcp_ser_link_name(void)
|
||
{
|
||
#if BLE_DEBUG_EN
|
||
return COMM_LINK_IS_BLE(comm_link_get_type()) ? "BLE" : "TCP";
|
||
#else
|
||
return "TCP";
|
||
#endif
|
||
}
|
||
|
||
/**
|
||
* @brief tcpSer task:协议/业务共用 tcp_server;DIP1 选 TCP 或 BLE 物理链路。
|
||
*/
|
||
void v_tcp_ser_task(void *argument)
|
||
{
|
||
#if TCP_DEBUG_EN
|
||
uint16_t sleep_time_ms;
|
||
uint32_t last_send_time;
|
||
uint32_t current_time;
|
||
uint32_t last_start_try_time;
|
||
uint8_t server_started;
|
||
|
||
(void)argument;
|
||
(void)TCPSER_LOCK_INIT();
|
||
sleep_time_ms = TCPSER_SLEEP_TIME_MS();
|
||
last_send_time = get_current_seconds();
|
||
last_start_try_time = 0U;
|
||
server_started = 0U;
|
||
|
||
#if BLE_DEBUG_EN
|
||
(void)ble_link_wait_dip_ready(3000U);
|
||
comm_link_set_type(comm_link_type_from_dip());
|
||
#else
|
||
comm_link_set_type(COMM_LINK_TCP);
|
||
if (u8_sub_comm_yx_get(E_SUB_DIP_1) != 0U) {
|
||
TCPSER_LOG("DIP1=1 but BLE_DEBUG_EN=0, force TCP link");
|
||
}
|
||
#endif
|
||
|
||
TCPSER_LOG("debug link=%s (BLE_DEBUG_EN=%d)", tcp_ser_link_name(), BLE_DEBUG_EN);
|
||
|
||
for (;;) {
|
||
TCPSER_FEED_DOG();
|
||
current_time = get_current_seconds();
|
||
|
||
if (server_started == 0U) {
|
||
if ((last_start_try_time == 0U) ||
|
||
(u32_safe_seconds_since(last_start_try_time) >= 2U)) {
|
||
if (tcp_server_start() == 0) {
|
||
server_started = 1U;
|
||
TCPSER_LOG("debug server start ok, link=%s", tcp_ser_link_name());
|
||
} else {
|
||
TCPSER_LOG("debug server start fail, retry in 2s");
|
||
}
|
||
last_start_try_time = current_time;
|
||
}
|
||
} else {
|
||
tcp_server_process();
|
||
}
|
||
|
||
if ((TASK_RUN_PRINT_TIME != 0U) &&
|
||
(u32_safe_seconds_since(last_send_time) >= TASK_RUN_PRINT_TIME)) {
|
||
if (TCPSER_LOCK_TAKE() == 1U) {
|
||
TCPSER_LOG("tcp ser running, link=%s sleep=%ums",
|
||
tcp_ser_link_name(), sleep_time_ms);
|
||
(void)TCPSER_LOCK_GIVE();
|
||
}
|
||
last_send_time = current_time;
|
||
}
|
||
{
|
||
mSleep(sleep_time_ms);
|
||
}
|
||
}
|
||
#else
|
||
(void)argument;
|
||
for (;;) {
|
||
mSleep(1000U);
|
||
}
|
||
#endif
|
||
}
|