Subversion Repositories HelenOS

Rev

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

Rev 2550 Rev 2555
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
/**
29
/**
30
 * @defgroup devmap Device mapper.
30
 * @defgroup devmap Device mapper.
31
 * @brief   HelenOS device mapper.
31
 * @brief   HelenOS device mapper.
32
 * @{
32
 * @{
33
 */
33
 */
34
 
34
 
35
/** @file
35
/** @file
36
 */
36
 */
37
 
37
 
38
#include <ipc/services.h>
38
#include <ipc/services.h>
39
#include <ipc/ns.h>
39
#include <ipc/ns.h>
40
#include <async.h>
40
#include <async.h>
41
#include <stdio.h>
41
#include <stdio.h>
42
#include <errno.h>
42
#include <errno.h>
-
 
43
#include <bool.h>
-
 
44
#include <libadt/list.h>
-
 
45
#include <futex.h>
43
 
46
 
44
#include "devmap.h"
47
#include "devmap.h"
45
 
48
 
-
 
49
LIST_INITIALIZE(device_list);
-
 
50
 
-
 
51
atomic_t device_list_futex = FUTEX_INITIALIZER;
-
 
52
 
46
/** Initialize device mapper.
53
/** Initialize device mapper.
47
 *
54
 *
48
 *
55
 *
49
 */
56
 */
50
static int devmap_init()
57
static int devmap_init()
51
{
58
{
52
    /* */
59
    /* */
53
 
60
 
54
    return 0;
61
    return EOK;
-
 
62
}
-
 
63
 
-
 
64
static int devmap_register()
-
 
65
{
-
 
66
    ipc_callid_t callid2;
-
 
67
    size_t size;
-
 
68
    char buffer[DEVMAP_NAME_MAXLEN + 1];
-
 
69
    ipc_callid_t callid;
-
 
70
    ipc_call_t call;
-
 
71
    int retval;
-
 
72
   
-
 
73
    if (ipc_data_receive(&callid, &call, NULL, &size) != 0) {
-
 
74
    //  retval = 
-
 
75
    }
-
 
76
 
-
 
77
    if (size > DEVMAP_NAME_MAXLEN) {
-
 
78
        retval = ELIMIT;
-
 
79
    } else {
-
 
80
        ipc_data_deliver(callid2, &call, buffer, size);
-
 
81
        buffer[DEVMAP_NAME_MAXLEN] = 0;
-
 
82
    }
-
 
83
    return EOK;
-
 
84
}
-
 
85
 
-
 
86
static int devmap_unregister()
-
 
87
{
-
 
88
    return EOK;
-
 
89
}
-
 
90
 
-
 
91
static int devmap_forward(int handle, ipc_call_t *call, ipc_callid_t callid)
-
 
92
{
-
 
93
    link_t *item;
-
 
94
    ipcarg_t phone;
-
 
95
    devmap_device_t *dev;
-
 
96
 
-
 
97
    /* FIXME: add futex */
-
 
98
    futex_down(&device_list_futex);
-
 
99
 
-
 
100
    item = (&device_list)->next;
-
 
101
 
-
 
102
    while (item != &device_list) {
-
 
103
 
-
 
104
        dev = list_get_instance(item, devmap_device_t, list);
-
 
105
        if (dev->handle == handle) {
-
 
106
            break;
-
 
107
        }
-
 
108
        item = item->next;
-
 
109
    }
-
 
110
 
-
 
111
    if (item == &device_list) {
-
 
112
        return ENOENT;
-
 
113
    }
-
 
114
 
-
 
115
    dev = list_get_instance(item, devmap_device_t, list);
-
 
116
    phone = dev->phone;
-
 
117
 
-
 
118
    futex_up(&device_list_futex);
-
 
119
   
-
 
120
    return ipc_forward_fast(callid, phone, 0, 0);
55
}
121
}
56
 
122
 
-
 
123
static int devmap_get_handle()
-
 
124
{
-
 
125
    return EOK;
-
 
126
}
57
 
127
 
58
/** Function for handling connections to devmap
128
/** Function for handling connections to devmap
59
 *
129
 *
60
 */
130
 */
