Subversion Repositories HelenOS

Rev

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

Rev 3443 Rev 3444
1
/*
1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
2
 * Copyright (c) 2008 Jiri Svoboda
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
/** @addtogroup trace
29
/** @addtogroup trace
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <stdio.h>
35
#include <stdio.h>
36
#include <stdlib.h>
36
#include <stdlib.h>
37
#include <libadt/hash_table.h>
37
#include <libadt/hash_table.h>
38
 
38
 
39
#include "ipc_desc.h"
39
#include "ipc_desc.h"
40
#include "proto.h"
40
#include "proto.h"
-
 
41
#include "trace.h"
41
#include "ipcp.h"
42
#include "ipcp.h"
42
 
43
 
43
#define IPCP_CALLID_SYNC 0
44
#define IPCP_CALLID_SYNC 0
44
 
45
 
45
typedef struct {
46
typedef struct {
46
    int phone_hash;
47
    int phone_hash;
47
    ipc_call_t question;
48
    ipc_call_t question;
48
 
49
 
49
    int call_hash;
50
    int call_hash;
50
 
51
 
51
    link_t link;
52
    link_t link;
52
} pending_call_t;
53
} pending_call_t;
53
 
54
 
54
typedef struct {
55
typedef struct {
55
    int server;
56
    int server;
56
    proto_t *proto;
57
    proto_t *proto;
57
} connection_t;
58
} connection_t;
58
 
59
 
59
#define MAX_PHONE 64
60
#define MAX_PHONE 64
60
connection_t connections[MAX_PHONE];
61
connection_t connections[MAX_PHONE];
61
int have_conn[MAX_PHONE];
62
int have_conn[MAX_PHONE];
62
 
63
 
63
#define PCALL_TABLE_CHAINS 32
64
#define PCALL_TABLE_CHAINS 32
64
hash_table_t pending_calls;
65
hash_table_t pending_calls;
65
 
66
 
66
/*
67
/*
67
 * Pseudo-protocols
68
 * Pseudo-protocols
68
 */
69
 */
69
proto_t *proto_system;      /**< Protocol describing system IPC methods. */
70
proto_t *proto_system;      /**< Protocol describing system IPC methods. */
70
proto_t *proto_unknown;     /**< Protocol with no known methods. */
71
proto_t *proto_unknown;     /**< Protocol with no known methods. */
71
 
72
 
72
static hash_index_t pending_call_hash(unsigned long key[]);
73
static hash_index_t pending_call_hash(unsigned long key[]);
73
static int pending_call_compare(unsigned long key[], hash_count_t keys,
74
static int pending_call_compare(unsigned long key[], hash_count_t keys,
74
    link_t *item);
75
    link_t *item);
75
static void pending_call_remove_callback(link_t *item);
76
static void pending_call_remove_callback(link_t *item);
76
 
77
 
77
hash_table_operations_t pending_call_ops = {
78
hash_table_operations_t pending_call_ops = {
78
    .hash = pending_call_hash,
79
    .hash = pending_call_hash,
79
    .compare = pending_call_compare,
80
    .compare = pending_call_compare,
80
    .remove_callback = pending_call_remove_callback
81
    .remove_callback = pending_call_remove_callback
81
};
82
};
82
 
83
 
83
 
84
 
84
static hash_index_t pending_call_hash(unsigned long key[])
85
static hash_index_t pending_call_hash(unsigned long key[])
85
{
86
{
86
//  printf("pending_call_hash\n");
87
//  printf("pending_call_hash\n");
87
    return key[0] % PCALL_TABLE_CHAINS;
88
    return key[0] % PCALL_TABLE_CHAINS;
88
}
89
}
89
 
90
 
90
static int pending_call_compare(unsigned long key[], hash_count_t keys,
91
static int pending_call_compare(unsigned long key[], hash_count_t keys,
91
    link_t *item)
92
    link_t *item)
