This repository has been archived on 2025-02-28. You can view files and clone it, but cannot push or open issues or pull requests.
controller-hd/User/lib/flow/flow_core.c

84 lines
2.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @file
* @author xxx
* @date 2023-07-21 17:00:15
* @brief
* @copyright Copyright (c) 2023 by xxx, All Rights Reserved.
*/
#include "flow.h"
unsigned long flow_tick;
/**
* @brief 设置流量计器的间隔时间。
* @param t 要设置的流量计器。
* @param interval 要设置的间隔时间值。
* @return 如果间隔时间无效或无法设置则返回NULL。
* @note 此函数设置间隔时间并重置计时器,因此它将在下一个间隔时间 occurs时开始。
*/
void fl_timer_set(struct flow_timer *t, unsigned long interval)
{
if (interval == 0)
{
// 如果间隔时间为零,返回错误
return;
}
t->interval = interval;
t->start = flow_tick;
}
/**
* @brief 重置流量计器。
* @param t 要重置的流量计器。
* @return 如果无法重置计时器则返回NULL。
* @note 此函数将计时器的开始时间加上间隔时间,因此它将在下一个间隔时间 occurs时开始。
*/
void fl_timer_reset(struct flow_timer *t)
{
t->start += t->interval;
}
/**
* @brief 重新启动流量计器。
* @param t 要重新启动的流量计器。
* @return 如果无法重新启动计时器则返回NULL。
* @note 此函数将计时器的开始时间设置为当前flow_tick因此它将在重新开始时从基本开始。
*/
void fl_timer_restart(struct flow_timer *t)
{
t->start = flow_tick;
}
/**
* @brief 检查给定的flow_timer结构体的超时状态
* @param {flow_timer} *t 指向flow_timer结构体的指针
* @return {unsigned char} 超时返回1否则返回0
* @note 检查当前时间与flow_timer结构体中的start时间之差是否大于或等于interval
*/
unsigned char fl_timer_timeout(struct flow_timer *t)
{
return ((flow_tick - t->start) >= t->interval) ? 1U : 0U;
}
/**
* @brief 计算给定flow_timer结构体中的时间长度
* @param {flow_timer} *t 指向flow_timer结构体的指针
* @return {unsigned long} 返回时间长度
* @note 计算start时间加上interval与当前时间flow_tick之差如果大于等于flow_tick则返回time_len - flow_tick否则返回0
*/
unsigned long fl_hour_much_time(struct flow_timer *t)
{
unsigned long time_len = t->start + t->interval;
if (time_len >= flow_tick)
{
return (time_len - flow_tick);
}
else
{
return 0U;
}
}