2 * @file frescan_servers.c
4 * @brief FRESCAN sporadic servers
15 * This file contains the FRESCAN sporadic servers that allow to isolate
16 * different streams of data by assigning them a budget and replenishment
21 * See MaRTE OS license
25 #include "frescan_servers.h"
26 #include "frescan_debug.h"
27 #include "frescan_data.h"
28 #include <misc/linux_list.h>
32 * frescan_servers_init() - initialize server structures
34 * @net: the network instance
37 int frescan_servers_init(frescan_network_t net)
41 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "initializing servers\n");
43 ret = freelist_init(&the_servers_pool_freelist[net], FRESCAN_MX_IDS);
44 if (ret != 0) return ret;
46 for (i=0; i<FRESCAN_MX_NETWORKS; i++) {
47 INIT_LIST_HEAD(&the_active_servers[net].servers_list);
54 * frescan_servers_create() - create a sporadic server
56 * @net: the network instance
57 * @params: the parameters for the server
58 * @id: the identificator for the server as a return value
62 int frescan_servers_create(frescan_network_t net,
63 const frescan_server_params_t *params,
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);
74 ERROR("could not allocate servers\n");
78 *id = (frescan_ss_t)pos;
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;
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);
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;
95 ret = timer_create (CLOCK_MONOTONIC,
96 &evp, &the_servers_pool[net][*id].repl_timer);
98 ERROR("could not create timer\n");
102 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
103 "server created, id:%u budget:%u prio:%u\n",
105 the_servers_pool[net][*id].params.values.budget,
106 the_servers_pool[net][*id].params.prio);
112 * frescan_servers_update() - update a sporadic server data
114 * @net: the network instance
115 * @params: the parameters for the server
116 * @id: the identificator for the server
120 int frescan_servers_update(frescan_network_t net,
121 const frescan_server_params_t *params,
124 the_servers_pool[net][id].params = *params;
129 * frescan_servers_destroy() - delete a sporadic server
131 * @net: the network instance
132 * @id: the identificator for the server
136 int frescan_servers_destroy(frescan_network_t net, frescan_ss_t id)
140 // TODO: free the replenishment operations and the packets for the
143 ret = timer_delete (the_servers_pool[net][id].repl_timer);
145 ERROR("could not delete timer\n");
149 FRESCAN_ACQUIRE_LOCK(&the_networks[net].lock);
150 list_del(&the_servers_pool[net][id].servers_list);
152 ret = freelist_free(&the_servers_pool_freelist[net], id);
153 FRESCAN_RELEASE_LOCK(&the_networks[net].lock);
155 ERROR("could not free server data from pool\n");
163 * frescan_servers_get_data() - get a sporadic server data
165 * @net: the network instance
166 * @params: the parameters of the server
167 * @id: the identificator for the server
171 int frescan_servers_get_data(frescan_network_t net,
172 frescan_server_params_t *params,
175 *params = the_servers_pool[net][id].params;
180 * frescan_servers_get_current_budget() - get the current sporadic server budget
182 * @net: the network instance
183 * @id: the identificator for the server
184 * @current_budget: the current budget of the server
188 int frescan_servers_get_current_budget(frescan_network_t net,
190 frescan_budget_t *current_budget)
192 *current_budget = the_servers_pool[net][id].current_budget;
197 * frescan_servers_get_highest_prio() - get the server with highest priority
199 * @net: the network instance
200 * @id: the identificator for the server
201 * @prio: the priority of that server
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
211 int frescan_servers_get_highest_prio(frescan_network_t net,
213 frescan_prio_t *prio)
215 frescan_server_data_t *server;
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;
224 list_for_each_entry(server, &the_active_servers[net].servers_list,
226 if (server->current_priority >= *prio) {
228 *prio = server->current_priority;
232 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
233 "highest prio:%u id:%u\n", *prio, *id);
239 * frescan_servers_frame_sent() - hook to control the server budget and prio
241 * @net: the network instance
242 * @id: the identificator for the server
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.
249 * NOTE: the replenishment operation is programmed using the corresponding
250 * function at frescan_servers_replenishments module
253 int frescan_servers_frame_sent(frescan_network_t net, frescan_ss_t id)
255 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
256 "id:%u, current_budget:%u, current_priority:%u\n",
258 the_servers_pool[net][id].current_budget,
259 the_servers_pool[net][id].current_priority);
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;
269 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
270 "now... id:%u, current_budget:%u, current_priority:%u\n",
272 the_servers_pool[net][id].current_budget,
273 the_servers_pool[net][id].current_priority);