summaryrefslogtreecommitdiffstats
path: root/testsuite/usb01/init.c
blob: eaaef1c6f5d3149d2e794c0e6e5089d010da52aa (plain) (blame)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
 * @file
 *
 * @brief File system test.
 */

/*
 * Copyright (c) 2010-2012 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Obere Lagerstr. 30
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.com/license/LICENSE.
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <pthread.h>

#include <rtems.h>
#include <rtems/media.h>
#include <rtems/chain.h>
#include <rtems/libio_.h>
#include <rtems/shell.h>
#include <rtems/console.h>
#include <rtems/diskdevs.h>
#include <rtems/bsd/bsd.h>

#include "test.h"

#define ASSERT_SC(sc) assert((sc) == RTEMS_SUCCESSFUL)

#define ASSERT_ENO(eno) assert((eno) == 0)

#define WORKER_COUNT 13

#define WORKER_EVENT RTEMS_EVENT_13

void test_file_system(unsigned index, const char *disk_path, const char *mount_path);

typedef struct {
  rtems_chain_node node;
  char *disk_or_partition_path;
  rtems_id task;
} worker;

static worker worker_table [WORKER_COUNT];

RTEMS_CHAIN_DEFINE_EMPTY(free_worker_list);

static const rtems_name WORKER_NAME = rtems_build_name('W', 'O', 'R', 'K');

static pthread_mutex_t worker_mutex;

static pthread_cond_t worker_changed;

static void add_worker_to_free_list(worker *w)
{
  rtems_chain_append(&free_worker_list, &w->node);
}

static worker *get_free_worker(void)
{
  return (worker *) rtems_chain_get(&free_worker_list);
}

static void activate_worker(char *disk_or_partition_path)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  worker *w = get_free_worker();

  if (w != NULL) {
    w->disk_or_partition_path = disk_or_partition_path;

    sc = rtems_event_send(w->task, WORKER_EVENT);
    ASSERT_SC(sc);
  } else {
    free(disk_or_partition_path);
  }
}

static worker *find_worker_for_disk_or_partition(const char *disk_or_partition_path)
{
  size_t i = 0;

  for (i = 0; i < WORKER_COUNT; ++i) {
    worker *w = &worker_table [i];

    if (w->disk_or_partition_path != NULL && strcmp(w->disk_or_partition_path, disk_or_partition_path) == 0) {
      return w;
    }
  }

  return NULL;
}

static void wait_for_worker_finished(const char *disk_or_partition_path)
{
  int eno = 0;
  worker *w = NULL;

  eno = pthread_mutex_lock(&worker_mutex);
  ASSERT_ENO(eno);

  w = find_worker_for_disk_or_partition(disk_or_partition_path);

  if (w != NULL) {
    while (w->disk_or_partition_path != NULL) {
      eno = pthread_cond_wait(&worker_changed, &worker_mutex);
      ASSERT_ENO(eno);
    }
  }

  eno = pthread_mutex_unlock(&worker_mutex);
  ASSERT_ENO(eno);
}

static rtems_status_code media_listener(rtems_media_event event, rtems_media_state state, const char *src, const char *dest, void *arg)
{
  rtems_status_code rsc = RTEMS_SUCCESSFUL;
  char *disk_or_partition_path = NULL;

  printf("media listener: event = %s, state = %s, src = %s", rtems_media_event_description(event), rtems_media_state_description(state), src);

  if (dest != NULL) {
    printf(", dest = %s", dest);
  }

  if (arg != NULL) {
    printf(", arg = %p\n", arg);
  }

  printf("\n");

  if (state == RTEMS_MEDIA_STATE_INQUIRY) {
    if (event == RTEMS_MEDIA_EVENT_MOUNT) {
      rsc = RTEMS_IO_ERROR;
    } else if (event == RTEMS_MEDIA_EVENT_PARTITION_DETACH || event == RTEMS_MEDIA_EVENT_DISK_DETACH) {
      wait_for_worker_finished(src);
    } else if (event == RTEMS_MEDIA_EVENT_UNMOUNT) {
      assert(false);
    }
  } else if (state == RTEMS_MEDIA_STATE_SUCCESS) {
    if (event == RTEMS_MEDIA_EVENT_PARTITION_ATTACH) {
      disk_or_partition_path = strdup(dest);
    }
  } else if (state == RTEMS_MEDIA_STATE_FAILED) {
    if (event == RTEMS_MEDIA_EVENT_PARTITION_INQUIRY) {
      disk_or_partition_path = strdup(src);
    }
  }

  if (disk_or_partition_path != NULL) {
    activate_worker(disk_or_partition_path);
  }

  return rsc;
}

static void worker_finished(worker *w)
{
  int eno = 0;

  eno = pthread_mutex_lock(&worker_mutex);
  ASSERT_ENO(eno);

  free(w->disk_or_partition_path);
  w->disk_or_partition_path = NULL;

  eno = pthread_cond_broadcast(&worker_changed);
  ASSERT_ENO(eno);

  eno = pthread_mutex_unlock(&worker_mutex);
  ASSERT_ENO(eno);
}

static void worker_task(rtems_task_argument arg)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  unsigned index = arg;
  worker *self = &worker_table [index];
  char mount_path [] = "/work/????";
  int rv = 0;

  rv = snprintf(mount_path, sizeof(mount_path), "/work/%u", index);
  assert(rv < (int) sizeof(mount_path));

  sc = rtems_libio_set_private_env();
  ASSERT_SC(sc);

  rv = rtems_mkdir(mount_path, S_IRWXU | S_IRWXG | S_IRWXO);
  assert(rv == 0);

  while (true) {
    rtems_event_set events = 0;

    sc = rtems_event_receive(WORKER_EVENT, RTEMS_EVENT_ALL | RTEMS_WAIT, RTEMS_NO_TIMEOUT, &events);
    ASSERT_SC(sc);

    test_file_system(index, self->disk_or_partition_path, mount_path);

    worker_finished(self);

    add_worker_to_free_list(self);
  }
}

static const char mac_address [6] = { 0x00, 0x1a, 0xf1, 0x00, 0x07, 0xa4 };

static void Init(rtems_task_argument arg)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  int eno = 0;
  rtems_id id = RTEMS_ID_NONE;
  size_t i = 0;

  eno = pthread_mutex_init(&worker_mutex, NULL);
  ASSERT_ENO(eno);

  eno = pthread_cond_init(&worker_changed, NULL);
  ASSERT_ENO(eno);

  for (i = 0; i < WORKER_COUNT; ++i) {
    worker *w = &worker_table [i];

    sc = rtems_task_create(
      WORKER_NAME,
      95 + 10 * (i % 4),
      32 * 1024,
      RTEMS_DEFAULT_MODES,
      RTEMS_FLOATING_POINT,
      &id
    );
    ASSERT_SC(sc);

    w->task = id;
    add_worker_to_free_list(w);

    sc = rtems_task_start(id, worker_task, i);
    ASSERT_SC(sc);
  }

  sc = rtems_disk_io_initialize();
  ASSERT_SC(sc);

  sc = rtems_media_initialize();
  ASSERT_SC(sc);

  sc = rtems_media_listener_add(media_listener, NULL);
  ASSERT_SC(sc);

  sc = rtems_media_server_initialize(200, 32 * 1024, RTEMS_DEFAULT_MODES, RTEMS_DEFAULT_ATTRIBUTES);
  ASSERT_SC(sc);

  sc = rtems_bsd_initialize();
  ASSERT_SC(sc);

  sc = rtems_shell_init(
    "SHLL",
    16 * 1024,
    10,
    CONSOLE_DEVICE_NAME,
    false,
    true,
    NULL
  );
  ASSERT_SC(sc);

  exit(0);
}

#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK

#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM

#define CONFIGURE_FILESYSTEM_IMFS
#define CONFIGURE_FILESYSTEM_DOSFS

#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 32

#define CONFIGURE_BDBUF_BUFFER_MIN_SIZE 512
#define CONFIGURE_BDBUF_BUFFER_MAX_SIZE 512
#define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE (WORKER_COUNT * 512)
#define CONFIGURE_BDBUF_MAX_READ_AHEAD_BLOCKS 0

#define CONFIGURE_MAXIMUM_USER_EXTENSIONS 1

#define CONFIGURE_UNLIMITED_OBJECTS

#define CONFIGURE_UNIFIED_WORK_AREAS

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT

#define CONFIGURE_STACK_CHECKER_ENABLED

#define CONFIGURE_INIT

#include <rtems/confdefs.h>

#define CONFIGURE_SHELL_COMMANDS_INIT

#include "demo-shell.h"
#include "demo-shell-block-devices.h"
#include <rtems/shellconfig.h>

#define USB_SYSINIT_INIT

#include "usb-sysinit.h"