Improve USBMS handling

Finally applying @NiLuJe's suggestion!
This commit is contained in:
Nicolas Mailloux 2022-02-12 23:54:46 -05:00
parent 30c71a954a
commit 76327f4958
8 changed files with 71 additions and 46 deletions

View file

@ -77,7 +77,6 @@ alert::alert(QWidget *parent) :
ui->warningLabel->setText("Please charge your eReader."); ui->warningLabel->setText("Please charge your eReader.");
ui->securityLabel->setText("The battery's charge level is critical."); ui->securityLabel->setText("The battery's charge level is critical.");
ui->messageLabel->setText("To prevent damage, your device has been turned off.\nPlease consider charging it."); ui->messageLabel->setText("To prevent damage, your device has been turned off.\nPlease consider charging it.");
poweroff(false);
} }
ui->warningLabel->setStyleSheet("QLabel { background-color : black; color : white; font-size: 16pt}"); ui->warningLabel->setStyleSheet("QLabel { background-color : black; color : white; font-size: 16pt}");

View file

@ -71,5 +71,7 @@
<file>resources/online-library.png</file> <file>resources/online-library.png</file>
<file>resources/online-library-inverted.png</file> <file>resources/online-library-inverted.png</file>
<file>resources/cover_unavailable.png</file> <file>resources/cover_unavailable.png</file>
<file>resources/clock-inverted.png</file>
<file>resources/clock.png</file>
</qresource> </qresource>
</RCC> </RCC>

View file

