Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add USB Ethernet Gadget toggle #933

Open
wants to merge 12 commits into
base: qml
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion src/OptionsPopup.qml
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ Window {
}
}

if (imageWriter.andSupportedFeatures("ether_gadget")) {
if (imageWriter.andCapabilities("usb_otg")) {
deviceUsbOtgSupport = true
} else {
deviceUsbOtgSupport = false
Expand Down
10 changes: 8 additions & 2 deletions src/downloadthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,11 +992,17 @@ bool DownloadThread::_customizeImage()
// little optimization for memory constrained systems
modulesConf.clear();

QByteArray controllScript = _fileGetContentsTrimmed("://extraFiles/rpi-usb-ether-gadget.sh");
fat->writeFile("uethc.sh", controllScript);
controllScript.clear();

// add config.txt change - \n prefix to also work if user defined config doesn't end with a LF
_config.append("\ndtoverlay=dwc2,dr_mode=peripheral\n");
_firstrun.append("\nmv /boot/firmware/10usb.net /etc/systemd/network/10-usb.network\n\n");
_firstrun.append("\nmv /boot/firmware/10usb.net /etc/systemd/network/10-usb.network\n");
_firstrun.append("mv /boot/firmware/geth.cnf /etc/modprobe.d/g_ether.conf\n");
_firstrun.append("mv /boot/firmware/gemod.cnf /etc/modules-load.d/usb-ether-gadget.conf\n\n");
_firstrun.append("mv /boot/firmware/gemod.cnf /etc/modules-load.d/usb-ether-gadget.conf\n");
_firstrun.append("mv /boot/firmware/uethc.sh /usr/bin/rpi-usb-ether-gadget\n");
_firstrun.append("chmod +x /usr/bin/rpi-usb-ether-gadget\n\n");
_firstrun.append("SERIAL=$(grep Serial /proc/cpuinfo | awk '{print $3}')\n");
_firstrun.append("sed -i \"s/<serial>/$SERIAL/g\" /etc/modprobe.d/g_ether.conf\n");
_firstrun.append("systemctl enable systemd-networkd\n\n");
Expand Down
1 change: 1 addition & 0 deletions src/extraFiles.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<file>extraFiles/10-usb.network</file>
<file>extraFiles/g_ether.conf</file>
<file>extraFiles/usb-ether-gadget.conf</file>
<file>extraFiles/rpi-usb-ether-gadget.sh</file>
paulober marked this conversation as resolved.
Show resolved Hide resolved
</qresource>
</RCC>
50 changes: 50 additions & 0 deletions src/extraFiles/rpi-usb-ether-gadget.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

# check if the script is run as root
if [ "$EUID" -ne 0 ]; then
echo -e "\e[31mPlease run this script as root\e[0m"
exit
fi

# check if the script is run on a Raspberry Pi
if ! grep -q "Raspberry Pi" /proc/device-tree/model; then
echo -e "\e[31mThis script is for Raspberry Pi only\e[0m"
exit
fi

FORCE_ON=false
FORCE_OFF=false
paulober marked this conversation as resolved.
Show resolved Hide resolved
# check if command line arguments to force on or to force off are provided if not continue with
# check else force
if [ "$1" == "on" ]; then
FORCE_ON=true
elif [ "$1" == "off" ]; then
FORCE_OFF=true
elif [ "$1" == "toggle" ]; then
: # Do nothing and continue to the code below
elif [ "$1" == "status" ]; then
if [ -f /etc/modules-load.d/usb-ether-gadget.conf ]; then
echo -e "\e[33mUSB Ethernet Gadget is on\e[0m"
else
echo -e "\e[33mUSB Ethernet Gadget is off\e[0m"
fi
exit
else # add else if to make toggling possible without toggle keyword
echo "Usage: rpi-usb-ether-gadget [on|off|toggle|status|help]"
exit
fi

#rm /etc/modprobe.d/g_ether.conf

# check if /etc/modules-load.d/usb-ether-gadget.conf exists to know if to turn of or on or the force flags are set
paulober marked this conversation as resolved.
Show resolved Hide resolved
if ([ -f /etc/modules-load.d/usb-ether-gadget.conf ] && [ "$FORCE_ON" = false ]) || [ "$FORCE_OFF" = true ]; then
paulober marked this conversation as resolved.
Show resolved Hide resolved
echo "Turning \e[31moff\e[0m USB Ethernet Gadget mode"
rm -f /etc/modules-load.d/usb-ether-gadget.conf
sed -i '/dtoverlay=dwc2,dr_mode=peripheral/d' /boot/firmware/config.txt
else
echo "Turning \e[32mon\e[0m USB Ethernet Gadget mode"
echo "dwc2\ng_ether\n" > /etc/modules-load.d/usb-ether-gadget.conf
echo "\ndtoverlay=dwc2,dr_mode=peripheral\n" >> /boot/firmware/config.txt
fi

echo "Reboot to apply changes"
22 changes: 11 additions & 11 deletions src/imagewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,15 @@ void ImageWriter::setHWFilterList(const QByteArray &json, const bool &inclusive)
_deviceFilterIsInclusive = inclusive;
}

void ImageWriter::setHWSupportedFeaturesList(const QByteArray &json) {
void ImageWriter::setHWCapabilitiesList(const QByteArray &json) {
QJsonDocument json_document = QJsonDocument::fromJson(json);
// TODO: maybe also clear the sw supported features as in the UI the OS is unselected when this changes
_hwSupportedFeatures = json_document.array();
// TODO: maybe also clear the sw capabilities as in the UI the OS is unselected when this changes
_hwCapabilities = json_document.array();
}

void ImageWriter::setSWSupportedFeaturesList(const QByteArray &json) {
void ImageWriter::setSWCapabilitiesList(const QByteArray &json) {
QJsonDocument json_document = QJsonDocument::fromJson(json);
_swSupportedFeatures = json_document.array();
_swCapabilities = json_document.array();
}

QJsonArray ImageWriter::getHWFilterList() {
Expand All @@ -502,16 +502,16 @@ bool ImageWriter::getHWFilterListInclusive() {
return _deviceFilterIsInclusive;
}

bool ImageWriter::andSupportedFeatures(const QString &feature) {
return this->checkHWFeatureSupport(feature) && this->checkSWFeatureSupport(feature);
bool ImageWriter::andCapabilities(const QString &cap) {
return this->checkHWCapability(cap) && this->checkSWCapability(cap);
paulober marked this conversation as resolved.
Show resolved Hide resolved
}

bool ImageWriter::checkHWFeatureSupport(const QString &feature) {
return _hwSupportedFeatures.contains(feature.toLower());
bool ImageWriter::checkHWCapability(const QString &cap) {
return _hwCapabilities.contains(cap.toLower());
}

bool ImageWriter::checkSWFeatureSupport(const QString &feature) {
return _swSupportedFeatures.contains(feature.toLower());
bool ImageWriter::checkSWCapability(const QString &cap) {
return _swCapabilities.contains(cap.toLower());
}

void ImageWriter::handleNetworkRequestFinished(QNetworkReply *data) {
Expand Down
16 changes: 8 additions & 8 deletions src/imagewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ class ImageWriter : public QObject
/** Set the HW filter, for a filtered view of the OS list */
Q_INVOKABLE void setHWFilterList(const QByteArray &json, const bool &inclusive);

/* Set the features supported by the hardware, for a filtered view of options that require certain features beeing supported by the hardware. */
Q_INVOKABLE void setHWSupportedFeaturesList(const QByteArray &json);
/* Set the capabilities supported by the hardware, for a filtered view of options that require the hardware to have certain capabilities. */
Q_INVOKABLE void setHWCapabilitiesList(const QByteArray &json);

/* Set the features supported by the hardware, for a filtered view of options that require certain features beeing supported by the software. */
Q_INVOKABLE void setSWSupportedFeaturesList(const QByteArray &json);
/* Set the capabilities supported by the hardware, for a filtered view of options that require the software to have certain capabilities. */
Q_INVOKABLE void setSWCapabilitiesList(const QByteArray &json);

/* Get the HW filter list */
Q_INVOKABLE QJsonArray getHWFilterList();
Expand All @@ -103,13 +103,13 @@ class ImageWriter : public QObject
Q_INVOKABLE bool getHWFilterListInclusive();

/* Get if both hard and software support a certain feature */
Q_INVOKABLE bool andSupportedFeatures(const QString &feature);
Q_INVOKABLE bool andCapabilities(const QString &cap);

/* Check if the hardware supports a certain feature. */
Q_INVOKABLE bool checkHWFeatureSupport(const QString &feature);
Q_INVOKABLE bool checkHWCapability(const QString &cap);

/* Check if the software supports a certain feature. */
Q_INVOKABLE bool checkSWFeatureSupport(const QString &feature);
Q_INVOKABLE bool checkSWCapability(const QString &cap);

/* Set custom cache file */
void setCustomCacheFile(const QString &cacheFile, const QByteArray &sha256);
Expand Down Expand Up @@ -208,7 +208,7 @@ protected slots:
void fillSubLists(QJsonArray &topLevel);
QNetworkAccessManager _networkManager;
QJsonDocument _completeOsList;
QJsonArray _deviceFilter, _hwSupportedFeatures, _swSupportedFeatures;
QJsonArray _deviceFilter, _hwCapabilities, _swCapabilities;
bool _deviceFilterIsInclusive;

protected:
Expand Down
12 changes: 5 additions & 7 deletions src/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -1489,9 +1489,8 @@ ApplicationWindow {
if ("subitems" in entry) {
entry["subitems_json"] = JSON.stringify(entry["subitems"])
delete entry["subitems"]
} else if ("supportedFeatures" in entry) {
entry["supported_features_json"] = JSON.stringify(entry["supportedFeatures"])
delete entry["supportedFeatures"]
} else if ("capabilities" in entry) {
entry["capabilities"] = JSON.stringify(entry["capabilities"])
}
}

Expand Down Expand Up @@ -1534,8 +1533,7 @@ ApplicationWindow {
for (var j in devices)
{
devices[j]["tags"] = JSON.stringify(devices[j]["tags"])
devices[j]["supported_features"] = JSON.stringify(devices[j]["supportedFeatures"])
delete devices[j]["supportedFeatures"];
devices[j]["capabilities"] = JSON.stringify(devices[j]["capabilities"])
deviceModel.append(devices[j])
if ("default" in devices[j] && devices[j]["default"])
{
Expand Down Expand Up @@ -1621,7 +1619,7 @@ ApplicationWindow {
}

imageWriter.setHWFilterList(hwmodel.tags, inclusive)
imageWriter.setHWSupportedFeaturesList(hwmodel.supported_features);
imageWriter.setHWCapabilitiesList(hwmodel.capabilities);

/* Reload list */
var oslist_json = imageWriter.getFilteredOSlist();
Expand Down Expand Up @@ -1738,7 +1736,7 @@ ApplicationWindow {
}
} else {
imageWriter.setSrc(d.url, d.image_download_size, d.extract_size, typeof(d.extract_sha256) != "undefined" ? d.extract_sha256 : "", typeof(d.contains_multiple_files) != "undefined" ? d.contains_multiple_files : false, ospopup.categorySelected, d.name, typeof(d.init_format) != "undefined" ? d.init_format : "")
imageWriter.setSWSupportedFeaturesList(d.supported_features_json);
imageWriter.setSWCapabilitiesList(d.capabilities)
osbutton.text = d.name
ospopup.close()
osswipeview.decrementCurrentIndex()
Expand Down