]> rtime.felk.cvut.cz Git - rtems-devel.git/blob - rtems-omk-template/appdl/init.c
appdl: do not reference NFS if networking is not configured for appdl.
[rtems-devel.git] / rtems-omk-template / appdl / init.c
1 /*  Init
2  *
3  *  This routine is the initialization task for this test program.
4  *  It is called from init_exec and has the responsibility for creating
5  *  and starting the tasks that make up the test.  If the time of day
6  *  clock is required for the test, it should also be set to a known
7  *  value by this function.
8  *
9  *  (C) 2015 Pavel Pisa <pisa@cmp.felk.cvut.cz>
10  *
11  *  Based on RTEMS example test by
12  *  On-Line Applications Research Corporation (OAR).
13  *
14  */
15
16 #define CONFIGURE_INIT
17 #include <system_def.h>
18 #include "appl_config.h"
19 #include "system.h"
20 #include "app_def.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <dlfcn.h>
24 #include <rtems/error.h>
25 #include <rtems/monitor.h>
26 #include <rtems/shell.h>
27
28 #define USE_RTEMS_TARFS_LOAD
29
30 #ifdef USE_RTEMS_TARFS_LOAD
31 #include <rtems/imfs.h>
32 #else /*USE_RTEMS_TARFS_LOAD*/
33 #include <rtems/untar.h>
34 #endif /*USE_RTEMS_TARFS_LOAD*/
35
36 #ifdef CONFIG_OC_APP_APPDL_NET
37 #include <rtems/rtems_bsdnet.h>
38
39 #include "networkconfig.h"
40 #endif /*CONFIG_OC_APP_APPDL_NET*/
41
42 #include <sys/types.h>
43
44 #ifdef CONFIG_OC_APP_APPDL_NET
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #endif /*CONFIG_OC_APP_APPDL_NET*/
49
50 #include <string.h>
51 #include <unistd.h>
52
53 #define CONFIGURE_SHELL_COMMANDS_INIT
54 #define CONFIGURE_SHELL_COMMANDS_ALL
55 #define CONFIGURE_SHELL_COMMANDS_ALL_NETWORKING
56 #define CONFIGURE_SHELL_MOUNT_MSDOS
57 #ifdef CONFIG_OC_APP_APPDL_NET
58 #define CONFIGURE_SHELL_MOUNT_NFS
59 #endif
60
61 #include <rtems/shellconfig.h>
62
63 #define BUILD_VERSION_STRING(major,minor,patch) \
64         __XSTRING(major) "." __XSTRING(minor) "." __XSTRING(patch)
65
66 #define RTEMS_VER_CODE VER_CODE(__RTEMS_MAJOR__ ,__RTEMS_MINOR__ ,__RTEMS_REVISION__)
67
68 #if RTEMS_VER_CODE < VER_CODE(4,7,99)
69   #define rtems_shell_add_cmd shell_add_cmd
70   #define rtems_shell_init(m_task_name,m_task_stacksize,m_task_priority,m_devname,m_forever,m_wait,m_login_check) \
71                 shell_init(m_task_name,m_task_stacksize,m_task_priority,m_devname,B19200 | CS8,m_forever)
72 #elif RTEMS_VER_CODE < VER_CODE(4,9,99)
73   #define rtems_shell_init(m_task_name,m_task_stacksize,m_task_priority,m_devname,m_forever,m_wait,m_login_check) \
74           rtems_shell_init(m_task_name,m_task_stacksize,m_task_priority,m_devname,m_forever,m_wait)
75 #endif
76
77 extern int _binary_rootfs_tarfile_start;
78 extern int _binary_rootfs_tarfile_size;
79 #define TARFILE_START _binary_rootfs_tarfile_start
80 #define TARFILE_SIZE _binary_rootfs_tarfile_size
81
82 #ifdef CONFIG_OC_APP_APPDL_TELNETD
83 #include <rtems/telnetd.h>
84
85 rtems_telnetd_config_table rtems_telnetd_config;
86
87 void run_telnetd_command(char *device_name,  void *arg)
88 {
89   rtems_shell_env_t shell_env;
90
91   rtems_shell_dup_current_env(&shell_env);
92   shell_env.taskname = NULL;
93   shell_env.devname = device_name;
94   rtems_shell_main_loop(&shell_env);
95 }
96 #endif /*CONFIG_OC_APP_APPDL_TELNETD*/
97
98 void 
99 bad_rtems_status(rtems_status_code status, int fail_level, const char *text)
100 {
101   printf("ERROR: %s status %s", text, rtems_status_text(status));
102   status = rtems_task_delete( RTEMS_SELF );
103 }
104
105 int testcmd_forshell(int argc, char **argv)
106 {
107   int i;
108   printf("Command %s called\n",argv[0]);
109   for(i=1;i<argc;i++)
110     if(argv[i])
111       printf("%s",argv[i]);
112   printf("\n");
113   return 0;
114 }
115
116 typedef int (*call_t)(int argc, char* argv[]);
117
118 int dlopen_forshell(int argc, char **argv)
119 {
120   void * handle;
121   int    unresolved;
122   char * message = "loaded";
123   char   *call_file_name;
124   char   *call_symbol = NULL;
125   call_t call;
126   int    call_ret;
127
128   if (argc < 2) {
129     printf ("file to load not specified\n");
130     return 1;
131   }
132   call_file_name = argv[1];
133
134   if (argc >= 3)
135     call_symbol = argv[2];
136
137   argc -= 2;
138   argv += 2;
139
140   handle = dlopen (call_file_name, RTLD_NOW | RTLD_GLOBAL);
141   if (!handle)
142   {
143     printf("dlopen failed: %s\n", dlerror());
144     return 1;
145   }
146
147   if (dlinfo (handle, RTLD_DI_UNRESOLVED, &unresolved) < 0)
148     message = "dlinfo error checking unresolved status";
149   else if (unresolved)
150     message = "has unresolved externals";
151
152   if (call_symbol == NULL) {
153     printf ("handle: %p %s\n", handle, message);
154     return 1;
155   }
156
157   call = dlsym (handle, call_symbol);
158   if (call == NULL)
159   {
160     printf("dlsym failed: symbol %s not found\n", call_symbol);
161     return 1;
162   }
163
164   call_ret = call(argc, argv);
165
166   return call_ret;
167 }
168
169 rtems_task Init(
170   rtems_task_argument ignored
171 )
172 {
173   rtems_status_code status;
174  #ifdef USE_RTEMS_TARFS_LOAD
175   int res;
176  #endif /*USE_RTEMS_TARFS_LOAD*/
177
178   printf( "\n\nRTEMS v "
179           BUILD_VERSION_STRING(__RTEMS_MAJOR__ ,__RTEMS_MINOR__ ,__RTEMS_REVISION__)
180           "\n");
181
182   rtems_monitor_init(RTEMS_MONITOR_SUSPEND|RTEMS_MONITOR_GLOBAL);
183   /*rtems_capture_cli_init (0);*/
184
185  #ifdef CONFIG_OC_APP_APPDL_NET
186   if (rtems_bsdnet_initialize_network() < 0)
187     printf( "Network initialization failed\n");
188   else
189     printf( "Network initialization OK\n");
190  #endif /*CONFIG_OC_APP_APPDL_NET*/
191
192
193   printf( "Starting application " SW_VER_ID " v "
194           BUILD_VERSION_STRING(SW_VER_MAJOR,SW_VER_MINOR,SW_VER_PATCH)
195           "\n" );
196
197  #ifdef USE_RTEMS_TARFS_LOAD
198   res = rtems_tarfs_load("/", (void*)(&TARFILE_START), (long)&TARFILE_SIZE);
199   printf("rtems_tarfs_load returned %d\n", res);
200  #else /*USE_RTEMS_TARFS_LOAD*/
201   status = Untar_FromMemory((unsigned char *)(&TARFILE_START), (long)&TARFILE_SIZE);
202   printf("Untar_FromMemory returned %s\n",rtems_status_text(status));
203  #endif /*USE_RTEMS_TARFS_LOAD*/
204
205   Task_1_name = rtems_build_name( 'T', 'S', 'K', '1' );
206
207   status = rtems_task_create(
208      Task_1_name,
209      TASK_1_PRIORITY,
210      RTEMS_MINIMUM_STACK_SIZE+0x10000,
211      RTEMS_DEFAULT_MODES /*& ~(RTEMS_TIMESLICE_MASK) | RTEMS_TIMESLICE*/,
212      RTEMS_DEFAULT_ATTRIBUTES,
213      &Task_1_id
214   );
215   check_rtems_status(status, 0, "rtems_task_create of Task_1");
216
217   status = rtems_task_start( Task_1_id, Task_1, 0 );
218   check_rtems_status(status, 0, "rtems_task_start of Task_1\n");
219
220   rtems_shell_init("SHLL",RTEMS_MINIMUM_STACK_SIZE+0x1000,
221               SHELL_TASK_PRIORITY,"/dev/console",1,0, NULL);
222
223   rtems_shell_add_cmd("testcmd", "app",
224                 "test command for shell",
225                 testcmd_forshell);
226
227   rtems_shell_add_cmd("dlopen", "app",
228                 "runtime load object and call contained function",
229                 dlopen_forshell);
230
231   //rtems_monitor_wakeup();
232
233  #ifdef CONFIG_OC_APP_APPDL_TELNETD
234   rtems_telnetd_config.command = run_telnetd_command;
235   rtems_telnetd_config.arg = NULL;
236   rtems_telnetd_config.priority = SHELL_TASK_PRIORITY;
237   rtems_telnetd_config.stack_size = RTEMS_MINIMUM_STACK_SIZE+0x1000;
238   rtems_telnetd_config.login_check = NULL;
239   rtems_telnetd_config.keep_stdio = 0;
240
241   status = rtems_telnetd_initialize();
242   check_rtems_status(status, 0, "rtems_telnetd_initialize\n");
243  #endif /*CONFIG_OC_APP_APPDL_TELNETD*/
244
245   status = rtems_task_delete( RTEMS_SELF );
246
247   printf( "*** END OF APPDL ***\n" );
248   exit( 0 );
249 }