Subversion Repositories HelenOS

Rev

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

Rev 2894 Rev 2946
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 sctrace
29
/** @addtogroup sctrace
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 "ipcp.h"
41
#include "ipcp.h"
42
 
42
 
43
#define IPCP_CALLID_SYNC 0
43
#define IPCP_CALLID_SYNC 0
44
 
44
 
45
typedef struct {
45
typedef struct {
46
    int phone_hash;
46
    int phone_hash;
47
    ipc_call_t question;
47
    ipc_call_t question;
48
 
48
 
49
    int call_hash;
49
    int call_hash;
50
 
50
 
51
    link_t link;
51
    link_t link;
52
} pending_call_t;
52
} pending_call_t;
53
 
53
 
54
typedef struct {
54
typedef struct {
55
    int server;
55
    int server;
56
    proto_t *proto;
56
    proto_t *proto;
57
} connection_t;
57
} connection_t;
58
 
58
 
59
#define MAX_PHONE 64
59
#define MAX_PHONE 64
60
connection_t connections[MAX_PHONE];
60
connection_t connections[MAX_PHONE];
61
int have_conn[MAX_PHONE];
61
int have_conn[MAX_PHONE];
62
 
62
 
63
#define PCALL_TABLE_CHAINS 32
63
#define PCALL_TABLE_CHAINS 32
64
hash_table_t pending_calls;
64
hash_table_t pending_calls;
65
 
65
 
-
 
66
static hash_index_t pending_call_hash(unsigned long key[]);
-
 
67
static int pending_call_compare(unsigned long key[], hash_count_t keys,
-
 
68
    link_t *item);
-
 
69
static void pending_call_remove_callback(link_t *item);
-
 
70
 
-
 
71
hash_table_operations_t pending_call_ops = {
-
 
72
    .hash = pending_call_hash,
-
 
73
    .compare = pending_call_compare,
-
 
74
    .remove_callback = pending_call_remove_callback
-
 
75
};
-
 
76
 
-
 
77
 
66
hash_index_t pending_call_hash(unsigned long key[])
78
static hash_index_t pending_call_hash(unsigned long key[])
67
{
79
{
68
//  printf("pending_call_hash\n");
80
//  printf("pending_call_hash\n");
69
    return key[0] % PCALL_TABLE_CHAINS;
81
    return key[0] % PCALL_TABLE_CHAINS;
70
}
82
}
71
 
83
 
72
int pending_call_compare(unsigned long key[], hash_count_t keys,
84
static int pending_call_compare(unsigned long key[], hash_count_t keys,
73
    link_t *item)
85
    link_t *item)
74
{
86
{
75
    pending_call_t *hs;
87
    pending_call_t *hs;
76
 
88
 
77
//  printf("pending_call_compare\n");
89
//  printf("pending_call_compare\n");
78
    hs = hash_table_get_instance(item, pending_call_t, link);
90
    hs = hash_table_get_instance(item, pending_call_t, link);
79
 
91
 
80
    return key[0] == hs->call_hash;
92
    return key[0] == hs->call_hash;
81
}
93
}
82
 
94
 
83
void pending_call_remove_callback(link_t *item)
95
static void pending_call_remove_callback(link_t *item)
84
{
96
{
85
//  printf("pending_call_remove_callback\n");
97
//  printf("pending_call_remove_callback\n");
86
}
98
}
87
 
99
 
88
hash_table_operations_t pending_call_ops = {
-
 
89
    .hash = pending_call_hash,
-
 
90
    .compare = pending_call_compare,
-
 
91
    .remove_callback = pending_call_remove_callback
-
 
92
};
-
 
93
 
100
 
94
void ipcp_connection_set(int phone, int server, proto_t *proto)
101
void ipcp_connection_set(int phone, int server, proto_t *proto)
95
{
102
{
96
    if (phone <0 || phone >= MAX_PHONE) return;
103
    if (phone <0 || phone >= MAX_PHONE) return;
97
    connections[phone].server = server;
104
    connections[phone].server = server;
98
    connections[phone].proto = proto;
105
    connections[phone].proto = proto;
99
    have_conn[phone] = 1;
106
    have_conn[phone] = 1;
100
}
107
}
101
 
108
 
102
void ipcp_connection_clear(int phone)
109
void ipcp_connection_clear(int phone)
103
{
110
{
104
    have_conn[phone] = 0;
111
    have_conn[phone] = 0;
105
    connections[phone].server = 0;
112
    connections[phone].server = 0;
106
    connections[phone].proto = NULL;
113
    connections[phone].proto = NULL;
107
}
114
}
108
 
115
 
109
static void ipc_m_print(proto_t *proto, ipcarg_t method)
116
static void ipc_m_print(proto_t *proto, ipcarg_t method)
110
{
117
{
111
    ipc_m_desc_t *desc;
118
    ipc_m_desc_t *desc;
112
    oper_t *oper;
119
    oper_t *oper;
113
 
120
 
114
    /* FIXME: too slow */
