From c19caf5bc5f22a8b7862d8c3eeea62dc44a0c86e Mon Sep 17 00:00:00 2001 From: asterix Date: Thu, 8 Jan 2009 14:17:52 +0000 Subject: [PATCH] Add first version of the msg test. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@2140 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/kern/msg_test.c | 144 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 bertos/kern/msg_test.c diff --git a/bertos/kern/msg_test.c b/bertos/kern/msg_test.c new file mode 100644 index 00000000..f8258a63 --- /dev/null +++ b/bertos/kern/msg_test.c @@ -0,0 +1,144 @@ +/** + * \file + * + * + * \brief Message test. + * + * \version $Id$ + * + * \author Daniele Basile + */ + +#include +#include +#include +#include + +#include +#include +#include + +#include + + +// Our message port. +static MsgPort test_port; + +// A test message with two parameters and a result. +typedef struct +{ + Msg msg; + + int x, y; + int result; +} TestMsg; + + +// Receive messages and do something boring with them. +static void receiver_proc(void) +{ + TestMsg *rec_msg; + for (;;) + { + kprintf("Proc[%d]..\n", 1); + sig_wait(SIG_SINGLE); + kprintf("Proc[%d] get message\n", 1); + rec_msg = containerof(msg_get(&test_port), TestMsg, msg); + // Do something with the message + rec_msg->result = rec_msg->x + rec_msg->y; + kprintf("Proc[%d] process message x[%d],y[%d],res[%d]\n", 1, rec_msg->x, rec_msg->y, rec_msg->result); + msg_reply(&rec_msg->msg); + kprintf("Proc[%d] reply\n", 1); + } +} + +static cpu_stack_t receiver_stack[CONFIG_KERN_MINSTACKSIZE / sizeof(cpu_stack_t)]; + +/** + * Run signal test + */ +int msg_testRun(void) +{ + kprintf("Run Message test..\n"); + + MsgPort test_reply_port; + TestMsg msg1; + TestMsg *reply; + + struct Process *recv = proc_new(receiver_proc, NULL, sizeof(receiver_stack), receiver_stack); + msg_initPort(&test_port, event_createSignal(recv, SIG_SINGLE)); + msg_initPort(&test_reply_port, event_createSignal(proc_current(), SIG_SINGLE)); + + // Fill-in first message and send it out. + msg1.x = 3; + msg1.y = 2; + kprintf("invio il msg..\n"); + msg1.msg.replyPort = &test_reply_port; + msg_put(&test_port, &msg1.msg); + + // Wait for a reply... + kprintf("prima sig wait..\n"); + sig_wait(SIG_SINGLE); + kprintf("dopo sig wait..\n"); + reply = containerof(msg_get(&test_reply_port), TestMsg, msg); + ASSERT(reply != NULL); + ASSERT(reply->result == 5); + + return 0; +} + +int msg_testSetup(void) +{ + kdbg_init(); + + #if CONFIG_KERN_PREEMPT + kprintf("Init Interrupt (preempt mode).."); + irq_init(); + kprintf("Done.\n"); + #endif + + kprintf("Init Timer.."); + timer_init(); + kprintf("Done.\n"); + + kprintf("Init Process.."); + proc_init(); + kprintf("Done.\n"); + return 0; +} + +int msg_testTearDown(void) +{ + kputs("TearDown Message test.\n"); + return 0; +} + +TEST_MAIN(msg); -- 2.25.1