-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add disk feature detection for MacOS (#91)
- Loading branch information
Showing
9 changed files
with
141 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
/**/build/** | ||
/.vs | ||
/CMakeSettings.json | ||
/.cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ | |
#include <cpuid.h> | ||
#endif | ||
|
||
#include <cstdint> | ||
|
||
#define MAX_INTEL_TOP_LVL 4 | ||
|
||
#define SSE_POS 0x02000000 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,132 @@ | ||
// Copyright Leon Freist | ||
// Author Leon Freist <[email protected]> | ||
|
||
#include "hwinfo/platform.h" | ||
#include <hwinfo/platform.h> | ||
|
||
#include <cstdint> | ||
|
||
#ifdef HWINFO_APPLE | ||
|
||
#include <fstream> | ||
#include <CoreFoundation/CoreFoundation.h> | ||
#include <IOKit/IOBSD.h> | ||
#include <IOKit/IOKitLib.h> | ||
#include <IOKit/storage/IOMedia.h> | ||
#include <hwinfo/disk.h> | ||
|
||
#include "hwinfo/disk.h" | ||
#include "hwinfo/utils/stringutils.h" | ||
#include <string> | ||
|
||
namespace hwinfo { | ||
|
||
// ===================================================================================================================== | ||
// _____________________________________________________________________________________________________________________ | ||
/** | ||
Converts a CFStringRef to a std::string | ||
*/ | ||
std::string cf_to_std(CFStringRef cfString) { | ||
if (cfString == nullptr) { | ||
return "<unknown>"; | ||
} | ||
|
||
CFIndex length = CFStringGetLength(cfString); | ||
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1; | ||
|
||
// Initialize std::string with maxSize and fill with null characters | ||
auto out = std::string(maxSize, '\0'); | ||
|
||
// Fill std::string with the actual string | ||
auto success = CFStringGetCString(cfString, const_cast<char*>(out.data()), maxSize, kCFStringEncodingUTF8); | ||
|
||
if (!success) { | ||
return "<unknown>"; | ||
} | ||
|
||
// Resize the string to the actual length | ||
out.resize(strlen(out.c_str())); | ||
return out; | ||
} | ||
|
||
/** | ||
Converts a CFNumberRef to a number of type ReturnType | ||
*/ | ||
template <typename NumberType> | ||
NumberType cf_to_std(CFNumberRef raw, CFNumberType cfNumberEnum) { | ||
if (raw == nullptr) { | ||
return NumberType(); | ||
} | ||
|
||
NumberType out; | ||
CFNumberGetValue(raw, cfNumberEnum, &out); | ||
|
||
return out; | ||
} | ||
|
||
int64_t cf_to_std(CFNumberRef raw) { return cf_to_std<int64_t>(raw, kCFNumberSInt64Type); } | ||
|
||
template <typename ReturnType, typename CFType> | ||
ReturnType getIORegistryProperty(io_object_t service, CFStringRef key) { | ||
// Get the property from I/O Registry | ||
auto raw = static_cast<CFType>(IORegistryEntryCreateCFProperty(service, key, kCFAllocatorDefault, 0)); | ||
|
||
// Convert the property to a output type | ||
ReturnType out = cf_to_std(raw); | ||
|
||
// Release the property | ||
if (raw) { | ||
CFRelease(raw); | ||
}; | ||
|
||
return out; | ||
} | ||
|
||
// Retrieves disk information using I/O Kit | ||
std::vector<Disk> getAllDisks() { | ||
// TODO: implement | ||
std::vector<Disk> disks; | ||
auto disks = std::vector<Disk>(); | ||
|
||
CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOMediaClass); | ||
CFDictionaryAddValue(matchingDict, CFSTR(kIOMediaWholeKey), kCFBooleanTrue); | ||
|
||
io_iterator_t iter; | ||
if (IOServiceGetMatchingServices(0, matchingDict, &iter) == KERN_SUCCESS) { | ||
int i_disk = 0; | ||
while (true) { | ||
auto service = IOIteratorNext(iter); | ||
if (service == 0) { | ||
break; | ||
} | ||
|
||
auto disk = Disk(); | ||
|
||
disk._id = i_disk; | ||
|
||
// get the name of the IO service | ||
std::string model; | ||
model.resize(128); | ||
if (IORegistryEntryGetName(service, const_cast<char*>(model.data())) != KERN_SUCCESS) { | ||
model = "<unknown>"; | ||
} | ||
model.resize(strlen(model.c_str())); | ||
disk._model = model; | ||
|
||
// guess the vendor from the model | ||
if (model.find("APPLE") != std::string::npos || model.find("Apple") != std::string::npos) { | ||
disk._vendor = "Apple"; | ||
} else { | ||
disk._vendor = "<unknown>"; | ||
} | ||
|
||
disk._serialNumber = getIORegistryProperty<std::string, CFStringRef>(service, CFSTR(kIOMediaUUIDKey)); | ||
|
||
disk._size_Bytes = getIORegistryProperty<int64_t, CFNumberRef>(service, CFSTR(kIOMediaSizeKey)); | ||
|
||
disks.push_back(std::move(disk)); | ||
|
||
IOObjectRelease(service); | ||
|
||
i_disk++; | ||
} | ||
IOObjectRelease(iter); | ||
} | ||
return disks; | ||
} | ||
|
||
} // namespace hwinfo | ||
|
||
#endif // HWINFO_APPLE | ||
#endif // HWINFO_APPLE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters