Subversion Repositories HelenOS

Rev

Rev 2550 | Rev 2594 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2007 Josef Cejka
  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. /**
  30.  * @defgroup devmap Device mapper.
  31.  * @brief   HelenOS device mapper.
  32.  * @{
  33.  */
  34.  
  35. /** @file
  36.  */
  37.  
  38. #include <ipc/services.h>
  39. #include <ipc/ns.h>
  40. #include <async.h>
  41. #include <stdio.h>
  42. #include <errno.h>
  43. #include <bool.h>
  44. #include <libadt/list.h>
  45. #include <futex.h>
  46.  
  47. #include "devmap.h"
  48.  
  49. LIST_INITIALIZE(device_list);
  50.  
  51. atomic_t device_list_futex = FUTEX_INITIALIZER;
  52.  
  53. /** Initialize device mapper.
  54.  *
  55.  *
  56.  */
  57. static int devmap_init()
  58. {
  59.     /* */
  60.  
  61.     return EOK;
  62. }
  63.  
  64. static int devmap_register()
  65. {
  66.     ipc_callid_t callid2;
  67.     size_t size;
  68.     char buffer[DEVMAP_NAME_MAXLEN + 1];
  69.     ipc_callid_t callid;
  70.     ipc_call_t call;
  71.     int retval;
  72.    
  73.     if (ipc_data_receive(&callid, &call, NULL, &size) != 0) {
  74.     //  retval =
  75.     }
  76.  
  77.     if (size > DEVMAP_NAME_MAXLEN) {
  78.         retval = ELIMIT;
  79.     } else {
  80.         ipc_data_deliver(callid2, &call, buffer, size);
  81.         buffer[DEVMAP_NAME_MAXLEN] = 0;
  82.     }
  83.     return EOK;
  84. }
  85.  
  86. static int devmap_unregister()
  87. {
  88.     return EOK;
  89. }
  90.  
  91. static int devmap_forward(int handle, ipc_call_t *call, ipc_callid_t callid)
  92. {
  93.     link_t *item;
  94.     ipcarg_t phone;
  95.     devmap_device_t *dev;
  96.  
  97.     /* FIXME: add futex */
  98.     futex_down(&device_list_futex);
  99.  
  100.     item = (&device_list)->next;
  101.  
  102.     while (item != &device_list) {
  103.  
  104.         dev = list_get_instance(item, devmap_device_t, list);
  105.         if (dev->handle == handle) {
  106.             break;
  107.         }
  108.         item = item->next;
  109.     }
  110.  
  111.     if (item == &device_list) {
  112.         return ENOENT;
  113.     }
  114.  
  115.     dev = list_get_instance(item, devmap_device_t, list);
  116.     phone = dev->phone;
  117.  
  118.     futex_up(&device_list_futex);
  119.    
  120.     return ipc_forward_fast(callid, phone, 0, 0);
  121. }
  122.  
  123. static int devmap_get_handle()
  124. {
  125.     return EOK;
  126. }
  127.  
  128. /** Function for handling connections to devmap
  129.  *
  130.  */
  131. static void
  132. devmap_client_connection(ipc_callid_t iid, ipc_call_t *icall)
  133. {
  134.     ipc_callid_t callid;
  135.     ipc_call_t call;
  136.     int retval;
  137.     bool cont = true;
  138.  
  139.     printf("DevMap: new connection.");
  140.  
  141.     ipc_answer_fast(iid, EOK, 0, 0); /* Accept connection */
  142.  
  143.     while (cont) {
  144.         callid = async_get_call(&call);
  145.  
  146.         switch (IPC_GET_METHOD(call)) {
  147.         case IPC_M_PHONE_HUNGUP:
  148.                 /* TODO: if its a device connection, remove it from table */
  149.             devmap_unregister();
  150.             printf("DevMap: connection hung up.");
  151.             cont = false;
  152.             continue; /* Exit thread */
  153.  
  154.         case DEVMAP_REGISTER:
  155.            
  156.             if ((retval = devmap_register()) != EOK) {
  157.                 cont = false;
  158.             }
  159.             break;
  160.         case DEVMAP_UNREGISTER:
  161.             /* TODO: remove device (if registred) */
  162.             retval = devmap_unregister();
  163.             cont = false;
  164.             break;
  165.         case DEVMAP_CONNECT_TO_DEVICE:
  166.             retval = devmap_forward(IPC_GET_ARG1(call), &call, callid);
  167.             cont = false;
  168.             break;
  169.         case DEVMAP_GET_HANDLE:
  170.            
  171.             break;
  172.         default:
  173.             retval = ENOENT;
  174.         }
  175.         ipc_answer_fast(callid, retval, 0, 0);
  176.     }
  177.        
  178.     printf("DevMap: connection closed.");
  179.     return;
  180. }
  181.  
  182.  
  183. int main(int argc, char *argv[])
  184. {
  185.  
  186.     ipcarg_t phonead;
  187.  
  188.     printf("DevMap: HelenOS device mapper.\n");
  189.  
  190.     if (devmap_init() != 0) {
  191.         printf("Error while initializing DevMap service.");
  192.         return -1;
  193.     }
  194.  
  195.         /* Set a handler of incomming connections */
  196.     async_set_client_connection(devmap_client_connection);
  197.  
  198.         /* Register device mapper at naming service */
  199.     if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAP, 0, &phonead) != 0)
  200.         return -1;
  201.    
  202.     async_manager();
  203.     /* Never reached */
  204.     return 0;
  205. }
  206.  
  207. /**
  208.  * @}
  209.  */
  210.  
  211.