[Aggregator] Access Point -> Capture Point

Use the new terminology "capture point" (CP) instead of "access point"
(AP) everywhere in Aggregator. The only user-visible change is that the
ap_keep_timeout option is now called cp_keep_timeout.
This commit is contained in:
Matteo Cypriani 2013-06-14 13:55:02 -04:00
parent 91cd819730
commit 0c3083bd41
2 changed files with 182 additions and 182 deletions

View File

@ -32,7 +32,7 @@
#define DEFAULT_AGGREGATE_TIMEOUT 600 // milliseconds #define DEFAULT_AGGREGATE_TIMEOUT 600 // milliseconds
#define DEFAULT_KEEP_TIMEOUT 3000 // milliseconds #define DEFAULT_KEEP_TIMEOUT 3000 // milliseconds
#define DEFAULT_CHECK_INTERVAL 300 // milliseconds #define DEFAULT_CHECK_INTERVAL 300 // milliseconds
#define DEFAULT_AP_KEEP_TIMEOUT 35 // seconds #define DEFAULT_CP_KEEP_TIMEOUT 35 // seconds
#define DEFAULT_AC_ORDER_INTERVAL 1000 // milliseconds #define DEFAULT_AC_ORDER_INTERVAL 1000 // milliseconds
#define POSITIONER_DEFAULT_IP "127.0.0.1" #define POSITIONER_DEFAULT_IP "127.0.0.1"
@ -50,11 +50,11 @@ typedef struct _request_info_list
{ {
// Number of the current packet: // Number of the current packet:
uint16_t packet_id ; uint16_t packet_id ;
// MAC address of the transmitter AP (in bytes): // MAC address of the transmitter CP (in bytes):
uint8_t ap_mac_addr_bytes[ETHER_ADDR_LEN] ; uint8_t cp_mac_addr_bytes[ETHER_ADDR_LEN] ;
// Timestamp of arrival on the listener: // Timestamp of arrival on the listener:
owl_timestamp capture_time ; owl_timestamp capture_time ;
// Signal strength received by the AP from the mobile: // Signal strength received by the CP from the mobile:
int8_t ss_dbm ; int8_t ss_dbm ;
struct _request_info_list *next ; struct _request_info_list *next ;
} request_info_list ; } request_info_list ;
@ -88,17 +88,17 @@ typedef struct _request_list
} request_list ; } request_list ;
/* Linked list of the known APs */ /* Linked list of the known CPs */
typedef struct _ap_list typedef struct _cp_list
{ {
uint8_t mac_addr_bytes[ETHER_ADDR_LEN] ; uint8_t mac_addr_bytes[ETHER_ADDR_LEN] ;
char ip_addr[INET_ADDRSTRLEN] ; char ip_addr[INET_ADDRSTRLEN] ;
owl_timestamp last_seen ; owl_timestamp last_seen ;
struct _ap_list *previous ; struct _cp_list *previous ;
struct _ap_list *next ; struct _cp_list *next ;
} ap_list ; } cp_list ;
/* Function headers */ /* Function headers */
@ -131,21 +131,21 @@ void print_request_list(void) ;
void print_request_info(request_info_list *info) ; void print_request_info(request_info_list *info) ;
#endif // NDEBUG #endif // NDEBUG
void* listen_for_aps(void *NULL_value) ; void* listen_for_cps(void *NULL_value) ;
void update_ap(uint8_t mac_addr_bytes[ETHER_ADDR_LEN], void update_cp(uint8_t mac_addr_bytes[ETHER_ADDR_LEN],
char ip_addr[INET_ADDRSTRLEN]) ; char ip_addr[INET_ADDRSTRLEN]) ;
ap_list* find_ap(uint8_t mac_addr_bytes[ETHER_ADDR_LEN]) ; cp_list* find_cp(uint8_t mac_addr_bytes[ETHER_ADDR_LEN]) ;
ap_list* add_ap_front(uint8_t mac_addr_bytes[ETHER_ADDR_LEN]) ; cp_list* add_cp_front(uint8_t mac_addr_bytes[ETHER_ADDR_LEN]) ;
void update_ap_ip_addr(ap_list *ap, char ip_addr[INET_ADDRSTRLEN]) ; void update_cp_ip_addr(cp_list *cp, char ip_addr[INET_ADDRSTRLEN]) ;
void update_ap_seen(ap_list *ap) ; void update_cp_seen(cp_list *cp) ;
void push_ap(ap_list *ap) ; void push_cp(cp_list *cp) ;
void* monitor_aps(void *NULL_value) ; void* monitor_cps(void *NULL_value) ;
void delete_old_aps(void) ; void delete_old_cps(void) ;
void delete_ap(ap_list *ap) ; void delete_cp(cp_list *cp) ;
void unlink_ap(ap_list *ap) ; void unlink_cp(cp_list *cp) ;
void order_send(ap_list *ap) ; void order_send(cp_list *cp) ;
void free_ap_list(void) ; void free_cp_list(void) ;
void print_usage(void) ; void print_usage(void) ;
void print_version(void) ; void print_version(void) ;

