Rev 1330 | Rev 1336 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1330 | Rev 1335 | ||
---|---|---|---|
Line -... | Line 1... | ||
- | 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 | /** |
|
- | 30 | * @file ns.c |
|
- | 31 | * @brief Naming service for HelenOS IPC. |
|
- | 32 | */ |
|
- | 33 | ||
1 | #include <ipc.h> |
34 | #include <ipc.h> |
2 | #include <stdio.h> |
35 | #include <stdio.h> |
3 | #include <unistd.h> |
36 | #include <unistd.h> |
4 | #include <stdlib.h> |
37 | #include <stdlib.h> |
5 | #include <ns.h> |
38 | #include <ns.h> |
6 | #include <errno.h> |
39 | #include <errno.h> |
- | 40 | #include <assert.h> |
|
- | 41 | #include <libadt/list.h> |
|
- | 42 | #include <libadt/hash_table.h> |
|
- | 43 | ||
- | 44 | #define NAME "NS" |
|
- | 45 | ||
- | 46 | #define NS_HASH_TABLE_CHAINS 20 |
|
- | 47 | ||
- | 48 | /* Static functions implementing NS hash table operations. */ |
|
- | 49 | static hash_index_t ns_hash(unsigned long *key); |
|
- | 50 | static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item); |
|
- | 51 | ||
- | 52 | /** Operations for NS hash table. */ |
|
- | 53 | static hash_table_operations_t ns_hash_table_ops = { |
|
- | 54 | .hash = ns_hash, |
|
- | 55 | .compare = ns_compare, |
|
- | 56 | .remove_callback = NULL |
|
- | 57 | }; |
|
- | 58 | ||
- | 59 | /** NS hash table structure. */ |
|
- | 60 | static hash_table_t ns_hash_table; |
|
- | 61 | ||
- | 62 | /** NS hash table item. */ |
|
- | 63 | typedef struct { |
|
- | 64 | link_t link; |
|
- | 65 | ipcarg_t service; /**< Number of the service. */ |
|
- | 66 | ipcarg_t phone; /**< Phone registered with the service. */ |
|
- | 67 | } hashed_service_t; |
|
- | 68 | ||
7 | /* |
69 | /* |
8 | irq_cmd_t msim_cmds[1] = { |
70 | irq_cmd_t msim_cmds[1] = { |
9 | { CMD_MEM_READ_1, (void *)0xB0000000, 0 } |
71 | { CMD_MEM_READ_1, (void *)0xB0000000, 0 } |
10 | }; |
72 | }; |
11 | 73 | ||
Line 23... | Line 85... | ||
23 | 1, |
85 | 1, |
24 | i8042_cmds |
86 | i8042_cmds |
25 | }; |
87 | }; |
26 | */ |
88 | */ |
27 | 89 | ||
- | 90 | ||
28 | static int service; |
91 | int static ping_phone; |
29 | 92 | ||
30 | int main(int argc, char **argv) |
93 | int main(int argc, char **argv) |
31 | { |
94 | { |
32 | ipc_call_t call; |
95 | ipc_call_t call; |
33 | ipc_callid_t callid; |
96 | ipc_callid_t callid; |
34 | char *as; |
97 | char *as; |
35 | 98 | ||
36 | ipcarg_t retval, arg1, arg2; |
99 | ipcarg_t retval, arg1, arg2; |
37 | 100 | ||
38 | printf("NS:Name service started.\n"); |
101 | printf("%s: Name service started.\n", NAME); |
- | 102 | ||
- | 103 | if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 1, &ns_hash_table_ops)) { |
|
- | 104 | printf("%s: cannot create hash table\n", NAME); |
|
- | 105 | return ENOMEM; |
|
- | 106 | } |
|
- | 107 | ||
- | 108 | ||
39 | // ipc_register_irq(2, &msim_kbd); |
109 | // ipc_register_irq(2, &msim_kbd); |
40 | // ipc_register_irq(1, &i8042_kbd); |
110 | // ipc_register_irq(1, &i8042_kbd); |
41 | while (1) { |
111 | while (1) { |
42 | callid = ipc_wait_for_call(&call, 0); |
112 | callid = ipc_wait_for_call(&call, 0); |
43 | printf("NS:Call phone=%lX..", call.phoneid); |
113 | printf("NS: Call phone=%lX...", call.phoneid); |
44 | switch (IPC_GET_METHOD(call)) { |
114 | switch (IPC_GET_METHOD(call)) { |
45 | case IPC_M_AS_SEND: |
115 | case IPC_M_AS_SEND: |
46 | as = (char *)IPC_GET_ARG2(call); |
116 | as = (char *)IPC_GET_ARG2(call); |
47 | printf("Received as: %P, size:%d\n", as, IPC_GET_ARG3(call)); |
117 | printf("Received as: %P, size:%d\n", as, IPC_GET_ARG3(call)); |
48 | retval = ipc_answer(callid, 0,(sysarg_t)(1024*1024), 0); |
118 | retval = ipc_answer(callid, 0,(sysarg_t)(1024*1024), 0); |
Line 58... | Line 128... | ||
58 | break; |
128 | break; |
59 | case IPC_M_PHONE_HUNGUP: |
129 | case IPC_M_PHONE_HUNGUP: |
60 | printf("Phone hung up.\n"); |
130 | printf("Phone hung up.\n"); |
61 | retval = 0; |
131 | retval = 0; |
62 | break; |
132 | break; |
63 | case IPC_M_CONNECT_TO_ME: |
133 | case IPC_M_CONNECT_TO_ME:; |
- | 134 | /* |
|
- | 135 | * Request for registration of a service. |
|
- | 136 | */ |
|
- | 137 | ipcarg_t service; |
|
- | 138 | ipcarg_t phone; |
|
- | 139 | hashed_service_t *hs; |
|
- | 140 | ||
64 | printf("Somebody connecting phid=%zd.\n", IPC_GET_ARG3(call)); |
141 | service = IPC_GET_ARG1(call); |
65 | service = IPC_GET_ARG3(call); |
142 | phone = IPC_GET_ARG3(call); |
- | 143 | printf("Registering service %d on phone %d...", service, phone); |
|
- | 144 | ||
- | 145 | hs = (hashed_service_t *) malloc(sizeof(hashed_service_t)); |
|
- | 146 | if (!hs) { |
|
- | 147 | printf("Failed to register service %d.\n", service); |
|
- | 148 | } |
|
- | 149 | ||
- | 150 | link_initialize(&hs->link); |
|
- | 151 | hs->service = service; |
|
- | 152 | hs->phone = phone; |
|
- | 153 | hash_table_insert(&ns_hash_table, (unsigned long *) &service, &hs->link); |
|
- | 154 | ||
- | 155 | ping_phone = phone; |
|
66 | retval = 0; |
156 | retval = 0; |
67 | break; |
157 | break; |
68 | case IPC_M_CONNECT_ME_TO: |
158 | case IPC_M_CONNECT_ME_TO: |
69 | printf("Connectme(%P)to: %zd\n", |
159 | printf("Connectme(%P)to: %zd\n", |
70 | IPC_GET_ARG3(call), IPC_GET_ARG1(call)); |
160 | IPC_GET_ARG3(call), IPC_GET_ARG1(call)); |
Line 80... | Line 170... | ||
80 | case NS_HANGUP: |
170 | case NS_HANGUP: |
81 | printf("Closing connection.\n"); |
171 | printf("Closing connection.\n"); |
82 | retval = EHANGUP; |
172 | retval = EHANGUP; |
83 | break; |
173 | break; |
84 | case NS_PING_SVC: |
174 | case NS_PING_SVC: |
85 | printf("NS:Pinging service %d\n", service); |
175 | printf("NS:Pinging service %d\n", ping_phone); |
86 | ipc_call_sync(service, NS_PING, 0xbeef, 0); |
176 | ipc_call_sync(ping_phone, NS_PING, 0xbeef, 0); |
87 | printf("NS:Got pong\n"); |
177 | printf("NS:Got pong\n"); |
88 | break; |
178 | break; |
89 | default: |
179 | default: |
90 | printf("Unknown method: %zd\n", IPC_GET_METHOD(call)); |
180 | printf("Unknown method: %zd\n", IPC_GET_METHOD(call)); |
91 | retval = ENOENT; |
181 | retval = ENOENT; |
92 | break; |
182 | break; |
93 | } |
183 | } |
94 | if (! (callid & IPC_CALLID_NOTIFICATION)) { |
184 | if (! (callid & IPC_CALLID_NOTIFICATION)) { |
95 | printf("Answerinh\n"); |
185 | printf("Answering.\n"); |
96 | ipc_answer(callid, retval, arg1, arg2); |
186 | ipc_answer(callid, retval, arg1, arg2); |
97 | } |
187 | } |
98 | } |
188 | } |
99 | } |
189 | } |
- | 190 | ||
- | 191 | /** Compute hash index into NS hash table. |
|
- | 192 | * |
|
- | 193 | * @param key Pointer to single key (i.e. service number). |
|
- | 194 | * @return Hash index corresponding to *key. |
|
- | 195 | */ |
|
- | 196 | hash_index_t ns_hash(unsigned long *key) |
|
- | 197 | { |
|
- | 198 | assert(key); |
|
- | 199 | return *key % NS_HASH_TABLE_CHAINS; |
|
- | 200 | } |
|
- | 201 | ||
- | 202 | /** Compare a key with hashed item. |
|
- | 203 | * |
|
- | 204 | * @param key Single key pointer. |
|
- | 205 | * @param keys Must be 1. |
|
- | 206 | * @param item Pointer to a hash table item. |
|
- | 207 | * @return Non-zero if the key matches the item, zero otherwise. |
|
- | 208 | */ |
|
- | 209 | int ns_compare(unsigned long *key, hash_count_t keys, link_t *item) |
|
- | 210 | { |
|
- | 211 | hashed_service_t *hs; |
|
- | 212 | ||
- | 213 | assert(key); |
|
- | 214 | assert(keys == 1); |
|
- | 215 | assert(item); |
|
- | 216 | ||
- | 217 | hs = hash_table_get_instance(item, hashed_service_t, link); |
|
- | 218 | ||
- | 219 | return *key == hs->service; |
|
- | 220 | } |