source: ada-examples/rtems_init.c @ 2059da6

ada-examples-4-10-branchada-examples-4-9-branch
Last change on this file since 2059da6 was eb04e53, checked in by Joel Sherrill <joel.sherrill@…>, on Sep 27, 2007 at 2:43:05 PM

2007-09-27 Joel Sherrill <joel.sherrill@…>

  • ChangeLog?: New tests. Clean up. Rework so all tests use the same shared Makefile for the hard logic of producing an executable.
  • Makefile, Makefile.shared, README.Makefiles, networkconfig.h, rtems_init.c: New files.
  • Property mode set to 100644
File size: 5.4 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2007.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be found in
6 *  the file LICENSE in this distribution or at
7 *  http://www.rtems.com/license/LICENSE.
8 *
9 *  $Id$
10 */
11
12#include <bsp.h>
13
14#include <assert.h>
15#include <pthread.h>
16#include <stdlib.h>
17
18#if defined(MAIN_USE_NETWORKING)
19  #include <rtems/rtems_bsdnet.h>
20  #include "networkconfig.h"
21#endif
22#if defined(ENABLE_UNTAR_ROOT_FILESYSTEM)
23  #include <rtems/untar.h>
24  /*
25   *  The tarfile image is built automatically externally.
26   */
27  #include "FilesystemImage.h"
28#endif
29
30
31extern rtems_configuration_table  BSP_Configuration;
32
33/*
34 *  Using GNAT's Distributed Systems Annex support requires
35 *  each node in the system to have different pid's.  In a
36 *  single CPU RTEMS system, it is always one.  This lets the
37 *  user override the RTEMS default.
38 */
39#ifdef GNAT_PID
40  #include <unistd.h>
41
42  pid_t getpid()
43  {
44    return GNAT_PID;
45  }
46#endif
47
48/*
49 * Set up first argument
50 */
51static int   argc     = 1;
52static char  arg0[20] = "rtems";
53static char *argv[20] = { arg0 };
54
55#if defined(MAIN_USE_REQUIRES_COMMAND_LINE)
56
57#define COMMAND_LINE_MAXIMUM 200
58
59#include <stdio.h>
60#include <ctype.h>
61
62void parse_arguments(
63  char   *buffer,
64  size_t  maximum_length
65)
66{
67  char   *cp;
68  char   *linebuf = buffer;
69  size_t  length;
70
71  for (;;) {
72
73    #if defined(MAIN_COMMAND_LINE)
74      strncpy (linebuf, MAIN_COMMAND_LINE, maximum_length);
75    #else
76      /*
77       * Read a line
78       */
79      printf (">>> %s ", argv[0]);
80      fflush (stdout);
81      fgets (linebuf, maximum_length, stdin);
82
83      length = strnlen( linebuf, maximum_length );
84      if ( linebuf[length - 1] == '\n' || linebuf[length - 1] == '\r' ) {
85         linebuf[length - 1] = '\0';
86      }
87    #endif
88
89    /*
90     * Break line into arguments
91     */
92    cp = linebuf;
93    for (;;) {
94      while (isspace (*cp))
95        *cp++ = '\0';
96      if (*cp == '\0')
97        break;
98      if (argc >= ((sizeof argv / sizeof argv[0]) - 1)) {
99        printf ("Too many arguments.\n");
100        argc = 0;
101        break;
102      }
103      argv[argc++] = cp;
104      while (!isspace (*cp)) {
105        if (*cp == '\0')
106          break;
107        cp++;
108      }
109    }
110    if (argc > 1) {
111      argv[argc] = NULL;
112      break;
113    }
114    printf ("You must give some arguments!\n");
115  }
116
117  #if defined(DEBUG_COMMAND_LINE_ARGUMENTS)
118    {
119      int   i;
120      for (i=0; i<argc ; i++ ) {
121        printf( "argv[%d] = ***%s***\n", i, argv[i] );
122      }
123      printf( "\n" );
124    }
125  #endif
126}
127
128
129#endif
130
131/*
132 *  By having the POSIX_Init thread create a second thread just
133 *  to invoke gnat_main, we can override all default attributes
134 *  of the "Ada environment task".  Otherwise, we would be
135 *  stuck with the defaults set by RTEMS.
136 */
137 
138void *start_gnat_main( void * argument )
139{
140  extern int gnat_main ( int argc, char **argv, char **envp );
141
142  #if defined(MAIN_USE_REQUIRES_COMMAND_LINE)
143    /*
144     * This is scoped to match the Ada program.
145     */
146    char command_line[ COMMAND_LINE_MAXIMUM ];
147
148    parse_arguments( command_line, COMMAND_LINE_MAXIMUM );
149  #endif
150
151  (void) gnat_main ( argc, argv, 0 );
152
153  exit( 0 );
154
155  return 0;
156}
157
158#ifndef GNAT_MAIN_STACKSPACE
159  #define GNAT_MAIN_STACKSPACE 0
160#endif
161
162void *POSIX_Init( void *argument )
163{
164  pthread_t       thread_id;
165  pthread_attr_t  attr;
166  size_t          stacksize;
167  int             status;
168  extern  size_t  _ada_pthread_minimum_stack_size();
169
170  #if defined(ENABLE_UNTAR_ROOT_FILESYSTEM)
171    printk("Loading filesystem image\n");
172    status = Untar_FromMemory( (char *)FilesystemImage, FilesystemImage_size );
173  #endif
174
175  #if defined(MAIN_USE_NETWORKING)
176    printk("Initializing Network\n");
177    rtems_bsdnet_initialize_network ();
178  #endif
179
180  status = pthread_attr_init( &attr );
181  assert( !status );
182
183  stacksize = GNAT_MAIN_STACKSPACE * 1024;
184  if ( stacksize < _ada_pthread_minimum_stack_size() )
185    stacksize = _ada_pthread_minimum_stack_size();
186
187  status = pthread_attr_setstacksize( &attr, stacksize );
188  assert( !status );
189
190  status = pthread_create( &thread_id, &attr, start_gnat_main, NULL );
191  assert( !status );
192
193  pthread_exit( 0 );
194  return 0;
195}
196
197/* configuration information */
198
199/* Standard output and a clock tick so time passes */
200#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
201#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
202
203/* We need to be able to create sockets */
204#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS        20
205
206/* We need the full IMFS to pass the full ACATS */
207#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
208
209/* This is overkill but is definitely enough to run the network stack */
210#define CONFIGURE_MAXIMUM_TASKS                         20
211#define CONFIGURE_MAXIMUM_SEMAPHORES                    20
212
213/* We want a clock tick every millisecond */
214#define CONFIGURE_MICROSECONDS_PER_TICK RTEMS_MILLISECONDS_TO_MICROSECONDS(1)
215
216/* The initialization task is a POSIX Initialization thread with default attributes */
217#define CONFIGURE_POSIX_INIT_THREAD_TABLE
218
219/* We are using GNAT/RTEMS with a maximum of 20 Ada tasks and no fake Ada tasks. */
220/* A fake Ada task is a task created outside the Ada run-time that calls into Ada. */
221#define CONFIGURE_GNAT_RTEMS
222#define CONFIGURE_MAXIMUM_ADA_TASKS      20
223#define CONFIGURE_MAXIMUM_FAKE_ADA_TASKS 0
224
225/* Account for any extra task stack size */
226#define CONFIGURE_MEMORY_OVERHEAD        (GNAT_MAIN_STACKSPACE)
227
228#define CONFIGURE_INIT
229
230#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.