Yun_Lib_Share преди 1 година
родител
ревизия
3a6ab34a47
променени са 5 файла, в които са добавени 451 реда и са изтрити 0 реда
  1. 48 0
      inc/zf_assert.h
  2. 110 0
      inc/zf_buffer.h
  3. 117 0
      inc/zf_event.h
  4. 138 0
      inc/zf_fsm.h
  5. 38 0
      inc/zf_includes.h

+ 48 - 0
inc/zf_assert.h

@@ -0,0 +1,48 @@
+/**
+  *****************************************************************************
+  * @file    zf_assert.h
+  * @author  Zorb
+  * @version V1.0.0
+  * @date    2018-06-28
+  * @brief   断言的头文件
+  *****************************************************************************
+  * @history
+  *
+  * 1. Date:2018-06-28
+  *    Author:Zorb
+  *    Modification:建立文件
+  *
+  *****************************************************************************
+  */
+
+#ifndef __ZF_ASSERT_H__
+#define __ZF_ASSERT_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "stdint.h"
+
+#define _ZF_ASSERT              /* 定义断言功能 */
+#define ZF_ASSERT_ON true       /* 启用断言功能 */
+
+#ifdef _ZF_ASSERT
+    #if ZF_ASSERT_ON
+         #define ZF_ASSERT(expression_) ((expression_) ?\
+            (void)0 : ZF_assertHandle((uint8_t *)__FILE__, (int)__LINE__));
+    #else
+         #define ZF_ASSERT(expression_)
+    #endif /* ZF_ASSERT_ON */
+#endif /* _ZF_ASSERT */
+
+/* 断言产生时的处理 */
+void ZF_assertHandle(uint8_t *pFileName, int line);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ZF_ASSERT_H__ */
+
+/******************************** END OF FILE ********************************/

+ 110 - 0
inc/zf_buffer.h

