iwlib-0.1.2/0000755000000000000000000000000007346545000011011 5ustar0000000000000000iwlib-0.1.2/LICENSE0000644000000000000000000000303407346545000012016 0ustar0000000000000000Copyright (c) 2018, Jose A. Ortega Ruiz All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Jose A. Ortega Ruiz nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. iwlib-0.1.2/Setup.hs0000644000000000000000000000005607346545000012446 0ustar0000000000000000import Distribution.Simple main = defaultMain iwlib-0.1.2/iwlib.cabal0000644000000000000000000000164507346545000013111 0ustar0000000000000000name: iwlib version: 0.1.2 synopsis: Bindings for the iw C library description: A binding to the iw library for getting info about the current WiFi connection. homepage: https://codeberg.org/xmobar/iwlib bug-reports: https://codeberg.org/xmobar/iwlib/issues license: BSD3 license-file: LICENSE author: Jose Antonio Ortega Ruiz maintainer: Jose Antonio Ortega Ruiz category: System, Network build-type: Simple cabal-version: >=1.10 extra-source-files: readme.md source-repository head type: git location: https://codeberg.org/xmobar/iwlib.git library build-depends: base >=4 && <5 hs-source-dirs: src includes: iwlib.h default-language: Haskell2010 exposed-modules: Network.IWlib extra-libraries: iw ghc-options: -Walliwlib-0.1.2/readme.md0000644000000000000000000000447407346545000012601 0ustar0000000000000000[![Hackage](https://img.shields.io/hackage/v/iwlib.svg)](http://hackage.haskell.org/package/iwlib) [![Build Status](https://travis-ci.org/jaor/iwlib.svg?branch=master)](https://travis-ci.org/jaor/iwlib) # About *iwlib* is a thin wrapper over the *iw* C library, providing access to wireless card information in supported systems. # Bug Reports To submit bug reports you can use the [bug tracker over at Codeberg]. [bug tracker over at Github]: https://codeberg.org/xmobar/iwlib/issues # Installation ## Using cabal-install iwlib is available from [Hackage], and you can install it using `cabal-install`: cabal install iwlib ## From source If you don't have `cabal-install` installed, you can get iwlib's source code in a variety of ways: - From [Hackage]. Just download the latest release from xmobar's hackage page. - From [Codeberg]. You can also obtain a tarball in [Codeberg's downloads page]. You'll find there links to each tagged release. - From the bleeding edge repo. If you prefer to live dangerously, just get the latest and greatest (and buggiest, I guess) using git: git clone git://codeberg.org/xmobar/iwlib [Codeberg's downloads page]: https://github.com/jaor/iwlib/downloads If you have cabal installed, you can now use it from within its source tree: cabal install Otherwise, run the configure script: runhaskell Setup.lhs configure Now you can build the source: runhaskell Setup.lhs build runhaskell Setup.lhs install # possibly to be run as root # External dependencies No other Haskell library is required, but you will need the [iwlib] C library and headers in your system (e.g., install `libiw-dev` in Debian-based systems or `wireless_tools` on Arch Linux). # Authors This library is written and maintaned by Jose Antonio Ortega Ruiz. # Thanks Many thanks to Leif Warner for suggesting the creation of this library and providing the initial hackage scaffolding. # License This software is released under a BSD-style license. See the [license file](./license) for more details. Copyright © 2018 Jose Antonio Ortega Ruiz [Codeberg]: http://github.com/jaor/iwlib/ [Codeberg page]: http://github.com/jaor/iwlib [Hackage]: http://hackage.haskell.org/package/iwlib/ [iwlib]: http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/Tools.html iwlib-0.1.2/src/Network/0000755000000000000000000000000007346545000013231 5ustar0000000000000000iwlib-0.1.2/src/Network/IWlib.hsc0000644000000000000000000000514207346545000014740 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : IWlib -- Copyright : (c) 2018 Jose A Ortega Ruiz -- License : BSD-style (see LICENSE) -- -- Maintainer : Jose A Ortega Ruiz -- Stability : unstable -- Portability : unportable -- -- A partial binding to iwlib -- ----------------------------------------------------------------------------- {-# LANGUAGE CPP, ForeignFunctionInterface, EmptyDataDecls #-} module Network.IWlib (WirelessInfo(..), getWirelessInfo) where import Foreign import Foreign.C.Types import Foreign.C.String data WirelessInfo = WirelessInfo { wiEssid :: String, wiQuality :: Int } deriving Show #include data WCfg data WStats data WRange foreign import ccall "iwlib.h iw_sockets_open" c_iw_open :: IO CInt foreign import ccall "unistd.h close" c_iw_close :: CInt -> IO () foreign import ccall "iwlib.h iw_get_basic_config" c_iw_basic_config :: CInt -> CString -> Ptr WCfg -> IO CInt foreign import ccall "iwlib.h iw_get_stats" c_iw_stats :: CInt -> CString -> Ptr WStats -> Ptr WRange -> CInt -> IO CInt foreign import ccall "iwlib.h iw_get_range_info" c_iw_range :: CInt -> CString -> Ptr WRange -> IO CInt getWirelessInfo :: String -> IO WirelessInfo getWirelessInfo iface = allocaBytes (#size struct wireless_config) $ \wc -> allocaBytes (#size struct iw_statistics) $ \stats -> allocaBytes (#size struct iw_range) $ \rng -> withCString iface $ \istr -> do i <- c_iw_open bcr <- c_iw_basic_config i istr wc str <- c_iw_stats i istr stats rng 1 rgr <- c_iw_range i istr rng c_iw_close i if bcr < 0 then return WirelessInfo { wiEssid = "", wiQuality = 0 } else do hase <- (#peek struct wireless_config, has_essid) wc :: IO CInt eon <- (#peek struct wireless_config, essid_on) wc :: IO CInt essid <- if hase /= 0 && eon /= 0 then do let e = (#ptr struct wireless_config, essid) wc peekCString e else return "" q <- if str >= 0 && rgr >=0 then do qualv <- xqual $ (#ptr struct iw_statistics, qual) stats mv <- xqual $ (#ptr struct iw_range, max_qual) rng let mxv = if mv /= 0 then fromIntegral mv else 1 return $ fromIntegral qualv / mxv else return 0 let qv = round (100 * (q :: Double)) return WirelessInfo { wiEssid = essid, wiQuality = min 100 qv } where xqual p = let qp = (#ptr struct iw_param, value) p in (#peek struct iw_quality, qual) qp :: IO CChar