From 7443277d6bb6881fb780a02efb5ce4b75c894df6 Mon Sep 17 00:00:00 2001 From: zogodo <742782908@qq.com> Date: Thu, 12 Dec 2019 21:40:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TC1/TC1.mk | 3 +- TC1/timed_task/timed_task.c | 69 +++++++++++++++++++++++++++++++++++++ TC1/timed_task/timed_task.h | 17 +++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 TC1/timed_task/timed_task.c create mode 100644 TC1/timed_task/timed_task.h diff --git a/TC1/TC1.mk b/TC1/TC1.mk index 2ae9aa0..f8fb460 100644 --- a/TC1/TC1.mk +++ b/TC1/TC1.mk @@ -38,7 +38,8 @@ $(NAME)_SOURCES := main.c\ mqtt_server/user_function.c\ http_server/web_log.c\ http_server/app_httpd.c - + timed_task/timed_task.c\ + $(NAME)_COMPONENTS := protocols/SNTP\ protocols/mqtt\ utilities/url\ diff --git a/TC1/timed_task/timed_task.c b/TC1/timed_task/timed_task.c new file mode 100644 index 0000000..75cce96 --- /dev/null +++ b/TC1/timed_task/timed_task.c @@ -0,0 +1,69 @@ +#include +#include +#include"timed_task/timed_task.h" + +pTimedTask task_top = NULL; +int task_count = 0; + +bool AddTask(pTimedTask task) +{ + task_count++ + if (task_top == NULL) + { + task->next = NULL; + task_top = task; + return true; + } + + if (task->time <= task_top->time) + { + task->next = task_top; + task_top = task; + return true; + } + + pTimedTask tmp = task_top; + while (tmp) + { + if (task->time > tmp->time && task->time <= tmp->next->time + || tmp->next == NULL) + { + task->next = tmp->next; + tmp->next = task; + return true; + } + tmp = tmp->next; + } + task_count--; + return false; +} + +bool DelTask() +{ + if (task_top) + { + pTimedTask tmp = task_top; + task_top = task_top->next; + free(tmp); + task_count--; + return true; + } + return false; +} + +char* GetTaskStr() +{ + char* str = (char*)malloc(sizeof(char)*task_count * 40); + pTimedTask tmp_tsk = task_top; + char* tmp_str = str; + tmp_str[0] = '['; + while (tmp_tsk) + { + sprintf(tmp_str, "{time:%d,socket_index:%d,on:%d},", + tmp_tsk->time, tmp_tsk->socket_idx, tmp_tsk->on); + tmp_str += strlen(tmp_str); + tmp_tsk = tmp_tsk->next; + } + *(--tmp_str) = ']'; + return str; +} diff --git a/TC1/timed_task/timed_task.h b/TC1/timed_task/timed_task.h new file mode 100644 index 0000000..48a330e --- /dev/null +++ b/TC1/timed_task/timed_task.h @@ -0,0 +1,17 @@ +#pragma once + +struct TimedTask; +typedef struct TimedTask* pTimedTask; +struct TimedTask +{ + int time; //被执行的格林尼治时间戳 + int socket_idx; //要控制的插孔 + int on; //开或者关 + pTimedTask next; //下一个任务(按之间排序) +}; + +extern pTimedTask task_top; + +bool AddTask(pTimedTask task); +bool DelTask(); +char* GetTaskStr();