Reputation: 115
How can I retrieve following information programmatically in c++:
This is a terminal command in Mac OSX:
ioreg -c IOPlatformExpertDevice | awk '/board-id/ {print $4}' | awk -F '\"' '{print $2}'
I use IOKit library to retrieve information such as IOPlatformSerialNumber and PlatformUUID information. But I couldn't find any corresponding key for "board-id".
Upvotes: 2
Views: 2172
Reputation: 23438
If you have the io_service_t
handle to the IOPlatformExpertDevice
in your C++ code, you can use the IORegistryEntryCreateCFProperty()
function to get the "board-id" property. Expect to receive a CFData
object, but check for null and the correct type id to be sure. Then, use the usual CFData
methods to extract the data in the form you want.
If you haven't got the IOService handle yet, you should be able to get there using either IOServiceGetMatchingService()
(I'm hoping it's safe to assume there will only be one IOPlatformExpertDevice
instance.), or using IORegistryGetRootEntry()
to get the root, and walking the IORegistry graph to the platform expert device with IORegistryEntryGetChildEntry()
or similar.
As the board-id
property doesn't have a named symbolic constant, you'll just have to hardcode it:
CFTypeRef board_id_property = IORegistryEntryCreateCFProperty(
platform_expert_device, CFSTR("board-id"), kCFAllocatorDefault, 0);
Note that property values can take different types, including CFNumber
, CFBoolean
, CFString
, CFData
, CFArray
, and CFDictionary
, and you need to be prepared to handle the case where the type doesn't match the one you expect, or when NULL is returned (if the property does not exist). Check the type using CFGetTypeID()
, e.g.:
if (board_id_property != NULL && CFGetTypeID(board_id_property) == CFDataGetTypeID())
{
CFDataRef board_id_data = (CFDataRef)board_id_property;
// safe to use CFData* functions now
...
CFRelease(board_id_property);
}
else
{
// Unexpected, do error handling.
...
if (board_id_property != NULL)
CFRelease(board_id_property);
}
Upvotes: 2