Skip to content

Commit

Permalink
Fix #145 Detect usb cable
Browse files Browse the repository at this point in the history
  • Loading branch information
Eeems committed Jul 4, 2023
1 parent bdee5dd commit 0d0aee6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
62 changes: 61 additions & 1 deletion shared/liboxide/power.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "power.h"
#include "debug.h"
#include "liboxide.h"

#include <QObject>
#include <QDir>
Expand All @@ -9,6 +10,7 @@ using Oxide::SysObject;

QList<SysObject>* _batteries = nullptr;
QList<SysObject>* _chargers = nullptr;
QList<SysObject>* _usbs = nullptr;

void _setup(){
if(_batteries != nullptr && _chargers != nullptr){
Expand All @@ -24,6 +26,11 @@ void _setup(){
}else{
_chargers = new QList<SysObject>();
}
if(_usbs != nullptr){
_usbs->clear();
}else{
_usbs = new QList<SysObject>();
}
QDir dir("/sys/class/power_supply");
O_DEBUG("Looking for batteries and chargers...");
for(auto& path : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable)){
Expand Down Expand Up @@ -53,6 +60,39 @@ void _setup(){
_chargers->append(battery);
}
}
if(deviceSettings.getDeviceType() == Oxide::DeviceSettings::RM1){
O_DEBUG("Looking for usbs...");
dir.setPath("/sys/bus/platform/devices");
for(QString& path : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable)){
if(!path.endsWith(".usbphy")){
continue;
}
O_DEBUG((" Checking " + path + "...").toStdString().c_str());
SysObject item(dir.path() + "/" + path);
if(!item.hasProperty("uevent")){
O_DEBUG(" Missing uevent property");
continue;
}
O_DEBUG(" Found USB!");
_usbs->append(item);
}
}else if(deviceSettings.getDeviceType() == Oxide::DeviceSettings::RM2){
O_DEBUG("Looking for usbs...");
dir.setPath("/sys/bus/platform/devices");
for(QString& path : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable)){
if(!path.startsWith("usbphy")){
continue;
}
O_DEBUG((" Checking " + path + "...").toStdString().c_str());
SysObject item(dir.path() + "/" + path);
if(!item.hasProperty("uevent")){
O_DEBUG(" Missing uevent property");
continue;
}
O_DEBUG(" Found USB!");
_usbs->append(item);
}
}
}
int _batteryInt(const QString& property){
int result = 0;
Expand All @@ -78,6 +118,14 @@ int _chargerInt(const QString& property){
}
return result;
}
bool _usbPropIs(const QString& property, const QString& value){
for(SysObject usb : *Oxide::Power::usbs()){
if(usb.uevent().value(property, "") == value){
return true;
}
}
return false;
}
static const QSet<QString> _batteryAlertState {
"Overheat",
"Dead",
Expand All @@ -104,6 +152,10 @@ namespace Oxide::Power {
_setup();
return _chargers;
}
const QList<SysObject>* usbs(){
_setup();
return _usbs;
}
int batteryLevel(){ return _batteryInt("capacity") / _batteries->length(); }
int batteryTemperature(){ return _batteryIntMax("temp") / 10; }
bool batteryCharging(){
Expand Down Expand Up @@ -156,5 +208,13 @@ namespace Oxide::Power {
}
bool batteryHasWarning(){ return batteryWarning().length(); }
bool batteryHasAlert(){ return batteryAlert().length(); }
bool chargerConnected(){ return batteryCharging() || _chargerInt("online"); }
bool chargerConnected(){
if(batteryCharging() || _chargerInt("online")){
return true;
}
if(deviceSettings.getDeviceType() == DeviceSettings::DeviceType::RM1){
return _usbPropIs("USB_CHARGER_STATE", "USB_CHARGER_PRESENT");
}
return false;
}
}
5 changes: 5 additions & 0 deletions shared/liboxide/power.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ namespace Oxide::Power {
* \return The list of charger objects
*/
LIBOXIDE_EXPORT const QList<Oxide::SysObject>* chargers();
/*!
* \brief Get a list of usb objects
* \return The list of usb objects
*/
LIBOXIDE_EXPORT const QList<Oxide::SysObject>* usbs();
/*!
* \brief Get the current battery level. This is averaged across the number of batteries connected to the device.
* \return The current battery level
Expand Down
29 changes: 28 additions & 1 deletion shared/liboxide/sysobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ namespace Oxide{
return dir.exists();
}
int SysObject::intProperty(const std::string& name){
if(!hasProperty(name)){
return 0;
}
try {
return std::stoi(strProperty(name));
}
Expand All @@ -37,7 +40,7 @@ namespace Oxide{
auto path = propertyPath(name);
QFile file(path.c_str());
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
O_DEBUG("Couldn't find the file " << path.c_str());
O_DEBUG("Couldn't find the file:" << path.c_str());
return "0";
}
QTextStream in(&file);
Expand All @@ -48,5 +51,29 @@ namespace Oxide{
}).base(), text.end());
return text;
}
QMap<QString, QString> SysObject::uevent(){
auto path = propertyPath("uevent");
QFile file(path.c_str());
QMap<QString, QString> props;
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
O_DEBUG("Couldn't find the file:" << path.c_str());
return props;
}
QTextStream in(&file);
do{
auto line = in.readLine();
// doesn't always properly provide EOF
if(line.trimmed().isEmpty()){
break;
}
auto parts = line.split("=");
if(parts.length() != 2){
O_WARNING("Invalid uevent line" << line);
continue;
}
props.insert(parts.first().trimmed(), parts.last().trimmed());
}while(!in.atEnd());
return props;
}
}

5 changes: 5 additions & 0 deletions shared/liboxide/sysobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ namespace Oxide {
* \return The path to the named property
*/
std::string propertyPath(const std::string& name);
/*!
* \brief Get the contents of uevent for this sysobject
* \return uevent properties
*/
QMap<QString, QString> uevent();

private:
std::string m_path;
Expand Down

0 comments on commit 0d0aee6

Please sign in to comment.