92
{
93
{
93
    pending_call_t *hs;
94
    pending_call_t *hs;
94
 
95
 
95
//  printf("pending_call_compare\n");
96
//  printf("pending_call_compare\n");
96
    hs = hash_table_get_instance(item, pending_call_t, link);
97
    hs = hash_table_get_instance(item, pending_call_t, link);
97
 
98
 
98
    return key[0] == hs->call_hash;
99
    return key[0] == hs->call_hash;
99
}
100
}
100
 
101
 
101
static void pending_call_remove_callback(link_t *item)
102
static void pending_call_remove_callback(link_t *item)
102
{
103
{
103
//  printf("pending_call_remove_callback\n");
104
//  printf("pending_call_remove_callback\n");
104
}
105
}
105
 
106
 
106
 
107
 
107
void ipcp_connection_set(int phone, int server, proto_t *proto)
108
void ipcp_connection_set(int phone, int server, proto_t *proto)
108
{
109
{
109
    if (phone <0 || phone >= MAX_PHONE) return;
110
    if (phone <0 || phone >= MAX_PHONE) return;
110
    connections[phone].server = server;
111
    connections[phone].server = server;
111
    connections[phone].proto = proto;
112
    connections[phone].proto = proto;
112
    have_conn[phone] = 1;
113
    have_conn[phone] = 1;
113
}
114
}
114
 
115
 
115
void ipcp_connection_clear(int phone)
116
void ipcp_connection_clear(int phone)
116
{
117
{
117
    have_conn[phone] = 0;
118
    have_conn[phone] = 0;
118
    connections[phone].server = 0;
119
    connections[phone].server = 0;
119
    connections[phone].proto = NULL;
120
    connections[phone].proto = NULL;
120
}
121
}
121
 
122
 
122
static void ipc_m_print(proto_t *proto, ipcarg_t method)
123
static void ipc_m_print(proto_t *proto, ipcarg_t method)
123
{
124
{
124
    oper_t *oper;
125
    oper_t *oper;
125
 
126
 
126
    /* Try system methods first */
127
    /* Try system methods first */
127
    oper = proto_get_oper(proto_system, method);
128
    oper = proto_get_oper(proto_system, method);
128
 
129
 
129
    if (oper == NULL && proto != NULL) {
130
    if (oper == NULL && proto != NULL) {
130
        /* Not a system method, try the user protocol. */
131
        /* Not a system method, try the user protocol. */
131
        oper = proto_get_oper(proto, method);
132
        oper = proto_get_oper(proto, method);
132
    }
133
    }
133
 
134
 
134
    if (oper != NULL) {
135
    if (oper != NULL) {
135
        printf("%s (%d)", oper->name, method);
136
        printf("%s (%d)", oper->name, method);
136
        return;
137
        return;
137
    }
138
    }
138
 
139
 
139
    printf("%d", method);
140
    printf("%d", method);
140
}
141
}
141
 
142
 
142
void ipcp_init(void)
143
void ipcp_init(void)
143
{
144
{
144
    ipc_m_desc_t *desc;
145
    ipc_m_desc_t *desc;
145
    oper_t *oper;
146
    oper_t *oper;
146
 
147
 
147
    /*
148
    /*
148
     * Create a pseudo-protocol 'unknown' that has no known methods.
149
     * Create a pseudo-protocol 'unknown' that has no known methods.
149
     */
150
     */
150
    proto_unknown = proto_new("unknown");
151
    proto_unknown = proto_new("unknown");
151
 
152
 
152
    /*
153
    /*
153
     * Create a pseudo-protocol 'system' defining names of system IPC
154
     * Create a pseudo-protocol 'system' defining names of system IPC
154
     * methods.
155
     * methods.
155
     */
156
     */
156
    proto_system = proto_new("system");
157
    proto_system = proto_new("system");
157
 
158
 
158
    desc = ipc_methods;
159
    desc = ipc_methods;
159
    while (desc->number != 0) {
160
    while (desc->number != 0) {
160
        oper = oper_new(desc->name);
161
        oper = oper_new(desc->name);
161
        proto_add_oper(proto_system, desc->number, oper);
162
        proto_add_oper(proto_system, desc->number, oper);
162
 
163
 
163
        ++desc;
164
        ++desc;
164
    }
165
    }
165
 
166
 
166
    hash_table_create(&pending_calls, PCALL_TABLE_CHAINS, 1, &pending_call_ops);
167
    hash_table_create(&pending_calls, PCALL_TABLE_CHAINS, 1, &pending_call_ops);
167
}
168
}
168
 
