Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/main_rtems.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 /*
21 * - Many thanks for Zetron, Inc. and Phil Torre <[email protected]> for
22 * donating this file and the RTEMS port in general!
23 */
24
25 #include "test.h"
26
27 #include <pj/errno.h>
28 #include <pj/string.h>
29 #include <pj/sock.h>
30 #include <pj/log.h>
31
32 #include <bsp.h>
33
34 #define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
35 #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 300
36 #define CONFIGURE_MAXIMUM_TASKS 50
37 #define CONFIGURE_MAXIMUM_MESSAGE_QUEUES rtems_resource_unlimited(10)
38 #define CONFIGURE_MAXIMUM_SEMAPHORES rtems_resource_unlimited(10)
39 #define CONFIGURE_MAXIMUM_TIMERS 50
40 #define CONFIGURE_MAXIMUM_REGIONS 3
41 #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
42 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
43 #define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER
44 #define CONFIGURE_TICKS_PER_TIMESLICE 2
45 //#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
46 #define CONFIGURE_POSIX_INIT_THREAD_TABLE
47
48
49 #define CONFIGURE_MAXIMUM_POSIX_MUTEXES rtems_resource_unlimited(16)
50 #define CONFIGURE_MAXIMUM_POSIX_CONDITION_VARIABLES rtems_resource_unlimited(5)
51 #define CONFIGURE_MAXIMUM_POSIX_SEMAPHORES rtems_resource_unlimited(16)
52 #define CONFIGURE_MAXIMUM_POSIX_TIMERS rtems_resource_unlimited(5)
53 #define CONFIGURE_MAXIMUM_POSIX_THREADS rtems_resource_unlimited(16)
54 #define CONFIGURE_MAXIMUM_POSIX_KEYS rtems_resource_unlimited(16)
55
56 #define CONFIGURE_POSIX_INIT_THREAD_STACK_SIZE 4096
57
58 /* Make sure that stack size is at least 4096 */
59 #define SZ (4096-RTEMS_MINIMUM_STACK_SIZE)
60 #define CONFIGURE_EXTRA_TASK_STACKS ((SZ)<0 ? 0 : (SZ))
61
62 #define CONFIGURE_INIT
63 #define STACK_CHECKER_ON
64
65 rtems_task Init(rtems_task_argument Argument) ;
66 void *POSIX_Init(void *argument);
67
68 #include <confdefs.h>
69 #include <rtems.h>
70
71 /* Any tests that want to build a linked executable for RTEMS must include
72 these headers to get a default config for the network stack. */
73 #include <rtems/rtems_bsdnet.h>
74 #include "rtems_network_config.h"
75
76 #include <assert.h>
77 #include <sys/types.h>
78 #include <sys/stat.h>
79 #include <fcntl.h>
80
81 #define THIS_FILE "main_rtems.c"
82
83 static void* pjlib_test_main(void* unused);
84 static void initialize_network();
85 static void test_sock(void);
86
87 static void my_perror(pj_status_t status, const char *title)
88 {
89 char err[PJ_ERR_MSG_SIZE];
90
91 pj_strerror(status, err, sizeof(err));
92 printf("%s: %s [%d]\n", title, err, status);
93 }
94
95 #define TEST(expr) { int rc;\
96 /*PJ_LOG(3,(THIS_FILE,"%s", #expr));*/ \
97 /*sleep(1);*/ \
98 rc=expr; \
99 if (rc) my_perror(PJ_STATUS_FROM_OS(rc),#expr); }
100
101
102
103 //rtems_task Init(rtems_task_argument Argument)
104 void *POSIX_Init(void *argument)
105 {
106 pthread_attr_t threadAttr;
107 pthread_t theThread;
108 struct sched_param sched_param;
109 size_t stack_size;
110 int result;
111 char data[1000];
112
113
114 memset(data, 1, sizeof(data));
115
116 /* Set the TOD clock, so that gettimeofday() will work */
117 rtems_time_of_day fakeTime = { 2006, 3, 15, 17, 30, 0, 0 };
118
119 if (RTEMS_SUCCESSFUL != rtems_clock_set(&fakeTime))
120 {
121 assert(0);
122 }
123
124 /* Bring up the network stack so we can run the socket tests. */
125 initialize_network();
126
127 /* Start a POSIX thread for pjlib_test_main(), since that's what it
128 * thinks it is running in.
129 */
130
131 /* Initialize attribute */
132 TEST( pthread_attr_init(&threadAttr) );
133
134 /* Looks like the rest of the attributes must be fully initialized too,
135 * or otherwise pthread_create will return EINVAL.
136 */
137
138 /* Specify explicit scheduling request */
139 TEST( pthread_attr_setinheritsched(&threadAttr, PTHREAD_EXPLICIT_SCHED));
140
141 /* Timeslicing is needed by thread test, and this is accomplished by
142 * SCHED_RR.
143 */
144 TEST( pthread_attr_setschedpolicy(&threadAttr, SCHED_RR));
145
146 /* Set priority */
147 TEST( pthread_attr_getschedparam(&threadAttr, &sched_param));
148 sched_param.sched_priority = NETWORK_STACK_PRIORITY - 10;
149 TEST( pthread_attr_setschedparam(&threadAttr, &sched_param));
150
151 /* Must have sufficient stack size (large size is needed by
152 * logger, because default settings for logger is to use message buffer
153 * from the stack).
154 */
155 TEST( pthread_attr_getstacksize(&threadAttr, &stack_size));
156 if (stack_size < 8192)
157 TEST( pthread_attr_setstacksize(&threadAttr, 8192));
158
159
160 /* Create the thread for application */
161 result = pthread_create(&theThread, &threadAttr, &pjlib_test_main, NULL);
162 if (result != 0) {
163 my_perror(PJ_STATUS_FROM_OS(result),
164 "Error creating pjlib_test_main thread");
165 assert(!"Error creating main thread");
166 }
167
168 return NULL;
169 }
170
171
172
173 #define boost()
174 #define init_signals()
175
176 static void*
177 pjlib_test_main(void* unused)
178 {
179 int rc;
180
181 /* Drop our priority to below that of the network stack, otherwise
182 * select() tests will fail. */
183 struct sched_param schedParam;
184 int schedPolicy;
185
186 printf("pjlib_test_main thread started..\n");
187
188 TEST( pthread_getschedparam(pthread_self(), &schedPolicy, &schedParam) );
189
190 schedParam.sched_priority = NETWORK_STACK_PRIORITY - 10;
191
192 TEST( pthread_setschedparam(pthread_self(), schedPolicy, &schedParam) );
193
194 boost();
195 init_signals();
196
197 //my_test_thread("from pjlib_test_main");
198 //test_sock();
199
200 rc = test_main();
201
202 return (void*)rc;
203 }
204
205 # include <sys/types.h>
206 # include <sys/socket.h>
207 # include <netinet/in.h>
208 # include <arpa/inet.h>
209 # include <unistd.h>
210
211 /*
212 * Send UDP packet to some host. We can then use Ethereal to sniff the packet
213 * to see if this target really transmits UDP packet.
214 */
215 static void
216 send_udp(const char *target)
217 {
218 int sock, rc;
219 struct sockaddr_in addr;
220
221 PJ_LOG(3,("main_rtems.c", "IP addr=%s/%s, gw=%s",
222 DEFAULT_IP_ADDRESS_STRING,
223 DEFAULT_NETMASK_STRING,
224 DEFAULT_GATEWAY_STRING));
225
226 sock = socket(AF_INET, SOCK_DGRAM, 0);
227 assert(sock > 0);
228
229 memset(&addr, 0, sizeof(addr));
230 addr.sin_family = AF_INET;
231
232 rc = bind(sock, (struct sockaddr*)&addr, sizeof(addr));
233 assert("bind error" && rc==0);
234
235 addr.sin_addr.s_addr = inet_addr(target);
236 addr.sin_port = htons(4444);
237
238 while(1) {
239 const char *data = "hello";
240
241 rc = sendto(sock, data, 5, 0, (struct sockaddr*)&addr, sizeof(addr));
242 PJ_LOG(3,("main_rtems.c", "pinging %s..(rc=%d)", target, rc));
243 sleep(1);
244 }
245 }
246
247
248 static void test_sock(void)
249 {
250 int sock;
251 struct sockaddr_in addr;
252 int rc;
253
254 sock = socket(AF_INET, SOCK_DGRAM, 0);
255 if (sock < 0) {
256 printf("socket() error\n");
257 goto end;
258 }
259
260 memset(&addr, 0, sizeof(addr));
261 addr.sin_family = AF_INET;
262 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
263 addr.sin_port = htons(5000);
264
265 rc = bind(sock, (struct sockaddr*)&addr, sizeof(addr));
266 if (rc != 0) {
267 printf("bind() error %d\n", rc);
268 close(sock);
269 goto end;
270 }
271
272 puts("Bind socket success");
273
274 close(sock);
275
276 end:
277 while(1) sleep(1);
278 }
279
280 /*
281 * Initialize the network stack and Ethernet driver, using the configuration
282 * in rtems-network-config.h
283 */
284 static void
285 initialize_network()
286 {
287 unsigned32 fd, result;
288 char ip_address_string[] = DEFAULT_IP_ADDRESS_STRING;
289 char netmask_string[] = DEFAULT_NETMASK_STRING;
290 char gateway_string[] = DEFAULT_GATEWAY_STRING;
291
292 // Write the network config files to /etc/hosts and /etc/host.conf
293 result = mkdir("/etc", S_IRWXU | S_IRWXG | S_IRWXO);
294 fd = open("/etc/host.conf", O_RDWR | O_CREAT, 0744);
295 result = write(fd, "hosts,bind\n", 11);
296 result = close(fd);
297 fd = open("/etc/hosts", O_RDWR | O_CREAT, 0744);
298 result = write(fd, "127.0.0.1 localhost\n", 26);
299 result = write(fd, ip_address_string, strlen(ip_address_string));
300 result = write(fd, " pjsip-test\n", 19);
301 result = close(fd);
302
303 netdriver_config.ip_address = ip_address_string;
304 netdriver_config.ip_netmask = netmask_string;
305 rtems_bsdnet_config.gateway = gateway_string;
306
307 if (0 != rtems_bsdnet_initialize_network())
308 PJ_LOG(3,(THIS_FILE, "Error: Unable to initialize network stack!"));
309 else
310 PJ_LOG(3,(THIS_FILE, "IP addr=%s/%s, gw=%s",
311 ip_address_string,
312 netmask_string,
313 gateway_string));
314
315 //rtems_rdbg_initialize();
316 //enterRdbg();
317 //send_udp("192.168.0.1");
318 //test_sock();
319 }
320
321