2024-10-29 08:42:21 +00:00
|
|
|
|
#ifndef ERPC_CORE_H_
|
|
|
|
|
#define ERPC_CORE_H_
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
2024-11-20 15:03:26 +00:00
|
|
|
|
#include "list.h"
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#define ERPC_VERSION "0.0.1"
|
2024-10-29 08:42:21 +00:00
|
|
|
|
|
2024-11-20 15:03:26 +00:00
|
|
|
|
typedef enum package_type
|
|
|
|
|
{
|
2024-10-29 08:42:21 +00:00
|
|
|
|
PACKAGE_TYPE_DEV_POWER_ON = 0,
|
|
|
|
|
PACKAGE_TYPE_CMD_REQ,
|
|
|
|
|
PACKAGE_TYPE_CMD_REQ_ACK,
|
|
|
|
|
PACKAGE_TYPE_CMD_RESP,
|
|
|
|
|
PACKAGE_TYPE_CMD_RESP_ACK,
|
2024-11-20 15:03:26 +00:00
|
|
|
|
PACKAGE_TYPE_CMD_REPEAT, // crc校验失败,请求重发
|
2024-10-29 08:42:21 +00:00
|
|
|
|
PACKAGE_TYPE_DEV_POWER_OFF,
|
|
|
|
|
PACKAGE_TYPE_CMD_SERCH_DEV_ONLINE,
|
|
|
|
|
} package_type;
|
|
|
|
|
|
|
|
|
|
// 广播ID
|
|
|
|
|
#define ERPC_BOARDCAST_ID 0xff
|
|
|
|
|
|
2024-11-20 15:03:26 +00:00
|
|
|
|
typedef struct erpc_cmd_head_t
|
|
|
|
|
{
|
2024-10-29 08:42:21 +00:00
|
|
|
|
u8 src_id;
|
|
|
|
|
u8 dest_id;
|
|
|
|
|
package_type type;
|
2024-11-20 15:03:26 +00:00
|
|
|
|
u16 port; // 指令号,指令号=端口号
|
2024-10-29 08:42:21 +00:00
|
|
|
|
u8 msg_len;
|
2024-11-20 15:03:26 +00:00
|
|
|
|
} erpc_cmd_head_t;
|
|
|
|
|
|
|
|
|
|
typedef struct erpc_cmd_def_t
|
|
|
|
|
{
|
|
|
|
|
erpc_cmd_head_t head;
|
2024-10-29 08:42:21 +00:00
|
|
|
|
u8 crc_16[2];
|
|
|
|
|
u8 *data;
|
|
|
|
|
} erpc_cmd_def_t;
|
|
|
|
|
|
2024-11-20 15:03:26 +00:00
|
|
|
|
#define MAX_CMD_LEN (256 - sizeof(erpc_cmd_head_t)) // crc16最大支持256字节
|
|
|
|
|
#define CMD_MAX_RETRY 5 // 最大重发次数
|
|
|
|
|
#define CMD_TIMEOUT 30 // 超时时间 tick
|
|
|
|
|
#define MAX_REC_CMD_CACHE_NUM 10 // 最大接收指令缓存数量,接受指令优先级>处理指令优先级
|
|
|
|
|
#define MAX_SEND_CMD_CACHE_NUM 10 // 最大发送指令缓存数量,发送指令优先级>接收指令优先级
|
|
|
|
|
typedef enum erpc_status
|
|
|
|
|
{
|
|
|
|
|
ERPC_CMD_NO_ERROR, // 发送cache,接收cache空闲
|
|
|
|
|
ERPC_CMD_DATA_DEAL, // 发送cache数据组织中
|
|
|
|
|
ERPC_CMD_WAIT_SERVER_DEAL, // 等待服务线程处理接受cache
|
|
|
|
|
ERPC_CMD_WAIT_TASK_DEAL, // 等待任务线程处理指令
|
|
|
|
|
ERPC_CMD_WAIT_TASK_DEAL_FINISH, // 等待任务线程处理指令完成
|
|
|
|
|
ERPC_CMD_WAIT_SEND, // 等待服务线程处理发送cache
|
|
|
|
|
ERPC_CMD_SEND_ONCE, // 发送指令成功
|
|
|
|
|
ERPC_CMD_SEND_REPEAT, // 发送指令重发
|
|
|
|
|
ERPC_CMD_SEND_OK, // 发送指令成功
|
|
|
|
|
ERPC_CMD_DEST_BUSY, // 目标设备忙
|
|
|
|
|
}erpc_status;
|
|
|
|
|
|
|
|
|
|
typedef struct erpc_data_cache_t
|
|
|
|
|
{
|
|
|
|
|
u8 state;
|
|
|
|
|
u8 data[256];
|
|
|
|
|
} erpc_data_cache_t;
|
|
|
|
|
|
|
|
|
|
typedef u32 (*erpc_cmd_handle_t)(u8 src_id, u8 dest_id, u16 port, u8 *data, u16 len);
|
|
|
|
|
|
|
|
|
|
typedef struct erpc_cmd_list_t
|
|
|
|
|
{
|
|
|
|
|
u16 cmd;
|
|
|
|
|
u32 (*handle)(u8 hw, u8 src_id, u8 dest_id, u16 port, u8 *data, u16 len);
|
|
|
|
|
} erpc_cmd_list_t;
|
|
|
|
|
|
|
|
|
|
typedef struct erpc_hw_cfg_t
|
|
|
|
|
{
|
|
|
|
|
u8 ord;
|
|
|
|
|
u8 local_id;
|
|
|
|
|
u8 (*write)(u8 *data, u16 len);
|
|
|
|
|
void (*send_lock)(void);
|
|
|
|
|
void (*send_unlock)(void);
|
|
|
|
|
erpc_data_cache_t rec_cache[MAX_REC_CMD_CACHE_NUM];
|
|
|
|
|
erpc_data_cache_t send_cache[MAX_SEND_CMD_CACHE_NUM];
|
|
|
|
|
void (*deal_lock)(void);
|
|
|
|
|
void (*deal_unlock)(void);
|
|
|
|
|
list_t cmd_list;
|
|
|
|
|
} erpc_hw_cfg_t;
|
|
|
|
|
|
|
|
|
|
#define CHECK_IF_ERROR(condition, error) \
|
|
|
|
|
if (condition) \
|
|
|
|
|
{ \
|
|
|
|
|
return error; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef enum erpc_error
|
|
|
|
|
{
|
|
|
|
|
ERPC_NO_ERROR = 0,
|
|
|
|
|
ERPC_ERR_NOFOND_HW,
|
|
|
|
|
ERPC_ERR_SEND_CACHE_FULL,
|
|
|
|
|
ERPC_ERR_SEND_TIMEOUT,
|
|
|
|
|
ERPC_ERR_HW_SEND_FAIL, // 硬件层发来的错误 erpc_hw_cfg_t.write返回错误
|
|
|
|
|
ERPC_ERR_DEST_BUSY,
|
|
|
|
|
ERPC_ERR_DEST_NO_RESPONSE, // 目标设备无响应
|
|
|
|
|
ERPC_ERR_MALLOC_ERROR, // malloc失败,内存不足
|
|
|
|
|
ERPC_ERR_HW_EXIST, // 硬件已经存在
|
|
|
|
|
ERPC_ERR_HW_NOT_EXIST, // 硬件不存在
|
|
|
|
|
ERPC_ERR_REC_CACHE_FULL, // 接收缓存满
|
|
|
|
|
}erpc_error;
|
|
|
|
|
typedef void (*erpc_sleep)(u32 tick);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void erpc_send_deal_core(); // 发送指令处理线程
|
|
|
|
|
u32 erpc_set_rev_cahce(u8 hw, u8 *data, u16 len); // 写入接受缓存
|
|
|
|
|
void erpc_rev_package_core(); // 处理接受到的包,通讯层面处理
|
|
|
|
|
void erpc_rev_deal_core(); // 处理接受到的指令,业务层面处理
|
|
|
|
|
extern erpc_sleep erpc_sleep_tick;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u32 erpc_hw_add(erpc_hw_cfg_t *hw);
|
|
|
|
|
u32 erpc_add_cmd_list(erpc_hw_cfg_t *hw, erpc_cmd_list_t *cmd_list);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u32 erpc_send(u8 hw, u8 dest_id, u16 port, u8 *data, u16 len);
|
2024-10-29 08:42:21 +00:00
|
|
|
|
#endif /* ERPC_CORE_H_ */
|