Subversion Repositories HelenOS-historic

Rev

Rev 1618 | Rev 1657 | Go to most recent revision | Blame | Compare with Previous | 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. /** @addtogroup ippc IPPC
  30.  * @brief   Interprocess communication tester.
  31.  * @{
  32.  */
  33. /**
  34.  * @file
  35.  */
  36.  
  37. #include <stdio.h>
  38. #include <async.h>
  39. #include <ipc/ipc.h>
  40. #include <ipc/services.h>
  41. #include <errno.h>
  42.  
  43. #define TEST_START       10000
  44. #define MAXLIST          4
  45.  
  46. #define MSG_HANG_ME_UP   2000
  47.  
  48. static int connections[50];
  49. static ipc_callid_t callids[50];
  50. static int phones[20];
  51. static int myservice = 0;
  52.  
  53. static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
  54. {
  55.     ipc_callid_t callid;
  56.     ipc_call_t call;
  57.     ipcarg_t phonehash = icall->in_phone_hash;
  58.     int retval;
  59.     int i;
  60.  
  61.     printf("Connected phone: %P, accepting\n", icall->in_phone_hash);
  62.     ipc_answer_fast(iid, 0, 0, 0);
  63.     for (i=0;i < 1024;i++)
  64.         if (!connections[i]) {
  65.             connections[i] = phonehash;
  66.             break;
  67.         }
  68.    
  69.     while (1) {
  70.         callid = async_get_call(&call);
  71.         switch (IPC_GET_METHOD(call)) {
  72.         case IPC_M_PHONE_HUNGUP:
  73.             printf("Phone (%P) hung up.\n", phonehash);
  74.             retval = 0;
  75.             break;
  76.         default:
  77.             printf("Received message from %P: %X\n", phonehash,callid);
  78.             for (i=0; i < 1024; i++)
  79.                 if (!callids[i]) {
  80.                     callids[i] = callid;
  81.                     break;
  82.                 }
  83.             continue;
  84.         }
  85.         ipc_answer_fast(callid, retval, 0, 0);
  86.     }
  87. }
  88.  
  89. static void printhelp(void)
  90. {
  91.     printf("? - help\n");
  92.     printf("c - connect to other service\n");
  93.     printf("h - hangup connection\n");
  94.     printf("a - send async message to other service\n");
  95.     printf("s - send sync message to other service\n");
  96.     printf("d - answer message that we have received\n");
  97.     printf("j - jump to endless loop\n");
  98.     printf("p - page fault\n");
  99.     printf("u - unaligned read\n");
  100. }
  101.  
  102. static void callback(void *private, int retval, ipc_call_t *data)
  103. {
  104.     printf("Received response to msg %d - retval: %d.\n", private,
  105.            retval);
  106. }
  107.  
  108. static void do_answer_msg(void)
  109. {
  110.     int i,cnt, errn;
  111.     char c;
  112.     ipc_callid_t callid;
  113.  
  114.     cnt = 0;
  115.     for (i=0;i < 50;i++) {
  116.         if (callids[i]) {
  117.             printf("%d: %P\n", cnt, callids[i]);
  118.             cnt++;
  119.         }
  120.         if (cnt >= 10)
  121.             break;
  122.     }
  123.     if (!cnt)
  124.         return;
  125.     printf("Choose message:\n");
  126.     do {
  127.         c = getchar();
  128.     } while (c < '0' || (c-'0') >= cnt);
  129.     cnt = c - '0' + 1;
  130.    
  131.     for (i=0;cnt;i++)
  132.         if (callids[i])
  133.             cnt--;
  134.     i -= 1;
  135.  
  136.     printf("Normal (n) or hangup (h) or error(e) message?\n");
  137.     do {
  138.         c = getchar();
  139.     } while (c != 'n' && c != 'h' && c != 'e');
  140.     if (c == 'n')
  141.         errn = 0;
  142.     else if (c == 'h')
  143.         errn = EHANGUP;
  144.     else if (c == 'e')
  145.         errn = ENOENT;
  146.     printf("Answering %P\n", callids[i]);
  147.     ipc_answer_fast(callids[i], errn, 0, 0);
  148.     callids[i] = 0;
  149. }
  150.  
  151. static void do_send_msg(int async)
  152. {
  153.     int phoneid;
  154.     int res;
  155.     static int msgid = 1;
  156.     char c;
  157.  
  158.     printf("Select phoneid to send msg: 2-9\n");
  159.     do {
  160.         c = getchar();
  161.     } while (c < '2' || c > '9');
  162.     phoneid = c - '0';
  163.  
  164.     if (async) {
  165.         ipc_call_async(phoneid, 2000, 0, (void *)msgid, callback, 1);
  166.         printf("Async sent - msg %d\n", msgid);
  167.         msgid++;
  168.     } else {
  169.         printf("Sending msg...");
  170.         res = ipc_call_sync_2(phoneid, 2000, 0, 0, NULL, NULL);
  171.         printf("done: %d\n", res);
  172.     }
  173. }
  174.  
  175. static void do_hangup(void)
  176. {
  177.     char c;
  178.     int res;
  179.     int phoneid;
  180.  
  181.     printf("Select phoneid to hangup: 2-9\n");
  182.     do {
  183.         c = getchar();
  184.     } while (c < '2' || c > '9');
  185.     phoneid = c - '0';
  186.    
  187.     printf("Hanging up...");
  188.     res = ipc_hangup(phoneid);
  189.     printf("done: %d\n", phoneid);
  190. }
  191.  
  192. static void do_connect(void)
  193. {
  194.     char c;
  195.     int svc;
  196.     int phid;
  197.  
  198.     printf("Choose one service: 0:10000....9:10009\n");
  199.     do {
  200.         c = getchar();
  201.     } while (c < '0' || c > '9');
  202.     svc = TEST_START + c - '0';
  203.     if (svc == myservice) {
  204.         printf("Currently cannot connect to myself, update test\n");
  205.         return;
  206.     }
  207.     printf("Connecting to %d..", svc);
  208.     phid = ipc_connect_me_to(PHONE_NS, svc, 0);
  209.     if (phid > 0) {
  210.         printf("phoneid: %d\n", phid);
  211.         phones[phid] = 1;
  212.     } else
  213.         printf("error: %d\n", phid);
  214. }
  215.  
  216.  
  217.  
  218. int main(void)
  219. {
  220.     ipcarg_t phonead;
  221.     int i;
  222.     char c;
  223.     int res;
  224.     volatile long long var;
  225.     volatile int var1;
  226.    
  227.     printf("********************************\n");
  228.     printf("***********IPC Tester***********\n");
  229.     printf("********************************\n");
  230.  
  231.    
  232.     async_set_client_connection(client_connection);
  233.  
  234.     for (i=TEST_START;i < TEST_START+10;i++) {
  235.         res = ipc_connect_to_me(PHONE_NS, i, 0, &phonead);
  236.         if (!res)
  237.             break;
  238.         printf("Failed registering as %d..:%d\n", i, res);
  239.     }
  240.     printf("Registered as service: %d\n", i);
  241.     myservice = i;
  242.  
  243.     printhelp();
  244.     while (1) {
  245.         c = getchar();
  246.         switch (c) {
  247.         case '?':
  248.             printhelp();
  249.             break;
  250.         case 'h':
  251.             do_hangup();
  252.             break;
  253.         case 'c':
  254.             do_connect();
  255.             break;
  256.         case 'a':
  257.             do_send_msg(1);
  258.             break;
  259.         case 's':
  260.             do_send_msg(0);
  261.             break;
  262.         case 'd':
  263.             do_answer_msg();
  264.             break;
  265.         case 'j':
  266.             while (1)
  267.                 ;
  268.         case 'p':
  269.             printf("Doing page fault\n");
  270.             *((char *)0) = 1;
  271.             printf("done\n");
  272.         case 'u':
  273.             var1=*( (int *) ( ( (char *)(&var) ) + 1 ) );
  274.             break;
  275.         }
  276.     }
  277. }
  278.  
  279. /** @}
  280.  */
  281.  
  282.