1
/* $Id: main_rtems.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program 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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
* - Many thanks for Zetron, Inc. and Phil Torre <ptorre@zetron.com> for
23
* donating this file and the RTEMS port in general!
29
#include <pj/string.h>
33
extern int param_echo_sock_type;
34
extern const char *param_echo_server;
35
extern int param_echo_port;
39
#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
40
#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 300
41
#define CONFIGURE_MAXIMUM_TASKS 50
42
#define CONFIGURE_MAXIMUM_MESSAGE_QUEUES rtems_resource_unlimited(10)
43
#define CONFIGURE_MAXIMUM_SEMAPHORES rtems_resource_unlimited(10)
44
#define CONFIGURE_MAXIMUM_TIMERS 50
45
#define CONFIGURE_MAXIMUM_REGIONS 3
46
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
47
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
48
#define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER
49
#define CONFIGURE_TICKS_PER_TIMESLICE 2
50
//#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
51
#define CONFIGURE_POSIX_INIT_THREAD_TABLE
54
#define CONFIGURE_MAXIMUM_POSIX_MUTEXES rtems_resource_unlimited(16)
55
#define CONFIGURE_MAXIMUM_POSIX_CONDITION_VARIABLES rtems_resource_unlimited(5)
56
#define CONFIGURE_MAXIMUM_POSIX_SEMAPHORES rtems_resource_unlimited(16)
57
#define CONFIGURE_MAXIMUM_POSIX_TIMERS rtems_resource_unlimited(5)
58
#define CONFIGURE_MAXIMUM_POSIX_THREADS rtems_resource_unlimited(16)
59
#define CONFIGURE_MAXIMUM_POSIX_KEYS rtems_resource_unlimited(16)
61
#define CONFIGURE_POSIX_INIT_THREAD_STACK_SIZE 4096
63
/* Make sure that stack size is at least 4096 */
64
#define SZ (4096-RTEMS_MINIMUM_STACK_SIZE)
65
#define CONFIGURE_EXTRA_TASK_STACKS ((SZ)<0 ? 0 : (SZ))
67
#define CONFIGURE_INIT
68
#define STACK_CHECKER_ON
70
rtems_task Init(rtems_task_argument Argument) ;
71
void *POSIX_Init(void *argument);
76
/* Any tests that want to build a linked executable for RTEMS must include
77
these headers to get a default config for the network stack. */
78
#include <rtems/rtems_bsdnet.h>
79
#include "rtems_network_config.h"
82
#include <sys/types.h>
86
#define THIS_FILE "main_rtems.c"
88
static void* pjlib_test_main(void* unused);
89
static void initialize_network();
90
static void test_sock(void);
92
static void my_perror(pj_status_t status, const char *title)
94
char err[PJ_ERR_MSG_SIZE];
96
pj_strerror(status, err, sizeof(err));
97
printf("%s: %s [%d]\n", title, err, status);
100
#define TEST(expr) { int rc;\
101
/*PJ_LOG(3,(THIS_FILE,"%s", #expr));*/ \
104
if (rc) my_perror(PJ_STATUS_FROM_OS(rc),#expr); }
108
//rtems_task Init(rtems_task_argument Argument)
109
void *POSIX_Init(void *argument)
111
pthread_attr_t threadAttr;
113
struct sched_param sched_param;
119
memset(data, 1, sizeof(data));
121
/* Set the TOD clock, so that gettimeofday() will work */
122
rtems_time_of_day fakeTime = { 2006, 3, 15, 17, 30, 0, 0 };
124
if (RTEMS_SUCCESSFUL != rtems_clock_set(&fakeTime))
129
/* Bring up the network stack so we can run the socket tests. */
130
initialize_network();
132
/* Start a POSIX thread for pjlib_test_main(), since that's what it
133
* thinks it is running in.
136
/* Initialize attribute */
137
TEST( pthread_attr_init(&threadAttr) );
139
/* Looks like the rest of the attributes must be fully initialized too,
140
* or otherwise pthread_create will return EINVAL.
143
/* Specify explicit scheduling request */
144
TEST( pthread_attr_setinheritsched(&threadAttr, PTHREAD_EXPLICIT_SCHED));
146
/* Timeslicing is needed by thread test, and this is accomplished by
149
TEST( pthread_attr_setschedpolicy(&threadAttr, SCHED_RR));
152
TEST( pthread_attr_getschedparam(&threadAttr, &sched_param));
153
sched_param.sched_priority = NETWORK_STACK_PRIORITY - 10;
154
TEST( pthread_attr_setschedparam(&threadAttr, &sched_param));
156
/* Must have sufficient stack size (large size is needed by
157
* logger, because default settings for logger is to use message buffer
160
TEST( pthread_attr_getstacksize(&threadAttr, &stack_size));
161
if (stack_size < 8192)
162
TEST( pthread_attr_setstacksize(&threadAttr, 8192));
165
/* Create the thread for application */
166
result = pthread_create(&theThread, &threadAttr, &pjlib_test_main, NULL);
168
my_perror(PJ_STATUS_FROM_OS(result),
169
"Error creating pjlib_test_main thread");
170
assert(!"Error creating main thread");
179
#define init_signals()
182
pjlib_test_main(void* unused)
186
/* Drop our priority to below that of the network stack, otherwise
187
* select() tests will fail. */
188
struct sched_param schedParam;
191
printf("pjlib_test_main thread started..\n");
193
TEST( pthread_getschedparam(pthread_self(), &schedPolicy, &schedParam) );
195
schedParam.sched_priority = NETWORK_STACK_PRIORITY - 10;
197
TEST( pthread_setschedparam(pthread_self(), schedPolicy, &schedParam) );
202
//my_test_thread("from pjlib_test_main");
210
# include <sys/types.h>
211
# include <sys/socket.h>
212
# include <netinet/in.h>
213
# include <arpa/inet.h>
217
* Send UDP packet to some host. We can then use Ethereal to sniff the packet
218
* to see if this target really transmits UDP packet.
221
send_udp(const char *target)
224
struct sockaddr_in addr;
226
PJ_LOG(3,("main_rtems.c", "IP addr=%s/%s, gw=%s",
227
DEFAULT_IP_ADDRESS_STRING,
228
DEFAULT_NETMASK_STRING,
229
DEFAULT_GATEWAY_STRING));
231
sock = socket(AF_INET, SOCK_DGRAM, 0);
234
memset(&addr, 0, sizeof(addr));
235
addr.sin_family = AF_INET;
237
rc = bind(sock, (struct sockaddr*)&addr, sizeof(addr));
238
assert("bind error" && rc==0);
240
addr.sin_addr.s_addr = inet_addr(target);
241
addr.sin_port = htons(4444);
244
const char *data = "hello";
246
rc = sendto(sock, data, 5, 0, (struct sockaddr*)&addr, sizeof(addr));
247
PJ_LOG(3,("main_rtems.c", "pinging %s..(rc=%d)", target, rc));
253
static void test_sock(void)
256
struct sockaddr_in addr;
259
sock = socket(AF_INET, SOCK_DGRAM, 0);
261
printf("socket() error\n");
265
memset(&addr, 0, sizeof(addr));
266
addr.sin_family = AF_INET;
267
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
268
addr.sin_port = htons(5000);
270
rc = bind(sock, (struct sockaddr*)&addr, sizeof(addr));
272
printf("bind() error %d\n", rc);
277
puts("Bind socket success");
286
* Initialize the network stack and Ethernet driver, using the configuration
287
* in rtems-network-config.h
292
unsigned32 fd, result;
293
char ip_address_string[] = DEFAULT_IP_ADDRESS_STRING;
294
char netmask_string[] = DEFAULT_NETMASK_STRING;
295
char gateway_string[] = DEFAULT_GATEWAY_STRING;
297
// Write the network config files to /etc/hosts and /etc/host.conf
298
result = mkdir("/etc", S_IRWXU | S_IRWXG | S_IRWXO);
299
fd = open("/etc/host.conf", O_RDWR | O_CREAT, 0744);
300
result = write(fd, "hosts,bind\n", 11);
302
fd = open("/etc/hosts", O_RDWR | O_CREAT, 0744);
303
result = write(fd, "127.0.0.1 localhost\n", 41);
304
result = write(fd, ip_address_string, strlen(ip_address_string));
305
result = write(fd, " pjsip-test\n", 32);
308
netdriver_config.ip_address = ip_address_string;
309
netdriver_config.ip_netmask = netmask_string;
310
rtems_bsdnet_config.gateway = gateway_string;
312
if (0 != rtems_bsdnet_initialize_network())
313
PJ_LOG(3,(THIS_FILE, "Error: Unable to initialize network stack!"));
315
PJ_LOG(3,(THIS_FILE, "IP addr=%s/%s, gw=%s",
320
//rtems_rdbg_initialize();
322
//send_udp("192.168.0.1");