Subversion Repositories HelenOS

Rev

Rev 3150 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3150 Rev 4345
1
/*
1
/*
2
 * Copyright (c) 2007 Josef Cejka
2
 * Copyright (c) 2007 Josef Cejka
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
#include <stdio.h>
29
#include <stdio.h>
30
#include <unistd.h>
30
#include <unistd.h>
31
#include <ipc/ipc.h>
31
#include <ipc/ipc.h>
32
#include <ipc/services.h>
32
#include <ipc/services.h>
33
#include <async.h>
33
#include <async.h>
34
#include <errno.h>
34
#include <errno.h>
35
#include <ipc/devmap.h>
35
#include <ipc/devmap.h>
36
#include "../tester.h"
36
#include "../tester.h"
37
 
37
 
38
#include <time.h>
38
#include <time.h>
39
 
39
 
40
#define TEST_DEVICE1 "TestDevice1"
40
#define TEST_DEVICE1 "TestDevice1"
41
#define TEST_DEVICE2 "TestDevice2"
41
#define TEST_DEVICE2 "TestDevice2"
42
 
42
 
43
/** Handle requests from clients
43
/** Handle requests from clients
44
 *
44
 *
45
 */
45
 */
46
static void driver_client_connection(ipc_callid_t iid, ipc_call_t *icall)
46
static void driver_client_connection(ipc_callid_t iid, ipc_call_t *icall)
47
{
47
{
48
    ipc_callid_t callid;
48
    ipc_callid_t callid;
49
    ipc_call_t call;
49
    ipc_call_t call;
50
    int retval;
50
    int retval;
51
   
51
   
52
    printf("connected: method=%u arg1=%u, arg2=%u arg3=%u.\n",
52
    printf("connected: method=%u arg1=%u, arg2=%u arg3=%u.\n",
53
        IPC_GET_METHOD(*icall), IPC_GET_ARG1(*icall), IPC_GET_ARG2(*icall),
53
        IPC_GET_METHOD(*icall), IPC_GET_ARG1(*icall), IPC_GET_ARG2(*icall),
54
        IPC_GET_ARG3(*icall));
54
        IPC_GET_ARG3(*icall));
55
 
55
 
56
    printf("driver_client_connection.\n");
56
    printf("driver_client_connection.\n");
57
    ipc_answer_0(iid, EOK);
57
    ipc_answer_0(iid, EOK);
58
 
58
 
59
    /* Ignore parameters, the connection is already opened */
59
    /* Ignore parameters, the connection is already opened */
60
    while (1) {
60
    while (1) {
61
        callid = async_get_call(&call);
61
        callid = async_get_call(&call);
62
        retval = EOK;
62
        retval = EOK;
63
        printf("method=%u arg1=%u, arg2=%u arg3=%u.\n",
63
        printf("method=%u arg1=%u, arg2=%u arg3=%u.\n",
64
            IPC_GET_METHOD(call), IPC_GET_ARG1(call),
64
            IPC_GET_METHOD(call), IPC_GET_ARG1(call),
65
            IPC_GET_ARG2(call), IPC_GET_ARG3(call));
65
            IPC_GET_ARG2(call), IPC_GET_ARG3(call));
66
        switch (IPC_GET_METHOD(call)) {
66
        switch (IPC_GET_METHOD(call)) {
67
        case IPC_M_PHONE_HUNGUP:
67
        case IPC_M_PHONE_HUNGUP:
68
            /* TODO: Handle hangup */
68
            /* TODO: Handle hangup */
69
            return;
69
            return;
70
        default:
70
        default:
71
            printf("Unknown device method %u.\n",
71
            printf("Unknown device method %u.\n",
72
                IPC_GET_METHOD(call));
72
                IPC_GET_METHOD(call));
73
            retval = ENOENT;
73
            retval = ENOENT;
74
        }
74
        }
75
        ipc_answer_0(callid, retval);
75
        ipc_answer_0(callid, retval);
76
    }
76
    }
77
    return;
77
    return;
78
}
78
}
79
 
79
 
80
static int device_client_fibril(void *arg)
80
static int device_client_fibril(void *arg)
81
{
81
{
82
    int handle;
82
    int handle;
83
    int device_phone;
83
    int device_phone;
84
 
84
 
85
    handle = (int)arg;
85
    handle = (int)arg;
86
 
86
 
87
    device_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
87
    device_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
88
        DEVMAP_CONNECT_TO_DEVICE, handle);
88
        DEVMAP_CONNECT_TO_DEVICE, handle);
89
 
89
 
90
    if (device_phone < 0) {
90
    if (device_phone < 0) {
91
        printf("Failed to connect to devmap as client (handle = %u).\n",
91
        printf("Failed to connect to devmap as client (handle = %u).\n",
92
            handle);
92
            handle);
93
        return -1;
93
        return -1;
94
    }
94
    }