169
 
169
void ipcp_cleanup(void)
170
void ipcp_cleanup(void)
170
{
171
{
171
    proto_delete(proto_system);
172
    proto_delete(proto_system);
172
    hash_table_destroy(&pending_calls);
173
    hash_table_destroy(&pending_calls);
173
}
174
}
174
 
175
 
175
void ipcp_call_out(int phone, ipc_call_t *call, ipc_callid_t hash)
176
void ipcp_call_out(int phone, ipc_call_t *call, ipc_callid_t hash)
176
{
177
{
177
    pending_call_t *pcall;
178
    pending_call_t *pcall;
178
    proto_t *proto;
179
    proto_t *proto;
179
    unsigned long key[1];
180
    unsigned long key[1];
-
 
181
    oper_t *oper;
180
 
182
 
181
    if (have_conn[phone]) proto = connections[phone].proto;
183
    if (have_conn[phone]) proto = connections[phone].proto;
182
    else proto = NULL;
184
    else proto = NULL;
183
 
185
 
184
//  printf("ipcp_call_out()\n");
186
    if ((display_mask & DM_IPC) != 0) {
185
    printf("call id: 0x%x, phone: %d, proto: %s, method: ", hash, phone,
187
        printf("Call ID: 0x%x, phone: %d, proto: %s, method: ", hash,
186
        (proto ? proto->name : "n/a"));
188
            phone, (proto ? proto->name : "n/a"));
187
    ipc_m_print(proto, IPC_GET_METHOD(*call));
189
        ipc_m_print(proto, IPC_GET_METHOD(*call));
188
    printf(" args: (%u, %u, %u, %u, %u)\n",
190
        printf(" args: (%u, %u, %u, %u, %u)\n",
189
        IPC_GET_ARG1(*call),
191
            IPC_GET_ARG1(*call),
190
        IPC_GET_ARG2(*call),
192
            IPC_GET_ARG2(*call),
191
        IPC_GET_ARG3(*call),
193
            IPC_GET_ARG3(*call),
192
        IPC_GET_ARG4(*call),
194
            IPC_GET_ARG4(*call),
193
        IPC_GET_ARG5(*call)
195
            IPC_GET_ARG5(*call)
194
    );
196
        );
-
 
197
    }
-
 
198
 
-
 
199
 
-
 
200
    if ((display_mask & DM_USER) != 0) {
-
 
201
 
-
 
202
        if (proto != NULL) {
-
 
203
            oper = proto_get_oper(proto, IPC_GET_METHOD(*call));
-
 
204
        } else {
-
 
205
            oper = NULL;
-
 
206
        }
-
 
207
 
-
 
208
        if (oper != NULL) {
-
 
209
 
-
 
210
            printf("%s(%d).%s", (proto ? proto->name : "n/a"),
-
 
211
                phone, (oper ? oper->name : "unknown"));
-
 
212
 
-
 
213
            printf("(%u, %u, %u, %u, %u)\n",
-
 
214
                IPC_GET_ARG1(*call),
-
 
215
                IPC_GET_ARG2(*call),
-
 
216
                IPC_GET_ARG3(*call),
-
 
217
                IPC_GET_ARG4(*call),
-
 
218
                IPC_GET_ARG5(*call)
-
 
219
            );
-
 
220
        }
-
 
221
    }
195
 
222
 
196
    /* Store call in hash table for response matching */
223
    /* Store call in hash table for response matching */
197
 
224
 
198
    pcall = malloc(sizeof(pending_call_t));
225
    pcall = malloc(sizeof(pending_call_t));
199
    pcall->phone_hash = phone;
226
    pcall->phone_hash = phone;
200
    pcall->question = *call;
227
    pcall->question = *call;
201
    pcall->call_hash = hash;
228
    pcall->call_hash = hash;
202
 
229
 
203
    key[0] = hash;
230
    key[0] = hash;
204
 
231
 
205
    hash_table_insert(&pending_calls, key, &pcall->link);
232
    hash_table_insert(&pending_calls, key, &pcall->link);
206
}
233
}
207
 
