| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 72 |
|
| 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 |
\ |
| 97 |
\ |
| 98 |
rc=expr; \ |
| 99 |
if (rc) my_perror(PJ_STATUS_FROM_OS(rc),#expr); } |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 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 |
|
| 125 |
initialize_network(); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
TEST( pthread_attr_init(&threadAttr) ); |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
TEST( pthread_attr_setinheritsched(&threadAttr, PTHREAD_EXPLICIT_SCHED)); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
TEST( pthread_attr_setschedpolicy(&threadAttr, SCHED_RR)); |
| 145 |
|
| 146 |
|
| 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 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
TEST( pthread_attr_getstacksize(&threadAttr, &stack_size)); |
| 156 |
if (stack_size < 8192) |
| 157 |
TEST( pthread_attr_setstacksize(&threadAttr, 8192)); |
| 158 |
|
| 159 |
|
| 160 |
|
| 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 |
|
| 182 |
|
| 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 |
|
| 198 |
|
| 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 |
|
| 213 |
|
| 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 |
|
| 282 |
|
| 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 |
|
| 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 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
|