Rev 1330 | Rev 1343 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
447 | decky | 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 | |||
713 | decky | 29 | #include "version.h" |
954 | palkovsky | 30 | #include <ipc.h> |
988 | palkovsky | 31 | #include <stdio.h> |
32 | #include <unistd.h> |
||
33 | #include <stdlib.h> |
||
1028 | palkovsky | 34 | #include <ns.h> |
1065 | jermar | 35 | #include <thread.h> |
1175 | jermar | 36 | #include <task.h> |
1113 | palkovsky | 37 | #include <psthread.h> |
1111 | jermar | 38 | #include <futex.h> |
1250 | jermar | 39 | #include <as.h> |
1279 | palkovsky | 40 | #include <ddi.h> |
1330 | palkovsky | 41 | #include <string.h> |
1339 | cejka | 42 | #include <kbd.h> |
999 | palkovsky | 43 | |
1089 | palkovsky | 44 | int a; |
1111 | jermar | 45 | atomic_t ftx; |
1089 | palkovsky | 46 | |
1152 | jermar | 47 | int __thread stage; |
1129 | palkovsky | 48 | |
1065 | jermar | 49 | extern void utest(void *arg); |
50 | void utest(void *arg) |
||
51 | { |
||
1081 | jermar | 52 | printf("Uspace thread started.\n"); |
1111 | jermar | 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 | |||
1065 | jermar | 60 | for (;;) |
61 | ; |
||
62 | } |
||
1049 | cejka | 63 | |
1197 | cejka | 64 | /* test different parameters types and modifiers */ |
995 | cejka | 65 | static void test_printf(void) |
66 | { |
||
67 | printf("Simple text.\n"); |
||
68 | printf("Now insert '%s' string.\n","this"); |
||
1073 | cejka | 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" ); |
||
1049 | cejka | 78 | |
995 | cejka | 79 | printf("Thats all, folks!\n"); |
80 | } |
||
999 | palkovsky | 81 | |
1197 | cejka | 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"); |
||
1049 | cejka | 89 | |
1197 | cejka | 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 | |||
1028 | palkovsky | 97 | extern char _heap; |
994 | jermar | 98 | static void test_mremap(void) |
988 | palkovsky | 99 | { |
100 | printf("Writing to good memory\n"); |
||
1228 | jermar | 101 | as_area_resize(&_heap, 120000, 0); |
988 | palkovsky | 102 | printf("%P\n", ((char *)&_heap)); |
103 | printf("%P\n", ((char *)&_heap) + 80000); |
||
104 | *(((char *)&_heap) + 80000) = 10; |
||
105 | printf("Making small\n"); |
||
1228 | jermar | 106 | as_area_resize(&_heap, 16000, 0); |
988 | palkovsky | 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 | |||
1028 | palkovsky | 141 | |
142 | static void test_ping(void) |
||
143 | { |
||
144 | ipcarg_t result; |
||
145 | int retval; |
||
146 | |||
1091 | palkovsky | 147 | printf("Pinging\n"); |
1028 | palkovsky | 148 | retval = ipc_call_sync(PHONE_NS, NS_PING, 0xbeef,&result); |
149 | printf("Retval: %d - received: %P\n", retval, result); |
||
150 | } |
||
151 | |||
1091 | palkovsky | 152 | static void got_answer(void *private, int retval, ipc_call_t *data) |
999 | palkovsky | 153 | { |
1073 | cejka | 154 | printf("Retval: %d...%s...%zX, %zX\n", retval, private, |
999 | palkovsky | 155 | IPC_GET_ARG1(*data), IPC_GET_ARG2(*data)); |
156 | } |
||
157 | static void test_async_ipc(void) |
||
158 | { |
||
1028 | palkovsky | 159 | ipc_call_t data; |
999 | palkovsky | 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 | |||
1028 | palkovsky | 183 | |
1091 | palkovsky | 184 | static void got_answer_2(void *private, int retval, ipc_call_t *data) |
1028 | palkovsky | 185 | { |
186 | printf("Pong\n"); |
||
187 | } |
||
188 | static void test_advanced_ipc(void) |
||
189 | { |
||
190 | int res; |
||
1091 | palkovsky | 191 | ipcarg_t phonead; |
1028 | palkovsky | 192 | ipc_callid_t callid; |
193 | ipc_call_t data; |
||
1061 | palkovsky | 194 | int i; |
1028 | palkovsky | 195 | |
196 | printf("Asking 0 to connect to me...\n"); |
||
1091 | palkovsky | 197 | res = ipc_connect_to_me(0, 1, 2, &phonead); |
198 | printf("Result: %d - phonead: %llu\n", res, phonead); |
||
1061 | palkovsky | 199 | for (i=0; i < 100; i++) { |
1028 | palkovsky | 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); |
||
1061 | palkovsky | 206 | } |
1089 | palkovsky | 207 | // callid = ipc_wait_for_call(&data, NULL); |
1028 | palkovsky | 208 | } |
209 | |||
1061 | palkovsky | 210 | static void test_connection_ipc(void) |
211 | { |
||
212 | int res; |
||
213 | ipcarg_t result; |
||
1091 | palkovsky | 214 | int phoneid; |
1061 | palkovsky | 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); |
||
1089 | palkovsky | 221 | printf("Retval: %d - received: %X\n", res, result); |
1061 | palkovsky | 222 | |
223 | } |
||
224 | |||
1089 | palkovsky | 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 | |||
1113 | palkovsky | 278 | static int ptest(void *arg) |
279 | { |
||
1152 | jermar | 280 | stage = 1; |
281 | printf("Pseudo thread stage%d.\n", stage); |
||
282 | stage++; |
||
1128 | jermar | 283 | psthread_schedule_next(); |
1152 | jermar | 284 | printf("Pseudo thread stage%d.\n", stage); |
285 | stage++; |
||
1128 | jermar | 286 | psthread_schedule_next(); |
1152 | jermar | 287 | printf("Pseudo thread stage%d.\n", stage); |
1128 | jermar | 288 | psthread_schedule_next(); |
1152 | jermar | 289 | stage++; |
290 | printf("Pseudo thread stage%d.\n", stage); |
||
1128 | jermar | 291 | psthread_schedule_next(); |
292 | printf("Pseudo thread exiting.\n"); |
||
1113 | palkovsky | 293 | return 0; |
294 | } |
||
1111 | jermar | 295 | |
1339 | cejka | 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 | |||
1330 | palkovsky | 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 | |||
447 | decky | 341 | int main(int argc, char *argv[]) |
342 | { |
||
1113 | palkovsky | 343 | pstid_t ptid; |
1065 | jermar | 344 | int tid; |
1197 | cejka | 345 | |
713 | decky | 346 | version_print(); |
954 | palkovsky | 347 | |
1125 | jermar | 348 | // test_printf(); |
1197 | cejka | 349 | // test_printf2(); |
1028 | palkovsky | 350 | // test_ping(); |
351 | // test_async_ipc(); |
||
1061 | palkovsky | 352 | // test_advanced_ipc(); |
1089 | palkovsky | 353 | // test_connection_ipc(); |
354 | // test_hangup(); |
||
1091 | palkovsky | 355 | // test_slam(); |
1339 | cejka | 356 | // test_as_send(); |
357 | test_kbd(); |
||
358 | |||
1330 | palkovsky | 359 | /* |
1228 | jermar | 360 | printf("Userspace task, taskid=%llX\n", task_get_id()); |
1175 | jermar | 361 | |
1111 | jermar | 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"); |
||
1089 | palkovsky | 367 | |
1111 | jermar | 368 | if (futex_down(&ftx) < 0) |
369 | printf("Futex failed.\n"); |
||
1152 | jermar | 370 | |
1125 | jermar | 371 | if ((tid = thread_create(utest, NULL, "utest")) != -1) { |
1091 | palkovsky | 372 | printf("Created thread tid=%d\n", tid); |
373 | } |
||
1111 | jermar | 374 | |
1125 | jermar | 375 | if ((tid = thread_create(utest, NULL, "utest")) != -1) { |
376 | printf("Created thread tid=%d\n", tid); |
||
377 | } |
||
1152 | jermar | 378 | |
1111 | jermar | 379 | int i; |
380 | |||
1125 | jermar | 381 | for (i = 0; i < 50000000; i++) |
1111 | jermar | 382 | ; |
383 | |||
384 | if (futex_up(&ftx) < 0) |
||
385 | printf("Futex failed.\n"); |
||
386 | |||
1129 | palkovsky | 387 | |
1152 | jermar | 388 | printf("Creating pseudo thread.\n"); |
389 | stage = 1; |
||
1113 | palkovsky | 390 | ptid = psthread_create(ptest, NULL); |
1152 | jermar | 391 | printf("Main thread stage%d.\n", stage); |
392 | stage++; |
||
1128 | jermar | 393 | psthread_schedule_next();; |
1152 | jermar | 394 | printf("Main thread stage%d.\n", stage); |
395 | stage++; |
||
1128 | jermar | 396 | psthread_schedule_next();; |
1152 | jermar | 397 | printf("Main thread stage%d.\n", stage); |
1113 | palkovsky | 398 | |
1128 | jermar | 399 | psthread_join(ptid); |
1113 | palkovsky | 400 | |
1111 | jermar | 401 | printf("Main thread exiting.\n"); |
1330 | palkovsky | 402 | */ |
447 | decky | 403 | return 0; |
404 | } |