234
 
208
static void parse_answer(pending_call_t *pcall, ipc_call_t *answer)
235
static void parse_answer(ipc_callid_t hash, pending_call_t *pcall,
-
 
236
    ipc_call_t *answer)
209
{
237
{
210
    int phone;
238
    int phone;
211
    ipcarg_t method;
239
    ipcarg_t method;
212
    ipcarg_t service;
240
    ipcarg_t service;
213
    int retval;
241
    int retval;
214
    proto_t *proto;
242
    proto_t *proto;
215
    int cphone;
243
    int cphone;
216
 
244
 
217
//  printf("parse_answer\n");
245
//  printf("parse_answer\n");
218
 
246
 
219
    phone = pcall->phone_hash;
247
    phone = pcall->phone_hash;
220
    method = IPC_GET_METHOD(pcall->question);
248
    method = IPC_GET_METHOD(pcall->question);
221
    retval = IPC_GET_RETVAL(*answer);
249
    retval = IPC_GET_RETVAL(*answer);
-
 
250
 
-
 
251
    if ((display_mask & DM_IPC) != 0) {
222
    printf("phone=%d, method=%d, retval=%d\n",
252
        printf("Response to 0x%x: retval=%d, args = (%u, %u, %u, %u, %u)\n",
-
 
253
            hash, retval, IPC_GET_ARG1(*answer),
-
 
254
            IPC_GET_ARG2(*answer), IPC_GET_ARG3(*answer),
-
 
255
            IPC_GET_ARG4(*answer), IPC_GET_ARG5(*answer));
-
 
256
    }
-
 
257
 
-
 
258
    if ((display_mask & DM_USER) != 0) {
223
        phone, method, retval);
259
        printf("-> %d\n", retval);
-
 
260
    }
224
 
261
 
225
    if (phone == 0 && method == IPC_M_CONNECT_ME_TO && retval == 0) {
262
    if (phone == 0 && method == IPC_M_CONNECT_ME_TO && retval == 0) {
226
        /* Connected to a service (through NS) */
263
        /* Connected to a service (through NS) */
227
        service = IPC_GET_ARG1(pcall->question);
264
        service = IPC_GET_ARG1(pcall->question);
228
        proto = proto_get_by_srv(service);
265
        proto = proto_get_by_srv(service);
229
        if (proto == NULL) proto = proto_unknown;
266
        if (proto == NULL) proto = proto_unknown;
230
 
267
 
231
        cphone = IPC_GET_ARG5(*answer);
268
        cphone = IPC_GET_ARG5(*answer);
-
 
269
        if ((display_mask & DM_SYSTEM) != 0) {
232
        printf("registering connection (phone %d, protocol: %s)\n", cphone,
270
            printf("Registering connection (phone %d, protocol: %s)\n", cphone,
233
            proto->name);
271
                    proto->name);
-
 
272
        }
234
        ipcp_connection_set(cphone, 0, proto);
273
        ipcp_connection_set(cphone, 0, proto);
235
    }
274
    }
236
}
275
}
237
 
276
 
