Skip to content

Commit

Permalink
[libssu] Implement Mer HA-based board mapping (hwrelease.device)
Browse files Browse the repository at this point in the history
Adaptations based on mer-hybris should store the device name of the
Droid device in /etc/hw-release under the MER_HA_DEVICE key.
  • Loading branch information
Thomas Perl committed Apr 17, 2014
1 parent e63fb84 commit 5d6ace3
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
7 changes: 6 additions & 1 deletion board-mappings.ini
Expand Up @@ -17,13 +17,14 @@
# - boardname.contains -- searches for substring in boardname
# NOTE: only use boardname if none of the other options match
# use the boardname command to set/check the current value
# - hwrelease.device -- Compares MER_HA_DEVICE in /etc/hw-release
# - cpuinfo.contains -- searches /proc/cpuinfo for a string
# - uname-release.contains -- searches the kernels release string for
# a string (uname -r)
# - arch.equals -- compares with zyppers arch (like i586)
#
# Resolve order is:
# file.exists -> cpuinfo.contains
# file.exists -> cpuinfo.contains -> hwrelease.device
# -> uname-release.contains -> arch.equals
#
# The found model (after resolving variants) will be used as category. The
Expand Down Expand Up @@ -85,6 +86,10 @@ N900=Nokia RX-51 board
N950=Nokia RM-680 board
N9=Nokia RM-696 board

[hwrelease.device]
mako=mako
grouper=grouper

[arch.equals]
generic-x86=i586

Expand Down
88 changes: 88 additions & 0 deletions libssu/ssudeviceinfo.cpp
Expand Up @@ -221,6 +221,23 @@ QString SsuDeviceInfo::deviceModel(){
}
if (!cachedModel.isEmpty()) return cachedModel;

// mer-hybris adaptations: /etc/hw-release MER_HA_DEVICE variable
QString hwReleaseDevice = hwRelease()["MER_HA_DEVICE"];
if (!hwReleaseDevice.isEmpty()) {
boardMappings->beginGroup("hwrelease.device");
keys = boardMappings->allKeys();

foreach (const QString &key, keys) {
QString value = boardMappings->value(key).toString();
if (hwReleaseDevice == value) {
cachedModel = key;
break;
}
}
boardMappings->endGroup();
}
if (!cachedModel.isEmpty()) return cachedModel;

// check if the device can be identified by the kernel version string
struct utsname buf;
if (!uname(&buf)){
Expand Down Expand Up @@ -428,3 +445,74 @@ QVariant SsuDeviceInfo::value(const QString &key, const QVariant &value){

return value;
}

QMap<QString, QString> SsuDeviceInfo::hwRelease()
{
QMap<QString, QString> result;

// Specification of the format, encoding is similar to /etc/os-release
// http://www.freedesktop.org/software/systemd/man/os-release.html

QFile hwRelease("/etc/hw-release");
if (hwRelease.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&hwRelease);

// "All strings should be in UTF-8 format, and non-printable characters
// should not be used."
in.setCodec("UTF-8");

while (!in.atEnd()) {
QString line = in.readLine();

// "Lines beginning with "#" shall be ignored as comments."
if (line.startsWith('#')) {
continue;
}

QString key = line.section('=', 0, 0);
QString value = line.section('=', 1);

// Remove trailing whitespace in value
value = value.trimmed();

// POSIX.1-2001 says uppercase, digits and underscores.
//
// Bash uses "[a-zA-Z_]+[a-zA-Z0-9_]*", so we'll use that too,
// as we can safely assume that "shell-compatible variable
// assignments" means it should be compatible with bash.
//
// see http://stackoverflow.com/a/2821183
// and http://stackoverflow.com/a/2821201
if (!QRegExp("[a-zA-Z_]+[a-zA-Z0-9_]*").exactMatch(key)) {
qWarning("Invalid key in input line: '%s'", qPrintable(line));
continue;
}

// "Variable assignment values should be enclosed in double or
// single quotes if they include spaces, semicolons or other
// special characters outside of A-Z, a-z, 0-9."
if (((value.at(0) == '\'') || (value.at(0) == '"'))) {
if (value.at(0) != value.at(value.size() - 1)) {
qWarning("Quoting error in input line: '%s'", qPrintable(line));
continue;
}

// Remove the quotes
value = value.mid(1, value.size() - 2);
}

// "If double or single quotes or backslashes are to be used within
// variable assignments, they should be escaped with backslashes,
// following shell style."
value = value.replace("\\\"", "\"");
value = value.replace("\\'", "'");
value = value.replace("\\\\", "\\");

result[key] = value;
}

hwRelease.close();
}

return result;
}
5 changes: 5 additions & 0 deletions libssu/ssudeviceinfo.h
Expand Up @@ -112,5 +112,10 @@ class SsuDeviceInfo: public QObject {
QString cachedFamily, cachedModel, cachedVariant;

void clearCache();

/**
* Return a map of key, value pairs parsed from /etc/hw-release
*/
QMap<QString, QString> hwRelease();
};
#endif

0 comments on commit 5d6ace3

Please sign in to comment.