/*
 * selftest.c: SelfTestDiagnostics datamodel handler
 *
 * Copyright (C) 2023-2024 IOPSYS Software Solutions AB. All rights reserved.
 *
 *
 * See LICENSE file for license related information.
 */

#include <sys/stat.h>

#include "libbbfdm-api/dmcommon.h"

#define DIAG_BIN "/usr/sbin/self-diagnostics"

static char *get_selftest_log_instance(struct dmctx *ctx)
{
	char *file_name = NULL;
	char *path = NULL;
	char *file_uri = NULL;

	struct uci_section *s = get_origin_section_from_dmmap("dmmap_logmngr", "self_test", "self_test_log");
	if (s == NULL)
		goto err;

	dmuci_get_value_by_section_string(s, "log_file", &file_name);
	if (DM_STRLEN(file_name) == 0)
		goto err;

	dmasprintf(&file_uri, "file://%s", file_name);
	_bbfdm_get_references(ctx, "Device.DeviceInfo.VendorLogFile.", "Name", file_uri, &path);

err:
	return path ? path : dmstrdup("");
}

/*************************************************************
* OPERATE COMMAND
**************************************************************/
static operation_args device_self_test_args = {
	.out = (const char *[]) {
		"Status",
		"Results",
		NULL
	}
};

int get_operate_args_SelfTest(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
{
	*value = (char *)&device_self_test_args;
	return 0;
}

int operate_Device_SelfTest(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action)
{
	char cmd[512] = {0};
	char output[512] = {0};

	snprintf(cmd, sizeof(cmd), "sh %s", DIAG_BIN);

	if (run_cmd(cmd, output, sizeof(output)) != 0)
		goto err;

	// truncate the new line char from end
	remove_new_line(output);

	if (!file_exists(output))
		goto err;

	/* Add in dmmap_logmngr */
	struct uci_section *s = get_origin_section_from_dmmap("dmmap_logmngr", "self_test", "self_test_log");
	if (s == NULL) {
		dmuci_add_section_bbfdm("dmmap_logmngr", "self_test", &s);
		dmuci_rename_section_by_section(s, "self_test_log");
	}

	dmuci_set_value_by_section(s, "log_file", output);
	dmuci_commit_package_bbfdm("dmmap_logmngr");

	/* Get self test log instance */
	char *result = get_selftest_log_instance(ctx);

	fill_blob_param(&ctx->bb, "Status", "Complete", DMT_TYPE[DMT_STRING], 0);
	fill_blob_param(&ctx->bb, "Results", result, DMT_TYPE[DMT_STRING], 0);

	if (ctx->dm_type != BBFDM_USP) {
		diagnostics_set_option("selftest", "DiagnosticState", "Complete");
		dmuci_commit_package_bbfdm(DMMAP_DIAGNOSTIGS);
	}

	return 0;

err:
	fill_blob_param(&ctx->bb, "Status", "Error_Internal", DMT_TYPE[DMT_STRING], 0);
	if (ctx->dm_type != BBFDM_USP) {
		diagnostics_set_option("selftest", "DiagnosticState", "Error");
		dmuci_commit_package_bbfdm(DMMAP_DIAGNOSTIGS);
	}

	return USP_FAULT_COMMAND_FAILURE;
}

/*************************************************************
* GET & SET PARAM
**************************************************************/
static int get_SelfTest_DiagnosticsState(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
{
	*value = diagnostics_get_option_fallback_def("selftest", "DiagnosticState", "None");
	return 0;
}

static int set_SelfTest_DiagnosticsState(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action)
{
	switch (action) {
		case VALUECHECK:
			if (bbfdm_validate_string(ctx, value, -1, -1, DiagnosticsState, NULL))
				return FAULT_9007;
			break;
		case VALUESET:
			if (DM_LSTRCMP(value, "Requested") == 0)
				diagnostics_set_option("selftest", "DiagnosticState", value);
	}
	return 0;
}

static int get_SelfTest_Results(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
{
	*value = get_selftest_log_instance(ctx);
	return 0;
}

/**********************************************************************************************************************************
*                                            OBJ & LEAF DEFINITION
***********************************************************************************************************************************/
DMLEAF tSelfTestParams[] = {
/* PARAM, permission, type, getvalue, setvalue, bbfdm_type*/
{"DiagnosticsState", &DMWRITE, DMT_STRING, get_SelfTest_DiagnosticsState, set_SelfTest_DiagnosticsState, BBFDM_CWMP},
{"Results", &DMREAD, DMT_STRING, get_SelfTest_Results, NULL, BBFDM_CWMP, DM_FLAG_REFERENCE},
{0}
};

DMOBJ tDeviceObjs[] = {
{"SelfTestDiagnostics", &DMREAD, NULL, NULL, NULL, NULL, NULL, NULL, NULL, tSelfTestParams, NULL, BBFDM_CWMP, NULL},
{0}
};

DMLEAF tDeviceParams[] = {
/* PARAM, permission, type, getvalue, setvalue, bbfdm_type, version*/
{"SelfTestDiagnostics()", &DMASYNC, DMT_COMMAND, get_operate_args_SelfTest, operate_Device_SelfTest, BBFDM_USP},
{0}
};

DM_MAP_OBJ tDynamicObj[] = {
/* parentobj, nextobject, parameter */
{"Device.", tDeviceObjs, tDeviceParams},
{0}
};
