#if LWIP_LOWPOWER
#include "lwip/lowpower.h"
#endif
+#include <los_task.h>
+#include <los_typedef.h>
+#include "custom_utils.h"
+
+static app_task_definition_t taskcfg={0};
+
#define TCPIP_MSG_VAR_REF(name) API_VAR_REF(name)
#define TCPIP_MSG_VAR_DECLARE(name) API_VAR_DECLARE(struct tcpip_msg, name)
LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
return sys_mbox_trypost_fromisr(&tcpip_mbox, msg);
}
-static sys_thread_t tcpip_thread_id=0xffffffffU;
+
/**
* @ingroup lwip_os
* Initialize this module:
}
#endif /* LWIP_TCPIP_CORE_LOCKING */
- tcpip_thread_id = sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);
+ // tcpip_thread_id = sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);
+ taskcfg.create_queue_handle = NULL;
+ taskcfg.attr.name=TCPIP_THREAD_NAME;
+ taskcfg.attr.priority = osPriorityBelowNormal;// osPriorityBelowNormal;
+ taskcfg.attr.stack_size = TCPIP_THREAD_STACKSIZE;
+ taskcfg.func = tcpip_thread;
+ taskcfg.use_mem = USE_EXTEND_MEM;
+
+ errcode_t ret=uapi_create_osThread_custom(&taskcfg);
+ if(ret != ERRCODE_SUCC)
+ printf("create tcpip_thread failed %d\n",ret);
+ else
+ printf("create tcpip_thread succed\n");
+
}
/**
return tcpip_try_callback(mem_free, m);
}
+/**
+ * @ingroup lwip_os
+ * Suspend the tcpip_thread
+ *
+ * @return ERR_OK if thread was successfully suspended, an err_t if not
+ */
+err_t
+tcpip_thread_suspend(void)
+{
+ if(taskcfg.task_handle != NULL)
+ {
+ int ret = uapi_suspend_thread(taskcfg.task_handle);
+ if(ret != ERRCODE_SUCC)
+ printf("tcpip_thread suspend failed ret : %d \n",ret);
+ else
+ printf("tcpip_thread suspend succed\n");
+ }
+ return 0;
+}
-void* tcp_get_thread_id(void)
+/**
+ * @ingroup lwip_os
+ * Resume the tcpip_thread
+ *
+ * @return ERR_OK if thread was successfully resumed, an err_t if not
+ */
+err_t
+tcpip_thread_resume(void)
{
- return tcpip_thread_id;
+ if(taskcfg.task_handle != NULL)
+ {
+ int ret = uapi_resume_thread(taskcfg.task_handle);
+ if(ret != ERRCODE_SUCC)
+ printf("tcpip_thread resume failed ret : %d \n",ret);
+ else
+ printf("tcpip_thread resume succed\n");
+ }
+ return 0;
}
#endif /* !NO_SYS */
#include "osal_wait.h" // 包含 osal_msleep 函数
#include "lwip/ip6_addr.h"
#include "lwip/netif.h"
+#include <los_task.h>
+#include <los_typedef.h>
+#include "custom_utils.h"
+
+static app_task_definition_t taskcfg={0};
#define SLIP_END 0xC0 /* 0300: start and end of every packet */
#define SLIP_ESC 0xDB /* 0333: escape start (one byte escaped data follows) */
#endif
};
+/* Global variable to store slipif_loop_thread ID */
+// static sys_thread_t slipif_loop_thread_id = 0xffffffffU;
+
+/* Event flag for thread control */
+static volatile bool slipif_thread_running = true;
+
+/* Global variable to store current netif */
+static struct netif *g_current_slipif_netif = NULL;
+
+/**
+ * @ingroup slipif
+ * Get the current netif from slipif_loop_thread
+ *
+ * @return pointer to the current netif, or NULL if not available
+ */
+ static struct netif *slipif_get_current_netif(void)
+ {
+ return g_current_slipif_netif;
+ }
+
+ /**
+ * @ingroup slipif
+ * Set the current netif for slipif_loop_thread
+ *
+ * @param netif pointer to the netif to set
+ */
+ static void slipif_set_current_netif(struct netif *netif)
+ {
+ g_current_slipif_netif = netif;
+ }
+
+
/**
* Send a pbuf using SLIP, note that the netif must be reinitialized in
* case of errors.
static void
slipif_loop_thread(void *nf)
{
- struct netif *netif = (struct netif *)nf;
+ /* 通过接口获取netif,而不是依赖参数传递 */
+ struct netif *netif = slipif_get_current_netif();
/* 添加空指针保护 */
if (netif == NULL) {
netif, priv, priv->sd));
while (1) {
-
+ /* 检查线程运行状态,如果被挂起则进入长时间休眠 */
+ if (!slipif_thread_running) {
+ LOS_Msleep(1000); /* 使用系统级休眠,让系统能够进入深度休眠 */
+ continue;
+ }
+ // printf("slipif_loop_thread \n");
#if SLIP_RX_FROM_ISR
- /* 主线程处理接收队列 */
+ /* 处理接收队列 */
u16_t processed_count = slipif_process_rxqueue(netif);
- /* 动态休眠时间调整 */
- static u16_t dynamic_sleep_time = 2; // 初始休眠时间
- static u32_t last_adjustment_time = 0;
- u32_t current_time = sys_now();
-
- /* 智能休眠策略:使用系统级休眠,避免频繁创建 semaphore */
- if (processed_count == 0) {
- /* 没有数据包处理,检查队列状态 */
- struct slipif_priv *priv_check = (struct slipif_priv *)netif->state;
- if (priv_check->rxpackets == NULL) {
- /* 队列完全为空,可以休眠更长时间 */
- u16_t sleep_time = (dynamic_sleep_time < 10) ? dynamic_sleep_time + 2 : 15;
- /* 使用系统级休眠,避免频繁创建 semaphore */
- osal_msleep(sleep_time);
- // LWIP_DEBUGF(SLIP_DEBUG, ("slipif_loop_thread: queue empty, sleeping %d ms\n", sleep_time));
- } else {
- /* 队列有数据但处理失败,短暂休眠后重试 */
- osal_msleep(dynamic_sleep_time);
- // LWIP_DEBUGF(SLIP_DEBUG, ("slipif_loop_thread: processing failed, sleeping %d ms\n", dynamic_sleep_time));
- }
- } else if (processed_count < 3) {
- /* 处理少量数据包,短暂休眠 */
- osal_msleep(dynamic_sleep_time);
- } else if (processed_count < 8) {
- /* 处理中等数量数据包,最小休眠 */
- osal_msleep(1);
- } else {
- /* 处理大量数据包,不休眠,立即继续处理 */
- /* 不休眠,保持高吞吐量 */
- }
-
- /* 优化:减少休眠频率,避免资源耗尽 */
- static u32_t last_sleep_time = 0;
- static u32_t total_sleep_time = 0;
- u32_t current_sleep_time = 0;
-
- /* 计算本次休眠时间 */
+ /* 简化的休眠策略:根据处理结果决定休眠时间 */
if (processed_count == 0) {
- struct slipif_priv *priv_check = (struct slipif_priv *)netif->state;
- if (priv_check->rxpackets == NULL) {
- current_sleep_time = (dynamic_sleep_time < 10) ? dynamic_sleep_time + 2 : 15;
- } else {
- current_sleep_time = dynamic_sleep_time;
- }
- } else if (processed_count < 3) {
- current_sleep_time = dynamic_sleep_time;
- } else if (processed_count < 8) {
- current_sleep_time = 1;
+ /* 没有数据包处理,长时间休眠 */
+ LOS_Msleep(50); /* 50ms休眠,减少CPU占用 */
+ } else if (processed_count < 5) {
+ /* 处理少量数据包,中等时间休眠 */
+ LOS_Msleep(20); /* 20ms休眠 */
} else {
- current_sleep_time = 0;
- }
-
- /* 记录休眠统计信息 */
- if (current_sleep_time > 0) {
- total_sleep_time += current_sleep_time;
- last_sleep_time = current_time;
-
- /* 每1000ms输出一次休眠统计,监控资源使用 */
- static u32_t last_stats_time = 0;
- if (current_time - last_stats_time > 1000) {
- // LWIP_DEBUGF(SLIP_DEBUG, ("slipif_loop_thread: sleep stats - total_sleep=%d ms, avg_sleep_per_sec=%d ms\n",
- // total_sleep_time, total_sleep_time / ((current_time - last_stats_time) / 1000)));
- total_sleep_time = 0;
- last_stats_time = current_time;
- }
- }
-
- /* 动态调整休眠时间:每200ms调整一次 */
- if (current_time - last_adjustment_time > 200) {
- if (processed_count > 5) {
- /* 高负载,减少休眠时间 */
- if (dynamic_sleep_time > 1) {
- dynamic_sleep_time--;
- // LWIP_DEBUGF(SLIP_DEBUG, ("slipif_loop_thread: high load, reducing sleep to %d ms\n", dynamic_sleep_time));
- }
- } else if (processed_count == 0) {
- /* 低负载,增加休眠时间 */
- if (dynamic_sleep_time < 10) {
- dynamic_sleep_time++;
- // LWIP_DEBUGF(SLIP_DEBUG, ("slipif_loop_thread: low load, increasing sleep to %d ms\n", dynamic_sleep_time));
- }
- }
- last_adjustment_time = current_time;
- }
-
- /* 添加队列长度检测,实现更智能的休眠 */
- static u32_t last_queue_check = 0;
- static u16_t last_queue_length = 0;
- static u32_t last_processing_time = 0;
- static u16_t last_processed_count = 0;
- static u32_t last_priority_check = 0;
-
- /* 每100ms检查一次队列长度变化趋势 */
- if (current_time - last_queue_check > 100) {
- u16_t current_queue_length = 0;
- struct slipif_priv *priv_len_check = (struct slipif_priv *)netif->state;
- struct pbuf *p = priv_len_check->rxpackets;
-
- while (p != NULL && current_queue_length < 1000) {
- current_queue_length++;
- p = p->next;
- }
-
- /* 根据队列长度变化趋势调整休眠策略 */
- if (current_queue_length > last_queue_length + 5) {
- /* 队列快速增长,减少休眠时间 */
- // LWIP_DEBUGF(SLIP_DEBUG, ("slipif_loop_thread: queue growing fast (%d->%d), reducing sleep\n",
- // last_queue_length, current_queue_length));
- } else if (current_queue_length < last_queue_length - 3) {
- /* 队列快速减少,可以增加休眠时间 */
- // LWIP_DEBUGF(SLIP_DEBUG, ("slipif_loop_thread: queue shrinking (%d->%d), increasing sleep\n",
- // last_queue_length, current_queue_length));
- }
-
- /* 计算处理效率,调整休眠策略 */
- u32_t time_diff = current_time - last_processing_time;
- if (time_diff > 0) {
- u16_t packets_per_second = ((processed_count - last_processed_count) * 1000) / time_diff;
- if (packets_per_second > 100) {
- /* 高吞吐量,减少休眠时间 */
- // LWIP_DEBUGF(SLIP_DEBUG, ("slipif_loop_thread: high throughput (%d pkt/s), optimizing sleep\n", packets_per_second));
- } else if (packets_per_second < 10) {
- /* 低吞吐量,增加休眠时间 */
- // LWIP_DEBUGF(SLIP_DEBUG, ("slipif_loop_thread: low throughput (%d pkt/s), increasing sleep\n", packets_per_second));
- }
- }
-
- last_queue_length = current_queue_length;
- last_queue_check = current_time;
- last_processing_time = current_time;
- last_processed_count = processed_count;
-
- /* 根据队列状态调整线程优先级(如果系统支持) */
- if (current_time - last_priority_check > 500) { // 每500ms检查一次
- if (current_queue_length > 20) {
- /* 队列积压严重,提高优先级 */
- // LWIP_DEBUGF(SLIP_DEBUG, ("slipif_loop_thread: queue backlog (%d), should increase priority\n", current_queue_length));
- } else if (current_queue_length < 5) {
- /* 队列清空,可以降低优先级 */
- // LWIP_DEBUGF(SLIP_DEBUG, ("slipif_loop_thread: queue clear (%d), can decrease priority\n", current_queue_length));
- }
- last_priority_check = current_time;
- }
+ /* 处理大量数据包,短暂休眠 */
+ LOS_Msleep(5); /* 5ms休眠 */
}
#else
if (sio_tryread(priv->sd, &c, 1) > 0) {
LWIP_DEBUGF(SLIP_DEBUG, ("slipif_loop_thread: received byte 0x%02x\n", c));
slipif_rxbyte_input(netif, c);
} else {
- sys_msleep(1);
- // LWIP_DEBUGF(SLIP_DEBUG, ("slipif_loop_thread: no data, sleeping\n"));
+ LOS_Msleep(20); /* 没有数据时休眠20ms,减少CPU占用 */
}
#endif
}
/* Create a thread to poll the serial device. */
LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: creating RX thread with stack=%d, prio=%d\n",
SLIP_RX_THREAD_STACKSIZE, SLIP_RX_THREAD_PRIO));
- sys_thread_new(SLIP_RX_THREAD_NAME, slipif_loop_thread, netif,
- SLIP_RX_THREAD_STACKSIZE, SLIP_RX_THREAD_PRIO);
+
+ // 设置全局netif变量,供线程使用
+ slipif_set_current_netif(netif);
+
+ // slipif_loop_thread_id = sys_thread_new(SLIP_RX_THREAD_NAME, slipif_loop_thread, netif,
+ // SLIP_RX_THREAD_STACKSIZE, SLIP_RX_THREAD_PRIO);
+ taskcfg.create_queue_handle = NULL;
+ taskcfg.attr.name=SLIP_RX_THREAD_NAME;
+ taskcfg.attr.priority = osPriorityBelowNormal;// osPriorityBelowNormal;
+ taskcfg.attr.stack_size = SLIP_RX_THREAD_STACKSIZE;
+ taskcfg.func = slipif_loop_thread;
+ taskcfg.use_mem = USE_EXTEND_MEM;
+
+ errcode_t ret=uapi_create_osThread_custom(&taskcfg);
+ if(ret != ERRCODE_SUCC)
+ printf("create slipif_loop_thread failed %d\n",ret);
+ else
+ printf("create slipif_loop_thread succed\n");
+
#endif /* SLIP_USE_RX_THREAD */
LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: initialization completed successfully\n"));
slipif_rxbyte_enqueue(netif, data[i]);
}
}
-#endif /* SLIP_RX_FROM_ISR */
\ No newline at end of file
+#endif /* SLIP_RX_FROM_ISR */
+
+/**
+ * @ingroup slipif
+ * Suspend the slipif_loop_thread
+ *
+ * @return ERR_OK if thread was successfully suspended, an err_t if not
+ */
+err_t
+slipif_loop_thread_suspend(void)
+{
+ // UINT32 ret;
+
+ // if (slipif_loop_thread_id == 0xffffffffU) {
+ // return ERR_VAL; /* Thread not initialized */
+ // }
+
+ // /* 首先停止线程运行标志 */
+ slipif_thread_running = false;
+
+ // /* 然后挂起线程 */
+ // ret = LOS_TaskSuspend((UINT32)slipif_loop_thread_id);
+ // printf("ret:0x%x slipif_loop_thread_id:0x%x\n",ret,slipif_loop_thread_id);
+ // if (ret == LOS_OK) {
+ // return ERR_OK;
+ // }
+ // else {
+ // return ERR_VAL;
+ // }
+ if(taskcfg.task_handle != NULL)
+ {
+ int ret = uapi_suspend_thread(taskcfg.task_handle);
+ if(ret != ERRCODE_SUCC)
+ printf("slipif_loop_thread suspend failed ret : %d \n",ret);
+ else
+ printf("slipif_loop_thread suspend succed\n");
+ }
+ return 0;
+}
+
+/**
+ * @ingroup slipif
+ * Resume the slipif_loop_thread
+ *
+ * @return ERR_OK if thread was successfully resumed, an err_t if not
+ */
+err_t
+slipif_loop_thread_resume(void)
+{
+ // UINT32 ret;
+
+ // if (slipif_loop_thread_id == 0xffffffffU) {
+ // return ERR_VAL; /* Thread not initialized */
+ // }
+
+ // /* 恢复线程运行标志 */
+ slipif_thread_running = true;
+
+ // /* 恢复线程 */
+ // ret = LOS_TaskResume((UINT32)slipif_loop_thread_id);
+ // printf("ret:0x%x slipif_loop_thread_id:0x%x\n",ret,slipif_loop_thread_id);
+ // if (ret == LOS_OK) {
+ // return ERR_OK;
+ // } else {
+ // return ERR_VAL;
+ // }
+ if(taskcfg.task_handle != NULL)
+ {
+ int ret = uapi_resume_thread(taskcfg.task_handle);
+ if(ret != ERRCODE_SUCC)
+ printf("slipif_loop_thread resume failed ret : %d \n",ret);
+ else
+ printf("slipif_loop_thread resume succed\n");
+ }
+ return 0;
+}