Initial commit: TXW82x FPV v2.7.0.7-42229 SDK + project sources
This commit is contained in:
129
sdk/lib/net/mqtt/kawaii/common/log/arch/txw/arch.c
Normal file
129
sdk/lib/net/mqtt/kawaii/common/log/arch/txw/arch.c
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-26 19:11:40
|
||||
* @LastEditTime : 2019-12-28 01:15:29
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#include "salof_defconfig.h"
|
||||
|
||||
#ifdef SALOF_USING_LOG
|
||||
|
||||
void *salof_alloc(unsigned int size)
|
||||
{
|
||||
return os_malloc(size);
|
||||
}
|
||||
|
||||
void salof_free(void *mem)
|
||||
{
|
||||
os_free(mem);
|
||||
}
|
||||
|
||||
salof_tcb salof_task_create(const char *name,
|
||||
void (*task_entry)(void *param),
|
||||
void * const param,
|
||||
unsigned int stack_size,
|
||||
unsigned int priority,
|
||||
unsigned int tick)
|
||||
{
|
||||
salof_tcb task = NULL;
|
||||
void *stack = NULL; //124 = sizeof(ktask_t)
|
||||
|
||||
task = salof_alloc(sizeof(salof_tcb_t));
|
||||
if (task == NULL) {
|
||||
os_printf("[%s]:task malloc failed!!!\n",__FUNCTION__);
|
||||
return NULL;
|
||||
}
|
||||
stack = salof_alloc(stack_size+124);
|
||||
if (stack == NULL) {
|
||||
os_printf("[%s]:stack malloc failed!!!\n",__FUNCTION__);
|
||||
salof_free(task);
|
||||
return NULL;
|
||||
}
|
||||
os_task_init((const uint8 *)name, task, task_entry, (uint32)param);
|
||||
os_task_set_priority(task, priority);
|
||||
os_task_set_stack(task, stack, stack_size+124); //124 = sizeof(ktask_t)
|
||||
os_task_run(task);
|
||||
return task;
|
||||
}
|
||||
|
||||
salof_mutex salof_mutex_create(void)
|
||||
{
|
||||
salof_mutex mutex = salof_alloc(sizeof(salof_mutex_t));
|
||||
if (mutex == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (os_mutex_init(mutex) != RET_OK) {
|
||||
salof_free(mutex);
|
||||
return NULL;
|
||||
}
|
||||
return mutex;
|
||||
}
|
||||
|
||||
|
||||
void salof_mutex_delete(salof_mutex mutex)
|
||||
{
|
||||
os_mutex_del(mutex);
|
||||
salof_free(mutex);
|
||||
}
|
||||
|
||||
|
||||
int salof_mutex_pend(salof_mutex mutex, unsigned int timeout)
|
||||
{
|
||||
return os_mutex_lock(mutex, timeout) == RET_OK ? 0 : -1;
|
||||
}
|
||||
|
||||
int salof_mutex_post(salof_mutex mutex)
|
||||
{
|
||||
return os_mutex_unlock(mutex) == RET_OK ? 0 : -1;
|
||||
}
|
||||
|
||||
|
||||
salof_sem salof_sem_create(void)
|
||||
{
|
||||
salof_sem sem = salof_alloc(sizeof(salof_sem_t));
|
||||
if (sem == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (os_sema_init(sem, 0) != RET_OK) {
|
||||
salof_free(sem);
|
||||
return NULL;
|
||||
}
|
||||
return sem;
|
||||
}
|
||||
|
||||
void salof_sem_delete(salof_sem sem)
|
||||
{
|
||||
os_sema_del(sem);
|
||||
salof_free(sem);
|
||||
}
|
||||
|
||||
int salof_sem_pend(salof_sem sem, unsigned int timeout)
|
||||
{
|
||||
return os_sema_down(sem, timeout) == RET_OK ? 0 : -1;
|
||||
}
|
||||
|
||||
int salof_sem_post(salof_sem sem)
|
||||
{
|
||||
return os_sema_up(sem) == RET_OK ? 0 : -1;
|
||||
}
|
||||
|
||||
unsigned int salof_get_tick(void)
|
||||
{
|
||||
return os_jiffies_to_msecs(os_jiffies());
|
||||
}
|
||||
|
||||
char *salof_get_task_name(void)
|
||||
{
|
||||
struct os_task *task = NULL;
|
||||
task = (struct os_task *)os_task_hdl2tsk(os_task_current());
|
||||
return task ? (char *)task->name : NULL;
|
||||
}
|
||||
|
||||
int send_buff(char *buf, int len)
|
||||
{
|
||||
hgprintf_out(buf, len, 0);
|
||||
return len;
|
||||
}
|
||||
|
||||
#endif
|
||||
136
sdk/lib/net/mqtt/kawaii/common/log/fifo.c
Normal file
136
sdk/lib/net/mqtt/kawaii/common/log/fifo.c
Normal file
@@ -0,0 +1,136 @@
|
||||
#include "fifo.h"
|
||||
#include <string.h>
|
||||
|
||||
#ifdef SALOF_USING_LOG
|
||||
|
||||
static unsigned int _flbs(unsigned int x) /* find last bit set*/
|
||||
{
|
||||
unsigned int r = 32;
|
||||
|
||||
if (!x)
|
||||
return 0;
|
||||
if (!(x & 0xffff0000u)) {
|
||||
x <<= 16;
|
||||
r -= 16;
|
||||
}
|
||||
if (!(x & 0xff000000u)) {
|
||||
x <<= 8;
|
||||
r -= 8;
|
||||
}
|
||||
if (!(x & 0xf0000000u)) {
|
||||
x <<= 4;
|
||||
r -= 4;
|
||||
}
|
||||
if (!(x & 0xc0000000u)) {
|
||||
x <<= 2;
|
||||
r -= 2;
|
||||
}
|
||||
if (!(x & 0x80000000u)) {
|
||||
x <<= 1;
|
||||
r -= 1;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static unsigned int _salof_fifo_align(unsigned int x)
|
||||
{
|
||||
return (1 << (_flbs(x-1)-1)); //memory down alignment
|
||||
}
|
||||
|
||||
salof_fifo_t salof_fifo_create(unsigned int size)
|
||||
{
|
||||
salof_fifo_t fifo;
|
||||
|
||||
if (0 == size)
|
||||
return NULL;
|
||||
|
||||
if (size & (size - 1))
|
||||
size = _salof_fifo_align(size);
|
||||
|
||||
fifo = (salof_fifo_t)salof_alloc((sizeof(struct salof_fifo) + size));
|
||||
|
||||
if (NULL != fifo) {
|
||||
fifo->buff = (unsigned char *)fifo + sizeof(struct salof_fifo);
|
||||
|
||||
fifo->mutex = salof_mutex_create();
|
||||
fifo->sem = salof_sem_create();
|
||||
if ((NULL == fifo->mutex) || (NULL == fifo->sem)) {
|
||||
salof_free(fifo);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fifo->size = size;
|
||||
fifo->in = 0;
|
||||
fifo->out = 0;
|
||||
|
||||
return fifo;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned int salof_fifo_write(salof_fifo_t fifo, void *buff, unsigned int len, unsigned int timeout)
|
||||
{
|
||||
int err, l;
|
||||
|
||||
if((!fifo) || (!buff) || (!len))
|
||||
return 0;
|
||||
|
||||
err = salof_mutex_pend(fifo->mutex, timeout);
|
||||
if(err == -1)
|
||||
return 0;
|
||||
|
||||
len = FIFO_MIN(len, (fifo->size - fifo->in + fifo->out));
|
||||
|
||||
l = FIFO_MIN(len, (fifo->size - (fifo->in & (fifo->size -1))));
|
||||
memcpy(((unsigned char *)fifo->buff + (fifo->in & (fifo->size -1))), buff, l);
|
||||
memcpy(fifo->buff, (unsigned char *)buff + l, len - l);
|
||||
|
||||
fifo->in += len;
|
||||
|
||||
salof_mutex_post(fifo->mutex);
|
||||
salof_sem_post(fifo->sem);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
unsigned int salof_fifo_read(salof_fifo_t fifo, void *buff, unsigned int len, unsigned int timeout)
|
||||
{
|
||||
int l;
|
||||
|
||||
salof_sem_pend(fifo->sem, timeout);
|
||||
|
||||
if((!fifo) || (!buff) || (!len))
|
||||
return 0;
|
||||
|
||||
len = FIFO_MIN(len, fifo->in - fifo->out);
|
||||
|
||||
l = FIFO_MIN(len, (fifo->size - (fifo->out & (fifo->size -1))));
|
||||
memcpy(buff, ((unsigned char *)fifo->buff + (fifo->out & (fifo->size -1))), l);
|
||||
memcpy((unsigned char *)buff + l, fifo->buff, len - l);
|
||||
|
||||
fifo->out += len;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
unsigned int salof_fifo_read_able(salof_fifo_t fifo)
|
||||
{
|
||||
if(NULL == fifo)
|
||||
return 0;
|
||||
|
||||
else if(fifo->in == fifo->out)
|
||||
return 0;
|
||||
|
||||
else if(fifo->in > fifo->out)
|
||||
return (fifo->in - fifo->out);
|
||||
|
||||
return (fifo->size - (fifo->out - fifo->in));
|
||||
}
|
||||
|
||||
unsigned int salof_fifo_write_able(salof_fifo_t fifo)
|
||||
{
|
||||
return (fifo->size - salof_fifo_read_able(fifo));
|
||||
}
|
||||
|
||||
#endif
|
||||
40
sdk/lib/net/mqtt/kawaii/common/log/fifo.h
Normal file
40
sdk/lib/net/mqtt/kawaii/common/log/fifo.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-25 23:54:38
|
||||
* @LastEditTime: 2020-06-17 15:10:03
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _FIFO_H_
|
||||
#define _FIFO_H_
|
||||
|
||||
#include "salof_defconfig.h"
|
||||
|
||||
#ifdef SALOF_USING_LOG
|
||||
|
||||
#define FIFO_READ 0
|
||||
#define FIFO_WRITE 1
|
||||
|
||||
#define FIFO_MAX(a,b) (((a) > (b)) ? (a) : (b))
|
||||
#define FIFO_MIN(a,b) (((a) < (b)) ? (a) : (b))
|
||||
|
||||
struct salof_fifo {
|
||||
unsigned int size; /* fifo size */
|
||||
unsigned int in; /* data input pointer (in % size) */
|
||||
unsigned int out; /* data output pointer (out % size) */
|
||||
salof_mutex mutex; /* mutex */
|
||||
salof_sem sem; /* sem */
|
||||
void *buff; /* data area */
|
||||
};
|
||||
typedef struct salof_fifo * salof_fifo_t;
|
||||
|
||||
salof_fifo_t salof_fifo_create(unsigned int size);
|
||||
unsigned int salof_fifo_write(salof_fifo_t fifo, void *buff, unsigned int len, unsigned int timeout);
|
||||
unsigned int salof_fifo_read(salof_fifo_t fifo, void *buff, unsigned int len, unsigned int timeout);
|
||||
unsigned int salof_fifo_read_able(salof_fifo_t fifo);
|
||||
unsigned int salof_fifo_write_able(salof_fifo_t fifo);
|
||||
|
||||
#endif
|
||||
|
||||
#endif // !_FIFO_H_
|
||||
|
||||
290
sdk/lib/net/mqtt/kawaii/common/log/format.c
Normal file
290
sdk/lib/net/mqtt/kawaii/common/log/format.c
Normal file
@@ -0,0 +1,290 @@
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-25 23:54:19
|
||||
* @LastEditTime: 2020-06-17 15:23:09
|
||||
* @Description: the code belongs to jiejie, please keep the author insalof_formation and source code according to the license.
|
||||
*/
|
||||
#include "format.h"
|
||||
#include "salof_defconfig.h"
|
||||
|
||||
#ifdef SALOF_USING_LOG
|
||||
|
||||
static int _get_atoi(const char **str)
|
||||
{
|
||||
int n;
|
||||
for (n = 0; is_digit(**str); (*str)++)
|
||||
n = n * 10 + **str - '0';
|
||||
return n;
|
||||
}
|
||||
|
||||
static void _buff_put_char(char *buf, unsigned int *pos, unsigned int max, char c)
|
||||
{
|
||||
if (*pos < max)
|
||||
buf[(*pos)] = c;
|
||||
(*pos)++;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Formats an integer number
|
||||
* buf - buffer to print into
|
||||
* len - current position in buffer
|
||||
* maxlen - last valid position in buf
|
||||
* num - number to print
|
||||
* base - it's base
|
||||
* width - how many spaces this should have; padding
|
||||
* flags - above F flags
|
||||
*/
|
||||
static void _salof_format_int(char *buf, unsigned int *len, unsigned int maxlen,
|
||||
signed long long num, int base, int width, int flags)
|
||||
{
|
||||
char nbuf[64], sign = 0;
|
||||
char altb[8]; // small buf for sign and #
|
||||
int n = num;
|
||||
int npad; // number of pads
|
||||
char pchar = ' '; // padding character
|
||||
char *digits = "0123456789ABCDEF";
|
||||
char *ldigits = "0123456789abcdef";
|
||||
int i, j;
|
||||
|
||||
if (base < 2 || base > 16)
|
||||
return;
|
||||
if (flags & F_SMALL) digits = ldigits;
|
||||
if (flags & F_LEFT) flags &= ~F_ZEROPAD;
|
||||
|
||||
if ((flags & F_SIGNED) && num < 0) {
|
||||
n = -num;
|
||||
sign = '-';
|
||||
} else if (flags & F_PLUS) {
|
||||
sign = '+';
|
||||
} else if (flags & F_SPACE)
|
||||
sign = ' ';
|
||||
|
||||
i = 0;
|
||||
do {
|
||||
nbuf[i++] = digits[n % base];
|
||||
n = n / base;
|
||||
} while (n > 0);
|
||||
|
||||
j = 0;
|
||||
if (sign) altb[j++] = sign;
|
||||
if (flags & F_ALTERNATE) {
|
||||
if (base == 8 || base == 16) {
|
||||
altb[j++] = '0';
|
||||
if (base == 16)
|
||||
altb[j++] = (flags & F_SMALL) ? 'x' : 'X';
|
||||
}
|
||||
}
|
||||
altb[j] = 0;
|
||||
|
||||
npad = width > i + j ? width - i - j : 0;
|
||||
|
||||
if (width > i + j)
|
||||
npad = width - i - j;
|
||||
|
||||
if (npad > 0 && ((flags & F_LEFT) == 0)) {
|
||||
if (flags & F_ZEROPAD) {
|
||||
for (j = 0; altb[j]; j++)
|
||||
_buff_put_char(buf, len, maxlen, altb[j]);
|
||||
altb[0] = 0;
|
||||
}
|
||||
while (npad-- > 0)
|
||||
_buff_put_char(buf, len, maxlen, (flags & F_ZEROPAD) ? '0' : ' ');
|
||||
}
|
||||
for (j = 0; altb[j]; j++)
|
||||
_buff_put_char(buf, len, maxlen, altb[j]);
|
||||
|
||||
while (i-- > 0)
|
||||
_buff_put_char(buf, len, maxlen, nbuf[i]);
|
||||
|
||||
if (npad > 0 && (flags & F_LEFT))
|
||||
while(npad-- > 0)
|
||||
_buff_put_char(buf, len, maxlen, pchar);
|
||||
}
|
||||
|
||||
static void _salof_format_char(char *buf, unsigned int *pos, unsigned int max, char c,
|
||||
int width, int flags)
|
||||
{
|
||||
int npad = 0;
|
||||
if (width > 0) npad = width - 1;
|
||||
if (npad < 0) npad = 0;
|
||||
|
||||
if (npad && ((flags & F_LEFT) == 0))
|
||||
while (npad-- > 0)
|
||||
_buff_put_char(buf, pos, max, ' ');
|
||||
|
||||
_buff_put_char(buf, pos, max, c);
|
||||
|
||||
if (npad && (flags & F_LEFT))
|
||||
while (npad-- > 0)
|
||||
_buff_put_char(buf, pos, max, ' ');
|
||||
}
|
||||
|
||||
/**
|
||||
* strlen()
|
||||
*/
|
||||
static unsigned int _str_len(char *s)
|
||||
{
|
||||
unsigned int i;
|
||||
for (i = 0; *s; i++, s++)
|
||||
;
|
||||
return i;
|
||||
}
|
||||
|
||||
static void _salof_format_str(char *buf, unsigned int *pos, unsigned int max, char *s,
|
||||
int width, int flags)
|
||||
{
|
||||
int npad = 0;
|
||||
if (width > 0) npad = width - _str_len(s);
|
||||
if (npad < 0) npad = 0;
|
||||
|
||||
if (npad && ((flags & F_LEFT) == 0))
|
||||
while (npad-- > 0)
|
||||
_buff_put_char(buf, pos, max, ' ');
|
||||
|
||||
while (*s)
|
||||
_buff_put_char(buf, pos, max, *s++);
|
||||
|
||||
if (npad && (flags & F_LEFT))
|
||||
while (npad-- > 0)
|
||||
_buff_put_char(buf, pos, max, ' ');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/***********************************************************************************************************************/
|
||||
|
||||
/**
|
||||
* Shrinked down, vsnprintf implementation.
|
||||
* This will not handle floating numbers (yet).
|
||||
*/
|
||||
int salof_format_nstr(char *buf, unsigned int size, const char *fmt, va_list ap)
|
||||
{
|
||||
unsigned int n = 0;
|
||||
char c, *s;
|
||||
char state = 0;
|
||||
signed long long num;
|
||||
int base;
|
||||
int flags = 0;
|
||||
int width = 0;
|
||||
int precision = 0;
|
||||
int lflags = 0;
|
||||
|
||||
if (!buf) size = 0;
|
||||
|
||||
for (;;) {
|
||||
c = *fmt++;
|
||||
if (state == S_DEFAULT) {
|
||||
if (c == '%') {
|
||||
state = S_FLAGS;
|
||||
flags = 0;
|
||||
} else {
|
||||
_buff_put_char(buf, &n, size, c);
|
||||
}
|
||||
} else if (state == S_FLAGS) {
|
||||
switch (c) {
|
||||
case '#': flags |= F_ALTERNATE; break;
|
||||
case '0': flags |= F_ZEROPAD; break;
|
||||
case '-': flags |= F_LEFT; break;
|
||||
case ' ': flags |= F_SPACE; break;
|
||||
case '+': flags |= F_PLUS; break;
|
||||
case '\'':
|
||||
case 'I' : break; // not yet used
|
||||
default: fmt--; width = 0; state = S_WIDTH;
|
||||
}
|
||||
} else if (state == S_WIDTH) {
|
||||
if (c == '*') {
|
||||
width = va_arg(ap, int);
|
||||
if (width < 0) {
|
||||
width = -width;
|
||||
flags |= F_LEFT;
|
||||
}
|
||||
} else if (is_digit(c) && c > '0') {
|
||||
fmt--;
|
||||
width = _get_atoi(&fmt);
|
||||
} else {
|
||||
fmt--;
|
||||
precision = -1;
|
||||
state = S_PRECIS;
|
||||
}
|
||||
} else if (state == S_PRECIS) {
|
||||
// Ignored for now, but skip it
|
||||
if (c == '.') {
|
||||
if (is_digit(*fmt))
|
||||
precision = _get_atoi(&fmt);
|
||||
else if (*fmt == '*')
|
||||
precision = va_arg(ap, int);
|
||||
precision = precision < 0 ? 0 : precision;
|
||||
} else
|
||||
fmt--;
|
||||
lflags = 0;
|
||||
state = S_LENGTH;
|
||||
} else if (state == S_LENGTH) {
|
||||
switch(c) {
|
||||
case 'h': lflags = lflags == L_CHAR ? L_SHORT : L_CHAR; break;
|
||||
case 'l': lflags = lflags == L_LONG ? L_LLONG : L_LONG; break;
|
||||
case 'L': lflags = L_DOUBLE; break;
|
||||
default: fmt--; state = S_CONV;
|
||||
}
|
||||
} else if (state == S_CONV) {
|
||||
if (c == 'd' || c == 'i' || c == 'o' || c == 'b' || c == 'u'
|
||||
|| c == 'x' || c == 'X') {
|
||||
if (lflags == L_LONG)
|
||||
num = va_arg(ap, int);
|
||||
else if (lflags & (L_LLONG | L_DOUBLE))
|
||||
num = va_arg(ap, signed long long);
|
||||
else if (c == 'd' || c == 'i')
|
||||
num = va_arg(ap, int);
|
||||
else
|
||||
num = (unsigned int) va_arg(ap, int);
|
||||
|
||||
base = 10;
|
||||
if (c == 'd' || c == 'i') {
|
||||
flags |= F_SIGNED;
|
||||
} else if (c == 'x' || c == 'X') {
|
||||
flags |= c == 'x' ? F_SMALL : 0;
|
||||
base = 16;
|
||||
} else if (c == 'o') {
|
||||
base = 8;
|
||||
} else if (c == 'b') {
|
||||
base = 2;
|
||||
}
|
||||
_salof_format_int(buf, &n, size, num, base, width, flags);
|
||||
} else if (c == 'p') {
|
||||
num = (size_t) va_arg(ap, void *);
|
||||
base = 16;
|
||||
flags |= F_SMALL | F_ALTERNATE;
|
||||
_salof_format_int(buf, &n, size, num, base, width, flags);
|
||||
} else if (c == 's') {
|
||||
s = va_arg(ap, char *);
|
||||
if (!s)
|
||||
s = "(null)";
|
||||
_salof_format_str(buf, &n, size, s, width, flags);
|
||||
} else if (c == 'c') {
|
||||
c = va_arg(ap, int);
|
||||
_salof_format_char(buf, &n, size, c, width, flags);
|
||||
} else if (c == '%') {
|
||||
_buff_put_char(buf, &n, size, c);
|
||||
} else {
|
||||
_buff_put_char(buf, &n, size, '%');
|
||||
_buff_put_char(buf, &n, size, c);
|
||||
}
|
||||
state = S_DEFAULT;
|
||||
}
|
||||
if (c == 0)
|
||||
break;
|
||||
}
|
||||
n--;
|
||||
if (n < size)
|
||||
buf[n] = 0;
|
||||
else if (size > 0)
|
||||
buf[size - 1] = 0;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
44
sdk/lib/net/mqtt/kawaii/common/log/format.h
Normal file
44
sdk/lib/net/mqtt/kawaii/common/log/format.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-25 23:54:38
|
||||
* @LastEditTime: 2020-06-17 15:22:42
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _FORMAT_H_
|
||||
#define _FORMAT_H_
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#define FORMAT_BUF_LEN 12
|
||||
|
||||
/* Format states */
|
||||
#define S_DEFAULT 0
|
||||
#define S_FLAGS 1
|
||||
#define S_WIDTH 2
|
||||
#define S_PRECIS 3
|
||||
#define S_LENGTH 4
|
||||
#define S_CONV 5
|
||||
|
||||
/* Lenght flags */
|
||||
#define L_CHAR 1
|
||||
#define L_SHORT 2
|
||||
#define L_LONG 3
|
||||
#define L_LLONG 4
|
||||
#define L_DOUBLE 5
|
||||
|
||||
#define F_ALTERNATE 0001 // put 0x infront 16, 0 on octals, b on binary
|
||||
#define F_ZEROPAD 0002 // value should be zero padded
|
||||
#define F_LEFT 0004 // left justified if set, otherwise right justified
|
||||
#define F_SPACE 0010 // place a space before positive number
|
||||
#define F_PLUS 0020 // show +/- on signed numbers, default only for -
|
||||
#define F_SIGNED 0040 // is an unsigned number?
|
||||
#define F_SMALL 0100 // use lowercase for hex?
|
||||
|
||||
#define is_digit(c) (c >= '0' && c <= '9')
|
||||
|
||||
int salof_format_nstr(char *buf, unsigned int size, const char *fmt, va_list ap);
|
||||
|
||||
|
||||
#endif // !_FORMAT_H_
|
||||
|
||||
110
sdk/lib/net/mqtt/kawaii/common/log/salof.c
Normal file
110
sdk/lib/net/mqtt/kawaii/common/log/salof.c
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-27 23:10:36
|
||||
* @LastEditTime: 2020-06-17 15:22:56
|
||||
* @Description: the code belongs to jiejie, please keep the author insalof_formation and source code according to the license.
|
||||
*/
|
||||
/** synchronous asynchronous log output framework */
|
||||
|
||||
#include "salof.h"
|
||||
|
||||
#ifdef SALOF_USING_LOG
|
||||
|
||||
#ifndef SALOF_BUFF_SIZE
|
||||
#define SALOF_BUFF_SIZE (1024U)
|
||||
#endif
|
||||
#ifndef SALOF_FIFO_SIZE
|
||||
#define SALOF_FIFO_SIZE (2048U)
|
||||
#endif
|
||||
|
||||
static int salof_out(char *buf, int len);
|
||||
|
||||
#if SALOF_USING_SALOF
|
||||
#include <string.h>
|
||||
static salof_fifo_t _salof_fifo = NULL;
|
||||
static int _len;
|
||||
static char _out_buff[SALOF_BUFF_SIZE];
|
||||
|
||||
#if !SALOF_USING_IDLE_HOOK
|
||||
static salof_tcb _salof_task;
|
||||
void salof_task(void *parm);
|
||||
#else
|
||||
|
||||
#if !defined(salof_handler)
|
||||
#error "salof_handler need to be defined as your hook function"
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static char _salof_format_buff[SALOF_BUFF_SIZE];
|
||||
|
||||
int salof_init(void)
|
||||
{
|
||||
#if SALOF_USING_SALOF
|
||||
_salof_fifo = salof_fifo_create(SALOF_FIFO_SIZE);
|
||||
if(_salof_fifo == NULL)
|
||||
return -1;
|
||||
|
||||
#if !SALOF_USING_IDLE_HOOK
|
||||
_salof_task = salof_task_create("salof_task", salof_task, NULL, SALOF_TASK_STACK_SIZE, SALOF_TASK_PRIO, SALOF_TASK_TICK);
|
||||
if(_salof_task == NULL)
|
||||
return -1;
|
||||
#endif
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void salof(const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
int len;
|
||||
va_start(args, fmt);
|
||||
|
||||
len = salof_format_nstr(_salof_format_buff, SALOF_BUFF_SIZE - 1, fmt, args);
|
||||
|
||||
if(len > SALOF_BUFF_SIZE)
|
||||
len = SALOF_BUFF_SIZE - 1;
|
||||
|
||||
#if SALOF_USING_SALOF
|
||||
salof_fifo_write(_salof_fifo, _salof_format_buff, len, 100);
|
||||
#else
|
||||
salof_out(_salof_format_buff, len);
|
||||
#endif
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
static int salof_out(char *buf, int len)
|
||||
{
|
||||
return send_buff(buf, len);
|
||||
}
|
||||
|
||||
#if SALOF_USING_SALOF
|
||||
void salof_handler( void )
|
||||
{
|
||||
_len = salof_fifo_read(_salof_fifo, _out_buff, sizeof(_out_buff), -1);
|
||||
if(_len > 0) {
|
||||
salof_out((char *)_out_buff, _len);
|
||||
memset(_out_buff, 0, _len);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !SALOF_USING_IDLE_HOOK
|
||||
void salof_task(void *parm)
|
||||
{
|
||||
(void)parm;
|
||||
while(1)
|
||||
{
|
||||
#if SALOF_USING_SALOF
|
||||
salof_handler();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
113
sdk/lib/net/mqtt/kawaii/common/log/salof.h
Normal file
113
sdk/lib/net/mqtt/kawaii/common/log/salof.h
Normal file
@@ -0,0 +1,113 @@
|
||||
#ifndef _SALOF_H_
|
||||
#define _SALOF_H_
|
||||
|
||||
#include "salof_defconfig.h"
|
||||
#include "format.h"
|
||||
#include "fifo.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int salof_init(void);
|
||||
void salof(const char *fmt, ...);
|
||||
|
||||
/** font color */
|
||||
#define SALOF_FC_BLACK 30
|
||||
#define SALOF_FC_RED 31
|
||||
#define SALOF_FC_GREEN 32
|
||||
#define SALOF_FC_YELLOW 33
|
||||
#define SALOF_FC_BLUE 34
|
||||
#define SALOF_FC_PURPLE 35
|
||||
#define SALOF_FC_DARK 36
|
||||
#define SALOF_FC_WHITE 37
|
||||
|
||||
#ifdef SALOF_USING_LOG
|
||||
|
||||
#if SALOF_USING_SALOF
|
||||
#define SALOF_PRINT_LOG salof
|
||||
#else
|
||||
|
||||
#if ((!SALOF_USING_SALOF)&&(!SALOF_PRINT_LOG))
|
||||
#define SALOF_PRINT_LOG printf
|
||||
#endif
|
||||
|
||||
#ifndef SALOF_PRINT_LOG
|
||||
#error "If the SALOF_USING_LOG macro definition is turned on, you must define SALOF_PRINT_LOG as the LOG output, such as #definePRINT_LOG printf"
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#if SALOF_LOG_COLOR
|
||||
#define SALOF_LOG_START(l, c) SALOF_PRINT_LOG("\033\n["#c"m["#l"] >> ")
|
||||
#define SALOF_LOG_END SALOF_PRINT_LOG("\033[0m")
|
||||
#else
|
||||
#define SALOF_LOG_START(l, c) SALOF_PRINT_LOG("\n["#l"] >> ")
|
||||
#define SALOF_LOG_END
|
||||
#endif
|
||||
|
||||
#if SALOF_LOG_TS && SALOF_LOG_TAR
|
||||
#define SALOF_LOG_T SALOF_PRINT_LOG("[TS: %d] [TAR: %s] ",salof_get_tick(), salof_get_task_name())
|
||||
#elif SALOF_LOG_TS
|
||||
#define SALOF_LOG_T SALOF_PRINT_LOG("[TS: %d] ", salof_get_tick())
|
||||
#elif SALOF_LOG_TAR
|
||||
#define SALOF_LOG_T SALOF_PRINT_LOG("[TAR: %s] ", salof_get_task_name())
|
||||
#else
|
||||
#define SALOF_LOG_T
|
||||
#endif
|
||||
|
||||
|
||||
#define SALOF_LOG_LINE(l, c, fmt, ...) \
|
||||
do { \
|
||||
SALOF_LOG_START(l, c); \
|
||||
SALOF_LOG_T; \
|
||||
SALOF_PRINT_LOG(fmt, ##__VA_ARGS__); \
|
||||
SALOF_LOG_END; \
|
||||
} while (0)
|
||||
|
||||
#define SALOF_BASE_LEVEL (0)
|
||||
#define SALOF_ERR_LEVEL (SALOF_BASE_LEVEL + 1)
|
||||
#define SALOF_WARN_LEVEL (SALOF_ERR_LEVEL + 1)
|
||||
#define SALOF_INFO_LEVEL (SALOF_WARN_LEVEL + 1)
|
||||
#define SALOF_DEBUG_LEVEL (SALOF_INFO_LEVEL + 1)
|
||||
|
||||
#ifndef SALOF_LOG_LEVEL
|
||||
#define SALOF_LOG_LEVEL SALOF_DEBUG_LEVEL
|
||||
#endif
|
||||
|
||||
#if SALOF_LOG_LEVEL < SALOF_DEBUG_LEVEL
|
||||
#define SALOF_LOG_DEBUG(fmt, ...)
|
||||
#else
|
||||
#define SALOF_LOG_DEBUG(fmt, ...) SALOF_LOG_LINE(D, 0, fmt, ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if SALOF_LOG_LEVEL < SALOF_INFO_LEVEL
|
||||
#define SALOF_LOG_INFO(fmt, ...)
|
||||
#else
|
||||
#define SALOF_LOG_INFO(fmt, ...) SALOF_LOG_LINE(I, SALOF_FC_GREEN, fmt, ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if SALOF_LOG_LEVEL < SALOF_WARN_LEVEL
|
||||
#define SALOF_LOG_WARN(fmt, ...)
|
||||
#else
|
||||
#define SALOF_LOG_WARN(fmt, ...) SALOF_LOG_LINE(W, SALOF_FC_YELLOW, fmt, ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if SALOF_LOG_LEVEL < SALOF_ERR_LEVEL
|
||||
#define SALOF_LOG_ERR(fmt, ...)
|
||||
#else
|
||||
#define SALOF_LOG_ERR(fmt, ...) SALOF_LOG_LINE(E, SALOF_FC_RED, fmt, ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if SALOF_LOG_LEVEL < SALOF_BASE_LEVEL
|
||||
#define SALOF_LOG(fmt, ...)
|
||||
#else
|
||||
#define SALOF_LOG(fmt, ...) SALOF_PRINT_LOG(fmt, ##__VA_ARGS__)
|
||||
#endif
|
||||
#else
|
||||
#define SALOF_LOG_DEBUG(fmt, ...)
|
||||
#define SALOF_LOG_INFO(fmt, ...)
|
||||
#define SALOF_LOG_WARN(fmt, ...)
|
||||
#define SALOF_LOG_ERR(fmt, ...)
|
||||
#define SALOF_LOG_LOG(fmt, ...)
|
||||
#endif
|
||||
|
||||
#endif // !_SALOF_H_
|
||||
|
||||
14
sdk/lib/net/mqtt/kawaii/common/log/salof_config.h
Normal file
14
sdk/lib/net/mqtt/kawaii/common/log/salof_config.h
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2020-02-25 06:01:24
|
||||
* @LastEditTime: 2020-02-25 09:28:09
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
|
||||
#ifndef _SALOF_CONFIG_H_
|
||||
#define _SALOF_CONFIG_H_
|
||||
|
||||
#include "mqtt_config.h"
|
||||
|
||||
#endif /* _SALOF_CONFIG_H_ */
|
||||
154
sdk/lib/net/mqtt/kawaii/common/log/salof_defconfig.h
Normal file
154
sdk/lib/net/mqtt/kawaii/common/log/salof_defconfig.h
Normal file
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-25 23:56:34
|
||||
* @LastEditTime: 2020-06-17 18:50:26
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _SALOF_DEFCONFIG_H_
|
||||
#define _SALOF_DEFCONFIG_H_
|
||||
|
||||
#include "salof_config.h"
|
||||
|
||||
#ifdef SALOF_USING_LOG
|
||||
|
||||
#define SALOF_USING_RTT 1
|
||||
#define SALOF_USING_FREERTOS 2
|
||||
#define SALOF_USING_TENCENTOS 3
|
||||
#define SALOF_USING_LINUX 4
|
||||
|
||||
#define SALOF_BASE_LEVEL (0)
|
||||
#define SALOF_ERR_LEVEL (SALOF_BASE_LEVEL + 1)
|
||||
#define SALOF_WARN_LEVEL (SALOF_ERR_LEVEL + 1)
|
||||
#define SALOF_INFO_LEVEL (SALOF_WARN_LEVEL + 1)
|
||||
#define SALOF_DEBUG_LEVEL (SALOF_INFO_LEVEL + 1)
|
||||
|
||||
#ifndef SALOF_USING_SALOF
|
||||
#define SALOF_USING_SALOF (1U)
|
||||
#endif
|
||||
|
||||
#ifndef SALOF_USING_IDLE_HOOK
|
||||
#define SALOF_USING_IDLE_HOOK (0U)
|
||||
#endif
|
||||
|
||||
#ifndef SALOF_LOG_COLOR
|
||||
#define SALOF_LOG_COLOR (1U)
|
||||
#endif
|
||||
|
||||
#ifndef SALOF_LOG_TS
|
||||
#define SALOF_LOG_TS (1U)
|
||||
#endif
|
||||
|
||||
#ifndef SALOF_LOG_TAR
|
||||
#define SALOF_LOG_TAR (0U)
|
||||
#endif
|
||||
|
||||
#ifndef SALOF_LOG_LEVEL
|
||||
#define SALOF_LOG_LEVEL SALOF_DEBUG_LEVEL //SALOF_WARN_LEVEL SALOF_DEBUG_LEVEL
|
||||
#endif
|
||||
|
||||
#if SALOF_USING_SALOF
|
||||
|
||||
#ifndef SALOF_BUFF_SIZE
|
||||
#define SALOF_BUFF_SIZE (512U)
|
||||
#endif
|
||||
|
||||
#ifndef SALOF_FIFO_SIZE
|
||||
#define SALOF_FIFO_SIZE (1024*4U)
|
||||
#endif
|
||||
|
||||
#ifndef SALOF_TASK_STACK_SIZE
|
||||
#define SALOF_TASK_STACK_SIZE (2048U)
|
||||
#endif
|
||||
|
||||
#ifndef SALOF_TASK_TICK
|
||||
#define SALOF_TASK_TICK (20U)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#if !defined(SALOF_OS)
|
||||
#error "SALOF_OS isn't defined in 'salof_config.h'"
|
||||
#endif
|
||||
|
||||
#if (SALOF_OS == SALOF_USING_FREERTOS)
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "semphr.h"
|
||||
#define salof_mutex SemaphoreHandle_t
|
||||
#define salof_tcb TaskHandle_t
|
||||
#define salof_sem salof_mutex
|
||||
#if SALOF_USING_IDLE_HOOK
|
||||
#define salof_handler vApplicationIdleHook
|
||||
#endif
|
||||
#define SALOF_TASK_PRIO (0U)
|
||||
|
||||
#elif (SALOF_OS == SALOF_USING_TENCENTOS)
|
||||
#include "tos_k.h"
|
||||
#define salof_mutex k_mutex_t*
|
||||
#define salof_sem k_sem_t*
|
||||
#define salof_tcb k_task_t*
|
||||
#define SALOF_TASK_PRIO (TOS_CFG_TASK_PRIO_MAX - 2u)
|
||||
#undef SALOF_USING_IDLE_HOOK
|
||||
|
||||
#elif (SALOF_OS == SALOF_USING_RTT)
|
||||
#include <rtconfig.h>
|
||||
#include <rtthread.h>
|
||||
#include <rthw.h>
|
||||
#include <stdio.h>
|
||||
#define salof_mutex rt_mutex_t
|
||||
#define salof_sem rt_sem_t
|
||||
#define salof_tcb rt_thread_t
|
||||
#define SALOF_TASK_PRIO (RT_THREAD_PRIORITY_MAX - 1)
|
||||
|
||||
#elif (SALOF_OS == SALOF_USING_LINUX)
|
||||
#include "pthread.h"
|
||||
#include "memory.h"
|
||||
#include <semaphore.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#define salof_mutex pthread_mutex_t*
|
||||
#define salof_sem sem_t*
|
||||
#define salof_tcb pthread_t*
|
||||
#define SALOF_TASK_PRIO (0U)
|
||||
#undef SALOF_USING_IDLE_HOOK
|
||||
|
||||
#elif (SALOF_OS == SALOF_USING_TXW)
|
||||
#include "basic_include.h"
|
||||
#define salof_mutex struct os_mutex *
|
||||
#define salof_sem struct os_semaphore *
|
||||
#define salof_tcb struct os_task *
|
||||
#define SALOF_TASK_PRIO (OS_TASK_PRIORITY_NORMAL)
|
||||
#undef SALOF_USING_IDLE_HOOK
|
||||
|
||||
typedef struct os_mutex salof_mutex_t;
|
||||
typedef struct os_semaphore salof_sem_t;
|
||||
typedef struct os_task salof_tcb_t;
|
||||
#else
|
||||
#error "not supported OS type"
|
||||
#endif
|
||||
|
||||
void *salof_alloc(unsigned int size);
|
||||
void salof_free(void *mem);
|
||||
salof_tcb salof_task_create(const char *name,
|
||||
void (*task_entry)(void *param),
|
||||
void * const param,
|
||||
unsigned int stack_size,
|
||||
unsigned int priority,
|
||||
unsigned int tick);
|
||||
salof_mutex salof_mutex_create(void);
|
||||
void salof_mutex_delete(salof_mutex mutex);
|
||||
int salof_mutex_pend(salof_mutex mutex, unsigned int timeout);
|
||||
int salof_mutex_post(salof_mutex mutex);
|
||||
salof_sem salof_sem_create(void);
|
||||
void salof_sem_delete(salof_sem sem);
|
||||
int salof_sem_pend(salof_sem sem, unsigned int timeout);
|
||||
int salof_sem_post(salof_sem sem);
|
||||
unsigned int salof_get_tick(void);
|
||||
char *salof_get_task_name(void);
|
||||
extern int send_buff(char *buf, int len);
|
||||
|
||||
#endif
|
||||
|
||||
#endif // !_SALOF_DEFCONFIG_H_
|
||||
|
||||
88
sdk/lib/net/mqtt/kawaii/common/mqtt_defconfig.h
Normal file
88
sdk/lib/net/mqtt/kawaii/common/mqtt_defconfig.h
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* @Author : jiejie
|
||||
* @GitHub : https://github.com/jiejieTop
|
||||
* @Date : 2021-02-26 12:00:24
|
||||
* @LastEditors : jiejie
|
||||
* @LastEditTime : 2022-06-15 23:22:10
|
||||
* @FilePath : /mqttclient/mqttclient/mqtt_defconfig.h
|
||||
* Copyright (c) 2022 jiejie, All Rights Reserved. Please keep the author information and source code according to the license.
|
||||
*/
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2020-02-25 03:36:09
|
||||
* @LastEditTime: 2020-06-17 19:59:41
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
|
||||
#ifndef _DEFCONFIG_H_
|
||||
#define _DEFCONFIG_H_
|
||||
|
||||
#include "mqtt_config.h"
|
||||
|
||||
#ifndef MQTT_LOG_LEVEL
|
||||
#define MQTT_LOG_LEVEL MQTT_LOG_DEBUG_LEVEL //MQTT_LOG_WARN_LEVEL MQTT_LOG_DEBUG_LEVEL
|
||||
#endif // !MQTT_LOG_LEVEL
|
||||
|
||||
#ifndef MQTT_MAX_PACKET_ID
|
||||
#define MQTT_MAX_PACKET_ID (0xFFFF - 1)
|
||||
#endif // !MQTT_MAX_PACKET_ID
|
||||
|
||||
#ifndef MQTT_TOPIC_LEN_MAX
|
||||
#define MQTT_TOPIC_LEN_MAX 64
|
||||
#endif // !MQTT_TOPIC_LEN_MAX
|
||||
|
||||
#ifndef MQTT_ACK_HANDLER_NUM_MAX
|
||||
#define MQTT_ACK_HANDLER_NUM_MAX 64
|
||||
#endif // !MQTT_ACK_HANDLER_NUM_MAX
|
||||
|
||||
#ifndef MQTT_DEFAULT_BUF_SIZE
|
||||
#define MQTT_DEFAULT_BUF_SIZE 1024
|
||||
#endif // !MQTT_DEFAULT_BUF_SIZE
|
||||
|
||||
#ifndef MQTT_DEFAULT_CMD_TIMEOUT
|
||||
#define MQTT_DEFAULT_CMD_TIMEOUT 4000
|
||||
#endif // !MQTT_DEFAULT_CMD_TIMEOUT
|
||||
|
||||
#ifndef MQTT_MAX_CMD_TIMEOUT
|
||||
#define MQTT_MAX_CMD_TIMEOUT 20000
|
||||
#endif // !MQTT_MAX_CMD_TIMEOUT
|
||||
|
||||
#ifndef MQTT_MIN_CMD_TIMEOUT
|
||||
#define MQTT_MIN_CMD_TIMEOUT 1000
|
||||
#endif // !MQTT_MIN_CMD_TIMEOUT
|
||||
|
||||
#ifndef MQTT_KEEP_ALIVE_INTERVAL
|
||||
#define MQTT_KEEP_ALIVE_INTERVAL 100 // unit: second
|
||||
#endif // !MQTT_KEEP_ALIVE_INTERVAL
|
||||
|
||||
#ifndef MQTT_VERSION
|
||||
#define MQTT_VERSION 4 // 4 is mqtt 3.1.1
|
||||
#endif // !MQTT_VERSION
|
||||
|
||||
#ifndef MQTT_RECONNECT_DEFAULT_DURATION
|
||||
#define MQTT_RECONNECT_DEFAULT_DURATION 1000
|
||||
#endif // !MQTT_RECONNECT_DEFAULT_DURATION
|
||||
|
||||
#ifndef MQTT_THREAD_STACK_SIZE
|
||||
#define MQTT_THREAD_STACK_SIZE 4096
|
||||
#endif // !MQTT_THREAD_STACK_SIZE
|
||||
|
||||
#ifndef MQTT_THREAD_PRIO
|
||||
#define MQTT_THREAD_PRIO 5
|
||||
#endif // !MQTT_THREAD_PRIO
|
||||
|
||||
#ifndef MQTT_THREAD_TICK
|
||||
#define MQTT_THREAD_TICK 50
|
||||
#endif // !MQTT_THREAD_TICK
|
||||
|
||||
|
||||
#ifndef MQTT_NETWORK_TYPE_NO_TLS
|
||||
|
||||
#ifndef MQTT_TLS_HANDSHAKE_TIMEOUT
|
||||
#define MQTT_TLS_HANDSHAKE_TIMEOUT (5 * 1000)
|
||||
#endif // !MQTT_TLS_HANDSHAKE_TIMEOUT
|
||||
|
||||
#endif /* MQTT_NETWORK_TYPE_NO_TLS */
|
||||
|
||||
#endif /* _DEFCONFIG_H_ */
|
||||
53
sdk/lib/net/mqtt/kawaii/common/mqtt_error.h
Normal file
53
sdk/lib/net/mqtt/kawaii/common/mqtt_error.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-15 00:42:16
|
||||
* @LastEditTime: 2020-10-17 14:16:15
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _MQTT_ERROR_H_
|
||||
#define _MQTT_ERROR_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum mqtt_error {
|
||||
MQTT_SSL_CERT_ERROR = -0x001C, /* cetr parse failed */
|
||||
MQTT_SOCKET_FAILED_ERROR = -0x001B, /* socket fd failed */
|
||||
MQTT_SOCKET_UNKNOWN_HOST_ERROR = -0x001A, /* socket unknown host ip or domain */
|
||||
MQTT_SET_PUBLISH_DUP_FAILED_ERROR = -0x0019, /* mqtt publish packet set udp bit failed */
|
||||
MQTT_CLEAN_SESSION_ERROR = -0x0018, /* mqtt clean session error */
|
||||
MQTT_ACK_NODE_IS_EXIST_ERROR = -0x0017, /* mqtt ack list is exist ack node */
|
||||
MQTT_ACK_HANDLER_NUM_TOO_MUCH_ERROR = -0x0016, /* mqtt ack handler number is too much */
|
||||
MQTT_RESUBSCRIBE_ERROR = -0x0015, /* mqtt resubscribe error */
|
||||
MQTT_SUBSCRIBE_ERROR = -0x0014, /* mqtt subscribe error */
|
||||
MQTT_SEND_PACKET_ERROR = -0x0013, /* mqtt send a packet */
|
||||
MQTT_SERIALIZE_PUBLISH_ACK_PACKET_ERROR = -0x0012, /* mqtt serialize publish ack packet error */
|
||||
MQTT_PUBLISH_PACKET_ERROR = -0x0011, /* mqtt publish packet error */
|
||||
MQTT_RECONNECT_TIMEOUT_ERROR = -0x0010, /* mqtt try reconnect, but timeout */
|
||||
MQTT_SUBSCRIBE_NOT_ACK_ERROR = -0x000F, /* mqtt subscribe, but not ack */
|
||||
MQTT_NOT_CONNECT_ERROR = -0x000E, /* mqtt not connect */
|
||||
MQTT_SUBSCRIBE_ACK_PACKET_ERROR = -0x000D, /* mqtt subscribe, but ack packet error */
|
||||
MQTT_UNSUBSCRIBE_ACK_PACKET_ERROR = -0x000C, /* mqtt unsubscribe, but ack packet error */
|
||||
MQTT_PUBLISH_ACK_PACKET_ERROR = -0x000B, /* mqtt pubilsh ack packet error */
|
||||
MQTT_PUBLISH_ACK_TYPE_ERROR = -0x000A, /* mqtt pubilsh ack type error */
|
||||
MQTT_PUBREC_PACKET_ERROR = -0x0009, /* mqtt pubrec packet error */
|
||||
MQTT_BUFFER_TOO_SHORT_ERROR = -0x0008, /* mqtt buffer too short */
|
||||
MQTT_NOTHING_TO_READ_ERROR = -0x0007, /* mqtt nothing to read */
|
||||
MQTT_SUBSCRIBE_QOS_ERROR = -0x0006, /* mqtt subsrcibe qos error */
|
||||
MQTT_BUFFER_OVERFLOW_ERROR = -0x0005, /* mqtt buffer overflow */
|
||||
MQTT_CONNECT_FAILED_ERROR = -0x0004, /* mqtt connect failed */
|
||||
MQTT_MEM_NOT_ENOUGH_ERROR = -0x0003, /* mqtt memory not enough */
|
||||
MQTT_NULL_VALUE_ERROR = -0x0002, /* mqtt value is null */
|
||||
MQTT_FAILED_ERROR = -0x0001, /* failed */
|
||||
MQTT_SUCCESS_ERROR = 0x0000 /* success */
|
||||
} mqtt_error_t;
|
||||
|
||||
#define RETURN_ERROR(x) { return x; }
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
77
sdk/lib/net/mqtt/kawaii/common/mqtt_list.c
Normal file
77
sdk/lib/net/mqtt/kawaii/common/mqtt_list.c
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-11 22:46:33
|
||||
* @LastEditTime: 2020-04-27 23:28:12
|
||||
* @Description: the following code references TencentOS tiny, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#include <stddef.h>
|
||||
#include "mqtt_list.h"
|
||||
|
||||
|
||||
static void _mqtt_list_add(mqtt_list_t *node, mqtt_list_t *prev, mqtt_list_t *next)
|
||||
{
|
||||
next->prev = node;
|
||||
node->next = next;
|
||||
node->prev = prev;
|
||||
prev->next = node;
|
||||
}
|
||||
|
||||
static void _mqtt_list_del(mqtt_list_t *prev, mqtt_list_t *next)
|
||||
{
|
||||
next->prev = prev;
|
||||
prev->next = next;
|
||||
}
|
||||
|
||||
static void _mqtt_list_del_entry(mqtt_list_t *entry)
|
||||
{
|
||||
_mqtt_list_del(entry->prev, entry->next);
|
||||
}
|
||||
|
||||
void mqtt_list_init(mqtt_list_t *list)
|
||||
{
|
||||
list->next = list;
|
||||
list->prev = list;
|
||||
}
|
||||
|
||||
void mqtt_list_add(mqtt_list_t *node, mqtt_list_t *list)
|
||||
{
|
||||
_mqtt_list_add(node, list, list->next);
|
||||
}
|
||||
|
||||
void mqtt_list_add_tail(mqtt_list_t *node, mqtt_list_t *list)
|
||||
{
|
||||
_mqtt_list_add(node, list->prev, list);
|
||||
}
|
||||
|
||||
void mqtt_list_del(mqtt_list_t* entry)
|
||||
{
|
||||
if (entry == NULL) return;
|
||||
if (entry->prev == NULL || entry->next == NULL) return;
|
||||
_mqtt_list_del(entry->prev, entry->next);
|
||||
entry->prev = NULL;
|
||||
entry->next = NULL;
|
||||
}
|
||||
|
||||
void mqtt_list_del_init(mqtt_list_t *entry)
|
||||
{
|
||||
_mqtt_list_del_entry(entry);
|
||||
mqtt_list_init(entry);
|
||||
}
|
||||
|
||||
void mqtt_list_move(mqtt_list_t *node, mqtt_list_t *list)
|
||||
{
|
||||
_mqtt_list_del_entry(node);
|
||||
mqtt_list_add(node, list);
|
||||
}
|
||||
|
||||
void mqtt_list_move_tail(mqtt_list_t *node, mqtt_list_t *list)
|
||||
{
|
||||
_mqtt_list_del_entry(node);
|
||||
mqtt_list_add_tail(node, list);
|
||||
}
|
||||
|
||||
int mqtt_list_is_empty(mqtt_list_t *list)
|
||||
{
|
||||
return list->next == list;
|
||||
}
|
||||
70
sdk/lib/net/mqtt/kawaii/common/mqtt_list.h
Normal file
70
sdk/lib/net/mqtt/kawaii/common/mqtt_list.h
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-11 22:47:55
|
||||
* @LastEditTime: 2020-10-17 14:18:02
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _MQTT_LIST_H_
|
||||
#define _MQTT_LIST_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct mqtt_list_node {
|
||||
struct mqtt_list_node *next;
|
||||
struct mqtt_list_node *prev;
|
||||
} mqtt_list_t;
|
||||
|
||||
#define OFFSET_OF_FIELD(type, field) \
|
||||
((size_t)&(((type *)0)->field))
|
||||
|
||||
#define CONTAINER_OF_FIELD(ptr, type, field) \
|
||||
((type *)((unsigned char *)(ptr) - OFFSET_OF_FIELD(type, field)))
|
||||
|
||||
#define LIST_NODE(node) \
|
||||
{ &(node), &(node) }
|
||||
|
||||
#define LIST_DEFINE(list) \
|
||||
mqtt_list_t list = { &(list), &(list) }
|
||||
|
||||
#define LIST_ENTRY(list, type, field) \
|
||||
CONTAINER_OF_FIELD(list, type, field)
|
||||
|
||||
#define LIST_FIRST_ENTRY(list, type, field) \
|
||||
LIST_ENTRY((list)->next, type, field)
|
||||
|
||||
#define LIST_FIRST_ENTRY_OR_NULL(list, type, field) \
|
||||
(mqtt_list_is_empty(list) ? NULL : LIST_FIRST_ENTRY(list, type, field))
|
||||
|
||||
#define LIST_FOR_EACH(curr, list) \
|
||||
for (curr = (list)->next; curr != (list); curr = curr->next)
|
||||
|
||||
#define LIST_FOR_EACH_PREV(curr, list) \
|
||||
for (curr = (list)->prev; curr != (list); curr = curr->prev)
|
||||
|
||||
#define LIST_FOR_EACH_SAFE(curr, next, list) \
|
||||
for (curr = (list)->next, next = curr->next; curr != (list); \
|
||||
curr = next, next = curr->next)
|
||||
|
||||
#define LIST_FOR_EACH_PREV_SAFE(curr, next, list) \
|
||||
for (curr = (list)->prev, next = curr->prev; \
|
||||
curr != (list); \
|
||||
curr = next, next = curr->prev)
|
||||
|
||||
void mqtt_list_init(mqtt_list_t *list);
|
||||
void mqtt_list_add(mqtt_list_t *node, mqtt_list_t *list);
|
||||
void mqtt_list_add_tail(mqtt_list_t *node, mqtt_list_t *list);
|
||||
void mqtt_list_del(mqtt_list_t *entry);
|
||||
void mqtt_list_del_init(mqtt_list_t *entry);
|
||||
void mqtt_list_move(mqtt_list_t *node, mqtt_list_t *list);
|
||||
void mqtt_list_move_tail(mqtt_list_t *node, mqtt_list_t *list);
|
||||
int mqtt_list_is_empty(mqtt_list_t *list);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _LIST_H_ */
|
||||
|
||||
71
sdk/lib/net/mqtt/kawaii/common/mqtt_log.h
Normal file
71
sdk/lib/net/mqtt/kawaii/common/mqtt_log.h
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-27 03:25:58
|
||||
* @LastEditTime: 2020-10-17 14:15:55
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _MQTT_LOG_H_
|
||||
#define _MQTT_LOG_H_
|
||||
|
||||
#include "mqtt_defconfig.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define MQTT_LOG_BASE_LEVEL (0)
|
||||
#define MQTT_LOG_ERR_LEVEL (MQTT_LOG_BASE_LEVEL + 1)
|
||||
#define MQTT_LOG_WARN_LEVEL (MQTT_LOG_ERR_LEVEL + 1)
|
||||
#define MQTT_LOG_INFO_LEVEL (MQTT_LOG_WARN_LEVEL + 1)
|
||||
#define MQTT_LOG_DEBUG_LEVEL (MQTT_LOG_INFO_LEVEL + 1)
|
||||
|
||||
#ifdef MQTT_LOG_IS_SALOF
|
||||
#include "salof.h"
|
||||
|
||||
#define MQTT_LOG_D(fmt, ...) SALOF_LOG_DEBUG(fmt, ##__VA_ARGS__)
|
||||
#define MQTT_LOG_I(fmt, ...) SALOF_LOG_INFO(fmt, ##__VA_ARGS__)
|
||||
#define MQTT_LOG_W(fmt, ...) SALOF_LOG_WARN(fmt, ##__VA_ARGS__)
|
||||
#define MQTT_LOG_E(fmt, ...) SALOF_LOG_ERR(fmt, ##__VA_ARGS__)
|
||||
#define mqtt_log_init salof_init
|
||||
#else
|
||||
#include <stdio.h>
|
||||
|
||||
#if MQTT_LOG_LEVEL < MQTT_LOG_DEBUG_LEVEL
|
||||
#define MQTT_LOG_D(fmt, ...)
|
||||
#else
|
||||
#define MQTT_LOG_D(fmt, ...) { printf(fmt, ##__VA_ARGS__); printf("\n");}
|
||||
#endif
|
||||
|
||||
#if MQTT_LOG_LEVEL < MQTT_LOG_INFO_LEVEL
|
||||
#define MQTT_LOG_I(fmt, ...)
|
||||
#else
|
||||
#define MQTT_LOG_I(fmt, ...) { printf(fmt, ##__VA_ARGS__); printf("\n");}
|
||||
#endif
|
||||
|
||||
#if MQTT_LOG_LEVEL < MQTT_LOG_WARN_LEVEL
|
||||
#define MQTT_LOG_W(fmt, ...)
|
||||
#else
|
||||
#define MQTT_LOG_W(fmt, ...) { printf(fmt, ##__VA_ARGS__); printf("\n");}
|
||||
#endif
|
||||
|
||||
#if MQTT_LOG_LEVEL < MQTT_LOG_ERR_LEVEL
|
||||
#define MQTT_LOG_E(fmt, ...)
|
||||
#else
|
||||
#define MQTT_LOG_E(fmt, ...) { printf(fmt, ##__VA_ARGS__); printf("\n");}
|
||||
#endif
|
||||
|
||||
#if MQTT_LOG_LEVEL < MQTT_LOG_BASE_LEVEL
|
||||
#define MQTT_LOG(fmt, ...)
|
||||
#else
|
||||
#define MQTT_LOG(fmt, ...) { printf(fmt, ##__VA_ARGS__); printf("\n");}
|
||||
#endif
|
||||
|
||||
#define mqtt_log_init()
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _LOG_H_ */
|
||||
77
sdk/lib/net/mqtt/kawaii/common/random.c
Normal file
77
sdk/lib/net/mqtt/kawaii/common/random.c
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* @Author : jiejie
|
||||
* @GitHub : https://github.com/jiejieTop
|
||||
* @Date : 2021-02-26 12:00:24
|
||||
* @LastEditors : jiejie
|
||||
* @LastEditTime : 2022-06-15 19:44:22
|
||||
* @FilePath : /mqttclient/common/random.c
|
||||
* Copyright (c) 2022 jiejie, All Rights Reserved. Please keep the author information and source code according to the license.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "random.h"
|
||||
|
||||
extern int platform_timer_now();
|
||||
static unsigned int last_seed = 1;
|
||||
|
||||
|
||||
int __attribute__((weak)) platform_timer_now()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int do_random(unsigned int seed)
|
||||
{
|
||||
srand(seed);
|
||||
return rand();
|
||||
}
|
||||
|
||||
int random_number(void)
|
||||
{
|
||||
unsigned int seed = (unsigned int) platform_timer_now();
|
||||
last_seed += (seed >> ((seed ^ last_seed) % 3));
|
||||
return do_random(last_seed ^ seed);
|
||||
}
|
||||
|
||||
// random number range interval [min, max)
|
||||
int random_number_range(unsigned int min, unsigned int max)
|
||||
{
|
||||
return (random_number() % (max - min)) + min;
|
||||
}
|
||||
|
||||
int random_string(char *buffer, int len)
|
||||
{
|
||||
unsigned int i, flag, seed, random;
|
||||
|
||||
if (NULL == buffer)
|
||||
return 0;
|
||||
|
||||
seed = (unsigned int) random_number();
|
||||
seed += (unsigned int) ((size_t)buffer ^ seed);
|
||||
|
||||
random = (unsigned int)do_random(seed);
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
random = do_random(seed ^ random);
|
||||
flag = (unsigned int)random % 3;
|
||||
switch (flag) {
|
||||
case 0:
|
||||
buffer[i] = 'A' + do_random(random ^ (i & flag)) % 26;
|
||||
break;
|
||||
case 1:
|
||||
buffer[i] = 'a' + do_random(random ^ (i & flag)) % 26;
|
||||
break;
|
||||
case 2:
|
||||
buffer[i] = '0' + do_random(random ^ (i & flag)) % 10;
|
||||
break;
|
||||
default:
|
||||
buffer[i] = 'x';
|
||||
break;
|
||||
}
|
||||
random += ((0xb433e5c6 ^ random) << (i & flag));
|
||||
}
|
||||
|
||||
buffer[len] = '\0';
|
||||
return len;
|
||||
}
|
||||
|
||||
29
sdk/lib/net/mqtt/kawaii/common/random.h
Normal file
29
sdk/lib/net/mqtt/kawaii/common/random.h
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* @Author : jiejie
|
||||
* @GitHub : https://github.com/jiejieTop
|
||||
* @Date : 2021-02-26 12:00:24
|
||||
* @LastEditors : jiejie
|
||||
* @LastEditTime : 2022-06-15 19:40:10
|
||||
* @FilePath : /mqttclient/common/random.h
|
||||
* Copyright (c) 2022 jiejie, All Rights Reserved. Please keep the author information and source code according to the license.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _RANDOM_H_
|
||||
#define _RANDOM_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define RANDOM_MAX 0x7FFFFFFF
|
||||
|
||||
int random_number(void);
|
||||
int random_number_range(unsigned int min, unsigned int max);
|
||||
int random_string(char *buffer, int len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _RANDOM_H_ */
|
||||
Reference in New Issue
Block a user