61
static void
131
static void
62
devmap_client_connection(ipc_callid_t iid, ipc_call_t *icall)
132
devmap_client_connection(ipc_callid_t iid, ipc_call_t *icall)
63
{
133
{
64
    ipc_callid_t callid;
134
    ipc_callid_t callid;
65
    ipc_call_t call;
135
    ipc_call_t call;
66
    int retval;
136
    int retval;
-
 
137
    bool cont = true;
-
 
138
 
-
 
139
    printf("DevMap: new connection.");
67
 
140
 
68
    ipc_answer_fast(iid, EOK, 0, 0); /* Accept connection */
141
    ipc_answer_fast(iid, EOK, 0, 0); /* Accept connection */
69
 
142
 
70
    while (1) {
143
    while (cont) {
71
        callid = async_get_call(&call);
144
        callid = async_get_call(&call);
72
 
145
 
73
        switch (IPC_GET_METHOD(call)) {
146
        switch (IPC_GET_METHOD(call)) {
74
        case IPC_M_PHONE_HUNGUP:
147
        case IPC_M_PHONE_HUNGUP:
75
                /* TODO: if its a device connection, remove it from table */
148
                /* TODO: if its a device connection, remove it from table */
-
 
149
            devmap_unregister();
-
 
150
            printf("DevMap: connection hung up.");
-
 
151
            cont = false;
76
            return; /* Exit thread */
152
            continue; /* Exit thread */
77
 
153
 
78
        case DEVMAP_REGISTER:
154
        case DEVMAP_REGISTER:
-
 
155
           
-
 
156
            if ((retval = devmap_register()) != EOK) {
79
            /* TODO: */
157
                cont = false;
-
 
158
            }
-
 
159
            break;
80
        case DEVMAP_UNREGISTER:
160
        case DEVMAP_UNREGISTER:
-
 
161
            /* TODO: remove device (if registred) */
-
 
162
            retval = devmap_unregister();
81
            /* TODO: */
163
            cont = false;
-
 
164
            break;
82
        case DEVMAP_CONNECT_TO_DEVICE:
165
        case DEVMAP_CONNECT_TO_DEVICE:
-
 
166
            retval = devmap_forward(IPC_GET_ARG1(call), &call, callid);
83
            /* TODO: */
167
            cont = false;
84
            retval = 0;
168
            break;
-
 
169
        case DEVMAP_GET_HANDLE:
-
 
170
           
85
            break;
171
            break;
86
        default:
172
        default:
87
            retval = ENOENT;
173
            retval = ENOENT;
88
        }
174
        }
89
        ipc_answer_fast(callid, retval, 0, 0);
175
        ipc_answer_fast(callid, retval, 0, 0);
90
    }
176
    }
91
       
177
       
-
 
178
    printf("DevMap: connection closed.");
-
 
179
    return;
92
}
180
}
93
 
181
 
94
 
182
 
95
int main(int argc, char *argv[])
183
int main(int argc, char *argv[])
96
{
184
{
97
 
185
 
98
    ipcarg_t phonead;
186
    ipcarg_t phonead;
99
 
187
 
100
    printf("DevMap: HelenOS device mapper.\n");
188
    printf("DevMap: HelenOS device mapper.\n");
101
 
189
 
102
    if (devmap_init() != 0) {
190
    if (devmap_init() != 0) {
103
        printf("Error while initializing DevMap service.");
191
        printf("Error while initializing DevMap service.");
104
        return -1;
192
        return -1;
105
    }
193
    }
106
 
194
 
107
        /* Set a handler of incomming connections */
195
        /* Set a handler of incomming connections */
108
    async_set_client_connection(devmap_client_connection);
196
    async_set_client_connection(devmap_client_connection);
109
 
197
 
110
        /* Register device mapper at naming service */
198
        /* Register device mapper at naming service */
111
    if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAP, 0, &phonead) != 0)
199
    if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAP, 0, &phonead) != 0)
112
        return -1;
200
        return -1;
113
   
201
   
114
    async_manager();
202
    async_manager();
115
    /* Never reached */
203
    /* Never reached */
116
    return 0;
204
    return 0;
117
}
205
}
118
 
206
 
119
/**
207
/**
120
 * @}
208
 * @}
121
 */
209
 */
122
 
210
 
123
 
211