diff --git a/.github/workflows/matlab.yml b/.github/workflows/matlab.yml index db52e2d7d..3836a35c5 100644 --- a/.github/workflows/matlab.yml +++ b/.github/workflows/matlab.yml @@ -26,22 +26,10 @@ jobs: run: | sudo apt-get -qq update sudo apt-get install -y git cmake graphviz libavahi-common-dev libavahi-client-dev libaio-dev libusb-1.0-0-dev libxml2-dev rpm tar bzip2 gzip flex bison git libzstd-dev - # Change LD_LIBRARY PATH TO USE MATLAB GLIBC - # Find location of libstdc++.so.6 of MATLAB - #LDLIBMATLAB=$(find /opt/hostedtoolcache/MATLAB/ -name libstdc++.so.6 | grep -v orig) - #echo "LDLIBMATLAB=$LDLIBMATLAB" echo "IIO_EMU_PREFIX='LD_LIBRARY_PATH=/lib/x86_64-linux-gnu/libstdc++.so.6'" >> $GITHUB_ENV - #export LIBRARY_PATH=$LDLIBMATLAB:$LD_LIBRARY_PATH - # export LD_LIBRARY_PATH=$LDLIBMATLAB - cmake . -DHAVE_DNS_SD=OFF -DPYTHON_BINDINGS=ON -DCMAKE_CROSSCOMPILING=ON + cmake . -DHAVE_DNS_SD=OFF make sudo make install - sudo make clean - cd bindings/python - ls - sudo rm -rf pylibiio.egg-info - pip install . - cd ../.. mkdir edeps cd edeps git clone https://github.com/analogdevicesinc/libtinyiiod.git @@ -62,15 +50,6 @@ jobs: cd ../.. cd .. - - name: Install pytest-libiio interface - run: | - pip install pytest pyyaml lxml click - git clone https://github.com/tfcollins/pytest-libiio.git -b tfcollins/glibc - mv pytest-libiio/pytest_libiio bindings/matlab/ - cd bindings/matlab - python -c 'import pytest_libiio.plugin' - cd ../.. - - name: Run Tests uses: matlab-actions/run-command@v2 with: diff --git a/bindings/matlab/test/TestContext.m b/bindings/matlab/test/TestContext.m index 536f2882d..644018aea 100644 --- a/bindings/matlab/test/TestContext.m +++ b/bindings/matlab/test/TestContext.m @@ -26,7 +26,7 @@ function startEmulation(testCase) disp(p); % Start background process xmlfile = fullfile(folder, 'pluto.xml'); - testCase.emu = py.pytest_libiio.plugin.iio_emu_manager(xmlfile); + testCase.emu = py.emu_manager.iio_emu_manager(xmlfile); disp(testCase.emu.uri) testCase.emu.start(); disp('iio-emu started'); diff --git a/bindings/matlab/test/emu_manager.py b/bindings/matlab/test/emu_manager.py new file mode 100644 index 000000000..a75646533 --- /dev/null +++ b/bindings/matlab/test/emu_manager.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- + +import os +import pathlib +import signal +import socket +import subprocess +import time +from shutil import which + + +class iio_emu_manager: + def __init__( + self, xml_path: str, auto: bool = True, rx_dev: str = None, tx_dev: str = None, + ): + self.xml_path = xml_path + self.rx_dev = rx_dev + self.tx_dev = tx_dev + self.current_device = None + self.auto = auto + self.data_devices = None + + iio_emu = which("iio-emu") is None + if iio_emu: + raise Exception("iio-emu not found on path") + + hostname = socket.gethostname() + self.local_ip = socket.gethostbyname(hostname) + self.uri = f"ip:{self.local_ip}" + self.p = None + if os.getenv("IIO_EMU_URI"): + self.uri = os.getenv("IIO_EMU_URI") + print('IIO_EMU_PREFIX', os.getenv("IIO_EMU_PREFIX")) + if os.getenv("IIO_EMU_PREFIX"): + self.prefix = os.getenv("IIO_EMU_PREFIX") + self.prefix = self.prefix.replace("'","") + print(f"Using IIO_EMU_PREFIX: {self.prefix}") + else: + self.prefix = None + + + def __del__(self): + if self.p: + self.stop() + + def start(self): + with open("data.bin", "w"): + pass + cmd = ["iio-emu", "generic", self.xml_path] + if self.prefix: + cmd = [self.prefix] + cmd + if self.data_devices: + for dev in self.data_devices: + cmd.append(f"{dev}@data.bin") + print(cmd) + self.p = subprocess.Popen(' '.join(cmd), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid) + time.sleep(3) # wait for server to boot + # With shell stopping or checking if the process is stopped is hard + # It should have a process group if its running. + try: + pidg = os.getpgid(self.p.pid) + failed_to_start = False + except ProcessLookupError: + failed_to_start = True # stop multiple exceptions appearing + + if failed_to_start: + self.p = None + raise Exception("iio-emu failed to start... exiting") + + def stop(self): + if self.p: + os.killpg(os.getpgid(self.p.pid), signal.SIGTERM) + self.p = None