/*! * @file frescan_servers.c * * @brief FRESCAN sporadic servers * * @version 0.01 * * @date 27-Feb-2008 * * @author * Daniel Sangorrin * * @comments * * This file contains the FRESCAN sporadic servers that allow to isolate * different streams of data by assigning them a budget and replenishment * period. * * @license * * ----------------------------------------------------------------------- * Copyright (C) 2006 - 2008 FRESCOR consortium partners: * * Universidad de Cantabria, SPAIN * University of York, UK * Scuola Superiore Sant'Anna, ITALY * Kaiserslautern University, GERMANY * Univ. Politécnica Valencia, SPAIN * Czech Technical University in Prague, CZECH REPUBLIC * ENEA SWEDEN * Thales Communication S.A. FRANCE * Visual Tools S.A. SPAIN * Rapita Systems Ltd UK * Evidence ITALY * * See http://www.frescor.org for a link to partners' websites * * FRESCOR project (FP6/2005/IST/5-034026) is funded * in part by the European Union Sixth Framework Programme * The European Union is not liable of any use that may be * made of this code. * * This file is part of FRESCAN * * FRESCAN is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * FRESCAN is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * distributed with FRESCAN; see file COPYING. If not, write to the * Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. * * As a special exception, including FRESCAN header files in a file, * instantiating FRESCAN generics or templates, or linking other files * with FRESCAN objects to produce an executable application, does not * by itself cause the resulting executable application to be covered * by the GNU General Public License. This exception does not * however invalidate any other reasons why the executable file might be * covered by the GNU Public License. * ----------------------------------------------------------------------- * */ #include "frescan_servers.h" #include "frescan_servers_replenishments.h" // frescan_replenishments_xxx #include "frescan_debug.h" #include "frescan_data.h" #include #include #include #include /** * frescan_servers_init() - initialize server structures * * @net: the network instance */ int frescan_servers_init(frescan_network_t net) { int ret, i; DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "initializing servers\n"); ret = freelist_init(&the_servers_pool_freelist[net], FRESCAN_MX_IDS); if (ret != 0) return ret; for (i=0; ivalues.budget; the_servers_pool[net][*id].current_priority = params->prio; the_servers_pool[net][*id].pending_packets = 0; // the first act_time is set to the server creation time clock_gettime (CLOCK_MONOTONIC, &the_servers_pool[net][*id].act_time); INIT_LIST_HEAD(&the_servers_pool[net][*id].replenishments.repl_list); INIT_LIST_HEAD(&the_servers_pool[net][*id].packet_list.fifo_list); // the repl timer sends a signal when it expires with the server id evp.sigev_notify = SIGEV_SIGNAL; evp.sigev_signo = FRESCAN_REPL_SIGNAL_NUM; evp.sigev_value.sival_int = (int)*id; ret = timer_create (CLOCK_MONOTONIC, &evp, &the_servers_pool[net][*id].repl_timer); if (ret != 0) { ERROR("could not create timer\n"); return ret; } DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "server created, id:%u budget:%u prio:%u\n", *id, the_servers_pool[net][*id].params.values.budget, the_servers_pool[net][*id].params.prio); return 0; } /** * frescan_servers_update() - update a sporadic server data * * @net: the network instance * @params: the parameters for the server * @id: the identificator for the server * */ int frescan_servers_update(frescan_network_t net, const frescan_server_params_t *params, frescan_ss_t id) { the_servers_pool[net][id].params = *params; return 0; } /** * frescan_servers_destroy() - delete a sporadic server * * @net: the network instance * @id: the identificator for the server * */ int frescan_servers_destroy(frescan_network_t net, frescan_ss_t id) { int ret; // TODO: free the replenishment operations and the packets for the // server. ret = timer_delete (the_servers_pool[net][id].repl_timer); if (ret != 0) { ERROR("could not delete timer\n"); return ret; } FRESCAN_ACQUIRE_LOCK(&the_networks[net].lock); list_del(&the_servers_pool[net][id].servers_list); ret = freelist_free(&the_servers_pool_freelist[net], id); FRESCAN_RELEASE_LOCK(&the_networks[net].lock); if (ret != 0) { ERROR("could not free server data from pool\n"); return ret; } return 0; } /** * frescan_servers_get_data() - get a sporadic server data * * @net: the network instance * @params: the parameters of the server * @id: the identificator for the server * */ int frescan_servers_get_data(frescan_network_t net, frescan_server_params_t *params, frescan_ss_t id) { *params = the_servers_pool[net][id].params; return 0; } /** * frescan_servers_get_current_budget() - get the current sporadic server budget * * @net: the network instance * @id: the identificator for the server * @current_budget: the current budget of the server * */ int frescan_servers_get_current_budget(frescan_network_t net, frescan_ss_t id, frescan_budget_t *current_budget) { *current_budget = the_servers_pool[net][id].current_budget; return 0; } /** * frescan_servers_get_highest_prio() - get the server with highest priority * * @net: the network instance * @id: the identificator for the server * @prio: the priority of that server * * For each active server, check the priority. * If "id" is returned with a value of FRESCAN_MX_IDS, * there are no active servers. * NOTE: id=FRESCAN_MX_IDS is the identifier for fixed priority messages * TODO: use a priority queue of active servers * */ int frescan_servers_get_highest_prio(frescan_network_t net, frescan_ss_t *id, frescan_prio_t *prio) { frescan_server_data_t *server; if (list_empty(&the_active_servers->servers_list)) { DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "server list is empty\n"); *id = FRESCAN_MX_IDS; return 0; } *prio = 0; list_for_each_entry(server, &the_active_servers[net].servers_list, servers_list) { if (server->current_priority >= *prio) { *id = server->id; *prio = server->current_priority; } } DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "highest prio:%u id:%u\n", *prio, *id); return 0; } /** * frescan_servers_frame_sent() - hook to control the server budget and prio * * @net: the network instance * @id: the identificator for the server * * This function is called when a frame has been effectively sent through the * CAN bus and that frame is associated to a certain server. The function * decreases the capacity of the server and sets the priority to background * in case the budget is exhausted. * * NOTE: the replenishment operation is programmed using the corresponding * function at frescan_servers_replenishments module */ int frescan_servers_frame_sent(frescan_network_t net, frescan_ss_t id, frescan_packet_t *packet) { int ret; struct timespec *repl_time; frescan_server_data_t *server; server = &the_servers_pool[net][id]; DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "before.. id:%u, current_budget:%u, current_priority:%u\n", id, server->current_budget, server->current_priority); if (server->current_budget > 0) { server->current_budget--; if (server->current_budget == 0) { server->current_priority = FRESCAN_BACKGROUND_PRIO; } if (smaller_timespec(&packet->timestamp, &server->act_time)) { DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "using act_time\n"); repl_time = &server->act_time; } else { repl_time = &packet->timestamp; } ret = frescan_replenishment_program(net, id, repl_time); if (ret != 0) return -1; } DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "after.. id:%u, current_budget:%u, current_priority:%u\n", id, server->current_budget, server->current_priority); return 0; }