From 93dfeb49df371685a2e155a3c6d24fe8e9d110b6 Mon Sep 17 00:00:00 2001
From: Suvendhu Hansa <suvendhu.hansa@iopsys.eu>
Date: Thu, 9 Jan 2025 00:37:43 +0000
Subject: [PATCH] Added TemperatureStatus DM params

---
 .gitlab-ci.yml                        |   1 +
 gitlab-ci/setup.sh                    |   4 +
 src/Makefile                          |   5 +
 src/deviceinfo.c                      |   8 ++
 src/temperature.c                     | 137 ++++++++++++++++++++++++++
 src/temperature.h                     |  18 ++++
 test/files/etc/sysmngr/temperature.sh |  56 +++++++++++
 7 files changed, 229 insertions(+)
 create mode 100755 gitlab-ci/setup.sh
 create mode 100644 src/temperature.c
 create mode 100644 src/temperature.h
 create mode 100644 test/files/etc/sysmngr/temperature.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 85da5f8..292f2b3 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -10,6 +10,7 @@ variables:
   SYSMNGR_NETWORK_PROPERTIES: 'y'
   SYSMNGR_VENDOR_EXTENSIONS: 'y'
   SYSMNGR_FWBANK_UBUS_SUPPORT: 'y'
+  SYSMNGR_TEMPERATURE_STATUS: 'y'
 
 include:
   - project: 'iopsys/gitlab-ci-pipeline'
diff --git a/gitlab-ci/setup.sh b/gitlab-ci/setup.sh
new file mode 100755
index 0000000..aec66db
--- /dev/null
+++ b/gitlab-ci/setup.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+cp -rf ./test/files/etc/ /etc/
+
diff --git a/src/Makefile b/src/Makefile
index a8d795b..5875fba 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -54,6 +54,11 @@ ifeq ($(SYSMNGR_FWBANK_UBUS_SUPPORT),y)
 PROG_CFLAGS += -DSYSMNGR_FWBANK_UBUS_SUPPORT
 endif
 
+ifeq ($(SYSMNGR_TEMPERATURE_STATUS),y)
+OBJS += temperature.o
+PROG_CFLAGS += -DSYSMNGR_TEMPERATURE_STATUS
+endif
+
 .PHONY: all clean
 
 %.o: %.c
diff --git a/src/deviceinfo.c b/src/deviceinfo.c
index d1e18c9..1b56254 100644
--- a/src/deviceinfo.c
+++ b/src/deviceinfo.c
@@ -39,6 +39,10 @@
 #include "network.h"
 #endif
 
+#ifdef SYSMNGR_TEMPERATURE_STATUS
+#include "temperature.h"
+#endif
+
 /*************************************************************
 * GET & SET PARAM
 **************************************************************/
@@ -258,6 +262,10 @@ DMOBJ tDeviceInfoObj[] = {
 {"Reboots", &DMREAD, NULL, NULL, NULL, NULL, NULL, NULL, tDeviceInfoRebootsObj, tDeviceInfoRebootsParams, NULL, BBFDM_USP},
 #endif
 
+#ifdef SYSMNGR_TEMPERATURE_STATUS
+{"TemperatureStatus", &DMREAD, NULL, NULL, "file:/etc/sysmngr/temperature.sh", NULL, NULL, NULL, tDeviceInfoTemperatureStatusObj, tDeviceInfoTemperatureStatusParams, NULL, BBFDM_BOTH},
+#endif
+
 {0}
 };
 
