Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 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. |
| 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * - Many thanks for Zetron, Inc. and Phil Torre <ptorre@zetron.com> for |
| 23 | * donating this file and the RTEMS port in general! |
| 24 | */ |
| 25 | |
| 26 | #include "test.h" |
| 27 | |
| 28 | #include <pj/errno.h> |
| 29 | #include <pj/string.h> |
| 30 | #include <pj/sock.h> |
| 31 | #include <pj/log.h> |
| 32 | |
| 33 | extern int param_echo_sock_type; |
| 34 | extern const char *param_echo_server; |
| 35 | extern int param_echo_port; |
| 36 | |
| 37 | #include <bsp.h> |
| 38 | |
| 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 |
| 52 | |
| 53 | |
| 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) |
| 60 | |
| 61 | #define CONFIGURE_POSIX_INIT_THREAD_STACK_SIZE 4096 |
| 62 | |
| 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)) |
| 66 | |
| 67 | #define CONFIGURE_INIT |
| 68 | #define STACK_CHECKER_ON |
| 69 | |
| 70 | rtems_task Init(rtems_task_argument Argument) ; |
| 71 | void *POSIX_Init(void *argument); |
| 72 | |
| 73 | #include <confdefs.h> |
| 74 | #include <rtems.h> |
| 75 | |
| 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" |
| 80 | |
| 81 | #include <assert.h> |
| 82 | #include <sys/types.h> |
| 83 | #include <sys/stat.h> |
| 84 | #include <fcntl.h> |
| 85 | |
| 86 | #define THIS_FILE "main_rtems.c" |
| 87 | |
| 88 | static void* pjlib_test_main(void* unused); |
| 89 | static void initialize_network(); |
| 90 | static void test_sock(void); |
| 91 | |
| 92 | static void my_perror(pj_status_t status, const char *title) |
| 93 | { |
| 94 | char err[PJ_ERR_MSG_SIZE]; |
| 95 | |
| 96 | pj_strerror(status, err, sizeof(err)); |
| 97 | printf("%s: %s [%d]\n", title, err, status); |
| 98 | } |
| 99 | |
| 100 | #define TEST(expr) { int rc;\ |
| 101 | /*PJ_LOG(3,(THIS_FILE,"%s", #expr));*/ \ |
| 102 | /*sleep(1);*/ \ |
| 103 | rc=expr; \ |
| 104 | if (rc) my_perror(PJ_STATUS_FROM_OS(rc),#expr); } |
| 105 | |
| 106 | |
| 107 | |
| 108 | //rtems_task Init(rtems_task_argument Argument) |
| 109 | void *POSIX_Init(void *argument) |
| 110 | { |
| 111 | pthread_attr_t threadAttr; |
| 112 | pthread_t theThread; |
| 113 | struct sched_param sched_param; |
| 114 | size_t stack_size; |
| 115 | int result; |
| 116 | char data[1000]; |
| 117 | |
| 118 | |
| 119 | memset(data, 1, sizeof(data)); |
| 120 | |
| 121 | /* Set the TOD clock, so that gettimeofday() will work */ |
| 122 | rtems_time_of_day fakeTime = { 2006, 3, 15, 17, 30, 0, 0 }; |
| 123 | |
| 124 | if (RTEMS_SUCCESSFUL != rtems_clock_set(&fakeTime)) |
| 125 | { |
| 126 | assert(0); |
| 127 | } |
| 128 | |
| 129 | /* Bring up the network stack so we can run the socket tests. */ |
| 130 | initialize_network(); |
| 131 | |
| 132 | /* Start a POSIX thread for pjlib_test_main(), since that's what it |
| 133 | * thinks it is running in. |
| 134 | */ |
| 135 | |
| 136 | /* Initialize attribute */ |
| 137 | TEST( pthread_attr_init(&threadAttr) ); |
| 138 | |
| 139 | /* Looks like the rest of the attributes must be fully initialized too, |
| 140 | * or otherwise pthread_create will return EINVAL. |
| 141 | */ |
| 142 | |
| 143 | /* Specify explicit scheduling request */ |
| 144 | TEST( pthread_attr_setinheritsched(&threadAttr, PTHREAD_EXPLICIT_SCHED)); |
| 145 | |
| 146 | /* Timeslicing is needed by thread test, and this is accomplished by |
| 147 | * SCHED_RR. |
| 148 | */ |
| 149 | TEST( pthread_attr_setschedpolicy(&threadAttr, SCHED_RR)); |
| 150 | |
| 151 | /* Set priority */ |
| 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)); |
| 155 | |
| 156 | /* Must have sufficient stack size (large size is needed by |
| 157 | * logger, because default settings for logger is to use message buffer |
| 158 | * from the stack). |
| 159 | */ |
| 160 | TEST( pthread_attr_getstacksize(&threadAttr, &stack_size)); |
| 161 | if (stack_size < 8192) |
| 162 | TEST( pthread_attr_setstacksize(&threadAttr, 8192)); |
| 163 | |
| 164 | |
| 165 | /* Create the thread for application */ |
| 166 | result = pthread_create(&theThread, &threadAttr, &pjlib_test_main, NULL); |
| 167 | if (result != 0) { |
| 168 | my_perror(PJ_STATUS_FROM_OS(result), |
| 169 | "Error creating pjlib_test_main thread"); |
| 170 | assert(!"Error creating main thread"); |
| 171 | } |
| 172 | |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | |
| 178 | #define boost() |
| 179 | #define init_signals() |
| 180 | |
| 181 | static void* |
| 182 | pjlib_test_main(void* unused) |
| 183 | { |
| 184 | int rc; |
| 185 | |
| 186 | /* Drop our priority to below that of the network stack, otherwise |
| 187 | * select() tests will fail. */ |
| 188 | struct sched_param schedParam; |
| 189 | int schedPolicy; |
| 190 | |
| 191 | printf("pjlib_test_main thread started..\n"); |
| 192 | |
| 193 | TEST( pthread_getschedparam(pthread_self(), &schedPolicy, &schedParam) ); |
| 194 | |
| 195 | schedParam.sched_priority = NETWORK_STACK_PRIORITY - 10; |
| 196 | |
| 197 | TEST( pthread_setschedparam(pthread_self(), schedPolicy, &schedParam) ); |
| 198 | |
| 199 | boost(); |
| 200 | init_signals(); |
| 201 | |
| 202 | //my_test_thread("from pjlib_test_main"); |
| 203 | //test_sock(); |
| 204 | |
| 205 | rc = test_main(); |
| 206 | |
| 207 | return (void*)rc; |
| 208 | } |
| 209 | |
| 210 | # include <sys/types.h> |
| 211 | # include <sys/socket.h> |
| 212 | # include <netinet/in.h> |
| 213 | # include <arpa/inet.h> |
| 214 | # include <unistd.h> |
| 215 | |
| 216 | /* |
| 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. |
| 219 | */ |
| 220 | static void |
| 221 | send_udp(const char *target) |
| 222 | { |
| 223 | int sock, rc; |
| 224 | struct sockaddr_in addr; |
| 225 | |
| 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)); |
| 230 | |
| 231 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 232 | assert(sock > 0); |
| 233 | |
| 234 | memset(&addr, 0, sizeof(addr)); |
| 235 | addr.sin_family = AF_INET; |
| 236 | |
| 237 | rc = bind(sock, (struct sockaddr*)&addr, sizeof(addr)); |
| 238 | assert("bind error" && rc==0); |
| 239 | |
| 240 | addr.sin_addr.s_addr = inet_addr(target); |
| 241 | addr.sin_port = htons(4444); |
| 242 | |
| 243 | while(1) { |
| 244 | const char *data = "hello"; |
| 245 | |
| 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)); |
| 248 | sleep(1); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | |
| 253 | static void test_sock(void) |
| 254 | { |
| 255 | int sock; |
| 256 | struct sockaddr_in addr; |
| 257 | int rc; |
| 258 | |
| 259 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 260 | if (sock < 0) { |
| 261 | printf("socket() error\n"); |
| 262 | goto end; |
| 263 | } |
| 264 | |
| 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); |
| 269 | |
| 270 | rc = bind(sock, (struct sockaddr*)&addr, sizeof(addr)); |
| 271 | if (rc != 0) { |
| 272 | printf("bind() error %d\n", rc); |
| 273 | close(sock); |
| 274 | goto end; |
| 275 | } |
| 276 | |
| 277 | puts("Bind socket success"); |
| 278 | |
| 279 | close(sock); |
| 280 | |
| 281 | end: |
| 282 | while(1) sleep(1); |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Initialize the network stack and Ethernet driver, using the configuration |
| 287 | * in rtems-network-config.h |
| 288 | */ |
| 289 | static void |
| 290 | initialize_network() |
| 291 | { |
| 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; |
| 296 | |
| 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); |
| 301 | result = close(fd); |
| 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); |
| 306 | result = close(fd); |
| 307 | |
| 308 | netdriver_config.ip_address = ip_address_string; |
| 309 | netdriver_config.ip_netmask = netmask_string; |
| 310 | rtems_bsdnet_config.gateway = gateway_string; |
| 311 | |
| 312 | if (0 != rtems_bsdnet_initialize_network()) |
| 313 | PJ_LOG(3,(THIS_FILE, "Error: Unable to initialize network stack!")); |
| 314 | else |
| 315 | PJ_LOG(3,(THIS_FILE, "IP addr=%s/%s, gw=%s", |
| 316 | ip_address_string, |
| 317 | netmask_string, |
| 318 | gateway_string)); |
| 319 | |
| 320 | //rtems_rdbg_initialize(); |
| 321 | //enterRdbg(); |
| 322 | //send_udp("192.168.0.1"); |
| 323 | //test_sock(); |
| 324 | } |
| 325 | |
| 326 | |