fix(add): 新增resp功能
This commit is contained in:
parent
70fb05c650
commit
c31caa0e72
|
@ -16,7 +16,11 @@
|
|||
"list.h": "c",
|
||||
"array": "c",
|
||||
"string": "c",
|
||||
"string_view": "c"
|
||||
"string_view": "c",
|
||||
"windows.h": "c",
|
||||
"string.h": "c",
|
||||
"stdio.h": "c",
|
||||
"stdlib.h": "c"
|
||||
},
|
||||
"makefile.launchConfigurations": [
|
||||
{
|
||||
|
|
326
erpc_core.c
326
erpc_core.c
|
@ -9,14 +9,11 @@
|
|||
erpc_sleep erpc_sleep_tick = NULL;
|
||||
|
||||
static list_t erpc_hw;
|
||||
void clean_cache(erpc_hw_cfg_t *hw)
|
||||
{
|
||||
for (int i = 0; i < MAX_REC_CMD_CACHE_NUM; i++)
|
||||
{
|
||||
void clean_cache(erpc_hw_cfg_t *hw) {
|
||||
for (int i = 0; i < MAX_REC_CMD_CACHE_NUM; i++) {
|
||||
hw->rec_cache->state = ERPC_CMD_NO_ERROR;
|
||||
}
|
||||
for (int i = 0; i < MAX_SEND_CMD_CACHE_NUM; i++)
|
||||
{
|
||||
for (int i = 0; i < MAX_SEND_CMD_CACHE_NUM; i++) {
|
||||
hw->send_cache[i].state = ERPC_CMD_NO_ERROR;
|
||||
}
|
||||
}
|
||||
|
@ -24,35 +21,29 @@ void clean_cache(erpc_hw_cfg_t *hw)
|
|||
* 注册 hardware 设备
|
||||
* 线程不安全
|
||||
*/
|
||||
u32 erpc_hw_add(erpc_hw_cfg_t *hw)
|
||||
{
|
||||
u32 erpc_hw_add(erpc_hw_cfg_t *hw) {
|
||||
list_t *list = &erpc_hw;
|
||||
if (list->data == NULL)
|
||||
{
|
||||
if (list->data == NULL) {
|
||||
list->data = (void *)hw;
|
||||
clean_cache(hw);
|
||||
return ERPC_NO_ERROR;
|
||||
}
|
||||
list_t *list_node = malloc(sizeof(list_t));
|
||||
if (list_node == NULL)
|
||||
{
|
||||
if (list_node == NULL) {
|
||||
return ERPC_ERR_MALLOC_ERROR;
|
||||
}
|
||||
list_node->data = (void *)hw;
|
||||
u8 hw_ord = hw->ord;
|
||||
// 检查hw是否已经存在
|
||||
while (list != NULL)
|
||||
{
|
||||
while (list != NULL) {
|
||||
erpc_hw_cfg_t *hw_cfg = (erpc_hw_cfg_t *)list->data;
|
||||
if (hw_cfg->ord == hw_ord)
|
||||
{
|
||||
if (hw_cfg->ord == hw_ord) {
|
||||
return ERPC_ERR_HW_EXIST;
|
||||
}
|
||||
list = list->next;
|
||||
}
|
||||
list_t *result = list_append(&erpc_hw, hw);
|
||||
if (result == NULL)
|
||||
{
|
||||
if (result == NULL) {
|
||||
return ERPC_ERR_MALLOC_ERROR;
|
||||
}
|
||||
clean_cache(hw);
|
||||
|
@ -62,17 +53,14 @@ 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_add_cmd_list(erpc_hw_cfg_t *hw, erpc_cmd_list_t *cmd_list) {
|
||||
list_t *list = &hw->cmd_list;
|
||||
if (list->data == NULL)
|
||||
{
|
||||
if (list->data == NULL) {
|
||||
list->data = (void *)cmd_list;
|
||||
return ERPC_NO_ERROR;
|
||||
}
|
||||
list = list_append(&hw->cmd_list, cmd_list);
|
||||
if (list == NULL)
|
||||
{
|
||||
if (list == NULL) {
|
||||
return ERPC_ERR_MALLOC_ERROR;
|
||||
}
|
||||
return ERPC_NO_ERROR;
|
||||
|
@ -81,18 +69,14 @@ u32 erpc_add_cmd_list(erpc_hw_cfg_t *hw, erpc_cmd_list_t *cmd_list)
|
|||
* 移除 hardware 设备
|
||||
* 线程不安全
|
||||
*/
|
||||
u32 erpc_hw_remove(u8 hw)
|
||||
{
|
||||
u32 erpc_hw_remove(u8 hw) {
|
||||
list_t *list = &erpc_hw;
|
||||
if (list->data == NULL)
|
||||
{
|
||||
if (list->data == NULL) {
|
||||
return ERPC_ERR_NOFOND_HW;
|
||||
}
|
||||
while (list != NULL)
|
||||
{
|
||||
while (list != NULL) {
|
||||
erpc_hw_cfg_t *hw_cfg = (erpc_hw_cfg_t *)list->data;
|
||||
if (hw_cfg->ord == hw)
|
||||
{
|
||||
if (hw_cfg->ord == hw) {
|
||||
list_delete(&erpc_hw, list);
|
||||
free(list);
|
||||
return ERPC_NO_ERROR;
|
||||
|
@ -104,87 +88,78 @@ u32 erpc_hw_remove(u8 hw)
|
|||
/**
|
||||
* 获取 hardware 设备
|
||||
*/
|
||||
erpc_hw_cfg_t *erpc_hw_get(u8 hw)
|
||||
{
|
||||
erpc_hw_cfg_t *erpc_hw_get(u8 hw) {
|
||||
list_t *list = &erpc_hw;
|
||||
if (list->data == NULL)
|
||||
{
|
||||
if (list->data == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
while (list != NULL)
|
||||
{
|
||||
while (list != NULL) {
|
||||
erpc_hw_cfg_t *hw_cfg = (erpc_hw_cfg_t *)list->data;
|
||||
if (hw_cfg->ord == hw)
|
||||
{
|
||||
if (hw_cfg->ord == hw) {
|
||||
return hw_cfg;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
u32 erpc_send(u8 hw, u8 dest_id, u16 port, u8 *data, u16 len)
|
||||
{
|
||||
erpc_hw_cfg_t *hw_cfg = erpc_hw_get(hw);
|
||||
u32 erpc_send_base(erpc_hw_cfg_t *hw_cfg, u8 dest_id, u16 port, u8 package_type,
|
||||
u8 *data, u16 len) {
|
||||
CHECK_IF_ERROR(hw_cfg == NULL, ERPC_ERR_NOFOND_HW);
|
||||
u8 cache_ord = 255;
|
||||
// 查找可用的发送缓存
|
||||
for (int i = 0; i < MAX_SEND_CMD_CACHE_NUM; i++)
|
||||
{
|
||||
if (hw_cfg->send_cache[i].state == ERPC_CMD_NO_ERROR)
|
||||
{
|
||||
for (int i = 0; i < MAX_SEND_CMD_CACHE_NUM; i++) {
|
||||
if (hw_cfg->send_cache[i].state == ERPC_CMD_NO_ERROR) {
|
||||
printf("find send cache %d\n", i);
|
||||
cache_ord = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CHECK_IF_ERROR(cache_ord == 255, ERPC_ERR_SEND_CACHE_FULL);
|
||||
// 准备数据
|
||||
hw_cfg->send_cache[cache_ord].state = ERPC_CMD_DATA_DEAL;
|
||||
erpc_cmd_def_t *cmd = (erpc_cmd_def_t *)&hw_cfg->send_cache[cache_ord].data[0];
|
||||
erpc_cmd_def_t *cmd =
|
||||
(erpc_cmd_def_t *)&hw_cfg->send_cache[cache_ord].data[0];
|
||||
cmd->head.dest_id = dest_id;
|
||||
cmd->head.port = port;
|
||||
cmd->head.msg_len = len;
|
||||
cmd->head.type = PACKAGE_TYPE_CMD_REQ;
|
||||
if (data != NULL) {
|
||||
cmd->head.msg_len = 0;
|
||||
}
|
||||
cmd->head.type = package_type;
|
||||
cmd->head.src_id = hw_cfg->local_id;
|
||||
if (len > 0 && data != NULL) {
|
||||
memcpy(cmd->data, data, len);
|
||||
}
|
||||
// 计算校验和
|
||||
cmd_cal_crc16(cmd);
|
||||
|
||||
// 发送数据
|
||||
hw_cfg->send_cache[cache_ord].state = ERPC_CMD_WAIT_SEND;
|
||||
// 等待数据发送完成
|
||||
int wait_time = CMD_TIMEOUT;
|
||||
while (wait_time > 0)
|
||||
{
|
||||
if (hw_cfg->send_cache[cache_ord].state == ERPC_CMD_SEND_OK)
|
||||
{
|
||||
while (wait_time > 0) {
|
||||
if (hw_cfg->send_cache[cache_ord].state == ERPC_CMD_SEND_OK) {
|
||||
break;
|
||||
}
|
||||
if (hw_cfg->send_cache[cache_ord].state == ERPC_CMD_DEST_BUSY)
|
||||
{
|
||||
if (hw_cfg->send_cache[cache_ord].state == ERPC_CMD_DEST_BUSY) {
|
||||
return ERPC_ERR_DEST_BUSY;
|
||||
}
|
||||
if (erpc_sleep_tick != NULL)
|
||||
{
|
||||
if (erpc_sleep_tick != NULL) {
|
||||
erpc_sleep_tick(1);
|
||||
}
|
||||
|
||||
wait_time--;
|
||||
}
|
||||
u32 ret = ERPC_NO_ERROR;
|
||||
do
|
||||
{
|
||||
if (wait_time == 0)
|
||||
{
|
||||
do {
|
||||
if (wait_time == 0) {
|
||||
ret = ERPC_ERR_SEND_TIMEOUT;
|
||||
break;
|
||||
}
|
||||
if (hw_cfg->send_cache[cache_ord].state == ERPC_CMD_SEND_ONCE)
|
||||
{
|
||||
if (hw_cfg->send_cache[cache_ord].state == ERPC_CMD_SEND_ONCE) {
|
||||
ret = ERPC_ERR_DEST_NO_RESPONSE;
|
||||
break;
|
||||
}
|
||||
if (hw_cfg->send_cache[cache_ord].state == ERPC_CMD_SEND_REPEAT)
|
||||
{
|
||||
if (hw_cfg->send_cache[cache_ord].state == ERPC_CMD_SEND_REPEAT) {
|
||||
ret = ERPC_ERR_DEST_NO_RESPONSE;
|
||||
break;
|
||||
}
|
||||
|
@ -192,13 +167,69 @@ u32 erpc_send(u8 hw, u8 dest_id, u16 port, u8 *data, u16 len)
|
|||
hw_cfg->send_cache[cache_ord].state = ERPC_CMD_NO_ERROR;
|
||||
return ret;
|
||||
}
|
||||
u32 erpc_send(u8 hw, u8 dest_id, u16 port, u8 *data, u16 len) {
|
||||
erpc_hw_cfg_t *hw_cfg = erpc_hw_get(hw);
|
||||
u32 ret =
|
||||
erpc_send_base(hw_cfg, dest_id, port, PACKAGE_TYPE_CMD_REQ, data, len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
u32 erpc_send_data(erpc_hw_cfg_t *hw)
|
||||
{
|
||||
for (int i = 0; i < MAX_SEND_CMD_CACHE_NUM; i++)
|
||||
{
|
||||
if (hw->send_cache[i].state == ERPC_CMD_WAIT_SEND)
|
||||
{
|
||||
u32 erpc_wait_resp_package(u8 hw, u8 dest_id, u16 port, u8 *reply_data,
|
||||
u16 *reply_len, u32 time_out) {
|
||||
erpc_hw_cfg_t *hw_cfg = erpc_hw_get(hw);
|
||||
CHECK_IF_ERROR(hw_cfg == NULL, ERPC_ERR_NOFOND_HW);
|
||||
while (time_out > 0) {
|
||||
for (int i = 0; i < MAX_REC_CMD_CACHE_NUM; i++) {
|
||||
erpc_data_cache_t *rec_cahce = &hw_cfg->rec_cache[i];
|
||||
if (rec_cahce->state != ERPC_CMD_RESP_OK) {
|
||||
continue;
|
||||
}
|
||||
erpc_cmd_def_t *cmd = (erpc_cmd_def_t *)&rec_cahce->data[0];
|
||||
if (cmd->head.port != port) {
|
||||
continue;
|
||||
}
|
||||
if (cmd->head.src_id != dest_id) {
|
||||
continue;
|
||||
}
|
||||
if (reply_data != NULL) {
|
||||
memccpy(reply_data, cmd->data, 0, cmd->head.msg_len);
|
||||
*reply_len = cmd->head.msg_len;
|
||||
}
|
||||
rec_cahce->state = ERPC_CMD_NO_ERROR;
|
||||
return ERPC_NO_ERROR;
|
||||
}
|
||||
if (erpc_sleep_tick != NULL) {
|
||||
erpc_sleep_tick(1);
|
||||
}
|
||||
time_out--;
|
||||
}
|
||||
CHECK_IF_ERROR(time_out == 0, ERPC_ERR_DEST_NO_REPLY);
|
||||
return ERPC_NO_ERROR;
|
||||
}
|
||||
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 ret = erpc_send(hw, dest_id, port, data, len);
|
||||
if (reply_len != NULL) {
|
||||
*reply_len = 0;
|
||||
}
|
||||
CHECK_IF_ERROR(ret != ERPC_NO_ERROR, ret);
|
||||
printf("send ok\n");
|
||||
ret =
|
||||
erpc_wait_resp_package(hw, dest_id, port, reply_data, reply_len, timeout);
|
||||
CHECK_IF_ERROR(ret != ERPC_NO_ERROR, ret);
|
||||
return ERPC_NO_ERROR;
|
||||
}
|
||||
u32 erpc_replay(u8 hw, u8 dest_id, u16 port, u8 *data_out, u16 len) {
|
||||
erpc_hw_cfg_t *hw_cfg = erpc_hw_get(hw);
|
||||
u32 ret = erpc_send_base(hw_cfg, dest_id, port, PACKAGE_TYPE_CMD_REPEAT,
|
||||
data_out, len);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
u32 erpc_send_data(erpc_hw_cfg_t *hw) {
|
||||
for (int i = 0; i < MAX_SEND_CMD_CACHE_NUM; i++) {
|
||||
if (hw->send_cache[i].state == ERPC_CMD_WAIT_SEND) {
|
||||
erpc_cmd_def_t *cmd = (erpc_cmd_def_t *)&hw->send_cache[i].data[0];
|
||||
u32 len = sizeof(erpc_cmd_head_t) + 2 + cmd->head.msg_len;
|
||||
hw->send_lock();
|
||||
|
@ -208,17 +239,15 @@ u32 erpc_send_data(erpc_hw_cfg_t *hw)
|
|||
hw->send_cache[i].state = ERPC_CMD_SEND_ONCE;
|
||||
}
|
||||
}
|
||||
return ERPC_NO_ERROR;
|
||||
}
|
||||
|
||||
u32 erpc_rev_ack_package(erpc_hw_cfg_t *hw, erpc_cmd_def_t *cmd_rev)
|
||||
{
|
||||
for (int i = 0; i < MAX_SEND_CMD_CACHE_NUM; i++)
|
||||
{
|
||||
if (hw->send_cache[i].state == ERPC_CMD_SEND_ONCE || hw->send_cache[i].state == ERPC_CMD_SEND_REPEAT)
|
||||
{
|
||||
u32 erpc_rev_ack_package(erpc_hw_cfg_t *hw, erpc_cmd_def_t *cmd_rev) {
|
||||
for (int i = 0; i < MAX_SEND_CMD_CACHE_NUM; i++) {
|
||||
if (hw->send_cache[i].state == ERPC_CMD_SEND_ONCE ||
|
||||
hw->send_cache[i].state == ERPC_CMD_SEND_REPEAT) {
|
||||
erpc_cmd_def_t *cmd_send = (erpc_cmd_def_t *)&hw->send_cache[i].data[0];
|
||||
if (cmd_rev->head.port == cmd_send->head.port)
|
||||
{
|
||||
if (cmd_rev->head.port == cmd_send->head.port) {
|
||||
hw->send_cache[i].state = ERPC_CMD_SEND_OK;
|
||||
return ERPC_NO_ERROR;
|
||||
}
|
||||
|
@ -226,30 +255,41 @@ u32 erpc_rev_ack_package(erpc_hw_cfg_t *hw, erpc_cmd_def_t *cmd_rev)
|
|||
}
|
||||
return ERPC_NO_ERROR;
|
||||
}
|
||||
|
||||
u32 erpc_rev_resp_package(erpc_hw_cfg_t *hw, erpc_cmd_def_t *cmd_rev) {
|
||||
erpc_cmd_def_t cmd_send;
|
||||
cmd_send.head.dest_id = cmd_rev->head.src_id;
|
||||
cmd_send.head.port = cmd_rev->head.port;
|
||||
cmd_send.head.msg_len = 0;
|
||||
cmd_send.head.type = PACKAGE_TYPE_CMD_RESP_ACK;
|
||||
cmd_send.head.src_id = hw->local_id;
|
||||
cmd_cal_crc16(&cmd_send);
|
||||
u32 len = sizeof(erpc_cmd_head_t) + 2 + cmd_send.head.msg_len;
|
||||
hw->send_lock();
|
||||
u8 ret = hw->write((u8 *)&cmd_send, len);
|
||||
hw->send_unlock();
|
||||
CHECK_IF_ERROR(ret != 0, ERPC_ERR_HW_SEND_FAIL);
|
||||
return ERPC_NO_ERROR;
|
||||
}
|
||||
// 重发数据包
|
||||
u32 erpc_rev_repeat_package(erpc_hw_cfg_t *hw, erpc_cmd_def_t *cmd_rev)
|
||||
{
|
||||
for (int i = 0; i < MAX_SEND_CMD_CACHE_NUM; i++)
|
||||
{
|
||||
if (hw->send_cache[i].state == ERPC_CMD_SEND_ONCE || hw->send_cache[i].state == ERPC_CMD_SEND_REPEAT)
|
||||
{
|
||||
u32 erpc_rev_repeat_package(erpc_hw_cfg_t *hw, erpc_cmd_def_t *cmd_rev) {
|
||||
for (int i = 0; i < MAX_SEND_CMD_CACHE_NUM; i++) {
|
||||
if (hw->send_cache[i].state == ERPC_CMD_SEND_ONCE ||
|
||||
hw->send_cache[i].state == ERPC_CMD_SEND_REPEAT) {
|
||||
erpc_cmd_def_t *cmd_send = (erpc_cmd_def_t *)&hw->send_cache[i].data[0];
|
||||
if (cmd_rev->head.port == cmd_send->head.port)
|
||||
{
|
||||
if (cmd_rev->head.port == cmd_send->head.port) {
|
||||
u32 len = sizeof(erpc_cmd_head_t) + 2 + cmd_send->head.msg_len;
|
||||
hw->send_lock();
|
||||
u8 ret = hw->write((u8 *)hw->send_cache[i].data, len);
|
||||
u32 ret = hw->write((u8 *)hw->send_cache[i].data, len);
|
||||
hw->send_unlock();
|
||||
hw->send_cache[i].state = ERPC_CMD_SEND_REPEAT;
|
||||
CHECK_IF_ERROR(ret, ERPC_ERR_HW_SEND_FAIL); //
|
||||
return ERPC_NO_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ERPC_NO_ERROR;
|
||||
}
|
||||
u32 erpc_send_ack_package(erpc_hw_cfg_t *hw, erpc_cmd_def_t *cmd_rev)
|
||||
{
|
||||
u32 erpc_send_ack_package(erpc_hw_cfg_t *hw, erpc_cmd_def_t *cmd_rev) {
|
||||
erpc_cmd_def_t cmd_send;
|
||||
cmd_send.head.dest_id = cmd_rev->head.src_id;
|
||||
cmd_send.head.port = cmd_rev->head.port;
|
||||
|
@ -265,36 +305,32 @@ u32 erpc_send_ack_package(erpc_hw_cfg_t *hw, erpc_cmd_def_t *cmd_rev)
|
|||
return ERPC_NO_ERROR;
|
||||
}
|
||||
|
||||
u32 erpc_rev_package(erpc_hw_cfg_t *hw)
|
||||
{
|
||||
for (int i = 0; i < MAX_REC_CMD_CACHE_NUM; i++)
|
||||
{
|
||||
if (hw->rec_cache[i].state == ERPC_CMD_WAIT_SERVER_DEAL)
|
||||
{
|
||||
u32 erpc_rev_package(erpc_hw_cfg_t *hw) {
|
||||
for (int i = 0; i < MAX_REC_CMD_CACHE_NUM; i++) {
|
||||
if (hw->rec_cache[i].state == ERPC_CMD_WAIT_SERVER_DEAL) {
|
||||
erpc_cmd_def_t *cmd = (erpc_cmd_def_t *)&hw->rec_cache[i].data[0];
|
||||
// 检查crc
|
||||
u8 crc_result = cmd_check_crc16(cmd);
|
||||
// 丢弃错误数据包
|
||||
if (crc_result)
|
||||
{
|
||||
if (crc_result) {
|
||||
hw->rec_cache[i].state = ERPC_CMD_NO_ERROR;
|
||||
continue;
|
||||
}
|
||||
// 丢弃不是本地数据包
|
||||
if (cmd->head.dest_id != hw->local_id)
|
||||
{
|
||||
|
||||
if (cmd->head.dest_id != hw->local_id) {
|
||||
hw->rec_cache[i].state = ERPC_CMD_NO_ERROR;
|
||||
continue;
|
||||
}
|
||||
// 处理数据包
|
||||
switch (cmd->head.type)
|
||||
{
|
||||
switch (cmd->head.type) {
|
||||
case PACKAGE_TYPE_CMD_REQ:
|
||||
printf("{REQ}\n");
|
||||
erpc_send_ack_package(hw, cmd);
|
||||
hw->rec_cache[i].state = ERPC_CMD_WAIT_TASK_DEAL;
|
||||
break;
|
||||
case PACKAGE_TYPE_CMD_REQ_ACK:
|
||||
case PACKAGE_TYPE_CMD_RESP_ACK:
|
||||
printf("{ACK}\n");
|
||||
erpc_rev_ack_package(hw, cmd);
|
||||
hw->rec_cache[i].state = ERPC_CMD_NO_ERROR;
|
||||
break;
|
||||
|
@ -302,36 +338,35 @@ u32 erpc_rev_package(erpc_hw_cfg_t *hw)
|
|||
erpc_rev_repeat_package(hw, cmd);
|
||||
hw->rec_cache[i].state = ERPC_CMD_NO_ERROR;
|
||||
break;
|
||||
case PACKAGE_TYPE_CMD_RESP:
|
||||
printf("{RESP}\n");
|
||||
erpc_rev_resp_package(hw, cmd);
|
||||
hw->rec_cache[i].state = ERPC_CMD_RESP_OK;
|
||||
break;
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ERPC_NO_ERROR;
|
||||
}
|
||||
void erpc_rev_package_core()
|
||||
{
|
||||
|
||||
void erpc_rev_package_core() {
|
||||
list_t *list = &erpc_hw;
|
||||
if (list->data == NULL)
|
||||
{
|
||||
if (list->data == NULL) {
|
||||
return;
|
||||
}
|
||||
while (list != NULL)
|
||||
{
|
||||
while (list != NULL) {
|
||||
erpc_hw_cfg_t *hw = (erpc_hw_cfg_t *)list->data;
|
||||
erpc_rev_package(hw);
|
||||
list = list->next;
|
||||
}
|
||||
}
|
||||
u32 erpc_set_rev_cahce(u8 hw, u8 *data, u16 len)
|
||||
{
|
||||
u32 erpc_set_rev_cahce(u8 hw, u8 *data, u16 len) {
|
||||
erpc_hw_cfg_t *hw_cfg = erpc_hw_get(hw);
|
||||
CHECK_IF_ERROR(hw_cfg == NULL, ERPC_ERR_NOFOND_HW);
|
||||
for (int i = 0; i < MAX_REC_CMD_CACHE_NUM; i++)
|
||||
{
|
||||
if (hw_cfg->rec_cache[i].state == ERPC_CMD_NO_ERROR)
|
||||
{
|
||||
for (int i = 0; i < MAX_REC_CMD_CACHE_NUM; i++) {
|
||||
if (hw_cfg->rec_cache[i].state == ERPC_CMD_NO_ERROR) {
|
||||
printf("set rev cache %d\r\n", i);
|
||||
memcpy(hw_cfg->rec_cache[i].data, data, len);
|
||||
hw_cfg->rec_cache[i].state = ERPC_CMD_WAIT_SERVER_DEAL;
|
||||
|
@ -340,52 +375,40 @@ u32 erpc_set_rev_cahce(u8 hw, u8 *data, u16 len)
|
|||
}
|
||||
return ERPC_ERR_REC_CACHE_FULL;
|
||||
}
|
||||
void erpc_send_deal_core()
|
||||
{
|
||||
void erpc_send_deal_core() {
|
||||
list_t *list = &erpc_hw;
|
||||
if (list->data == NULL)
|
||||
{
|
||||
if (list->data == NULL) {
|
||||
return;
|
||||
}
|
||||
while (list != NULL)
|
||||
{
|
||||
while (list != NULL) {
|
||||
erpc_hw_cfg_t *hw = (erpc_hw_cfg_t *)list->data;
|
||||
erpc_send_data(hw);
|
||||
}
|
||||
if (erpc_sleep_tick != NULL)
|
||||
{
|
||||
if (erpc_sleep_tick != NULL) {
|
||||
erpc_sleep_tick(1);
|
||||
}
|
||||
}
|
||||
|
||||
void erpc_rev_deal_core()
|
||||
{
|
||||
void erpc_rev_deal_core() {
|
||||
list_t *list = &erpc_hw;
|
||||
if (list->data == NULL)
|
||||
{
|
||||
if (list->data == NULL) {
|
||||
return;
|
||||
}
|
||||
while (list != NULL)
|
||||
{
|
||||
while (list != NULL) {
|
||||
erpc_hw_cfg_t *hw = (erpc_hw_cfg_t *)list->data;
|
||||
{ // 遍历接收缓存
|
||||
for (int j = 0; j < MAX_REC_CMD_CACHE_NUM; j++)
|
||||
{
|
||||
for (int j = 0; j < MAX_REC_CMD_CACHE_NUM; j++) {
|
||||
{ // 发现等待处理的数据包
|
||||
if (hw->rec_cache[j].state != ERPC_CMD_WAIT_TASK_DEAL)
|
||||
{
|
||||
if (hw->rec_cache[j].state != ERPC_CMD_WAIT_TASK_DEAL) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
{ // 多线程下,抢占处理权限
|
||||
hw->deal_lock();
|
||||
if (hw->rec_cache[j].state == ERPC_CMD_WAIT_TASK_DEAL)
|
||||
{
|
||||
if (hw->rec_cache[j].state == ERPC_CMD_WAIT_TASK_DEAL) {
|
||||
// 获取指令的处理权限
|
||||
hw->rec_cache[j].state = ERPC_CMD_WAIT_TASK_DEAL_FINISH;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// 已经有线程抢先执行了
|
||||
hw->deal_unlock();
|
||||
continue;
|
||||
|
@ -396,21 +419,19 @@ void erpc_rev_deal_core()
|
|||
// 搜索指令列表
|
||||
list_t *cmd_list = &hw->cmd_list;
|
||||
// 链表是空的
|
||||
if (cmd_list->data == NULL)
|
||||
{
|
||||
if (cmd_list->data == NULL) {
|
||||
continue;
|
||||
}
|
||||
// 获取指令指针
|
||||
erpc_cmd_def_t *cmd_def = (erpc_cmd_def_t *)hw->rec_cache[j].data;
|
||||
while (cmd_list != NULL)
|
||||
{ // 搜索指令列表
|
||||
while (cmd_list != NULL) { // 搜索指令列表
|
||||
erpc_cmd_list_t *cmd_obj = (erpc_cmd_list_t *)cmd_list->data;
|
||||
if (cmd_obj->cmd == cmd_def->head.port)
|
||||
{
|
||||
if (cmd_obj->handle != NULL)
|
||||
{
|
||||
if (cmd_obj->cmd == cmd_def->head.port) {
|
||||
if (cmd_obj->handle != NULL) {
|
||||
// 指令调用
|
||||
cmd_obj->handle(hw->ord, cmd_def->head.src_id, cmd_def->head.dest_id, cmd_def->head.port, cmd_def->data, cmd_def->head.msg_len);
|
||||
cmd_obj->handle(hw->ord, cmd_def->head.src_id,
|
||||
cmd_def->head.dest_id, cmd_def->head.port,
|
||||
cmd_def->data, cmd_def->head.msg_len);
|
||||
}
|
||||
|
||||
break;
|
||||
|
@ -424,8 +445,7 @@ void erpc_rev_deal_core()
|
|||
}
|
||||
list = list->next;
|
||||
}
|
||||
if (erpc_sleep_tick != NULL)
|
||||
{
|
||||
if (erpc_sleep_tick != NULL) {
|
||||
erpc_sleep_tick(1);
|
||||
}
|
||||
}
|
64
erpc_core.h
64
erpc_core.h
|
@ -1,15 +1,15 @@
|
|||
#ifndef ERPC_CORE_H_
|
||||
#define ERPC_CORE_H_
|
||||
|
||||
#include "config.h"
|
||||
#include "list.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "list.h"
|
||||
#define ERPC_VERSION "0.0.1"
|
||||
|
||||
typedef enum package_type
|
||||
{
|
||||
typedef enum package_type {
|
||||
PACKAGE_TYPE_DEV_POWER_ON = 0,
|
||||
PACKAGE_TYPE_CMD_REQ,
|
||||
PACKAGE_TYPE_CMD_REQ_ACK,
|
||||
|
@ -23,17 +23,15 @@ typedef enum package_type
|
|||
// 广播ID
|
||||
#define ERPC_BOARDCAST_ID 0xff
|
||||
|
||||
typedef struct erpc_cmd_head_t
|
||||
{
|
||||
typedef struct erpc_cmd_head_t {
|
||||
u8 src_id;
|
||||
u8 dest_id;
|
||||
package_type type;
|
||||
u8 type; // package_type
|
||||
u16 port; // 指令号,指令号=端口号
|
||||
u8 msg_len;
|
||||
} erpc_cmd_head_t;
|
||||
|
||||
typedef struct erpc_cmd_def_t
|
||||
{
|
||||
typedef struct erpc_cmd_def_t {
|
||||
erpc_cmd_head_t head;
|
||||
u8 crc_16[2];
|
||||
u8 *data;
|
||||
|
@ -42,10 +40,11 @@ typedef struct erpc_cmd_def_t
|
|||
#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
|
||||
{
|
||||
#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
|
||||
|
@ -56,28 +55,26 @@ typedef enum erpc_status
|
|||
ERPC_CMD_SEND_REPEAT, // 发送指令重发
|
||||
ERPC_CMD_SEND_OK, // 发送指令成功
|
||||
ERPC_CMD_DEST_BUSY, // 目标设备忙
|
||||
ERPC_CMD_RESP_OK, // 指令响应成功
|
||||
} erpc_status;
|
||||
|
||||
typedef struct erpc_data_cache_t
|
||||
{
|
||||
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 u32 (*erpc_cmd_handle_t)(u8 src_id, u8 dest_id, u16 port, u8 *data,
|
||||
u16 len);
|
||||
|
||||
typedef struct erpc_cmd_list_t
|
||||
{
|
||||
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 enum ERPC_ROLE
|
||||
{
|
||||
typedef enum ERPC_ROLE {
|
||||
ERPC_ROLE_MASTER,
|
||||
ERPC_ROLE_SLAVE,
|
||||
} ERPC_ROLE;
|
||||
typedef struct erpc_hw_cfg_t
|
||||
{
|
||||
typedef struct erpc_hw_cfg_t {
|
||||
u8 ord;
|
||||
u8 local_id;
|
||||
u8 (*write)(u8 *data, u16 len);
|
||||
|
@ -91,18 +88,15 @@ typedef struct erpc_hw_cfg_t
|
|||
} erpc_hw_cfg_t;
|
||||
|
||||
#define CHECK_IF_ERROR(condition, error) \
|
||||
if (condition) \
|
||||
{ \
|
||||
if (condition) { \
|
||||
return error; \
|
||||
}
|
||||
#define CHECK_IF_ERROR_AND_DO(condition, error, action) \
|
||||
if (condition) \
|
||||
{ \
|
||||
if (condition) { \
|
||||
action; \
|
||||
return error; \
|
||||
}
|
||||
typedef enum erpc_error
|
||||
{
|
||||
typedef enum erpc_error {
|
||||
ERPC_NO_ERROR = 0,
|
||||
ERPC_ERR_NOFOND_HW,
|
||||
ERPC_ERR_SEND_CACHE_FULL,
|
||||
|
@ -110,6 +104,7 @@ typedef enum erpc_error
|
|||
ERPC_ERR_HW_SEND_FAIL, // 硬件层发来的错误 erpc_hw_cfg_t.write返回错误
|
||||
ERPC_ERR_DEST_BUSY,
|
||||
ERPC_ERR_DEST_NO_RESPONSE, // 目标设备无响应
|
||||
ERPC_ERR_DEST_NO_REPLY, // 目标设备无回复或者回复超时
|
||||
ERPC_ERR_MALLOC_ERROR, // malloc失败,内存不足
|
||||
ERPC_ERR_HW_EXIST, // 硬件已经存在
|
||||
ERPC_ERR_HW_NOT_EXIST, // 硬件不存在
|
||||
|
@ -125,6 +120,17 @@ 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);
|
||||
/**
|
||||
* 发送消息并等待回复
|
||||
*/
|
||||
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);
|
||||
#endif /* ERPC_CORE_H_ */
|
BIN
erpc_core.o
BIN
erpc_core.o
Binary file not shown.
11
port_self.c
11
port_self.c
|
@ -14,9 +14,17 @@ void hello_cat()
|
|||
{
|
||||
printf("Hello Cat!\n");
|
||||
}
|
||||
|
||||
void hello_dog(u8 hw, u8 src_id, u8 dest_id, u16 port, u8 *data, u16 len)
|
||||
{
|
||||
printf("Hello Dog!\n");
|
||||
u8 data_out[10] = "12345";
|
||||
erpc_replay(hw, dest_id, port, data_out, sizeof(data_out));
|
||||
}
|
||||
erpc_cmd_list_t erpc_cmd_list[] = {
|
||||
{.cmd = 0x01, hellworld},
|
||||
{.cmd = 0x02, hello_cat},
|
||||
{.cmd = 0x03, hello_dog},
|
||||
};
|
||||
|
||||
HANDLE deal_lock;
|
||||
|
@ -93,8 +101,11 @@ DWORD WINAPI test_task(LPVOID lpParam)
|
|||
{
|
||||
// printf("[test_task]send\n");
|
||||
u32 ret = erpc_send(self_hw_cfg.ord, self_hw_cfg.local_id, 0x01, NULL, 0);
|
||||
printf("[test_task]send ret:%d\n", ret);
|
||||
ret = erpc_send(self_hw_cfg.ord, self_hw_cfg.local_id, 0x02, NULL, 0);
|
||||
printf("[test_task]send ret:%d\n", ret);
|
||||
ret = erpc_send_wait_reply(self_hw_cfg.ord, self_hw_cfg.local_id, 0x03, NULL, 0, data, NULL, 1000);
|
||||
printf("[test_task]send ret:%d\n", ret);
|
||||
sys_sleep(1000);
|
||||
}
|
||||
}
|
||||
|
|
BIN
port_self.exe
BIN
port_self.exe
Binary file not shown.
BIN
port_self.o
BIN
port_self.o
Binary file not shown.
Loading…
Reference in New Issue