1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners: */
5 /* Universidad de Cantabria, SPAIN */
6 /* University of York, UK */
7 /* Scuola Superiore Sant'Anna, ITALY */
8 /* Kaiserslautern University, GERMANY */
9 /* Univ. Politécnica Valencia, SPAIN */
10 /* Czech Technical University in Prague, CZECH REPUBLIC */
12 /* Thales Communication S.A. FRANCE */
13 /* Visual Tools S.A. SPAIN */
14 /* Rapita Systems Ltd UK */
17 /* See http://www.frescor.org for a link to partners' websites */
19 /* FRESCOR project (FP6/2005/IST/5-034026) is funded */
20 /* in part by the European Union Sixth Framework Programme */
21 /* The European Union is not liable of any use that may be */
22 /* made of this code. */
25 /* This file is part of FRSH (FRescor ScHeduler) */
27 /* FRSH is free software; you can redistribute it and/or modify it */
28 /* under terms of the GNU General Public License as published by the */
29 /* Free Software Foundation; either version 2, or (at your option) any */
30 /* later version. FRSH is distributed in the hope that it will be */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
33 /* General Public License for more details. You should have received a */
34 /* copy of the GNU General Public License along with FRSH; see file */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, */
36 /* Cambridge, MA 02139, USA. */
38 /* As a special exception, including FRSH header files in a file, */
39 /* instantiating FRSH generics or templates, or linking other files */
40 /* with FRSH objects to produce an executable application, does not */
41 /* by itself cause the resulting executable application to be covered */
42 /* by the GNU General Public License. This exception does not */
43 /* however invalidate any other reasons why the executable file might be */
44 /* covered by the GNU Public License. */
45 /**************************************************************************/
49 * @author Michal Sojka <sojkam1@fel.cvut.cz>
50 * @author Jiri Trdlicka <trdlij1@fel.cvut.cz>
51 * @date Thu Nov 6 17:21:48 2008
53 * @brief Resource manager for ITEM protocol
57 #include <frm_generic.h>
61 #include <fres_sa_scenario.h>
65 #include <frsh_distributed.h>
69 static inline bool node_present(fres_item_node_mask nodes, int node)
71 return (nodes & (1LL<<node)) != 0;
74 static inline void node_add(fres_item_node_mask *nodes, int node)
76 *nodes |= (1LL<<node);
79 static inline void node_del(fres_item_node_mask *nodes, int node)
81 *nodes &= (~(1LL<<node));
85 admission_test(struct fres_sa_scenario *scenario, void *priv, bool *schedulable)
87 struct fres_sa_contract *c;
88 fres_item_node_mask all_nodes;
89 frsh_rel_time_t min_deadline = fosa_msec_to_rel_time(0);
92 /* Find communicating nodes and the most restricting deadline */
94 fres_sa_scenario_for_each_no_cancel_contract(scenario, c) {
95 fres_block_item_nodes *nodes;
96 fosa_rel_time_t deadline;
98 nodes = fres_contract_get_item_nodes(c->contract);
99 if (!nodes) return FRES_ERR_NEEDS_MORE_DATA_IN_CONTRACT;
100 all_nodes |= nodes->mask;
103 if (fres_contract_get_deadline(&c->contract, &deadline)) {
104 if (fosa_rel_time_is_null(min_deadline) ||
105 fosa_rel_time_smaller(deadline, min_deadline)) {
106 min_deadline = deadline;
112 if (!fosa_rel_time_is_null(min_deadline)) {
113 /* Compare number of nodes and min_deadline */
118 fres_item_node_mask counting_var;
124 for (i = 0; i <= sizeof(all_nodes)-1; i++) //FIXME Number of bits
126 //printf("JOP 5 - %i\n",aa);
127 if ((all_nodes & counting_var) > 0)
128 count_nodes = count_nodes + 1;
129 counting_var = counting_var<<1;
132 free_slots = noOfFreeSlots(fosa_rel_time_to_msec(min_deadline));
136 *schedulable = false; // adding of new nodes in action or the actual period is higher then the minimum demanded one.
140 if (free_slots < count_nodes)
141 *schedulable = false; // too much nodes are demanded
153 /** Specifies how many applications want to read from a which
155 int node_requested[64];
159 add_nodes(fres_item_scheduler obj, const fres_item_node_mask mask, CORBA_Environment *ev)
162 struct item_status *item_status = (struct item_status*)forb_instance_data(obj);
164 for (node=1; node<64; node++) {
165 if (node_present(mask, node)) {
166 if (item_status->node_requested[node] == 0)
169 addDevice(node); //ITEM ADD
171 item_status->node_requested[node]++;
178 del_nodes(fres_item_scheduler obj, const fres_item_node_mask mask, CORBA_Environment *ev)
180 struct item_status *item_status = (struct item_status*)forb_instance_data(obj);
183 for (node=1; node<64; node++) {
184 if (node_present(mask, node)) {
185 item_status->node_requested[node]--;
186 if (item_status->node_requested[node] == 0)
189 removeDevice(node); //ITEM ADD
197 get_data(fres_item_scheduler obj, const fres_item_node_mask mask, fres_item_data_seq** data, CORBA_Environment *ev)
200 //struct item_status *item_status = (struct item_status*)forb_instance_data(obj);
201 fres_item_data_seq *d = forb_malloc(sizeof(*d));
203 d->_buffer = CORBA_sequence_octet_allocbuf(max); //FIXME: CORBA_sequence_fres_item_data_seq_allocbuf(max);
205 d->_length = getData(d->_buffer, d->_maximum);
212 static const struct fres_res_manager frm_item_desc = {
213 .res_type = FRSH_RT_NETWORK,
214 .res_id = FRSH_NETPF_ITEM,
215 .admission_test = admission_test,
219 static const struct forb_fres_item_scheduler_impl item_scheduler_impl = {
220 .add_nodes = add_nodes,
221 .del_nodes = del_nodes,
222 .get_data = get_data,
225 struct item_status item_status;
227 int forb_main(forb_orb orb, int argc, char *argv[])
230 forb_executor_t executor;
231 fres_resource_manager frm;
232 fres_item_scheduler fis;
233 struct frm_data frm_data;
236 pthread_t threadRead;
237 char device[] ="/dev/ttyUSB0";
238 char platform[]="telosb";
239 char *parametrsITEM[] ={NULL,device,platform};
241 forb_init_attr_t attr = { .orb_id = "org.frescor.frm.item" };
243 fres_block_register_item();
246 ret = forb_executor_init(&executor);
247 if (ret) error(1, errno, "forb_executor_init failed");
250 frm = frm_register(orb, &frm_data, &executor, &frm_item_desc);
252 error(1, errno, "frm_register failed");
256 fis = forb_fres_item_scheduler_new(orb, &item_scheduler_impl, &item_status);
257 if (!fis) error(1, errno, "forb_fres_item_scheduler_new failed");
260 ret = forb_register_reference(fis, "item_scheduler");
261 if (ret) error (1, errno, "Cannot register ITEM scheduler");
264 forb_executor_register_object(&executor, fis);
268 threadRead = init(parametrsITEM);
269 //if (threadRead == NULL) error (1, errno, "Cannot run the ITEM PC Manager"); //TODO fix the worning
272 forb_executor_run(&executor);