Subversion Repositories HelenOS

Rev

Rev 2946 | Rev 3471 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2894 svoboda 1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
3435 svoboda 29
/** @addtogroup trace
2871 svoboda 30
 * @{
31
 */
32
/** @file
33
 */
34
 
35
#include <stdio.h>
2873 svoboda 36
#include <stdlib.h>
37
#include <libadt/hash_table.h>
2871 svoboda 38
 
39
#include "ipc_desc.h"
2878 svoboda 40
#include "proto.h"
2874 svoboda 41
#include "ipcp.h"
2871 svoboda 42
 
2877 svoboda 43
#define IPCP_CALLID_SYNC 0
44
 
2873 svoboda 45
typedef struct {
2874 svoboda 46
    int phone_hash;
47
    ipc_call_t question;
48
 
49
    int call_hash;
50
 
51
    link_t link;
52
} pending_call_t;
53
 
54
typedef struct {
2873 svoboda 55
    int server;
56
    proto_t *proto;
57
} connection_t;
58
 
59
#define MAX_PHONE 64
60
connection_t connections[MAX_PHONE];
61
int have_conn[MAX_PHONE];
62
 
63
#define PCALL_TABLE_CHAINS 32
64
hash_table_t pending_calls;
65
 
2946 svoboda 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
 
78
static hash_index_t pending_call_hash(unsigned long key[])
2871 svoboda 79
{
2877 svoboda 80
//  printf("pending_call_hash\n");
2873 svoboda 81
    return key[0] % PCALL_TABLE_CHAINS;
82
}
83
 
2946 svoboda 84
static int pending_call_compare(unsigned long key[], hash_count_t keys,
2877 svoboda 85
    link_t *item)
2873 svoboda 86
{
87
    pending_call_t *hs;
88
 
2877 svoboda 89
//  printf("pending_call_compare\n");
2873 svoboda 90
    hs = hash_table_get_instance(item, pending_call_t, link);
91
 
92
    return key[0] == hs->call_hash;
93
}
94
 
2946 svoboda 95
static void pending_call_remove_callback(link_t *item)
2873 svoboda 96
{
2877 svoboda 97
//  printf("pending_call_remove_callback\n");
2873 svoboda 98
}
99
 
100
 
2882 svoboda 101
void ipcp_connection_set(int phone, int server, proto_t *proto)
2873 svoboda 102
{
103
    if (phone <0 || phone >= MAX_PHONE) return;
104
    connections[phone].server = server;
105
    connections[phone].proto = proto;
106
    have_conn[phone] = 1;
107
}
108
 
2882 svoboda 109
void ipcp_connection_clear(int phone)
2873 svoboda 110
{
111
    have_conn[phone] = 0;
112
    connections[phone].server = 0;
113
    connections[phone].proto = NULL;
114
}
115
 
2880 svoboda 116
static void ipc_m_print(proto_t *proto, ipcarg_t method)
2873 svoboda 117
{
2872 svoboda 118
    ipc_m_desc_t *desc;
2880 svoboda 119
    oper_t *oper;
2872 svoboda 120
 
121
    /* FIXME: too slow */
122
    desc = ipc_methods;
123
    while (desc->number != 0) {
124
        if (desc->number == method) {
125
            printf("%s (%d)", desc->name, method);
126
            return;
127
        }
128
 
129
        ++desc;
130
    }
131
 
2880 svoboda 132
    if (proto != NULL) {
133
        oper = proto_get_oper(proto, method);
134
        if (oper != NULL) {
135
            printf("%s (%d)", oper->name, method);
136
            return;
137
        }
138
    }
139
 
2872 svoboda 140
    printf("%d", method);
141
}
142
 
2873 svoboda 143
void ipcp_init(void)
144
{
145
    hash_table_create(&pending_calls, PCALL_TABLE_CHAINS, 1, &pending_call_ops);
146
}
147
 
148
void ipcp_cleanup(void)
149
{
150
    hash_table_destroy(&pending_calls);
151
}
152
 
2874 svoboda 153
void ipcp_call_out(int phone, ipc_call_t *call, ipc_callid_t hash)
2872 svoboda 154
{
2873 svoboda 155
    pending_call_t *pcall;
2880 svoboda 156
    proto_t *proto;
2946 svoboda 157
    unsigned long key[1];
2873 svoboda 158
 
2880 svoboda 159
    if (have_conn[phone]) proto = connections[phone].proto;
160
    else proto = NULL;
2877 svoboda 161
 
2874 svoboda 162
//  printf("ipcp_call_out()\n");
2877 svoboda 163
    printf("call id: 0x%x, phone: %d, proto: %s, method: ", hash, phone,
2880 svoboda 164
        (proto ? proto->name : "n/a"));
165
    ipc_m_print(proto, IPC_GET_METHOD(*call));
2872 svoboda 166
    printf(" args: (%u, %u, %u, %u, %u)\n",
2871 svoboda 167
        IPC_GET_ARG1(*call),
168
        IPC_GET_ARG2(*call),
169
        IPC_GET_ARG3(*call),
170
        IPC_GET_ARG4(*call),
171
        IPC_GET_ARG5(*call)
172
    );
2873 svoboda 173
 
174
    /* Store call in hash table for response matching */
175
 
176
    pcall = malloc(sizeof(pending_call_t));
177
    pcall->phone_hash = phone;
178
    pcall->question = *call;
179
    pcall->call_hash = hash;
180
 
2946 svoboda 181
    key[0] = hash;
182
 
183
    hash_table_insert(&pending_calls, key, &pcall->link);
2871 svoboda 184
}
185
 
