summaryrefslogtreecommitdiff
path: root/validation/test/av_home/av_mme.py
blob: b3787b7be346fa417cfcad4b2e23c853a3a1fa71 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import os
import sys
import shlex
import av_home_attr

from scapy.all import *

def srmme (src, dst, iface, payload):
    from scammer import MME
    mme = Ether (src = src, dst = dst) / MME () / payload
    return srp1 (mme, iface = iface)

def get_network_env(mode):
    sys.path.append (os.path.join (av_home_attr.prefix,
        av_home_attr.scammer_path))
    from cc import CC_DISCOVER_LIST_REQ, CC_DISCOVER_LIST_CNF
    from cm import CM_NW_INFO_REQ, CM_NW_INFO_CNF
    from vs import VS_GET_VERSION_REQ
    from vs import VS_GET_VERSION_CNF
    from vs import VS_GET_ATTENUATION_LIST_REQ
    from vs import VS_GET_ATTENUATION_LIST_CNF
    from vs import VS_GET_TONEMAP_REQ
    from vs import VS_GET_TONEMAP_CNF

    mme_list = [ [ VS_GET_VERSION_REQ (), process_version ],
            [ CC_DISCOVER_LIST_REQ (), process_discovery ],
            [ CM_NW_INFO_REQ (), process_avln ],
            [ VS_GET_ATTENUATION_LIST_REQ (), process_attenuation ],
            ]

    for mme in mme_list:
        ans = srmme (av_home_attr.source_mac,
                    av_home_attr.plug_mac,
                    av_home_attr.iface,
                    mme[0])
        if ans:
            mme[1] (ans, mode)

    ans = srmme (av_home_attr.source_mac,
                av_home_attr.plug_mac,
                av_home_attr.iface,
                VS_GET_TONEMAP_REQ (mac=av_home_attr.peer_mac, tmi=0xff))
    if ans:
        ans = srmme (av_home_attr.source_mac,
                    av_home_attr.plug_mac,
                    av_home_attr.iface,
                    VS_GET_TONEMAP_REQ (mac=av_home_attr.peer_mac,
                                        int_id = ans.int_id,
                                        tmi = ans.tmi_default))
        if ans:
            process_tonemap (ans[VS_GET_TONEMAP_CNF])

def get_stats(mode):
    sys.path.append (av_home_attr.scammer_path)
    from vs import VS_GET_STATS_REQ
    from vs import VS_GET_STATS_CNF

    f = open (os.path.join (av_home_attr.output_dir, "stats-%s" % mode), "w")
    ans = srmme (av_home_attr.source_mac,
                av_home_attr.plug_mac,
                av_home_attr.iface,
                VS_GET_STATS_REQ (level=0, page=0))

    if ans:
        stats = shlex.split (ans[VS_GET_STATS_CNF].stats)
        for s in stats:
            print >>f, s
        for i in range (ans[VS_GET_STATS_CNF].nb_pages - 1):
            ans = srmme (av_home_attr.source_mac,
                        av_home_attr.plug_mac,
                        av_home_attr.iface,
                        VS_GET_STATS_REQ (level=0, page=i + 1))
            if ans and ans[VS_GET_STATS_CNF].result == 0:
                stats = shlex.split (ans[VS_GET_STATS_CNF].stats)
                for s in stats:
                    print >>f, s

    ans = srmme (av_home_attr.source_mac,
                av_home_attr.plug_mac,
                av_home_attr.iface,
                VS_GET_STATS_REQ (level=1, page=0))

    if ans:
        stats = shlex.split (ans[VS_GET_STATS_CNF].stats)
        for s in stats:
            print >>f, s
        for i in range (ans[VS_GET_STATS_CNF].nb_pages - 1):
            ans = srmme (av_home_attr.source_mac,
                        av_home_attr.plug_mac,
                        av_home_attr.iface,
                        VS_GET_STATS_REQ (level=1, page=i + 1))
            if ans and ans[VS_GET_STATS_CNF].result == 0:
                stats = shlex.split (ans[VS_GET_STATS_CNF].stats)
                for s in stats:
                    print >>f, s

    f.close ()

def ping_peer():
    ans, unans = srloop (IP(dst=av_home_attr.addr) / ICMP (), count=10)
    if not ans:
        print "Unable to ping peer, aborting ..."
        sys.exit (1)

