summaryrefslogtreecommitdiff
path: root/test-cli/test/helpers/iw.py
blob: 8e29448d441ea61f8c9d78f206817523985f675f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from sh import iw
from scanf import scanf

class iw_scan:
    __interface = None
    __raw_unparsed = []
    __raw = None

    def __init__(self, interface='wlan0'):
        self.__interface = interface

    def scan(self):
        self.__raw_unparsed = []
        self.__raw = iw('{}'.format(self.__interface), 'scan')
        if self.__raw.exit_code == 0:
            self.__raw_unparsed = self.__raw.stdout.decode("utf-8").split('\n')
            return True
        return False

    def getRawData (self):
        return self.__raw_unparsed

    def getInterface(self):
        return self.__interface

    def getRawObject(self):
        return self.__raw


class iwScan:
    __iw_scan = None
    __station_list = {}

    def __init__(self, interface='wlan0'):
        self.__iw_scan = iw_scan(interface)

    def scan(self):
        if self.__iw_scan.scan():
            self.__parse_scan(self.__iw_scan.getRawData())
            return True
        return False

    def getLastError(self):
        rObject = self.__iw_scan.getRawObject()
        if rObject.exit_code != 0:
            return rObject.stderr
        return ''

    def __Parse_BBS(self, line):
        data = {}
        res = scanf("BSS %s(on %s) -- %s", line)
        if res == None:
            res = scanf("BSS %s(on %s)", line)
        data["MAC"] = res[0]
        data["DEV"] = res[1]
        if len(res) == 3:
            data["ASSOCIATED"] = 'YES'
        else:
            data["ASSOCIATED"] = 'NO'
        return data

    def __Parse_T(self, id, OnNotFoundAttr ,line):
        data = {}
        res = scanf("{}: %s".format(id), line)
        if res == None:
            data[id.upper()] = OnNotFoundAttr
        else:
            data[id.upper()] = res[0]
        return data

    def __Parse_X(self, line):
        data = {}
        res = scanf("DS Parameter set: channel %s", line)
        if res == None:
            data['CHANNEL'] = '-1'
        else:
            data['CHANNEL'] = res[0]
        return data

    def __block_stationlist(self, rawdata):
        list = {}
        count = 0
        for line in rawdata:
            idx = line.find('BSS')
            if idx != -1 and idx == 0:
                if len(list) > 0:
                    count += 1
                    self.__station_list[count] = list
                list = self.__Parse_BBS(line)
            elif line.find('SSID:') != -1:
                list = {**list, **self.__Parse_T('SSID', 'HIDDEN', line)}
            elif line.find('freq:') != -1:
                list = {**list, **self.__Parse_T('freq', '', line)}
            elif line.find('signal:') != -1:
                list = {**list, **self.__Parse_T('signal', '', line)}
            elif line.find('DS Parameter set:') != -1:
                list = {**list, **self.__Parse_X(line)}
        if count > 0:
            count += 1
            self.__station_list[count] = list

    def __parse_scan(self, rawData):
        self.__station_list = {}
        self.__block_stationlist(rawData)
        #print('{}'.format(self.__station_list))
        #for i in self.__station_list:
        #   print(self.__station_list[i])

    def findConnected (self):
        for i in self.__station_list:
            st = self.__station_list[i]
            if st.get('ASSOCIATED', 'NO') == 'YES':
                return True, self.__station_list[i]
        return False, None

    def findBySSID(self, ssid):
        for i in self.__station_list:
            st = self.__station_list[i]
            if st.get('SSID', '') == ssid:
                return True, self.__station_list[i]
        return False, None

    def getStations(self):
        return self.__station_list