summaryrefslogtreecommitdiffstats
path: root/testsuites/libtests/block08/bdbuf_tests.c
blob: 8cf0111dbcbd9c0549b5117115c1d7915e19043f (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
/*! @file
 * @brief Implementation of auxiliary functions of bdbuf tests.
 *
 * Copyright (C) 2010 OKTET Labs, St.-Petersburg, Russia
 * Author: Oleg Kravtsov <Oleg.Kravtsov@oktetlabs.ru>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#include <rtems.h>
#include <rtems/io.h>
#include <rtems/bdbuf.h>
#include <rtems/inttypes.h>
#include "bdbuf_tests.h"

#include <tmacros.h>

struct bdbuf_test_descr {
    void (* main)(void);
} bdbuf_tests[] = {
    { bdbuf_test1_1_main },
    { bdbuf_test1_2_main },
    { bdbuf_test1_3_main },
    { bdbuf_test1_4_main },
    { bdbuf_test1_5_main },

    { bdbuf_test2_1_main },
    { bdbuf_test2_2_main },

    { bdbuf_test3_1_main },
    { bdbuf_test3_2_main },
    { bdbuf_test3_3_main },

    { bdbuf_test4_1_main },
    { bdbuf_test4_2_main },
    { bdbuf_test4_3_main },
};

#define TEST_SEM_ATTRIBS RTEMS_DEFAULT_ATTRIBUTES

/** Device ID used for testing */
rtems_disk_device *test_dd = NULL;

/** Test result variable */
bool       good_test_result = true;

test_ctx g_test_ctx;

const char *test_name = "NO TEST";

bdbuf_test_msg test_drv_msg;

/** Count of messages in RX message queue used on disk driver side. */
#define TEST_DRV_RX_MQUEUE_COUNT 10
/** Name of disk driver RX message queue. */
#define TEST_DRV_RX_MQUEUE_NAME  (rtems_build_name( 'M', 'Q', 'D', ' ' ))

/** Count of messages in Test task RX message queue */
#define TEST_TASK_RX_MQUEUE_COUNT 10
/** Name of Test task RX message queue */
#define TEST_TASK_RX_MQUEUE_NAME  (rtems_build_name( 'M', 'Q', 'T', ' ' ))

rtems_status_code
bdbuf_test_start_aux_task(rtems_name name,
                          rtems_task_entry entry_point,
                          rtems_task_argument arg,
                          Objects_Id *id)
{
    rtems_status_code rc;
    Objects_Id        task_id;

    rc = rtems_task_create(name, BDBUF_TEST_THREAD_PRIO_NORMAL, 1024 * 2,
                           RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_NO_ASR,
                           RTEMS_LOCAL | RTEMS_NO_FLOATING_POINT,
                           &task_id);
    if (rc != RTEMS_SUCCESSFUL)
    {
        printf("Failed to create task\n");
        return rc;
    }

    rc = rtems_task_start(task_id, entry_point, arg);
    if (rc != RTEMS_SUCCESSFUL)
    {
        printf("Failed to start task\n");
    }
    else
    {
        if (id != NULL)
            *id = task_id;
    }
    return rc;
}

void
run_bdbuf_tests()
{
    rtems_status_code sc;
    unsigned int      i;
    int               fd;
    int               rv;

    /* Create a message queue to get events from disk driver. */
    sc = rtems_message_queue_create(TEST_TASK_RX_MQUEUE_NAME,
                                    TEST_TASK_RX_MQUEUE_COUNT,
                                    sizeof(bdbuf_test_msg),
                                    RTEMS_DEFAULT_ATTRIBUTES,
                                    &g_test_ctx.test_qid);

    if (sc != RTEMS_SUCCESSFUL)
    {
        printf("Failed to create message queue for test task: %u\n", sc);
        return;
    }

    sc = test_disk_initialize();
    rtems_test_assert(sc == RTEMS_SUCCESSFUL);

    fd = open(TEST_DISK_NAME, O_RDWR);
    rtems_test_assert(fd >= 0);

    rv = rtems_disk_fd_get_disk_device(fd, &test_dd);
    rtems_test_assert(rv == 0);

    rv = close(fd);
    rtems_test_assert(rv == 0);

    /*
     * On initialization test disk device driver registers
     * its RX message queue, so we just need to locate it.
     */
    sc = rtems_message_queue_ident(TEST_DRV_RX_MQUEUE_NAME,
                                   RTEMS_SEARCH_ALL_NODES,
                                   &g_test_ctx.test_drv_qid);
    if (sc != RTEMS_SUCCESSFUL)
    {
        printf("Failed to find Test Driver Queue: %u\n", sc);
        return;
    }

    for (i = 0; i < ARRAY_NUM(g_test_ctx.test_sync_main); i++)
    {
        sc = rtems_semaphore_create(rtems_build_name('T', 'S', 'M', '0' + i),
                                    0, TEST_SEM_ATTRIBS, 0,
                                    &g_test_ctx.test_sync_main[i]);
        if (sc != RTEMS_SUCCESSFUL)
        {
            printf("Failed to create sync sem for test task: %u\n", sc);
            return;
        }
    }

    for (i = 0; i < ARRAY_NUM(g_test_ctx.test_sync); i++)
    {
        sc = rtems_semaphore_create(rtems_build_name('T', 'S', 'T', '0' + i),
                                    0, TEST_SEM_ATTRIBS, 0,
                                    &g_test_ctx.test_sync[i]);
        if (sc != RTEMS_SUCCESSFUL)
        {
            printf("Failed to create sync sem for test task #%d: %u\n", i + 1, sc);
            return;
        }
    }

    sc = rtems_semaphore_create(rtems_build_name('T', 'S', 'M', 'E'),
                                0, TEST_SEM_ATTRIBS, 0,
                                &g_test_ctx.test_end_main);
    if (sc != RTEMS_SUCCESSFUL)
    {
        printf("Failed to create end sync sem for test task: %u\n", sc);
        return;
    }

    for (i = 0; i < ARRAY_NUM(g_test_ctx.test_task); i++)
        g_test_ctx.test_task[i] = OBJECTS_ID_NONE;

    for (i = 0; i < sizeof(bdbuf_tests) / sizeof(bdbuf_tests[0]); i++)
    {
        bdbuf_tests[i].main();
    }
}


rtems_status_code
bdbuf_test_create_drv_rx_queue(Objects_Id *id)
{
    return rtems_message_queue_create(TEST_DRV_RX_MQUEUE_NAME,
                                      TEST_DRV_RX_MQUEUE_COUNT,
                                      sizeof(bdbuf_test_msg),
                                      RTEMS_DEFAULT_ATTRIBUTES,
                                      id);
}

rtems_status_code
bdbuf_test_create_drv_tx_queue(Objects_Id *id)
{
    return  rtems_message_queue_ident(TEST_TASK_RX_MQUEUE_NAME,
                                      RTEMS_SEARCH_ALL_NODES,
                                      id);
}

rtems_status_code
bdbuf_test_start_thread(unsigned int idx, rtems_task_entry func)
{
    rtems_status_code sc;

    if (g_test_ctx.test_task[idx] != OBJECTS_ID_NONE)
    {
        sc = rtems_task_delete(g_test_ctx.test_task[idx]);
        if (sc != RTEMS_SUCCESSFUL)
        {
            printf("Failed to delete test thread %u in test %s\n",
                   idx + 1, g_test_ctx.test_name);
            return sc;
        }
    }
    sc = bdbuf_test_start_aux_task(
            rtems_build_name('T', 'S', '.', '0' + idx),
            func, (rtems_task_argument)(NULL),
            &g_test_ctx.test_task[idx]);
    return sc;
}

rtems_status_code
bdbuf_test_end()
{
    rtems_status_code sc;
    unsigned int      i;

    for (i = 0; i < ARRAY_NUM(g_test_ctx.test_task); i++)
    {
        if (g_test_ctx.test_task[i] != OBJECTS_ID_NONE)
        {
            sc = rtems_semaphore_obtain(g_test_ctx.test_end_main,
                                        RTEMS_WAIT, RTEMS_NO_TIMEOUT);
            if (sc != RTEMS_SUCCESSFUL)
            {
                printf("Failed to get a thread stopped\n");
            }
            g_test_ctx.test_task[i] = OBJECTS_ID_NONE;
        }
    }
    return RTEMS_SUCCESSFUL;
}