121
    /* FIXME: too slow */
115
    desc = ipc_methods;
122
    desc = ipc_methods;
116
    while (desc->number != 0) {
123
    while (desc->number != 0) {
117
        if (desc->number == method) {
124
        if (desc->number == method) {
118
            printf("%s (%d)", desc->name, method);
125
            printf("%s (%d)", desc->name, method);
119
            return;
126
            return;
120
        }
127
        }
121
 
128
 
122
        ++desc;
129
        ++desc;
123
    }
130
    }
124
 
131
 
125
    if (proto != NULL) {
132
    if (proto != NULL) {
126
        oper = proto_get_oper(proto, method);
133
        oper = proto_get_oper(proto, method);
127
        if (oper != NULL) {
134
        if (oper != NULL) {
128
            printf("%s (%d)", oper->name, method);
135
            printf("%s (%d)", oper->name, method);
129
            return;
136
            return;
130
        }
137
        }
131
    }
138
    }
132
 
139
 
133
    printf("%d", method);
140
    printf("%d", method);
134
}
141
}
135
 
142
 
136
void ipcp_init(void)
143
void ipcp_init(void)
137
{
144
{
138
    hash_table_create(&pending_calls, PCALL_TABLE_CHAINS, 1, &pending_call_ops);
145
    hash_table_create(&pending_calls, PCALL_TABLE_CHAINS, 1, &pending_call_ops);
139
}
146
}
140
 
147
 
141
void ipcp_cleanup(void)
148
void ipcp_cleanup(void)
142
{
149
{
143
    hash_table_destroy(&pending_calls);
150
    hash_table_destroy(&pending_calls);
144
}
151
}
145
 
152
 
