Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp849242pxb; Sat, 16 Jan 2021 09:07:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJxVorUsaOc8uMoRMXViDrZBoH4RfiJp5upvZysSxMDar4b4W0Dsy3ipQS5CC+gW3kj+ycpl X-Received: by 2002:a17:906:144e:: with SMTP id q14mr12309052ejc.150.1610816847908; Sat, 16 Jan 2021 09:07:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610816847; cv=none; d=google.com; s=arc-20160816; b=XBKnPvmX4+zzYcdPmFsC8yW3fInH6NqI0qOob7DElT5JYca2KKGE/ogKfFvvgvx/K4 6hHUnn7EOdni1l/nDAY01uiOVJAstJSvsaBf3Bz8f1ogB9yIvRURs3j3L3RmqhSMdFa2 wDaLVRFHKIMwrqwFgto6ixvkVgcsFXjLba9ISpGZRV/gQgrJPdYP2O4NljgvQ71c2Znv HMr0lnHMwb0pYEDkMur4g1bi8TCqWCM3BwRrRGD0ip7qLFXR6QkvSr+D/GX5OSQolJXe tcuCt/L4IZ8eAo4ou6dYcBbY3VQ+dKuj1l5F+HdyK30a61DZ1E8SHFFtUanjyJhWQ4fp vFvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=OPoexJpyTUKfJ3D5bjlDeAhw+zScDhux7fS5Pqv2PrI=; b=ErJR4wBDe3qdTWPEW0SaogR4NkPk62l3mqTirZ/Cf5KCwcw7SSGRp95OuZOzhyrfcZ w08oGyHv+bUDmyP3AgZyu7blXb8iMjxltHBineR+nmvmdQUZi2TdZ0jV9glZVBCJDbMp lWKATFXNRI8w7RlQJ/ZUVYgquY/pXRqYly3QmQjphmdH9P2veFeyYMQjoXXDB+l7FYfH aI0cwC74sdGvxB2WNmuiEsv83uVp99U7zgK/qFaK5eSn+7JLD1EkXh/Bu78IeyQ/WesC AwKo0TL5p8eqw5AeK35qiZmp6A776DLbSioRd8xwAe1k/JFg/GiuSB8imKINuL8f7eWQ Oy1w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 35si5900668edm.126.2021.01.16.09.06.35; Sat, 16 Jan 2021 09:07:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726780AbhAPMx2 (ORCPT + 99 others); Sat, 16 Jan 2021 07:53:28 -0500 Received: from smail.rz.tu-ilmenau.de ([141.24.186.67]:36247 "EHLO smail.rz.tu-ilmenau.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726603AbhAPMx2 (ORCPT ); Sat, 16 Jan 2021 07:53:28 -0500 Received: from legolas.fritz.box (unknown [84.174.243.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smail.rz.tu-ilmenau.de (Postfix) with ESMTPSA id 46AD258008E; Sat, 16 Jan 2021 13:52:04 +0100 (CET) From: Markus Theil To: johannes@sipsolutions.net Cc: linux-wireless@vger.kernel.org, Markus Theil Subject: [PATCH 3/4] rfkill: support hard block reason in python code Date: Sat, 16 Jan 2021 13:51:45 +0100 Message-Id: <20210116125146.31932-4-markus.theil@tu-ilmenau.de> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210116125146.31932-1-markus.theil@tu-ilmenau.de> References: <20210116125146.31932-1-markus.theil@tu-ilmenau.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org Signed-off-by: Markus Theil --- rfkill.py | 60 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/rfkill.py b/rfkill.py index 983671f..57d21b0 100755 --- a/rfkill.py +++ b/rfkill.py @@ -28,6 +28,9 @@ import os _OP_CHANGE, _OP_CHANGE_ALL) = list(range(4)) +HARD_BLOCK_SIGNAL = 1 << 0 +HARD_BLOCK_NOT_OWNER = 1 << 1 + _type_names = { TYPE_ALL: "all", TYPE_WLAN: "Wireless LAN", @@ -40,10 +43,14 @@ _type_names = { TYPE_NFC: "NFC", } -# idx, type, op, soft, hard -_event_struct = '@IBBBB' +# idx, type, op, soft, hard, hard_block_reasons +_event_struct = '@IBBBBB' _event_sz = struct.calcsize(_event_struct) +# idx, type, op, soft, hard +_event_struct_old = '@IBBBB' +_event_old_sz = struct.calcsize(_event_struct_old) + class RFKillException(Exception): pass @@ -63,7 +70,7 @@ class RFKill(object): @property def type(self): if not self._type: - for r, s, h in RFKill.list(): + for r, s, h, hbr in RFKill.list(): if r.idx == self.idx: self._type = r._type break @@ -76,7 +83,7 @@ class RFKill(object): @property def blocked(self): l = RFKill.list() - for r, s, h in l: + for r, s, h, hbr in l: if r.idx == self.idx: return (s, h) raise RFKillException("RFKill instance no longer exists") @@ -97,48 +104,60 @@ class RFKill(object): return self.blocked[1] def block(self): - rfk = open('/dev/rfkill', 'wb') - s = struct.pack(_event_struct, self.idx, TYPE_ALL, _OP_CHANGE, 1, 0) + rfk = open('/dev/rfkill', 'wb', 0) + s = struct.pack(_event_struct, self.idx, TYPE_ALL, _OP_CHANGE, 1, 0, 0) rfk.write(s) rfk.close() def unblock(self): - rfk = open('/dev/rfkill', 'wb') - s = struct.pack(_event_struct, self.idx, TYPE_ALL, _OP_CHANGE, 0, 0) + rfk = open('/dev/rfkill', 'wb', 0) + s = struct.pack(_event_struct, self.idx, TYPE_ALL, _OP_CHANGE, 0, 0, 0) rfk.write(s) rfk.close() @classmethod def block_all(cls, t=TYPE_ALL): - rfk = open('/dev/rfkill', 'wb') + rfk = open('/dev/rfkill', 'wb', 0) print(rfk) - s = struct.pack(_event_struct, 0, t, _OP_CHANGE_ALL, 1, 0) + s = struct.pack(_event_struct, 0, t, _OP_CHANGE_ALL, 1, 0, 0) rfk.write(s) rfk.close() @classmethod def unblock_all(cls, t=TYPE_ALL): - rfk = open('/dev/rfkill', 'wb') - s = struct.pack(_event_struct, 0, t, _OP_CHANGE_ALL, 0, 0) + rfk = open('/dev/rfkill', 'wb', 0) + s = struct.pack(_event_struct, 0, t, _OP_CHANGE_ALL, 0, 0, 0) rfk.write(s) rfk.close() @classmethod def list(cls): res = [] - rfk = open('/dev/rfkill', 'rb') + rfk = open('/dev/rfkill', 'rb', 0) + fd = rfk.fileno() flgs = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, flgs | os.O_NONBLOCK) while True: try: d = rfk.read(_event_sz) - _idx, _t, _op, _s, _h = struct.unpack(_event_struct, d) + read_len = len(d) + assert read_len in [_event_sz, _event_old_sz] + + # init additional fields of newer formats to 'None' here + _hbr = None + + # hard block reason included ? + if read_len >= _event_sz: + _idx, _t, _op, _s, _h, _hbr = struct.unpack(_event_struct, d) + else: + _idx, _t, _op, _s, _h = struct.unpack(_event_struct_old, d) + if _op != _OP_ADD: continue r = RFKill(_idx) r._type = _t - res.append((r, _s, _h)) + res.append((r, _s, _h, _hbr)) except IOError: break except TypeError: @@ -146,7 +165,16 @@ class RFKill(object): return res if __name__ == "__main__": - for r, s, h in RFKill.list(): + for r, s, h, hbr in RFKill.list(): print("%d: %s: %s" % (r.idx, r.name, r.type_name)) print("\tSoft blocked: %s" % ("yes" if s else "no")) print("\tHard blocked: %s" % ("yes" if h else "no")) + if hbr != None: + print("\tHard block reasons: ", end="") + if hbr == 0: + print("[NONE]", end="") + if hbr & HARD_BLOCK_NOT_OWNER: + print("[NOT_OWNER]", end="") + if hbr & HARD_BLOCK_SIGNAL: + print("[SIGNAL]", end="") + print() -- 2.30.0