Subversion Repositories HelenOS-historic

Rev

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

  1. /*
  2.  * Copyright (C) 2006 Ondrej Palkovsky
  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. #include <ipc.h>
  30. #include <libc.h>
  31. #include <malloc.h>
  32. #include <errno.h>
  33. #include <list.h>
  34. #include <stdio.h>
  35. #include <unistd.h>
  36.  
  37. /** Structure used for keeping track of sent async msgs
  38.  * and queing unsent msgs
  39.  *
  40.  */
  41. typedef struct {
  42.     link_t list;
  43.  
  44.     ipc_async_callback_t callback;
  45.     void *private;
  46.     union {
  47.         ipc_callid_t callid;
  48.         struct {
  49.             int phoneid;
  50.             ipc_data_t data;
  51.         } msg;
  52.     }u;
  53. } async_call_t;
  54.  
  55. LIST_INITIALIZE(dispatched_calls);
  56. LIST_INITIALIZE(queued_calls);
  57.  
  58. int ipc_call_sync(int phoneid, ipcarg_t method, ipcarg_t arg1,
  59.           ipcarg_t *result)
  60. {
  61.     ipc_data_t resdata;
  62.     int callres;
  63.    
  64.     callres = __SYSCALL4(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1,
  65.                  (sysarg_t)&resdata);
  66.     if (callres)
  67.         return callres;
  68.     if (result)
  69.         *result = IPC_GET_ARG1(resdata);
  70.     return IPC_GET_RETVAL(resdata);
  71. }
  72.  
  73. int ipc_call_sync_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
  74.             ipcarg_t arg2, ipcarg_t arg3,
  75.             ipcarg_t *result1, ipcarg_t *result2, ipcarg_t *result3)
  76. {
  77.     ipc_data_t data;
  78.     int callres;
  79.  
  80.     IPC_SET_METHOD(data, method);
  81.     IPC_SET_ARG1(data, arg1);
  82.     IPC_SET_ARG2(data, arg2);
  83.     IPC_SET_ARG3(data, arg3);
  84.  
  85.     callres = __SYSCALL3(SYS_IPC_CALL_SYNC, phoneid, (sysarg_t)&data,
  86.                  (sysarg_t)&data);
  87.     if (callres)
  88.         return callres;
  89.  
  90.     if (result1)
  91.         *result1 = IPC_GET_ARG1(data);
  92.     if (result2)
  93.         *result2 = IPC_GET_ARG2(data);
  94.     if (result3)
  95.         *result3 = IPC_GET_ARG3(data);
  96.     return IPC_GET_RETVAL(data);
  97. }
  98.  
  99. /** Syscall to send asynchronous message */
  100. static  ipc_callid_t _ipc_call_async(int phoneid, ipc_data_t *data)
  101. {
  102.     return __SYSCALL2(SYS_IPC_CALL_ASYNC, phoneid, (sysarg_t)data);
  103. }
  104.  
  105. /** Send asynchronous message
  106.  *
  107.  * - if fatal error, call callback handler with proper error code
  108.  * - if message cannot be temporarily sent, add to queue
  109.  */
  110. void ipc_call_async_2(int phoneid, ipcarg_t method, ipcarg_t arg1,
  111.               ipcarg_t arg2, void *private,
  112.               ipc_async_callback_t callback)
  113. {
  114.     async_call_t *call;
  115.     ipc_callid_t callid;
  116.  
  117.     call = malloc(sizeof(*call));
  118.     if (!call) {
  119.         callback(private, ENOMEM, NULL);
  120.         return;
  121.     }
  122.        
  123.     callid = __SYSCALL4(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1, arg2);
  124.     if (callid == IPC_CALLRET_FATAL) {
  125.         /* Call asynchronous handler with error code */
  126.         callback(private, ENOENT, NULL);
  127.         free(call);
  128.         return;
  129.     }
  130.  
  131.     call->callback = callback;
  132.     call->private = private;
  133.  
  134.     if (callid == IPC_CALLRET_TEMPORARY) {
  135.         /* Add asynchronous call to queue of non-dispatched async calls */
  136.         call->u.msg.phoneid = phoneid;
  137.         IPC_SET_METHOD(call->u.msg.data, method);
  138.         IPC_SET_ARG1(call->u.msg.data, arg1);
  139.         IPC_SET_ARG2(call->u.msg.data, arg2);
  140.  
  141.         list_append(&call->list, &queued_calls);
  142.         return;
  143.     }
  144.     call->u.callid = callid;
  145.     /* Add call to list of dispatched calls */
  146.     list_append(&call->list, &dispatched_calls);
  147. }
  148.  
  149.  
  150. /** Send answer to a received call */
  151. void ipc_answer(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1,
  152.         ipcarg_t arg2)
  153. {
  154.     __SYSCALL4(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2);
  155. }
  156.  
  157.  
  158. /** Call syscall function sys_ipc_wait_for_call */
  159. static inline ipc_callid_t _ipc_wait_for_call(ipc_call_t *call, int flags)
  160. {
  161.     return __SYSCALL3(SYS_IPC_WAIT, (sysarg_t)&call->data,
  162.               (sysarg_t)&call->taskid, flags);
  163. }
  164.  
  165. /** Try to dispatch queed calls from async queue */
  166. static void try_dispatch_queued_calls(void)
  167. {
  168.     async_call_t *call;
  169.     ipc_callid_t callid;
  170.  
  171.     while (!list_empty(&queued_calls)) {
  172.         call = list_get_instance(queued_calls.next, async_call_t,
  173.                      list);
  174.  
  175.         callid = _ipc_call_async(call->u.msg.phoneid,
  176.                      &call->u.msg.data);
  177.         if (callid == IPC_CALLRET_TEMPORARY)
  178.             break;
  179.         list_remove(&call->list);
  180.         if (callid == IPC_CALLRET_FATAL) {
  181.             call->callback(call->private, ENOENT, NULL);
  182.             free(call);
  183.         } else {
  184.             call->u.callid = callid;
  185.             list_append(&call->list, &dispatched_calls);
  186.         }
  187.     }
  188. }
  189.  
  190. /** Handle received answer
  191.  *
  192.  * TODO: Make it use hash table
  193.  *
  194.  * @param callid Callid (with first bit set) of the answered call
  195.  */
  196. static void handle_answer(ipc_callid_t callid, ipc_data_t *data)
  197. {
  198.     link_t *item;
  199.     async_call_t *call;
  200.  
  201.     callid &= ~IPC_CALLID_ANSWERED;
  202.    
  203.     for (item = dispatched_calls.next; item != &dispatched_calls;
  204.          item = item->next) {
  205.         call = list_get_instance(item, async_call_t, list);
  206.         if (call->u.callid == callid) {
  207.             list_remove(&call->list);
  208.             call->callback(call->private,
  209.                        IPC_GET_RETVAL(*data),
  210.                        data);
  211.             return;
  212.         }
  213.     }
  214.     printf("Received unidentified answer: %P!!!\n", callid);
  215. }
  216.  
  217.  
  218. /** Wait for IPC call and return
  219.  *
  220.  * - dispatch ASYNC reoutines in the background
  221.  * @param data Space where the message is stored
  222.  * @return Callid or 0 if nothing available and started with
  223.  *         IPC_WAIT_NONBLOCKING
  224.  */
  225. ipc_callid_t ipc_wait_for_call(ipc_call_t *call, int flags)
  226. {
  227.     ipc_callid_t callid;
  228.  
  229.     do {
  230.         try_dispatch_queued_calls();
  231.  
  232.         callid = _ipc_wait_for_call(call, flags);
  233.         /* Handle received answers */
  234.         if (callid & IPC_CALLID_ANSWERED)
  235.             handle_answer(callid, &call->data);
  236.     } while (callid & IPC_CALLID_ANSWERED);
  237.  
  238.     return callid;
  239. }
  240.  
  241. /** Ask destination to do a callback connection */
  242. int ipc_connect_to_me(int phoneid, int arg1, int arg2,
  243.               unsigned long long *taskid)
  244. {
  245.     return __SYSCALL4(SYS_IPC_CONNECT_TO_ME, phoneid, arg1, arg2,
  246.               (sysarg_t) taskid);
  247. }
  248.