]> rtime.felk.cvut.cz Git - rtems-devel.git/blob - rtems-omk-template/appdl/init.c
OMK template example applications print APP_VER_ID instead of common SW_VER_ID.
[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 #include <rtems/rtl/dlfcn-shell.h>
28
29 #define USE_RTEMS_TARFS_LOAD
30
31 #ifdef USE_RTEMS_TARFS_LOAD
32 #include <rtems/imfs.h>
33 #else /*USE_RTEMS_TARFS_LOAD*/
34 #include <rtems/untar.h>
35 #endif /*USE_RTEMS_TARFS_LOAD*/
36
37 #ifdef CONFIG_OC_APP_APPDL_NET
38 #include <rtems/rtems_bsdnet.h>
39
40 #include "networkconfig.h"
41 #endif /*CONFIG_OC_APP_APPDL_NET*/
42
43 #include <sys/types.h>
44
45 #ifdef CONFIG_OC_APP_APPDL_NET
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #endif /*CONFIG_OC_APP_APPDL_NET*/
50
51 #include <string.h>
52 #include <unistd.h>
53
54 #define CONFIGURE_SHELL_COMMANDS_INIT
55 #define CONFIGURE_SHELL_COMMANDS_ALL
56 #define CONFIGURE_SHELL_COMMANDS_ALL_NETWORKING
57 #define CONFIGURE_SHELL_MOUNT_MSDOS
58 #ifdef CONFIG_OC_APP_APPDL_NET
59 #define CONFIGURE_SHELL_MOUNT_NFS
60 #endif
61
62 #include <rtems/shellconfig.h>
63
64 #define BUILD_VERSION_STRING(major,minor,patch) \
65         __XSTRING(major) "." __XSTRING(minor) "." __XSTRING(patch)
66
67 #define RTEMS_VER_CODE VER_CODE(__RTEMS_MAJOR__ ,__RTEMS_MINOR__ ,__RTEMS_REVISION__)
68
69 #if RTEMS_VER_CODE < VER_CODE(4,7,99)
70   #define rtems_shell_add_cmd shell_add_cmd
71   #define rtems_shell_init(m_task_name,m_task_stacksize,m_task_priority,m_devname,m_forever,m_wait,m_login_check) \
72                 shell_init(m_task_name,m_task_stacksize,m_task_priority,m_devname,B19200 | CS8,m_forever)
73 #elif RTEMS_VER_CODE < VER_CODE(4,9,99)
74   #define rtems_shell_init(m_task_name,m_task_stacksize,m_task_priority,m_devname,m_forever,m_wait,m_login_check) \
75           rtems_shell_init(m_task_name,m_task_stacksize,m_task_priority,m_devname,m_forever,m_wait)
76 #endif
77
78 extern int _binary_rootfs_tarfile_start;
79 extern int _binary_rootfs_tarfile_size;
80 #define TARFILE_START _binary_rootfs_tarfile_start
81 #define TARFILE_SIZE _binary_rootfs_tarfile_size
82
83 #ifdef CONFIG_OC_APP_APPDL_TELNETD
84 #include <rtems/telnetd.h>
85
86 rtems_telnetd_config_table rtems_telnetd_config;
87
88 void run_telnetd_command(char *device_name,  void *arg)
89 {
90   rtems_shell_env_t shell_env;
91
92   rtems_shell_dup_current_env(&shell_env);
93   shell_env.taskname = NULL;
94   shell_env.devname = device_name;
95   rtems_shell_main_loop(&shell_env);
96 }
97 #endif /*CONFIG_OC_APP_APPDL_TELNETD*/
98
99 void 
100 bad_rtems_status(rtems_status_code status, int fail_level, const char *text)
101 {
102   printf("ERROR: %s status %s", text, rtems_status_text(status));
103   status = rtems_task_delete( RTEMS_SELF );
104 }
105
106 int testcmd_forshell(int argc, char **argv)
107 {
108   int i;
109   printf("Command %s called\n",argv[0]);
110   for(i=1;i<argc;i++)
111     if(argv[i])
112       printf("%s",argv[i]);
113   printf("\n");
114   return 0;
115 }
116
117 typedef int (*call_t)(int argc, char* argv[]);
118
119 volatile int continue_execution;
120
121 int dlrun_forshell(int argc, char **argv)
122 {
123   void * handle;
124   int    unresolved;
125   char * message = "loaded";
126   char   *call_file_name;
127   char   *call_symbol = NULL;
128   call_t call;
129   int    call_ret;
130
131   if (argc < 2) {
132     printf ("file to load not specified\n");
133     return 1;
134   }
135   call_file_name = argv[1];
136
137   if (argc >= 3)
138     call_symbol = argv[2];
139
140   argc -= 2;
141   argv += 2;
142
143   handle = dlopen (call_file_name, RTLD_NOW | RTLD_GLOBAL);
144   if (!handle)
145   {
146     printf("dlopen failed: %s\n", dlerror());
147     return 1;
148   }
149
150   if (dlinfo (handle, RTLD_DI_UNRESOLVED, &unresolved) < 0)
151     message = "dlinfo error checking unresolved status";
152   else if (unresolved)
153     message = "has unresolved externals";
154
155   if (call_symbol == NULL) {
156     printf ("handle: %p %s\n", handle, message);
157     return 1;
158   }
159
160   call = dlsym (handle, call_symbol);
161   if (call == NULL)
162   {
163     printf("dlsym failed: symbol %s not found\n", call_symbol);
164     return 1;
165   }
166
167   call_ret = call(argc, argv);
168
169   return call_ret;
170 }
171
172 rtems_task Init(
173   rtems_task_argument ignored
174 )
175 {
176   rtems_status_code status;
177  #ifdef USE_RTEMS_TARFS_LOAD
178   int res;
179  #endif /*USE_RTEMS_TARFS_LOAD*/
180
181   printf( "\n\nRTEMS v "
182           BUILD_VERSION_STRING(__RTEMS_MAJOR__ ,__RTEMS_MINOR__ ,__RTEMS_REVISION__)
183           "\n");
184
185   rtems_monitor_init(RTEMS_MONITOR_SUSPEND|RTEMS_MONITOR_GLOBAL);
186   /*rtems_capture_cli_init (0);*/
187
188  #ifdef CONFIG_OC_APP_APPDL_NET
189   if (rtems_bsdnet_initialize_network() < 0)
190     printf( "Network initialization failed\n");
191   else
192     printf( "Network initialization OK\n");
193  #endif /*CONFIG_OC_APP_APPDL_NET*/
194
195
196   printf( "Starting application " APP_VER_ID " v "
197           BUILD_VERSION_STRING(SW_VER_MAJOR,SW_VER_MINOR,SW_VER_PATCH)
198           "\n" );
199
200  #ifdef USE_RTEMS_TARFS_LOAD
201   res = rtems_tarfs_load("/", (void*)(&TARFILE_START), (long)&TARFILE_SIZE);
202   printf("rtems_tarfs_load returned %d\n", res);
203  #else /*USE_RTEMS_TARFS_LOAD*/
204   status = Untar_FromMemory((unsigned char *)(&TARFILE_START), (long)&TARFILE_SIZE);
205   printf("Untar_FromMemory returned %s\n",rtems_status_text(status));
206  #endif /*USE_RTEMS_TARFS_LOAD*/
207
208   Task_1_name = rtems_build_name( 'T', 'S', 'K', '1' );
209
210   status = rtems_task_create(
211      Task_1_name,
212      TASK_1_PRIORITY,
213      RTEMS_MINIMUM_STACK_SIZE+0x10000,
214      RTEMS_DEFAULT_MODES /*& ~(RTEMS_TIMESLICE_MASK) | RTEMS_TIMESLICE*/,
215      RTEMS_DEFAULT_ATTRIBUTES,
216      &Task_1_id
217   );
218   check_rtems_status(status, 0, "rtems_task_create of Task_1");
219
220   status = rtems_task_start( Task_1_id, Task_1, 0 );
221   check_rtems_status(status, 0, "rtems_task_start of Task_1\n");
222
223   rtems_shell_init("SHLL",RTEMS_MINIMUM_STACK_SIZE+0x1000,
224               SHELL_TASK_PRIORITY,"/dev/console",1,0, NULL);
225
226   rtems_shell_add_cmd("testcmd", "app",
227                 "test command for shell",
228                 testcmd_forshell);
229
230   rtems_shell_add_cmd("dlrun", "rtl",
231                 "runtime load object and run specified function",
232                 dlrun_forshell);
233
234   rtems_shell_add_cmd("dlopen", "rtl",
235                 "runtime load object",
236                 shell_dlopen);
237
238   rtems_shell_add_cmd("dlclose", "rtl",
239                 "close reference to loaded object",
240                 shell_dlclose);
241
242   rtems_shell_add_cmd("dlsym", "rtl",
243                 "obtain reference to symbol in loaded object",
244                 shell_dlsym);
245
246   rtems_shell_add_cmd("dlcall", "rtl",
247                 "call function in loaded object",
248                 shell_dlcall);
249
250   //rtems_monitor_wakeup();
251
252  #ifdef CONFIG_OC_APP_APPDL_TELNETD
253   rtems_telnetd_config.command = run_telnetd_command;
254   rtems_telnetd_config.arg = NULL;
255   rtems_telnetd_config.priority = SHELL_TASK_PRIORITY;
256   rtems_telnetd_config.stack_size = RTEMS_MINIMUM_STACK_SIZE+0x1000;
257   rtems_telnetd_config.login_check = NULL;
258   rtems_telnetd_config.keep_stdio = 0;
259
260   status = rtems_telnetd_initialize();
261   check_rtems_status(status, 0, "rtems_telnetd_initialize\n");
262  #endif /*CONFIG_OC_APP_APPDL_TELNETD*/
263
264   status = rtems_task_delete( RTEMS_SELF );
265
266   printf( "*** END OF APPDL ***\n" );
267   exit( 0 );
268 }