]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - src_frescan/frescan_servers.c
locks for server functions
[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         FRESCAN_ACQUIRE_LOCK(&the_networks[net].lock);
70         pos = freelist_alloc(&the_servers_pool_freelist[net]);
71         FRESCAN_RELEASE_LOCK(&the_networks[net].lock);
72
73         if (pos == -1) {
74                 ERROR("could not allocate servers\n");
75                 return -1;
76         }
77
78         *id = (frescan_ss_t)pos;
79
80         the_servers_pool[net][*id].net = net;
81         the_servers_pool[net][*id].id  = *id;
82         the_servers_pool[net][*id].params = *params;
83         the_servers_pool[net][*id].current_budget = params->values.budget;
84         the_servers_pool[net][*id].current_priority = params->prio;
85         the_servers_pool[net][*id].pending_packets = 0;
86
87         INIT_LIST_HEAD(&the_servers_pool[net][*id].replenishments.repl_list);
88         INIT_LIST_HEAD(&the_servers_pool[net][*id].packet_list.fifo_list);
89
90         // the repl timer sends a signal when it expires with the server id
91         evp.sigev_notify = SIGEV_SIGNAL;
92         evp.sigev_signo  = FRESCAN_REPL_SIGNAL_NUM;
93         evp.sigev_value.sival_int  = (int)*id;
94
95         ret = timer_create (CLOCK_MONOTONIC,
96                             &evp, &the_servers_pool[net][*id].repl_timer);
97         if (ret != 0) {
98                 ERROR("could not create timer\n");
99                 return ret;
100         }
101
102         DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
103               "server created, id:%u budget:%u prio:%u\n",
104               *id,
105               the_servers_pool[net][*id].params.values.budget,
106               the_servers_pool[net][*id].params.prio);
107
108         return 0;
109 }
110
111 /**
112  * frescan_servers_update() - update a sporadic server data
113  *
114  * @net: the network instance
115  * @params: the parameters for the server
116  * @id: the identificator for the server
117  *
118  */
119
120 int frescan_servers_update(frescan_network_t net,
121                            const frescan_server_params_t *params,
122                            frescan_ss_t id)
123 {
124         the_servers_pool[net][id].params = *params;
125         return 0;
126 }
127
128 /**
129  * frescan_servers_destroy() - delete a sporadic server
130  *
131  * @net: the network instance
132  * @id: the identificator for the server
133  *
134  */
135
136 int frescan_servers_destroy(frescan_network_t net, frescan_ss_t id)
137 {
138         int ret;
139
140         // TODO: free the replenishment operations and the packets for the
141         // server.
142
143         ret = timer_delete (the_servers_pool[net][id].repl_timer);
144         if (ret != 0) {
145                 ERROR("could not delete timer\n");
146                 return ret;
147         }
148
149         FRESCAN_ACQUIRE_LOCK(&the_networks[net].lock);
150         list_del(&the_servers_pool[net][id].servers_list);
151
152         ret = freelist_free(&the_servers_pool_freelist[net], id);
153         FRESCAN_RELEASE_LOCK(&the_networks[net].lock);
154         if (ret != 0) {
155                 ERROR("could not free server data from pool\n");
156                 return ret;
157         }
158
159         return 0;
160 }
161
162 /**
163  * frescan_servers_get_data() - get a sporadic server data
164  *
165  * @net: the network instance
166  * @params: the parameters of the server
167  * @id: the identificator for the server
168  *
169  */
170
171 int frescan_servers_get_data(frescan_network_t net,
172                              frescan_server_params_t *params,
173                              frescan_ss_t id)
174 {
175         *params = the_servers_pool[net][id].params;
176         return 0;
177 }
178
179 /**
180  * frescan_servers_get_current_budget() - get the current sporadic server budget
181  *
182  * @net: the network instance
183  * @id: the identificator for the server
184  * @current_budget: the current budget of the server
185  *
186  */
187
188 int frescan_servers_get_current_budget(frescan_network_t net,
189                                        frescan_ss_t id,
190                                        frescan_budget_t *current_budget)
191 {
192         *current_budget = the_servers_pool[net][id].current_budget;
193         return 0;
194 }
195
196 /**
197  * frescan_servers_get_highest_prio() - get the server with highest priority
198  *
199  * @net: the network instance
200  * @id: the identificator for the server
201  * @prio: the priority of that server
202  *
203  * For each active server, check the priority.
204  * If "id" is returned with a value of FRESCAN_MX_IDS,
205  * there are no active servers.
206  * NOTE: id=FRESCAN_MX_IDS is the identifier for fixed priority messages
207  * TODO: use a priority queue of active servers
208  *
209  */
210
211 int frescan_servers_get_highest_prio(frescan_network_t net,
212                                      frescan_ss_t *id,
213                                      frescan_prio_t *prio)
214 {
215         frescan_server_data_t *server;
216
217         if (list_empty(&the_active_servers->servers_list)) {
218                 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "server list is empty\n");
219                 *id = FRESCAN_MX_IDS;
220                 return 0;
221         }
222
223         *prio = 0;
224         list_for_each_entry(server, &the_active_servers[net].servers_list,
225                             servers_list) {
226                 if (server->current_priority >= *prio) {
227                         *id = server->id;
228                         *prio = server->current_priority;
229                 }
230         }
231
232         DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
233               "highest prio:%u id:%u\n", *prio, *id);
234
235         return 0;
236 }
237
238 /**
239  * frescan_servers_frame_sent() - hook to control the server budget and prio
240  *
241  * @net: the network instance
242  * @id: the identificator for the server
243  *
244  * This function is called when a frame has been effectively sent through the
245  * CAN bus and that frame is associated to a certain server. The function
246  * decreases the capacity of the server and sets the priority to background
247  * in case the budget is exhausted.
248  *
249  * NOTE: the replenishment operation is programmed using the corresponding
250  * function at frescan_servers_replenishments module
251  */
252
253 int frescan_servers_frame_sent(frescan_network_t net, frescan_ss_t id)
254 {
255         DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
256               "id:%u, current_budget:%u, current_priority:%u\n",
257               id,
258               the_servers_pool[net][id].current_budget,
259               the_servers_pool[net][id].current_priority);
260
261         if (the_servers_pool[net][id].current_budget > 0) {
262                 the_servers_pool[net][id].current_budget--;
263                 if (the_servers_pool[net][id].current_budget == 0) {
264                         the_servers_pool[net][id].current_priority =
265                                         FRESCAN_BACKGROUND_PRIO;
266                 }
267         }
268
269         DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
270               "now... id:%u, current_budget:%u, current_priority:%u\n",
271               id,
272               the_servers_pool[net][id].current_budget,
273               the_servers_pool[net][id].current_priority);
274
275         return 0;
276 }