146
void ipcp_call_out(int phone, ipc_call_t *call, ipc_callid_t hash)
153
void ipcp_call_out(int phone, ipc_call_t *call, ipc_callid_t hash)
147
{
154
{
148
    pending_call_t *pcall;
155
    pending_call_t *pcall;
149
    proto_t *proto;
156
    proto_t *proto;
-
 
157
    unsigned long key[1];
150
 
158
 
151
    if (have_conn[phone]) proto = connections[phone].proto;
159
    if (have_conn[phone]) proto = connections[phone].proto;
152
    else proto = NULL;
160
    else proto = NULL;
153
 
161
 
154
//  printf("ipcp_call_out()\n");
162
//  printf("ipcp_call_out()\n");
155
    printf("call id: 0x%x, phone: %d, proto: %s, method: ", hash, phone,
163
    printf("call id: 0x%x, phone: %d, proto: %s, method: ", hash, phone,
156
        (proto ? proto->name : "n/a"));
164
        (proto ? proto->name : "n/a"));
157
    ipc_m_print(proto, IPC_GET_METHOD(*call));
165
    ipc_m_print(proto, IPC_GET_METHOD(*call));
158
    printf(" args: (%u, %u, %u, %u, %u)\n",
166
    printf(" args: (%u, %u, %u, %u, %u)\n",
159
        IPC_GET_ARG1(*call),
167
        IPC_GET_ARG1(*call),
160
        IPC_GET_ARG2(*call),
168
        IPC_GET_ARG2(*call),
161
        IPC_GET_ARG3(*call),
169
        IPC_GET_ARG3(*call),
162
        IPC_GET_ARG4(*call),
170
        IPC_GET_ARG4(*call),
163
        IPC_GET_ARG5(*call)
171
        IPC_GET_ARG5(*call)
164
    );
172
    );
165
 
173
 
166
    /* Store call in hash table for response matching */
174
    /* Store call in hash table for response matching */
167
 
175
 
168
    pcall = malloc(sizeof(pending_call_t));
176
    pcall = malloc(sizeof(pending_call_t));
169
    pcall->phone_hash = phone;
177
    pcall->phone_hash = phone;
170
    pcall->question = *call;
178
    pcall->question = *call;
171
    pcall->call_hash = hash;
179
    pcall->call_hash = hash;
172
 
180
 
-
 
181
    key[0] = hash;
-
 
182
 
173
    hash_table_insert(&pending_calls, &pcall->call_hash, &pcall->link);
183
    hash_table_insert(&pending_calls, key, &pcall->link);
174
}
184
}
175
 
185
 
176
static void parse_answer(pending_call_t *pcall, ipc_call_t *answer)
186
static void parse_answer(pending_call_t *pcall, ipc_call_t *answer)
177
{
187
{
178
    int phone;
188
    int phone;
179
    ipcarg_t method;
189
    ipcarg_t method;
180
    ipcarg_t service;
190
    ipcarg_t service;
181
    int retval;
191
    int retval;
182
    static proto_t proto_unknown = { .name = "unknown" };
192
    static proto_t proto_unknown = { .name = "unknown" };
183
    proto_t *proto;
193
    proto_t *proto;
184
    int cphone;
194
    int cphone;
185
 
195
 
186
//  printf("parse_answer\n");
196
//  printf("parse_answer\n");
187
 
197
 
188
    phone = pcall->phone_hash;
198
    phone = pcall->phone_hash;
189
    method = IPC_GET_METHOD(pcall->question);
199
    method = IPC_GET_METHOD(pcall->question);
190
    retval = IPC_GET_RETVAL(*answer);
200
    retval = IPC_GET_RETVAL(*answer);
191
    printf("phone=%d, method=%d, retval=%d\n",
201
    printf("phone=%d, method=%d, retval=%d\n",
192
        phone, method, retval);
202
        phone, method, retval);
193
 
203
 
194
    if (phone == 0 && method == IPC_M_CONNECT_ME_TO && retval == 0) {
204
    if (phone == 0 && method == IPC_M_CONNECT_ME_TO && retval == 0) {
195
        /* Connected to a service (through NS) */
205
        /* Connected to a service (through NS) */
196
        service = IPC_GET_ARG1(pcall->question);
206
        service = IPC_GET_ARG1(pcall->question);
197
        proto = proto_get_by_srv(service);
207
        proto = proto_get_by_srv(service);
198
        if (proto == NULL) proto = &proto_unknown;
208
        if (proto == NULL) proto = &proto_unknown;
199
 
209
 
200
        cphone = IPC_GET_ARG5(*answer);
210
        cphone = IPC_GET_ARG5(*answer);
201
        printf("registering connection (phone %d, protocol: %s)\n", cphone,
211
        printf("registering connection (phone %d, protocol: %s)\n", cphone,
202
            proto->name);
212
            proto->name);
203
        ipcp_connection_set(cphone, 0, proto);
213
        ipcp_connection_set(cphone, 0, proto);
204
    }
214
    }
205
}
215
}
206
 
216
 
