Subversion Repositories HelenOS

Rev

Rev 2882 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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