]> granicus.if.org Git - esp-idf/commitdiff
tiny-test-fw: implement `env.get_pc_nic_info`:
authorHe Yin Ling <heyinling@espressif.com>
Sun, 7 Jan 2018 12:24:38 +0000 (20:24 +0800)
committerbot <bot@espressif.com>
Tue, 15 May 2018 03:29:31 +0000 (03:29 +0000)
this method is used to get mac/ipv4/ipv6 address for one NIC

tools/tiny-test-fw/Env.py
tools/tiny-test-fw/docs/index.rst

index da5c0b5982008dcee7e5a818d46e1cce4488689a..fa194d10c97f121084d5b757c914b1bafc8f878a 100644 (file)
@@ -17,6 +17,8 @@ import os
 import threading
 import functools
 
+import netifaces
+
 import EnvConfig
 
 
@@ -130,17 +132,34 @@ class Env(object):
         """
         return self.config.get_variable(variable_name)
 
+    PROTO_MAP = {
+        "ipv4": netifaces.AF_INET,
+        "ipv6": netifaces.AF_INET6,
+        "mac": netifaces.AF_LINK,
+    }
+
     @_synced
-    def get_pc_nic_info(self, nic_name="pc_nic"):
+    def get_pc_nic_info(self, nic_name="pc_nic", proto="ipv4"):
         """
         get_pc_nic_info(nic_name="pc_nic")
-        try to get nic info (ip address, ipv6 address, mac address)
+        try to get info of a specified NIC and protocol.
 
-        :param nic_name: pc nic name. allows passing variable name, nic name value or omitted (to get default nic info).
-        :return: a dict of address ("ipv4", "ipv6", "mac") if successfully found. otherwise None.
+        :param nic_name: pc nic name. allows passing variable name, nic name value.
+        :param proto: "ipv4", "ipv6" or "mac"
+        :return: a dict of nic info if successfully found. otherwise None.
+                 nic info keys could be different for different protocols.
+                 key "addr" is available for both mac, ipv4 and ipv6 pic info.
         """
-        # TODO: need to implement auto get nic info method
-        return self.config.get_variable("nic_info/" + nic_name)
+        interfaces = netifaces.interfaces()
+        if nic_name in interfaces:
+            # the name is in the interface list, we regard it as NIC name
+            if_addr = netifaces.ifaddresses(nic_name)
+        else:
+            # it's not in interface name list, we assume it's variable name
+            _nic_name = self.get_variable(nic_name)
+            if_addr = netifaces.ifaddresses(_nic_name)
+
+        return if_addr[self.PROTO_MAP[proto]][0]
 
     @_synced
     def close(self):
index a70e6bf328e01b793f0a4a96d2c544f0dc2a302d..22cfd73a86dddf34943d5e5a83ad52c826f79d2f 100644 (file)
@@ -128,6 +128,7 @@ The following 3rd party lib is required:
     * pyserial
     * pyyaml
     * xunitgen
+    * netifaces
 
 To build document, we need to install ``Sphinx`` and ``sphinx-rtd-theme`` (you may replace this with your own theme).