]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ferret/lib/consumer/monitor.cc
update
[l4.git] / l4 / pkg / ferret / lib / consumer / monitor.cc
1 /**
2  * \file   ferret/lib/monitor/monitor.cc
3  * \brief  Functions to access sensors from monitors.
4  *
5  * \date   10/03/2009
6  * \author Martin Pohlack <mp26@os.inf.tu-dresden.de>
7  * \author Bjoern Doebel <doebel@tudos.org>
8  */
9 /*
10  * (c) 2009 Technische Universität Dresden
11  * This file is part of TUD:OS and distributed under the terms of the
12  * GNU General Public License 2.
13  * Please see the COPYING-GPL-2 file for details.
14  */
15 #include <l4/ferret/types.h>
16
17 #include <stdlib.h>
18 #include <iostream>    // std::cout
19 #include <l4/cxx/ipc_stream>    // L4::Ipc::Iostream
20 #include <l4/re/util/cap_alloc> // L4::Cap
21 #include <l4/re/dataspace>      // L4Re::Dataspace
22 #include <l4/re/mem_alloc>      // mem_alloc()
23 #include <l4/re/rm>             // L4::Rm
24 #include <l4/re/env>            // L4::Env
25
26
27 //#include <l4/ferret/clock.h>
28 #include <l4/ferret/comm.h>
29 #include <l4/ferret/monitor.h>
30 #include <l4/ferret/monitor_list.h>
31 #include <l4/ferret/sensordir.h>
32
33 #include <l4/ferret/sensors/common.h>
34 #include <l4/ferret/sensors/list_consumer.h>
35 //#include <l4/ferret/sensors/dplist_consumer.h>
36 //#include <l4/ferret/sensors/slist_consumer.h>
37 //#include <l4/ferret/sensors/ulist_consumer.h>
38 //#include <l4/ferret/sensors/tbuf.h>
39 //#include <l4/ferret/sensors/tbuf_consumer.h>
40
41 int ferret_attach(l4_cap_idx_t srv,
42                   uint16_t major, uint16_t minor,
43                   uint16_t instance, void ** addr)
44 {
45         /*
46          * Locally alloc a cap for the new DS
47          */
48     L4::Cap<L4Re::Dataspace> ds = L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>();
49
50         L4::Ipc::Iostream s(l4_utcb());
51         s << l4_umword_t(Attach);
52         s << major << minor << instance;
53         s << L4::Ipc::Small_buf(ds.cap(), 0);
54
55         l4_msgtag_t res = s.call(srv, /* Protocol = */ Monitor);
56         if (l4_ipc_error(res, l4_utcb()))
57         {
58                 std::cout << "IPC error in " << __func__ << "()\n";
59                 return -1;
60         }
61
62         /*
63          * unpack return val
64          */
65         int ret, err;
66         s >> ret;
67
68         /*
69          * Post-processing
70          */
71         if (ret == 0)
72         {
73 #if DEBUG
74                 std::cout << "Successfully attached. Got DS.\n";
75 #endif
76                 if (!ds.is_valid())
77                 {
78                         std::cout << "No DS mapped during attach.\n";
79                         return -1;
80                 }
81
82                 err =  L4Re::Env::env()->rm()->attach(addr, ds->size(),
83                                                       L4Re::Rm::Search_addr,
84                                                       ds);
85                 if (err < 0)
86                 {
87                         std::cout << "Could not attach DS in " << __func__ << "\n";
88                         std::cout << "Error: " <<  l4sys_errtostr(err) << "\n";
89                         return -1;
90                 }
91
92                 /*
93                  * Sensor-specific processing
94                  */
95                 switch(((ferret_common_t*)*addr)->type)
96                 {
97                         case FERRET_LIST:
98                                 ferret_list_init_consumer(addr);
99                                 break;
100                         case FERRET_SCALAR:
101                                 // fall through
102                         default:
103                                 break;
104                 }
105         }
106         else
107         {
108                 std::cout << "Failure attaching to sensor " << ret << "\n";
109         }
110
111         return ret;
112 }
113
114 int ferret_detach(l4_cap_idx_t srv,
115                   uint16_t major, uint16_t minor,
116                   uint16_t instance, void ** addr)
117 {
118         L4::Ipc::Iostream s(l4_utcb());
119         s << l4_umword_t(Detach);
120         s << major << minor << instance;
121
122         l4_msgtag_t res = s.call(srv, /* Protocol = */ Monitor);
123         if (l4_ipc_error(res, l4_utcb()))
124         {
125                 std::cout << "IPC error in " << __func__ << "()\n";
126                 return -1;
127         }
128
129         /*
130          * unpack return val
131          */
132         int ret;
133         s >> ret;
134
135         if (ret == 0)
136         {
137                 /*
138                  * Sensor-specific processing
139                  */
140                 switch(((ferret_common_t*)*addr)->type)
141                 {
142                         case FERRET_SCALAR:
143                                 // fall through
144                         default:
145                                 break;
146                 }
147
148                 // detach dataspace
149                 L4::Cap<L4Re::Dataspace> ds(((ferret_common_t*)*addr)->ds_cap);
150
151                 int err =  L4Re::Env::env()->rm()->detach(*addr, 0);
152 #if DEBUG
153                 std::cout << "detach: " << err << "\n";
154 #endif
155                 if (err == L4Re::Rm::Detached_ds)
156                 {
157                         l4_msgtag_t res = l4_task_unmap(L4RE_THIS_TASK_CAP,
158                                                         ds.fpage(), L4_FP_ALL_SPACES);
159                         if (l4_error(res))
160                         {
161                                 std::cout << "unmap error: " << l4_error(res) << "\n";
162                                 ret = -1;
163                         }
164                 }
165                 else
166                 {
167                         std::cout << "Error detaching sensor ds.\n";
168                         ret = -1;
169                 }
170         }
171         else
172         {
173                 std::cout << "Failure detaching sensor: " << ret << "\n";
174         }
175
176         return ret;
177 }
178
179 int ferret_list(l4_cap_idx_t srv __attribute__((unused)),
180                 ferret_monitor_list_entry_t ** entries __attribute__((unused)),
181                 int count __attribute__((unused)),
182                 int offset __attribute__((unused)))
183 {
184 #if 0
185     int ret;
186     CORBA_Environment _dice_corba_env = dice_default_environment;
187     _dice_corba_env.malloc = (dice_malloc_func)malloc;
188     _dice_corba_env.free = (dice_free_func)free;
189
190     // get sensor_dir task id lazily
191     if (l4_is_invalid_id(ferret_sensor_dir))
192         ferret_sensor_dir = ferret_comm_get_threadid();
193
194     ret = ferret_monitor_list_call(&ferret_sensor_dir, entries, &count, offset,
195                                     &_dice_corba_env);
196     if (ret < 0)
197         return ret;
198     else
199         return count;
200 #endif
201         return -1;
202 }