From e31864a0a75132c0308d74f49237892f54225f50 Mon Sep 17 00:00:00 2001 From: Milo Sobral Date: Fri, 26 May 2023 19:55:26 +0000 Subject: [PATCH] Added setup files and xdf utils to code --- portiloop/setup_files/create_ap.service | 12 ++++ portiloop/setup_files/create_ap0.sh | 28 ++++++++ portiloop/setup_files/dnsmasq.conf | 6 ++ portiloop/setup_files/hostapd.conf | 12 ++++ portiloop/setup_files/jupyter.service | 16 +++++ portiloop/setup_files/setup_tables.service | 12 ++++ portiloop/setup_files/setup_tables.sh | 22 +++++++ portiloop/src/file_utils.py | 75 ++++++++++++++++++++++ 8 files changed, 183 insertions(+) create mode 100644 portiloop/setup_files/create_ap.service create mode 100644 portiloop/setup_files/create_ap0.sh create mode 100644 portiloop/setup_files/dnsmasq.conf create mode 100644 portiloop/setup_files/hostapd.conf create mode 100644 portiloop/setup_files/jupyter.service create mode 100644 portiloop/setup_files/setup_tables.service create mode 100644 portiloop/setup_files/setup_tables.sh create mode 100644 portiloop/src/file_utils.py diff --git a/portiloop/setup_files/create_ap.service b/portiloop/setup_files/create_ap.service new file mode 100644 index 0000000..4f6a260 --- /dev/null +++ b/portiloop/setup_files/create_ap.service @@ -0,0 +1,12 @@ +[Unit] +Description=Create The Access Point for the coral +Before=hostapd.service dnsmasq.service +After=network-online.target +Wants=network-online.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/create_ap0.sh + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/portiloop/setup_files/create_ap0.sh b/portiloop/setup_files/create_ap0.sh new file mode 100644 index 0000000..b1daf95 --- /dev/null +++ b/portiloop/setup_files/create_ap0.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +# Get the name of the interface on phy1 +phy1_interface=$(sudo iw dev | awk '/phy#1/ {getline; print $2}') + +# Check if the interface name is p2p0 +if [[ $phy1_interface == "ap0" ]]; then + echo "ap0 already set up, not running script..." +else + echo $phy1_interface + # Delete the existing p2p0 interface + /sbin/iw dev $phy1_interface del + + # Reload the Network Manager utility + systemctl restart NetworkManager + + # Create a new ap0 interface in AP mode + /sbin/iw phy phy1 interface add ap0 type __ap + + # Disable power management for the ap0 interface + /sbin/iw dev ap0 set power_save off + + # Reload the Network Manager utility again + systemctl restart NetworkManager + + # Get an IPV4 address for the server + ifconfig ap0 192.168.4.1 up +fi diff --git a/portiloop/setup_files/dnsmasq.conf b/portiloop/setup_files/dnsmasq.conf new file mode 100644 index 0000000..da45093 --- /dev/null +++ b/portiloop/setup_files/dnsmasq.conf @@ -0,0 +1,6 @@ +# Configuration for Access Point +interface=ap0 +dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h +dhcp-option=3,192.168.4.1 +dhcp-option=6,192.168.4.1 +server=8.8.8.8 \ No newline at end of file diff --git a/portiloop/setup_files/hostapd.conf b/portiloop/setup_files/hostapd.conf new file mode 100644 index 0000000..9c2c0ad --- /dev/null +++ b/portiloop/setup_files/hostapd.conf @@ -0,0 +1,12 @@ +interface=ap0 +driver=nl80211 +ssid=YOUR-SSID-HERE +hw_mode=g +channel=6 +wpa=2 +wpa_passphrase=YOUR-PASSWORD-HERE +wpa_key_mgmt=WPA-PSK +wpa_pairwise=TKIP CCMP +rsn_pairwise=CCMP +auth_algs=1 +macaddr_acl=0 \ No newline at end of file diff --git a/portiloop/setup_files/jupyter.service b/portiloop/setup_files/jupyter.service new file mode 100644 index 0000000..a692d17 --- /dev/null +++ b/portiloop/setup_files/jupyter.service @@ -0,0 +1,16 @@ +[Unit] +Description=Jupyter Notebook Server +After=create_ap.service +After=hostapd.service +After=dnsmasq.service + +[Service] +Type=exec +ExecStart=/bin/bash -c "XDG_RUNTIME_DIR=/run/user/`id -u` /usr/bin/jupyter notebook --no-browser --ip 192.168.4.1 --port 8080 --notebook-dir=/home/mendel" +User=mendel +Group=mendel +Restart=on-failure +RestartSec=60s + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/portiloop/setup_files/setup_tables.service b/portiloop/setup_files/setup_tables.service new file mode 100644 index 0000000..eaaddab --- /dev/null +++ b/portiloop/setup_files/setup_tables.service @@ -0,0 +1,12 @@ +[Unit] +Description=Setup tables service +After=create_ap.service +Wants=network-online.target +After=network-online.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/setup_tables.sh + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/portiloop/setup_files/setup_tables.sh b/portiloop/setup_files/setup_tables.sh new file mode 100644 index 0000000..9973a05 --- /dev/null +++ b/portiloop/setup_files/setup_tables.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +echo "Telling kernel to turn on ipv4 ip_forwarding" +echo 1 > /proc/sys/net/ipv4/ip_forward +echo "Done. Setting up iptables rules to allow FORWARDING" + +DOWNSTREAM=ap0 # ap0 is client network (running hostapd) +UPSTREAM=wlan0 # upstream network (internet) + +# Allow IP Masquerading (NAT) of packets from clients (downstream) to upstream network (internet) +iptables -t nat -A POSTROUTING -o $UPSTREAM -j MASQUERADE + +# Forward packets from downstream clients to the upstream internet +iptables -A FORWARD -i $DOWNSTREAM -o $UPSTREAM -j ACCEPT + +# Forward packers from the internet to clients IF THE CONNECTION IS ALREADY OPEN! +iptables -A FORWARD -i $UPSTREAM -o $DOWNSTREAM -m state --state RELATED,ESTABLISHED -j ACCEPT + +# Setup the external DNS server +iptables -t nat -A PREROUTING -i $DOWNSTREAM -p udp --dport 53 -j DNAT --to-destination 8.8.8.8:53 + +echo "Done setting up iptables rules. Forwarding enabled" \ No newline at end of file diff --git a/portiloop/src/file_utils.py b/portiloop/src/file_utils.py new file mode 100644 index 0000000..d719403 --- /dev/null +++ b/portiloop/src/file_utils.py @@ -0,0 +1,75 @@ + +import numpy as np +import pyxdf +import os +import matplotlib.pyplot as plt +import pandas as pd + +STREAM_NAMES = { + 'filtered_data': 'Portiloop Filtered', + 'raw_data': 'Portiloop Raw Data', + 'stimuli': 'Portiloop_stimuli' +} + + +def read_xdf_file(xdf_file, channel): + """ + Read a single xdf file and return the data of the given channel as a dataframe. + """ + xdf_data, _ = pyxdf.load_xdf(xdf_file) + + # Load all streams given their names + filtered_stream, raw_stream, markers = None, None, None + for stream in xdf_data: + # print(stream['info']['name']) + if stream['info']['name'][0] == STREAM_NAMES['filtered_data']: + filtered_stream = stream + elif stream['info']['name'][0] == STREAM_NAMES['raw_data']: + raw_stream = stream + elif stream['info']['name'][0] == STREAM_NAMES['stimuli']: + markers = stream + + if filtered_stream is None or raw_stream is None: + raise ValueError("One of the necessary streams could not be found. Make sure that at least one signal stream is present in XDF recording") + + # Add all samples from raw and filtered signals + points = [] + diffs = [] + shortest_stream = min(int(filtered_stream['footer']['info']['sample_count'][0]), + int(raw_stream['footer']['info']['sample_count'][0])) + for i in range(shortest_stream): + if markers is not None: + datapoint = [filtered_stream['time_stamps'][i], + float(filtered_stream['time_series'][i, channel-1]), + raw_stream['time_series'][i, channel-1], + 0] + else: + datapoint = [filtered_stream['time_stamps'][i], + float(filtered_stream['time_series'][i, channel-1]), + raw_stream['time_series'][i, channel-1]] + diffs.append(abs(filtered_stream['time_stamps'][i] - raw_stream['time_stamps'][i])) + points.append(datapoint) + + # Add markers + columns = ["Time Stamps", "Filtered Signal", "Raw Signal"] + if markers is not None: + columns.append("Stimuli") + for time_stamp in markers['time_stamps']: + new_index = np.abs(filtered_stream['time_stamps'] - time_stamp).argmin() + points[new_index][3] = 1 + + # Create dataframe + array = np.array(points) + + return array, columns + + +def read_edf_file(edf_file): + """ + Read a single edf file and return the data as a dictionary. + """ + pass + + +if __name__ == "__main__": + pass \ No newline at end of file