Refactor to use new protocol module and sipo.
[bertos.git] / bertos / kern / signal_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 Signals 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
50 #include <kern/signal.h>
51 #include <kern/proc.h>
52 #include <kern/irq.h>
53
54 #include <drv/timer.h>
55
56 // Set mask with all signal that we want to test.
57 int test_signal[] = {
58         SIG_USER0,
59         SIG_USER1,
60         SIG_USER2,
61         SIG_USER3,
62         SIG_TIMEOUT,
63         SIG_SYSTEM5,
64         SIG_SYSTEM6,
65         SIG_SINGLE
66 };
67
68 // Current signal to send
69 int count = 0;
70
71 sigmask_t sig_to_master;
72 sigmask_t sig_to_slave;
73
74 /*
75  * These macros generate the code needed to create the test process functions.
76  */
77 #define PROC_TEST_SLAVE(index, signal) \
78 static void proc_signalTest##index(void) \
79 { \
80         kputs("> Slave [" #index "]: Wait signal [" #signal "]\n"); \
81         sig_wait(signal); \
82         kputs("> Slave [" #index "]: send signal [" #signal "]\n"); \
83         sig_send(proc_currentUserData(), signal); \
84 }
85
86 #define MAIN_CHECK_SIGNAL(index, slave) \
87         do { \
88                 kprintf("> Main: send signal [%d]\n", test_signal[index]); \
89                 sig_send(slave, test_signal[index]); \
90                 kprintf("> Main: wait signal [%d]\n", test_signal[index]); \
91                 sig_wait(test_signal[index]); \
92                 count++; \
93         } while(0) \
94
95 #if CONFIG_KERN_HEAP
96
97 #define PROC_TEST_SLAVE_INIT(index, master_process) proc_new(proc_signalTest##index, master_process, KERN_MINSTACKSIZE * 2, NULL)
98
99 #else
100
101 #define PROC_TEST_SLAVE_STACK(index) PROC_DEFINE_STACK(proc_signal_test##index##_stack, KERN_MINSTACKSIZE * 2);
102 #define PROC_TEST_SLAVE_INIT(index, master_process) proc_new(proc_signalTest##index, master_process, sizeof(proc_signal_test##index##_stack), proc_signal_test##index##_stack)
103
104 PROC_TEST_SLAVE_STACK(0)
105 PROC_TEST_SLAVE_STACK(1)
106 PROC_TEST_SLAVE_STACK(2)
107 PROC_TEST_SLAVE_STACK(3)
108 PROC_TEST_SLAVE_STACK(4)
109 PROC_TEST_SLAVE_STACK(5)
110 PROC_TEST_SLAVE_STACK(6)
111 PROC_TEST_SLAVE_STACK(7)
112
113 #endif
114
115 // Generate the code for signal test.
116 PROC_TEST_SLAVE(0, SIG_USER0)
117 PROC_TEST_SLAVE(1, SIG_USER1)
118 PROC_TEST_SLAVE(2, SIG_USER2)
119 PROC_TEST_SLAVE(3, SIG_USER3)
120 PROC_TEST_SLAVE(4, SIG_TIMEOUT)
121 PROC_TEST_SLAVE(5, SIG_SYSTEM5)
122 PROC_TEST_SLAVE(6, SIG_SYSTEM6)
123 PROC_TEST_SLAVE(7, SIG_SINGLE)
124
125 /**
126  * Run signal test
127  */
128 int signal_testRun(void)
129 {
130         struct Process *main_process = proc_current();
131         struct Process *slave_0;
132         struct Process *slave_1;
133         struct Process *slave_2;
134         struct Process *slave_3;
135         struct Process *slave_4;
136         struct Process *slave_5;
137         struct Process *slave_6;
138         struct Process *slave_7;
139
140         kprintf("Run Signal test..\n");
141         slave_0 = PROC_TEST_SLAVE_INIT(0, main_process);
142         slave_1 = PROC_TEST_SLAVE_INIT(1, main_process);
143         slave_2 = PROC_TEST_SLAVE_INIT(2, main_process);
144         slave_3 = PROC_TEST_SLAVE_INIT(3, main_process);
145         slave_4 = PROC_TEST_SLAVE_INIT(4, main_process);
146         slave_5 = PROC_TEST_SLAVE_INIT(5, main_process);
147         slave_6 = PROC_TEST_SLAVE_INIT(6, main_process);
148         slave_7 = PROC_TEST_SLAVE_INIT(7, main_process);
149
150         MAIN_CHECK_SIGNAL(0, slave_0);
151         MAIN_CHECK_SIGNAL(1, slave_1);
152         MAIN_CHECK_SIGNAL(2, slave_2);
153         MAIN_CHECK_SIGNAL(3, slave_3);
154         MAIN_CHECK_SIGNAL(4, slave_4);
155         MAIN_CHECK_SIGNAL(5, slave_5);
156         MAIN_CHECK_SIGNAL(6, slave_6);
157         MAIN_CHECK_SIGNAL(7, slave_7);
158
159         if(count == countof(test_signal))
160         {
161                 kprintf("Signal test finished..ok!\n");
162                 return 0;
163         }
164
165         kprintf("Signal test finished..fail!\n");
166         return -1;
167 }
168
169 int signal_testSetup(void)
170 {
171         kdbg_init();
172
173         kprintf("Init Timer..");
174         timer_init();
175         kprintf("Done.\n");
176
177         kprintf("Init Process..");
178         proc_init();
179         kprintf("Done.\n");
180         return 0;
181 }
182
183 int signal_testTearDown(void)
184 {
185         kputs("TearDown Signal test.\n");
186         return 0;
187 }
188
189 TEST_MAIN(signal);