/* * This file is part of the rtap localisation project. */ #include "owlps-aggregator.h" #include char *program_name = NULL ; cfg_t *cfg = NULL ; // Configuration structure request_list *requests = NULL ; // Computed data list ap_list *token_aps = NULL ; // Token ring of the APs uint_fast16_t nb_aps = 0 ; // Number of APs in the AP ring int main(int argc, char **argv) { int ret = 0 ; // Program return value struct sigaction action ; // Signal handler structure pthread_t monitor_thread, // Aggregated data monitoring thread monitor_aps_thread, // APs monitoring thread autocalibration_hello_thread ; // Hello messages reception thread uint_fast16_t listening_port ; int sockfd ; // UDP listening socket program_name = argv[0] ; initialise_configuration(argc, argv) ; /* Set up signal handlers */ action.sa_flags = 0 ; sigemptyset(&action.sa_mask) ; action.sa_handler = owl_sigint_handler ; sigaction(SIGINT, &action, NULL) ; action.sa_handler = owl_sigterm_handler ; sigaction(SIGTERM, &action, NULL) ; /* Create UDP socket */ listening_port = cfg_getint(cfg, "listening_port") ; if ((sockfd = owl_create_udp_listening_socket(listening_port)) < 0) { fprintf(stderr, "Error! Cannot listen on port %"PRIuFAST16".\n", listening_port) ; ret = ERR_CREATING_SOCKET ; goto exit ; } /* Set up threads */ ret = pthread_create(&monitor_thread, NULL, &monitor_requests, NULL) ; if (ret != 0) { perror("Cannot create monitor thread") ; ret = ERR_CREATING_THREAD ; goto exit ; } if (cfg_getbool(cfg, "autocalibration")) { ret = pthread_create(&autocalibration_hello_thread, NULL, &listen_for_aps, NULL) ; if (ret != 0) { perror("Cannot create autocalibration hello thread") ; ret = ERR_CREATING_THREAD ; goto exit ; } ret = pthread_create(&monitor_aps_thread, NULL, &monitor_aps, NULL) ; if (ret != 0) { perror("Cannot create monitor APs thread") ; ret = ERR_CREATING_THREAD ; goto exit ; } } run = TRUE ; ret = read_loop(sockfd) ; /* Wait for the threads to terminate */ fprintf(stderr, "Waiting for the monitor thread... ") ; if (pthread_join(monitor_thread, NULL) != 0) perror("Cannot join monitor thread") ; else fprintf(stderr, "OK.\n") ; if (cfg_getbool(cfg, "autocalibration")) { // We must cancel this thread because it can be blocked on the // recvfrom() call: fprintf(stderr, "Cancelling the autocalibration hello thread... ") ; if (pthread_cancel(autocalibration_hello_thread) != 0) perror("Cannot cancel autocalibration hello thread") ; else fprintf(stderr, "OK.\n") ; fprintf(stderr, "Waiting for the autocalibration hello thread... ") ; if (pthread_join(autocalibration_hello_thread, NULL) != 0) perror("Cannot join autocalibration hello thread") ; else fprintf(stderr, "OK.\n") ; fprintf(stderr, "Waiting for the monitor APs thread... ") ; if (pthread_join(monitor_aps_thread, NULL) != 0) perror("Cannot join monitor APs thread") ; else fprintf(stderr, "OK.\n") ; } /* Last cleaning tasks */ exit: close(sockfd) ; // Close socket free_request_list() ; free_ap_list() ; cfg_free(cfg) ; // Clean configuration fprintf(stderr, "%s: end.\n", program_name) ; return ret ; } void initialise_configuration(int argc, char **argv) { parse_config_file(argc, argv) ; parse_command_line(argc, argv) ; check_configuration() ; #ifdef DEBUG /* Configuration printing */ fprintf(stderr, "Configuration:\n") ; cfg_print(cfg, stderr) ; #endif // DEBUG } void parse_config_file(int argc, char **argv) { // Config file options for confuse cfg_opt_t opts[] = { // Be verbose? CFG_BOOL("verbose", cfg_false, CFGF_NONE), // Aggregation listening port CFG_INT("listening_port", AGGREGATE_DEFAULT_PORT, CFGF_NONE), // Port and IP address of the localisation server: CFG_INT("positioner_port", POSITIONER_DEFAULT_PORT, CFGF_NONE), CFG_STR("positioner_ip", POSITIONER_DEFAULT_IP, CFGF_NONE), CFG_STR("output_file", "", CFGF_NONE), // Timeouts (in milliseconds): CFG_INT("aggregate_timeout", DEFAULT_AGGREGATE_TIMEOUT, CFGF_NONE), CFG_INT("keep_timeout", DEFAULT_KEEP_TIMEOUT, CFGF_NONE), // Time between two list checks (in microseconds): CFG_INT("check_interval", DEFAULT_CHECK_INTERVAL, CFGF_NONE), // Autocalibration activated? CFG_BOOL("autocalibration", cfg_false, CFGF_NONE), // Port on which autocalibration data are exchanged: CFG_INT("autocalibration_port", DEFAULT_AUTOCALIBRATION_PORT, CFGF_NONE), // Time we keep APs in the list (in seconds): CFG_INT("ap_keep_timeout", DEFAULT_AP_KEEP_TIMEOUT, CFGF_NONE), // Time between two checks of the AP list (in milliseconds): CFG_INT("ap_check_interval", DEFAULT_AP_CHECK_INTERVAL, CFGF_NONE), CFG_END() } ; char *config_file ; // Option -f specifies a config file, so we search for it first int opt ; do opt = getopt(argc, argv, OPTIONS) ; while (opt != 'f' && opt != -1) ; if (opt == 'f') { config_file = malloc((strlen(optarg) + 1) * sizeof(char)) ; strcpy(config_file, optarg) ; } else // If -f isn't found, we use the default config file { config_file = malloc((strlen(DEFAULT_CONFIG_FILE) + 1) * sizeof(char)) ; strcpy(config_file, DEFAULT_CONFIG_FILE) ; } /* Parse config file */ cfg = cfg_init(opts, CFGF_NONE) ; // Initialise options switch (cfg_parse(cfg, config_file)) { case CFG_FILE_ERROR : fprintf(stderr, "Error! Cannot open configuration file « %s »: %s.\n", config_file, strerror(errno)) ; break ; case CFG_PARSE_ERROR : fprintf(stderr, "Error! Parsing of configuration file « %s » failed!\n", config_file) ; free(config_file) ; exit(ERR_PARSING_CONFIG_FILE) ; } free(config_file) ; } void parse_command_line(int argc, char **argv) { int opt ; optind = 1 ; // Rewind argument parsing while ((opt = getopt(argc, argv, OPTIONS)) != -1) { switch (opt) { case 'A' : cfg_setbool(cfg, "autocalibration", cfg_true) ; break ; case 'a' : cfg_setint(cfg, "autocalibration_port", strtol(optarg, NULL, 0)) ; break ; case 'c' : cfg_setint(cfg, "check_interval", strtol(optarg, NULL, 0)) ; break ; case 'C' : cfg_setint(cfg, "ap_check_interval", strtol(optarg, NULL, 0)) ; break ; case 'f' : // Config file break ; // (already parsed) case 'h' : print_usage() ; exit(0) ; case 'i' : cfg_setstr(cfg, "positioner_ip", optarg) ; break ; case 'k' : cfg_setint(cfg, "keep_timeout", strtol(optarg, NULL, 0)) ; break ; case 'K' : cfg_setint(cfg, "ap_keep_timeout", strtol(optarg, NULL, 0)) ; break ; case 'l' : cfg_setint(cfg, "listening_port", strtol(optarg, NULL, 0)) ; break ; case 'o' : cfg_setstr(cfg, "output_file", optarg) ; break ; case 'p' : cfg_setint(cfg, "positioner_port", strtol(optarg, NULL, 0)) ; break ; case 't' : cfg_setint(cfg, "aggregate_timeout", strtol(optarg, NULL, 0)) ; break ; case 'v' : cfg_setbool(cfg, "verbose", cfg_true) ; break ; default : print_usage() ; exit(ERR_BAD_USAGE) ; } } } void check_configuration() { // output_file // if (cfg_getstr(cfg, "output_file")[0] == '\0') { fprintf(stderr, "Error! You must specify an output file.\n") ; print_usage() ; exit(ERR_BAD_USAGE) ; } // listening_port // if (cfg_getint(cfg, "listening_port") < 1 || cfg_getint(cfg, "listening_port") > 65535) { #ifdef DEBUG fprintf(stderr, "Warning! Bad listening_port:" " failing back to the default value.\n") ; #endif // DEBUG cfg_setint(cfg, "listening_port", AGGREGATE_DEFAULT_PORT) ; } // positioner_port // if (cfg_getint(cfg, "positioner_port") < 1 || cfg_getint(cfg, "positioner_port") > 65535) { #ifdef DEBUG fprintf(stderr, "Warning! Bad positioner_port:" " failing back to the default value.\n") ; #endif // DEBUG cfg_setint(cfg, "positioner_port", POSITIONER_DEFAULT_PORT) ; } // positioner_ip // if (cfg_getstr(cfg, "positioner_ip")[0] == '\0') { fprintf(stderr, "Error! You must specify the IP address of the" " localisation server.\n") ; print_usage() ; exit(ERR_BAD_USAGE) ; } // aggregate_timeout // if (cfg_getint(cfg, "aggregate_timeout") < 0) { #ifdef DEBUG fprintf(stderr, "Warning! aggregate_timeout cannot be negative:" " failing back to the default value.\n") ; #endif // DEBUG cfg_setint(cfg, "aggregate_timeout", DEFAULT_AGGREGATE_TIMEOUT) ; } // keep_timeout // if (cfg_getint(cfg, "keep_timeout") < 0) { #ifdef DEBUG fprintf(stderr, "Warning! keep_timeout cannot be negative:" " failing back to the default value.\n") ; #endif // DEBUG cfg_setint(cfg, "keep_timeout", DEFAULT_KEEP_TIMEOUT) ; } // check_interval // if (cfg_getint(cfg, "check_interval") < 0) { #ifdef DEBUG fprintf(stderr, "Warning! check_interval cannot be negative:" " failing back to the default value.\n") ; #endif // DEBUG cfg_setint(cfg, "check_interval", DEFAULT_CHECK_INTERVAL) ; } // ap_keep_timeout // if (cfg_getint(cfg, "ap_keep_timeout") < 0) { #ifdef DEBUG fprintf(stderr, "Warning! ap_keep_timeout cannot be negative:" " failing back to the default value.\n") ; #endif // DEBUG cfg_setint(cfg, "ap_keep_timeout", DEFAULT_AP_KEEP_TIMEOUT) ; } // ap_check_interval // if (cfg_getint(cfg, "ap_check_interval") < 0) { #ifdef DEBUG fprintf(stderr, "Warning! ap_check_interval cannot be negative:" " failing back to the default value.\n") ; #endif // DEBUG cfg_setint(cfg, "ap_check_interval", DEFAULT_AP_CHECK_INTERVAL) ; } } /* * Reads packets while the program is not stopped. */ int read_loop(int sockfd) { int ret = 0 ; // Return value ssize_t nread ; // recvfrom return value struct sockaddr_in client; // UDP client structure socklen_t client_len = sizeof(client) ; // Size of clients owl_captured_request request ; // Message read on the socket char // Return values of mobile_ip_str[INET_ADDRSTRLEN], // inet_ntop() // and owl_timestamp_to_string(): request_time_str[OWL_TIMESTAMP_STR_LEN], start_time_str[OWL_TIMESTAMP_STR_LEN] ; while (run) { nread = recvfrom(sockfd, &request, sizeof(request), 0, (struct sockaddr *) &client, &client_len) ; if (nread <= 0) { if (run) { fprintf(stderr, "No request received from client!\n") ; ret = ERR_NO_MESSAGE_RECEIVED ; } break ; } // Endianess conversions: request.request_time = owl_ntoh_timestamp(request.request_time) ; request.start_time = owl_ntoh_timestamp(request.start_time) ; if (cfg_getbool(cfg, "verbose")) { inet_ntop(AF_INET, &request.mobile_ip_addr_bytes, mobile_ip_str, INET_ADDRSTRLEN) ; owl_timestamp_to_string(request_time_str, request.request_time) ; owl_timestamp_to_string(start_time_str, request.start_time) ; fprintf(stderr, "\n" "*** Request received from AP ***\n" "\tType: %"PRIu8"\n" "\tAP MAC: %s\n" "\tMobile MAC: %s\n" "\tMobile IP: %s\n" "\tSequence number (request timestamp): %s\n" "\tRequest arrival time on the AP: %s\n" "\tSignal: %d dBm\n" "\tPosition X: %f\n" "\tPosition Y: %f\n" "\tPosition Z: %f\n" "\tDirection: %hhd\n" , request.type, owl_mac_bytes_to_string(request.ap_mac_addr_bytes), owl_mac_bytes_to_string(request.mobile_mac_addr_bytes), mobile_ip_str, request_time_str, start_time_str, request.antenna_signal_dbm - 0x100, request.x_position, request.y_position, request.z_position, request.direction ) ; } #ifdef DEBUG else fprintf(stderr, "Request received from AP « %s ».\n", owl_mac_bytes_to_string(request.ap_mac_addr_bytes)) ; #endif // DEBUG got_request(request) ; } return ret ; } /* * Thread function. Monitors the list and sends information to the * localisation server when the timeout is reached. */ void* monitor_requests(void *NULL_value) { request_list *request_ptr, *request_prev ; request_info_list *request_info_ptr ; owl_timestamp current_time ; FILE *fd = NULL ; char mac_str[OWL_ETHER_ADDR_STRLEN] ; uint_fast32_t sub ; // owl_time_elapsed_ms() result #ifdef USE_TIMESTAMP char request_time_str[OWL_TIMESTAMP_STR_LEN] ; #endif // USE_TIMESTAMP uint_fast32_t aggregate_timeout = cfg_getint(cfg, "aggregate_timeout") ; uint_fast32_t keep_timeout = cfg_getint(cfg, "keep_timeout") ; struct sockaddr_in serv; struct sockaddr_in client ; socklen_t serv_len = sizeof(serv); owl_request request ; owl_request_info info; int sockfd; #ifdef DEBUG fprintf(stderr, "Monitor requests thread launched.\n") ; #endif // DEBUG sockfd = owl_create_udp_trx_socket(cfg_getstr(cfg, "positioner_ip"), cfg_getint(cfg, "positioner_port"), &serv, &client) ; pthread_cleanup_push(&owl_close_fd, &sockfd) ; /* Open output file */ if (strcmp("-", cfg_getstr(cfg, "output_file")) == 0) fd = stdout ; else { fd = fopen(cfg_getstr(cfg, "output_file"), "a") ; // add mode if (fd == NULL) // If we failed to open the file, { perror("Cannot open output file") ; fprintf(stderr, "Redirecting output to standard output.\n") ; fd = stdout ; // we fail back to stdout. } } pthread_cleanup_push(&owl_close_file, &fd) ; while (run) { request_ptr = requests ; request_prev = NULL ; request_info_ptr = NULL ; owl_timestamp_now(¤t_time) ; while (request_ptr != NULL) // Parsing list { sub = owl_time_elapsed_ms(request_ptr->start_time, current_time) ; // If the request was not treated already if (request_ptr->info != NULL) { // If the timeout is reached if (sub > aggregate_timeout) { fprintf(stderr, "* Timeout reached.") ; #ifdef DEBUG fprintf(stderr, " sub=%"PRIuFAST32" >" " aggregate_timeout=%"PRIuFAST32"\n", sub, aggregate_timeout) ; #else // DEBUG putc('\n', stderr) ; #endif // DEBUG // Print mobile MAC address to the output file owl_mac_bytes_to_string_r(request_ptr-> mobile_mac_addr_bytes, mac_str) ; fprintf(fd, "%s;", mac_str) ; // Print request type to the output file fprintf(fd, "%"PRIu8";", request_ptr->type) ; #ifdef USE_TIMESTAMP // Print request mobile timestamp to the output file owl_timestamp_to_string(request_time_str, request_ptr->request_time) ; fprintf(fd, "%s;", request_time_str) ; #endif // USE_TIMESTAMP // Print request info to the output file fprintf(fd, "%0.2f;%0.2f;%0.2f;%hhd", request_ptr->x_position, request_ptr->y_position, request_ptr->z_position, request_ptr->direction) ; request.type = request_ptr->type ; memcpy(request.mobile_mac_addr_bytes, request_ptr->mobile_mac_addr_bytes, ETHER_ADDR_LEN) ; request.request_time = request_ptr->request_time ; request.nb_info = 0 ; // Count the requests: request_info_ptr = request_ptr->info ; while (request_info_ptr != NULL) { request.nb_info++; request_info_ptr = request_info_ptr->next ; } // Endianess conversions: request.nb_info = htons(request.nb_info) ; request.request_time = owl_hton_timestamp(request.request_time) ; // Send the request: sendto(sockfd, &request, sizeof(request), 0, (struct sockaddr *)&serv, serv_len) ; // Send requests to the server and empty the list request_info_ptr = request_ptr->info ; while (request_info_ptr != NULL) { // Send AP info to the localisation server memcpy(info.ap_mac_addr_bytes, request_info_ptr->ap_mac_addr_bytes, ETHER_ADDR_LEN) ; info.antenna_signal_dbm = request_info_ptr->antenna_signal_dbm - 0x100 ; sendto(sockfd, &info, sizeof(info), 0, (struct sockaddr *)&serv, serv_len) ; // Print AP info to the output file owl_mac_bytes_to_string_r(request_info_ptr-> ap_mac_addr_bytes, mac_str) ; fprintf(fd, ";%s;%d", mac_str, request_info_ptr->antenna_signal_dbm - 0x100) ; // Delete request request_info_ptr = request_info_ptr->next ; free(request_ptr->info) ; request_ptr->info = request_info_ptr ; } fprintf(fd, "\n") ; } } // If the request was treated and keep timeout is reached else if (sub > keep_timeout) { request_list *request_tmp = request_ptr ; fprintf(stderr, "* Keep timeout reached.") ; #ifdef DEBUG fprintf(stderr, " sub=%"PRIuFAST32" >" " keep_timeout=%"PRIuFAST32"\n", sub, keep_timeout) ; #else // DEBUG putc('\n', stderr) ; #endif // DEBUG request_ptr = request_ptr->next ; // If it is the first request of the list if (request_prev == NULL) requests = request_ptr ; // we shift the head else // else we put the next of the previous on the next request_prev->next = request_ptr ; free(request_tmp) ; continue ; } // Next request request_prev = request_ptr ; request_ptr = request_ptr->next ; } fflush(NULL) ; usleep(cfg_getint(cfg, "check_interval")) ; // Wait to check again } /* Close output file & socket */ pthread_cleanup_pop(1) ; pthread_cleanup_pop(1) ; pthread_exit(NULL_value) ; } /* * Treats a received packet. */ void got_request(owl_captured_request request) { request_list *tmp_request = NULL ; request_info_list *tmp_info = NULL ; owl_timestamp start_time ; // Reception time on the aggregator owl_timestamp_now(&start_time) ; /* Create a new request */ tmp_info = malloc(sizeof(request_info_list)) ; memcpy(tmp_info->ap_mac_addr_bytes, request.ap_mac_addr_bytes, ETHER_ADDR_LEN) ; tmp_info->antenna_signal_dbm = request.antenna_signal_dbm ; tmp_info->next = NULL ; /* Add it in the list */ tmp_request = requests ; if (requests == NULL) // If the request list does not exist, { fprintf(stderr, "Creating request list.\n") ; tmp_request = malloc(sizeof(request_list)) ; // create it. tmp_request->type = request.type ; memcpy(tmp_request->mobile_mac_addr_bytes, request.mobile_mac_addr_bytes, ETHER_ADDR_LEN) ; // Explicit packet: if (request.type != OWL_REQUEST_IMPLICIT) // Transmission time on the mobile: tmp_request->request_time = request.request_time ; // Implicit packet: else // Reception time on the AP: tmp_request->request_time = request.start_time ; // Save locale time on the aggregator (not the reception time // on the AP): tmp_request->start_time = start_time ; tmp_request->x_position = request.x_position ; tmp_request->y_position = request.y_position ; tmp_request->z_position = request.z_position ; tmp_request->direction = request.direction ; tmp_request->next = NULL ; tmp_request->info = tmp_info ; requests = tmp_request ; } else // If the request list exists already { // we search the list for the request // Explicit packet: if (request.type != OWL_REQUEST_IMPLICIT) { while (tmp_request != NULL) { // Research criterion: MAC and transmission time if (owl_mac_equals(request.mobile_mac_addr_bytes, tmp_request->mobile_mac_addr_bytes) && owl_timestamp_equals(request.request_time, tmp_request->request_time)) break ; // If the request exists, we stop on it tmp_request = tmp_request->next ; } } // Implicit packet: else { while (tmp_request != NULL) { // Research criterion: MAC addresses equals and reception // times on the APs less than 10 ms // TODO : define an option for the maximal difference time. if (owl_mac_equals(request.mobile_mac_addr_bytes, tmp_request->mobile_mac_addr_bytes) && owl_time_elapsed_ms(request.start_time, tmp_request->request_time) <= 10) break ; // If the request exists, we stop on it tmp_request = tmp_request->next ; } } if (tmp_request == NULL) // The request does not exist in the list { fprintf(stderr, "Create new request.\n") ; tmp_request = malloc(sizeof(request_list)) ; // create it tmp_request->type = request.type ; memcpy(tmp_request->mobile_mac_addr_bytes, request.mobile_mac_addr_bytes, ETHER_ADDR_LEN) ; // Explicit packet: if (request.type != OWL_REQUEST_IMPLICIT) // Transmission time on the mobile: tmp_request->request_time = request.request_time ; // Implicit packet: else // Reception time on the AP: tmp_request->request_time = request.start_time ; // Save locale time on the aggregator (not the reception time // on the AP): tmp_request->start_time = start_time ; tmp_request->x_position = request.x_position ; tmp_request->y_position = request.y_position ; tmp_request->z_position = request.z_position ; tmp_request->direction = request.direction ; tmp_request->next = requests ; tmp_request->info = tmp_info ; requests = tmp_request ; } else // If the request was found in the list { if (tmp_request->info == NULL) { // We already sent to the server data for this request fprintf(stderr, "Request already treated.\n") ; free(tmp_info) ; } else { fprintf(stderr, "Add information to the request.\n") ; tmp_info->next = tmp_request->info ; // Add data tmp_request->info = tmp_info ; } } } } /* * Empties the request list. */ void free_request_list() { request_list *next_request ; request_info_list *next_request_info ; while (requests != NULL) { while (requests->info != NULL) { next_request_info = requests->info->next ; free(requests->info) ; requests->info = next_request_info ; } next_request = requests->next ; free(requests) ; requests = next_request ; } } /* * Thread function. Listens for hello messages from APs. */ void* listen_for_aps(void *NULL_value) { int listen_sockfd ; int nread ; // recvfrom return value struct sockaddr_in client; // UDP client structure socklen_t client_len = sizeof(client) ; // Size of clients owl_autocalibration_hello message ; char ap_ip_addr[INET_ADDRSTRLEN] ; #ifdef DEBUG fprintf(stderr, "Autocalibration Hello thread launched.\n") ; #endif // DEBUG listen_sockfd = owl_create_udp_listening_socket(cfg_getint(cfg, "autocalibration_port")) ; if (listen_sockfd < 0) { perror("Error! Cannot create UDP listening socket from the" " listeners") ; exit(ERR_CREATING_SOCKET) ; } pthread_cleanup_push(&owl_close_fd, &listen_sockfd) ; while (run) { nread = recvfrom(listen_sockfd, &message, sizeof(message), 0, (struct sockaddr *) &client, &client_len) ; if (nread <= 0 && run) { if (run) fprintf(stderr, "No message received from listener!\n") ; continue ; } strncpy(ap_ip_addr, inet_ntoa(client.sin_addr), INET_ADDRSTRLEN) ; #ifdef DEBUG fprintf(stderr, "Got a Hello message from « %s »\n", ap_ip_addr) ; #endif // DEBUG update_ap(message.ap_mac_addr_bytes, ap_ip_addr) ; } /* Close the socket */ pthread_cleanup_pop(1) ; pthread_exit(NULL_value) ; } /* * Updates the timestamp of the AP with the given MAC address if it is in * the AP list, or add a new AP with this MAC address to the AP list. */ void update_ap(uint8_t mac_addr_bytes[ETHER_ADDR_LEN], char ip_addr[INET_ADDRSTRLEN]) { ap_list *found ; if ((found = find_ap(mac_addr_bytes)) == NULL) { ap_list *new_ap = add_ap_front(mac_addr_bytes) ; update_ap_ip_addr(new_ap, ip_addr) ; } else update_ap_seen(found) ; } /* * Searches the AP list for an AP with the given MAC address and returns * it. */ ap_list* find_ap(uint8_t mac_addr_bytes[ETHER_ADDR_LEN]) { ap_list *found ; if (token_aps == NULL) return NULL ; found = token_aps ; do { if (owl_mac_equals(found->mac_addr_bytes, mac_addr_bytes)) return found ; found = found->next ; } while (found != token_aps) ; return NULL ; } /* * Adds a new AP in front of the AP list. */ ap_list* add_ap_front(uint8_t mac_addr_bytes[ETHER_ADDR_LEN]) { #ifdef DEBUG char mac_str[OWL_ETHER_ADDR_STRLEN] ; owl_mac_bytes_to_string_r(mac_addr_bytes, mac_str) ; fprintf(stderr, "Creating AP with MAC address « %s »...\n", mac_str) ; #endif // DEBUG ap_list *ap = malloc(sizeof(ap_list)) ; memcpy(ap->mac_addr_bytes, mac_addr_bytes, ETHER_ADDR_LEN) ; update_ap_seen(ap) ; push_ap(ap) ; return ap ; } /* * Change the IP address of the AP 'ap' with 'ip_addr'. */ void update_ap_ip_addr(ap_list *ap, char ip_addr[INET_ADDRSTRLEN]) { strncpy(ap->ip_addr, ip_addr, INET_ADDRSTRLEN) ; } /* * Updates the timestamp of the given AP. */ void update_ap_seen(ap_list *ap) { assert(ap) ; owl_timestamp_now(&ap->last_seen) ; } /* * Puts an existing AP in front of the AP list. The AP must not be in * the list yet. */ void push_ap(ap_list *ap) { assert(ap) ; ++nb_aps ; if (token_aps == NULL) // List does not exist yet { token_aps = ap ; ap->next = ap ; ap->previous = ap ; return ; } ap->previous = token_aps->previous ; ap->previous->next = ap ; ap->next = token_aps ; token_aps->previous = ap ; token_aps = ap ; } /* * Monitors the AP list: sends orders to APs following their order in * the list, and deletes old APs. */ void* monitor_aps(void *NULL_value) { #ifdef DEBUG fprintf(stderr, "Monitor AP thread launched.\n") ; #endif // DEBUG while (run) { delete_old_aps() ; if (nb_aps > 1) { order_send(token_aps) ; token_aps = token_aps->next ; } usleep(cfg_getint(cfg, "ap_check_interval") * 1000) ; } pthread_exit(NULL_value) ; } /* * Deletes APs that did not send any Hello packet for a while, following * the list order. Stops on the first not-to-be-deleted AP. */ void delete_old_aps() { owl_timestamp now ; owl_timestamp_now(&now) ; while (token_aps != NULL) if (owl_time_elapsed_ms(token_aps->last_seen, now) > (uint_fast32_t) cfg_getint(cfg, "ap_keep_timeout") * 1000) delete_ap(token_aps) ; else return ; } /* * Deletes the given AP from the AP list. */ void delete_ap(ap_list *ap) { #ifdef DEBUG assert(ap) ; char mac_str[OWL_ETHER_ADDR_STRLEN] ; owl_mac_bytes_to_string_r(token_aps->mac_addr_bytes, mac_str) ; fprintf(stderr, "Deleting AP « %s »...\n", mac_str) ; #endif // DEBUG unlink_ap(ap) ; free(ap) ; } /* * Extracts the given AP from the AP list (it will not be linked to any * other element of the list). */ void unlink_ap(ap_list *ap) { ap_list *ap_previous, *ap_next ; assert(ap) ; ap_previous = ap->previous ; ap_next = ap->next ; assert(ap_previous) ; assert(ap_next) ; ap_previous->next = ap_next ; ap_next->previous = ap_previous ; if (ap == token_aps) { if (ap->next == ap) // It was the last AP in the ring token_aps = NULL ; else token_aps = ap_next ; } --nb_aps ; } /* * Sends a 'send' order to the given AP. */ void order_send(ap_list *ap) { owl_autocalibration_order message ; struct sockaddr_in serv; struct sockaddr_in client ; socklen_t serv_len = sizeof(serv); int sockfd ; ssize_t nsent ; #ifdef DEBUG fprintf(stderr, "Sending an order to %s...\n", ap->ip_addr) ; #endif // DEBUG sockfd = owl_create_udp_trx_socket(ap->ip_addr, cfg_getint(cfg, "autocalibration_port"), &serv, &client) ; message.order = AUTOCALIBRATION_ORDER_SEND ; nsent = sendto(sockfd, &message, sizeof(message), 0, (struct sockaddr *)&serv, serv_len) ; if (nsent != (ssize_t) sizeof(message)) { perror("Error sending order to the listener") ; exit(ERR_SENDING_INFO) ; } close(sockfd) ; } /* * Empties the AP list. */ void free_ap_list() { ap_list *ap_ptr ; if (token_aps == NULL) return ; ap_ptr = token_aps->next ; assert(ap_ptr) ; while (ap_ptr != token_aps) { ap_list *ap_tmp = ap_ptr ; ap_ptr = ap_ptr->next ; free(ap_tmp) ; } free(token_aps) ; token_aps = NULL ; } #ifdef DEBUG /* * Prints the request list. */ void print_request_list() { request_list *request_ptr = requests ; request_info_list *info_ptr = NULL ; char mobile_mac_str[OWL_ETHER_ADDR_STRLEN] ; char request_time_str[OWL_TIMESTAMP_STR_LEN], start_time_str[OWL_TIMESTAMP_STR_LEN] ; if (requests == NULL) // Empty list { fprintf(stderr, "No request.\n") ; return ; } while (request_ptr != NULL) { info_ptr = request_ptr->info ; // Get the sub-list pointer owl_mac_bytes_to_string_r(request_ptr->mobile_mac_addr_bytes, mobile_mac_str) ; owl_timestamp_to_string(request_time_str, request_ptr->request_time) ; owl_timestamp_to_string(start_time_str, request_ptr->start_time) ; fprintf(stderr, "Type: %"PRIu8"\n" "Mobile MAC: %s\n" "Sequence number: %s\n" "Reception timestamp: %s\n" "\n", request_ptr->type, mobile_mac_str, request_time_str, start_time_str ) ; // Parse information relative to the current request while (info_ptr != NULL) { print_request_info(info_ptr) ; putc('\n', stderr) ; info_ptr = info_ptr->next ; } fprintf(stderr, "\n\n") ; request_ptr = request_ptr->next ; } } /* * Prints an element of a request_info_list. */ void print_request_info(request_info_list *info) { char ap_mac_str[OWL_ETHER_ADDR_STRLEN] ; if (info == NULL) return ; owl_mac_bytes_to_string_r(info->ap_mac_addr_bytes, ap_mac_str) ; fprintf(stderr, "\tAP MAC: %s\n" "\tSignal strength: %d dBm\n", ap_mac_str, info->antenna_signal_dbm - 0x100 ) ; } #endif // DEBUG void print_usage() { printf("Usage:\n" "\t%s" " [-f config_file]" " [-v]" " [-o output_file]" " [-i positionner_ip]" " [-p positioner_port]" " [-l listening_port]" " [-t aggregate_timeout]" " [-k keep_timeout]" " [-c check_interval]" " [-A]" " [-a autocalibration_port]" " [-K ap_keep_timeout]" " [-C ap_check_interval]" "\n" "Main options:\n" "\t-h\t\tPrint this help.\n" "\t-f config_file\tUse 'config_file' instead of the default" " configuration file (%s).\n" "\t-v\t\tBe verbose (print detailed info on each received" " message).\n" "Output options:\n" "\t-o output_file\t\tAggregated requests will be appended to" " this file ('-' designates the standard output).\n" "\t-i positionner_ip\tIP address of the localisation server" " (default: %s).\n" "\t-p positioner_port\tAggregated requests are transmitted to" " the localisation server on this port (default: %d).\n" "Aggregation options:\n" "\t-l listening_port\tOnly requests sent on this port will be" " treated (default: %d).\n" "\t-t aggregate_timeout\tRequests are stored during" " 'aggregate_timeout' milliseconds before to be grouped" " (default: %d ms).\n" "\t-k keep_timeout\t\tAggregated requests are kept during" " 'keep_timeout' milliseconds (default: %d ms).\n" "\t-c check_interval\tTime between two checks of the stored" " requests (default\t%d microseconds).\n" "Autocalibration options:\n" "\t-A\t\t\tEnable autocalibration (default: disabled).\n" "\t-a port\t\t\tPort on which autocalibration data" " are exchanged with the listeners (default: %d).\n" "\t-K ap_keep_timeout\tInactive APs are kept during" " 'ap_keep_timeout' seconds (default: %d s).\n" "\t-C ap_check_interval\tTime (in milliseconds) between two" " checks of the stored APs (default: %d ms).\n" , program_name, DEFAULT_CONFIG_FILE, POSITIONER_DEFAULT_IP, POSITIONER_DEFAULT_PORT, AGGREGATE_DEFAULT_PORT, DEFAULT_AGGREGATE_TIMEOUT, DEFAULT_KEEP_TIMEOUT, DEFAULT_CHECK_INTERVAL, DEFAULT_AUTOCALIBRATION_PORT, DEFAULT_AP_KEEP_TIMEOUT, DEFAULT_AP_CHECK_INTERVAL ) ; }