diff --git a/src/temperature.c b/src/temperature.c
new file mode 100644
index 0000000..24cc977
--- /dev/null
+++ b/src/temperature.c
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2024 iopsys Software Solutions AB
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
+ *
+ *	  Author: Suvendhu Hansa <suvendhu.hansa@iopsys.eu>
+ *
+ */
+
+#include "libbbfdm-api/dmcommon.h"
+
+#define TEMPERATURE_SCRIPT "/etc/sysmngr/temperature.sh"
+#define TEMPERATURE_STATUS_CMD TEMPERATURE_SCRIPT " status"
+
+static void get_temperature_status(json_object **temp_status)
+{
+	char res[1024] = {0};
+
+	if (temp_status == NULL)
+		return;
+
+	*temp_status = NULL;
+	if (run_cmd(TEMPERATURE_STATUS_CMD, res, sizeof(res)) != 0)
+		return;
+
+	if (DM_STRLEN(res) != 0) {
+		remove_new_line(res);
+		*temp_status = json_tokener_parse(res);
+	}
+}
+
+static int browseTemperatureSensor(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance)
+{
+	json_object *temp_status = NULL;
+	struct dm_data data = {0};
+
+	get_temperature_status(&temp_status);
+	if (temp_status) { /* cppcheck-suppress knownConditionTrueFalse */
+		int id = 0, iter = 0;
+		json_object *arrobj = NULL, *tobj = NULL;
+
+		dmjson_foreach_obj_in_array(temp_status, arrobj, tobj, iter, 1, "status") {
+			char *inst;
+
+			data.json_object = tobj;
+
+			inst = handle_instance_without_section(dmctx, parent_node, ++id);
+			if (DM_LINK_INST_OBJ(dmctx, parent_node, (void *)&data, inst) == DM_STOP)
+				break;
+		}
+
+		json_object_put(temp_status);
+	}
+
+	return 0;
+}
+
+static int get_TemperatureStatus_numentries(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
+{
+	int cnt = get_number_of_entries(ctx, data, instance, browseTemperatureSensor);
+	dmasprintf(value, "%d", cnt);
+	return 0;
+}
+
+static int get_TemperatureSensor_alias(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
+{
+	struct uci_section *s = NULL;
+
+	uci_path_foreach_option_eq(bbfdm, "dmmap", "temperature", "temperature_instance", instance, s) {
+		dmuci_get_value_by_section_string(s, "alias", value);
+		break;
+	}
+	if ((*value)[0] == '\0')
+		dmasprintf(value, "cpe-%s", instance);
+	return 0;
+}
+
+static int set_TemperatureSensor_alias(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action)
+{
+	struct uci_section *s = NULL, *dmmap = NULL;
+	switch (action) {
+		case VALUECHECK:
+			if (bbfdm_validate_string(ctx, value, -1, 64, NULL, NULL))
+				return FAULT_9007;
+			break;
+		case VALUESET:
+			uci_path_foreach_option_eq(bbfdm, "dmmap", "temperature", "temperature_instance", instance, s) {
+				dmuci_set_value_by_section_bbfdm(s, "alias", value);
+				return 0;
+			}
+			dmuci_add_section_bbfdm("dmmap", "temperature", &dmmap);
+			dmuci_set_value_by_section(dmmap, "temperature_instance", instance);
+			dmuci_set_value_by_section(dmmap, "alias", value);
+			break;
+	}
+	return 0;
+}
+
+static int get_TemperatureSensor_name(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
+{
+	json_object *obj = ((struct dm_data *)data)->json_object;
+	*value = dmjson_get_value(obj, 1, "name");
+	return 0;
+}
+
+static int get_TemperatureSensor_value(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
+{
+	json_object *obj = ((struct dm_data *)data)->json_object;
+	*value = dmjson_get_value(obj, 1, "temperature");
+	return 0;
+}
+
+/******************************************************************************************************************************
+*                                            OBJ & PARAM DEFINITION
+*******************************************************************************************************************************/
+static DMLEAF tTemperatureStatusTemperatureSensorParams[] = {
+/* PARAM, permission, type, getvalue, setvalue, bbfdm_type*/
+{"Alias", &DMWRITE, DMT_STRING, get_TemperatureSensor_alias, set_TemperatureSensor_alias, BBFDM_BOTH, DM_FLAG_UNIQUE},
+{"Name", &DMREAD, DMT_STRING, get_TemperatureSensor_name, NULL, BBFDM_BOTH, DM_FLAG_UNIQUE},
+{"Value", &DMREAD, DMT_INT, get_TemperatureSensor_value, NULL, BBFDM_BOTH},
+{0}
+};
+
+/* *** Device.DeviceInfo.TemperatureTemperatureStatus; ubus call "bbf.temp status" *** */
+DMOBJ tDeviceInfoTemperatureStatusObj[] = {
+/* OBJ, permission, addobj, delobj, checkdep, browseinstobj, nextdynamicobj, dynamicleaf, nextobj, leaf, linker, bbfdm_type, uniqueKeys*/
+{"TemperatureSensor", &DMREAD, NULL, NULL, NULL, browseTemperatureSensor, NULL, NULL, NULL, tTemperatureStatusTemperatureSensorParams, NULL, BBFDM_BOTH},
+{0}
+};
+
+DMLEAF tDeviceInfoTemperatureStatusParams[] = {
+/* OBJ, permission, addobj, delobj, checkdep, browseinstobj, nextdynamicobj, dynamicleaf, nextobj, leaf, linker, bbfdm_type, uniqueKeys*/
+{"TemperatureSensorNumberOfEntries", &DMREAD, DMT_UNINT, get_TemperatureStatus_numentries, NULL, BBFDM_BOTH},
+{0}
+};
diff --git a/src/temperature.h b/src/temperature.h
new file mode 100644
index 0000000..8b94a1d
--- /dev/null
+++ b/src/temperature.h
@@ -0,0 +1,18 @@
+/*
+ * Copyright (C) 2024 iopsys Software Solutions AB
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
+ *
+ *	  Author: Suvendhu Hansa <suvendhu.hansa@iopsys.eu>
+ *
+ */
+
+#ifndef __TEMPERATURE_H
+#define __TEMPERATURE_H
+
+extern DMOBJ tDeviceInfoTemperatureStatusObj[];
+extern DMLEAF tDeviceInfoTemperatureStatusParams[];
+
+#endif //__TEMPERATURE_H
diff --git a/test/files/etc/sysmngr/temperature.sh b/test/files/etc/sysmngr/temperature.sh
new file mode 100644
index 0000000..0318f57
--- /dev/null
+++ b/test/files/etc/sysmngr/temperature.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+. /usr/share/libubox/jshn.sh
+. /lib/functions.sh
+
+function get_wlan_temperature()
+{
+  :
+}
+
+function get_sfp_temperature()
+{
+  json_add_object
+  json_add_string "name" "sfp"
+  json_add_int "temperature" "-274"
+  json_close_object
+}
+
+function get_cpu_temperature()
+{
+  json_add_object
+  json_add_string "name" "cpu"
+  json_add_int "temperature" "-274"
+  json_close_object
+}
+
+function get_temperature_status()
+{
+  local hasWifi="$(db -q get hw.board.hasWifi)"
+  local hasSfp="$(db -q get hw.board.hasSfp)"
+
+  json_init
+  json_add_array "status"
+    get_cpu_temperature
+    [ "$hasSfp" = "1" ] && get_sfp_temperature
+    [ "$hasWifi" = "1" ] && get_wlan_temperature
+  json_close_array
+
+  json_dump
+}
+
+function dump_invalid()
+{
+  json_init
+  json_add_string "fault" "invalid request"
+  json_dump
+}
+
+case "$1" in
+    status)
+        get_temperature_status
+        ;;
+    *)
+        dump_invalid
+        ;;
+esac
-- 
GitLab