def process_tonemap (tm):
    from vs import VS_GET_TONEMAP_CNF
    fec = tm.tm_fec
    ml = tm.modulation_list
    f = open (os.path.join (av_home_attr.output_dir, "tonemap"), "w")
    print >>  f, fec
    print >>  f,"Default tonemap:"
    for i in range (768):
        print >> f, ml[2 * i:2 * (i + 1)] + " ",
        if (i % 16) == 15:
            print >> f, " "
    f.close ()

def process_version (ans, mode):
    from vs import VS_GET_VERSION_CNF
    f = open ("soft_version-%s" % mode, "w")
    soft = """
Firmware version of {mac}:

::

    PLC Driver: {linux}
    PLC Firmware: {driver}
"""
    soft = soft.format (mac = ans[Ether].src,
            linux = ans[VS_GET_VERSION_CNF].applicative_version.replace ('\0', ''),
            driver = ans[VS_GET_VERSION_CNF].av_stack_version.replace ('\0', ''))
    print >>f, soft,
    f.close ()

def process_discovery (ans, mode):
    from cc import CC_DISCOVER_LIST_CNF
    f = open (os.path.join (av_home_attr.output_dir, "discovery-%s" % mode), "w")
    discover = """
List of stations for {plug}:

+-------------------+------+-----+
|       MAC         | SNID | TEI |
+-------------------+------+-----+
{stations}

List of neighbor networks for {plug}:

+-------------------+------+
|       NID         | SNID |
+-------------------+------+
{networks}
"""
    stations = ""
    networks = ""
    if ans[CC_DISCOVER_LIST_CNF].num_station != 0:
        for e in ans[CC_DISCOVER_LIST_CNF].stations:
            stations += "| %s |  %02d  | %03d |\n" % (e.mac, int (e.snid), int (e.tei))
            stations += "+-------------------+------+-----+\n"
    if ans[CC_DISCOVER_LIST_CNF].num_network != 0:
        for e in ans[CC_DISCOVER_LIST_CNF].networks:
            networks += "| %s  |  %02d  |\n" % (e.nid, int (e.snid))
            networks += "+-------------------+------+\n"
    discover = discover.format (plug = av_home_attr.plug_mac,
                                stations = stations,
                                networks = networks)
    print >>f, discover,
    f.close ()

def process_avln (ans, mode):
    from cm import CM_NW_INFO_CNF
    role = {
                0: "STA",
                1: "PROXY_COORDINATOR",
                2: "CCO" }
    f = open (os.path.join (av_home_attr.output_dir, "avln-%s" % mode), "w")
    avln = """
AVLN(s) for {plug}:

+-------------------+------+-----+------+
|       NID         | SNID | TEI | Role |
+-------------------+------+-----+------+
{avlns}
"""
    avlns = ""
    for e in ans[CM_NW_INFO_CNF].nwinfo:
        avlns += "| %s  |  %02d  | %03d |  %s |\n" % (e.nid, int (e.snid), int (e.tei), role[e.stationrole])
        avlns += "+-------------------+------+-----+------+\n"
    avln = avln.format (plug = av_home_attr.plug_mac, avlns = avlns)
    print >>f, avln,
    f.close ()

def process_attenuation (ans, mode):
    from vs import VS_GET_ATTENUATION_LIST_CNF
    f = open (os.path.join (av_home_attr.output_dir, "attenuation-%s" % mode), "w")
    attenuation = """
Attenuation for {plug}:

+-------------------+------+
|       MAC         | Att  |
+-------------------+------+
{attenuations}
"""
    attenuations = ""
    for e in ans[VS_GET_ATTENUATION_LIST_CNF].entries:
        if e.valid:
            # Values above are not valid
            if e.attenuation_db > 80:
                e.attenuation_db = 80
            attenuations += "| %s |  %02d  |\n" % (e.mac, int(e.attenuation_db))
        else:
            attenuations += "| %s |  NA  |\n" % e.mac
        attenuations += "+-------------------+------+\n"
    attenuation = attenuation.format (plug = av_home_attr.plug_mac,
                                            attenuations = attenuations)
    print >>f, attenuation,
    f.close ()