예제 #1
0
 def connect(self):
     try:
         self.omapi = Omapi(self.module.params['host'], self.module.params['port'], self.module.params['key_name'],
                            self.module.params['key'])
     except socket.error:
         e = get_exception()
         self.module.fail_json(msg="Unable to connect to OMAPI server: %s" % e)
예제 #2
0
 def __init__(self, omapi_key: str, ipv6: bool = False):
     self._omapi = Omapi(
         "127.0.0.1",
         7912 if ipv6 else 7911,
         b"omapi_key",
         omapi_key.encode("ascii"),
     )
예제 #3
0
 def connect(self):
     try:
         self.omapi = Omapi(self.module.params['host'], self.module.params['port'], self.module.params['key_name'],
                            self.module.params['key'])
     except binascii.Error:
         self.module.fail_json(msg="Unable to open OMAPI connection. 'key' is not a valid base64 key.")
     except OmapiError as e:
         self.module.fail_json(msg="Unable to open OMAPI connection. Ensure 'host', 'port', 'key' and 'key_name' "
                                   "are valid. Exception was: %s" % to_native(e))
     except socket.error as e:
         self.module.fail_json(msg="Unable to connect to OMAPI server: %s" % to_native(e))
예제 #4
0
    def post_init(self, **kwargs):
        # Get parameters from keyword arguments
        skip_modules = kwargs.get("skip_modules", False)
        skip_network = kwargs.get("skip_network", False)
        skip_ap = kwargs.get("skip_ap", False)
        # Extra options
        self.sdr_dev = kwargs.get("sdr_dev", "sdr0")
        #  self.lan_ip = kwargs.get("lan_ip", "192.168.13.1")
        self.lan_ip = kwargs.get("lan_ip", "10.0.0.1")
        #  gw_ip = kwargs.get("gw_ip", "134.226.55.211")
        gw_dev = kwargs.get("gw_dev", "eth0")
        ap_config_path = kwargs.get("ap_path",
                                    "/root/openwifi/hostapd-openwifi.conf")
        openwifi_path = kwargs.get("openwifi_path", "/root/openwifi")

        # Stop Network Manager
        bash("service network-manager stop")
        self._log("Stopped Network Manager")

        # If loading kernel modules
        if not skip_modules:
            fpga_dev_path = "/sys/bus/iio/devices/iio:device2"
            filter_file = "openwifi_ad9361_fir.ftr"

            # Load mac80211 kernel module
            bash("modprobe mac80211")
            self._log("Loaded 'mac80211' kernel module")

            # If the SDR kernel module is loaded
            if bool(bash("lsmod | grep sdr")):
                # Remove SDR kernel module
                bash("rmmod sdr")
                self._log("Removed 'sdr' kernel module")

            # List of custom kernel modules
            module_list = {
                "first_batch": ["xilinx_dma", "tx_intf", "ad9361_drv"],
                "second_batch":
                ["rx_intf", "openofdm_tx", "openofdm_rx", "xpu", "sdr"]
            }

            # Device configuration dictionary
            device_config = {
                "first_batch": {
                    "in_voltage_rf_bandwidth": "17500000",
                    "out_voltage_rf_bandwidth": "37500000",
                    "in_voltage_sampling_frequency": "40000000",
                    "out_voltage_sampling_frequency": "40000000",
                    "out_altvoltage0_RX_LO_frequency": "5240000000",
                    "out_altvoltage1_TX_LO_frequency": "5250000000"
                },
                "second_batch": {
                    "in_voltage_filter_fir_en": "1",
                    "out_voltage_filter_fir_en": "0",
                    "in_voltage0_gain_control_mode": "fast_attack",
                    "in_voltage1_gain_control_mode": "fast_attack",
                    "in_voltage0_hardwaregain": "70",
                    "in_voltage1_hardwaregain": "70",
                    "out_voltage0_hardwaregain": "89",
                    "out_voltage1_hardwaregain": "0"
                }
            }

            # Iterate over the first batch
            for submodule in module_list['first_batch']:
                # Check whether the module is loaded
                if bool(bash("lsmod | grep {0}".format(submodule))):
                    # Removing current version of the module
                    bash("rmmod {0}".format(submodule))
                # Installing new version of the module
                bash("insmod {0}/{1}.ko".format(openwifi_path, submodule))
                # Check installation of kernel module
                sleep(1)
                if not bash("lsmod | grep {0}".format(submodule)).code:
                    self._log("Loaded", submodule, "kernel module")

                else:
                    self._log("Not loaded", submodule, "kernel module")

            # Iterate over the first batch of parameters
            for parameter in device_config["first_batch"].keys():
                # Update the parameter value
                bash("echo {0} > {1}/{2}".format(
                    device_config["first_batch"][parameter], fpga_dev_path,
                    parameter))
                bash("sync")
                sleep(0.5)

            # Filter file string
            filter_str = "cat {0}/{1} > {2}/filter_fir_config"
            # Load filter response values
            bash(filter_str.format(openwifi_path, filter_file, fpga_dev_path))

            # Iterate over the second batch of parameters
            for parameter in device_config["second_batch"].keys():
                # Update the parameter value
                bash("echo {0} > {1}/{2}".format(
                    device_config["second_batch"][parameter], fpga_dev_path,
                    parameter))
                bash("sync")
                sleep(0.5)

            # Iterate over the second batch
            for submodule in module_list['second_batch']:
                # Check whether the module is loaded
                if bool(bash("lsmod | grep {0}".format(submodule))):
                    # Removing current version of the module
                    bash("rmmod {0}".format(submodule))

                # Installing new version of the module
                bash("insmod {0}/{1}.ko".format(openwifi_path, submodule))
                sleep(1)
                # Check installation of kernel module
                if not bash("lsmod | grep {0}".format(submodule)).code:
                    self._log("Loaded", submodule, "kernel module")

            # Sleep for 10 seconds and log event
            self._log("Waiting for configurations to take effect")
            sleep(10)
            self._log("Configured kernel modules and FPGA")

        # If configuring routing and networking
        if not skip_network:
            # Configure the SDR interface's IP
            bash("ifconfig {0} {1} netmask 255.255.255.0".format(
                self.sdr_dev, self.lan_ip))
            self._log("Set {0} interface IP's to: {1}".format(
                self.sdr_dev, self.lan_ip))

            # Set the default route through eth0
            #  bash("ip route add default via {0} dev eth0".format(gw_ip))
            bash("ip route add default dev {0}".format(gw_dev))
            #  self._log("Set default gateway to: {0}".format(gw_ip))
            self._log("Set default gateway to: {0}".format(gw_dev))

            # Sleep for 2 seconds and log event
            self._log("Configured routing and networking")

        # Stop DHCP server
        bash("service isc-dhcp-server stop")
        # Clear current leases
        bash("echo '' > /var/lib/dhcp/dhcpd.leases")
        # Start DHCP server
        bash("service isc-dhcp-server start")

        sleep(5)

        self._log("Restarted DHCP server")

        # Create a list of possible client IPs
        self.dhcp_pool = set(".".join(self.lan_ip.split(".")[:-1]) + "." +
                             str(x) for x in range(110, 200))

        # OMAPI Configuration parameters
        omapi_host = "127.0.0.1"
        omapi_port = 7911
        omapi_keyname = b"defomapi"
        omapi_key = b"SmR3+XVX95vDQ3SaZD1B7xTXYTcwNg/AZn9DAsJS" + \
            b"9oESudsTQ5bRMaSt bHPyOJchWlXF2Q6CuhSD70eTNl5hOg=="

        # Create OMAPI object
        self.omapi = Omapi(omapi_host, omapi_port, omapi_keyname, omapi_key)

        # If starting the access point
        if not skip_ap:
            # If there is a Host AP Daemon running in the background
            if bool(bash("ps -aux | grep [h]ostapd")):
                # Kill the process
                bash("killall hostapd")
                self._log("Stopped existing hostapd instances")

            # Run this 10 times or until hostapd starts
            for x in range(10):
                # Start Host AP Daemon in the background and log event
                apd = bash("hostapd -B {0}".format(ap_config_path)).code

                # If it worked
                if not apd:
                    break

                self._log("Trying to start the AP, #", x + 1)
                sleep(1)

            # If we could not configure the AP
            if apd:
                raise Exception("Could not configure AP")

            # Log event
            self._log("Configured access point")

        # TODO This ought to change in the future
        # List of currently support RAN slices
        self.ran_slice_list = [
            {
                "index": 0,
                "available": True
            },
            {
                "index": 1,
                "available": True
            },
            {
                "index": 2,
                "available": True
            },
            {
                "index": 3,
                "available": True
            },
        ]

        # Iterate over existing slices
        for ran_slice in self.ran_slice_list:
            # Set current slice index
            idx = bash("sdrctl dev {0} set slice_idx {1}".format(
                self.sdr_dev, ran_slice['index'])).code

            # Clear MAC addresses associated with slice
            cls = bash("sdrctl dev {0} set addr {1}".format(
                self.sdr_dev, "00000000")).code

            # Log event
            self._log("Failed clearing slice #" if cls else "Cleared slice #",
                      ran_slice["index"])

        # Sync all slices
        sync = bash("sdrctl dev {0} set slice_idx 4".format(self.sdr_dev)).code

        # Output status message and/or exit
        if not sync:
            self._log("Synchronised all RAN slices!")
        else:
            self._log("Failed synchronising RAN slices.")
            exit(10)