owlps/libowlps/owlps.h

315 lines
11 KiB
C

/*
* This file is part of the Owl Positioning System (OwlPS).
* OwlPS is a project of the University of Franche-Comte
* (Université de Franche-Comté), France.
*
* Copyright © Université de Franche-Comté 2007-2012.
*
* Corresponding author: Matteo Cypriani <mcy@lm7.fr>
*
***********************************************************************
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL:
* http://www.cecill.info
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided
* only with a limited warranty and the software's authors, the holder
* of the economic rights, and the successive licensors have only
* limited liability.
*
* In this respect, the user's attention is drawn to the risks
* associated with loading, using, modifying and/or developing or
* reproducing the software by the user in light of its specific status
* of free software, that may mean that it is complicated to manipulate,
* and that also therefore means that it is reserved for developers and
* experienced professionals having in-depth computer knowledge. Users
* are therefore encouraged to load and test the software's suitability
* as regards their requirements in conditions enabling the security of
* their systems and/or data to be ensured and, more generally, to use
* and operate it in the same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*
***********************************************************************
*
* This is the header file of libowlps, the library containing the code
* shared by most of the OwlPS programs.
*/
#ifndef _LIBOWLPS_H_
#define _LIBOWLPS_H_
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#include <stdint.h> // We'll use <cstdint> with C++ 0x
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
// Port on which the positioning and calibration requests are sent by
// the mobiles (or the listeners for autocalibration requests):
#define OWL_DEFAULT_REQUEST_PORT 9900
// Port on which listeners and aggregator communicate:
#define OWL_DEFAULT_LISTENER_PORT 9901
// Port on which Aggregator and Positioner communicate:
#define OWL_DEFAULT_AGGREGATION_PORT 9902
// Port on which the aggregator listens for hello messages from the
// listeners:
#define OWL_DEFAULT_AUTOCALIBRATION_HELLO_PORT 9903
// Port on which the listeners listen for orders from the aggregator:
#define OWL_DEFAULT_AUTOCALIBRATION_ORDER_PORT 9904
// Port on which the mobile listens for its position:
#define OWL_DEFAULT_RESULT_PORT 9910
/* Boolean type */
typedef enum {owl_false, owl_true} owl_bool ;
#define OWL_BOOL_TO_STRING(B) ((B) ? "true" : "false")
/* Direction type */
enum {owl_north = 1, owl_east, owl_south, owl_west} ;
#define OWL_DIRECTION_MIN 1
#define OWL_DIRECTION_MAX 4
typedef uint8_t owl_direction ;
/* Timestamp type (struct timespec clone with fix-sized fields) */
typedef struct _owl_timestamp
{
uint32_t tv_sec ;
uint32_t tv_nsec ;
} owl_timestamp ;
// Length of a owl_timestamp when converted to string:
#define OWL_TIMESTAMP_STRLEN 22 // 22 = 10 digits, '.', 10 digits, '\0'
/* Message sent by the listener to the aggregator */
typedef struct _owl_captured_request
{
uint8_t type ; // Type of the captured request
uint16_t nb_packets ; // Number of packets for this request
uint16_t packet_id ; // Number of the current packet
uint8_t ap_mac_addr_bytes[ETHER_ADDR_LEN] ; // MAC of the listener
uint8_t mobile_mac_addr_bytes[ETHER_ADDR_LEN] ; // MAC of the mobile
uint8_t mobile_ip_addr_bytes[4] ; // IP of the mobile
owl_timestamp request_time ; // Timestamp on the mobile
owl_timestamp capture_time ; // Timestamp of arrival on the listener
uint8_t ss_dbm ; // Signal strength measured by the listener (dBm)
/* Calibration data */
float x_position ;
float y_position ;
float z_position ;
owl_direction direction ;
} owl_captured_request ;
/* Message sent by the Aggregator to the Positioner containing
* the main data of a request */
typedef struct _owl_request
{
uint8_t type ; // Type of the request
uint16_t nb_packets ; // Number of packets sent for this request
uint8_t mobile_mac_addr_bytes[ETHER_ADDR_LEN] ; // MAC of the mobile
uint8_t mobile_ip_addr_bytes[4] ; // IP of the mobile
owl_timestamp request_time ; // Timestamp on the mobile
uint16_t nb_info ; // Number of owl_request_info
/* Calibration data */
float x_position ;
float y_position ;
float z_position ;
owl_direction direction ;
} owl_request ;
/* Message sent by the Aggregator to the Positioner refering to
* a request, indicating that an AP received the request with a given
* signal strength */
typedef struct _owl_request_info
{
uint16_t packet_id ; // Number of the current packet
uint8_t ap_mac_addr_bytes[ETHER_ADDR_LEN] ; // MAC of the listener
owl_timestamp capture_time ; // Timestamp of arrival on the listener
uint8_t ss_dbm ; // Signal strength measured by the listener (dBm)
} owl_request_info ;
/* Hello message sent by the listener to the aggregator */
typedef struct _owl_autocalibration_hello
{
uint8_t ap_mac_addr_bytes[ETHER_ADDR_LEN] ;
} owl_autocalibration_hello ;
/* Message sent to the listener to order an emission */
#define AUTOCALIBRATION_ORDER_SEND 1
typedef struct _owl_autocalibration_order
{
uint8_t order ;
} owl_autocalibration_order ;
/* Positioning request types */
#define OWL_REQUEST_NORMAL 0
#define OWL_REQUEST_CALIBRATION 1
#define OWL_REQUEST_AUTOCALIBRATION 2
#define OWL_REQUEST_GENERATED 3
#define OWL_REQUEST_IMPLICIT 10
#define OWL_REQUEST_UNDEFINED 255
/* Wi-Fi channel frequencies in MHz */
#define OWL_80211_MHZ_CHANNEL_1 2412
#define OWL_80211_MHZ_CHANNEL_2 2417
#define OWL_80211_MHZ_CHANNEL_3 2422
#define OWL_80211_MHZ_CHANNEL_4 2427
#define OWL_80211_MHZ_CHANNEL_5 2432
#define OWL_80211_MHZ_CHANNEL_6 2437
#define OWL_80211_MHZ_CHANNEL_7 2442
#define OWL_80211_MHZ_CHANNEL_8 2447
#define OWL_80211_MHZ_CHANNEL_9 2452
#define OWL_80211_MHZ_CHANNEL_10 2457
#define OWL_80211_MHZ_CHANNEL_11 2462
#define OWL_80211_MHZ_CHANNEL_12 2467
#define OWL_80211_MHZ_CHANNEL_13 2472
#define OWL_80211_MHZ_CHANNEL_14 2477
/* Wi-Fi channel frequencies in Hz */
#define OWL_80211_HZ_CHANNEL_1 2412000000ul
#define OWL_80211_HZ_CHANNEL_2 2417000000ul
#define OWL_80211_HZ_CHANNEL_3 2422000000ul
#define OWL_80211_HZ_CHANNEL_4 2427000000ul
#define OWL_80211_HZ_CHANNEL_5 2432000000ul
#define OWL_80211_HZ_CHANNEL_6 2437000000ul
#define OWL_80211_HZ_CHANNEL_7 2442000000ul
#define OWL_80211_HZ_CHANNEL_8 2447000000ul
#define OWL_80211_HZ_CHANNEL_9 2452000000ul
#define OWL_80211_HZ_CHANNEL_10 2457000000ul
#define OWL_80211_HZ_CHANNEL_11 2462000000ul
#define OWL_80211_HZ_CHANNEL_12 2467000000ul
#define OWL_80211_HZ_CHANNEL_13 2472000000ul
#define OWL_80211_HZ_CHANNEL_14 2477000000ul
/* Misc. */
// Length of a MAC address in string format (including '\0')
#define OWL_ETHER_ADDR_STRLEN 18
// Maximum length of an algorithm name (including '\0')
#define OWL_ALGORITHM_STRLEN 31
// Maximum length of an area name (including '\0')
#define OWL_AREA_STRLEN 31
// Maximum length of a coordinate X, Y or Z (including '\0')
#define OWL_COORDINATE_STRLEN 16
/* Global variables */
// Used to handle end of loops:
extern owl_bool owl_run ;
/* Error codes */
/* User interface */
// Wrong program invokation (command-line arguments):
#define OWL_ERR_BAD_USAGE 100
// Error when reading/parsing the configuration file:
#define OWL_ERR_CONFIG_FILE 101
/* System */
// Error when creating a thread:
#define OWL_ERR_THREAD_CREATE 110
// Wrong signal received:
#define OWL_ERR_BAD_SIGNAL 111
/* Network communication */
// Error when creating a socket:
#define OWL_ERR_SOCKET_CREATE 120
// Error when sending a message on a socket:
#define OWL_ERR_SOCKET_SEND 121
// Error when reading from a socket:
#define OWL_ERR_SOCKET_RECV 122
/* Network interface / capture */
// Error when opening the capture interface:
#define OWL_ERR_IFACE_PCAP_OPEN 130
// Error when reading the interface Wi-Fi mode:
#define OWL_ERR_IFACE_MODE_GET 131
// Error when setting the interface Wi-Fi mode:
#define OWL_ERR_IFACE_MODE_SET 132
/* Function headers */
// Misc
const char* owl_mac_bytes_to_string(const uint8_t *const mac_binary) ;
void owl_mac_bytes_to_string_r(const uint8_t *const mac_binary,
char mac_str[OWL_ETHER_ADDR_STRLEN]) ;
owl_bool owl_mac_equals(const uint8_t *const mac1,
const uint8_t *const mac2) ;
uint_fast8_t owl_frequency_to_channel(const uint_fast16_t channel) ;
// Time
int owl_msleep(uint32_t time_ms) ;
int owl_timestamp_now(owl_timestamp *const now) ;
void owl_timespec_to_timestamp(const struct timespec *const src,
owl_timestamp *const dst) ;
void owl_timeval_to_timestamp(const struct timeval *const src,
owl_timestamp *const dst) ;
owl_bool owl_timestamp_equals(const owl_timestamp *const d1,
const owl_timestamp *const d2) ;
owl_bool owl_timestamp_is_null(const owl_timestamp *const d) ;
uint64_t owl_timestamp_to_ms(const owl_timestamp *const d) ;
void owl_timestamp_to_string(const owl_timestamp *const src,
char *const dst) ;
uint_fast32_t owl_time_elapsed_ms(const owl_timestamp *const d1,
const owl_timestamp *const d2) ;
void owl_time_elapsed(const owl_timestamp *const d1,
const owl_timestamp *const d2,
owl_timestamp *const elapsed) ;
// Endianess
void owl_hton_timestamp(owl_timestamp *const d) ;
void owl_ntoh_timestamp(owl_timestamp *const d) ;
float owl_swap_float(const float f) ;
#if __BYTE_ORDER == __BIG_ENDIAN
# define owl_htonf(f) (f)
# define owl_ntohf(f) (f)
#else // __BYTE_ORDER == __BIG_ENDIAN
# if __BYTE_ORDER == __LITTLE_ENDIAN
# define owl_htonf(f) owl_swap_float(f)
# define owl_ntohf(f) owl_swap_float(f)
# else // __BYTE_ORDER == __LITTLE_ENDIAN
# error "This program does not handle strange architectures."
# endif // __BYTE_ORDER == __LITTLE_ENDIAN
#endif // __BYTE_ORDER == __BIG_ENDIAN
// Network
int owl_create_udp_trx_socket(const char *const server_address,
const uint_fast16_t server_port,
struct sockaddr_in *const server_description,
struct sockaddr_in *const client_description) ;
int owl_create_udp_listening_socket(const uint_fast16_t port) ;
// Signals
void owl_sigint_handler(const int num) ;
void owl_sigterm_handler(const int num) ;
// Threads
void owl_close_fd(void *const fd) ;
void owl_close_file(void *const file) ;
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // _LIBOWLPS_H_