initial commit
This commit is contained in:
2
common/utils/iot_math.c
Executable file
2
common/utils/iot_math.c
Executable file
@@ -0,0 +1,2 @@
|
||||
#include "iot_math.h"
|
||||
|
||||
46
common/utils/iot_math.h
Executable file
46
common/utils/iot_math.h
Executable file
@@ -0,0 +1,46 @@
|
||||
#ifndef _IOTMATH_H_INCLUDED
|
||||
#define _IOTMATH_H_INCLUDED 1
|
||||
#include <stdint.h>
|
||||
|
||||
/* 2的n次方数 */
|
||||
#define roundup_pow_of_two(n) \
|
||||
(1UL << \
|
||||
( \
|
||||
( \
|
||||
(n) & (1UL << 31) ? 31 : \
|
||||
(n) & (1UL << 30) ? 30 : \
|
||||
(n) & (1UL << 29) ? 29 : \
|
||||
(n) & (1UL << 28) ? 28 : \
|
||||
(n) & (1UL << 27) ? 27 : \
|
||||
(n) & (1UL << 26) ? 26 : \
|
||||
(n) & (1UL << 25) ? 25 : \
|
||||
(n) & (1UL << 24) ? 24 : \
|
||||
(n) & (1UL << 23) ? 23 : \
|
||||
(n) & (1UL << 22) ? 22 : \
|
||||
(n) & (1UL << 21) ? 21 : \
|
||||
(n) & (1UL << 20) ? 20 : \
|
||||
(n) & (1UL << 19) ? 19 : \
|
||||
(n) & (1UL << 18) ? 18 : \
|
||||
(n) & (1UL << 17) ? 17 : \
|
||||
(n) & (1UL << 16) ? 16 : \
|
||||
(n) & (1UL << 15) ? 15 : \
|
||||
(n) & (1UL << 14) ? 14 : \
|
||||
(n) & (1UL << 13) ? 13 : \
|
||||
(n) & (1UL << 12) ? 12 : \
|
||||
(n) & (1UL << 11) ? 11 : \
|
||||
(n) & (1UL << 10) ? 10 : \
|
||||
(n) & (1UL << 9) ? 9 : \
|
||||
(n) & (1UL << 8) ? 8 : \
|
||||
(n) & (1UL << 7) ? 7 : \
|
||||
(n) & (1UL << 6) ? 6 : \
|
||||
(n) & (1UL << 5) ? 5 : \
|
||||
(n) & (1UL << 4) ? 4 : \
|
||||
(n) & (1UL << 3) ? 3 : \
|
||||
(n) & (1UL << 2) ? 2 : \
|
||||
(n) & (1UL << 1) ? 1 : \
|
||||
(n) & (1UL << 0) ? 0 : -1 \
|
||||
) + 1 \
|
||||
) \
|
||||
)
|
||||
|
||||
#endif
|
||||
29
common/utils/ratelimit.c
Executable file
29
common/utils/ratelimit.c
Executable file
@@ -0,0 +1,29 @@
|
||||
/**********************************************************************************************
|
||||
File name : ratelimit.h
|
||||
Module : common
|
||||
Author :
|
||||
Copyright :
|
||||
Version : 0.1
|
||||
Created on : 2021-11-18
|
||||
Creator : amir.liang
|
||||
Description :
|
||||
以一定的速率执行入参函数, ratelimit(func()); 每次call这个调用,默认每5秒最多执行一次func()
|
||||
Modify History:
|
||||
1. Date: Author: Modification:
|
||||
************************************************************************************************/
|
||||
#include "ratelimit.h"
|
||||
|
||||
static void retalimit_delay(){
|
||||
hv_msleep(500);
|
||||
}
|
||||
void retalimit_test()
|
||||
{
|
||||
uint64_t start = gettimeofday_ms();
|
||||
LOG_TIME(retalimit_delay());
|
||||
while(gettimeofday_ms() - start < 1000){
|
||||
ratelimit(printf("%s test\n", __func__));
|
||||
}
|
||||
LOG_TIME("retalimit_delay()");
|
||||
}
|
||||
|
||||
|
||||
103
common/utils/ratelimit.h
Executable file
103
common/utils/ratelimit.h
Executable file
@@ -0,0 +1,103 @@
|
||||
/**********************************************************************************************
|
||||
File name : ratelimit.h
|
||||
Module : common
|
||||
Author :
|
||||
Copyright :
|
||||
Version : 0.1
|
||||
Created on : 2021-11-18
|
||||
Creator : amir.liang
|
||||
Description :
|
||||
以一定的速率执行入参函数, ratelimit(func()); 每次call这个调用,默认每5秒最多执行一次func()
|
||||
Modify History:
|
||||
1. Date: Author: Modification:
|
||||
************************************************************************************************/
|
||||
|
||||
#ifndef __RATELIMIT_H__
|
||||
#define __RATELIMIT_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include "htime.h"
|
||||
|
||||
/**
|
||||
* 参考linux kernel的 printk_ratelimited
|
||||
*
|
||||
* 使用方法:
|
||||
* 默认5秒只执行1次
|
||||
* ratelimit(printf("test\n"));
|
||||
* *
|
||||
* 设置成10秒可以执行30次
|
||||
* ratelimit2(10, 30, printf("test\n"));
|
||||
*/
|
||||
|
||||
struct ratelimit_state {
|
||||
//下面是运行时信息
|
||||
uint32_t interval_ms;
|
||||
uint32_t next_ms;
|
||||
};
|
||||
|
||||
struct task_time_state
|
||||
{
|
||||
uint32_t task_start;
|
||||
uint32_t task_print_time;
|
||||
};
|
||||
|
||||
#define DEFAULT_RATELIMIT_INTERVAL 5
|
||||
#define DEFAULT_RATELIMIT_BURST 1
|
||||
|
||||
//第2个版本,你也可以指定多少秒可以打印多少次
|
||||
//ratelimit2(10, 30, printf("test\n"));
|
||||
|
||||
#define ratelimit2(_sec, _times, _action) \
|
||||
({ \
|
||||
static struct ratelimit_state _rs = { \
|
||||
_sec*1000/_times, 0 \
|
||||
}; \
|
||||
uint32_t _now = gettimeofday_ms();\
|
||||
if (_now >= _rs.next_ms){ \
|
||||
_rs.next_ms = _now + _rs.interval_ms; \
|
||||
_action; \
|
||||
} \
|
||||
})
|
||||
|
||||
//默认允许5秒打印1次log
|
||||
//ratelimit(printf("test\n"));
|
||||
#define ratelimit(_action) ratelimit2(DEFAULT_RATELIMIT_INTERVAL, 1, _action)
|
||||
|
||||
|
||||
//尝试times,直到action结果等于ifequ,每次延时delayms
|
||||
//do_retry2(action(), 10, 100, 0); 尝试10次action结果等于0时即退出,每次延时100ms
|
||||
#define do_retry2(_action, _times, _delayms, _ifequ_break)\
|
||||
({ \
|
||||
int32_t _t = _times;\
|
||||
while(_t-->0){\
|
||||
if (_ifequ_break)\
|
||||
break;\
|
||||
ratelimit2(1, 5, printf("waiting %dms:%s\n", _delayms, #_action));\
|
||||
usleep(_delayms*1000);\
|
||||
}\
|
||||
})
|
||||
|
||||
//默认延时100ms, action结果等于0时退出
|
||||
//do_retry(action(), 10); 尝试10次,直到action结果等于0时即退出,每次延时100ms
|
||||
#define do_retry(_action, _times) do_retry2(_action, _times, 100, ==0)
|
||||
|
||||
|
||||
|
||||
#define LOG_TIME2(_start_ts, _action) ({\
|
||||
uint64_t pre_ts = _start_ts == -1 ? gettimeofday_ms() : _start_ts;\
|
||||
_action;\
|
||||
printf("%lu %s take:%llums\n", pre_ts, #_action, gettimeofday_ms() - pre_ts);\
|
||||
})
|
||||
|
||||
/*
|
||||
* 计算某个函数或操作所消耗的时间
|
||||
* _action, 可以是一个函数如: retalimit_delay()
|
||||
* _action, 可以是一个字符串: "imin" 用于时间打印记录
|
||||
*/
|
||||
#define LOG_TIME(_action) LOG_TIME2(-1, _action)
|
||||
|
||||
|
||||
void retalimit_test();
|
||||
|
||||
#endif //__RATELIMIT_H__
|
||||
|
||||
38
common/utils/util.c
Executable file
38
common/utils/util.c
Executable file
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "hlog.h"
|
||||
|
||||
#include "util.h"
|
||||
|
||||
|
||||
const char *get_filename(char *path)
|
||||
{
|
||||
CHECK_NULL_RETURN("path", path);
|
||||
char *s1 = strrchr(path, '/');
|
||||
char *s2 = strrchr(path, '\\');
|
||||
if (s1 && s2) {
|
||||
path = (s1 > s2) ? s1 + 1 : s2 + 1;
|
||||
}
|
||||
else if (s1)
|
||||
path = s1 + 1;
|
||||
else if (s2)
|
||||
path = s2 + 1;
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
uint8_t file_exist(const char* path)
|
||||
{
|
||||
return !access(path, F_OK);
|
||||
}
|
||||
|
||||
uint8_t isIP(const char*ip){
|
||||
int32_t a,b,c,d;
|
||||
char str[128];
|
||||
return sscanf(ip,"%d.%d.%d.%d",&a,&b,&c,&d) == 4 || sscanf(ip,"%[^:]|%[^:]|%[^:]|%[^:]|%[^:]|%[^:]|%s",str,str,str,str,str,str,str) == 7;
|
||||
}
|
||||
|
||||
|
||||
94
common/utils/util.h
Executable file
94
common/utils/util.h
Executable file
@@ -0,0 +1,94 @@
|
||||
/*************************************************************************
|
||||
File name : util.h
|
||||
Module : common
|
||||
Author :
|
||||
Copyright :
|
||||
Version : 0.1
|
||||
Created on : 2022-08-18
|
||||
Creator : amir.liang
|
||||
Description :
|
||||
utils
|
||||
Modify History:
|
||||
1. Date: Author: Modification:
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef __UTIL_H__
|
||||
#define __UTIL_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
|
||||
#ifndef OK
|
||||
#define OK (uint8_t)(0)
|
||||
#endif
|
||||
#ifndef NG
|
||||
#define NG (uint8_t)(1)
|
||||
#endif
|
||||
#ifndef MAX_PATH
|
||||
#define MAX_PATH (256)
|
||||
#endif
|
||||
#ifndef MIN
|
||||
#define MIN(x,y) (((x)<(y))?(x):(y))
|
||||
#endif
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(x,y) (((x)>(y))?(x):(y))
|
||||
#endif
|
||||
|
||||
#define SET_BITS(value, bits) ((value) | (bits))
|
||||
|
||||
#define CLEAR_BITS(value, bits) ((value) & ~(bits))
|
||||
|
||||
#define CHECK_BITS(value, bits) ((value) & (bits))
|
||||
|
||||
#define TOGGLE_BITS(value, bits) ((value) ^ (bits))
|
||||
|
||||
// note: 'a' must be align 2
|
||||
#define ROUNDUP(x, a) (((x) + (a)-1) & ~((a)-1)) // ALIGN(x, a)
|
||||
|
||||
#define CHECK_SAME(a, b) ({\
|
||||
if ( a != b ){\
|
||||
hloge("%s [%d]!=[%d], fail.", __func__, a, b); \
|
||||
}; a==b;})
|
||||
|
||||
#define CHECK_SAME_RETURN(a, b) ({if (!CHECK_SAME(a, b) ){ return NG;}})
|
||||
|
||||
|
||||
#define CHECK_NULL(who, val) ({if(!val){hloge("%s %s is null param", __func__, who);}; val;})
|
||||
|
||||
#define CHECK_NULL_RETURN(who, val) if (!CHECK_NULL(who, val)){return NULL;}
|
||||
|
||||
#define CHECK_LESS_RETURN(who, val) if(who<val){hloge("%s %d<%d", __func__, who, val); return NG;}
|
||||
|
||||
|
||||
#ifndef ARRAY_SIZE
|
||||
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#define DECLAR_UT_CMD(FUNC, ARRAY, CMD, USAGE) \
|
||||
{FUNC, ARRAY, ARRAY_SIZE(ARRAY), CMD, USAGE}
|
||||
|
||||
typedef struct unittest_st unittest_cmd;
|
||||
|
||||
typedef struct unittest_st
|
||||
{
|
||||
int32_t (*exec_func)(int32_t argc, char **argv); /* 如果不为NULL 即为终函数 */
|
||||
unittest_cmd *next_cmd;
|
||||
uint16_t next_cmd_cnt;
|
||||
char *cmd_name;
|
||||
char *cmd_help; /* 如果exec_func=NULL 即 一个'cmd_name'提示help*/
|
||||
} unittest_cmd;
|
||||
|
||||
|
||||
const char *get_filename(char *path);
|
||||
|
||||
uint8_t file_exist(const char* path);
|
||||
|
||||
uint8_t isIP(const char*ip);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user