Skip to content

Commit

Permalink
Merge pull request #53 from wimrijnders/fix-detect-platform
Browse files Browse the repository at this point in the history
Bugfix: Better detection of Pi platform
  • Loading branch information
mn416 authored Jul 16, 2018
2 parents 26eefe6 + 5cc6c45 commit 11f2163
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 9 deletions.
19 changes: 16 additions & 3 deletions Tests/testMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using RegMap = QPULib::RegisterMap;


//
// get the base directory right for calling AutoTest
// get the base directory right for calling compiled apps.
//
#ifdef DEBUG
#define POSTFIX_DEBUG "-debug"
Expand All @@ -35,19 +35,32 @@ using RegMap = QPULib::RegisterMap;
#define POSTFIX_QPU ""
#endif

#define BIN_PATH "obj" POSTFIX_DEBUG POSTFIX_QPU "/bin"

//
// This is a good place to put simple, global tests
//
const char *AUTOTEST_PATH = "obj" POSTFIX_DEBUG POSTFIX_QPU "/bin/AutoTest";
const char *AUTOTEST_PATH = BIN_PATH "/AutoTest";


TEST_CASE("Check random specifications for interpreter and emulator2", "[specs]") {
TEST_CASE("Check random specifications for interpreter and emulator2", "[specs][cmdline]") {
printf("Running AutoTest from '%s'\n", AUTOTEST_PATH);
REQUIRE(system(AUTOTEST_PATH) == 0);
}


TEST_CASE("Detect platform scripts should both return the same thing", "[cmdline]") {
int ret1 = system(BIN_PATH "/detectPlatform > /dev/null");
bool success1 = (ret1 == 0);

int ret2 = system("Tools/detectPlatform.sh > /dev/null");
bool success2 = (ret2 == 0);

INFO("C++ script returned " << ret1 << ", shell script returned " << ret2);
REQUIRE(success1 == success2);
}


#ifdef QPU_MODE

TEST_CASE("Test correct working of RegisterMap", "[regmap]") {
Expand Down
72 changes: 67 additions & 5 deletions Tools/detectPlatform.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <unistd.h> // geteuid()
#include <string.h> // strstr()
#include <string>
#include <fstream>
#include <streambuf>
Expand Down Expand Up @@ -35,19 +36,80 @@ bool loadFileInString(const char *filename, std::string & out_str) {


/**
* @brief Detect if this is running on a Rpi.
* @brief Detect Pi platform for newer Pi versions.
*
* @returns 0 if this is so, 1 if it's a different platform.
* On success, it displays a string with the model version.
*
* @return true if Pi detected, false otherwise
*/
int main(int argc, char *argv[]) {
bool detect_from_sys() {
const char *filename = "/sys/firmware/devicetree/base/model";

std::string content;
bool success = loadFileInString(filename, content);
if (success && !content.empty()) {
printf("Detected platform: %s\n", content.c_str());
} else {
printf("This is not a RPi platform\n");
return true;
}

return false;
}


/**
* @brief Detect Pi platform for older Pi versions.
*
* Detects if this is a VideoCore. This should be sufficient for detecting Pi,
* since it's the only thing to date(!) using this particular chip version.
*
* @return true if Pi detected, false otherwise
*
* --------------------------------------------------------------------------
* ## NOTES
*
* * The following are valid model numbers:
*
* - BCM2708
* - BCM2835 - This appears to be returned for all higher BCM versions
*
* * The following are also valid, but appear to be represented by 'BCM2835'
* in `/proc/cpuinfo`:
*
* - BCM2836 // If that's not the case, enable these as well
* - BCM2837
* - BCM2837B0
*/
bool detect_from_proc() {
const char *BCM_VERSION_PREFIX = "BCM2";
const char *filename = "/proc/cpuinfo";

std::ifstream t(filename);
if (!t.is_open()) return false;

std::string line;
while (getline(t, line)) {
if (!strstr(line.c_str(), "Hardware")) continue;

if (strstr(line.c_str(), BCM_VERSION_PREFIX)) {
// For now, don't try to exactly specify the model.
// This could be done with field "Revision' in current input.
printf("This is a Pi platform\n");
return true;
}
}

return false;
}


/**
* @brief Detect if this is running on a Rpi.
*
* @returns 0 if this is so, 1 if it's a different platform.
*/
int main(int argc, char *argv[]) {
if (!detect_from_sys() && !detect_from_proc()) {
printf("This is not a Pi platform\n");
return 1;
}

Expand Down
27 changes: 26 additions & 1 deletion Tools/detectPlatform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

file=/sys/firmware/devicetree/base/model

#
# This works for later version of Pi (distro's)
#
if [ -f $file ]
then
model=$(tr -d '\0' < $file) # as `cat`, but avoid warning 'ignored null byte in input'
Expand All @@ -26,5 +29,27 @@ then
fi
fi

echo This is not an RPi platform

#
# This should work for all Pi's.
#
# Detect if this is a VideoCore. This should be sufficient for detecting Pi,
# since it's the only thing to date(!) using this particular chip version.
#
# There are several model numbers possible, but they should all start with 'BCM2'.
#

# Prefix of allowed model numbers
modelPrefix=BCM2

model=$(cat /proc/cpuinfo | grep Hardware | grep $modelPrefix)
ret=$?
if [ $ret -eq 0 ]
then
echo This is a Pi platform
exit 0
fi


echo This is not a Pi platform
exit 1

0 comments on commit 11f2163

Please sign in to comment.