From 28e412b79d80bacf2e7e9e76ba9ef7ae00d1d764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 30 Aug 2023 14:46:04 +0800 Subject: [PATCH] CPU: add JSON printing support --- src/detection/cpu/cpu_apple.c | 3 +++ src/modules/cpu/cpu.c | 36 ++++++++++++++++++++++++++++++++++- src/modules/cpu/cpu.h | 1 + 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/detection/cpu/cpu_apple.c b/src/detection/cpu/cpu_apple.c index 9dcb07edd..da55404ee 100644 --- a/src/detection/cpu/cpu_apple.c +++ b/src/detection/cpu/cpu_apple.c @@ -35,7 +35,10 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) { if (ffSysctlGetString("machdep.cpu.brand_string", &cpu->name) != NULL) return "sysctlbyname(machdep.cpu.brand_string) failed"; + ffSysctlGetString("machdep.cpu.vendor", &cpu->vendor); + if (cpu->vendor.length == 0 && ffStrbufStartsWithS(&cpu->name, "Apple ")) + ffStrbufAppendS(&cpu->vendor, "Apple"); cpu->coresPhysical = (uint16_t) ffSysctlGetInt("hw.physicalcpu_max", 1); if(cpu->coresPhysical == 1) diff --git a/src/modules/cpu/cpu.c b/src/modules/cpu/cpu.c index d89a9a17e..dc46d835b 100644 --- a/src/modules/cpu/cpu.c +++ b/src/modules/cpu/cpu.c @@ -79,7 +79,7 @@ void ffPrintCPU(FFCPUOptions* options) void ffInitCPUOptions(FFCPUOptions* options) { - ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_CPU_MODULE_NAME, ffParseCPUCommandOptions, ffParseCPUJsonObject, ffPrintCPU, NULL); + ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_CPU_MODULE_NAME, ffParseCPUCommandOptions, ffParseCPUJsonObject, ffPrintCPU, ffGenerateCPUJson); ffOptionInitModuleArg(&options->moduleArgs); options->temp = false; } @@ -127,3 +127,37 @@ void ffParseCPUJsonObject(FFCPUOptions* options, yyjson_val* module) ffPrintError(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key); } } + +void ffGenerateCPUJson(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +{ + FFCPUResult cpu; + cpu.temperature = FF_CPU_TEMP_UNSET; + cpu.coresPhysical = cpu.coresLogical = cpu.coresOnline = 0; + cpu.frequencyMax = cpu.frequencyMin = 0; + ffStrbufInit(&cpu.name); + ffStrbufInit(&cpu.vendor); + + const char* error = ffDetectCPU(options, &cpu); + + if(error) + { + yyjson_mut_obj_add_str(doc, module, "error", error); + } + else if(cpu.vendor.length == 0 && cpu.name.length == 0 && cpu.coresOnline <= 1) + { + yyjson_mut_obj_add_str(doc, module, "error", "No CPU detected"); + } + else + { + yyjson_mut_val* obj = yyjson_mut_obj(doc); + yyjson_mut_obj_add_val(doc, module, "result", obj); + yyjson_mut_obj_add_strbuf(doc, obj, "cpu", &cpu.name); + yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &cpu.vendor); + yyjson_mut_obj_add_uint(doc, obj, "coresPhysical", cpu.coresPhysical); + yyjson_mut_obj_add_uint(doc, obj, "coresLogical", cpu.coresLogical); + yyjson_mut_obj_add_uint(doc, obj, "coresOnline", cpu.coresOnline); + yyjson_mut_obj_add_real(doc, obj, "frequencyMin", cpu.frequencyMin); + yyjson_mut_obj_add_real(doc, obj, "frequencyMax", cpu.frequencyMax); + yyjson_mut_obj_add_real(doc, obj, "temperature", cpu.temperature); + } +} diff --git a/src/modules/cpu/cpu.h b/src/modules/cpu/cpu.h index acff0a13c..0a3e7c4c6 100644 --- a/src/modules/cpu/cpu.h +++ b/src/modules/cpu/cpu.h @@ -9,3 +9,4 @@ void ffInitCPUOptions(FFCPUOptions* options); bool ffParseCPUCommandOptions(FFCPUOptions* options, const char* key, const char* value); void ffDestroyCPUOptions(FFCPUOptions* options); void ffParseCPUJsonObject(FFCPUOptions* options, yyjson_val* module); +void ffGenerateCPUJson(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module);