freeRTOS操作系统机器人实现
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.1 KiB

3 days ago
/******************************************************************************
(C), 2018-2099, Radkil
******************************************************************************
: com.h
: 稿
: Radkil
: 2026327
:
: com.c
:
1. : 2026327
: Radkil
:
******************************************************************************/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
#ifndef __COM_H__
#define __COM_H__
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
/*==============================================*
* include header files *
*----------------------------------------------*/
#include <stdint.h>
#include "common.h"
#include "rd_thread.h"
#include "ringbuffer.h"
#include "utarray.h"
/*==============================================*
* constants or macros define *
*----------------------------------------------*/
#define RECV_TIMEOUT 100 //接收超时时间(100ms)
typedef int (*cbk_ComCheck)(char *_pBuffer, uint32_t _iSize);
// 返回值 > 0 表示校验成功且返回包的实际长度
// 返回值 = 0 表示数据不完整,需要继续等待
// 返回值 < 0 表示数据错误
typedef void (*cbk_ComDecode)(const char *_pBuffer, uint32_t _iSize);
typedef int (*cbk_ComSend)(char *_pBuffer, uint32_t _iSize);
typedef struct
{
Rd_sem_t m_sem_Send;
Rd_sem_t m_sem_Recv;
rd_ringbuf_t *m_ptSend;
rd_ringbuf_t *m_ptRecv;
} TComBuffer;
typedef struct
{
uint32_t m_uiLastRecvTime; // 超时丢弃时间
cbk_ComCheck m_pfCheck; // 协议特征检查
cbk_ComDecode m_pfDecode; // 真正的协议处理
}TMutiDev;
typedef struct
{
TComBuffer m_stComBuf;
int m_iBufferSize;
int m_iDevIndex; // 表示当前正在接收的设备索引,-1表示当前无设备在处理
UT_array *m_pMutiDevArray;
cbk_ComSend m_pfSend;
void *m_pUserData;
}TComCtrl;
/*==============================================*
* project-wide global variables *
*----------------------------------------------*/
/*==============================================*
* routines' or functions' implementations *
*----------------------------------------------*/
TComCtrl *rd_ComCreate(cbk_ComCheck _pfCheck, cbk_ComDecode _pfDecode, cbk_ComSend _pfSend, int _iSize, void *_pUserData);
void rd_ComAddDev(TComCtrl *_ptComCtrl, cbk_ComCheck _pfCheck, cbk_ComDecode _pfDecode);
int rd_ComDelete(TComCtrl *_ptComCtrl);
int rd_ComSend(TComCtrl *_ptComCtrl, char *_pBuffer, uint32_t _iSize);
void rd_ComSendProc(TComCtrl *_ptComCtrl);
void rd_ComRecvProc(TComCtrl *_ptComCtrl, const char *_pBuffer, uint32_t _iSize);
int rd_ComWrite(TComCtrl *_ptComCtrl, char *_pBuffer, uint32_t _iSize);
int rd_ComRead(TComCtrl *_ptComCtrl, char *_pBuffer, uint32_t _iSize);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* __COM_H__ */