Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Nov 24, 2023
1 parent 839e1b6 commit f45d9bc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 50 deletions.
60 changes: 17 additions & 43 deletions cpp/hal/sbc_version.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//---------------------------------------------------------------------------
//
// SCSI Target Emulator PiSCSI
// for Raspberry Pi
// SCSI Target Emulator PiSCSI
// for Raspberry Pi
//
// Copyright (C) 2022 akuker
//
// [ Hardware version detection routines ]
// Copyright (C) 2022 akuker
// Copyright (c) 2023 Uwe Seimet
//
//---------------------------------------------------------------------------

Expand All @@ -15,13 +14,11 @@
#include <iostream>
#include <sstream>

SBC_Version::sbc_version_type SBC_Version::sbc_version = sbc_version_type::sbc_unknown;

// TODO: THESE NEED TO BE VALIDATED!!!!
const string SBC_Version::str_raspberry_pi_1 = "Raspberry Pi 1";
const string SBC_Version::str_raspberry_pi_2_3 = "Raspberry Pi 2/3";
const string SBC_Version::str_raspberry_pi_4 = "Raspberry Pi 4";
const string SBC_Version::str_unknown_sbc = "Unknown platform";
const string SBC_Version::str_raspberry_pi_1 = "Raspberry Pi 1";
const string SBC_Version::str_raspberry_pi_2_3 = "Raspberry Pi 2/3";
const string SBC_Version::str_raspberry_pi_4 = "Raspberry Pi 4";
const string SBC_Version::str_unknown_sbc = "Unknown platform";

// The strings in this table should align with the 'model' embedded
// in the device tree. This can be aquired by running:
Expand All @@ -38,17 +35,10 @@ const map<string, SBC_Version::sbc_version_type, less<>> SBC_Version::proc_devic
{"Raspberry Pi 3 Model ", sbc_version_type::sbc_raspberry_pi_2_3},
{"Raspberry Pi 4 Model ", sbc_version_type::sbc_raspberry_pi_4},
{"Raspberry Pi 400 ", sbc_version_type::sbc_raspberry_pi_4},
{"Raspberry Pi Zero W", sbc_version_type::sbc_raspberry_pi_1},
{"Raspberry Pi Zero", sbc_version_type::sbc_raspberry_pi_1}
};

const string SBC_Version::m_device_tree_model_path = "/proc/device-tree/model";

//---------------------------------------------------------------------------
//
// Convert the SBC Version to a printable string
//
//---------------------------------------------------------------------------
// Convert the SBC Version to a printable string
string SBC_Version::GetAsString()
{
switch (sbc_version) {
Expand All @@ -63,26 +53,17 @@ string SBC_Version::GetAsString()
}
}

//---------------------------------------------------------------------------
//
// Determine which version of single board computer (Pi) is being used
// based upon the device tree model string.
//
//---------------------------------------------------------------------------
// Determine which version of single board computer (Pi) is being used
// based upon the device tree model string.
void SBC_Version::Init()
{
ifstream input_stream(SBC_Version::m_device_tree_model_path);
ifstream input_stream(SBC_Version::DEVICE_TREE_MODEL_PATH);

if (input_stream.fail()) {
#if defined(__x86_64__) || defined(__X86__)
// We expect this to fail on x86
spdlog::debug("Detected " + GetAsString());
spdlog::trace("Failed to open " + DEVICE_TREE_MODEL_PATH + ": This may not be a Raspberry Pi");
sbc_version = sbc_version_type::sbc_unknown;
spdlog::info("Detected " + GetAsString());
return;
#else
spdlog::error("Failed to open " + SBC_Version::m_device_tree_model_path + ". Are you running as root?");
throw invalid_argument("Failed to open /proc/device-tree/model");
#endif
}

stringstream str_buffer;
Expand All @@ -98,23 +79,16 @@ void SBC_Version::Init()
}

sbc_version = sbc_version_type::sbc_raspberry_pi_4;
spdlog::error("Unable to determine single board computer type. Defaulting to Raspberry Pi 4");
spdlog::error("Unable to determine Raspberry Pi model. Defaulting to Raspberry Pi 4");
}

bool SBC_Version::IsRaspberryPi()
{
switch (sbc_version) {
case sbc_version_type::sbc_raspberry_pi_1:
case sbc_version_type::sbc_raspberry_pi_2_3:
case sbc_version_type::sbc_raspberry_pi_4:
return true;
default:
return false;
}
return sbc_version != sbc_version_type::sbc_unknown;
}

// The following functions are only used on the Raspberry Pi
// (imported from bcm_host.c)
// (imported from bcm_host.c)
uint32_t SBC_Version::GetDeviceTreeRanges(const char *filename, uint32_t offset)
{
uint32_t address = ~0;
Expand Down
17 changes: 10 additions & 7 deletions cpp/hal/sbc_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// for Raspberry Pi
//
// Copyright (C) 2022 akuker
// Copyright (C) 2023 Uwe Seimet
//
//---------------------------------------------------------------------------

Expand All @@ -22,17 +23,18 @@ using namespace std;
//===========================================================================
class SBC_Version
{
public:

public:

// Type of Single Board Computer
enum class sbc_version_type : uint8_t {
sbc_unknown = 0,
sbc_raspberry_pi_1,
sbc_raspberry_pi_2_3,
sbc_raspberry_pi_4
};

SBC_Version() = delete;
~SBC_Version() = delete;
SBC_Version() = default;
~SBC_Version() = default;

static void Init();

Expand All @@ -42,8 +44,9 @@ class SBC_Version

static uint32_t GetPeripheralAddress();

private:
static sbc_version_type sbc_version;
private:

inline static sbc_version_type sbc_version = sbc_version_type::sbc_unknown;;

static const string str_raspberry_pi_1;
static const string str_raspberry_pi_2_3;
Expand All @@ -52,7 +55,7 @@ class SBC_Version

static const map<std::string, sbc_version_type, less<>> proc_device_tree_mapping;

static const string m_device_tree_model_path;
inline static const string DEVICE_TREE_MODEL_PATH = "/proc/device-tree/model";

static uint32_t GetDeviceTreeRanges(const char *filename, uint32_t offset);
};

0 comments on commit f45d9bc

Please sign in to comment.