Files
fastfetch/src/modules/tpm/tpm.c
T

109 lines
3.2 KiB
C
Raw Normal View History

2026-01-06 09:48:38 +08:00
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/stringUtils.h"
2024-09-25 22:55:57 +08:00
#include "detection/tpm/tpm.h"
#include "modules/tpm/tpm.h"
bool ffPrintTPM(FFTPMOptions* options)
2024-09-25 22:55:57 +08:00
{
FFTPMResult result = {
.version = ffStrbufCreate(),
2024-09-26 09:53:00 +08:00
.description = ffStrbufCreate()
2024-09-25 22:55:57 +08:00
};
const char* error = ffDetectTPM(&result);
if(error)
{
ffPrintError(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
return false;
2024-09-25 22:55:57 +08:00
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2024-09-26 09:53:00 +08:00
if (result.description.length > 0)
ffStrbufPutTo(&result.description, stdout);
else
ffStrbufPutTo(&result.version, stdout);
2024-09-25 22:55:57 +08:00
}
else
{
2024-12-11 10:47:02 +08:00
FF_PRINT_FORMAT_CHECKED(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){
FF_ARG(result.version, "version"),
FF_ARG(result.description, "description"),
2024-09-25 22:55:57 +08:00
}));
}
ffStrbufDestroy(&result.version);
2024-09-26 09:53:00 +08:00
ffStrbufDestroy(&result.description);
return true;
2024-09-25 22:55:57 +08:00
}
void ffParseTPMJsonObject(FFTPMOptions* options, yyjson_val* module)
{
yyjson_val *key, *val;
2024-09-25 22:55:57 +08:00
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key, val)
2024-09-25 22:55:57 +08:00
{
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
ffPrintError(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2024-09-25 22:55:57 +08:00
}
}
void ffGenerateTPMJsonConfig(FFTPMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2024-09-25 22:55:57 +08:00
}
bool ffGenerateTPMJsonResult(FF_MAYBE_UNUSED FFTPMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
2024-09-25 22:55:57 +08:00
{
FFTPMResult result = {
.version = ffStrbufCreate(),
2024-09-26 09:53:00 +08:00
.description = ffStrbufCreate()
2024-09-25 22:55:57 +08:00
};
const char* error = ffDetectTPM(&result);
if(error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
return false;
2024-09-25 22:55:57 +08:00
}
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version);
2024-09-26 09:53:00 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "description", &result.description);
2024-09-25 22:55:57 +08:00
ffStrbufDestroy(&result.version);
2024-09-26 09:53:00 +08:00
ffStrbufDestroy(&result.description);
return true;
2024-09-25 22:55:57 +08:00
}
void ffInitTPMOptions(FFTPMOptions* options)
{
ffOptionInitModuleArg(&options->moduleArgs, "");
}
void ffDestroyTPMOptions(FFTPMOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffTPMModuleInfo = {
2024-12-11 10:47:02 +08:00
.name = FF_TPM_MODULE_NAME,
.description = "Print info of Trusted Platform Module (TPM) Security Device",
.initOptions = (void*) ffInitTPMOptions,
.destroyOptions = (void*) ffDestroyTPMOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseTPMJsonObject,
.printModule = (void*) ffPrintTPM,
.generateJsonResult = (void*) ffGenerateTPMJsonResult,
.generateJsonConfig = (void*) ffGenerateTPMJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
{"TPM device version", "version"},
{"TPM general description", "description"},
}))
};