PageRenderTime 85ms CodeModel.GetById 53ms RepoModel.GetById 1ms app.codeStats 0ms

/CSipSimple/jni/pjsip/sources/pjlib/src/pjlib-test/main_rtems.c

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