xref: /aosp_15_r20/external/libnl/python/examples/iface.py (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1from __future__ import print_function
2import netlink.capi as nl
3import netlink.genl.capi as genl
4import nl80211
5import sys
6import traceback
7
8
9class test_class:
10    def __init__(self):
11        self.done = 1
12
13
14def msg_handler(m, a):
15    try:
16        e, attr = genl.py_genlmsg_parse(
17            nl.nlmsg_hdr(m), 0, nl80211.NL80211_ATTR_MAX, None
18        )
19        if nl80211.NL80211_ATTR_WIPHY in attr:
20            thiswiphy = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_WIPHY])
21            print("phy#%d" % thiswiphy)
22        if nl80211.NL80211_ATTR_IFNAME in attr:
23            print(
24                "\tinterface %s" % nl.nla_get_string(attr[nl80211.NL80211_ATTR_IFNAME])
25            )
26        if nl80211.NL80211_ATTR_IFINDEX in attr:
27            print("\tifindex %d" % nl.nla_get_u32(attr[nl80211.NL80211_ATTR_IFINDEX]))
28        if nl80211.NL80211_ATTR_WDEV in attr:
29            print("\twdev 0x%lx" % nl.nla_get_u64(attr[nl80211.NL80211_ATTR_WDEV]))
30        if nl80211.NL80211_ATTR_MAC in attr:
31            print(
32                "\tmac %02x:%02x:%02x:%02x:%02x:%02x"
33                % tuple(nl.nla_data(attr[nl80211.NL80211_ATTR_MAC]))
34            )
35        if nl80211.NL80211_ATTR_SSID in attr:
36            print("\tssid ", nl.nla_data(attr[nl80211.NL80211_ATTR_SSID]))
37        if nl80211.NL80211_ATTR_IFTYPE in attr:
38            iftype = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_IFTYPE])
39            print("\ttype %s" % nl80211.nl80211_iftype2str[iftype])
40        if nl80211.NL80211_ATTR_WIPHY_FREQ in attr:
41            freq = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_WIPHY_FREQ])
42
43            sys.stdout.write("\tfreq %d MHz" % freq)
44
45            if nl80211.NL80211_ATTR_CHANNEL_WIDTH in attr:
46                chanw = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_CHANNEL_WIDTH])
47                sys.stdout.write(", width: %s" % nl80211.nl80211_chan_width2str[chanw])
48                if nl80211.NL80211_ATTR_CENTER_FREQ1 in attr:
49                    sys.stdout.write(
50                        ", center1: %d MHz"
51                        % nl.nla_get_u32(attr[nl80211.NL80211_ATTR_CENTER_FREQ1])
52                    )
53                if nl80211.NL80211_ATTR_CENTER_FREQ2 in attr:
54                    sys.stdout.write(
55                        ", center2: %d MHz"
56                        % nl.nla_get_u32(attr[nl80211.NL80211_ATTR_CENTER_FREQ2])
57                    )
58            elif nl80211.NL80211_ATTR_WIPHY_CHANNEL_TYPE in attr:
59                channel_type = nl.nla_get_u32(
60                    attr[nl80211.NL80211_ATTR_WIPHY_CHANNEL_TYPE]
61                )
62                sys.stdout.write(" %s" % nl80211.nl80211_channel_type2str(channel_type))
63
64            sys.stdout.write("\n")
65        return nl.NL_SKIP
66    except Exception:
67        (t, v, tb) = sys.exc_info()
68        print(v.message)
69        traceback.print_tb(tb)
70
71
72def error_handler(err, a):
73    a.done = err.error
74    return nl.NL_STOP
75
76
77def finish_handler(m, a):
78    return nl.NL_SKIP
79
80
81def ack_handler(m, a):
82    a.done = 0
83    return nl.NL_STOP
84
85
86try:
87    cbd = test_class()
88    tx_cb = nl.nl_cb_alloc(nl.NL_CB_DEFAULT)
89    rx_cb = nl.nl_cb_clone(tx_cb)
90    s = nl.nl_socket_alloc_cb(tx_cb)
91    nl.py_nl_cb_err(rx_cb, nl.NL_CB_CUSTOM, error_handler, cbd)
92    nl.py_nl_cb_set(rx_cb, nl.NL_CB_FINISH, nl.NL_CB_CUSTOM, finish_handler, cbd)
93    nl.py_nl_cb_set(rx_cb, nl.NL_CB_ACK, nl.NL_CB_CUSTOM, ack_handler, cbd)
94    nl.py_nl_cb_set(rx_cb, nl.NL_CB_VALID, nl.NL_CB_CUSTOM, msg_handler, cbd)
95
96    genl.genl_connect(s)
97    family = genl.genl_ctrl_resolve(s, "nl80211")
98    m = nl.nlmsg_alloc()
99    genl.genlmsg_put(m, 0, 0, family, 0, 0, nl80211.NL80211_CMD_GET_INTERFACE, 0)
100    nl.nla_put_u32(m, nl80211.NL80211_ATTR_IFINDEX, nl.if_nametoindex("wlan0"))
101
102    err = nl.nl_send_auto_complete(s, m)
103    if err < 0:
104        nl.nlmsg_free(m)
105
106    while cbd.done > 0 and not err < 0:
107        err = nl.nl_recvmsgs(s, rx_cb)
108
109except Exception:
110    (t, v, tb) = sys.exc_info()
111    print(v.message)
112    traceback.print_tb(tb)
113