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