@@ -0,0 +1,110 @@
+/**
+  *****************************************************************************
+  * @file    zf_buffer.h
+  * @author  Zorb
+  * @version V1.0.0
+  * @date    2018-06-28
+  * @brief   环形缓冲器的头文件
+  *****************************************************************************
+  * @history
+  *
+  * 1. Date:2018-06-28
+  *    Author:Zorb
+  *    Modification:建立文件
+  *
+  *****************************************************************************
+  */
+
+#ifndef __ZF_BUFFER_H__
+#define __ZF_BUFFER_H__
+
+#ifdef __cplusplus 
+extern "C" {
+#endif
+
+#include "stdbool.h"
+#include "stdint.h"
+
+/* 环形缓冲区数据结构 */
+typedef struct _RingBuffer
+{
+    bool IsExternBuffer;  /* 是否外部缓冲区,是则销毁时不释放 */
+    uint8_t *pBuf;        /* 缓冲区指针 */
+    uint32_t Head;        /* 缓冲区头地址 */
+    uint32_t Trail;       /* 缓冲区尾地址 */
+    uint32_t Size;        /* 缓冲区大小 */
+    uint32_t Count;       /* 数据字节数 */
+    
+    /* 缓冲器是否已满 */
+    bool (*IsFull)(struct _RingBuffer * const pRb);
+    
+    /* 缓冲器是否空 */
+    bool (*IsEmpty)(struct _RingBuffer * const pRb);
+    
+    /* 压入一个字节 */
+    bool (*SaveByte)(struct _RingBuffer * const pRb, uint8_t byte);
+    
+    /* 压入n个字节的数据 */
+    uint32_t (*SaveRange)(struct _RingBuffer * const pRb, uint8_t *pArray,
+        uint32_t n);
+    
+    /* 取出一个字节 */
+    bool (*GetByte)(struct _RingBuffer * const pRb, uint8_t *pByte);
+    
+    /* 读取缓冲器已使用字节个数 */
+    uint32_t (*GetCount)(struct _RingBuffer * const pRb);
+    
+    /* 读取n个字节(n超过最大数据数时全部读出) */
+    uint32_t (*ReadBytes)(struct _RingBuffer * const pRb, uint8_t *pArray,
+        uint32_t n);
+    
+    /* 丢弃n个字节(n超过最大数据数时全部丢弃) */
+    uint32_t (*DropBytes)(struct _RingBuffer * const pRb, uint32_t n);
+    
+    /* 清空缓冲器 */
+    bool (*Clear)(struct _RingBuffer * const pRb);
+    
+    /* 释放缓冲器(不释放外部创建的缓冲区) */
+    bool (*Dispose)(struct _RingBuffer * const pRb);
+} RingBuffer;
+
+/* 创建缓冲器(内部分配空间,size=0表示使用外部数据) */
+bool RB_create(RingBuffer **ppRb, uint32_t size);
+
+/* 缓冲器是否已满 */
+bool RB_isFull(RingBuffer * const pRb);
+
+/* 缓冲器是否空 */
+bool RB_isEmpty(RingBuffer * const pRb);
+
+/* 压入一个字节 */
+bool RB_saveByte(RingBuffer * const pRb, uint8_t byte);
+
+/* 压入n个字节的数据 */
+uint32_t RB_saveRange(RingBuffer * const pRb, uint8_t *pArray, uint32_t n);
+
+/* 取出一个字节 */
+bool RB_getByte(RingBuffer * const pRb, uint8_t *pByte);
+
+/* 读取缓冲器已使用字节个数 */
+uint32_t RB_getCount(RingBuffer * const pRb);
+
+/* 读取n个字节(n超过最大数据数时全部读出) */
+uint32_t RB_readBytes(RingBuffer * const pRb, uint8_t *pArray, uint32_t n);
+
+/* 丢弃n个字节(n超过最大数据数时全部丢弃) */
+uint32_t RB_dropBytes(RingBuffer * const pRb, uint32_t n);
+
+/* 清空缓冲器 */
+bool RB_clear(RingBuffer * const pRb);
+
+/* 释放缓冲器(不释放外部创建的缓冲区) */
+bool RB_dispose(RingBuffer * const pRb);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ZF_BUFFER_H__ */
+
+/******************************** END OF FILE ********************************/

+ 117 - 0
inc/zf_event.h

@@ -0,0 +1,117 @@
+/**
+  *****************************************************************************
+  * @file    zf_event.h
+  * @author  Zorb
+  * @version V1.0.0
+  * @date    2018-06-28
+  * @brief   事件和事件处理器的头文件
+  *****************************************************************************
+  * @history
+  *
+  * 1. Date:2018-06-28
+  *    Author:Zorb
+  *    Modification:建立文件
+  *
+  *****************************************************************************
+  */
+
+#ifndef __ZF_EVENT_H__
+#define __ZF_EVENT_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "stdbool.h"
+#include "stdint.h"
+#include "zf_list.h"
+
+#define EVENT_HIGHEST_PRIORITY 0  /* 事件最高优先级(用户不可用) */
+#define EVENT_LOWEST_PRIORITY 32  /* 事件最低优先级(用户不可用) */
+
+/* 推送事件 */
+#define EVENT_POST(handler_, event_) handler_->Add(handler_, event_)
+
+/* 事件 */
+typedef struct _Event Event;
+/* 事件处理器 */
+typedef struct _EventHandler EventHandler;
+/* 事件处理程序类型 */
+typedef void (*IEventProcess)(List *pArgList);
+
+/* 事件结构 */
+struct _Event
+{
+    uint8_t Priority;               /* 优先级 */
+    IEventProcess EventProcess;     /* 事件程序 */
+    List *pArgList;                 /* 事件程序的参数指针 */
+    
+    /* 增加程序参数(深拷贝,按先后顺序入队列) */
+    bool (*AddArg)(Event * const pEvent, void *pArg, uint32_t size);
+    
+    /* 销毁事件 */
+    bool (*Dispose)(Event * const pEvent);
+};
+
+/* 事件处理器结构 */
+struct _EventHandler
+{
+    List *pEventList;   /* 事件列表 */
+    bool IsRunning;     /* 是否正在运行:默认开 */
+    
+    /* 获取事件数 */
+    uint32_t (*GetEventCount)(EventHandler * const pEventHandler);
+    
+    /* 增加事件(按优先级排序) */
+    bool (*Add)(EventHandler * const pEventHandler, Event *pEvent);
+    
+    /* 删除事件(释放空间) */
+    bool (*Delete)(EventHandler * const pEventHandler, Event *pEvent);
+    
+    /* 清空事件列表(释放空间) */
+    bool (*Clear)(EventHandler * const pEventHandler);
+    
+    /* 销毁事件处理器(释放空间) */
+    bool (*Dispose)(EventHandler * const pEventHandler);
+    
+    /* 执行事件(按列表位置) */
+    void (*Execute)(struct _EventHandler * const pEventHandler);
+};
+
+/* 创建事件 */
+bool Event_create(Event **ppEvent);
+
+/* 增加事件参数(深拷贝,按先后顺序入队列) */
+bool Event_addArg(Event * const pEvent, void *pArg, uint32_t size);
+
+/* 销毁事件 */
+bool Event_Dispose(Event * const pEvent);
+
+/* 创建事件处理器 */
+bool EventHandler_create(EventHandler **ppEventHandler);
+
+/* 获取事件数 */
+uint32_t EventHandler_getEventCount(EventHandler * const pEventHandler);
+
+/* 增加事件(按优先级排序) */
+bool EventHandler_add(EventHandler * const pEventHandler, Event *pEvent);
+
+/* 删除事件(释放空间) */
+bool EventHandler_delete(EventHandler * const pEventHandler, Event *pEvent);
+
+/* 清空事件列表(释放空间) */
+bool EventHandler_clear(EventHandler * const pEventHandler);
+
+/* 销毁事件处理器(释放空间) */
+bool EventHandler_dispose(EventHandler * const pEventHandler);
+
+/* 执行事件(按列表位置) */
+void EventHandler_execute(EventHandler * const pEventHandler);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ZF_EVENT_H__ */
+
+/******************************** END OF FILE ********************************/

+ 138 - 0
inc/zf_fsm.h

@@ -0,0 +1,138 @@
+/**
+  *****************************************************************************
+  * @file    zf_fsm.h
+  * @author  Zorb
+  * @version V1.0.0
+  * @date    2018-06-28
+  * @brief   有限状态机的头文件
+  *****************************************************************************
+  * @history
+  *
+  * 1. Date:2018-06-28
+  *    Author:Zorb
+  *    Modification:建立文件
+  *
+  *****************************************************************************
+  */
+
+#ifndef __ZF_FSM_H__
+#define __ZF_FSM_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "stdint.h"
+#include "stdbool.h"
+#include "zf_list.h"
+
+/* 状态机信号0-31保留,用户信号在32以后定义 */
+enum {
+    FSM_NULL_SIG = 0,
+    FSM_ENTER_SIG,
+    FSM_EXIT_SIG,
+    FSM_USER_SIG_START = 32
+    /* 用户信号请在用户文件定义,不允许在此定义 */
+};
+
+typedef struct _Fsm Fsm;
+
+typedef uint32_t FsmSignal;
+
+typedef void (*IFsmState)(Fsm * const, FsmSignal const);
+
+/* 状态机结构 */
+struct _Fsm
+{
+    uint8_t Level;                  /* 嵌套层数,根状态机层数为1,子状态机层数自增 */
+                                    /* 注:严禁递归嵌套和环形嵌套 */
+    List *ChildList;                /* 子状态机列表 */
+    Fsm *Owner;                     /* 父状态机 */
+    IFsmState OwnerTriggerState;    /* 当父状态机为设定状态时,才触发当前状态机 */
+                                    /* 若不设定,则当执行完父状态机,立即运行子状态机 */
+    IFsmState CurrentState;         /* 当前状态 */
+    bool IsRunning;                 /* 是否正在运行(默认关) */
+    
+    /* 设置初始状态 */
+    void (*SetInitialState)(Fsm * const pFsm, IFsmState initialState);
+    
+    /* 运行当前状态机 */
+    bool (*Run)(Fsm * const pFsm);
+    
+    /* 运行当前状态机和子状态机 */
+    bool (*RunAll)(Fsm * const pFsm);
+    
+    /* 停止当前状态机 */
+    bool (*Stop)(Fsm * const pFsm);
+    
+    /* 停止当前状态机和子状态机 */
+    bool (*StopAll)(Fsm * const pFsm);
+    
+    /* 释放当前状态机 */
+    bool (*Dispose)(Fsm * const pFsm);
+    
+    /* 释放当前状态机和子状态机 */
+    bool (*DisposeAll)(Fsm * const pFsm);
+    
+    /* 添加子状态机 */
+    bool (*AddChild)(Fsm * const pFsm, Fsm * const pChildFsm);
+    
+    /* 移除子状态机(不释放空间) */
+    bool (*RemoveChild)(Fsm * const pFsm, Fsm * const pChildFsm);
+    
+    /* 调度状态机 */
+    bool (*Dispatch)(Fsm * const pFsm, FsmSignal const signal);
+    
+    /* 状态转移 */
+    void (*Transfer)(Fsm * const pFsm, IFsmState nextState);
+    
+    /* 状态转移(触发转出和转入事件) */
+    void (*TransferWithEvent)(Fsm * const pFsm, IFsmState nextState);
+};
+
+/* 创建状态机(内部分配空间) */
+bool Fsm_create(Fsm ** ppFsm);
+
+/* 设置初始状态 */
+void Fsm_setInitialState(Fsm * const pFsm, IFsmState initialState);
+
+/* 运行当前状态机 */
+bool Fsm_run(Fsm * const pFsm);
+
+/* 运行当前状态机和子状态机 */
+bool Fsm_runAll(Fsm * const pFsm);
+
+/* 停止当前状态机 */
+bool Fsm_stop(Fsm * const pFsm);
+
+/* 停止当前状态机和子状态机 */
+bool Fsm_stopAll(Fsm * const pFsm);
+
+/* 释放当前状态机 */
+bool Fsm_dispose(Fsm * const pFsm);
+
+/* 释放当前状态机和子状态机 */
+bool Fsm_disposeAll(Fsm * const pFsm);
+
+/* 添加子状态机 */
+bool Fsm_addChild(Fsm * const pFsm, Fsm * const pChildFsm);
+
+/* 移除子状态机(不释放空间) */
+bool Fsm_removeChild(Fsm * const pFsm, Fsm * const pChildFsm);
+
+/* 调度状态机 */
+bool Fsm_dispatch(Fsm * const pFsm, FsmSignal const signal);
+
+/* 状态转移 */
+void Fsm_transfer(Fsm * const pFsm, IFsmState nextState);
+
+/* 状态转移(触发转出和转入事件) */
+void Fsm_transferWithEvent(Fsm * const pFsm, IFsmState nextState);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ZF_FSM_H__ */
+
+/******************************** END OF FILE ********************************/

+ 38 - 0
inc/zf_includes.h

@@ -0,0 +1,38 @@
+/**
+  *****************************************************************************
+  * @file    zf_includes.h
+  * @author  Zorb
+  * @version V1.0.0
+  * @date    2018-06-28
+  * @brief   包含zorb flamework用到的所有头文件
+  *****************************************************************************
+  * @history
+  *
+  * 1. Date:2018-06-28
+  *    Author:Zorb
+  *    Modification:建立文件
+  *
+  *****************************************************************************
+  */
+
+#ifndef __ZF_INCLUDES_H__
+#define __ZF_INCLUDES_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "zf_debug.h"
+#include "zf_assert.h"
+#include "zf_time.h"
+#include "zf_malloc.h"
+#include "zf_buffer.h"
+#include "zf_list.h"
+#include "zf_fsm.h"
+#include "zf_event.h"
+#include "zf_timer.h"
+#include "zf_task.h"
+
+#endif /* __ZF_INCLUDES_H__ */
+
+/******************************** END OF FILE ********************************/