95
/* 
95
/* 
96
 *  device_phone = (int) IPC_GET_ARG5(answer);
96
 *  device_phone = (int) IPC_GET_ARG5(answer);
97
 */
97
 */
98
    printf("Connected to device.\n");
98
    printf("Connected to device.\n");
99
    ipc_call_sync_1_0(device_phone, 1024, 1025);
99
    ipc_call_sync_1_0(device_phone, 1024, 1025);
100
/*
100
/*
101
 * ipc_hangup(device_phone);
101
 * ipc_hangup(device_phone);
102
 */
102
 */
103
    ipc_hangup(device_phone);
103
    ipc_hangup(device_phone);
104
 
104
 
105
    return EOK;
105
    return EOK;
106
}
106
}
107
 
107
 
108
/** Communication test with device.
108
/** Communication test with device.
109
 * @param handle handle to tested instance.
109
 * @param handle handle to tested instance.
110
 */
110
 */
111
static int device_client(int handle)
111
static int device_client(int handle)
112
{
112
{
113
/*  fid_t fid;
113
/*  fid_t fid;
114
    ipc_call_t call;
114
    ipc_call_t call;
115
    ipc_callid_t callid;
115
    ipc_callid_t callid;
116
 
116
 
117
    fid = fibril_create(device_client_fibril, (void *)handle);
117
    fid = fibril_create(device_client_fibril, (void *)handle);
118
    fibril_add_ready(fid);
118
    fibril_add_ready(fid);
119
 
119
 
120
*/
120
*/
121
    return EOK;
121
    return EOK;
122
}
122
}
123
 
123
 
124
/**
124
/**
125
 *
125
 *
126
 */
126
 */
127
static int driver_register(char *name)
127
static int driver_register(char *name)
128
{
128
{
129
    ipcarg_t retval;
129
    ipcarg_t retval;
130
    aid_t req;
130
    aid_t req;
131
    ipc_call_t answer;
131
    ipc_call_t answer;
132
    int phone;
132
    int phone;
133
    ipcarg_t callback_phonehash;
133
    ipcarg_t callback_phonehash;
134
 
134
 
135
    phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_DRIVER, 0);
135
    phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP, DEVMAP_DRIVER, 0);
136
 
-
 
137
    while (phone < 0) {
136
    if (phone < 0) {
138
        usleep(100000);
-
 
139
        phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
137
        printf("Failed to connect to device mapper\n");
140
            DEVMAP_DRIVER, 0);
138
        return -1;
141
    }
139
    }
142
   
140
   
143
    req = async_send_2(phone, DEVMAP_DRIVER_REGISTER, 0, 0, &answer);
141
    req = async_send_2(phone, DEVMAP_DRIVER_REGISTER, 0, 0, &answer);
144
 
142
 
145
    retval = ipc_data_write_start(phone, (char *)name, strlen(name) + 1);
143
    retval = ipc_data_write_start(phone, (char *)name, strlen(name) + 1);
146
 
144
 
147
    if (retval != EOK) {
145
    if (retval != EOK) {
148
        async_wait_for(req, NULL);
146
        async_wait_for(req, NULL);
149
        return -1;
147
        return -1;
150
    }
148
    }
151
 
149
 
152
    async_set_client_connection(driver_client_connection);
150
    async_set_client_connection(driver_client_connection);
153
 
151
 
154
    ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash);
152
    ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash);
155
/* 
153
/* 
156
    if (NULL == async_new_connection(callback_phonehash, 0, NULL,
154
    if (NULL == async_new_connection(callback_phonehash, 0, NULL,
157
            driver_client_connection)) {
155
            driver_client_connection)) {
158
        printf("Failed to create new fibril.\n");  
156
        printf("Failed to create new fibril.\n");  
159
        async_wait_for(req, NULL);
157
        async_wait_for(req, NULL);
160
        return -1;
158
        return -1;
161
    }
159
    }
162
*/
160
*/
163
    async_wait_for(req, &retval);
161
    async_wait_for(req, &retval);
164
    printf("Driver '%s' registered.\n", name);
162
    printf("Driver '%s' registered.\n", name);
165
 
163
 
166
    return phone;
164
    return phone;
167
}
165
}
168
 
166
 
