main_rtems.c 9.4 KB

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