From a35d1a32ea46664cc5938354d4bbfbc7b0b4226d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 22 Jun 2023 19:05:13 +0800 Subject: [PATCH] Chassis: code refactor --- src/detection/chassis/chassis.c | 4 ++-- src/detection/chassis/chassis.h | 8 ++++---- src/detection/chassis/chassis_bsd.c | 12 ++++++------ src/detection/chassis/chassis_linux.c | 14 ++++++------- src/detection/chassis/chassis_windows.c | 10 +++++----- src/modules/chassis/chassis.c | 26 ++++++++++++------------- 6 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/detection/chassis/chassis.c b/src/detection/chassis/chassis.c index 732df2074..f2ebc2da1 100644 --- a/src/detection/chassis/chassis.c +++ b/src/detection/chassis/chassis.c @@ -1,10 +1,10 @@ #include "chassis.h" -const char* ffChassisTypeToString(uint32_t chassisType) +const char* ffChassisTypeToString(uint32_t type) { // https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.2.0.pdf // 7.4.1 System Enclosure or Chassis Types - switch (chassisType) + switch (type) { case 0x01: return "Other"; case 0x02: return "Unknown"; diff --git a/src/detection/chassis/chassis.h b/src/detection/chassis/chassis.h index b7551d38b..876936c8d 100644 --- a/src/detection/chassis/chassis.h +++ b/src/detection/chassis/chassis.h @@ -7,12 +7,12 @@ typedef struct FFChassisResult { - FFstrbuf chassisType; - FFstrbuf chassisVendor; - FFstrbuf chassisVersion; + FFstrbuf type; + FFstrbuf vendor; + FFstrbuf version; } FFChassisResult; const char* ffDetectChassis(FFChassisResult* result); -const char* ffChassisTypeToString(uint32_t chassisType); +const char* ffChassisTypeToString(uint32_t type); #endif diff --git a/src/detection/chassis/chassis_bsd.c b/src/detection/chassis/chassis_bsd.c index 91ca9f7e0..c38b562c2 100644 --- a/src/detection/chassis/chassis_bsd.c +++ b/src/detection/chassis/chassis_bsd.c @@ -5,11 +5,11 @@ const char* ffDetectChassis(FFChassisResult* result) { // Unlike other platforms, `smbios.chassis.type` return display string directly on my machine - ffSettingsGetFreeBSDKenv("smbios.chassis.type", &result->chassisType); - ffCleanUpSmbiosValue(&result->chassisType); - ffSettingsGetFreeBSDKenv("smbios.chassis.maker", &result->chassisVendor); - ffCleanUpSmbiosValue(&result->chassisVendor); - ffSettingsGetFreeBSDKenv("smbios.chassis.version", &result->chassisVersion); - ffCleanUpSmbiosValue(&result->chassisVersion); + ffSettingsGetFreeBSDKenv("smbios.chassis.type", &result->type); + ffCleanUpSmbiosValue(&result->type); + ffSettingsGetFreeBSDKenv("smbios.chassis.maker", &result->vendor); + ffCleanUpSmbiosValue(&result->vendor); + ffSettingsGetFreeBSDKenv("smbios.chassis.version", &result->version); + ffCleanUpSmbiosValue(&result->version); return NULL; } diff --git a/src/detection/chassis/chassis_linux.c b/src/detection/chassis/chassis_linux.c index 82aff1189..deace35d9 100644 --- a/src/detection/chassis/chassis_linux.c +++ b/src/detection/chassis/chassis_linux.c @@ -19,15 +19,15 @@ static void getSmbiosValue(const char* devicesPath, const char* classPath, FFstr const char* ffDetectChassis(FFChassisResult* result) { - getSmbiosValue("/sys/devices/virtual/dmi/id/chassis_type", "/sys/class/dmi/id/chassis_type", &result->chassisType); - getSmbiosValue("/sys/devices/virtual/dmi/id/chassis_vendor", "/sys/class/dmi/id/chassis_vendor", &result->chassisVendor); - getSmbiosValue("/sys/devices/virtual/dmi/id/chassis_version", "/sys/class/dmi/id/chassis_version", &result->chassisVersion); + getSmbiosValue("/sys/devices/virtual/dmi/id/chassis_type", "/sys/class/dmi/id/chassis_type", &result->type); + getSmbiosValue("/sys/devices/virtual/dmi/id/chassis_vendor", "/sys/class/dmi/id/chassis_vendor", &result->vendor); + getSmbiosValue("/sys/devices/virtual/dmi/id/chassis_version", "/sys/class/dmi/id/chassis_version", &result->version); - if(result->chassisType.length) + if(result->type.length) { - const char* chassisTypeStr = ffChassisTypeToString(ffStrbufToUInt16(&result->chassisType, 9999)); - if(chassisTypeStr) - ffStrbufSetS(&result->chassisType, chassisTypeStr); + const char* typeStr = ffChassisTypeToString(ffStrbufToUInt16(&result->type, 9999)); + if(typeStr) + ffStrbufSetS(&result->type, typeStr); } return NULL; } diff --git a/src/detection/chassis/chassis_windows.c b/src/detection/chassis/chassis_windows.c index f80d0ff28..52d1337c1 100644 --- a/src/detection/chassis/chassis_windows.c +++ b/src/detection/chassis/chassis_windows.c @@ -7,14 +7,14 @@ const char* ffDetectChassis(FFChassisResult* result) if(!ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\BIOS", &hKey, NULL)) // SMBIOS return "ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L\"HARDWARE\\DESCRIPTION\\System\\BIOS\", &hKey, NULL) failed"; - uint32_t chassisType = 0; - if(!ffRegReadUint(hKey, L"EnclosureType", &chassisType, NULL)) + uint32_t type = 0; + if(!ffRegReadUint(hKey, L"EnclosureType", &type, NULL)) return "\"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\BIOS\\EnclosureType\" doesn't exist"; - const char* chassisTypeStr = ffChassisTypeToString(chassisType); - if(!chassisTypeStr) + const char* typeStr = ffChassisTypeToString(type); + if(!typeStr) return "Unknown chassis type"; - ffStrbufAppendS(&result->chassisType, chassisTypeStr); + ffStrbufAppendS(&result->type, typeStr); return NULL; } diff --git a/src/modules/chassis/chassis.c b/src/modules/chassis/chassis.c index 66ae7f2d7..15d6ba959 100644 --- a/src/modules/chassis/chassis.c +++ b/src/modules/chassis/chassis.c @@ -9,9 +9,9 @@ void ffPrintChassis(FFinstance* instance, FFChassisOptions* options) { FFChassisResult result; - ffStrbufInit(&result.chassisType); - ffStrbufInit(&result.chassisVendor); - ffStrbufInit(&result.chassisVersion); + ffStrbufInit(&result.type); + ffStrbufInit(&result.vendor); + ffStrbufInit(&result.version); const char* error = ffDetectChassis(&result); @@ -21,7 +21,7 @@ void ffPrintChassis(FFinstance* instance, FFChassisOptions* options) goto exit; } - if(result.chassisType.length == 0) + if(result.type.length == 0) { ffPrintError(instance, FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, "chassis_type is not set by O.E.M."); goto exit; @@ -30,24 +30,24 @@ void ffPrintChassis(FFinstance* instance, FFChassisOptions* options) if(options->moduleArgs.outputFormat.length == 0) { ffPrintLogoAndKey(instance, FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs.key, &options->moduleArgs.keyColor); - ffStrbufWriteTo(&result.chassisType, stdout); - if (result.chassisVersion.length) - printf(" (%s)", result.chassisVersion.chars); + ffStrbufWriteTo(&result.type, stdout); + if (result.version.length) + printf(" (%s)", result.version.chars); putchar('\n'); } else { ffPrintFormat(instance, FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, FF_CHASSIS_NUM_FORMAT_ARGS, (FFformatarg[]) { - {FF_FORMAT_ARG_TYPE_STRBUF, &result.chassisType}, - {FF_FORMAT_ARG_TYPE_STRBUF, &result.chassisVendor}, - {FF_FORMAT_ARG_TYPE_STRBUF, &result.chassisVersion}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result.type}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result.vendor}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result.version}, }); } exit: - ffStrbufDestroy(&result.chassisType); - ffStrbufDestroy(&result.chassisVendor); - ffStrbufDestroy(&result.chassisVersion); + ffStrbufDestroy(&result.type); + ffStrbufDestroy(&result.vendor); + ffStrbufDestroy(&result.version); } void ffInitChassisOptions(FFChassisOptions* options)