169
static int device_get_handle(int driver_phone, char *name, int *handle)
167
static int device_get_handle(int driver_phone, char *name, int *handle)
170
{
168
{
171
    ipcarg_t retval;
169
    ipcarg_t retval;
172
    aid_t req;
170
    aid_t req;
173
    ipc_call_t answer;
171
    ipc_call_t answer;
174
 
172
 
175
    req = async_send_2(driver_phone, DEVMAP_DEVICE_GET_HANDLE, 0, 0,
173
    req = async_send_2(driver_phone, DEVMAP_DEVICE_GET_HANDLE, 0, 0,
176
        &answer);
174
        &answer);
177
 
175
 
178
    retval = ipc_data_write_start(driver_phone, name, strlen(name) + 1);
176
    retval = ipc_data_write_start(driver_phone, name, strlen(name) + 1);
179
 
177
 
180
    if (retval != EOK) {
178
    if (retval != EOK) {
181
        printf("Failed to send device name '%s'.\n", name);
179
        printf("Failed to send device name '%s'.\n", name);
182
        async_wait_for(req, NULL);
180
        async_wait_for(req, NULL);
183
        return retval;
181
        return retval;
184
    }
182
    }
185
 
183
 
186
    async_wait_for(req, &retval);
184
    async_wait_for(req, &retval);
187
 
185
 
188
    if (NULL != handle) {
186
    if (NULL != handle) {
189
        *handle = -1;
187
        *handle = -1;
190
    }
188
    }
191
 
189
 
192
    if (EOK == retval) {
190
    if (EOK == retval) {
193
       
191
       
194
        if (NULL != handle) {
192
        if (NULL != handle) {
195
            *handle = (int) IPC_GET_ARG1(answer);
193
            *handle = (int) IPC_GET_ARG1(answer);
196
        }
194
        }
197
        printf("Device '%s' has handle %u.\n", name,
195
        printf("Device '%s' has handle %u.\n", name,
198
            (int) IPC_GET_ARG1(answer));
196
            (int) IPC_GET_ARG1(answer));
199
    } else {
197
    } else {
200
        printf("Failed to get handle for device '%s'.\n", name);
198
        printf("Failed to get handle for device '%s'.\n", name);
201
    }
199
    }
202
 
200
 
203
    return retval;
201
    return retval;
204
}
202
}
205
 
203
 
206
/** Register new device.
204
/** Register new device.
207
 * @param driver_phone
205
 * @param driver_phone
208
 * @param name Device name.
206
 * @param name Device name.
209
 * @param handle Output variable. Handle to the created instance of device.
207
 * @param handle Output variable. Handle to the created instance of device.
210
 */
208
 */
211
static int device_register(int driver_phone, char *name, int *handle)
209
static int device_register(int driver_phone, char *name, int *handle)
212
{
210
{
213
    ipcarg_t retval;
211
    ipcarg_t retval;
214
    aid_t req;
212
    aid_t req;
215
    ipc_call_t answer;
213
    ipc_call_t answer;
216
 
214
 
217
    req = async_send_2(driver_phone, DEVMAP_DEVICE_REGISTER, 0, 0, &answer);
215
    req = async_send_2(driver_phone, DEVMAP_DEVICE_REGISTER, 0, 0, &answer);
218
 
216
 
219
    retval = ipc_data_write_start(driver_phone, (char *)name,
217
    retval = ipc_data_write_start(driver_phone, (char *)name,
220
        strlen(name) + 1);
218
        strlen(name) + 1);
221
 
219
 
222
    if (retval != EOK) {
220
    if (retval != EOK) {
223
        printf("Failed to send device name '%s'.\n", name);
221
        printf("Failed to send device name '%s'.\n", name);
224
        async_wait_for(req, NULL);
222
        async_wait_for(req, NULL);
225
        return retval;
223
        return retval;
226
    }
224
    }
227
 
225
 
228
    async_wait_for(req, &retval);
226
    async_wait_for(req, &retval);
229
 
227
 
230
    if (NULL != handle) {
228
    if (NULL != handle) {
231
        *handle = -1;
229
        *handle = -1;
232
    }
230
    }
233
 
231
 
234
    if (EOK == retval) {
232
    if (EOK == retval) {
235
       
233
       
236
        if (NULL != handle) {
234
        if (NULL != handle) {
237
            *handle = (int) IPC_GET_ARG1(answer);
235
            *handle = (int) IPC_GET_ARG1(answer);
238
        }
236
        }
239
        printf("Device registered with handle %u.\n",
237
        printf("Device registered with handle %u.\n",
240
            (int) IPC_GET_ARG1(answer));
238
            (int) IPC_GET_ARG1(answer));
241
    }
239
    }
242
 
240
 
243
    return retval;
241
    return retval;
244
}
242
}
245
 
243
 
246
/** Test DevMap from the driver's point of view.
244
/** Test DevMap from the driver's point of view.
247
 *
245
 *
248
 *
246
 *
249
 */
247
 */
