Add first version of the msg test.
[bertos.git] / bertos / kern / msg_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2008 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999, 2000, 2001 Bernie Innocenti <bernie@codewiz.org>
31  * -->
32  *
33  * \brief Message test.
34  *
35  * \version $Id$
36  *
37  * \author Daniele Basile <asterix@develer.com>
38  */
39
40 #include <cfg/cfg_timer.h>
41 #include <cfg/debug.h>
42 #include <cfg/test.h>
43 #include <cfg/compiler.h>
44
45 #include <kern/msg.h>
46 #include <kern/proc.h>
47 #include <kern/irq.h>
48
49 #include <drv/timer.h>
50
51
52 // Our message port.
53 static MsgPort test_port;
54
55 // A test message with two parameters and a result.
56 typedef struct
57 {
58         Msg msg;
59
60         int x, y;
61         int result;
62 } TestMsg;
63
64
65 // Receive messages and do something boring with them.
66 static void receiver_proc(void)
67 {
68         TestMsg *rec_msg;
69         for (;;)
70         {
71             kprintf("Proc[%d]..\n", 1);
72                 sig_wait(SIG_SINGLE);
73             kprintf("Proc[%d] get message\n", 1);
74         rec_msg = containerof(msg_get(&test_port), TestMsg, msg);
75         // Do something with the message
76         rec_msg->result = rec_msg->x + rec_msg->y;
77             kprintf("Proc[%d] process message x[%d],y[%d],res[%d]\n", 1, rec_msg->x, rec_msg->y, rec_msg->result);
78         msg_reply(&rec_msg->msg);
79             kprintf("Proc[%d] reply\n", 1);
80         }
81 }
82
83 static cpu_stack_t receiver_stack[CONFIG_KERN_MINSTACKSIZE / sizeof(cpu_stack_t)];
84
85 /**
86  * Run signal test
87  */
88 int msg_testRun(void)
89 {
90         kprintf("Run Message test..\n");
91
92         MsgPort test_reply_port;
93         TestMsg msg1;
94         TestMsg *reply;
95
96     struct Process *recv = proc_new(receiver_proc, NULL, sizeof(receiver_stack), receiver_stack);
97         msg_initPort(&test_port, event_createSignal(recv, SIG_SINGLE));
98         msg_initPort(&test_reply_port, event_createSignal(proc_current(), SIG_SINGLE));
99
100         // Fill-in first message and send it out.
101         msg1.x = 3;
102         msg1.y = 2;
103         kprintf("invio il msg..\n");
104         msg1.msg.replyPort = &test_reply_port;
105         msg_put(&test_port, &msg1.msg);
106
107         // Wait for a reply...
108         kprintf("prima sig wait..\n");
109         sig_wait(SIG_SINGLE);
110         kprintf("dopo sig wait..\n");
111     reply = containerof(msg_get(&test_reply_port), TestMsg, msg);
112         ASSERT(reply != NULL);
113         ASSERT(reply->result == 5);
114
115         return 0;
116 }
117
118 int msg_testSetup(void)
119 {
120         kdbg_init();
121
122         #if CONFIG_KERN_PREEMPT
123                 kprintf("Init Interrupt (preempt mode)..");
124                 irq_init();
125                 kprintf("Done.\n");
126         #endif
127
128         kprintf("Init Timer..");
129         timer_init();
130         kprintf("Done.\n");
131
132         kprintf("Init Process..");
133         proc_init();
134         kprintf("Done.\n");
135         return 0;
136 }
137
138 int msg_testTearDown(void)
139 {
140         kputs("TearDown Message test.\n");
141         return 0;
142 }
143
144 TEST_MAIN(msg);