View File

@ -49,9 +49,9 @@ bool dump_configuration = false ;
request_list *requests = NULL ; // Computed data list request_list *requests = NULL ; // Computed data list
sem_t lock_requests ; // Semaphore to get access to the requests sem_t lock_requests ; // Semaphore to get access to the requests
ap_list *token_aps = NULL ; // Token ring of the APs cp_list *token_cps = NULL ; // Token ring of the CPs
uint_fast16_t nb_aps = 0 ; // Number of APs in the APs' ring uint_fast16_t nb_cps = 0 ; // Number of CPs in the CPs' ring
sem_t lock_aps ; // Semaphore to get access to the APs' ring sem_t lock_cps ; // Semaphore to get access to the CPs' ring
@ -61,7 +61,7 @@ int main(int argc, char **argv)
struct sigaction action ; // Signal handler structure struct sigaction action ; // Signal handler structure
pthread_t pthread_t
monitor_thread, // Aggregated data monitoring thread monitor_thread, // Aggregated data monitoring thread
monitor_aps_thread, // APs monitoring thread monitor_cps_thread, // CPs monitoring thread
autocalibration_hello_thread ; // Hello messages reception thread autocalibration_hello_thread ; // Hello messages reception thread
uint_fast16_t listening_port ; uint_fast16_t listening_port ;
int sockfd = -1 ; // UDP listening socket int sockfd = -1 ; // UDP listening socket
@ -91,7 +91,7 @@ int main(int argc, char **argv)
/* Set up semaphores */ /* Set up semaphores */
sem_init(&lock_requests, 0, 1) ; sem_init(&lock_requests, 0, 1) ;
sem_init(&lock_aps, 0, 1) ; sem_init(&lock_cps, 0, 1) ;
/* Create UDP socket */ /* Create UDP socket */
listening_port = cfg_getint(cfg, "listening_port") ; listening_port = cfg_getint(cfg, "listening_port") ;
@ -115,18 +115,18 @@ int main(int argc, char **argv)
if (cfg_getbool(cfg, "autocalibration")) if (cfg_getbool(cfg, "autocalibration"))
{ {
ret = pthread_create(&autocalibration_hello_thread, NULL, ret = pthread_create(&autocalibration_hello_thread, NULL,
&listen_for_aps, NULL) ; &listen_for_cps, NULL) ;
if (ret != 0) if (ret != 0)
{ {
perror("Cannot create autocalibration hello thread") ; perror("Cannot create autocalibration hello thread") ;
ret = OWL_ERR_THREAD_CREATE ; ret = OWL_ERR_THREAD_CREATE ;
goto exit ; goto exit ;
} }
ret = pthread_create(&monitor_aps_thread, NULL, ret = pthread_create(&monitor_cps_thread, NULL,
&monitor_aps, NULL) ; &monitor_cps, NULL) ;
if (ret != 0) if (ret != 0)
{ {
perror("Cannot create monitor APs thread") ; perror("Cannot create monitor CPs thread") ;
ret = OWL_ERR_THREAD_CREATE ; ret = OWL_ERR_THREAD_CREATE ;
goto exit ; goto exit ;
} }
@ -164,11 +164,11 @@ int main(int argc, char **argv)
fprintf(stderr, "Autocalibration hello thread done.\n") ; fprintf(stderr, "Autocalibration hello thread done.\n") ;
if (VERBOSE_WARNING) if (VERBOSE_WARNING)
fprintf(stderr, "Waiting for the monitor APs thread...\n") ; fprintf(stderr, "Waiting for the monitor CPs thread...\n") ;
if (pthread_join(monitor_aps_thread, NULL) != 0) if (pthread_join(monitor_cps_thread, NULL) != 0)
perror("Cannot join monitor APs thread") ; perror("Cannot join monitor CPs thread") ;
else if (VERBOSE_WARNING) else if (VERBOSE_WARNING)
fprintf(stderr, "Monitor APs thread done.\n") ; fprintf(stderr, "Monitor CPs thread done.\n") ;
} }
/* Last cleaning tasks */ /* Last cleaning tasks */
@ -181,11 +181,11 @@ int main(int argc, char **argv)
if (sockfd >= 0) if (sockfd >= 0)
close(sockfd) ; // Close socket close(sockfd) ; // Close socket
assert(requests == NULL) ; assert(requests == NULL) ;
free_ap_list() ; free_cp_list() ;
cfg_free(cfg) ; // Clean configuration cfg_free(cfg) ; // Clean configuration
// Destroy semaphores: // Destroy semaphores:
sem_destroy(&lock_requests) ; sem_destroy(&lock_requests) ;
sem_destroy(&lock_aps) ; sem_destroy(&lock_cps) ;
return ret ; return ret ;
} }
@ -274,8 +274,8 @@ int parse_config_file(int argc, char **argv)
// listeners: // listeners:
CFG_INT("autocalibration_hello_port", CFG_INT("autocalibration_hello_port",
OWL_DEFAULT_AUTOCALIBRATION_HELLO_PORT, CFGF_NONE), OWL_DEFAULT_AUTOCALIBRATION_HELLO_PORT, CFGF_NONE),
// Time we keep APs in the list (in seconds): // Time we keep CPs in the list (in seconds):
CFG_INT("ap_keep_timeout", DEFAULT_AP_KEEP_TIMEOUT, CFGF_NONE), CFG_INT("cp_keep_timeout", DEFAULT_CP_KEEP_TIMEOUT, CFGF_NONE),
// Time between two checks of the CP list (in milliseconds): // Time between two checks of the CP list (in milliseconds):
CFG_INT("ac_order_interval", DEFAULT_AC_ORDER_INTERVAL, CFGF_NONE), CFG_INT("ac_order_interval", DEFAULT_AC_ORDER_INTERVAL, CFGF_NONE),
@ -392,7 +392,7 @@ int parse_command_line(int argc, char **argv)
cfg_setint(cfg, "keep_timeout", strtol(optarg, NULL, 0)) ; cfg_setint(cfg, "keep_timeout", strtol(optarg, NULL, 0)) ;
break ; break ;
case 'K' : case 'K' :
cfg_setint(cfg, "ap_keep_timeout", strtol(optarg, NULL, 0)) ; cfg_setint(cfg, "cp_keep_timeout", strtol(optarg, NULL, 0)) ;
break ; break ;
case 'l' : case 'l' :
cfg_setint(cfg, "listening_port", strtol(optarg, NULL, 0)) ; cfg_setint(cfg, "listening_port", strtol(optarg, NULL, 0)) ;
@ -499,13 +499,13 @@ int check_configuration()
cfg_setint(cfg, "check_interval", DEFAULT_CHECK_INTERVAL) ; cfg_setint(cfg, "check_interval", DEFAULT_CHECK_INTERVAL) ;
} }
// ap_keep_timeout // // cp_keep_timeout //
if (cfg_getint(cfg, "ap_keep_timeout") < 0) if (cfg_getint(cfg, "cp_keep_timeout") < 0)
{ {
if (VERBOSE_WARNING) if (VERBOSE_WARNING)
fprintf(stderr, "Warning! ap_keep_timeout cannot be negative:" fprintf(stderr, "Warning! cp_keep_timeout cannot be negative:"
" failing back to the default value.\n") ; " failing back to the default value.\n") ;
cfg_setint(cfg, "ap_keep_timeout", DEFAULT_AP_KEEP_TIMEOUT) ; cfg_setint(cfg, "cp_keep_timeout", DEFAULT_CP_KEEP_TIMEOUT) ;
} }
// ac_order_interval // // ac_order_interval //
@ -560,7 +560,7 @@ int read_loop(int sockfd)
if (VERBOSE_REQUESTS) if (VERBOSE_REQUESTS)
print_captured_request(&request) ; print_captured_request(&request) ;
else if (VERBOSE_CHATTERBOX) else if (VERBOSE_CHATTERBOX)
fprintf(stderr, "Request received from AP \"%s\".\n", fprintf(stderr, "Request received from CP \"%s\".\n",
owl_mac_bytes_to_string(request.ap_mac_addr_bytes)) ; owl_mac_bytes_to_string(request.ap_mac_addr_bytes)) ;
got_request(&request) ; got_request(&request) ;
@ -578,7 +578,7 @@ void print_captured_request(const owl_captured_request *const request)
char char
request_time_str[OWL_TIMESTAMP_STRLEN], request_time_str[OWL_TIMESTAMP_STRLEN],
capture_time_str[OWL_TIMESTAMP_STRLEN], capture_time_str[OWL_TIMESTAMP_STRLEN],
ap_mac_addr_str[OWL_ETHER_ADDR_STRLEN], cp_mac_addr_str[OWL_ETHER_ADDR_STRLEN],
mobile_mac_addr_str[OWL_ETHER_ADDR_STRLEN], mobile_mac_addr_str[OWL_ETHER_ADDR_STRLEN],
mobile_ip_str[INET_ADDRSTRLEN] ; mobile_ip_str[INET_ADDRSTRLEN] ;
@ -587,7 +587,7 @@ void print_captured_request(const owl_captured_request *const request)
owl_timestamp_to_string(&request->request_time, request_time_str) ; owl_timestamp_to_string(&request->request_time, request_time_str) ;
owl_timestamp_to_string(&request->capture_time, capture_time_str) ; owl_timestamp_to_string(&request->capture_time, capture_time_str) ;
owl_mac_bytes_to_string_r(request->ap_mac_addr_bytes, owl_mac_bytes_to_string_r(request->ap_mac_addr_bytes,
ap_mac_addr_str) ; cp_mac_addr_str) ;
owl_mac_bytes_to_string_r(request->mobile_mac_addr_bytes, owl_mac_bytes_to_string_r(request->mobile_mac_addr_bytes,
mobile_mac_addr_str) ; mobile_mac_addr_str) ;
inet_ntop(AF_INET, &request->mobile_ip_addr_bytes, inet_ntop(AF_INET, &request->mobile_ip_addr_bytes,
@ -595,13 +595,13 @@ void print_captured_request(const owl_captured_request *const request)
fprintf(stderr, fprintf(stderr,
"\n" "\n"
"*** Request received from AP ***\n" "*** Request received from CP ***\n"
"\tType: %"PRIu8"\n" "\tType: %"PRIu8"\n"
"\tAP MAC: %s\n" "\tCP MAC: %s\n"
"\tMobile MAC: %s\n" "\tMobile MAC: %s\n"
"\tMobile IP: %s\n" "\tMobile IP: %s\n"
"\tRequest timestamp: %s\n" "\tRequest timestamp: %s\n"
"\tRequest arrival time on the AP: %s\n" "\tRequest arrival time on the CP: %s\n"
"\tSignal: %"PRId8" dBm\n" "\tSignal: %"PRId8" dBm\n"
"\tPosition X: %f\n" "\tPosition X: %f\n"
"\tPosition Y: %f\n" "\tPosition Y: %f\n"
@ -610,7 +610,7 @@ void print_captured_request(const owl_captured_request *const request)
"\tPacket number: %"PRIu16"/%"PRIu16"\n" "\tPacket number: %"PRIu16"/%"PRIu16"\n"
, ,
request->type, request->type,
ap_mac_addr_str, cp_mac_addr_str,
mobile_mac_addr_str, mobile_mac_addr_str,
mobile_ip_str, mobile_ip_str,
request_time_str, request_time_str,
@ -648,7 +648,7 @@ void got_request(const owl_captured_request *const request)
} }
tmp_info->packet_id = request->packet_id ; tmp_info->packet_id = request->packet_id ;
memcpy(tmp_info->ap_mac_addr_bytes, request->ap_mac_addr_bytes, memcpy(tmp_info->cp_mac_addr_bytes, request->ap_mac_addr_bytes,
ETHER_ADDR_LEN) ; ETHER_ADDR_LEN) ;
tmp_info->capture_time = request->capture_time ; tmp_info->capture_time = request->capture_time ;
tmp_info->ss_dbm = request->ss_dbm ; tmp_info->ss_dbm = request->ss_dbm ;
@ -687,7 +687,7 @@ void add_captured_request(const owl_timestamp *const reception_time,
if (requests == NULL) // If the request list does not exist, if (requests == NULL) // If the request list does not exist,
{ {
if (VERBOSE_INFO) if (VERBOSE_INFO)
fprintf(stderr, "Creating request list with AP \"%s\".\n", fprintf(stderr, "Creating request list with CP \"%s\".\n",
owl_mac_bytes_to_string(request->ap_mac_addr_bytes)) ; owl_mac_bytes_to_string(request->ap_mac_addr_bytes)) ;
tmp_request = malloc(sizeof(request_list)) ; // create it. tmp_request = malloc(sizeof(request_list)) ; // create it.
if (! tmp_request) if (! tmp_request)
@ -708,10 +708,10 @@ void add_captured_request(const owl_timestamp *const reception_time,
tmp_request->request_time = request->request_time ; tmp_request->request_time = request->request_time ;
// Implicit packet: // Implicit packet:
else else
// Reception time on the AP: // Reception time on the CP:
tmp_request->request_time = request->capture_time ; tmp_request->request_time = request->capture_time ;
// Save locale time on the aggregator (not the reception time // Save locale time on the aggregator (not the reception time
// on the AP): // on the CP):
tmp_request->start_time = *reception_time ; tmp_request->start_time = *reception_time ;
tmp_request->x_position = request->x_position ; tmp_request->x_position = request->x_position ;
tmp_request->y_position = request->y_position ; tmp_request->y_position = request->y_position ;
@ -742,7 +742,7 @@ void add_captured_request(const owl_timestamp *const reception_time,
{ {
while (tmp_request != NULL) while (tmp_request != NULL)
{ // Research criterion: MAC addresses equals and reception { // Research criterion: MAC addresses equals and reception
// times on the APs less than 10 ms // times on the CPs less than 10 ms
// TODO : define an option for the maximal difference time. // TODO : define an option for the maximal difference time.
if (owl_mac_equals(request->mobile_mac_addr_bytes, if (owl_mac_equals(request->mobile_mac_addr_bytes,
tmp_request->mobile_mac_addr_bytes) && tmp_request->mobile_mac_addr_bytes) &&
@ -756,7 +756,7 @@ void add_captured_request(const owl_timestamp *const reception_time,
if (tmp_request == NULL) // The request does not exist in the list if (tmp_request == NULL) // The request does not exist in the list
{ {
if (VERBOSE_INFO) if (VERBOSE_INFO)
fprintf(stderr, "Create new request from AP \"%s\".\n", fprintf(stderr, "Create new request from CP \"%s\".\n",
owl_mac_bytes_to_string(request->ap_mac_addr_bytes)) ; owl_mac_bytes_to_string(request->ap_mac_addr_bytes)) ;
tmp_request = malloc(sizeof(request_list)) ; // create it tmp_request = malloc(sizeof(request_list)) ; // create it
if (! tmp_request) if (! tmp_request)
@ -777,10 +777,10 @@ void add_captured_request(const owl_timestamp *const reception_time,
tmp_request->request_time = request->request_time ; tmp_request->request_time = request->request_time ;
// Implicit packet: // Implicit packet:
else else
// Reception time on the AP: // Reception time on the CP:
tmp_request->request_time = request->capture_time ; tmp_request->request_time = request->capture_time ;
// Save the local time on the aggregator (not the reception // Save the local time on the aggregator (not the reception
// time on the AP): // time on the CP):
tmp_request->start_time = *reception_time ; tmp_request->start_time = *reception_time ;
tmp_request->x_position = request->x_position ; tmp_request->x_position = request->x_position ;
tmp_request->y_position = request->y_position ; tmp_request->y_position = request->y_position ;
@ -1058,7 +1058,7 @@ void output_request(request_list *const request_ptr,
// Send CP info to the localisation server // Send CP info to the localisation server
info.packet_id = htons(request_info_ptr->packet_id) ; info.packet_id = htons(request_info_ptr->packet_id) ;
memcpy(info.ap_mac_addr_bytes, memcpy(info.ap_mac_addr_bytes,
request_info_ptr->ap_mac_addr_bytes, ETHER_ADDR_LEN) ; request_info_ptr->cp_mac_addr_bytes, ETHER_ADDR_LEN) ;
info.capture_time = request_info_ptr->capture_time ; info.capture_time = request_info_ptr->capture_time ;
owl_hton_timestamp(&info.capture_time) ; owl_hton_timestamp(&info.capture_time) ;
info.ss_dbm = request_info_ptr->ss_dbm ; info.ss_dbm = request_info_ptr->ss_dbm ;
@ -1066,7 +1066,7 @@ void output_request(request_list *const request_ptr,
0, (const struct sockaddr *const)serv, serv_len) ; 0, (const struct sockaddr *const)serv, serv_len) ;
// Print CP info to the output file // Print CP info to the output file
owl_mac_bytes_to_string_r(request_info_ptr->ap_mac_addr_bytes, owl_mac_bytes_to_string_r(request_info_ptr->cp_mac_addr_bytes,
mac_str) ; mac_str) ;
fprintf(stream, ";%s;%"PRIu16";%"PRId8, fprintf(stream, ";%s;%"PRIu16";%"PRId8,
mac_str, mac_str,
@ -1151,15 +1151,15 @@ void print_request_list()
*/ */
void print_request_info(request_info_list *info) void print_request_info(request_info_list *info)
{ {
char ap_mac_str[OWL_ETHER_ADDR_STRLEN] ; char cp_mac_str[OWL_ETHER_ADDR_STRLEN] ;
if (info == NULL) if (info == NULL)
return ; return ;
owl_mac_bytes_to_string_r(info->ap_mac_addr_bytes, ap_mac_str) ; owl_mac_bytes_to_string_r(info->ap_mac_addr_bytes, cp_mac_str) ;
fprintf(stderr, fprintf(stderr,
"\tAP MAC: %s\n" "\tCP MAC: %s\n"
"\tSignal strength: %"PRId8" dBm\n", "\tSignal strength: %"PRId8" dBm\n",
ap_mac_str, cp_mac_str,
info->ss_dbm info->ss_dbm
) ; ) ;
} }
@ -1168,16 +1168,16 @@ void print_request_info(request_info_list *info)
/* /*
* Thread function. Listens for hello messages from APs. * Thread function. Listens for hello messages from CPs.
*/ */
void* listen_for_aps(void *NULL_value) void* listen_for_cps(void *NULL_value)
{ {
int listen_sockfd ; int listen_sockfd ;
int nread ; // recvfrom return value int nread ; // recvfrom return value
struct sockaddr_in client; // UDP client structure struct sockaddr_in client; // UDP client structure
socklen_t client_len = sizeof(client) ; // Size of clients socklen_t client_len = sizeof(client) ; // Size of clients
owl_autocalibration_hello message ; owl_autocalibration_hello message ;
char ap_ip_addr[INET_ADDRSTRLEN] ; char cp_ip_addr[INET_ADDRSTRLEN] ;
if (VERBOSE_WARNING) if (VERBOSE_WARNING)
fprintf(stderr, "Autocalibration Hello thread launched.\n") ; fprintf(stderr, "Autocalibration Hello thread launched.\n") ;
@ -1205,15 +1205,15 @@ void* listen_for_aps(void *NULL_value)
continue ; continue ;
} }
inet_ntop(AF_INET, &client.sin_addr, ap_ip_addr, INET_ADDRSTRLEN) ; inet_ntop(AF_INET, &client.sin_addr, cp_ip_addr, INET_ADDRSTRLEN) ;
if (VERBOSE_INFO) if (VERBOSE_INFO)
fprintf(stderr, fprintf(stderr,
"Got a Hello message from \"%s\"\n", ap_ip_addr) ; "Got a Hello message from \"%s\"\n", cp_ip_addr) ;
sem_wait(&lock_aps) ; sem_wait(&lock_cps) ;
update_ap(message.ap_mac_addr_bytes, ap_ip_addr) ; update_cp(message.ap_mac_addr_bytes, cp_ip_addr) ;
sem_post(&lock_aps) ; sem_post(&lock_cps) ;
} }
/* Close the socket */ /* Close the socket */
@ -1224,150 +1224,150 @@ void* listen_for_aps(void *NULL_value)
/* /*
* Updates the timestamp of the AP with the given MAC address if it is in * Updates the timestamp of the CP 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. * the CP list, or add a new CP with this MAC address to the CP list.
*/ */
void update_ap(uint8_t mac_addr_bytes[ETHER_ADDR_LEN], void update_cp(uint8_t mac_addr_bytes[ETHER_ADDR_LEN],
char ip_addr[INET_ADDRSTRLEN]) char ip_addr[INET_ADDRSTRLEN])
{ {
ap_list *found ; cp_list *found ;
if ((found = find_ap(mac_addr_bytes)) == NULL) if ((found = find_cp(mac_addr_bytes)) == NULL)
{ {
ap_list *new_ap = add_ap_front(mac_addr_bytes) ; cp_list *new_cp = add_cp_front(mac_addr_bytes) ;
if (new_ap) if (new_cp)
update_ap_ip_addr(new_ap, ip_addr) ; update_cp_ip_addr(new_cp, ip_addr) ;
} }
else else
update_ap_seen(found) ; update_cp_seen(found) ;
} }
/* /*
* Searches the AP list for an AP with the given MAC address and returns * Searches the CP list for a CP with the given MAC address and returns
* it. * it.
*/ */
ap_list* find_ap(uint8_t mac_addr_bytes[ETHER_ADDR_LEN]) cp_list* find_cp(uint8_t mac_addr_bytes[ETHER_ADDR_LEN])
{ {
ap_list *found ; cp_list *found ;
if (token_aps == NULL) if (token_cps == NULL)
return NULL ; return NULL ;
found = token_aps ; found = token_cps ;
do do
{ {
if (owl_mac_equals(found->mac_addr_bytes, mac_addr_bytes)) if (owl_mac_equals(found->mac_addr_bytes, mac_addr_bytes))
return found ; return found ;
found = found->next ; found = found->next ;
} }
while (found != token_aps) ; while (found != token_cps) ;
return NULL ; return NULL ;
} }
/* /*
* Adds a new AP in front of the AP list. * Adds a new CP in front of the CP list.
* Returns the new list head, or NULL in case of error. * Returns the new list head, or NULL in case of error.
*/ */
ap_list* add_ap_front(uint8_t mac_addr_bytes[ETHER_ADDR_LEN]) cp_list* add_cp_front(uint8_t mac_addr_bytes[ETHER_ADDR_LEN])
{ {
if (VERBOSE_INFO) if (VERBOSE_INFO)
{ {
char mac_str[OWL_ETHER_ADDR_STRLEN] ; char mac_str[OWL_ETHER_ADDR_STRLEN] ;
owl_mac_bytes_to_string_r(mac_addr_bytes, mac_str) ; owl_mac_bytes_to_string_r(mac_addr_bytes, mac_str) ;
fprintf(stderr, fprintf(stderr,
"Creating AP with MAC address \"%s\"...\n", mac_str) ; "Creating CP with MAC address \"%s\"...\n", mac_str) ;
} }
ap_list *ap = malloc(sizeof(ap_list)) ; cp_list *cp = malloc(sizeof(cp_list)) ;
if (! ap) if (! cp)
{ {
perror("Cannot allocate memory") ; perror("Cannot allocate memory") ;
owl_run = false ; owl_run = false ;
return NULL ; return NULL ;
} }
memcpy(ap->mac_addr_bytes, mac_addr_bytes, ETHER_ADDR_LEN) ; memcpy(cp->mac_addr_bytes, mac_addr_bytes, ETHER_ADDR_LEN) ;
update_ap_seen(ap) ; update_cp_seen(cp) ;
push_ap(ap) ; push_cp(cp) ;
return ap ; return cp ;
} }
/* /*
* Change the IP address of the AP 'ap' with 'ip_addr'. * Change the IP address of the CP 'cp' with 'ip_addr'.
*/ */
void update_ap_ip_addr(ap_list *ap, char ip_addr[INET_ADDRSTRLEN]) void update_cp_ip_addr(cp_list *cp, char ip_addr[INET_ADDRSTRLEN])
{ {
strncpy(ap->ip_addr, ip_addr, INET_ADDRSTRLEN) ; strncpy(cp->ip_addr, ip_addr, INET_ADDRSTRLEN) ;
} }
/* /*
* Updates the timestamp of the given AP. * Updates the timestamp of the given CP.
*/ */
void update_ap_seen(ap_list *ap) void update_cp_seen(cp_list *cp)
{ {
assert(ap) ; assert(cp) ;
owl_timestamp_now(&ap->last_seen) ; owl_timestamp_now(&cp->last_seen) ;
} }
/* /*
* Puts an existing AP in front of the AP list. The AP must not be in * Puts an existing CP in front of the CP list. The CP must not be in
* the list yet. * the list yet.
*/ */
void push_ap(ap_list *ap) void push_cp(cp_list *cp)
{ {
assert(ap) ; assert(cp) ;
++nb_aps ; ++nb_cps ;
if (token_aps == NULL) // List does not exist yet if (token_cps == NULL) // List does not exist yet
{ {
token_aps = ap ; token_cps = cp ;
ap->next = ap ; cp->next = cp ;
ap->previous = ap ; cp->previous = cp ;
return ; return ;
} }
ap->previous = token_aps->previous ; cp->previous = token_cps->previous ;
ap->previous->next = ap ; cp->previous->next = cp ;
ap->next = token_aps ; cp->next = token_cps ;
token_aps->previous = ap ; token_cps->previous = cp ;
token_aps = ap ; token_cps = cp ;
} }
/* /*
* Monitors the AP list: sends orders to APs following their order in * Monitors the CP list: sends orders to CPs following their order in
* the list, and deletes old APs. * the list, and deletes old CPs.
*/ */
void* monitor_aps(void *NULL_value) void* monitor_cps(void *NULL_value)
{ {
if (VERBOSE_WARNING) if (VERBOSE_WARNING)
fprintf(stderr, "Monitor AP thread launched.\n") ; fprintf(stderr, "Monitor CP thread launched.\n") ;
while (owl_run) while (owl_run)
{ {
sem_wait(&lock_aps) ; sem_wait(&lock_cps) ;
delete_old_aps() ; delete_old_cps() ;
sem_post(&lock_aps) ; sem_post(&lock_cps) ;
// Here we're not in a hurry, so we released the semaphore to // Here we're not in a hurry, so we released the semaphore to
// allow listen_for_aps() to process a received hello packet, // allow listen_for_cps() to process a received hello packet,
// if needed. // if needed.
sem_wait(&lock_aps) ; sem_wait(&lock_cps) ;
if (nb_aps > 1) if (nb_cps > 1)
{ {
order_send(token_aps) ; order_send(token_cps) ;
token_aps = token_aps->next ; token_cps = token_cps->next ;
} }
sem_post(&lock_aps) ; sem_post(&lock_cps) ;
owl_msleep(cfg_getint(cfg, "ac_order_interval")) ; owl_msleep(cfg_getint(cfg, "ac_order_interval")) ;
} }
@ -1377,77 +1377,77 @@ void* monitor_aps(void *NULL_value)
/* /*
* Deletes APs that did not send any Hello packet for a while, following * Deletes CPs that did not send any Hello packet for a while, following
* the list order. Stops on the first not-to-be-deleted AP. * the list order. Stops on the first not-to-be-deleted CP.
*/ */
void delete_old_aps() void delete_old_cps()
{ {
owl_timestamp now ; owl_timestamp now ;
owl_timestamp_now(&now) ; owl_timestamp_now(&now) ;
while (token_aps != NULL) while (token_cps != NULL)
if (owl_time_elapsed_ms(&token_aps->last_seen, &now) > if (owl_time_elapsed_ms(&token_cps->last_seen, &now) >
(uint_fast32_t) cfg_getint(cfg, "ap_keep_timeout") * 1000) (uint_fast32_t) cfg_getint(cfg, "cp_keep_timeout") * 1000)
delete_ap(token_aps) ; delete_cp(token_cps) ;
else else
return ; return ;
} }
/* /*
* Deletes the given AP from the AP list. * Deletes the given CP from the CP list.
*/ */
void delete_ap(ap_list *ap) void delete_cp(cp_list *cp)
{ {
if (VERBOSE_INFO) if (VERBOSE_INFO)
{ {
char mac_str[OWL_ETHER_ADDR_STRLEN] ; char mac_str[OWL_ETHER_ADDR_STRLEN] ;
assert(ap) ; assert(cp) ;
owl_mac_bytes_to_string_r(ap->mac_addr_bytes, mac_str) ; owl_mac_bytes_to_string_r(cp->mac_addr_bytes, mac_str) ;
fprintf(stderr, "Deleting AP \"%s\"...\n", mac_str) ; fprintf(stderr, "Deleting CP \"%s\"...\n", mac_str) ;
} }
unlink_ap(ap) ; unlink_cp(cp) ;
free(ap) ; free(cp) ;
} }
/* /*
* Extracts the given AP from the AP list (it will not be linked to any * Extracts the given CP from the CP list (it will not be linked to any
* other element of the list). * other element of the list).
*/ */
void unlink_ap(ap_list *ap) void unlink_cp(cp_list *cp)
{ {
ap_list cp_list
*ap_previous, *cp_previous,
*ap_next ; *cp_next ;
assert(ap) ; assert(cp) ;
ap_previous = ap->previous ; cp_previous = cp->previous ;
ap_next = ap->next ; cp_next = cp->next ;
assert(ap_previous) ; assert(cp_previous) ;
assert(ap_next) ; assert(cp_next) ;
ap_previous->next = ap_next ; cp_previous->next = cp_next ;
ap_next->previous = ap_previous ; cp_next->previous = cp_previous ;
if (ap == token_aps) if (cp == token_cps)
{ {
if (ap->next == ap) // It was the last AP in the ring if (cp->next == cp) // It was the last CP in the ring
token_aps = NULL ; token_cps = NULL ;
else else
token_aps = ap_next ; token_cps = cp_next ;
} }
--nb_aps ; --nb_cps ;
} }
/* /*
* Sends a 'send' order to the given AP. * Sends a 'send' order to the given CP.
*/ */
void order_send(ap_list *ap) void order_send(cp_list *cp)
{ {
owl_autocalibration_order message ; owl_autocalibration_order message ;
struct sockaddr_in serv; struct sockaddr_in serv;
@ -1457,10 +1457,10 @@ void order_send(ap_list *ap)
ssize_t nsent ; ssize_t nsent ;
if (VERBOSE_INFO) if (VERBOSE_INFO)
fprintf(stderr, "Sending an order to %s...\n", ap->ip_addr) ; fprintf(stderr, "Sending an order to %s...\n", cp->ip_addr) ;
sockfd = sockfd =
owl_create_udp_trx_socket(ap->ip_addr, owl_create_udp_trx_socket(cp->ip_addr,
cfg_getint(cfg, cfg_getint(cfg,
"autocalibration_order_port"), "autocalibration_order_port"),
&serv, &client) ; &serv, &client) ;
@ -1479,29 +1479,29 @@ void order_send(ap_list *ap)
/* /*
* Empties the AP list. * Empties the CP list.
* Note that this function does not use lock_aps, so it should not * Note that this function does not use lock_cps, so it should not
* be called in a concurrent context. * be called in a concurrent context.
*/ */
void free_ap_list() void free_cp_list()
{ {
ap_list *ap_ptr ; cp_list *cp_ptr ;
if (token_aps == NULL) if (token_cps == NULL)
return ; return ;
ap_ptr = token_aps->next ; cp_ptr = token_cps->next ;
assert(ap_ptr) ; assert(cp_ptr) ;
while (ap_ptr != token_aps) while (cp_ptr != token_cps)
{ {
ap_list *ap_tmp = ap_ptr ; cp_list *cp_tmp = cp_ptr ;
ap_ptr = ap_ptr->next ; cp_ptr = cp_ptr->next ;
free(ap_tmp) ; free(cp_tmp) ;
} }
free(token_aps) ; free(token_cps) ;
token_aps = NULL ; token_cps = NULL ;
} }
@ -1528,7 +1528,7 @@ void print_usage()
" [-A]" " [-A]"
" [-a autocalibration_port]" " [-a autocalibration_port]"
"\n\t" "\n\t"
" [-K ap_keep_timeout]" " [-K cp_keep_timeout]"
" [-C ac_order_interval]" " [-C ac_order_interval]"
"\n" "\n"
"\t%s -h\n" "\t%s -h\n"
@ -1579,8 +1579,8 @@ void print_usage()
" sent to\n\t\t\t\tthe listeners (default: %d).\n" " sent to\n\t\t\t\tthe listeners (default: %d).\n"
"\t-H port\t\t\tPort on which autocalibration hello are" "\t-H port\t\t\tPort on which autocalibration hello are"
" received\n\t\t\t\tfrom the listeners (default: %d).\n" " received\n\t\t\t\tfrom the listeners (default: %d).\n"
"\t-K ap_keep_timeout\tInactive APs are kept during" "\t-K cp_keep_timeout\tInactive CPs are kept during"
" 'ap_keep_timeout'\n\t\t\t\tseconds (default: %d s).\n" " 'cp_keep_timeout'\n\t\t\t\tseconds (default: %d s).\n"
"\t-C ac_order_interval\tTime (in milliseconds) between two" "\t-C ac_order_interval\tTime (in milliseconds) between two"
" transmissions\n\t\t\t\tof autocalibration orders to the" " transmissions\n\t\t\t\tof autocalibration orders to the"
" stored CPs\n\t\t\t\t(default: %d ms).\n" " stored CPs\n\t\t\t\t(default: %d ms).\n"
@ -1597,7 +1597,7 @@ void print_usage()
DEFAULT_CHECK_INTERVAL, DEFAULT_CHECK_INTERVAL,
OWL_DEFAULT_AUTOCALIBRATION_ORDER_PORT, OWL_DEFAULT_AUTOCALIBRATION_ORDER_PORT,
OWL_DEFAULT_AUTOCALIBRATION_HELLO_PORT, OWL_DEFAULT_AUTOCALIBRATION_HELLO_PORT,
DEFAULT_AP_KEEP_TIMEOUT, DEFAULT_CP_KEEP_TIMEOUT,
DEFAULT_AC_ORDER_INTERVAL DEFAULT_AC_ORDER_INTERVAL
) ; ) ;
} }