Update preset.
[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  *
36  * \author Daniele Basile <asterix@develer.com>
37  *
38  * $test$: cp bertos/cfg/cfg_proc.h $cfgdir/
39  * $test$: echo  "#undef CONFIG_KERN" >> $cfgdir/cfg_proc.h
40  * $test$: echo "#define CONFIG_KERN 1" >> $cfgdir/cfg_proc.h
41  * $test$: cp bertos/cfg/cfg_signal.h $cfgdir/
42  * $test$: echo  "#undef CONFIG_KERN_SIGNALS" >> $cfgdir/cfg_signal.h
43  * $test$: echo "#define CONFIG_KERN_SIGNALS 1" >> $cfgdir/cfg_signal.h
44  */
45
46 #include "cfg/cfg_timer.h"
47 #include <cfg/debug.h>
48 #include <cfg/test.h>
49 #include <cfg/compiler.h>
50
51 #include <kern/msg.h>
52 #include <kern/proc.h>
53 #include <kern/signal.h>
54
55 #include <mware/event.h>
56
57 #include <drv/timer.h>
58
59 /*
60  * In the nightly build test, signals are disables, so this
61  * code won't compile.
62  * Since this code is used when we run "make check" it will be
63  * compiled and therefor tested there.
64  */
65 #if CONFIG_KERN_SIGNALS
66
67
68 // Global settings for the test.
69 #define MAX_GLOBAL_COUNT            11040
70 #define TEST_TIME_OUT_MS             5000
71 #define DELAY                           5
72
73 // Settings for the test message.
74 //Process 0
75 #define INC_PROC_T0                     1
76 #define DELAY_PROC_T0   INC_PROC_T0*DELAY
77 //Process 1
78 #define INC_PROC_T1                     3
79 #define DELAY_PROC_T1   INC_PROC_T1*DELAY
80 //Process 2
81 #define INC_PROC_T2                     5
82 #define DELAY_PROC_T2   INC_PROC_T2*DELAY
83 //Process 3
84 #define INC_PROC_T3                     7
85 #define DELAY_PROC_T3   INC_PROC_T3*DELAY
86 //Process 4
87 #define INC_PROC_T4                    11
88 #define DELAY_PROC_T4   INC_PROC_T4*DELAY
89 //Process 5
90 #define INC_PROC_T5                    13
91 #define DELAY_PROC_T5   INC_PROC_T5*DELAY
92
93 /*
94  * These macros generate the code needed to create the test process functions.
95  */
96 #define RECV_PROC(num, sig) \
97 static NORETURN void receiver_proc##num(void) \
98 { \
99         TestMsg *rec_msg; \
100         for(;;) \
101         { \
102                 sig_wait(sig); \
103                 kprintf("Proc[%d]..get message\n", num); \
104                 rec_msg = containerof(msg_get(&test_port##num), TestMsg, msg); \
105                 timer_delay(rec_msg->delay); \
106                 rec_msg->result += rec_msg->val; \
107                 kprintf("Proc[%d]..process message val[%d],delay[%d],res[%d]\n", num, rec_msg->val, rec_msg->delay, rec_msg->result); \
108                 msg_reply(&rec_msg->msg); \
109                 process_num++; \
110                 kprintf("Proc[%d] reply\n", num); \
111         } \
112 }
113
114 #define SEND_MSG(num) \
115         do { \
116                 kprintf("Main send message to proc[%d]\n", num); \
117                 msg##num.msg.replyPort = &test_portMain; \
118                 msg_put(&test_port##num, &msg##num.msg); \
119         } while(0)
120
121 #define RECV_STACK(num) PROC_DEFINE_STACK(receiver_stack##num, KERN_MINSTACKSIZE * 2)
122 #define RECV_INIT_PROC(num) proc_new(receiver_proc##num, NULL, sizeof(receiver_stack##num), receiver_stack##num)
123 #define RECV_INIT_MSG(num, proc, sig) msg_initPort(&test_port##num, event_createSignal(proc, sig))
124
125 // A test message with the parameters and a result.
126 typedef struct
127 {
128         Msg msg;
129
130         int val;
131         int delay;
132         int result;
133 } TestMsg;
134
135 // Global count to check if the test is going ok.
136 static int count = 0;
137 static int process_num;
138
139 // Our message port.
140 static MsgPort test_port0;
141 static MsgPort test_port1;
142 static MsgPort test_port2;
143 static MsgPort test_port3;
144 static MsgPort test_port4;
145 static MsgPort test_port5;
146
147 /*
148  * Generate the process to test message.
149  */
150 RECV_PROC(0, SIG_USER0)
151 RECV_PROC(1, SIG_USER1)
152 RECV_PROC(2, SIG_USER2)
153 RECV_PROC(3, SIG_USER3)
154 RECV_PROC(4, SIG_SYSTEM5)
155 RECV_PROC(5, SIG_SYSTEM6)
156 /*
157  * These signal are already use from
158  * main process and the sig_waitWithTimeout functions, so we don't
159  * use it.
160  *
161  * RECV_PROC(6, SIG_SINGLE)
162  * RECV_PROC(7, SIG_TIMEOUT)
163  */
164
165 RECV_STACK(0);
166 RECV_STACK(1);
167 RECV_STACK(2);
168 RECV_STACK(3);
169 RECV_STACK(4);
170 RECV_STACK(5);
171
172 /*
173  * Help function to fill the message to send
174  */
175 static void fill_msg(TestMsg *msg, int val, int delay, int res)
176 {
177         msg->val = val;
178         msg->delay = delay;
179         msg->result = res;
180 }
181
182 /**
183  * Run signal test
184  */
185 int msg_testRun(void)
186 {
187         MsgPort test_portMain;
188         TestMsg msg0;
189         TestMsg msg1;
190         TestMsg msg2;
191         TestMsg msg3;
192         TestMsg msg4;
193         TestMsg msg5;
194         TestMsg *reply;
195
196         // Allocate and start the test process
197     struct Process *recv0 = RECV_INIT_PROC(0);
198     struct Process *recv1 = RECV_INIT_PROC(1);
199     struct Process *recv2 = RECV_INIT_PROC(2);
200     struct Process *recv3 = RECV_INIT_PROC(3);
201     struct Process *recv4 = RECV_INIT_PROC(4);
202     struct Process *recv5 = RECV_INIT_PROC(5);
203
204         kprintf("Run Message test..\n");
205
206         // Init port and message
207     RECV_INIT_MSG(Main, proc_current(), SIG_SINGLE);
208     RECV_INIT_MSG(0, recv0, SIG_USER0);
209     RECV_INIT_MSG(1, recv1, SIG_USER1);
210     RECV_INIT_MSG(2, recv2, SIG_USER2);
211     RECV_INIT_MSG(3, recv3, SIG_USER3);
212     RECV_INIT_MSG(4, recv4, SIG_SYSTEM5);
213     RECV_INIT_MSG(5, recv5, SIG_SYSTEM6);
214
215         // Fill-in first message and send it out.
216         fill_msg(&msg0, INC_PROC_T0, DELAY_PROC_T0, 0);
217         fill_msg(&msg1, INC_PROC_T1, DELAY_PROC_T1, 0);
218         fill_msg(&msg2, INC_PROC_T2, DELAY_PROC_T2, 0);
219         fill_msg(&msg3, INC_PROC_T3, DELAY_PROC_T3, 0);
220         fill_msg(&msg4, INC_PROC_T4, DELAY_PROC_T4, 0);
221         fill_msg(&msg5, INC_PROC_T5, DELAY_PROC_T5, 0);
222
223
224         // Send and wait the message
225         for (int i = 0; i < 23; ++i)
226     {
227                 process_num = 0;
228                 SEND_MSG(0);
229                 SEND_MSG(1);
230                 SEND_MSG(2);
231                 SEND_MSG(3);
232                 SEND_MSG(4);
233                 SEND_MSG(5);
234                 while(1)
235                 {
236                         sigmask_t sigs = sig_waitTimeout(SIG_SINGLE, ms_to_ticks(TEST_TIME_OUT_MS));
237                         if (sigs & SIG_SINGLE)
238                         {
239                                 // Wait for a reply...
240                                 while ((reply = (TestMsg *)msg_get(&test_portMain)))
241                                 {
242                                         count += reply->result;
243                                         kprintf("Main recv[%d] count[%d]\n", reply->result, count);
244                                 }
245                         }
246                         
247                         if (process_num == 6)
248                                 break;
249
250                         if (sigs & SIG_TIMEOUT)
251                         {
252                                 kputs("Main: sig timeout\n");
253                                 goto error;
254                         }
255                 }
256     }
257
258         if(count == MAX_GLOBAL_COUNT)
259         {
260                 kprintf("Message test finished..ok!\n");
261                 return 0;
262         }
263         
264 error:
265         kprintf("Message test finished..fail!\n");
266         return -1;
267 }
268
269 int msg_testSetup(void)
270 {
271         kdbg_init();
272
273         kprintf("Init Timer..");
274         timer_init();
275         kprintf("Done.\n");
276
277         kprintf("Init Process..");
278         proc_init();
279         kprintf("Done.\n");
280         return 0;
281 }
282
283 int msg_testTearDown(void)
284 {
285         kputs("TearDown Message test.\n");
286         return 0;
287 }
288
289 TEST_MAIN(msg);
290
291 #endif /* CONFIG_KERN_SIGNALS */