iwlib (empty) → 0.1.0
raw patch · 5 files changed
+217/−0 lines, 5 filesdep +basesetup-changed
Dependencies added: base
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- iwlib.cabal +28/−0
- readme.md +82/−0
- src/Network/IWlib.hsc +75/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2018, Jose A. Ortega Ruiz <jao@gnu.org>++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 <jao@gnu.org> 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ iwlib.cabal view
@@ -0,0 +1,28 @@+name: iwlib+version: 0.1.0+synopsis: Bindings for the iw C library+description:+ A binding to the iw library for getting info about the current WiFi connection.+homepage: https://github.com/jaor/iwlib+bug-reports: https://github.com/jaor/iwlib/issues+license: BSD3+license-file: LICENSE+author: Jose Antonio Ortega Ruiz <jao@gnu.org>+maintainer: Jose Antonio Ortega Ruiz <jao@gnu.org>+category: System, Network+build-type: Simple+cabal-version: >=1.10++extra-source-files: readme.md+source-repository head+ type: git+ location: https://github.com/jaor/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: -Wall
+ readme.md view
@@ -0,0 +1,82 @@+++# 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 Github].++[bug tracker over at Github]: https://github.com/jaor/iwlib/issues++# Installation++## Using cabal-install++Xmobar 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 xmobar's+source code in a variety of ways:++ - From [Hackage]. Just download the latest release from xmobar's+ hackage page.+ - From [Github]. You can also obtain a tarball in [Github'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://github.com/jaor/iwlib+++[Github'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 [LICENSE] for+more details.++Copyright © 2018 Jose Antonio Ortega Ruiz++[Github]: http://github.com/jaor/iwlib/+[Github page]: http://github.com/jaor/iwlib+[Hackage]: http://hackage.haskell.org/package/iwlib/+[LICENSE]: https://github.com/jaor/iwlib/raw/master/license+[iwlib]: http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/Tools.html
+ src/Network/IWlib.hsc view
@@ -0,0 +1,75 @@+-----------------------------------------------------------------------------+-- |+-- Module : IWlib+-- Copyright : (c) 2018 Jose A Ortega Ruiz+-- License : BSD-style (see LICENSE)+--+-- Maintainer : Jose A Ortega Ruiz <jao@gnu.org>+-- 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 <iwlib.h>++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