Subversion Repositories HelenOS-historic

Rev

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

  1. /*
  2.  * Copyright (C) 2005 Martin Decky
  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 "version.h"
  30. #include <ipc.h>
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. #include <stdlib.h>
  34. #include <ns.h>
  35. #include <thread.h>
  36. #include <task.h>
  37. #include <psthread.h>
  38. #include <futex.h>
  39. #include <as.h>
  40. #include <ddi.h>
  41. #include <string.h>
  42. #include <kbd.h>
  43.  
  44. int a;
  45. atomic_t ftx;
  46.  
  47. int __thread stage;
  48.  
  49. extern void utest(void *arg);
  50. void utest(void *arg)
  51. {
  52.     printf("Uspace thread started.\n");
  53.     if (futex_down(&ftx) < 0)
  54.         printf("Futex failed.\n");
  55.     if (futex_up(&ftx) < 0)
  56.         printf("Futex failed.\n");
  57.        
  58.     printf("%s in good condition.\n", __FUNCTION__);
  59.    
  60.     for (;;)
  61.         ;
  62. }
  63.  
  64. /* test different parameters types and modifiers */
  65. static void test_printf(void)
  66. {
  67.     printf("Simple text.\n");
  68.     printf("Now insert '%s' string.\n","this");
  69.     printf("Signed formats on uns. numbers: '%d', '%+d', '% d', '%u' (,+, ,u)\n", 321, 321, 321, 321);
  70.     printf("Signed formats on sig. numbers: '%d', '%+d', '% d', '%u' (,+, ,u)\n", -321, -321, -321, -321);
  71.     printf("Signed with different sized: '%hhd', '%hd', '%d', '%ld', %lld;\n", -3, -32, -321, -32101l, -3210123ll);
  72.     printf("And now... '%hhd' byte! '%hd' word! '%d' int! \n", 11, 11111, 1111111111);
  73.     printf("Different bases: %#hx, %#hu, %#ho and %#hb\n", 123, 123, 123, 123);
  74.     printf("Different bases signed: %#hx, %#hu, %#ho and %#hb\n", -123, -123, -123, -123);
  75.     printf("'%llX' llX! Another '%llx' llx! \n", 0x1234567887654321ll, 0x1234567887654321ll);
  76.     printf("'%llX' with 64bit value and '%x' with 32 bit value. \n", 0x1234567887654321ll, 0x12345678 );
  77.     printf("'%llx' 64bit, '%x' 32bit, '%hhx' 8bit, '%hx' 16bit, '%llX' 64bit and '%s' string.\n", 0x1234567887654321ll, 0x12345678, 0x12, 0x1234, 0x1234567887654321ull, "Lovely string" );
  78.    
  79.     printf("Thats all, folks!\n");
  80. }
  81.  
  82. /* test width and precision modifiers */
  83. static void test_printf2(void)
  84. {
  85.     printf(" text 10.8s %*.*s \n", 5, 3, "text");
  86.     printf(" very long text 10.8s %10.8s \n", "very long text");
  87.     printf(" text 8.10s %8.10s \n", "text");
  88.     printf(" very long text 8.10s %8.10s \n", "very long text");
  89.  
  90.     printf(" char: c '%c', 3.2c '%3.2c', -3.2c '%-3.2c', 2.3c '%2.3c', -2.3c '%-2.3c' \n",'a', 'b', 'c', 'd', 'e' );
  91.     printf(" int: d '%d', 3.2d '%3.2d', -3.2d '%-3.2d', 2.3d '%2.3d', -2.3d '%-2.3d' \n",1, 1, 1, 1, 1 );
  92.     printf(" -int: d '%d', 3.2d '%3.2d', -3.2d '%-3.2d', 2.3d '%2.3d', -2.3d '%-2.3d' \n",-1, -1, -1, -1, -1 );
  93.     printf(" 0xint: x '%x', 5.3x '%#5.3x', -5.3x '%#-5.3x', 3.5x '%#3.5x', -3.5x '%#-3.5x' \n",17, 17, 17, 17, 17 );
  94.  
  95. }
  96.  
  97. extern char _heap;
  98. static void test_mremap(void)
  99. {
  100.     printf("Writing to good memory\n");
  101.     as_area_resize(&_heap, 120000, 0);
  102.     printf("%P\n", ((char *)&_heap));
  103.     printf("%P\n", ((char *)&_heap) + 80000);
  104.     *(((char *)&_heap) + 80000) = 10;
  105.     printf("Making small\n");
  106.     as_area_resize(&_heap, 16000, 0);
  107.     printf("Failing..\n");
  108.     *((&_heap) + 80000) = 10;
  109.  
  110.     printf("memory done\n");
  111. }
  112. /*
  113. static void test_sbrk(void)
  114. {
  115.     printf("Writing to good memory\n");
  116.     printf("Got: %P\n", sbrk(120000));
  117.     printf("%P\n", ((char *)&_heap));
  118.     printf("%P\n", ((char *)&_heap) + 80000);
  119.     *(((char *)&_heap) + 80000) = 10;
  120.     printf("Making small, got: %P\n",sbrk(-120000));
  121.     printf("Testing access to heap\n");
  122.     _heap = 10;
  123.     printf("Failing..\n");
  124.     *((&_heap) + 80000) = 10;
  125.  
  126.     printf("memory done\n");
  127. }
  128. */
  129. /*
  130. static void test_malloc(void)
  131. {
  132.     char *data;
  133.  
  134.     data = malloc(10);
  135.     printf("Heap: %P, data: %P\n", &_heap, data);
  136.     data[0] = 'a';
  137.     free(data);
  138. }
  139. */
  140.  
  141.  
  142. static void test_ping(void)
  143. {
  144.     ipcarg_t result;
  145.     int retval;
  146.  
  147.     printf("Pinging\n");
  148.     retval = ipc_call_sync(PHONE_NS, NS_PING, 0xbeef,&result);
  149.     printf("Retval: %d - received: %P\n", retval, result);
  150. }
  151.  
  152. static void got_answer(void *private, int retval, ipc_call_t *data)
  153. {
  154.     printf("Retval: %d...%s...%zX, %zX\n", retval, private,
  155.            IPC_GET_ARG1(*data), IPC_GET_ARG2(*data));
  156. }
  157. static void test_async_ipc(void)
  158. {
  159.     ipc_call_t data;
  160.     int i;
  161.  
  162.     printf("Sending ping\n");
  163.     ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
  164.              "Pong1", got_answer);
  165.     ipc_call_async_2(PHONE_NS, NS_PING, 2, 0xbeefbee4,
  166.              "Pong2", got_answer);
  167.     ipc_call_async_2(PHONE_NS, NS_PING, 3, 0xbeefbee4,
  168.              "Pong3", got_answer);
  169.     ipc_call_async_2(PHONE_NS, NS_PING, 4, 0xbeefbee4,
  170.              "Pong4", got_answer);
  171.     ipc_call_async_2(PHONE_NS, NS_PING, 5, 0xbeefbee4,
  172.              "Pong5", got_answer);
  173.     ipc_call_async_2(PHONE_NS, NS_PING, 6, 0xbeefbee4,
  174.              "Pong6", got_answer);
  175.     printf("Waiting forever...\n");
  176.     for (i=0; i<100;i++)
  177.         printf(".");
  178.     printf("\n");
  179.     ipc_wait_for_call(&data, NULL);
  180.     printf("Received call???\n");
  181. }
  182.  
  183.  
  184. static void got_answer_2(void *private, int retval, ipc_call_t *data)
  185. {
  186.     printf("Pong\n");
  187. }
  188. static void test_advanced_ipc(void)
  189. {
  190.     int res;
  191.     ipcarg_t phonead;
  192.     ipc_callid_t callid;
  193.     ipc_call_t data;
  194.     int i;
  195.  
  196.     printf("Asking 0 to connect to me...\n");
  197.     res = ipc_connect_to_me(0, 1, 2, &phonead);
  198.     printf("Result: %d - phonead: %llu\n", res, phonead);
  199.     for (i=0; i < 100; i++) {
  200.         printf("----------------\n");
  201.         ipc_call_async(PHONE_NS, NS_PING_SVC, 0, "prov",
  202.                    got_answer_2);
  203.         callid = ipc_wait_for_call(&data, NULL);
  204.         printf("Received ping\n");
  205.         ipc_answer(callid, 0, 0, 0);
  206.     }
  207. //  callid = ipc_wait_for_call(&data, NULL);
  208. }
  209.  
  210. static void test_connection_ipc(void)
  211. {
  212.     int res;
  213.     ipcarg_t result;
  214.     int phoneid;
  215.  
  216.     printf("Starting connect...\n");
  217.     res = ipc_connect_me_to(PHONE_NS, 10, 20);
  218.     printf("Connected: %d\n", res);
  219.     printf("pinging.\n");
  220.     res = ipc_call_sync(res, NS_PING, 0xbeef,&result);
  221.     printf("Retval: %d - received: %X\n", res, result);
  222.    
  223. }
  224.  
  225. static void test_hangup(void)
  226. {
  227.     int phoneid;
  228.     ipc_call_t data;
  229.     ipc_callid_t callid;
  230.     int i;
  231.  
  232.     printf("Starting connect...\n");
  233.     phoneid = ipc_connect_me_to(PHONE_NS, 10, 20);
  234.     printf("Phoneid: %d, pinging\n", phoneid);
  235.     ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
  236.              "Pong1", got_answer);
  237.     printf("Hangin up\n");
  238.     ipc_hangup(phoneid);
  239.     printf("Connecting\n");
  240.     phoneid = ipc_connect_me_to(PHONE_NS, 10, 20);
  241.     printf("Newphid: %d\n", phoneid);
  242.     for (i=0; i < 1000; i++) {
  243.         if ((callid=ipc_wait_for_call(&data, IPC_WAIT_NONBLOCKING)))
  244.             printf("callid: %d\n");
  245.     }
  246.     printf("New new phoneid: %d\n", ipc_connect_me_to(PHONE_NS, 10, 20));
  247. }
  248.  
  249. static void test_slam(void)
  250. {
  251.     int i;
  252.     ipc_call_t data;
  253.     ipc_callid_t callid;
  254.  
  255.     printf("ping");
  256.     ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
  257.              "Pong1", got_answer);
  258.     printf("slam");
  259.     ipc_call_async_2(PHONE_NS, NS_HANGUP, 1, 0xbeefbee2,
  260.              "Hang", got_answer);
  261.     printf("ping2\n");
  262.     ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
  263.              "Ping2", got_answer);
  264.    
  265.     for (i=0; i < 1000; i++) {
  266.         if ((callid=ipc_wait_for_call(&data, IPC_WAIT_NONBLOCKING)))
  267.             printf("callid: %d\n");
  268.     }
  269.     ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
  270.              "Pong1", got_answer);
  271.     printf("Closing file\n");
  272.     ipc_hangup(PHONE_NS);
  273.     ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
  274.              "Pong1", got_answer);
  275.     ipc_wait_for_call(&data, 0);
  276. }
  277.  
  278. static int ptest(void *arg)
  279. {
  280.     stage = 1;
  281.     printf("Pseudo thread stage%d.\n", stage);
  282.     stage++;
  283.     psthread_schedule_next();
  284.     printf("Pseudo thread stage%d.\n", stage);
  285.     stage++;
  286.     psthread_schedule_next();
  287.     printf("Pseudo thread stage%d.\n", stage);
  288.     psthread_schedule_next();
  289.     stage++;
  290.     printf("Pseudo thread stage%d.\n", stage);
  291.     psthread_schedule_next();
  292.     printf("Pseudo thread exiting.\n");
  293.     return 0;  
  294. }
  295.  
  296. static void test_kbd()
  297. {
  298.     int res;
  299.     ipcarg_t result;
  300.     int phoneid;
  301.  
  302.     printf("Test: Starting connect...\n");
  303.     while ((phoneid = ipc_connect_me_to(PHONE_NS, 30, 60)) < 0) {
  304.     };
  305.    
  306.     printf("Test: Connected: %d\n", res);
  307.     printf("Test: pinging.\n");
  308.     while (1) {
  309.         res = ipc_call_sync(phoneid, KBD_GETCHAR, 0xbeef,&result);
  310. //      printf("Test: Retval: %d - received: %c\n", res, result);
  311.         printf("%c", result);
  312.     }
  313.    
  314.     printf("Test: Hangin up\n");
  315.     ipc_hangup(phoneid);
  316. }
  317.  
  318. static int test_as_send()
  319. {
  320.     char *as;
  321.     int retval;
  322.     ipcarg_t result;
  323.  
  324.     as = as_area_create((void *)(1024*1024), 16384, AS_AREA_READ | AS_AREA_WRITE);
  325.     if (!as) {
  326.         printf("Error creating as.\n");
  327.         return 0;
  328.     }
  329.  
  330.     memcpy(as, "Hello world.\n", 14);
  331.  
  332.     retval = ipc_call_sync_2(PHONE_NS, IPC_M_AS_SEND, 0, (sysarg_t) as,
  333.         NULL, NULL);
  334.     if (retval) {
  335.         printf("AS_SEND failed.\n");
  336.         return 0;
  337.     }
  338.     printf("Done\n");
  339. }
  340.  
  341. int main(int argc, char *argv[])
  342. {
  343.     pstid_t ptid;
  344.     int tid;
  345.    
  346.     version_print();
  347.  
  348. //  test_printf();
  349. //  test_printf2();
  350. //  test_ping();
  351. //  test_async_ipc();
  352. //  test_advanced_ipc();
  353. //  test_connection_ipc();
  354. //  test_hangup();
  355. //  test_slam();
  356. //  test_as_send();
  357.     test_kbd();
  358.  
  359. /* 
  360.     printf("Userspace task, taskid=%llX\n", task_get_id());
  361.  
  362.     futex_initialize(&ftx, 1);
  363.     if (futex_down(&ftx) < 0)
  364.         printf("Futex failed.\n");
  365.     if (futex_up(&ftx) < 0)
  366.         printf("Futex failed.\n");
  367.  
  368.     if (futex_down(&ftx) < 0)
  369.         printf("Futex failed.\n");
  370.  
  371.     if ((tid = thread_create(utest, NULL, "utest")) != -1) {
  372.         printf("Created thread tid=%d\n", tid);
  373.     }
  374.  
  375.     if ((tid = thread_create(utest, NULL, "utest")) != -1) {
  376.         printf("Created thread tid=%d\n", tid);
  377.     }
  378.  
  379.     int i;
  380.    
  381.     for (i = 0; i < 50000000; i++)
  382.         ;
  383.        
  384.     if (futex_up(&ftx) < 0)
  385.         printf("Futex failed.\n");
  386.  
  387.  
  388.     printf("Creating pseudo thread.\n");
  389.     stage = 1;
  390.     ptid = psthread_create(ptest, NULL);
  391.     printf("Main thread stage%d.\n", stage);
  392.     stage++;
  393.     psthread_schedule_next();;
  394.     printf("Main thread stage%d.\n", stage);
  395.     stage++;
  396.     psthread_schedule_next();;
  397.     printf("Main thread stage%d.\n", stage);
  398.  
  399.     psthread_join(ptid);
  400.  
  401.     printf("Main thread exiting.\n");
  402. */
  403.     return 0;
  404. }
  405.