From 45d4eeb7919b3f8ec30f19811f381118d45b0a31 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Wed, 12 Jun 2013 15:30:56 -0400 Subject: [PATCH] [Aggregator] Reorder functions Reorder the functions in owlps-aggregatord.c to respect the order of owlps-aggregator.h. --- owlps-aggregator/owlps-aggregatord.c | 486 +++++++++++++-------------- 1 file changed, 241 insertions(+), 245 deletions(-) diff --git a/owlps-aggregator/owlps-aggregatord.c b/owlps-aggregator/owlps-aggregatord.c index 3030b5e..9532f72 100644 --- a/owlps-aggregator/owlps-aggregatord.c +++ b/owlps-aggregator/owlps-aggregatord.c @@ -619,6 +619,166 @@ int read_loop(int sockfd) } +/* + * 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)) ; + if (! tmp_info) + { + perror("Cannot allocate memory") ; + owl_run = false ; + return ; + } + + tmp_info->packet_id = request.packet_id ; + memcpy(tmp_info->ap_mac_addr_bytes, request.ap_mac_addr_bytes, + ETHER_ADDR_LEN) ; + tmp_info->capture_time = request.capture_time ; + tmp_info->ss_dbm = request.ss_dbm ; + tmp_info->next = NULL ; + + /* Add it in the list */ + sem_wait(&lock_requests) ; + + tmp_request = requests ; + if (requests == NULL) // If the request list does not exist, + { + if (VERBOSE_INFO) + fprintf(stderr, "Creating request list with AP \"%s\".\n", + owl_mac_bytes_to_string(request.ap_mac_addr_bytes)) ; + tmp_request = malloc(sizeof(request_list)) ; // create it. + if (! tmp_request) + { + perror("Cannot allocate memory") ; + owl_run = false ; + goto end ; + } + tmp_request->type = request.type ; + tmp_request->nb_packets = request.nb_packets ; + memcpy(tmp_request->mobile_mac_addr_bytes, + request.mobile_mac_addr_bytes, ETHER_ADDR_LEN) ; + memcpy(tmp_request->mobile_ip_addr_bytes, + request.mobile_ip_addr_bytes, 4) ; + // 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.capture_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.capture_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 + { + if (VERBOSE_INFO) + fprintf(stderr, "Create new request from AP \"%s\".\n", + owl_mac_bytes_to_string(request.ap_mac_addr_bytes)) ; + tmp_request = malloc(sizeof(request_list)) ; // create it + if (! tmp_request) + { + perror("Cannot allocate memory") ; + owl_run = false ; + goto end ; + } + tmp_request->type = request.type ; + tmp_request->nb_packets = request.nb_packets ; + memcpy(tmp_request->mobile_mac_addr_bytes, + request.mobile_mac_addr_bytes, ETHER_ADDR_LEN) ; + memcpy(tmp_request->mobile_ip_addr_bytes, + request.mobile_ip_addr_bytes, 4) ; + // 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.capture_time ; + // Save the local 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 + if (VERBOSE_CHATTERBOX) + fprintf(stderr, "Request already treated.\n") ; + free(tmp_info) ; + } + else + { + if (VERBOSE_CHATTERBOX) + fprintf(stderr, "Add information to the request.\n") ; + tmp_info->next = tmp_request->info ; // Add data + tmp_request->info = tmp_info ; + } + } + } + + end: + sem_post(&lock_requests) ; +} + + /* * Thread function. Monitors the list and sends information to the @@ -830,167 +990,6 @@ void* monitor_requests(void *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)) ; - if (! tmp_info) - { - perror("Cannot allocate memory") ; - owl_run = false ; - return ; - } - - tmp_info->packet_id = request.packet_id ; - memcpy(tmp_info->ap_mac_addr_bytes, request.ap_mac_addr_bytes, - ETHER_ADDR_LEN) ; - tmp_info->capture_time = request.capture_time ; - tmp_info->ss_dbm = request.ss_dbm ; - tmp_info->next = NULL ; - - /* Add it in the list */ - sem_wait(&lock_requests) ; - - tmp_request = requests ; - if (requests == NULL) // If the request list does not exist, - { - if (VERBOSE_INFO) - fprintf(stderr, "Creating request list with AP \"%s\".\n", - owl_mac_bytes_to_string(request.ap_mac_addr_bytes)) ; - tmp_request = malloc(sizeof(request_list)) ; // create it. - if (! tmp_request) - { - perror("Cannot allocate memory") ; - owl_run = false ; - goto end ; - } - tmp_request->type = request.type ; - tmp_request->nb_packets = request.nb_packets ; - memcpy(tmp_request->mobile_mac_addr_bytes, - request.mobile_mac_addr_bytes, ETHER_ADDR_LEN) ; - memcpy(tmp_request->mobile_ip_addr_bytes, - request.mobile_ip_addr_bytes, 4) ; - // 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.capture_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.capture_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 - { - if (VERBOSE_INFO) - fprintf(stderr, "Create new request from AP \"%s\".\n", - owl_mac_bytes_to_string(request.ap_mac_addr_bytes)) ; - tmp_request = malloc(sizeof(request_list)) ; // create it - if (! tmp_request) - { - perror("Cannot allocate memory") ; - owl_run = false ; - goto end ; - } - tmp_request->type = request.type ; - tmp_request->nb_packets = request.nb_packets ; - memcpy(tmp_request->mobile_mac_addr_bytes, - request.mobile_mac_addr_bytes, ETHER_ADDR_LEN) ; - memcpy(tmp_request->mobile_ip_addr_bytes, - request.mobile_ip_addr_bytes, 4) ; - // 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.capture_time ; - // Save the local 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 - if (VERBOSE_CHATTERBOX) - fprintf(stderr, "Request already treated.\n") ; - free(tmp_info) ; - } - else - { - if (VERBOSE_CHATTERBOX) - fprintf(stderr, "Add information to the request.\n") ; - tmp_info->next = tmp_request->info ; // Add data - tmp_request->info = tmp_info ; - } - } - } - - end: - sem_post(&lock_requests) ; -} - - - /* * Empties the request list. * Note that this function does not use lock_requests, so it should not @@ -1016,6 +1015,87 @@ void free_request_list() } +#ifndef NDEBUG +/* + * Prints the request list. + */ +void print_request_list() +{ + request_list *request_ptr = NULL ; + request_info_list *info_ptr = NULL ; + char mobile_mac_str[OWL_ETHER_ADDR_STRLEN] ; + char + request_time_str[OWL_TIMESTAMP_STRLEN], + start_time_str[OWL_TIMESTAMP_STRLEN] ; + + sem_wait(&lock_requests) ; + + if (requests == NULL) // Empty list + { + fprintf(stderr, "No request.\n") ; + return ; + } + + request_ptr = requests ; + 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_ptr->request_time, + request_time_str) ; + owl_timestamp_to_string(&request_ptr->start_time, + start_time_str) ; + 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 ; + } + + sem_post(&lock_requests) ; +} + + +/* + * 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: %"PRId8" dBm\n", + ap_mac_str, + info->ss_dbm + ) ; +} +#endif // NDEBUG + + /* * Thread function. Listens for hello messages from APs. @@ -1073,7 +1153,6 @@ void* listen_for_aps(void *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. @@ -1357,89 +1436,6 @@ void free_ap_list() -#ifndef NDEBUG -/* - * Prints the request list. - */ -void print_request_list() -{ - request_list *request_ptr = NULL ; - request_info_list *info_ptr = NULL ; - char mobile_mac_str[OWL_ETHER_ADDR_STRLEN] ; - char - request_time_str[OWL_TIMESTAMP_STRLEN], - start_time_str[OWL_TIMESTAMP_STRLEN] ; - - sem_wait(&lock_requests) ; - - if (requests == NULL) // Empty list - { - fprintf(stderr, "No request.\n") ; - return ; - } - - request_ptr = requests ; - 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_ptr->request_time, - request_time_str) ; - owl_timestamp_to_string(&request_ptr->start_time, - start_time_str) ; - 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 ; - } - - sem_post(&lock_requests) ; -} - - - -/* - * 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: %"PRId8" dBm\n", - ap_mac_str, - info->ss_dbm - ) ; -} -#endif // NDEBUG - - - void print_usage() { printf("Usage:\n"