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