source: rtems/testsuites/libtests/block09/init.c @ 40284de

4.115
Last change on this file since 40284de was 40284de, checked in by Sebastian Huber <sebastian.huber@…>, on 05/30/12 at 10:43:56

libblock: Remove const qualifier from bdbuf API

This allows addtion of per disk statistics for example.

  • Property mode set to 100644
File size: 5.2 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup test_bdbuf
5 *
6 * @brief Bdbuf test for disk driver error handling..
7 */
8
9/*
10 * Copyright (c) 2009-2012 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Obere Lagerstr. 30
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * The license and distribution terms for this file may be
19 * found in the file LICENSE in this distribution or at
20 * http://www.rtems.com/license/LICENSE.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <assert.h>
28#include <errno.h>
29
30#include <rtems.h>
31#include <rtems/bdbuf.h>
32#include <rtems/diskdevs.h>
33
34/* forward declarations to avoid warnings */
35static rtems_task Init(rtems_task_argument argument);
36
37#define ASSERT_SC(sc) assert((sc) == RTEMS_SUCCESSFUL)
38
39#define BLOCK_SIZE sizeof(char)
40
41#define BLOCK_COUNT 4
42
43#define BLOCK_READ_IO_ERROR 0
44#define BLOCK_READ_UNSATISFIED 1
45#define BLOCK_READ_SUCCESSFUL 2
46#define BLOCK_WRITE_IO_ERROR 3
47
48static char disk_data [BLOCK_COUNT];
49
50static const rtems_driver_address_table disk_ops = {
51  .initialization_entry = NULL,
52  RTEMS_GENERIC_BLOCK_DEVICE_DRIVER_ENTRIES
53};
54
55static int disk_ioctl(rtems_disk_device *dd, uint32_t req, void *arg)
56{
57  rtems_status_code sc = RTEMS_SUCCESSFUL;
58
59  if (req == RTEMS_BLKIO_REQUEST) {
60    rtems_blkdev_request *r = arg;
61    uint32_t i = 0;
62
63    for (i = 0; i < r->bufnum; ++i) {
64      rtems_blkdev_sg_buffer *sg = &r->bufs [i];
65      char *buf = sg->buffer;
66
67      if (sg->length != 1) {
68        return -1;
69      }
70
71      switch (r->req) {
72        case RTEMS_BLKDEV_REQ_READ:
73          switch (sg->block) {
74            case BLOCK_READ_IO_ERROR:
75              sc = RTEMS_IO_ERROR;
76              break;
77            case BLOCK_READ_UNSATISFIED:
78              sc = RTEMS_UNSATISFIED;
79              break;
80            case BLOCK_READ_SUCCESSFUL:
81            case BLOCK_WRITE_IO_ERROR:
82              *buf = disk_data [sg->block];
83              break;
84            default:
85              return -1;
86          }
87          break;
88        case RTEMS_BLKDEV_REQ_WRITE:
89          switch (sg->block) {
90            case BLOCK_READ_IO_ERROR:
91            case BLOCK_READ_UNSATISFIED:
92            case BLOCK_READ_SUCCESSFUL:
93              disk_data [sg->block] = *buf;
94              break;
95            case BLOCK_WRITE_IO_ERROR:
96              sc = RTEMS_IO_ERROR;
97              break;
98            default:
99              return -1;
100          }
101          break;
102        default:
103          return -1;
104      }
105    }
106
107    r->req_done(r->done_arg, sc);
108
109    return 0;
110  } else {
111    return rtems_blkdev_ioctl(dd, req, arg);
112  }
113}
114
115static rtems_status_code disk_register(
116  uint32_t block_size,
117  rtems_blkdev_bnum block_count,
118  dev_t *dev_ptr
119)
120{
121  rtems_status_code sc = RTEMS_SUCCESSFUL;
122  rtems_device_major_number major = 0;
123  dev_t dev = 0;
124
125  sc = rtems_io_register_driver(0, &disk_ops, &major);
126  ASSERT_SC(sc);
127
128  dev = rtems_filesystem_make_dev_t(major, 0);
129
130  sc = rtems_disk_create_phys(
131    dev,
132    block_size,
133    block_count,
134    disk_ioctl,
135    NULL,
136    NULL
137  );
138  ASSERT_SC(sc);
139
140  *dev_ptr = dev;
141
142  return RTEMS_SUCCESSFUL;
143}
144
145static void check_read(
146  rtems_disk_device *dd,
147  rtems_blkdev_bnum block,
148  rtems_status_code expected_sc
149)
150{
151  rtems_status_code sc = RTEMS_SUCCESSFUL;
152  rtems_bdbuf_buffer *bd = NULL;
153
154  sc = rtems_bdbuf_read(dd, block, &bd);
155  assert(sc == expected_sc);
156
157  if (sc == RTEMS_SUCCESSFUL) {
158    sc = rtems_bdbuf_release(bd);
159    ASSERT_SC(sc);
160  }
161}
162
163static rtems_task Init(rtems_task_argument argument)
164{
165  rtems_status_code sc = RTEMS_SUCCESSFUL;
166  rtems_bdbuf_buffer *bd = NULL;
167  dev_t dev = 0;
168  rtems_disk_device *dd = NULL;
169
170  printk("\n\n*** TEST BLOCK 9 ***\n");
171
172  sc = rtems_disk_io_initialize();
173  ASSERT_SC(sc);
174
175  sc = disk_register(BLOCK_SIZE, BLOCK_COUNT, &dev);
176  ASSERT_SC(sc);
177
178  dd = rtems_disk_obtain(dev);
179  assert(dd != NULL);
180
181  check_read(dd, BLOCK_READ_IO_ERROR, RTEMS_IO_ERROR);
182  check_read(dd, BLOCK_READ_UNSATISFIED, RTEMS_UNSATISFIED);
183  check_read(dd, BLOCK_READ_SUCCESSFUL, RTEMS_SUCCESSFUL);
184  check_read(dd, BLOCK_WRITE_IO_ERROR, RTEMS_SUCCESSFUL);
185
186  /* Check write IO error */
187
188  sc = rtems_bdbuf_read(dd, BLOCK_WRITE_IO_ERROR, &bd);
189  ASSERT_SC(sc);
190
191  bd->buffer [0] = 1;
192
193  sc = rtems_bdbuf_sync(bd);
194  ASSERT_SC(sc);
195
196  sc = rtems_bdbuf_read(dd, BLOCK_WRITE_IO_ERROR, &bd);
197  ASSERT_SC(sc);
198
199  assert(bd->buffer [0] == 0);
200
201  sc = rtems_bdbuf_release(bd);
202  ASSERT_SC(sc);
203
204  /* Check write to deleted disk */
205
206  sc = rtems_bdbuf_read(dd, BLOCK_READ_SUCCESSFUL, &bd);
207  ASSERT_SC(sc);
208
209  sc = rtems_disk_delete(dev);
210  ASSERT_SC(sc);
211
212  sc = rtems_bdbuf_sync(bd);
213  ASSERT_SC(sc);
214
215  sc = rtems_disk_release(dd);
216  ASSERT_SC(sc);
217
218  printk("*** END OF TEST BLOCK 9 ***\n");
219
220  exit(0);
221}
222
223#define CONFIGURE_INIT
224
225#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
226#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
227#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
228
229#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
230
231#define CONFIGURE_MAXIMUM_TASKS 1
232#define CONFIGURE_MAXIMUM_DRIVERS 4
233
234#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
235
236#define CONFIGURE_BDBUF_BUFFER_MIN_SIZE BLOCK_SIZE
237#define CONFIGURE_BDBUF_BUFFER_MAX_SIZE BLOCK_SIZE
238#define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE (BLOCK_SIZE * BLOCK_COUNT)
239
240#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.