207
void ipcp_call_in(ipc_call_t *call, ipc_callid_t hash)
217
void ipcp_call_in(ipc_call_t *call, ipc_callid_t hash)
208
{
218
{
209
    link_t *item;
219
    link_t *item;
210
    pending_call_t *pcall;
220
    pending_call_t *pcall;
-
 
221
    unsigned long key[1];
211
 
222
 
212
//  printf("ipcp_call_in()\n");
223
//  printf("ipcp_call_in()\n");
213
/*  printf("phone: %d, method: ", call->in_phone_hash);
224
/*  printf("phone: %d, method: ", call->in_phone_hash);
214
    ipc_m_print(IPC_GET_METHOD(*call));
225
    ipc_m_print(IPC_GET_METHOD(*call));
215
    printf(" args: (%u, %u, %u, %u, %u)\n",
226
    printf(" args: (%u, %u, %u, %u, %u)\n",
216
        IPC_GET_ARG1(*call),
227
        IPC_GET_ARG1(*call),
217
        IPC_GET_ARG2(*call),
228
        IPC_GET_ARG2(*call),
218
        IPC_GET_ARG3(*call),
229
        IPC_GET_ARG3(*call),
219
        IPC_GET_ARG4(*call),
230
        IPC_GET_ARG4(*call),
220
        IPC_GET_ARG5(*call)
231
        IPC_GET_ARG5(*call)
221
    );*/
232
    );*/
222
 
233
 
223
    if ((hash & IPC_CALLID_ANSWERED) == 0 && hash != IPCP_CALLID_SYNC) {
234
    if ((hash & IPC_CALLID_ANSWERED) == 0 && hash != IPCP_CALLID_SYNC) {
224
        /* Not a response */
235
        /* Not a response */
225
        printf("Not a response (hash %d)\n", hash);
236
        printf("Not a response (hash %d)\n", hash);
226
        return;
237
        return;
227
    }
238
    }
228
 
239
 
229
    hash = hash & ~IPC_CALLID_ANSWERED;
240
    hash = hash & ~IPC_CALLID_ANSWERED;
-
 
241
    key[0] = hash;
230
 
242
 
231
    item = hash_table_find(&pending_calls, &hash);
243
    item = hash_table_find(&pending_calls, key);
232
    if (item == NULL) return; // No matching question found
244
    if (item == NULL) return; // No matching question found
233
   
245
   
234
    pcall = hash_table_get_instance(item, pending_call_t, link);
246
    pcall = hash_table_get_instance(item, pending_call_t, link);
235
 
247
 
236
    printf("response matched to question\n");
248
    printf("response matched to question\n");
237
    hash_table_remove(&pending_calls, &hash, 1);
249
    hash_table_remove(&pending_calls, key, 1);
238
 
250
 
239
    parse_answer(pcall, call);
251
    parse_answer(pcall, call);
240
    free(pcall);
252
    free(pcall);
241
}
253
}
242
 
254
 
243
void ipcp_call_sync(int phone, ipc_call_t *call, ipc_call_t *answer)
255
void ipcp_call_sync(int phone, ipc_call_t *call, ipc_call_t *answer)
244
{
256
{
245
    ipcp_call_out(phone, call, IPCP_CALLID_SYNC);
257
    ipcp_call_out(phone, call, IPCP_CALLID_SYNC);
246
    ipcp_call_in(answer, IPCP_CALLID_SYNC);
258
    ipcp_call_in(answer, IPCP_CALLID_SYNC);
247
}
259
}
248
 
260
 
249
void ipcp_hangup(int phone, int rc)
261
void ipcp_hangup(int phone, int rc)
250
{
262
{
251
    printf("hangup phone %d -> %d\n", phone, rc);
263
    printf("hangup phone %d -> %d\n", phone, rc);
252
    ipcp_connection_clear(phone);
264
    ipcp_connection_clear(phone);
253
}
265
}
254
 
266
 
255
/** @}
267
/** @}
256
 */
268
 */
257
 
269