diff --git a/Tests/testMain.cpp b/Tests/testMain.cpp index b6f1bbc..6eb9c08 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" @@ -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]") { diff --git a/Tools/detectPlatform.cpp b/Tools/detectPlatform.cpp index 28cd8e6..25afbea 100644 --- a/Tools/detectPlatform.cpp +++ b/Tools/detectPlatform.cpp @@ -1,4 +1,5 @@ #include // geteuid() +#include // strstr() #include #include #include @@ -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; } diff --git a/Tools/detectPlatform.sh b/Tools/detectPlatform.sh index 1764e8a..0e11c0b 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 [ -f $file ] then model=$(tr -d '\0' < $file) # as `cat`, but avoid warning 'ignored null byte in input' @@ -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