erpc/port_self.c

196 lines
4.7 KiB
C
Raw Normal View History

2024-11-20 15:03:26 +00:00
// #ifndef WINDOWS
// #error "This file is for Windows only"
// #endif
#include "erpc_core.h"
#include "windows.h"
#include <stdio.h>
u32 hellworld(u8 hw, u8 src_id, u8 dest_id, u16 port, u8 *data, u16 len)
2024-11-20 15:03:26 +00:00
{
ERPC_DEBUG("Hello World:\n");
ERPC_DEBUG("len = %d, msg :%s\n", len, data);
// 防止编译警告
UNUSED(hw);
UNUSED(src_id);
UNUSED(dest_id);
UNUSED(port);
UNUSED(data);
UNUSED(len);
return 0;
2024-11-20 15:03:26 +00:00
}
u32 hello_cat(u8 hw, u8 src_id, u8 dest_id, u16 port, u8 *data, u16 len)
2024-11-20 15:03:26 +00:00
{
ERPC_DEBUG("Hello Cat!\n");
// 防止编译警告
UNUSED(hw);
UNUSED(src_id);
UNUSED(dest_id);
UNUSED(port);
UNUSED(data);
UNUSED(len);
return 0;
2024-11-20 15:03:26 +00:00
}
2024-11-21 08:59:31 +00:00
u32 hello_dog(u8 hw, u8 src_id, u8 dest_id, u16 port, u8 *data, u16 len)
2024-11-21 08:59:31 +00:00
{
ERPC_DEBUG("Hello Dog!\n");
// 防止编译警告
2024-11-21 08:59:31 +00:00
u8 data_out[10] = "12345";
u32 ret = erpc_replay(hw, dest_id, port, data_out, sizeof(data_out));
ERPC_DEBUG("erpc_replay ret:%d\n", ret);
// 防止编译警告
UNUSED(ret);
UNUSED(hw);
UNUSED(src_id);
UNUSED(dest_id);
UNUSED(port);
UNUSED(data);
UNUSED(len);
return 0;
2024-11-21 08:59:31 +00:00
}
2024-11-20 15:03:26 +00:00
erpc_cmd_list_t erpc_cmd_list[] = {
{.cmd = 0x01, hellworld},
{.cmd = 0x02, hello_cat},
2024-11-21 08:59:31 +00:00
{.cmd = 0x03, hello_dog},
2024-11-20 15:03:26 +00:00
};
HANDLE deal_lock;
HANDLE send_lock;
static void deal_lock_h()
{
WaitForSingleObject(deal_lock, INFINITE);
}
static void deal_unlock_h()
{
ReleaseMutex(deal_lock);
}
static void send_lock_h()
{
WaitForSingleObject(send_lock, INFINITE);
}
static void send_unlock_h()
{
ReleaseMutex(send_lock);
}
u8 data[300];
static u8 data_write(u8 *data, u16 len)
2024-11-20 15:03:26 +00:00
{
erpc_set_rev_cahce(0, data, len);
return 0;
}
erpc_hw_cfg_t self_hw_cfg = {
.ord = 0,
.deal_lock = deal_lock_h,
.deal_unlock = deal_unlock_h,
.send_lock = send_lock_h,
.send_unlock = send_unlock_h,
.local_id = 0x01,
.write = data_write,
};
void erpc_cmd_config()
{
for (int i = 0; i < (int)sizeof(erpc_cmd_list) / (int)sizeof(erpc_cmd_list[0]); i++)
2024-11-20 15:03:26 +00:00
{
erpc_add_cmd_list(&self_hw_cfg, &erpc_cmd_list[i]);
}
}
void sys_sleep(u32 ms)
{
Sleep(ms);
}
DWORD WINAPI send_task(LPVOID lpParam)
{
UNUSED(lpParam);
2024-11-20 15:03:26 +00:00
while (1)
{
erpc_send_deal_core();
}
}
DWORD WINAPI rev_task(LPVOID lpParam)
{
UNUSED(lpParam);
2024-11-20 15:03:26 +00:00
while (1)
{
// ERPC_DEBUG("rev_task\n");
2024-11-20 16:12:48 +00:00
erpc_rev_package_core();
2024-11-20 15:03:26 +00:00
erpc_rev_deal_core();
}
}
DWORD WINAPI test_task(LPVOID lpParam)
{
UNUSED(lpParam);
ERPC_DEBUG("test_task\n");
2024-11-20 15:03:26 +00:00
while (1)
{
// ERPC_DEBUG("[test_task]send\n");
char msg[] = "miao miao miao\n";
u32 ret = erpc_send(self_hw_cfg.ord, self_hw_cfg.local_id, 0x01, (u8 *)msg, sizeof(msg));
ERPC_DEBUG("[test_task]send ret:%d\n", ret);
2024-11-20 16:26:15 +00:00
ret = erpc_send(self_hw_cfg.ord, self_hw_cfg.local_id, 0x02, NULL, 0);
ERPC_DEBUG("[test_task]send ret:%d\n", ret);
u16 reply_len = 30;
u8 data[reply_len];
ret = erpc_send_wait_reply(self_hw_cfg.ord, self_hw_cfg.local_id, 0x03, NULL, 0, data, &reply_len, 1000);
ERPC_DEBUG("[test_task]send ret:%d\n", ret);
for (int i = 0; i < reply_len; i++)
{
ERPC_DEBUG("%c", data[i]);
}
ERPC_DEBUG("\n");
UNUSED(ret);
2024-11-20 15:03:26 +00:00
sys_sleep(1000);
}
}
int main(int argc, char *argv[])
{
UNUSED(argc);
UNUSED(argv);
2024-11-20 15:03:26 +00:00
u32 ret = 0;
ERPC_DEBUG("erpc_port_self start\n");
2024-11-20 15:03:26 +00:00
ret = erpc_hw_add(&self_hw_cfg);
ERPC_DEBUG("erpc_hw_add ret:%d\n", ret);
2024-11-20 15:03:26 +00:00
CHECK_IF_ERROR(ret, 0);
erpc_cmd_config();
ERPC_DEBUG("erpc_cmd_config ret:%d\n", ret);
2024-11-20 15:03:26 +00:00
erpc_sleep_tick = sys_sleep;
HANDLE threadHandle[4];
2024-11-20 15:03:26 +00:00
deal_lock = CreateMutex(NULL, FALSE, NULL);
if (deal_lock == NULL)
{
ERPC_DEBUG("创建互斥锁失败\n");
2024-11-20 15:03:26 +00:00
return 1;
}
send_lock = CreateMutex(NULL, FALSE, NULL);
if (send_lock == NULL)
{
ERPC_DEBUG("创建互斥锁失败\n");
2024-11-20 15:03:26 +00:00
return 1;
}
threadHandle[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)send_task, NULL, 0, NULL);
threadHandle[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)rev_task, NULL, 0, NULL);
threadHandle[3] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)rev_task, NULL, 0, NULL);
threadHandle[4] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)test_task, NULL, 0, NULL);
2024-11-20 15:03:26 +00:00
WaitForSingleObject(threadHandle[0], INFINITE);
WaitForSingleObject(threadHandle[1], INFINITE);
WaitForSingleObject(threadHandle[2], INFINITE);
WaitForSingleObject(threadHandle[3], INFINITE);
2024-11-20 15:03:26 +00:00
CloseHandle(threadHandle[0]);
CloseHandle(threadHandle[1]);
2024-11-20 16:12:48 +00:00
CloseHandle(threadHandle[2]);
CloseHandle(threadHandle[3]);
2024-11-20 15:03:26 +00:00
return 0;
}