]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - resources/item/frm_item.c
04eb850979f6eb9335f9484963dfc98e542f5422
[frescor/frsh.git] / resources / item / frm_item.c
1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                 */
4 /*                                                                        */
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                */
11 /*   ENEA                                   SWEDEN                        */
12 /*   Thales Communication S.A.              FRANCE                        */
13 /*   Visual Tools S.A.                      SPAIN                         */
14 /*   Rapita Systems Ltd                     UK                            */
15 /*   Evidence                               ITALY                         */
16 /*                                                                        */
17 /*   See http://www.frescor.org for a link to partners' websites          */
18 /*                                                                        */
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.                                               */
23 /*                                                                        */
24 /*                                                                        */
25 /*  This file is part of FRSH (FRescor ScHeduler)                         */
26 /*                                                                        */
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.                                              */
37 /*                                                                        */
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 /**************************************************************************/
46
47 /**
48  * @file   frm_item.c
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
52  * 
53  * @brief  Resource manager for ITEM protocol
54  * 
55  * 
56  */
57 #include <frm_generic.h>
58 #include <forb.h>
59 #include <error.h>
60 #include <errno.h>
61 #include <fres_sa_scenario.h>
62 #include <stdbool.h>
63 #include <ul_log.h>
64 #include <stdio.h>
65 #include <frsh_distributed.h>
66 #include <item.h>
67 #include "FWSN.h"
68
69 static inline bool node_present(fres_item_node_mask nodes, int node)
70 {
71         return (nodes & (1LL<<node)) != 0;
72 }
73
74 static inline void node_add(fres_item_node_mask *nodes, int node)
75 {
76         *nodes |= (1LL<<node);
77 }
78
79 static inline void node_del(fres_item_node_mask *nodes, int node)
80 {
81         *nodes &= (~(1LL<<node));
82 }
83
84 static int
85 admission_test(struct fres_sa_scenario *scenario, void *priv, bool *schedulable)
86 {
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);
90
91         
92         /* Find communicating nodes and the most restricting deadline */
93         all_nodes = 0;
94         fres_sa_scenario_for_each_no_cancel_contract(scenario, c) {
95                 fres_block_item_nodes *nodes;
96                 fosa_rel_time_t deadline;
97
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;
101                 
102
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;
107                         }
108                 }
109         }
110         
111                 
112         if (!fosa_rel_time_is_null(min_deadline)) {             
113                 /* Compare number of nodes and min_deadline */
114                 //ITEM ADD
115                 int i;
116                 int count_nodes;
117                 int free_slots;
118                 fres_item_node_mask counting_var;
119                 
120                 counting_var =1;
121                 count_nodes = 0;
122                 *schedulable = true;
123                 
124                 for (i = 0; i <= sizeof(all_nodes)-1; i++) //FIXME Number of bits
125                 {
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;
130                 }
131                 
132                 free_slots = noOfFreeSlots(fosa_rel_time_to_msec(min_deadline));
133                 
134                 if (free_slots < 0) 
135                 {       
136                         *schedulable = false; // adding of new nodes in action or the actual period is higher then the minimum demanded one.
137                 }
138                 else
139                 {       
140                         if (free_slots < count_nodes)
141                                 *schedulable = false;   // too much nodes are demanded
142                 }               
143                 
144         } 
145         else {
146                 *schedulable = true;
147         }
148         
149         return 0;
150 }
151
152 struct item_status {
153         /** Specifies how many applications want to read from a which
154          * node */
155         int node_requested[64];
156 };
157
158 CORBA_long
159 add_nodes(fres_item_scheduler obj, const fres_item_node_mask mask, CORBA_Environment *ev)
160 {
161         int node;
162         struct item_status *item_status = (struct item_status*)forb_instance_data(obj);
163         
164         for (node=1; node<64; node++) {
165                 if (node_present(mask, node)) {
166                         if (item_status->node_requested[node] == 0)
167                         {
168                                 /* Add node */
169                                 addDevice(node);        //ITEM ADD
170                         }
171                         item_status->node_requested[node]++;
172                 }
173         }
174         return  0;
175 }
176
177 CORBA_long
178 del_nodes(fres_item_scheduler obj, const fres_item_node_mask mask, CORBA_Environment *ev)
179 {
180         struct item_status *item_status = (struct item_status*)forb_instance_data(obj);
181         int node;
182
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)
187                         {
188                                 /* Remove node */
189                                 removeDevice(node);     //ITEM ADD
190                         }
191                 }
192         }
193         return 0;
194 }
195
196 CORBA_long
197 get_data(fres_item_scheduler obj, const fres_item_node_mask mask, fres_item_data_seq** data, CORBA_Environment *ev)
198 {
199         unsigned max = 256;
200         //struct item_status *item_status = (struct item_status*)forb_instance_data(obj);
201         fres_item_data_seq *d = forb_malloc(sizeof(*d));
202
203         d->_buffer = CORBA_sequence_octet_allocbuf(max); //FIXME: CORBA_sequence_fres_item_data_seq_allocbuf(max);
204         d->_maximum = max;
205         d->_length = getData(d->_buffer, d->_maximum);
206
207         *data = d;
208         
209         return 0;
210 }
211
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,
216         .priv = NULL,
217 };
218
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,
223 };
224
225 struct item_status item_status;
226
227 int forb_main(forb_orb orb, int argc, char *argv[])
228 {
229         int ret;
230         forb_executor_t executor;
231         fres_resource_manager frm;
232         fres_item_scheduler fis;
233         struct frm_data frm_data;
234         
235         //ITEM ADD
236         pthread_t threadRead;   
237         char device[] ="/dev/ttyUSB0";
238         char platform[]="telosb";       
239         char *parametrsITEM[] ={NULL,device,platform};
240
241         forb_init_attr_t attr = { .orb_id = "org.frescor.frm.item" };
242
243         fres_block_register_item();
244
245         
246         ret = forb_executor_init(&executor);
247         if (ret) error(1, errno, "forb_executor_init failed");
248         
249
250         frm = frm_register(orb, &frm_data, &executor, &frm_item_desc);
251         if (!frm) {
252                 error(1, errno, "frm_register failed");
253         }
254
255
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");
258
259
260         ret = forb_register_reference(fis, "item_scheduler");
261         if (ret) error (1, errno, "Cannot register ITEM scheduler");
262
263
264         forb_executor_register_object(&executor, fis);
265
266
267         // ITEM ADD     
268         threadRead = init(parametrsITEM);
269         //if (threadRead == NULL) error (1, errno, "Cannot run the ITEM PC Manager");   //TODO fix the worning
270         
271         
272         forb_executor_run(&executor);
273         
274         
275         
276         return 0;
277 }