2 * Copyright (C) 2006 Ondrej Palkovsky
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
31 * @brief Naming service for HelenOS IPC.
41 #include <libadt/list.h>
42 #include <libadt/hash_table.h>
46 #define NS_HASH_TABLE_CHAINS 20
48 static int register_service(ipcarg_t service
, ipcarg_t phone
, ipc_call_t
*call
);
49 static int connect_to_service(ipcarg_t service
, ipc_call_t
*call
, ipc_callid_t callid
);
51 /* Static functions implementing NS hash table operations. */
52 static hash_index_t
ns_hash(unsigned long *key
);
53 static int ns_compare(unsigned long *key
, hash_count_t keys
, link_t
*item
);
54 static void ns_remove(link_t
*item
);
56 /** Operations for NS hash table. */
57 static hash_table_operations_t ns_hash_table_ops
= {
59 .compare
= ns_compare
,
60 .remove_callback
= ns_remove
63 /** NS hash table structure. */
64 static hash_table_t ns_hash_table
;
66 /** NS hash table item. */
69 ipcarg_t service
; /**< Number of the service. */
70 ipcarg_t phone
; /**< Phone registered with the service. */
71 ipcarg_t in_phone_hash
; /**< Incoming phone hash. */
75 irq_cmd_t msim_cmds[1] = {
76 { CMD_MEM_READ_1, (void *)0xB0000000, 0 }
79 irq_code_t msim_kbd = {
85 irq_cmd_t i8042_cmds[1] = {
86 { CMD_PORT_READ_1, (void *)0x60, 0 }
89 irq_code_t i8042_kbd = {
96 int static ping_phone
;
98 int main(int argc
, char **argv
)
104 ipcarg_t retval
, arg1
, arg2
;
106 printf("%s: Naming service started.\n", NAME
);
108 if (!hash_table_create(&ns_hash_table
, NS_HASH_TABLE_CHAINS
, 3, &ns_hash_table_ops
)) {
109 printf("%s: cannot create hash table\n", NAME
);
114 // ipc_register_irq(2, &msim_kbd);
115 // ipc_register_irq(1, &i8042_kbd);
117 callid
= ipc_wait_for_call(&call
, 0);
118 printf("NS: Call in_phone_hash=%lX...", call
.in_phone_hash
);
119 switch (IPC_GET_METHOD(call
)) {
121 as
= (char *)IPC_GET_ARG2(call
);
122 printf("Received as: %P, size:%d\n", as
, IPC_GET_ARG3(call
));
123 retval
= ipc_answer(callid
, 0,(sysarg_t
)(1024*1024), 0);
125 printf("Reading shared memory...");
126 printf("Text: %s", as
);
128 printf("Failed answer: %d\n", retval
);
131 case IPC_M_INTERRUPT
:
132 printf("GOT INTERRUPT: %c\n", IPC_GET_ARG2(call
));
134 case IPC_M_PHONE_HUNGUP
:
135 printf("Phone hung up.\n");
138 case IPC_M_CONNECT_TO_ME
:
140 * Server requests service registration.
142 retval
= register_service(IPC_GET_ARG1(call
), IPC_GET_ARG3(call
), &call
);
143 ping_phone
= IPC_GET_ARG3(call
);
145 case IPC_M_CONNECT_ME_TO
:
147 * Client requests to be connected to a service.
149 retval
= connect_to_service(IPC_GET_ARG1(call
), &call
, callid
);
152 printf("Closing connection.\n");
156 printf("Ping...%P %P\n", IPC_GET_ARG1(call
),
163 printf("NS:Pinging service %d\n", ping_phone
);
164 ipc_call_sync(ping_phone
, NS_PING
, 0xbeef, 0);
165 printf("NS:Got pong\n");
168 printf("Unknown method: %zd\n", IPC_GET_METHOD(call
));
172 if (! (callid
& IPC_CALLID_NOTIFICATION
)) {
173 printf("Answering.\n");
174 ipc_answer(callid
, retval
, arg1
, arg2
);
179 /** Register service.
181 * @param service Service to be registered.
182 * @param phone phone Phone to be used for connections to the service.
183 * @param call Pointer to call structure.
185 * @return Zero on success or a value from @ref errno.h.
187 int register_service(ipcarg_t service
, ipcarg_t phone
, ipc_call_t
*call
)
189 unsigned long keys
[3] = { service
, call
->in_phone_hash
, 0 };
190 hashed_service_t
*hs
;
192 printf("Registering service %d on phone %d...", service
, phone
);
194 if (hash_table_find(&ns_hash_table
, keys
)) {
195 printf("Service %d already registered.\n", service
);
199 hs
= (hashed_service_t
*) malloc(sizeof(hashed_service_t
));
201 printf("Failed to register service %d.\n", service
);
205 link_initialize(&hs
->link
);
206 hs
->service
= service
;
208 hs
->in_phone_hash
= call
->in_phone_hash
;
209 hash_table_insert(&ns_hash_table
, keys
, &hs
->link
);
214 /** Connect client to service.
216 * @param service Service to be connected to.
217 * @param call Pointer to call structure.
218 * @param callid Call ID of the request.
220 * @return Zero on success or a value from @ref errno.h.
222 int connect_to_service(ipcarg_t service
, ipc_call_t
*call
, ipc_callid_t callid
)
224 unsigned long keys
[3] = { service
, 0, 0 };
226 hashed_service_t
*hs
;
228 hlp
= hash_table_find(&ns_hash_table
, keys
);
230 printf("Service %d not registered.\n", service
);
233 hs
= hash_table_get_instance(hlp
, hashed_service_t
, link
);
234 printf("Connecting in_phone_hash=%lX to service at phone %d...", call
->in_phone_hash
, hs
->phone
);
235 return ipc_forward_fast(callid
, hs
->phone
, 0, 0);
238 /** Compute hash index into NS hash table.
240 * @param key Pointer keys. However, only the first key (i.e. service number)
241 * is used to compute the hash index.
242 * @return Hash index corresponding to key[0].
244 hash_index_t
ns_hash(unsigned long *key
)
247 return *key
% NS_HASH_TABLE_CHAINS
;
250 /** Compare a key with hashed item.
252 * This compare function always ignores the third key.
253 * It exists only to make it possible to remove records
254 * originating from connection with key[1] in_phone_hash
255 * value. Note that this is close to being classified
258 * @param key Array of keys.
259 * @param keys Must be lesser or equal to 3.
260 * @param item Pointer to a hash table item.
261 * @return Non-zero if the key matches the item, zero otherwise.
263 int ns_compare(unsigned long key
[], hash_count_t keys
, link_t
*item
)
265 hashed_service_t
*hs
;
271 hs
= hash_table_get_instance(item
, hashed_service_t
, link
);
274 return key
[1] == hs
->in_phone_hash
;
276 return key
[0] == hs
->service
;
279 /** Perform actions after removal of item from the hash table.
281 * @param item Item that was removed from the hash table.
283 void ns_remove(link_t
*item
)
286 free(hash_table_get_instance(item
, hashed_service_t
, link
));