Subversion Repositories HelenOS

Rev

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

Rev 2927 Rev 3150
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 <../../../srv/devmap/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(PHONE_NS, SERVICE_DEVMAP, DEVMAP_DRIVER, 0);
136
 
136
 
137
    while (phone < 0) {
137
    while (phone < 0) {
138
        usleep(100000);
138
        usleep(100000);
139
        phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
139
        phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
140
            DEVMAP_DRIVER, 0);
140
            DEVMAP_DRIVER, 0);
141
    }
141
    }
142
   
142
   
143
    req = async_send_2(phone, DEVMAP_DRIVER_REGISTER, 0, 0, &answer);
143
    req = async_send_2(phone, DEVMAP_DRIVER_REGISTER, 0, 0, &answer);
144
 
144
 
145
    retval = ipc_data_write_start(phone, (char *)name, strlen(name) + 1);
145
    retval = ipc_data_write_start(phone, (char *)name, strlen(name) + 1);
146
 
146
 
147
    if (retval != EOK) {
147
    if (retval != EOK) {
148
        async_wait_for(req, NULL);
148
        async_wait_for(req, NULL);
149
        return -1;
149
        return -1;
150
    }
150
    }
151
 
151
 
152
    async_set_client_connection(driver_client_connection);
152
    async_set_client_connection(driver_client_connection);
153
 
153
 
154
    ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash);
154
    ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash);
155
/* 
155
/* 
156
    if (NULL == async_new_connection(callback_phonehash, 0, NULL,
156
    if (NULL == async_new_connection(callback_phonehash, 0, NULL,
157
            driver_client_connection)) {
157
            driver_client_connection)) {
158
        printf("Failed to create new fibril.\n");  
158
        printf("Failed to create new fibril.\n");  
159
        async_wait_for(req, NULL);
159
        async_wait_for(req, NULL);
160
        return -1;
160
        return -1;
161
    }
161
    }
162
*/
162
*/
163
    async_wait_for(req, &retval);
163
    async_wait_for(req, &retval);
164
    printf("Driver '%s' registered.\n", name);
164
    printf("Driver '%s' registered.\n", name);
165
 
165
 
166
    return phone;
166
    return phone;
167
}
167
}
168
 
168
 
169
static int device_get_handle(int driver_phone, char *name, int *handle)
169
static int device_get_handle(int driver_phone, char *name, int *handle)
170
{
170
{
171
    ipcarg_t retval;
171
    ipcarg_t retval;
172
    aid_t req;
172
    aid_t req;
173
    ipc_call_t answer;
173
    ipc_call_t answer;
174
 
174
 
175
    req = async_send_2(driver_phone, DEVMAP_DEVICE_GET_HANDLE, 0, 0,
175
    req = async_send_2(driver_phone, DEVMAP_DEVICE_GET_HANDLE, 0, 0,
176
        &answer);
176
        &answer);
177
 
177
 
178
    retval = ipc_data_write_start(driver_phone, name, strlen(name) + 1);
178
    retval = ipc_data_write_start(driver_phone, name, strlen(name) + 1);
179
 
179
 
180
    if (retval != EOK) {
180
    if (retval != EOK) {
181
        printf("Failed to send device name '%s'.\n", name);
181
        printf("Failed to send device name '%s'.\n", name);
182
        async_wait_for(req, NULL);
182
        async_wait_for(req, NULL);
183
        return retval;
183
        return retval;
184
    }
184
    }
185
 
185
 
186
    async_wait_for(req, &retval);
186
    async_wait_for(req, &retval);
187
 
187
 
188
    if (NULL != handle) {
188
    if (NULL != handle) {
189
        *handle = -1;
189
        *handle = -1;
190
    }
190
    }
191
 
191
 
192
    if (EOK == retval) {
192
    if (EOK == retval) {
193
       
193
       
194
        if (NULL != handle) {
194
        if (NULL != handle) {
195
            *handle = (int) IPC_GET_ARG1(answer);
195
            *handle = (int) IPC_GET_ARG1(answer);
196
        }
196
        }
197
        printf("Device '%s' has handle %u.\n", name,
197
        printf("Device '%s' has handle %u.\n", name,
198
            (int) IPC_GET_ARG1(answer));
198
            (int) IPC_GET_ARG1(answer));
199
    } else {
199
    } else {
200
        printf("Failed to get handle for device '%s'.\n", name);
200
        printf("Failed to get handle for device '%s'.\n", name);
201
    }
201
    }
202
 
202
 
203
    return retval;
203
    return retval;
204
}
204
}
205
 