@ -15,6 +15,7 @@
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h>
// WoW, global variables and namespaces are awesome // WoW, global variables and namespaces are awesome
namespace global { namespace global {
@ -206,6 +207,7 @@ namespace {
fprintf(stderr, "Error opening ntx_io device\n"); fprintf(stderr, "Error opening ntx_io device\n");
} }
ioctl(light, 241, value); ioctl(light, 241, value);
close(light);
} }
int int_checkconfig(QString file) { int int_checkconfig(QString file) {
if(QFile::exists(file)) { if(QFile::exists(file)) {
@ -666,6 +668,17 @@ namespace {
bool getEncFSStatus() { bool getEncFSStatus() {
return checkconfig("/external_root/run/encfs_mounted"); return checkconfig("/external_root/run/encfs_mounted");
} }
bool isUsbPluggedIn() {
// Thanks to https://github.com/koreader/KoboUSBMS/blob/2efdf9d920c68752b2933f21c664dc1afb28fc2e/usbms.c#L148-L158
int ntxfd;
if((ntxfd = open("/dev/ntx_io", O_RDWR)) == -1) {
fprintf(stderr, "Error opening ntx_io device\n");
}
unsigned long ptr = 0U;
ioctl(ntxfd, 108, &ptr);
close(ntxfd);
return !!ptr;
}
} }
#endif // FUNCTIONS_H #endif // FUNCTIONS_H

View file

@ -348,15 +348,13 @@ MainWindow::MainWindow(QWidget *parent)
connect(usbmsPrompt, &QTimer::timeout, [&]() { connect(usbmsPrompt, &QTimer::timeout, [&]() {
if(checkconfig("/opt/inkbox_genuine") == true) { if(checkconfig("/opt/inkbox_genuine") == true) {
if(global::usbms::showUsbmsDialog != true) { if(global::usbms::showUsbmsDialog != true) {
string_checkconfig_ro("/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/status"); if(isUsbPluggedIn() != usbmsStatus) {
if(usbmsStatus != checkconfig_str_val) {
global::usbms::showUsbmsDialog = true; global::usbms::showUsbmsDialog = true;
} }
} }
else { else {
string_checkconfig_ro("/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/status"); usbmsStatus = isUsbPluggedIn();
usbmsStatus = checkconfig_str_val; if(usbmsStatus == false) {
if(usbmsStatus != "Charging\n") {
// Loop again... // Loop again...
; ;
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

BIN
resources/clock.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

View file

@ -14,8 +14,8 @@ usbms_splash::usbms_splash(QWidget *parent) :
ui->setupUi(this); ui->setupUi(this);
// Getting the screen's size // Getting the screen's size
float sW = QGuiApplication::screens()[0]->size().width(); sW = QGuiApplication::screens()[0]->size().width();
float sH = QGuiApplication::screens()[0]->size().height(); sH = QGuiApplication::screens()[0]->size().height();
// Defining what the default icon size will be // Defining what the default icon size will be
if(global::kobox::showKoboxSplash == true) { if(global::kobox::showKoboxSplash == true) {
@ -97,27 +97,31 @@ void usbms_splash::usbms_launch()
proc_1->waitForFinished(); proc_1->waitForFinished();
proc_1->deleteLater(); proc_1->deleteLater();
bool exitUsbMsDone = false;
QTimer *usbms_t = new QTimer(this); QTimer *usbms_t = new QTimer(this);
usbms_t->setInterval(1000); usbms_t->setInterval(1000);
connect(usbms_t, &QTimer::timeout, [&]() { connect(usbms_t, &QTimer::timeout, [&]() {
QString prog ("mass_storage.sh"); if(exitUsbMsDone == false) {
QStringList args; if(isUsbPluggedIn() == false) {
QProcess *proc = new QProcess();
proc->start(prog, args);
proc->waitForFinished();
proc->deleteLater();
QFile modules("/tmp/usbevent");
modules.open(QIODevice::ReadWrite);
QTextStream in (&modules);
const QString content = in.readAll();
std::string contentstr = content.toStdString();
modules.close();
if(contentstr.find("1") != std::string::npos) {
if(global::usbms::koboxExportExtensions == true) { if(global::usbms::koboxExportExtensions == true) {
reboot(false); reboot(false);
} }
else { else {
qDebug() << "Exiting USBMS session...";
ui->label->setText("Processing content");
ui->label_3->setText("Please wait");
ui->label->setStyleSheet("QLabel { background-color : black; color : white; font-size: 15pt }");
ui->label_3->setStyleSheet("QLabel { background-color : black; color : white; font-size: 11pt }");
float stdIconWidth = sW / 1.5;
float stdIconHeight = sH / 1.5;
QPixmap pixmap(":/resources/clock-inverted.png");
QPixmap scaledPixmap = pixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
ui->label_2->setPixmap(scaledPixmap);
this->repaint();
QString prog("rmmod"); QString prog("rmmod");
QStringList args; QStringList args;
args << "g_mass_storage"; args << "g_mass_storage";
@ -126,23 +130,14 @@ void usbms_splash::usbms_launch()
proc->waitForFinished(); proc->waitForFinished();
proc->deleteLater(); proc->deleteLater();
// Restarting USBNet QTimer::singleShot(500, this, SLOT(restartServices()));
// NOTE: USBNet is only started if required conditions are met (see https://github.com/Kobo-InkBox/rootfs/blob/master/etc/init.d/usbnet)
string_writeconfig("/opt/ibxd", "usbnet_start\n");
QThread::msleep(1000);
// Mounting onboard storage
string_writeconfig("/opt/ibxd", "onboard_mount\n");
QThread::msleep(1000);
// Checking for updates
string_writeconfig("/opt/ibxd", "update_inkbox_restart\n");
QThread::msleep(2500);
quit_restart();
} }
exitUsbMsDone = true;
} }
else { else {
; ;
} }
}
} ); } );
usbms_t->start(); usbms_t->start();
} }
@ -165,3 +160,18 @@ void usbms_splash::quit_restart() {
process.startDetached("inkbox", QStringList()); process.startDetached("inkbox", QStringList());
qApp->quit(); qApp->quit();
} }
void usbms_splash::restartServices() {
// Restarting USBNet
// NOTE: USBNet is only started if required conditions are met (see https://github.com/Kobo-InkBox/rootfs/blob/master/etc/init.d/usbnet)
string_writeconfig("/opt/ibxd", "usbnet_start\n");
QThread::msleep(1000);
// Mounting onboard storage
string_writeconfig("/opt/ibxd", "onboard_mount\n");
QThread::msleep(1000);
// Checking for updates
string_writeconfig("/opt/ibxd", "update_inkbox_restart\n");
QThread::msleep(2500);
quit_restart();
}

View file

@ -14,12 +14,15 @@ class usbms_splash : public QWidget
public: public:
explicit usbms_splash(QWidget *parent = nullptr); explicit usbms_splash(QWidget *parent = nullptr);
~usbms_splash(); ~usbms_splash();
float sW;
float sH;
void usbms_launch(); void usbms_launch();
private slots: private slots:
void brightnessDown(); void brightnessDown();
void quit_restart(); void quit_restart();
void restartServices();
private: private:
Ui::usbms_splash *ui; Ui::usbms_splash *ui;