Skip to content

Commit

Permalink
[WIP] MATLAB bindings for v1
Browse files Browse the repository at this point in the history
Signed-off-by: Travis F. Collins <travis.collins@analog.com>
  • Loading branch information
tfcollins committed Aug 15, 2024
1 parent b91f347 commit 3c6550a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
23 changes: 1 addition & 22 deletions .github/workflows/matlab.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion bindings/matlab/test/TestContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
73 changes: 73 additions & 0 deletions bindings/matlab/test/emu_manager.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3c6550a

Please sign in to comment.