2026-01-06 09:48:38 +08:00
|
|
|
#include "common/printing.h"
|
|
|
|
|
#include "common/jsonconfig.h"
|
2026-05-28 15:58:08 +08:00
|
|
|
#include "common/strutil.h"
|
2023-06-09 10:08:04 +08:00
|
|
|
#include "detection/chassis/chassis.h"
|
|
|
|
|
#include "modules/chassis/chassis.h"
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
bool ffPrintChassis(FFChassisOptions* options) {
|
2025-08-18 13:38:30 +08:00
|
|
|
bool success = false;
|
|
|
|
|
|
2023-06-09 10:08:04 +08:00
|
|
|
FFChassisResult result;
|
2023-06-22 19:05:13 +08:00
|
|
|
ffStrbufInit(&result.type);
|
|
|
|
|
ffStrbufInit(&result.vendor);
|
|
|
|
|
ffStrbufInit(&result.version);
|
2024-01-06 18:35:10 +08:00
|
|
|
ffStrbufInit(&result.serial);
|
2023-06-09 10:08:04 +08:00
|
|
|
|
2024-01-06 10:43:51 +08:00
|
|
|
const char* error = ffDetectChassis(&result);
|
2023-06-16 16:33:43 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (error) {
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
2023-06-09 10:08:04 +08:00
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (result.type.length == 0) {
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "chassis_type is not set by O.E.M.");
|
2023-06-16 16:33:43 +08:00
|
|
|
goto exit;
|
2023-06-09 10:08:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (options->moduleArgs.outputFormat.length == 0) {
|
2023-08-15 21:40:22 +08:00
|
|
|
ffPrintLogoAndKey(FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
2023-06-22 19:05:13 +08:00
|
|
|
ffStrbufWriteTo(&result.type, stdout);
|
2026-03-29 09:28:49 +08:00
|
|
|
if (result.version.length) {
|
2023-06-22 19:05:13 +08:00
|
|
|
printf(" (%s)", result.version.chars);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2023-06-19 21:19:09 +08:00
|
|
|
putchar('\n');
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2024-12-11 10:47:02 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
|
2026-03-29 09:28:49 +08:00
|
|
|
FF_ARG(result.type, "type"),
|
|
|
|
|
FF_ARG(result.vendor, "vendor"),
|
|
|
|
|
FF_ARG(result.version, "version"),
|
|
|
|
|
FF_ARG(result.serial, "serial"),
|
|
|
|
|
}));
|
2023-06-09 10:08:04 +08:00
|
|
|
}
|
2025-08-18 13:38:30 +08:00
|
|
|
success = true;
|
2023-06-09 10:08:04 +08:00
|
|
|
|
|
|
|
|
exit:
|
2023-06-22 19:05:13 +08:00
|
|
|
ffStrbufDestroy(&result.type);
|
|
|
|
|
ffStrbufDestroy(&result.vendor);
|
|
|
|
|
ffStrbufDestroy(&result.version);
|
2024-01-06 18:35:10 +08:00
|
|
|
ffStrbufDestroy(&result.serial);
|
2025-08-18 13:38:30 +08:00
|
|
|
return success;
|
2023-06-09 10:08:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffParseChassisJsonObject(FFChassisOptions* options, yyjson_val* module) {
|
2025-08-02 18:51:03 +08:00
|
|
|
yyjson_val *key, *val;
|
2023-08-17 16:09:41 +08:00
|
|
|
size_t idx, max;
|
2026-03-29 09:28:49 +08:00
|
|
|
yyjson_obj_foreach (module, idx, max, key, val) {
|
|
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
|
2023-08-17 16:09:41 +08:00
|
|
|
continue;
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2023-06-09 10:08:04 +08:00
|
|
|
|
2025-08-02 18:51:03 +08:00
|
|
|
ffPrintError(FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
|
2023-06-09 10:08:04 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-30 14:13:26 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffGenerateChassisJsonConfig(FFChassisOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
2025-08-04 16:46:28 +08:00
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
|
2023-10-24 14:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-10 21:13:03 +08:00
|
|
|
bool ffGenerateChassisJsonResult([[maybe_unused]] FFChassisOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
2025-08-18 13:38:30 +08:00
|
|
|
bool success = false;
|
2023-08-30 14:13:26 +08:00
|
|
|
FFChassisResult result;
|
|
|
|
|
ffStrbufInit(&result.type);
|
|
|
|
|
ffStrbufInit(&result.vendor);
|
|
|
|
|
ffStrbufInit(&result.version);
|
2024-01-06 18:35:10 +08:00
|
|
|
ffStrbufInit(&result.serial);
|
2023-08-30 14:13:26 +08:00
|
|
|
|
2024-01-06 10:43:51 +08:00
|
|
|
const char* error = ffDetectChassis(&result);
|
2023-08-30 14:13:26 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (error) {
|
2023-08-30 14:13:26 +08:00
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", error);
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (result.type.length == 0) {
|
2023-08-30 14:13:26 +08:00
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", "chassis_type is not set by O.E.M.");
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 13:54:46 +08:00
|
|
|
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
|
2023-08-30 14:13:26 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "type", &result.type);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &result.vendor);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version);
|
2024-01-06 18:35:10 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "serial", &result.serial);
|
2025-08-18 13:38:30 +08:00
|
|
|
success = true;
|
2023-08-30 14:13:26 +08:00
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
ffStrbufDestroy(&result.type);
|
|
|
|
|
ffStrbufDestroy(&result.vendor);
|
|
|
|
|
ffStrbufDestroy(&result.version);
|
2024-01-06 18:35:10 +08:00
|
|
|
ffStrbufDestroy(&result.serial);
|
2025-08-18 13:38:30 +08:00
|
|
|
return success;
|
2023-08-30 14:13:26 +08:00
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffInitChassisOptions(FFChassisOptions* options) {
|
2025-08-04 14:25:14 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffDestroyChassisOptions(FFChassisOptions* options) {
|
2025-08-04 14:25:14 +08:00
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 22:50:56 +08:00
|
|
|
FFModuleBaseInfo ffChassisModuleInfo = {
|
2024-12-11 10:47:02 +08:00
|
|
|
.name = FF_CHASSIS_MODULE_NAME,
|
2026-04-16 18:27:11 +08:00
|
|
|
.description = "Print chassis type information (desktop, laptop, etc.)",
|
2025-08-03 18:39:41 +08:00
|
|
|
.initOptions = (void*) ffInitChassisOptions,
|
|
|
|
|
.destroyOptions = (void*) ffDestroyChassisOptions,
|
2024-12-11 10:47:02 +08:00
|
|
|
.parseJsonObject = (void*) ffParseChassisJsonObject,
|
|
|
|
|
.printModule = (void*) ffPrintChassis,
|
|
|
|
|
.generateJsonResult = (void*) ffGenerateChassisJsonResult,
|
|
|
|
|
.generateJsonConfig = (void*) ffGenerateChassisJsonConfig,
|
|
|
|
|
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
|
2026-03-30 10:27:23 +08:00
|
|
|
{ "Chassis type", "type" },
|
|
|
|
|
{ "Chassis vendor", "vendor" },
|
|
|
|
|
{ "Chassis version", "version" },
|
|
|
|
|
{ "Chassis serial number", "serial" },
|
2024-12-11 10:47:02 +08:00
|
|
|
})),
|
2026-07-31 10:56:13 +08:00
|
|
|
.defaultOrder = 8,
|
2024-12-11 10:47:02 +08:00
|
|
|
};
|