]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - src_frescan/frescan_servers.c
eb31bbe511e59e2441e53ca50b67d106e5c5e0e1
[frescor/fna.git] / src_frescan / frescan_servers.c
1 /*!
2  * @file frescan_servers.c
3  *
4  * @brief FRESCAN sporadic servers
5  *
6  * @version 0.01
7  *
8  * @date 27-Feb-2008
9  *
10  * @author
11  *      Daniel Sangorrin
12  *
13  * @comments
14  *
15  * This file contains the FRESCAN sporadic servers that allow to isolate
16  * different streams of data by assigning them a budget and replenishment
17  * period.
18  *
19  * @license
20  *
21  * See MaRTE OS license
22  *
23  */
24
25 #include "frescan_servers.h"
26 #include "frescan_debug.h"
27 #include "frescan_data.h"
28 #include <misc/linux_list.h>
29 #include <signal.h>
30
31 /**
32  * frescan_servers_init() - initialize server structures
33  *
34  * @net: the network instance
35  */
36
37 int frescan_servers_init(frescan_network_t net)
38 {
39         int ret, i;
40
41         DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "initializing servers\n");
42
43         ret = freelist_init(&the_servers_pool_freelist[net], FRESCAN_MX_IDS);
44         if (ret != 0) return ret;
45
46         for (i=0; i<FRESCAN_MX_NETWORKS; i++) {
47                 INIT_LIST_HEAD(&the_active_servers[net].servers_list);
48         }
49
50         return 0;
51 }
52
53 /**
54  * frescan_servers_create() - create a sporadic server
55  *
56  * @net: the network instance
57  * @params: the parameters for the server
58  * @id: the identificator for the server as a return value
59  *
60  */
61
62 int frescan_servers_create(frescan_network_t net,
63                            const frescan_server_params_t *params,
64                            frescan_ss_t *id)
65 {
66         int ret, pos;
67         struct sigevent evp;
68
69         pos = freelist_alloc(&the_servers_pool_freelist[net]);
70         if (pos == -1) {
71                 ERROR("could not allocate servers\n");
72                 return -1;
73         }
74
75         *id = (frescan_ss_t)pos;
76
77         the_servers_pool[net][*id].net = net;
78         the_servers_pool[net][*id].id  = *id;
79         the_servers_pool[net][*id].params = *params;
80         the_servers_pool[net][*id].current_budget = params->values.budget;
81         the_servers_pool[net][*id].current_priority = params->prio;
82         the_servers_pool[net][*id].pending_packets = 0;
83
84         INIT_LIST_HEAD(&the_servers_pool[net][*id].replenishments.repl_list);
85         INIT_LIST_HEAD(&the_servers_pool[net][*id].packet_list.fifo_list);
86
87         // the repl timer sends a signal when it expires with the server id
88         evp.sigev_notify = SIGEV_SIGNAL;
89         evp.sigev_signo  = FRESCAN_REPL_SIGNAL_NUM;
90         evp.sigev_value.sival_int  = (int)*id;
91
92         ret = timer_create (CLOCK_MONOTONIC,
93                             &evp, &the_servers_pool[net][*id].repl_timer);
94         if (ret != 0) {
95                 ERROR("could not create timer\n");
96                 return ret;
97         }
98
99         DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
100               "server created, id:%u budget:%u prio:%u\n",
101               *id,
102               the_servers_pool[net][*id].params.values.budget,
103               the_servers_pool[net][*id].params.prio);
104
105         return 0;
106 }
107
108 /**
109  * frescan_servers_update() - update a sporadic server data
110  *
111  * @net: the network instance
112  * @params: the parameters for the server
113  * @id: the identificator for the server
114  *
115  */
116
117 int frescan_servers_update(frescan_network_t net,
118                            const frescan_server_params_t *params,
119                            frescan_ss_t id)
120 {
121         the_servers_pool[net][id].params = *params;
122         return 0;
123 }
124
125 /**
126  * frescan_servers_destroy() - delete a sporadic server
127  *
128  * @net: the network instance
129  * @id: the identificator for the server
130  *
131  */
132
133 int frescan_servers_destroy(frescan_network_t net, frescan_ss_t id)
134 {
135         int ret;
136
137         // TODO: free the replenishment operations and the packets for the
138         // server.
139
140         ret = timer_delete (the_servers_pool[net][id].repl_timer);
141         if (ret != 0) {
142                 ERROR("could not delete timer\n");
143                 return ret;
144         }
145
146         list_del(&the_servers_pool[net][id].servers_list);
147
148         ret = freelist_free(&the_servers_pool_freelist[net], id);
149         if (ret != 0) {
150                 ERROR("could not free server data from pool\n");
151                 return ret;
152         }
153
154         return 0;
155 }
156
157 /**
158  * frescan_servers_get_data() - get a sporadic server data
159  *
160  * @net: the network instance
161  * @params: the parameters of the server
162  * @id: the identificator for the server
163  *
164  */
165
166 int frescan_servers_get_data(frescan_network_t net,
167                              frescan_server_params_t *params,
168                              frescan_ss_t id)
169 {
170         *params = the_servers_pool[net][id].params;
171         return 0;
172 }
173
174 /**
175  * frescan_servers_get_current_budget() - get the current sporadic server budget
176  *
177  * @net: the network instance
178  * @id: the identificator for the server
179  * @current_budget: the current budget of the server
180  *
181  */
182
183 int frescan_servers_get_current_budget(frescan_network_t net,
184                                        frescan_ss_t id,
185                                        frescan_budget_t *current_budget)
186 {
187         *current_budget = the_servers_pool[net][id].current_budget;
188         return 0;
189 }
190
191 /**
192  * frescan_servers_get_highest_prio() - get the server with highest priority
193  *
194  * @net: the network instance
195  * @id: the identificator for the server
196  * @prio: the priority of that server
197  *
198  * For each active server, check the priority.
199  * If "id" is returned with a value of FRESCAN_MX_IDS,
200  * there are no active servers.
201  * NOTE: id=FRESCAN_MX_IDS is the identifier for fixed priority messages
202  * TODO: use a priority queue of active servers
203  *
204  */
205
206 int frescan_servers_get_highest_prio(frescan_network_t net,
207                                      frescan_ss_t *id,
208                                      frescan_prio_t *prio)
209 {
210         frescan_server_data_t *server;
211
212         if (list_empty(&the_active_servers->servers_list)) {
213                 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "server list is empty\n");
214                 *id = FRESCAN_MX_IDS;
215                 return 0;
216         }
217
218         *prio = 0;
219         list_for_each_entry(server, &the_active_servers[net].servers_list,
220                             servers_list) {
221                 if (server->current_priority >= *prio) {
222                         *id = server->id;
223                         *prio = server->current_priority;
224                 }
225         }
226
227         DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
228               "highest prio:%u id:%u\n", *prio, *id);
229
230         return 0;
231 }
232
233 /**
234  * frescan_servers_frame_sent() - hook to control the server budget and prio
235  *
236  * @net: the network instance
237  * @id: the identificator for the server
238  *
239  * This function is called when a frame has been effectively sent through the
240  * CAN bus and that frame is associated to a certain server. The function
241  * decreases the capacity of the server and sets the priority to background
242  * in case the budget is exhausted.
243  *
244  * NOTE: the replenishment operation is programmed using the corresponding
245  * function at frescan_servers_replenishments module
246  */
247
248 int frescan_servers_frame_sent(frescan_network_t net, frescan_ss_t id)
249 {
250         DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
251               "id:%u, current_budget:%u, current_priority:%u\n",
252               id,
253               the_servers_pool[net][id].current_budget,
254               the_servers_pool[net][id].current_priority);
255
256         if (the_servers_pool[net][id].current_budget > 0) {
257                 the_servers_pool[net][id].current_budget--;
258                 if (the_servers_pool[net][id].current_budget == 0) {
259                         the_servers_pool[net][id].current_priority =
260                                         FRESCAN_BACKGROUND_PRIO;
261                 }
262         }
263
264         DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
265               "now... id:%u, current_budget:%u, current_priority:%u\n",
266               id,
267               the_servers_pool[net][id].current_budget,
268               the_servers_pool[net][id].current_priority);
269
270         return 0;
271 }