summaryrefslogtreecommitdiff
path: root/led/complex1/test.cc
blob: 3be1e23d085f47cd3e344ea0d740576f60a3947f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//
//  $Id$
//

#include <stdio.h>
#include "ledServer.h"
#include "menu.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

Examples::LEDServer *led;

void StartLED(void)
{
  led->start();
}

void StopLED(void)
{
  led->stop();
}

void SetPeriodOfLED(void)
{
  unsigned int onPeriod;
  unsigned int offPeriod;
  onPeriod = Test_menu_Get_int_bounded(
    "Length LED is ON (in milliseconds)",
    1,
    10000
  );
  offPeriod = Test_menu_Get_int_bounded(
    "Length LED is OFF (in milliseconds)",
    1,
    10000
  );
  led->setPeriod( onPeriod, offPeriod );
}

Menu_entry LEDMenu_entries[] = {
 { "Activate LED Server",            StartLED },
 { "Deactivate LED Server",          StopLED },
 { "Set Period of LED Server",       SetPeriodOfLED },
};

Test_menu LEDMenu = {
  "LED Server Control Menu",
  (sizeof(LEDMenu_entries) / sizeof(LEDMenu_entries[0])),
  LEDMenu_entries
};


/*
 *  Top level menu
 */

#define MAX_FILES_IN_MENU 60

char *FileNames[MAX_FILES_IN_MENU];
int  FileCount = 0;
bool FileNamesLoaded = false;

extern "C" {
  // strcasecmp is not 100% portable
  int Mystrcasecmp( const char *s1, const char *s2 )
  {
    while (*s1 != '\0' && tolower(*s1) == tolower(*s2)) {
      s1++;
      s2++;
    }
    return tolower(*(unsigned char *) s1) - tolower(*(unsigned char *) s2);
  }

  static int qsortcmp( const void *l, const void *r )
  {
    char **ld = (char **)l;
    char **rd = (char **)r;

    // fprintf( stderr, "-%s- -%s-\n", *ld, *rd );
    return Mystrcasecmp( *ld, *rd );
  }
};

int LoadDirectoryMenu(void)
{
  DIR *dir;
  struct dirent *d;

  if (FileNamesLoaded)
    return 0;
  FileNamesLoaded = true;

  /* initialize */
  dir = opendir(".");

  if (!dir) {
    fprintf( stderr, "Unable to open current directory\n" );
    return -1;
  }

  /* find all the .txt files */

  FileNames[ FileCount++ ] = strdup("Return to previous menu");

  for ( ; ;  ) {
    d = readdir(dir);
    if (!d)
      break;

    FileNames[ FileCount++ ] = strdup( d->d_name );

    /* fprintf( stderr, "%s\n", d->d_name ); */
    if ( FileCount >= MAX_FILES_IN_MENU ) {
      fprintf( stderr, "Too many files in current directory\n" );
      return -1;
    }
  }

  /* shutdown */
  closedir( dir );

  qsort( &FileNames[1], FileCount - 1, sizeof(char *), qsortcmp );

  return 0;
}

void LEDMenuFunction(void)
{
  Test_menu_run( &LEDMenu );
}

void statFile()
{
  int selected;
  struct stat statb;

  if ( LoadDirectoryMenu() )
    return;

  selected = Test_menu_Get_selection(
    "Select a File To stat()",
    FileCount,
    FileNames
  );
  if ( selected == 0 )
    return;

  fprintf( stderr,
     "You picked %d --> %s\n", selected, FileNames[selected] );

  if ( stat( FileNames[selected], &statb ) ) {
    fprintf( stderr, "Error stating file\n" );
    return;
  }

  fprintf( stderr, "Owner/Group: %d %d Permissions: 0%o\n"
                   "Size... in bytes: %ld  in blocks: %ld of %ld\n"
                   "Times of Last.. Access: %ld Modification %ld Change: %ld\n",
     statb.st_uid, statb.st_gid, statb.st_mode,
     (long) statb.st_size, (long) statb.st_blocks, (long) statb.st_blksize,
     (long) statb.st_atime, (long) statb.st_mtime, (long) statb.st_ctime );
}

#if defined(__rtems__)
#include <rtems.h>
#include <rtems/stackchk.h>
#include <rtems/cpuuse.h>

#include "periodic.h"

void SetNumberOfPeriodicThreads(void)
{
  unsigned int threads = Test_menu_Get_int_bounded(
    "How many periodic threads do you want to create",
    1,
    100
  );
  PeriodicThreads_Count = threads;
}

Menu_entry PeriodicThreadsMenu_entries[] = {
 { "Start Periodic Threads",         PeriodicThreads_Start},
 { "Stop Periodic Threads",          PeriodicThreads_Stop},
 { "Set Number Of Periodic Threads", SetNumberOfPeriodicThreads},
};

Test_menu PeriodicThreadsMenu = {
  "Periodic Threads Menu",
  (sizeof(PeriodicThreadsMenu_entries) /
          sizeof(PeriodicThreadsMenu_entries[0])),
  PeriodicThreadsMenu_entries
};

void PeriodicThreadsFunction(void)
{
  Test_menu_run( &PeriodicThreadsMenu );
}

#endif

Menu_entry MainMenu_entries[] = {
 { "LED Server Control Menu",        LEDMenuFunction },
 { "stat() a file",                  statFile },
#if defined(__rtems__)
 { "Periodic Threads Control Menu",  PeriodicThreadsFunction },
 { "Stack Usage Report",             rtems_stack_checker_report_usage },
 { "CPU Usage Report",               rtems_cpu_usage_report },
 { "Reset CPU Usage Statistics",     rtems_cpu_usage_reset },
 { "Period Usage Report",            rtems_rate_monotonic_report_statistics },
 { "Reset Period Usage Statistics",  rtems_rate_monotonic_reset_all_statistics},
#endif
};

Test_menu MainMenu = {
  "Main Menu",
  (sizeof(MainMenu_entries) / sizeof(MainMenu_entries[0])),
  MainMenu_entries
};

void doTest(void)
{
  LED_INIT();

  led = new Examples::LEDServer();

  Test_menu_run( &MainMenu );
#if defined(__rtems__)
  PeriodicThreads_Stop();
#endif
  delete led;
}