From c8695831de58aaed764d887662115194badfe584 Mon Sep 17 00:00:00 2001 From: Wim Rijnders Date: Fri, 13 Jul 2018 09:53:06 +0200 Subject: [PATCH 1/7] Detect Pi for older versions --- Tools/detectPlatform.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Tools/detectPlatform.sh b/Tools/detectPlatform.sh index 1764e8a..aa9266c 100755 --- a/Tools/detectPlatform.sh +++ b/Tools/detectPlatform.sh @@ -9,6 +9,9 @@ file=/sys/firmware/devicetree/base/model +# This works for later version of Pi (distro's) +if false; then + if [ -f $file ] then model=$(tr -d '\0' < $file) # as `cat`, but avoid warning 'ignored null byte in input' @@ -26,5 +29,18 @@ then fi fi +fi + +# This should work for all Pi's +# The hardware from Pi 2 onward is actually BCM2836, but the cat-call still returns BCM2835 +model=$(cat /proc/cpuinfo | grep Hardware | grep BCM2835 ) +ret=$? +if [ $ret -eq 0 ] +then + echo model checks out + exit 0 +fi + + echo This is not an RPi platform exit 1 From d19a5d05ed91931124944646ab8420dd28303efa Mon Sep 17 00:00:00 2001 From: Wim Rijnders Date: Fri, 13 Jul 2018 10:20:41 +0200 Subject: [PATCH 2/7] Adjusted C++ version of detectPlatform for more general Pi detection --- Tools/detectPlatform.cpp | 62 ++++++++++++++++++++++++++++++++++++---- Tools/detectPlatform.sh | 19 +++++++----- 2 files changed, 69 insertions(+), 12 deletions(-) diff --git a/Tools/detectPlatform.cpp b/Tools/detectPlatform.cpp index 28cd8e6..eaa2b29 100644 --- a/Tools/detectPlatform.cpp +++ b/Tools/detectPlatform.cpp @@ -1,4 +1,5 @@ #include // geteuid() +#include // strstr() #include #include #include @@ -35,19 +36,70 @@ bool loadFileInString(const char *filename, std::string & out_str) { /** - * @brief Detect if this is running on a Rpi. + * @brief Pi platform detection 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 Pi platform detection for newer 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 + */ +bool detect_from_proc() { + // The hardware from Pi 2 onward is actually BCM2836, but the call still returns BCM2835 + const char *BCM_VERSION = "BCM2835"; + + 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)) { + // 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; } diff --git a/Tools/detectPlatform.sh b/Tools/detectPlatform.sh index aa9266c..72e757a 100755 --- a/Tools/detectPlatform.sh +++ b/Tools/detectPlatform.sh @@ -9,9 +9,9 @@ file=/sys/firmware/devicetree/base/model +# # This works for later version of Pi (distro's) -if false; then - +# if [ -f $file ] then model=$(tr -d '\0' < $file) # as `cat`, but avoid warning 'ignored null byte in input' @@ -29,18 +29,23 @@ then fi fi -fi -# This should work for all Pi's -# The hardware from Pi 2 onward is actually BCM2836, but the cat-call still returns BCM2835 +# +# 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. +# +# The hardware from Pi 2 onward is actually BCM2836, but the call still returns BCM2835 +# model=$(cat /proc/cpuinfo | grep Hardware | grep BCM2835 ) ret=$? if [ $ret -eq 0 ] then - echo model checks out + echo This is a Pi platform exit 0 fi -echo This is not an RPi platform +echo This is not a Pi platform exit 1 From 35c25a0fd5ed6d14b6223408e6f9d3ce6b302a5c Mon Sep 17 00:00:00 2001 From: Wim Rijnders Date: Fri, 13 Jul 2018 10:36:33 +0200 Subject: [PATCH 3/7] Added unit test for detect platform scripts --- Tests/testMain.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Tests/testMain.cpp b/Tests/testMain.cpp index f304f10..d10186c 100644 --- a/Tests/testMain.cpp +++ b/Tests/testMain.cpp @@ -35,21 +35,35 @@ using RegMap = QPULib::RegisterMap; #define POSTFIX_QPU "" #endif -const char *AUTOTEST_PATH = "obj" POSTFIX_DEBUG POSTFIX_QPU "/bin/AutoTest"; +#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]") { + printf("Running detectPlatform\n"); + int ret1 = system(BIN_PATH "/detectPlatform"); + bool success1 = (ret1 == 0); + + int ret2 = system("Tools/detectPlatform.sh"); + 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]") { From 5f474bda0a2e7478681ec54dbb2e7f22878376a4 Mon Sep 17 00:00:00 2001 From: wimrijnders Date: Fri, 13 Jul 2018 10:48:54 +0200 Subject: [PATCH 4/7] Small edits in unit tests --- Tests/testMain.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Tests/testMain.cpp b/Tests/testMain.cpp index d10186c..646dc80 100644 --- a/Tests/testMain.cpp +++ b/Tests/testMain.cpp @@ -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" @@ -52,11 +52,10 @@ TEST_CASE("Check random specifications for interpreter and emulator2", "[specs][ TEST_CASE("Detect platform scripts should both return the same thing", "[cmdline]") { - printf("Running detectPlatform\n"); - int ret1 = system(BIN_PATH "/detectPlatform"); + int ret1 = system(BIN_PATH "/detectPlatform > /dev/null"); bool success1 = (ret1 == 0); - int ret2 = system("Tools/detectPlatform.sh"); + int ret2 = system("Tools/detectPlatform.sh > /dev/null"); bool success2 = (ret2 == 0); INFO("C++ script returned " << ret1 << ", shell script returned " << ret2); From 151af3c82b48e4d5af2eab67546830758b4dfa17 Mon Sep 17 00:00:00 2001 From: wimrijnders Date: Mon, 16 Jul 2018 08:44:31 +0200 Subject: [PATCH 5/7] Added checking of multiple model numbers in scripts --- Tools/detectPlatform.cpp | 31 ++++++++++++++++++++----------- Tools/detectPlatform.sh | 23 ++++++++++++++++++++--- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/Tools/detectPlatform.cpp b/Tools/detectPlatform.cpp index eaa2b29..24a3497 100644 --- a/Tools/detectPlatform.cpp +++ b/Tools/detectPlatform.cpp @@ -36,7 +36,7 @@ bool loadFileInString(const char *filename, std::string & out_str) { /** - * @brief Pi platform detection for newer Pi versions. + * @brief Detect Pi platform for newer Pi versions. * * On success, it displays a string with the model version. * @@ -57,7 +57,7 @@ bool detect_from_sys() { /** - * @brief Pi platform detection for newer Pi versions. + * @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. @@ -65,8 +65,15 @@ bool detect_from_sys() { * @return true if Pi detected, false otherwise */ bool detect_from_proc() { - // The hardware from Pi 2 onward is actually BCM2836, but the call still returns BCM2835 - const char *BCM_VERSION = "BCM2835"; + // List of allowed model numbers + const char *BCM_VERSION[] = { + "BCM2807", + "BCM2835", // This appears to be returned for all higher BCM versions + //"BCM2836", // If that's not the case, enable these as well + //"BCM2837", + //BCM2837B0", + nullptr // end marker + }; const char *filename = "/proc/cpuinfo"; @@ -79,15 +86,16 @@ bool detect_from_proc() { while (getline(t, line)) { if (!strstr(line.c_str(), "Hardware")) continue; - if (strstr(line.c_str(), BCM_VERSION)) { - // 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; + for (int i = 0; BCM_VERSION[i] != nullptr; ++i) { + if (strstr(line.c_str(), BCM_VERSION[i])) { + // 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; } @@ -98,7 +106,8 @@ bool detect_from_proc() { * @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()) { + //if (!detect_from_sys() && !detect_from_proc()) { + if (!detect_from_proc()) { printf("This is not a Pi platform\n"); return 1; } diff --git a/Tools/detectPlatform.sh b/Tools/detectPlatform.sh index 72e757a..d700060 100755 --- a/Tools/detectPlatform.sh +++ b/Tools/detectPlatform.sh @@ -38,12 +38,29 @@ fi # # The hardware from Pi 2 onward is actually BCM2836, but the call still returns BCM2835 # -model=$(cat /proc/cpuinfo | grep Hardware | grep BCM2835 ) + +# List of allowed model numbers +knownModels=( + "BCM2807" + "BCM2835" # This appears to be returned for all higher BCM versions + #"BCM2836" # If that's not the case, enable these as well + #"BCM2837" + #"BCM2837B0" +) + + +model=$(cat /proc/cpuinfo | grep Hardware) ret=$? if [ $ret -eq 0 ] then - echo This is a Pi platform - exit 0 + for knownModel in "${knownModels[@]}" + do + if echo $model | grep $knownModel + then + echo This is a Pi platform + exit 0 + fi + done fi From 302e80c3d4691721413331288498dbb2441f8baa Mon Sep 17 00:00:00 2001 From: wimrijnders Date: Mon, 16 Jul 2018 10:29:39 +0200 Subject: [PATCH 6/7] Check field 'Hardware' on prefix only --- Tools/detectPlatform.cpp | 45 ++++++++++++++++++++-------------------- Tools/detectPlatform.sh | 26 +++++++---------------- 2 files changed, 30 insertions(+), 41 deletions(-) diff --git a/Tools/detectPlatform.cpp b/Tools/detectPlatform.cpp index 24a3497..0b69e02 100644 --- a/Tools/detectPlatform.cpp +++ b/Tools/detectPlatform.cpp @@ -63,36 +63,38 @@ bool detect_from_sys() { * 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: + * + * - BCM2807 + * - 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() { - // List of allowed model numbers - const char *BCM_VERSION[] = { - "BCM2807", - "BCM2835", // This appears to be returned for all higher BCM versions - //"BCM2836", // If that's not the case, enable these as well - //"BCM2837", - //BCM2837B0", - nullptr // end marker - }; - + const char *BCM_VERSION_PREFIX = "BCM28"; const char *filename = "/proc/cpuinfo"; std::ifstream t(filename); - if (!t.is_open()) { - return false; - } + if (!t.is_open()) return false; std::string line; while (getline(t, line)) { if (!strstr(line.c_str(), "Hardware")) continue; - for (int i = 0; BCM_VERSION[i] != nullptr; ++i) { - if (strstr(line.c_str(), BCM_VERSION[i])) { - // 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; - } + 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; } } @@ -106,8 +108,7 @@ bool detect_from_proc() { * @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()) { - if (!detect_from_proc()) { + if (!detect_from_sys() && !detect_from_proc()) { printf("This is not a Pi platform\n"); return 1; } diff --git a/Tools/detectPlatform.sh b/Tools/detectPlatform.sh index d700060..765431b 100755 --- a/Tools/detectPlatform.sh +++ b/Tools/detectPlatform.sh @@ -36,31 +36,19 @@ fi # 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. # -# The hardware from Pi 2 onward is actually BCM2836, but the call still returns BCM2835 +# There are several model numbers possible, but they should all start +# with 'BCM28'. # -# List of allowed model numbers -knownModels=( - "BCM2807" - "BCM2835" # This appears to be returned for all higher BCM versions - #"BCM2836" # If that's not the case, enable these as well - #"BCM2837" - #"BCM2837B0" -) +# Prefix of allowed model numbers +modelPrefix=BCM28 - -model=$(cat /proc/cpuinfo | grep Hardware) +model=$(cat /proc/cpuinfo | grep Hardware | grep $modelPrefix) ret=$? if [ $ret -eq 0 ] then - for knownModel in "${knownModels[@]}" - do - if echo $model | grep $knownModel - then - echo This is a Pi platform - exit 0 - fi - done + echo This is a Pi platform + exit 0 fi From 5cc6c4565730598faab100fece2fd9cc9baf692d Mon Sep 17 00:00:00 2001 From: Wim Rijnders Date: Mon, 16 Jul 2018 10:53:18 +0200 Subject: [PATCH 7/7] Made prefix --- Tools/detectPlatform.cpp | 4 ++-- Tools/detectPlatform.sh | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Tools/detectPlatform.cpp b/Tools/detectPlatform.cpp index 0b69e02..25afbea 100644 --- a/Tools/detectPlatform.cpp +++ b/Tools/detectPlatform.cpp @@ -69,7 +69,7 @@ bool detect_from_sys() { * * * The following are valid model numbers: * - * - BCM2807 + * - BCM2708 * - BCM2835 - This appears to be returned for all higher BCM versions * * * The following are also valid, but appear to be represented by 'BCM2835' @@ -80,7 +80,7 @@ bool detect_from_sys() { * - BCM2837B0 */ bool detect_from_proc() { - const char *BCM_VERSION_PREFIX = "BCM28"; + const char *BCM_VERSION_PREFIX = "BCM2"; const char *filename = "/proc/cpuinfo"; std::ifstream t(filename); diff --git a/Tools/detectPlatform.sh b/Tools/detectPlatform.sh index 765431b..0e11c0b 100755 --- a/Tools/detectPlatform.sh +++ b/Tools/detectPlatform.sh @@ -36,12 +36,11 @@ fi # 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 'BCM28'. +# There are several model numbers possible, but they should all start with 'BCM2'. # # Prefix of allowed model numbers -modelPrefix=BCM28 +modelPrefix=BCM2 model=$(cat /proc/cpuinfo | grep Hardware | grep $modelPrefix) ret=$?