238
void ipcp_call_in(ipc_call_t *call, ipc_callid_t hash)
277
void ipcp_call_in(ipc_call_t *call, ipc_callid_t hash)
239
{
278
{
240
    link_t *item;
279
    link_t *item;
241
    pending_call_t *pcall;
280
    pending_call_t *pcall;
242
    unsigned long key[1];
281
    unsigned long key[1];
243
 
282
 
244
//  printf("ipcp_call_in()\n");
283
//  printf("ipcp_call_in()\n");
245
/*  printf("phone: %d, method: ", call->in_phone_hash);
284
/*  printf("phone: %d, method: ", call->in_phone_hash);
246
    ipc_m_print(IPC_GET_METHOD(*call));
285
    ipc_m_print(IPC_GET_METHOD(*call));
247
    printf(" args: (%u, %u, %u, %u, %u)\n",
286
    printf(" args: (%u, %u, %u, %u, %u)\n",
248
        IPC_GET_ARG1(*call),
287
        IPC_GET_ARG1(*call),
249
        IPC_GET_ARG2(*call),
288
        IPC_GET_ARG2(*call),
250
        IPC_GET_ARG3(*call),
289
        IPC_GET_ARG3(*call),
251
        IPC_GET_ARG4(*call),
290
        IPC_GET_ARG4(*call),
252
        IPC_GET_ARG5(*call)
291
        IPC_GET_ARG5(*call)
253
    );*/
292
    );*/
254
 
293
 
255
    if ((hash & IPC_CALLID_ANSWERED) == 0 && hash != IPCP_CALLID_SYNC) {
294
    if ((hash & IPC_CALLID_ANSWERED) == 0 && hash != IPCP_CALLID_SYNC) {
256
        /* Not a response */
295
        /* Not a response */
-
 
296
        if ((display_mask & DM_IPC) != 0) {
257
        printf("Not a response (hash %d)\n", hash);
297
            printf("Not a response (hash %d)\n", hash);
-
 
298
        }
258
        return;
299
        return;
259
    }
300
    }
260
 
301
 
261
    hash = hash & ~IPC_CALLID_ANSWERED;
302
    hash = hash & ~IPC_CALLID_ANSWERED;
262
    key[0] = hash;
303
    key[0] = hash;
263
 
304
 
264
    item = hash_table_find(&pending_calls, key);
305
    item = hash_table_find(&pending_calls, key);
265
    if (item == NULL) return; // No matching question found
306
    if (item == NULL) return; // No matching question found
-
 
307
 
-
 
308
    /*
-
 
309
     * Response matched to question.
-
 
310
     */
266
   
311
   
267
    pcall = hash_table_get_instance(item, pending_call_t, link);
312
    pcall = hash_table_get_instance(item, pending_call_t, link);
268
 
-
 
269
    printf("response matched to question\n");
-
 
270
    hash_table_remove(&pending_calls, key, 1);
313
    hash_table_remove(&pending_calls, key, 1);
271
 
314
 
272
    parse_answer(pcall, call);
315
    parse_answer(hash, pcall, call);
273
    free(pcall);
316
    free(pcall);
274
}
317
}
275
 
318
 
276
void ipcp_call_sync(int phone, ipc_call_t *call, ipc_call_t *answer)
319
void ipcp_call_sync(int phone, ipc_call_t *call, ipc_call_t *answer)
277
{
320
{
278
    ipcp_call_out(phone, call, IPCP_CALLID_SYNC);
321
    ipcp_call_out(phone, call, IPCP_CALLID_SYNC);
279
    ipcp_call_in(answer, IPCP_CALLID_SYNC);
322
    ipcp_call_in(answer, IPCP_CALLID_SYNC);
280
}
323
}
281
 
324
 
282
void ipcp_hangup(int phone, int rc)
325
void ipcp_hangup(int phone, int rc)
283
{
326
{
-
 
327
    if ((display_mask & DM_SYSTEM) != 0) {
284
    printf("hangup phone %d -> %d\n", phone, rc);
328
        printf("Hang phone %d up -> %d\n", phone, rc);
285
    ipcp_connection_clear(phone);
329
        ipcp_connection_clear(phone);
-
 
330
    }
286
}
331
}
287
 
332
 
288
/** @}
333
/** @}
289
 */
334
 */
290
 
335