325 lines
10 KiB
C
325 lines
10 KiB
C
/*
|
|
* This file is part of the rtap localisation project.
|
|
*/
|
|
|
|
#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 request is sent by the mobile:
|
|
#define LOC_REQUEST_DEFAULT_PORT 9900
|
|
// Port on which listeners and aggregator communicate:
|
|
#define AGGREGATE_DEFAULT_PORT 9901
|
|
// Port on which aggregator and positioning server communicate:
|
|
#define POSITIONER_DEFAULT_PORT 9902
|
|
// Port on which autocalibration requests are sent by the listeners:
|
|
#define DEFAULT_AUTOCALIBRATION_REQUEST_PORT 9903
|
|
// Port on which the aggregator listens for hello messages from the
|
|
// listeners, and the listeners listen for orders from the aggregator:
|
|
#define DEFAULT_AUTOCALIBRATION_PORT 9904
|
|
// Port on which the mobile listens for its position:
|
|
#define MOBILE_DEFAULT_PORT 9910
|
|
|
|
|
|
/* Boolean type */
|
|
typedef enum {FALSE, TRUE} owl_bool ;
|
|
#define OWL_BOOL_TO_STRING(B) ((B) ? "true" : "false")
|
|
|
|
|
|
/* Direction type */
|
|
enum {NORTH = 1, EAST, SOUTH, 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_STR_LEN 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
|
|
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 ; // Request ID (timestamp on the mobile)
|
|
owl_timestamp start_time ; // Timestamp of arrival on the listener
|
|
uint8_t antenna_signal_dbm ; // Signal strength measured by the listener
|
|
/* Calibration data */
|
|
float x_position ;
|
|
float y_position ;
|
|
float z_position ;
|
|
owl_direction direction ;
|
|
} owl_captured_request ;
|
|
|
|
|
|
/* Message sent by the aggregator to the positioning server containing
|
|
* the main data of a request */
|
|
typedef struct _owl_request
|
|
{
|
|
uint8_t type ; // Type of the request
|
|
uint8_t mobile_mac_addr_bytes[ETHER_ADDR_LEN] ; // MAC of the mobile
|
|
owl_timestamp request_time ; // Request ID (timestamp on the mobile)
|
|
uint16_t nb_info ; // Number of (listener MAC;signal strength) couples
|
|
/* Calibration data */
|
|
float x_position ;
|
|
float y_position ;
|
|
float z_position ;
|
|
owl_direction direction ;
|
|
} owl_request ;
|
|
|
|
|
|
/* Message sent by the aggregator to the positioning server refering to
|
|
* a request, indicating that an AP received the request with a given
|
|
* signal strength */
|
|
typedef struct _owl_request_info
|
|
{
|
|
uint8_t ap_mac_addr_bytes[ETHER_ADDR_LEN] ; // MAC of the listener
|
|
uint8_t antenna_signal_dbm ; // Signal strength measured by the listener
|
|
} 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
|
|
|
|
|
|
|
|
/* Packet header sizes (in bytes) */
|
|
#define IEEE80211_HEADER_SIZE_DATA 24 // Header size for a Data frame
|
|
#define LLC_HEADER_SIZE 8
|
|
|
|
/* IEEE 802.11 frame types */
|
|
// Beacon (TODO: convert to mask)
|
|
#define RAW_PACKET_TYPE_BEACON 0x80
|
|
|
|
// Data frame
|
|
#define FRAME_TYPE_DATA_MASK 0x08
|
|
#define IS_DATA_FRAME(FC1) \
|
|
(((FC1) & FRAME_TYPE_DATA_MASK) == FRAME_TYPE_DATA_MASK)
|
|
|
|
// QoS Data frame
|
|
#define FRAME_SUBTYPE_QOS_MASK 0x80
|
|
#define DATA_FRAME_IS_QOS(FC1) \
|
|
(((FC1) & FRAME_SUBTYPE_QOS_MASK) == FRAME_SUBTYPE_QOS_MASK)
|
|
|
|
// To/From DS
|
|
#define FRAME_FROM_STA_MASK 0x02
|
|
#define IS_FRAME_FROM_STA(FC2) \
|
|
(((FC2) & FRAME_FROM_STA_MASK) != FRAME_FROM_STA_MASK)
|
|
|
|
|
|
/* Positions of the radiotap header fixed fields (in bytes) */
|
|
#define RTAP_P_HREVISION 0 // Header revision
|
|
#define RTAP_P_HPAD 1 // Header pad
|
|
#define RTAP_P_HLENGTH 2 // Header length
|
|
#define RTAP_P_PRESENTFLAGS 4 // Present flags
|
|
|
|
|
|
/* Radiotap field lengths (in bytes) */
|
|
#define RTAP_L_HREVISION 1 // Header revision
|
|
#define RTAP_L_HPAD 1 // Header pad
|
|
#define RTAP_L_HLENGTH 2 // Header length
|
|
#define RTAP_L_PRESENTFLAGS 4 // Present flags
|
|
#define RTAP_L_MACTS 8 // MAC timestamp (Time Synchronization Function Timer)
|
|
#define RTAP_L_FLAGS 1 // autre champ de flags
|
|
#define RTAP_L_RATE 1 // Data rate
|
|
#define RTAP_L_CHANNEL 2 // Channel frequency
|
|
#define RTAP_L_CHANNELTYPE 2 // Channel type
|
|
#define RTAP_L_ANTENNASIGNALDBM 1 // SSI signal (dBm)
|
|
#define RTAP_L_ANTENNANOISEDBM 1 // SSI noise (dBm)
|
|
#define RTAP_L_ANTENNA 1 // Antenna number
|
|
#define RTAP_L_FHSS 2 // Hop set and pattern for Frequency-Hopping Spread Spectrum
|
|
#define RTAP_L_LOCKQUALITY 2 // Signal quality
|
|
#define RTAP_L_TXATTENUATION 2 // Transmit power from max power
|
|
#define RTAP_L_TXATTENUATIONDB 2 // Idem (dB)
|
|
#define RTAP_L_TXATTENUATIONDBM 1 // Idem (dBm)
|
|
#define RTAP_L_ANTENNASIGNALDB 1 // SSI signal (dB)
|
|
#define RTAP_L_ANTENNANOISEDB 1 // SSI noise (dB)
|
|
#define RTAP_L_FCS 4 // Frame Check Sequence
|
|
//#define RTAP_L_CHANNELP // Extended channel info (not implemented)
|
|
//#define RTAP_L_EXT // Extension aux Present flags (not emplemented)
|
|
|
|
|
|
/* Positions in 'Present flags' (and present fields 'check' array) */
|
|
#define RTAP_MACTS 0
|
|
#define RTAP_FLAGS 1
|
|
#define RTAP_RATE 2
|
|
#define RTAP_CHANNEL 3 // and RTAP_CHANNELTYPE
|
|
#define RTAP_FHSS 4
|
|
#define RTAP_ANTENNASIGNALDBM 5
|
|
#define RTAP_ANTENNANOISEDBM 6
|
|
#define RTAP_LOCKQUALITY 7
|
|
#define RTAP_TXATTENUATION 8
|
|
#define RTAP_TXATTENUATIONDB 9
|
|
#define RTAP_TXATTENUATIONDBM 10
|
|
#define RTAP_ANTENNA 11
|
|
#define RTAP_ANTENNASIGNALDB 12
|
|
#define RTAP_ANTENNANOISEDB 13
|
|
#define RTAP_FCS 14
|
|
//#define RTAP_CHANNELP 18
|
|
//#define RTAP_EXT 31
|
|
|
|
|
|
/* Misc. */
|
|
// Length of a MAC address in string format (including '\0')
|
|
#define OWL_ETHER_ADDR_STRLEN 18
|
|
|
|
|
|
/* Global variables */
|
|
// Used to handle end of loops:
|
|
extern owl_bool owl_run ;
|
|
|
|
|
|
/* Function error codes */
|
|
#define ERR_SETTING_MODE 101
|
|
#define ERR_SETTING_CHANNEL 102
|
|
#define ERR_READING_CHANNEL 103
|
|
#define ERR_READING_MODE 104
|
|
#define ERR_BAD_SIGNAL 111
|
|
|
|
|
|
/* 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_timestamp_now(owl_timestamp *const now) ;
|
|
owl_timestamp owl_timespec_to_timestamp(const struct timespec d) ;
|
|
owl_timestamp owl_timeval_to_timestamp(const struct timeval d) ;
|
|
owl_bool owl_timestamp_equals(const owl_timestamp d1,
|
|
const owl_timestamp d2) ;
|
|
owl_bool owl_timestamp_is_null(const owl_timestamp d) ;
|
|
uint64_t owl_timestamp_to_ms(const owl_timestamp d) ;
|
|
void owl_timestamp_to_string(char *const dst, const owl_timestamp src) ;
|
|
uint_fast32_t owl_time_elapsed_ms(const owl_timestamp d1,
|
|
const owl_timestamp d2) ;
|
|
owl_timestamp owl_time_elapsed(const owl_timestamp d1,
|
|
const owl_timestamp d2) ;
|
|
|
|
// Endianess
|
|
owl_timestamp owl_hton_timestamp(const owl_timestamp d) ;
|
|
owl_timestamp owl_ntoh_timestamp(const owl_timestamp 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) ;
|
|
|
|
|
|
/* Macros */
|
|
|
|
/*
|
|
* Test if a IEEE 802.11 frame is a retry.
|
|
* Input: IEEE 802.11 header flags.
|
|
* Returns 0 if the Retry bit is absent, a positive value if present.
|
|
*/
|
|
#define IS_RETRY(IEEE80211_FLAGS) ((IEEE80211_FLAGS) & 0x08)
|
|
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif // __cplusplus
|
|
|
|
#endif // _LIBOWLPS_H_
|