2 * (c) 2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3 * Alexander Warg <warg@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
6 * This file is part of TUD:OS and distributed under the terms of the
7 * GNU General Public License 2.
8 * Please see the COPYING-GPL-2 file for details.
12 #include <l4/re/namespace>
13 #include <l4/re/util/cap_alloc>
14 #include <l4/re/util/object_registry>
15 #include <l4/re/dataspace>
16 #include <l4/cxx/ipc_server>
18 #include <l4/sys/typeinfo_svr>
24 #include "interface.h"
27 * A very simple server object, just providing the
28 * shared memory data space and an IRQ object to send a notification.
30 class My_server_obj : public L4::Server_object
34 * The capability to the shared memory.
36 L4::Cap<L4Re::Dataspace> _shm;
37 L4::Cap<L4::Irq> _irq;
41 * Create a new object for the given data space.
43 explicit My_server_obj(L4::Cap<L4Re::Dataspace> shm, L4::Cap<L4::Irq> irq)
44 : _shm(shm), _irq(irq)
48 * Dispatch function, dealing with remote requests.
50 int dispatch(l4_umword_t obj, L4::Ipc::Iostream &ios);
54 int My_server_obj::dispatch(l4_umword_t obj, L4::Ipc::Iostream &ios)
56 // we don't care about the original object reference, however
57 // we could read out the access rights from the lowest 2 bits
61 ios >> t; // extract the tag
65 case L4::Meta::Protocol:
66 // handle the meta protocol requests, implementing the
67 // runtime dynamic type system for L4 objects.
68 return L4::Util::handle_meta_request<My_interface>(ios);
70 // since we have just one operation we have no opcode dispatch,
71 // and just return the data-space and the notifier IRQ capabilities
75 // every other protocol is not supported.
82 * A simple Server object attached to a notifier IRQ.
83 * This provides a kind of interrupt handler integrated in our
86 class Shm_observer : public L4::Server_object
90 * The pointer to the shared memory.
96 * Create a new object for the given shared memory.
98 explicit Shm_observer(char *shm)
103 * Dispatch function, dealing with remote requests.
106 int dispatch(l4_umword_t obj, L4::Ipc::Iostream &ios);
109 int Shm_observer::dispatch(l4_umword_t obj, L4::Ipc::Iostream &ios)
111 // we don't care about the original object reference, however
112 // we could read out the access rights from the lowest 2 bits
116 ios >> t; // extract the tag
120 case L4::Irq::Protocol:
121 // Got an IRQ so just print the new contents of the
123 printf("Content: %s\n", _shm);
126 // every other protocol is not supported.
127 return -L4_EBADPROTO;
132 * The singleton for implementing the generic server logic for the
133 * main thread. The factory is used for creating IPC gates for new server
134 * objects, however in this example no IPC gate is created, instead an IPC gate
135 * provided by the parent is used.
137 static L4Re::Util::Registry_server<> server;
144 static char *get_ds(L4::Cap<L4Re::Dataspace> *_ds)
146 *_ds = L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>();
147 if (!(*_ds).is_valid())
149 printf("Dataspace allocation failed.\n");
153 int err = L4Re::Env::env()->mem_alloc()->alloc(DS_SIZE, *_ds, 0);
156 printf("mem_alloc->alloc() failed.\n");
157 L4Re::Util::cap_alloc.free(*_ds);
162 * Attach DS to local address space
165 err = L4Re::Env::env()->rm()->attach(&_addr, (*_ds)->size(),
166 L4Re::Rm::Search_addr,
170 printf("Error attaching data space: %s\n", l4sys_errtostr(err));
171 L4Re::Util::cap_alloc.free(*_ds);
177 * Success! Write something to DS.
179 printf("Attached DS\n");
180 static char const * const msg = "[DS] Hello from server!";
181 snprintf(_addr, strlen(msg) + 1, msg);
188 L4::Cap<L4Re::Dataspace> ds;
191 if (!(addr = get_ds(&ds)))
195 // first the IRQ handler, because we need it in the My_server_obj object
196 Shm_observer observer(addr);
198 // Registering the observer as an IRQ handler, this allocates an
199 // IRQ object using the factory of our server.
200 L4::Cap<L4::Irq> irq = server.registry()->register_irq_obj(&observer);
202 // now the initial server object shared with the client via our parent.
203 // it provides the data-space and the IRQ capabilies to a client.
204 My_server_obj server_obj(ds, irq);
206 // Registering the server object to the capability 'shm' in our the L4Re::Env.
207 // This capability must be provided by the parent. (see the shared_ds.lua)
208 server.registry()->register_obj(&server_obj, "shm");
210 // Run our server loop.