wifiscanner/
lib.rs

1// Copyright 2016 Mark Sta Ana.
2// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
3// http://www.apache.org/licenses/LICENSE-2.0>, at your option.
4// This file may not be copied, modified, or distributed except
5// according to those terms.
6
7// Inspired by Maurice Svay's node-wifiscanner (https://github.com/mauricesvay/node-wifiscanner)
8
9//! A crate to list WiFi hotspots in your area.
10//!
11//! As of v0.5.x now supports macOS, Linux and Windows. :tada:
12//!
13//! # Usage
14//!
15//! This crate is on [crates.io](https://crates.io/crates/wifiscanner) and can be
16//! used by adding `wifiscanner` to the dependencies in your project's `Cargo.toml`.
17//!
18//! ```toml
19//! [dependencies]
20//! wifiscanner = "0.5.*"
21//! ```
22//!
23//! and this to your crate root:
24//!
25//! ```rust
26//! extern crate wifiscanner;
27//! ```
28//!
29//! # Example
30//!
31//! ```
32//! use wifiscanner;
33//! println!("{:?}", wifiscanner::scan());
34//! ```
35//!
36//! Alternatively if you've cloned the the Git repo, you can run the above example
37//! using: `cargo run --example scan`.
38
39//TODO need to find a way to move these out of lib and into sys or better still windows module
40#[cfg(target_os = "windows")]
41#[macro_use]
42extern crate itertools;
43#[cfg(target_os = "windows")]
44extern crate regex;
45
46mod sys;
47
48#[allow(missing_docs)]
49#[derive(Debug, PartialEq, Eq)]
50pub enum Error {
51    SyntaxRegexError,
52    CommandNotFound,
53    NoMatch,
54    FailedToParse,
55    NoValue,
56}
57
58/// Wifi struct used to return information about wifi hotspots
59#[derive(Debug, PartialEq, Eq, Default)]
60pub struct Wifi {
61    /// mac address
62    pub mac: String,
63    /// hotspot name
64    pub ssid: String,
65    pub channel: String,
66    /// wifi signal strength in dBm
67    pub signal_level: String,
68    /// this field is currently empty in the Linux version of the lib
69    pub security: String,
70}
71
72/// Returns a list of WiFi hotspots in your area.
73/// Uses `airport` on macOS and `iw` on Linux.
74pub fn scan() -> Result<Vec<Wifi>, Error> {
75    crate::sys::scan()
76}