测试用例
typedef struct rz_mq_magic_test_def rz_mq_magic_test_t;
struct rz_mq_magic_test_def{
rt_thread_t mq_tid;
pub_mq_magic_t *pub_mq_magic;
sub_mq_magic_t *sub_mq_magic;
topic_t pub_topic;
topic_t sub_topic;
bool (*init)(rz_mq_magic_test_t *t);
};
static bool rz_mq_magic_test_init(rz_mq_magic_test_t *t)
{
rz_mq_magic_t *prz_mq_magic = rz_mq_magic_obj_get();
char name[60];
t->mq_tid = RT_NULL;
t->pub_mq_magic = rt_malloc(sizeof(mq_magic_t));
if (RT_NULL == t->pub_mq_magic) {
rt_kprintf("%s(),line %d, malloc failed\r\n", __FUNCTION__, __LINE__);
return false;
}
memset(name, 0, 20);
sprintf(name, "mq_magic_pub_%x", t->pub_mq_magic);
if (!mq_magic_user_create(t->pub_mq_magic, name, RT_IPC_FLAG_FIFO)) {
rt_kprintf("%s():mq_magic_user_create %s failed.\r\n", __FUNCTION__, name);
rt_free(t->pub_mq_magic);
return false;
}
if (RT_NULL != t->pub_mq_magic) {
if (!prz_mq_magic->publisher_register(prz_mq_magic, t->pub_mq_magic, t->pub_topic)) {
rt_kprintf("%s():publisher_register %s failed.\r\n", __FUNCTION__, name);
rt_free(t->pub_mq_magic);
return false;
}
}
t->sub_mq_magic = rt_malloc(sizeof(mq_magic_t));
if (RT_NULL == t->sub_mq_magic) {
rt_kprintf("%s(),line %d, malloc failed\r\n", __FUNCTION__, __LINE__);
rt_free(t->pub_mq_magic);
return false;
}
memset(name, 0, 20);
sprintf(name, "mq_magic_sub_%x", t->pub_mq_magic);
if (!mq_magic_user_create(t->sub_mq_magic, name, RT_IPC_FLAG_FIFO)) {
rt_kprintf("%s():mq_magic_user_create %s failed.\r\n", __FUNCTION__, name);
rt_free(t->pub_mq_magic);
rt_free(t->sub_mq_magic);
return false;
}
if (RT_NULL != t->sub_mq_magic) {
if (!prz_mq_magic->subscriber_register(prz_mq_magic, t->sub_mq_magic, t->sub_topic)) {
rt_kprintf("%s():publisher_register %s failed.\r\n", __FUNCTION__, name);
rt_free(t->pub_mq_magic);
rt_free(t->sub_mq_magic);
return false;
}
}
return true;
}
static void mq_thread_A_entry(void *parameter)
{
rz_mq_magic_test_t *t = (rz_mq_magic_test_t *)parameter;
mq_magic_frame_t buff;
buff.head = 0x0A050A05;
int count = 0;
while(1)
{
count++;
if (RT_EOK == rz_mq_magic_recv((mq_magic_t *)t->sub_mq_magic, &buff, RT_WAITING_NO)) {
rt_kprintf("%s(): rz_mq_magic_recv success.value = %d\r\n", __FUNCTION__, buff.value);
}
if (count > 10) {
continue;
}
buff.value = count;
if (RT_EOK == rz_mq_magic_send((mq_magic_t *)t->pub_mq_magic, &buff)) {
rt_kprintf("%s(): rz_mq_magic_send success.value = %d\r\n", __FUNCTION__, buff.value);
}
rt_thread_delay(100);
}
}
static void mq_thread_B_entry(void *parameter)
{
rz_mq_magic_test_t *t = (rz_mq_magic_test_t *)parameter;
mq_magic_frame_t buff;
buff.head = 0x55AA55AA;
int count = 0;
while(1)
{
if (RT_EOK == rz_mq_magic_recv((mq_magic_t *)t->sub_mq_magic, &buff, RT_WAITING_NO)) {
rt_kprintf("%s(): rz_mq_magic_recv success.value = %d\r\n", __FUNCTION__, buff.value);
count++;
if (count == 6) {
buff.value = 100 + count;
if (RT_EOK == rz_mq_magic_send((mq_magic_t *)t->pub_mq_magic, &buff)) {
rt_kprintf("%s(): rz_mq_magic_send success.value = %d\r\n", __FUNCTION__, buff.value);
}
}
}
}
}
static void mq_thread_C_entry(void *parameter)
{
rz_mq_magic_test_t *t = (rz_mq_magic_test_t *)parameter;
mq_magic_frame_t buff;
buff.head = 0x0A050A05;
while(1)
{
if (RT_EOK == rz_mq_magic_recv((mq_magic_t *)t->sub_mq_magic, &buff, RT_WAITING_NO)) {
rt_kprintf("%s(): rz_mq_magic_recv success.value = %d\r\n", __FUNCTION__, buff.value);
}
}
}
int mq_magic_test(void)
{
#define RTHREAD_PRIORITY_MQ_MAGIC_TEST 31
#define THREAD_STACK_SIZE_MQ_MAGIC_TEST 1024
#define THREAD_TIMESLICE_MQ_MAGIC_TEST 5
rz_mq_magic_init();
rz_mq_magic_test_t *prz_mq_magic_A = (rz_mq_magic_test_t *)rt_malloc(sizeof(*prz_mq_magic_A));
if (RT_NULL != prz_mq_magic_A) {
prz_mq_magic_A->init = rz_mq_magic_test_init;
prz_mq_magic_A->pub_topic = 12;
prz_mq_magic_A->sub_topic = 13;
prz_mq_magic_A->init(prz_mq_magic_A);
} else {
rt_kprintf("%s(),line %d, malloc failed\r\n", __FUNCTION__, __LINE__);
return 0;
}
prz_mq_magic_A->mq_tid =
rt_thread_create("mq_thread_A",
mq_thread_A_entry, prz_mq_magic_A,
THREAD_STACK_SIZE_MQ_MAGIC_TEST,
RTHREAD_PRIORITY_MQ_MAGIC_TEST,
THREAD_TIMESLICE_MQ_MAGIC_TEST);
if (RT_NULL != prz_mq_magic_A->mq_tid) {
rt_thread_startup(prz_mq_magic_A->mq_tid);
}
rz_mq_magic_test_t *prz_mq_magic_B = (rz_mq_magic_test_t *)rt_malloc(sizeof(*prz_mq_magic_B));
if (RT_NULL != prz_mq_magic_B) {
prz_mq_magic_B->init = rz_mq_magic_test_init;
prz_mq_magic_B->pub_topic = 13;
prz_mq_magic_B->sub_topic = 12;
prz_mq_magic_B->init(prz_mq_magic_B);
} else {
rt_kprintf("%s(),line %d, malloc failed\r\n", __FUNCTION__, __LINE__);
return 0;
}
prz_mq_magic_B->mq_tid =
rt_thread_create("mq_thread_B",
mq_thread_B_entry, prz_mq_magic_B,
THREAD_STACK_SIZE_MQ_MAGIC_TEST,
RTHREAD_PRIORITY_MQ_MAGIC_TEST,
THREAD_TIMESLICE_MQ_MAGIC_TEST);
if (RT_NULL != prz_mq_magic_B->mq_tid) {
rt_thread_startup(prz_mq_magic_B->mq_tid);
}
rz_mq_magic_test_t *prz_mq_magic_C = (rz_mq_magic_test_t *)rt_malloc(sizeof(*prz_mq_magic_C));
if (RT_NULL != prz_mq_magic_C) {
prz_mq_magic_C->init = rz_mq_magic_test_init;
prz_mq_magic_C->pub_topic = 14;
prz_mq_magic_C->sub_topic = 13;
prz_mq_magic_C->init(prz_mq_magic_C);
} else {
rt_kprintf("%s(),line %d, malloc failed\r\n", __FUNCTION__, __LINE__);
return 0;
}
prz_mq_magic_C->mq_tid =
rt_thread_create("mq_thread_C",
mq_thread_C_entry, prz_mq_magic_C,
THREAD_STACK_SIZE_MQ_MAGIC_TEST,
RTHREAD_PRIORITY_MQ_MAGIC_TEST,
THREAD_TIMESLICE_MQ_MAGIC_TEST);
if (RT_NULL != prz_mq_magic_C->mq_tid) {
rt_thread_startup(prz_mq_magic_C->mq_tid);
}
return 0;
}
MSH_CMD_EXPORT(mq_magic_test, RT-Thread first led sample);