2877 svoboda 186
static void parse_answer(pending_call_t *pcall, ipc_call_t *answer)
187
{
188
    int phone;
189
    ipcarg_t method;
2878 svoboda 190
    ipcarg_t service;
2877 svoboda 191
    int retval;
2878 svoboda 192
    static proto_t proto_unknown = { .name = "unknown" };
193
    proto_t *proto;
2877 svoboda 194
    int cphone;
195
 
196
//  printf("parse_answer\n");
197
 
198
    phone = pcall->phone_hash;
199
    method = IPC_GET_METHOD(pcall->question);
200
    retval = IPC_GET_RETVAL(*answer);
201
    printf("phone=%d, method=%d, retval=%d\n",
202
        phone, method, retval);
203
 
204
    if (phone == 0 && method == IPC_M_CONNECT_ME_TO && retval == 0) {
205
        /* Connected to a service (through NS) */
2878 svoboda 206
        service = IPC_GET_ARG1(pcall->question);
207
        proto = proto_get_by_srv(service);
208
        if (proto == NULL) proto = &proto_unknown;
209
 
2877 svoboda 210
        cphone = IPC_GET_ARG5(*answer);
2878 svoboda 211
        printf("registering connection (phone %d, protocol: %s)\n", cphone,
212
            proto->name);
2882 svoboda 213
        ipcp_connection_set(cphone, 0, proto);
2877 svoboda 214
    }
215
}
216
 
2874 svoboda 217
void ipcp_call_in(ipc_call_t *call, ipc_callid_t hash)
2871 svoboda 218
{
2873 svoboda 219
    link_t *item;
220
    pending_call_t *pcall;
2946 svoboda 221
    unsigned long key[1];
2873 svoboda 222
 
2874 svoboda 223
//  printf("ipcp_call_in()\n");
2872 svoboda 224
/*  printf("phone: %d, method: ", call->in_phone_hash);
225
    ipc_m_print(IPC_GET_METHOD(*call));
226
    printf(" args: (%u, %u, %u, %u, %u)\n",
2871 svoboda 227
        IPC_GET_ARG1(*call),
228
        IPC_GET_ARG2(*call),
229
        IPC_GET_ARG3(*call),
230
        IPC_GET_ARG4(*call),
231
        IPC_GET_ARG5(*call)
2872 svoboda 232
    );*/
2873 svoboda 233
 
2877 svoboda 234
    if ((hash & IPC_CALLID_ANSWERED) == 0 && hash != IPCP_CALLID_SYNC) {
2873 svoboda 235
        /* Not a response */
2877 svoboda 236
        printf("Not a response (hash %d)\n", hash);
2873 svoboda 237
        return;
238
    }
239
 
240
    hash = hash & ~IPC_CALLID_ANSWERED;
2946 svoboda 241
    key[0] = hash;
2873 svoboda 242
 
2946 svoboda 243
    item = hash_table_find(&pending_calls, key);
2873 svoboda 244
    if (item == NULL) return; // No matching question found
245
 
246
    pcall = hash_table_get_instance(item, pending_call_t, link);
247
 
248
    printf("response matched to question\n");
2946 svoboda 249
    hash_table_remove(&pending_calls, key, 1);
2877 svoboda 250
 
251
    parse_answer(pcall, call);
252
    free(pcall);
2871 svoboda 253
}
254
 
2874 svoboda 255
void ipcp_call_sync(int phone, ipc_call_t *call, ipc_call_t *answer)
2872 svoboda 256
{
2877 svoboda 257
    ipcp_call_out(phone, call, IPCP_CALLID_SYNC);
258
    ipcp_call_in(answer, IPCP_CALLID_SYNC);
2872 svoboda 259
}
2871 svoboda 260
 
2874 svoboda 261
void ipcp_hangup(int phone, int rc)
2872 svoboda 262
{
263
    printf("hangup phone %d -> %d\n", phone, rc);
2882 svoboda 264
    ipcp_connection_clear(phone);
2872 svoboda 265
}
266
 
2871 svoboda 267
/** @}
268
 */