205
 
206
/** Register new device.
206
/** Register new device.
207
 * @param driver_phone
207
 * @param driver_phone
208
 * @param name Device name.
208
 * @param name Device name.
209
 * @param handle Output variable. Handle to the created instance of device.
209
 * @param handle Output variable. Handle to the created instance of device.
210
 */
210
 */
211
static int device_register(int driver_phone, char *name, int *handle)
211
static int device_register(int driver_phone, char *name, int *handle)
212
{
212
{
213
    ipcarg_t retval;
213
    ipcarg_t retval;
214
    aid_t req;
214
    aid_t req;
215
    ipc_call_t answer;
215
    ipc_call_t answer;
216
 
216
 
217
    req = async_send_2(driver_phone, DEVMAP_DEVICE_REGISTER, 0, 0, &answer);
217
    req = async_send_2(driver_phone, DEVMAP_DEVICE_REGISTER, 0, 0, &answer);
218
 
218
 
219
    retval = ipc_data_write_start(driver_phone, (char *)name,
219
    retval = ipc_data_write_start(driver_phone, (char *)name,
220
        strlen(name) + 1);
220
        strlen(name) + 1);
221
 
221
 
222
    if (retval != EOK) {
222
    if (retval != EOK) {
223
        printf("Failed to send device name '%s'.\n", name);
223
        printf("Failed to send device name '%s'.\n", name);
224
        async_wait_for(req, NULL);
224
        async_wait_for(req, NULL);
225
        return retval;
225
        return retval;
226
    }
226
    }
227
 
227
 
228
    async_wait_for(req, &retval);
228
    async_wait_for(req, &retval);
229
 
229
 
230
    if (NULL != handle) {
230
    if (NULL != handle) {
231
        *handle = -1;
231
        *handle = -1;
232
    }
232
    }
233
 
233
 
234
    if (EOK == retval) {
234
    if (EOK == retval) {
235
       
235
       
236
        if (NULL != handle) {
236
        if (NULL != handle) {
237
            *handle = (int) IPC_GET_ARG1(answer);
237
            *handle = (int) IPC_GET_ARG1(answer);
238
        }
238
        }
239
        printf("Device registered with handle %u.\n",
239
        printf("Device registered with handle %u.\n",
240
            (int) IPC_GET_ARG1(answer));
240
            (int) IPC_GET_ARG1(answer));
241
    }
241
    }
242
 
242
 
243
    return retval;
243
    return retval;
244
}
244
}
245
 
245
 
246
/** Test DevMap from the driver's point of view.
246
/** Test DevMap from the driver's point of view.
247
 *
247
 *
248
 *
248
 *
249
 */
249
 */
