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 pos = freelist_alloc(&the_servers_pool_freelist[net]);
71 ERROR("could not allocate servers\n");
75 *id = (frescan_ss_t)pos;
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;
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);
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;
92 ret = timer_create (CLOCK_MONOTONIC,
93 &evp, &the_servers_pool[net][*id].repl_timer);
95 ERROR("could not create timer\n");
99 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
100 "server created, id:%u budget:%u prio:%u\n",
102 the_servers_pool[net][*id].params.values.budget,
103 the_servers_pool[net][*id].params.prio);
109 * frescan_servers_update() - update a sporadic server data
111 * @net: the network instance
112 * @params: the parameters for the server
113 * @id: the identificator for the server
117 int frescan_servers_update(frescan_network_t net,
118 const frescan_server_params_t *params,
121 the_servers_pool[net][id].params = *params;
126 * frescan_servers_destroy() - delete a sporadic server
128 * @net: the network instance
129 * @id: the identificator for the server
133 int frescan_servers_destroy(frescan_network_t net, frescan_ss_t id)
137 // TODO: free the replenishment operations and the packets for the
140 ret = timer_delete (the_servers_pool[net][id].repl_timer);
142 ERROR("could not delete timer\n");
146 list_del(&the_servers_pool[net][id].servers_list);
148 ret = freelist_free(&the_servers_pool_freelist[net], id);
150 ERROR("could not free server data from pool\n");
158 * frescan_servers_get_data() - get a sporadic server data
160 * @net: the network instance
161 * @params: the parameters of the server
162 * @id: the identificator for the server
166 int frescan_servers_get_data(frescan_network_t net,
167 frescan_server_params_t *params,
170 *params = the_servers_pool[net][id].params;
175 * frescan_servers_get_current_budget() - get the current sporadic server budget
177 * @net: the network instance
178 * @id: the identificator for the server
179 * @current_budget: the current budget of the server
183 int frescan_servers_get_current_budget(frescan_network_t net,
185 frescan_budget_t *current_budget)
187 *current_budget = the_servers_pool[net][id].current_budget;
192 * frescan_servers_get_highest_prio() - get the server with highest priority
194 * @net: the network instance
195 * @id: the identificator for the server
196 * @prio: the priority of that server
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
206 int frescan_servers_get_highest_prio(frescan_network_t net,
208 frescan_prio_t *prio)
210 frescan_server_data_t *server;
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;
219 list_for_each_entry(server, &the_active_servers[net].servers_list,
221 if (server->current_priority >= *prio) {
223 *prio = server->current_priority;
227 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
228 "highest prio:%u id:%u\n", *prio, *id);
234 * frescan_servers_frame_sent() - hook to control the server budget and prio
236 * @net: the network instance
237 * @id: the identificator for the server
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.
244 * NOTE: the replenishment operation is programmed using the corresponding
245 * function at frescan_servers_replenishments module
248 int frescan_servers_frame_sent(frescan_network_t net, frescan_ss_t id)
250 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
251 "id:%u, current_budget:%u, current_priority:%u\n",
253 the_servers_pool[net][id].current_budget,
254 the_servers_pool[net][id].current_priority);
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;
264 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
265 "now... id:%u, current_budget:%u, current_priority:%u\n",
267 the_servers_pool[net][id].current_budget,
268 the_servers_pool[net][id].current_priority);