2024-10-29 08:42:21 +00:00
|
|
|
|
#ifndef ERPC_CORE_H_
|
|
|
|
|
#define ERPC_CORE_H_
|
|
|
|
|
|
2024-11-20 15:03:26 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2024-11-21 08:59:31 +00:00
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#include "list.h"
|
2024-11-20 15:03:26 +00:00
|
|
|
|
#define ERPC_VERSION "0.0.1"
|
2024-10-29 08:42:21 +00:00
|
|
|
|
|
2024-11-21 08:59:31 +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-21 08:59:31 +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-21 08:59:31 +00:00
|
|
|
|
typedef struct erpc_cmd_head_t {
|
2024-10-29 08:42:21 +00:00
|
|
|
|
u8 src_id;
|
|
|
|
|
u8 dest_id;
|
2024-11-21 08:59:31 +00:00
|
|
|
|
u8 type; // package_type
|
|
|
|
|
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;
|
|
|
|
|
|
2024-11-21 13:22:33 +00:00
|
|
|
|
#define MAX_CMD_LEN (256 - sizeof(erpc_cmd_head_t)) // crc16最大支持256字节
|
|
|
|
|
|
2024-11-21 08:59:31 +00:00
|
|
|
|
typedef struct erpc_cmd_def_t {
|
2024-11-20 15:03:26 +00:00
|
|
|
|
erpc_cmd_head_t head;
|
2024-10-29 08:42:21 +00:00
|
|
|
|
u8 crc_16[2];
|
2024-11-21 13:22:33 +00:00
|
|
|
|
u8 data[MAX_CMD_LEN];
|
2024-10-29 08:42:21 +00:00
|
|
|
|
} erpc_cmd_def_t;
|
|
|
|
|
|
2024-11-21 08:59:31 +00:00
|
|
|
|
#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
|
2024-11-24 13:51:09 +00:00
|
|
|
|
ERPC_CMD_SERVER_DEAL, // 服务线程处理接受cache
|
2024-11-21 08:59:31 +00:00
|
|
|
|
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_CMD_RESP_OK, // 指令响应成功
|
2024-11-20 16:12:48 +00:00
|
|
|
|
} erpc_status;
|
2024-11-20 15:03:26 +00:00
|
|
|
|
|
2024-11-21 08:59:31 +00:00
|
|
|
|
typedef struct erpc_data_cache_t {
|
2024-11-20 15:03:26 +00:00
|
|
|
|
u8 state;
|
|
|
|
|
u8 data[256];
|
|
|
|
|
} erpc_data_cache_t;
|
|
|
|
|
|
2024-11-21 08:59:31 +00:00
|
|
|
|
typedef u32 (*erpc_cmd_handle_t)(u8 src_id, u8 dest_id, u16 port, u8 *data,
|
|
|
|
|
u16 len);
|
2024-11-20 15:03:26 +00:00
|
|
|
|
|
2024-11-21 08:59:31 +00:00
|
|
|
|
typedef struct erpc_cmd_list_t {
|
2024-11-20 15:03:26 +00:00
|
|
|
|
u16 cmd;
|
|
|
|
|
u32 (*handle)(u8 hw, u8 src_id, u8 dest_id, u16 port, u8 *data, u16 len);
|
|
|
|
|
} erpc_cmd_list_t;
|
2024-11-21 08:59:31 +00:00
|
|
|
|
typedef enum ERPC_ROLE {
|
2024-11-20 16:12:48 +00:00
|
|
|
|
ERPC_ROLE_MASTER,
|
|
|
|
|
ERPC_ROLE_SLAVE,
|
|
|
|
|
} ERPC_ROLE;
|
2024-11-21 08:59:31 +00:00
|
|
|
|
typedef struct erpc_hw_cfg_t {
|
2024-11-20 15:03:26 +00:00
|
|
|
|
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) \
|
2024-11-21 08:59:31 +00:00
|
|
|
|
if (condition) { \
|
2024-11-20 15:03:26 +00:00
|
|
|
|
return error; \
|
|
|
|
|
}
|
2024-11-20 16:12:48 +00:00
|
|
|
|
#define CHECK_IF_ERROR_AND_DO(condition, error, action) \
|
2024-11-21 08:59:31 +00:00
|
|
|
|
if (condition) { \
|
2024-11-20 16:12:48 +00:00
|
|
|
|
action; \
|
|
|
|
|
return error; \
|
|
|
|
|
}
|
2024-11-21 08:59:31 +00:00
|
|
|
|
typedef enum erpc_error {
|
2024-11-20 15:03:26 +00:00
|
|
|
|
ERPC_NO_ERROR = 0,
|
|
|
|
|
ERPC_ERR_NOFOND_HW,
|
|
|
|
|
ERPC_ERR_SEND_CACHE_FULL,
|
|
|
|
|
ERPC_ERR_SEND_TIMEOUT,
|
2024-11-21 08:59:31 +00:00
|
|
|
|
ERPC_ERR_HW_SEND_FAIL, // 硬件层发来的错误 erpc_hw_cfg_t.write返回错误
|
2024-11-20 15:03:26 +00:00
|
|
|
|
ERPC_ERR_DEST_BUSY,
|
2024-11-21 08:59:31 +00:00
|
|
|
|
ERPC_ERR_DEST_NO_RESPONSE, // 目标设备无响应
|
|
|
|
|
ERPC_ERR_DEST_NO_REPLY, // 目标设备无回复或者回复超时
|
|
|
|
|
ERPC_ERR_MALLOC_ERROR, // malloc失败,内存不足
|
|
|
|
|
ERPC_ERR_HW_EXIST, // 硬件已经存在
|
|
|
|
|
ERPC_ERR_HW_NOT_EXIST, // 硬件不存在
|
|
|
|
|
ERPC_ERR_REC_CACHE_FULL, // 接收缓存满
|
2024-11-20 16:12:48 +00:00
|
|
|
|
} erpc_error;
|
2024-11-20 15:03:26 +00:00
|
|
|
|
typedef void (*erpc_sleep)(u32 tick);
|
|
|
|
|
|
2024-11-21 08:59:31 +00:00
|
|
|
|
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(); // 处理接受到的指令,业务层面处理
|
2024-11-20 15:03:26 +00:00
|
|
|
|
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);
|
2024-11-21 08:59:31 +00:00
|
|
|
|
/**
|
|
|
|
|
* 单纯发送消息
|
|
|
|
|
*/
|
2024-11-20 15:03:26 +00:00
|
|
|
|
u32 erpc_send(u8 hw, u8 dest_id, u16 port, u8 *data, u16 len);
|
2024-11-21 08:59:31 +00:00
|
|
|
|
/**
|
|
|
|
|
* 发送消息并等待回复
|
|
|
|
|
*/
|
|
|
|
|
u32 erpc_send_wait_reply(u8 hw, u8 dest_id, u16 port, u8 *data, u16 len,
|
|
|
|
|
u8 *reply_data, u16 *reply_len, u32 timeout);
|
|
|
|
|
/**
|
|
|
|
|
* 回复消息
|
|
|
|
|
*/
|
|
|
|
|
u32 erpc_replay(u8 hw, u8 dest_id, u16 port, u8 *data_out, u16 len);
|
2024-10-29 08:42:21 +00:00
|
|
|
|
#endif /* ERPC_CORE_H_ */
|