]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
tests: Introduce hwsim radio context manager
authorJohannes Berg <johannes.berg@intel.com>
Mon, 20 Oct 2014 10:00:05 +0000 (12:00 +0200)
committerJouni Malinen <j@w1.fi>
Tue, 21 Oct 2014 14:35:20 +0000 (17:35 +0300)
The new HWSimRadio context manager allows the following
syntax to create (and appropriately destroy) a new radio:

  with HWSimRadio([...]) as (radio_id, iface_name):
    [...]

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
tests/hwsim/hwsim.py

index 5fe91b5d8a46d0f4e6c33d6c445662050b1d8fea..e21c814f2e8e6928a61ac09d34b72d14884b2bdb 100644 (file)
@@ -7,7 +7,7 @@
 # This software may be distributed under the terms of the BSD license.
 # See README for more details.
 
-import netlink
+import netlink, os
 
 # constants
 HWSIM_CMD_CREATE_RADIO         = 4
@@ -48,6 +48,32 @@ class HWSimController(object):
                                   attrs = attrs)
         msg.send_and_recv(self._conn)
 
+class HWSimRadio(object):
+    def __init__(self, n_channels=None, use_chanctx=False,
+                 use_p2p_device=False):
+        self._controller = HWSimController()
+        self._n_channels = n_channels
+        self._use_chanctx = use_chanctx
+        self._use_p2p_dev = use_p2p_device
+
+    def __enter__(self):
+        self._radio_id = self._controller.create_radio(
+              n_channels=self._n_channels,
+              use_chanctx=self._use_chanctx,
+              use_p2p_device=self._use_p2p_dev)
+        if self._radio_id < 0:
+            raise Exception("Failed to create radio (err:%d)" % self._radio_id)
+        try:
+            iface = os.listdir('/sys/class/mac80211_hwsim/hwsim%d/net/' % self._radio_id)[0]
+        except Exception,e:
+            self._controller.destroy_radio(self._radio_id)
+            raise e
+        return self._radio_id, iface
+
+    def __exit__(self, type, value, traceback):
+        self._controller.destroy_radio(self._radio_id)
+
+
 def create(args):
     print 'Created radio %d' % c.create_radio(n_channels=args.channels,
                                               use_chanctx=args.chanctx)