diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 0000000..ac0de08 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,86 @@ +# 变量BIN: 给定的是我们想要生成的可执行文件的名称 +BIN = run.exe +SO = lib.dll + +# 变量SRC中给的是所有的想要编译的.c源文件,与makefile在同一目录下可直接写(如这里的main.c),否则需要写明相对路径(如这里的其余源文件都在目录src下)。 +# 多文件时,选择用"\"进行分行处理 +SRC = \ + ../src/malloc.c \ + ../src/sqqueue.c \ + ../src/mlist.c \ + ../src/debug.c \ + ../src/data_analysis.c \ + ../src/filter.c \ + ../src/clist.c \ + ../src/aes.c \ + ../src/cmac.c \ + ../src/lib.c + +EXAMPLE = \ + ./simple_clist.c \ + ./simple_data_analysis.c \ + ./simple_sqqueue.c \ + ./simple_aes.c \ + ./simple_cmac.c + +CPLUS_INCLUDE_PATH= -I ../inc + +# 变量CC:给定编译器名gcc +# 变量CFLAGS:传给编译器的某些编译参数,看需求添加 +CC = gcc +CFLAGS = -m32 -std=c99 +# 变量GDB:给定debugger名gdb +# 变量RM:给定删除文件方式,用于后面删除所有编译所得的.o文件,linux下使用rm -rf +GDB = gdb +RM = rm -rf +# 变量OBJS:将变量SRC中所有的.c文件替换成以.o结尾,即将.c源文件编译成.o文件 +OBJS = $(SRC:%.c=%.o) +EXAPMLES = $(EXAMPLE:%.c=%.o) + +$(SO): $(OBJS) $(EXAPMLES) + + +# pull in dependencies for .o files +-include $(OBJS:.o=.d) + +%.o: %.c + $(CC) $(CPLUS_INCLUDE_PATH) $(CFLAGS) -c $< -o $@ + +.PHONY: all clean clist data_analysis + +all: $(SO) + +rm: + $(RM) $(OBJS) + +#简单链表 +clist: $(SO) + $(CC) $(CPLUS_INCLUDE_PATH) $(CFLAGS) $(OBJS) ./simple_clist.o -o $(BIN) + $(RM) $(OBJS) $(EXAPMLES) + +#数据分析器 +data_analysis: $(SO) + $(CC) $(CPLUS_INCLUDE_PATH) $(CFLAGS) $(OBJS) ./simple_data_analysis.o -o $(BIN) + $(RM) $(OBJS) $(EXAPMLES) + +#队列 +sqqueue: $(SO) + $(CC) $(CPLUS_INCLUDE_PATH) $(CFLAGS) $(OBJS) ./simple_sqqueue.o -o $(BIN) + $(RM) $(OBJS) $(EXAPMLES) + +#aes加密 +aes: $(SO) + $(CC) $(CPLUS_INCLUDE_PATH) $(CFLAGS) $(OBJS) ./simple_aes.o -o $(BIN) + $(RM) $(OBJS) $(EXAPMLES) + +#cmac类CRC +cmac: $(SO) + $(CC) $(CPLUS_INCLUDE_PATH) $(CFLAGS) $(OBJS) ./simple_cmac.o -o $(BIN) + $(RM) $(OBJS) $(EXAPMLES) + +#运行程序 +run: + ./run.exe + +clean: + $(RM) $(OBJS) $(EXAPMLES) $(BIN) diff --git a/examples/simple_aes.c b/examples/simple_aes.c new file mode 100644 index 0000000..fb4fd5c --- /dev/null +++ b/examples/simple_aes.c @@ -0,0 +1,40 @@ +#include "../inc/data_type_def.h" +#include "../inc/log.h" +#include "../inc/osel_arch.h" +#include "../inc/aes.h" + +// 全局变量 +static aes_context AesContext; // 密钥表 +static uint8_t aBlock[] = {0x00, 0x00, 0x00, 0xcc, 0xff, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // 数据块 +static uint8_t sBlock[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // 存放输出结果 + +int32_t main(void) +{ + uint8_t buf[16] = {0x00}; + uint8_t size = ARRAY_LEN(buf); + uint8_t key[] = { + 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, + 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C}; // 密钥 + + // 初始化密文 + for (int i = 0; i < size; i++) + { + buf[i] = i; + } + + // 设置预密钥 + osel_memset(AesContext.ksch, 0, ARRAY_LEN(AesContext.ksch)); + aes_set_key(key, 16, &AesContext); + + // 加密 + osel_memcpy(aBlock, buf, size); + aes_encrypt(aBlock, sBlock, &AesContext); + LOG_HEX(sBlock, ARRAY_LEN(sBlock)); // 打印加密结果:50 fe 67 cc 99 6d 32 b6 da 09 37 e9 9b af ec 60 + + // 解密 + osel_memcpy(aBlock, sBlock, size); + aes_decrypt(aBlock, sBlock, &AesContext); + LOG_HEX(sBlock, ARRAY_LEN(sBlock)); // 打印解密结果:00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f +} diff --git a/examples/simple_clist.c b/examples/simple_clist.c new file mode 100644 index 0000000..3254b07 --- /dev/null +++ b/examples/simple_clist.c @@ -0,0 +1,52 @@ +#include "../inc/data_type_def.h" +#include "../inc/clist.h" + +int32_t main(void) +{ + clist_node_t *head = NULL; // 创建头指针,初始化为NULL + clist_init(&head); // 初始化指针(可有可无) + + // 1:添加数据 + for (int32_t i = 0; i < 30; i++) + { + if (i > 10) + clist_push_front(&head, (cnode)i); // 头部插入 + else + clist_push_back(&head, (cnode)i); // 尾部插入 + } + + LOG_PRINT("\n 1: count:%d \n", clist_node_count(head)); // 获取链表节点数,打印 + clist_print(head); // 打印链表 + + // 2:删除数据 + for (int32_t i = 0; i < 10; i++) + { + if (i > 5) + clist_pop_back(&head); // 删除尾部 + else + clist_pop_front(&head); // 头部删除 + } + LOG_PRINT("\n 2: count:%d \n", clist_node_count(head)); + clist_print(head); + + // 3:插入数据 + clist_insert(&head, 5, (cnode)1111); + clist_insert_for_node(&head, head->Next->Next->Next->Next->Next, (cnode)10000); + clist_insert(&head, 1000, (cnode)2222); // 无效插入 + LOG_PRINT("\n 3: count:%d \n", clist_node_count(head)); + clist_print(head); + + // 4:删除指定节点 + clist_remove(&head, (cnode)5); + clist_erase_for_node(&head, head->Next->Next); + clist_remove(&head, (cnode)1000); // 无效删除 + clist_print(head); + LOG_PRINT("\n 4: count:%d \n", clist_node_count(head)); + clist_print(head); + + // 5:删除所有节点 + clist_destroy(&head); + LOG_PRINT("\n 5: count:%d ", clist_node_count(head)); + clist_print(head); + return 0; +} diff --git a/examples/simple_cmac.c b/examples/simple_cmac.c new file mode 100644 index 0000000..a96726f --- /dev/null +++ b/examples/simple_cmac.c @@ -0,0 +1,33 @@ +#include "../inc/data_type_def.h" +#include "../inc/log.h" +#include "../inc/osel_arch.h" +#include "../inc/cmac.h" + +static uint8_t key[] = { + 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, + 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C}; // 密钥 +int32_t main(void) +{ + uint8_t *p; + uint8_t buffer[16] = {0x00}; + uint32_t size = ARRAY_LEN(buffer); + // 初始化需要校验的数据 + for (int i = 0; i < size; i++) + { + buffer[i] = i; + } + uint8_t mic[16]; // 存放生成校验数据的数组 + AES_CMAC_CTX AesCmacCtx[1]; // 密钥扩展表 + AES_CMAC_Init(AesCmacCtx); // 完成密钥扩展表的初始化 + + AES_CMAC_SetKey(AesCmacCtx, key); // 完成密钥扩展表数据 + + AES_CMAC_Update(AesCmacCtx, buffer, size & 0xFF); // 完成数据的奇偶校验 + + AES_CMAC_Final(mic, AesCmacCtx); // 生成16个字节的校验表 + + uint32_t xor_vol = (uint32_t)((uint32_t)mic[3] << 24 | (uint32_t)mic[2] << 16 | (uint32_t)mic[1] << 8 | (uint32_t)mic[0]); // 取表4个字节作为校验码 + + p = (uint8_t *)&xor_vol; + LOG_HEX(p, 4); // 打印结果:5c 7e fb 43 +} diff --git a/examples/simple_cmd.c b/examples/simple_cmd.c new file mode 100644 index 0000000..563e759 --- /dev/null +++ b/examples/simple_cmd.c @@ -0,0 +1,26 @@ +#include "../inc/data_type_def.h" +#include "../inc/log.h" +#include "../inc/cmd.h" + +void at_name_req(void) +{ + LOG_PRINT("name:cmd\n"); +} + +void at_version_req(void) +{ + LOG_PRINT("version:1.0\n"); +} + +REGISTER_CMD(NAME, at_name_req, at name); +REGISTER_CMD(VERSION, at_version_req, at version); + +int32_t main(void) +{ + cmd_init(); + + cmd_parsing("TEST"); + cmd_parsing("NAME"); + cmd_parsing("VERSION"); + return 0; +} diff --git a/examples/simple_data_analysis.c b/examples/simple_data_analysis.c new file mode 100644 index 0000000..41d38ad --- /dev/null +++ b/examples/simple_data_analysis.c @@ -0,0 +1,178 @@ +#include "../inc/data_type_def.h" +#include "../inc/log.h" +#include "../inc/osel_arch.h" +#include "../inc/data_analysis.h" +#define UART_RXSIZE (254U) +#define UART_DATA_ANALYSIS_PORT_1 DATA_1 +#define UART_DATA_ANALYSIS_PORT_2 DATA_2 + +static data_interupt_cb_t uart_data_analysis_cb = NULL; // 数据源中断回调函数 + +static void data_analysis_event1(void) +{ + uint8_t frame[UART_RXSIZE]; + uint8_t data_head[3]; + uint8_t crc[2]; + uint16_t frame_len, out_frame_len; + data_read(UART_DATA_ANALYSIS_PORT_1, &data_head[0], 3); + osel_memcpy((uint8_t *)&frame_len, &data_head[1], 2); + + frame_len = B2S_UINT16(frame_len) - 2; // 报文长度包含帧长,这里需要减2 + if (frame_len > UART_RXSIZE) + { + lock_data(UART_DATA_ANALYSIS_PORT_1); + unlock_data(UART_DATA_ANALYSIS_PORT_1); + return; + } + + out_frame_len = data_read(UART_DATA_ANALYSIS_PORT_1, frame, (uint16_t)frame_len); + if (out_frame_len != frame_len) + { + return; + } + out_frame_len = out_frame_len - 1; // 报文中包含帧尾,这里需要减1 + + // 校验CRC_16 + uint16_t crc_16 = 0; + uint16_t crc16 = crc16_compute(&frame[0], out_frame_len - 2); + osel_memcpy(&crc[0], &frame[out_frame_len - 2], 2); + crc_16 = BUILD_UINT16(crc[1], crc[0]); + if (crc16 != crc_16) + { + return; + } + // CRC校验通过后将数据长度-2 + out_frame_len -= 2; + + LOG_PRINT("data_analysis_event1 ok:"); + LOG_HEX(frame, out_frame_len); +} + +static void data_analysis_event2(void) +{ + uint8_t frame[UART_RXSIZE]; + uint8_t data_head[4]; + uint8_t crc[2]; + uint16_t frame_len, out_frame_len; + data_read(UART_DATA_ANALYSIS_PORT_2, &data_head[0], 4); + osel_memcpy((uint8_t *)&frame_len, &data_head[2], 2); + frame_len = B2S_UINT16(frame_len); + if (frame_len > UART_RXSIZE) + { + lock_data(UART_DATA_ANALYSIS_PORT_2); + unlock_data(UART_DATA_ANALYSIS_PORT_2); + return; + } + + out_frame_len = data_read(UART_DATA_ANALYSIS_PORT_2, frame, (uint16_t)frame_len); + if (out_frame_len != frame_len) + { + return; + } + + // 校验CRC_16 + uint16_t crc_16 = 0; + uint16_t crc16 = crc16_compute(&frame[0], out_frame_len - 2); + osel_memcpy(&crc[0], &frame[out_frame_len - 2], 2); + crc_16 = BUILD_UINT16(crc[1], crc[0]); + if (crc16 != crc_16) + { + LOG_PRINT("crc error crc16:%x, crc_16:%x\n"); + return; + } + + out_frame_len -= 2; // 去掉CRC_16 + + LOG_PRINT("data_analysis_event2 ok:"); + LOG_HEX(frame, out_frame_len); +} +/** + * @brief 需要识别帧头和帧尾的数据协议 + * @return {*} + * @note + */ +static void data_register1(void) +{ +/** + * 帧头 帧长度 源地址 目标地址 报文类型 报文体 校验 帧尾 + 1 2 2 2 1 n 2 1 +*/ +#define FRAME_HEAD 0x05 // 帧头 +#define FRAME_TAIL 0x1b // 帧尾 + + // 注册数据解析 + data_reg_t reg; + reg.sd.valid = true; // 数据头部验证有效标志位 + reg.sd.len = 1; // 数据头部长度 + reg.sd.pos = 0; // 数据头部偏移量 + reg.sd.data[0] = FRAME_HEAD; // 数据头部数据 + reg.ld.len = 2; // 数据长度 + reg.ld.pos = 2; // 报文长度包含帧长,这里需要设置偏移2 + reg.ld.valid = true; // 数据长度有效标志位 + reg.ld.little_endian = false; // 数据长度是否小端模式 + reg.argu.len_max = UART_RXSIZE; // 数据最大长度 + reg.argu.len_min = 2; // 数据最小长度 + reg.ed.valid = true; // 数据尾部有效标志位 + reg.ed.len = 1; // 数据尾部长度 + reg.ed.data[0] = FRAME_TAIL; // 数据尾部数据 + reg.echo_en = false; // 是否回显 + reg.func_ptr = data_analysis_event1; // 数据解析回调函数 data_analysis模块处理完数据后,会调用这个函数继续数据协议的处理 + uart_data_analysis_cb = data_fsm_init(UART_DATA_ANALYSIS_PORT_1); // 注册数据处理函数 data_analysis模块会调用这个函数,将数据写入到data_analysis模块 + data_reg(UART_DATA_ANALYSIS_PORT_1, reg); // 注册数据解析 +} + +/** + * @brief 需要识别帧头和没有帧尾的数据协议 + * @return {*} + * @note + */ +static void data_register2(void) +{ +/** + * 帧头 帧长度 源地址 目标地址 报文类型 报文体 校验 + 2 2 2 2 1 n 2 +*/ +#define FRAME_HEAD1 0xD5 // 帧头 +#define FRAME_HEAD2 0xC8 // 帧尾 + + // 注册数据解析 + data_reg_t reg; + reg.sd.valid = true; // 数据头部验证有效标志位 + reg.sd.len = 2; // 数据头部长度 + reg.sd.pos = 0; // 数据头部偏移量 + reg.sd.data[0] = FRAME_HEAD1; // 数据头部数据 + reg.sd.data[1] = FRAME_HEAD2; // 数据头部数据 + reg.ld.len = 2; // 数据长度 + reg.ld.pos = 2; // 报文长度包含帧长,这里需要设置偏移2 + reg.ld.valid = true; // 数据长度有效标志位 + reg.ld.little_endian = false; // 数据长度是否小端模式 + reg.argu.len_max = UART_RXSIZE; // 数据最大长度 + reg.argu.len_min = 2; // 数据最小长度 + reg.ed.valid = false; // 数据尾部有效标志位 + reg.echo_en = false; // 是否回显 + reg.func_ptr = data_analysis_event2; // 数据解析回调函数 data_analysis模块处理完数据后,会调用这个函数继续数据协议的处理 + uart_data_analysis_cb = data_fsm_init(UART_DATA_ANALYSIS_PORT_2); // 注册数据处理函数 data_analysis模块会调用这个函数,将数据写入到data_analysis模块 + data_reg(UART_DATA_ANALYSIS_PORT_2, reg); // 注册数据解析 +} + +int32_t main(void) +{ + data_register1(); + data_register2(); + + // 模拟串口数据 + uint8_t data1[] = {0x05, 0x00, 0x0a, 0xff, 0xff, 0x00, 0x01, 0x00, 0x55, 0x40, 0x1b}; + for (uint16_t i = 0; i < ARRAY_LEN(data1); i++) + { + uart_data_analysis_cb(UART_DATA_ANALYSIS_PORT_1, *(data1 + i)); + } + + // 模拟串口数据 + uint8_t data2[] = {0xD5, 0xC8, 0x00, 0x07, 0xff, 0xff, 0x00, 0x01, 0x00, 0x55, 0x40}; + for (uint16_t i = 0; i < ARRAY_LEN(data2); i++) + { + uart_data_analysis_cb(UART_DATA_ANALYSIS_PORT_2, *(data2 + i)); + } + + return 0; +} diff --git a/examples/simple_sqqueue.c b/examples/simple_sqqueue.c new file mode 100644 index 0000000..3ed8c91 --- /dev/null +++ b/examples/simple_sqqueue.c @@ -0,0 +1,53 @@ +#include "../inc/data_type_def.h" +#include "../inc/log.h" +#include "../inc/osel_arch.h" +#include "../inc/sqqueue.h" + +typedef struct +{ + uint8_t x; + uint8_t y; +} element_t; + +sqqueue_ctrl_t queue; // 创建队列对象 + +void traverse_cb(const void *e) +{ + element_t *p = (element_t *)e; + LOG_PRINT("x = %d, y = %d", p->x, p->y); +} + +int32_t main(void) +{ + int size = 10; + // 初始化队列 + if (FALSE == sqqueue_ctrl_init(&queue, sizeof(element_t), size)) + { + LOG_ERR("queue init failed!"); + return -1; // 创建失败 + } + + // 添加测试元素 + for (int i = 1; i <= 10; i++) + { + element_t element; + element.x = i * 10; + element.y = i * 10; + queue.enter(&queue, &element); // 将成员插入到队列中 + } + LOG_PRINT("add queue len = %d", queue.get_len(&queue)); // 获取队列长度 + + queue.del(&queue); // 移除首元素 + LOG_PRINT("del queue len = %d", queue.get_len(&queue)); // 获取队列长度 + queue.revoke(&queue); // 移除尾元素 + LOG_PRINT("revoke queue len = %d", queue.get_len(&queue)); // 获取队列长度 + queue.remove(&queue, 3); // 删除相对队头指定偏移位置的元素 + LOG_PRINT("remove queue len = %d", queue.get_len(&queue)); // 获取队列长度 + + LOG_PRINT("queue traverse:"); + queue.traverse(&queue, traverse_cb); // 遍历队列 + + queue.clear_sqq(&queue); // 清空队列 + LOG_PRINT("clear queue len = %d", queue.get_len(&queue)); // 获取队列长度 + return 0; +}