250
char * test_devmap1(bool quiet)
248
char * test_devmap1(bool quiet)
251
{
249
{
252
    int driver_phone;
250
    int driver_phone;
253
    int dev1_handle;
251
    int dev1_handle;
254
    int dev2_handle;
252
    int dev2_handle;
255
    int dev3_handle;
253
    int dev3_handle;
256
    int handle;
254
    int handle;
257
 
255
 
258
    /* Register new driver */
256
    /* Register new driver */
259
    driver_phone = driver_register("TestDriver");
257
    driver_phone = driver_register("TestDriver");
260
 
258
 
261
    if (driver_phone < 0) {
259
    if (driver_phone < 0) {
262
        return "Error: Cannot register driver.\n"; 
260
        return "Error: Cannot register driver.\n"; 
263
    }
261
    }
264
 
262
 
265
    /* Register new device dev1*/
263
    /* Register new device dev1*/
266
    if (EOK != device_register(driver_phone, TEST_DEVICE1, &dev1_handle)) {
264
    if (EOK != device_register(driver_phone, TEST_DEVICE1, &dev1_handle)) {
267
        ipc_hangup(driver_phone);
265
        ipc_hangup(driver_phone);
268
        return "Error: cannot register device.\n";
266
        return "Error: cannot register device.\n";
269
    }
267
    }
270
 
268
 
271
    /* Get handle for dev2 (Should fail unless device is already
269
    /* Get handle for dev2 (Should fail unless device is already
272
     * registered by someone else)
270
     * registered by someone else)
273
     */
271
     */
274
    if (EOK == device_get_handle(driver_phone, TEST_DEVICE2, &handle)) {
272
    if (EOK == device_get_handle(driver_phone, TEST_DEVICE2, &handle)) {
275
        ipc_hangup(driver_phone);
273
        ipc_hangup(driver_phone);
276
        return "Error: got handle for dev2 before it was registered.\n";
274
        return "Error: got handle for dev2 before it was registered.\n";
277
    }
275
    }
278
 
276
 
279
    /* Register new device dev2*/
277
    /* Register new device dev2*/
280
    if (EOK != device_register(driver_phone, TEST_DEVICE2, &dev2_handle)) {
278
    if (EOK != device_register(driver_phone, TEST_DEVICE2, &dev2_handle)) {
281
        ipc_hangup(driver_phone);
279
        ipc_hangup(driver_phone);
282
        return "Error: cannot register device dev2.\n";
280
        return "Error: cannot register device dev2.\n";
283
    }
281
    }
284
 
282
 
285
    /* Register again device dev1 */
283
    /* Register again device dev1 */
286
    if (EOK == device_register(driver_phone, TEST_DEVICE1, &dev3_handle)) {
284
    if (EOK == device_register(driver_phone, TEST_DEVICE1, &dev3_handle)) {
287
        return "Error: dev1 registered twice.\n";
285
        return "Error: dev1 registered twice.\n";
288
    }
286
    }
289
 
287
 
290
    /* Get handle for dev1*/
288
    /* Get handle for dev1*/
291
    if (EOK != device_get_handle(driver_phone, TEST_DEVICE1, &handle)) {
289
    if (EOK != device_get_handle(driver_phone, TEST_DEVICE1, &handle)) {
292
        ipc_hangup(driver_phone);
290
        ipc_hangup(driver_phone);
293
        return "Error: cannot get handle for 'DEVMAP_DEVICE1'.\n";
291
        return "Error: cannot get handle for 'DEVMAP_DEVICE1'.\n";
294
    }
292
    }
295
 
293
 
296
    if (handle != dev1_handle) {
294
    if (handle != dev1_handle) {
297
        ipc_hangup(driver_phone);
295
        ipc_hangup(driver_phone);
298
        return "Error: cannot get handle for 'DEVMAP_DEVICE1'.\n";
296
        return "Error: cannot get handle for 'DEVMAP_DEVICE1'.\n";
299
    }
297
    }
300
 
298
 
301
    if (EOK != device_client(dev1_handle)) {
299
    if (EOK != device_client(dev1_handle)) {
302
        ipc_hangup(driver_phone);
300
        ipc_hangup(driver_phone);
303
        return "Error: failed client test for 'DEVMAP_DEVICE1'.\n";
301
        return "Error: failed client test for 'DEVMAP_DEVICE1'.\n";
304
    }
302
    }
305
 
303
 
306
    /* TODO: */
304
    /* TODO: */
307
 
305
 
308
    ipc_hangup(driver_phone);
306
    ipc_hangup(driver_phone);
309
 
307
 
310
    return NULL;
308
    return NULL;
311
}
309
}
312
 
310
 
313
char *test_devmap2(bool quiet)
311
char *test_devmap2(bool quiet)
314
{
312
{
315
    /*TODO: Full automatic test */
313
    /*TODO: Full automatic test */
316
    return NULL;
314
    return NULL;
317
}
315
}
318
 
316
 
319
char *test_devmap3(bool quiet)
317
char *test_devmap3(bool quiet)
320
{
318
{
321
    /* TODO: allow user to call test functions in random order */
319
    /* TODO: allow user to call test functions in random order */
322
    return NULL;
320
    return NULL;
323
}
321
}
324
 
322
 
325
 
323