250
char * test_devmap1(bool quiet)
250
char * test_devmap1(bool quiet)
251
{
251
{
252
    int driver_phone;
252
    int driver_phone;
253
    int dev1_handle;
253
    int dev1_handle;
254
    int dev2_handle;
254
    int dev2_handle;
255
    int dev3_handle;
255
    int dev3_handle;
256
    int handle;
256
    int handle;
257
 
257
 
258
    /* Register new driver */
258
    /* Register new driver */
259
    driver_phone = driver_register("TestDriver");
259
    driver_phone = driver_register("TestDriver");
260
 
260
 
261
    if (driver_phone < 0) {
261
    if (driver_phone < 0) {
262
        return "Error: Cannot register driver.\n"; 
262
        return "Error: Cannot register driver.\n"; 
263
    }
263
    }
264
 
264
 
265
    /* Register new device dev1*/
265
    /* Register new device dev1*/
266
    if (EOK != device_register(driver_phone, TEST_DEVICE1, &dev1_handle)) {
266
    if (EOK != device_register(driver_phone, TEST_DEVICE1, &dev1_handle)) {
267
        ipc_hangup(driver_phone);
267
        ipc_hangup(driver_phone);
268
        return "Error: cannot register device.\n";
268
        return "Error: cannot register device.\n";
269
    }
269
    }
270
 
270
 
271
    /* Get handle for dev2 (Should fail unless device is already
271
    /* Get handle for dev2 (Should fail unless device is already
272
     * registered by someone else)
272
     * registered by someone else)
273
     */
273
     */
274
    if (EOK == device_get_handle(driver_phone, TEST_DEVICE2, &handle)) {
274
    if (EOK == device_get_handle(driver_phone, TEST_DEVICE2, &handle)) {
275
        ipc_hangup(driver_phone);
275
        ipc_hangup(driver_phone);
276
        return "Error: got handle for dev2 before it was registered.\n";
276
        return "Error: got handle for dev2 before it was registered.\n";
277
    }
277
    }
278
 
278
 
279
    /* Register new device dev2*/
279
    /* Register new device dev2*/
280
    if (EOK != device_register(driver_phone, TEST_DEVICE2, &dev2_handle)) {
280
    if (EOK != device_register(driver_phone, TEST_DEVICE2, &dev2_handle)) {
281
        ipc_hangup(driver_phone);
281
        ipc_hangup(driver_phone);
282
        return "Error: cannot register device dev2.\n";
282
        return "Error: cannot register device dev2.\n";
283
    }
283
    }
284
 
284
 
285
    /* Register again device dev1 */
285
    /* Register again device dev1 */
286
    if (EOK == device_register(driver_phone, TEST_DEVICE1, &dev3_handle)) {
286
    if (EOK == device_register(driver_phone, TEST_DEVICE1, &dev3_handle)) {
287
        return "Error: dev1 registered twice.\n";
287
        return "Error: dev1 registered twice.\n";
288
    }
288
    }
289
 
289
 
290
    /* Get handle for dev1*/
290
    /* Get handle for dev1*/
291
    if (EOK != device_get_handle(driver_phone, TEST_DEVICE1, &handle)) {
291
    if (EOK != device_get_handle(driver_phone, TEST_DEVICE1, &handle)) {
292
        ipc_hangup(driver_phone);
292
        ipc_hangup(driver_phone);
293
        return "Error: cannot get handle for 'DEVMAP_DEVICE1'.\n";
293
        return "Error: cannot get handle for 'DEVMAP_DEVICE1'.\n";
294
    }
294
    }
295
 
295
 
296
    if (handle != dev1_handle) {
296
    if (handle != dev1_handle) {
297
        ipc_hangup(driver_phone);
297
        ipc_hangup(driver_phone);
298
        return "Error: cannot get handle for 'DEVMAP_DEVICE1'.\n";
298
        return "Error: cannot get handle for 'DEVMAP_DEVICE1'.\n";
299
    }
299
    }
300
 
300
 
301
    if (EOK != device_client(dev1_handle)) {
301
    if (EOK != device_client(dev1_handle)) {
302
        ipc_hangup(driver_phone);
302
        ipc_hangup(driver_phone);
303
        return "Error: failed client test for 'DEVMAP_DEVICE1'.\n";
303
        return "Error: failed client test for 'DEVMAP_DEVICE1'.\n";
304
    }
304
    }
305
 
305
 
306
    /* TODO: */
306
    /* TODO: */
307
 
307
 
308
    ipc_hangup(driver_phone);
308
    ipc_hangup(driver_phone);
309
 
309
 
310
    return NULL;
310
    return NULL;
311
}
311
}
312
 
312
 
313
char *test_devmap2(bool quiet)
313
char *test_devmap2(bool quiet)
314
{
314
{
315
    /*TODO: Full automatic test */
315
    /*TODO: Full automatic test */
316
    return NULL;
316
    return NULL;
317
}
317
}
318
 
318
 
319
char *test_devmap3(bool quiet)
319
char *test_devmap3(bool quiet)
320
{
320
{
321
    /* TODO: allow user to call test functions in random order */
321
    /* TODO: allow user to call test functions in random order */
322
    return NULL;
322
    return NULL;
323
}
323
}
324
 
324
 
325
 
325