#!/usr/bin/python
'''
//=============================================================================
//
//  File : ufo_linuxscanner.py
//  Creation date : Thu Gen 16 14:30:48 CEST 2012
//  Working on this file:Grifisx (Antonino G. Imbesi)
//                      
//  This file is part of the Ufo Wardriving distribution
//
//  Websites: http://ufo-wardriving.com
//
//  This program is FREE software. You can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your opinion) any later version.
//
//  This program is distributed in the HOPE that it will be USEFUL,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//  See the GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program. If not, write to the Free Software Foundation,
//  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================
@ class name:

@ short description:

@ functions:

@signals:

'''
from __future__ import division
from PyQt4.QtCore import *
from PyQt4.QtGui import *

import sys, os.path,platform, time
import subprocess
import urllib2
import decimal


def dbg(arg):
    if DEFINE_DEBUG_MODE:
        print "@"+str(arg)
            
class APnetObj(QObject):
    def __init__(self, parent = None):
        super(APnetObj, self).__init__(parent=None)
        self.essid = ""
        self.encr = ""
        self.mac = ""
        self.signal = 0
        self.signalPerc = 0
        self.freq = 0
        self.iface = ""
        self.key = []
        
    def set_iface(self,iface):  self.iface = iface
    def set_essid(self,ssid):   self.essid = ssid  
    def set_key(self,key):  self.key = key
    def set_encr(self,encr):    self.encr = encr
    def set_mac(self,mac):  self.mac = mac
    def set_signal(self,signal):
        self.signal = signal
        self.signalPerc = self.signal_percentage(signal)
    def set_freq(self,freq):    self.freq = freq
    def set_encription(self,encr):  self.encr = encr

    def get_iface(self):    return self.iface
    def get_essid(self):    return self.essid
    def get_mac(self):  return self.mac
    def get_signal(self):   return self.signal
    def get_freq(self): return self.freq
    def get_encription(self):   return self.encr
    def get_signal_percentage(self):    return self.signalPerc
    def get_key(self):  return self.key
    def get_encr(self): return self.encr

    def signal_percentage(self,dbm):
            # dbm puo' variare da -35(100%) a -95(0%) o da 0(0%) a 255(100%) a seconda dei driver della scheda usata ...speriamo non ci siano altre varianti
            dbm =int(dbm)
            if dbm < 0 :
                if dbm == -95 : return 100
                # ho fatto questa formuletta che sembra funzionare p = 100(%) / ((max-min 60 -scarto-)/(max-(valore*-1 reso positivo))
                p = 100/(60/(95-(dbm*-1)))
                return p
            if dbm >-1:
                p = 100/(255/dbm)
                return p
'''
================================================
'''  

def get_iface_list():
    p = subprocess.Popen(['/sbin/wpa_cli','interface'], stdout = subprocess.PIPE)
    p.wait()
    (stdout,stderr) = p.communicate(None)
    availableifaces = stdout.split('\n')[2:]
    return availableifaces
    
def get_ap_list():

    availableifaces = get_iface_list()
    aplist=[]
    idx=0
    for iface in availableifaces:
        if iface !="":
            p = subprocess.Popen(['/sbin/wpa_cli','-i',iface,'scan'], stdout = subprocess.PIPE)
            p.wait()
            popen = subprocess.Popen(['/sbin/wpa_cli','-i',iface,'scan_results'], stdout = subprocess.PIPE)
            popen.wait()
            (stdout,stderr) = popen.communicate(None)
            availablenets = stdout.split('\n')[1:]
            for net in availablenets:
                apinfo = net.split('\t')
                if apinfo[0] != "":
                    apObj=APnetObj()
                    apObj.set_iface(iface)
                    apObj.set_mac(apinfo[0])
                    apObj.set_freq(apinfo[1])
                    apObj.set_signal(apinfo[2])
                    apObj.set_encr(apinfo[3])
                    apObj.set_essid(apinfo[4])
                    aplist.append(apObj)
    return aplist
