From 2dd55d9c9eb01ed1b11e16bad9e2c3e8badfffeb Mon Sep 17 00:00:00 2001
From: Vivek Kumar Dutta <vivek.dutta@iopsys.eu>
Date: Mon, 17 Apr 2023 19:14:18 +0530
Subject: [PATCH] Align with bbfdm

---
 .gitignore                        |  3 ++
 .gitlab-ci.yml                    | 16 ++++++++
 docs/.pages                       |  2 +-
 docs/api/.pages                   |  2 +-
 docs/api/ubus/.pages              |  1 +
 docs/api/uci/.pages               |  2 +-
 docs/arch/.pages                  |  1 +
 docs/guide/.pages                 |  2 +-
 gitlab-ci/compilation-test.sh     | 23 +++++++++++
 gitlab-ci/install-dependencies.sh |  9 ++++
 gitlab-ci/iopsys-supervisord.conf | 24 +++++++++++
 gitlab-ci/setup.sh                |  7 ++++
 gitlab-ci/shared.sh               | 68 +++++++++++++++++++++++++++++++
 src/utils.c                       | 24 +++++++----
 test/files/etc/config/bulkdata    | 17 ++++++++
 15 files changed, 188 insertions(+), 13 deletions(-)
 create mode 100755 gitlab-ci/compilation-test.sh
 create mode 100755 gitlab-ci/install-dependencies.sh
 create mode 100644 gitlab-ci/iopsys-supervisord.conf
 create mode 100755 gitlab-ci/setup.sh
 create mode 100644 gitlab-ci/shared.sh
 create mode 100644 test/files/etc/config/bulkdata

diff --git a/.gitignore b/.gitignore
index 5c15fbc..b7939bd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,7 @@
 bulkdatad
 *.o
 *.so
+*.gcda
+*.gcno
+*.log
 docs/index.md
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index cd26647..556b6c2 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -14,4 +14,20 @@ variables:
 
 stages:
     - static_code_analysis
+    - compilation_test
     - deploy
+
+run_compilation_test:
+    stage: compilation_test
+    image: ${COMMON_IMAGE}
+    allow_failure: false
+    script:
+    - "./gitlab-ci/install-dependencies.sh"
+    - "./gitlab-ci/setup.sh"
+    - "./gitlab-ci/compilation-test.sh"
+
+    artifacts:
+        when: always
+        paths:
+            - timestamp.log
+            - /tmp/memory-report.xml
diff --git a/docs/.pages b/docs/.pages
index daec10e..c31edb4 100644
--- a/docs/.pages
+++ b/docs/.pages
@@ -1,4 +1,4 @@
-title: "Bulkdata documentation"
+title: "Bulkdata"
 
 nav:
     - "arch"
diff --git a/docs/api/.pages b/docs/api/.pages
index 9290341..2d67086 100644
--- a/docs/api/.pages
+++ b/docs/api/.pages
@@ -1,4 +1,4 @@
-title: "Bulkdata API"
+title: "Bulkdata"
 
 nav:
     - "ubus"
diff --git a/docs/api/ubus/.pages b/docs/api/ubus/.pages
index e2d5ae9..1acac62 100644
--- a/docs/api/ubus/.pages
+++ b/docs/api/ubus/.pages
@@ -1 +1,2 @@
+title: "Bulkdata"
 hide: true
diff --git a/docs/api/uci/.pages b/docs/api/uci/.pages
index beaa06d..f2e8601 100644
--- a/docs/api/uci/.pages
+++ b/docs/api/uci/.pages
@@ -1,4 +1,4 @@
-title: "Bulkdata UCI"
+title: "Bulkdata"
 
 nav:
     - "..."
diff --git a/docs/arch/.pages b/docs/arch/.pages
index e2d5ae9..1acac62 100644
--- a/docs/arch/.pages
+++ b/docs/arch/.pages
@@ -1 +1,2 @@
+title: "Bulkdata"
 hide: true
diff --git a/docs/guide/.pages b/docs/guide/.pages
index 27aaee4..f2e8601 100644
--- a/docs/guide/.pages
+++ b/docs/guide/.pages
@@ -1,4 +1,4 @@
-title: "Bulkdata Developer Guide"
+title: "Bulkdata"
 
 nav:
     - "..."
diff --git a/gitlab-ci/compilation-test.sh b/gitlab-ci/compilation-test.sh
new file mode 100755
index 0000000..af3f1d3
--- /dev/null
+++ b/gitlab-ci/compilation-test.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+echo "$0 preparation script"
+pwd
+
+source ./gitlab-ci/shared.sh
+
+# build package
+date +%s > timestamp.log
+build_bulkdata
+
+supervisorctl update
+supervisorctl restart all
+supervisorctl status all
+exec_cmd ubus -t 20 wait_for bbfdm
+
+supervisorctl stop all
+supervisorctl status
+
+grep -q "Leak_" /tmp/memory-report.xml
+error_on_zero $?
+
+echo "Compilation Test :: PASS"
diff --git a/gitlab-ci/install-dependencies.sh b/gitlab-ci/install-dependencies.sh
new file mode 100755
index 0000000..8de80eb
--- /dev/null
+++ b/gitlab-ci/install-dependencies.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+echo "install dependencies for unit-test script"
+pwd
+
+source ./gitlab-ci/shared.sh
+
+# install bbfdm
+install_bbfdm
diff --git a/gitlab-ci/iopsys-supervisord.conf b/gitlab-ci/iopsys-supervisord.conf
new file mode 100644
index 0000000..5ffa481
--- /dev/null
+++ b/gitlab-ci/iopsys-supervisord.conf
@@ -0,0 +1,24 @@
+[program:ubusd]
+autorestart=false
+startretries=0
+priority=1
+command=/bin/bash -c "/usr/sbin/ubusd"
+
+[program:rpcd]
+autorestart=false
+startretries=0
+priority=2
+command=/bin/bash -c "/usr/sbin/rpcd"
+
+[program:bbfdmd]
+autorestart=false
+startretries=0
+priority=3
+command=/bin/bash -c "/usr/sbin/bbfdmd"
+
+[program:bulkdatad]
+autorestart=false
+startretries=0
+priority=4
+command=/bin/bash -c "/usr/bin/valgrind --xml=yes --xml-file=/tmp/memory-report.xml --leak-check=full --show-reachable=yes --show-leak-kinds=all --errors-for-leak-kinds=all --error-exitcode=1 --track-origins=yes /usr/sbin/bulkdatad"
+
diff --git a/gitlab-ci/setup.sh b/gitlab-ci/setup.sh
new file mode 100755
index 0000000..5c59186
--- /dev/null
+++ b/gitlab-ci/setup.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+echo "preparation script"
+
+mkdir -p /var/state/
+cp ./gitlab-ci/iopsys-supervisord.conf /etc/supervisor/conf.d/
+cp -rf ./test/files/* /
diff --git a/gitlab-ci/shared.sh b/gitlab-ci/shared.sh
new file mode 100644
index 0000000..fc573ee
--- /dev/null
+++ b/gitlab-ci/shared.sh
@@ -0,0 +1,68 @@
+#!/bin/bash
+
+function cleanup()
+{
+	echo ""
+}
+
+function check_ret()
+{
+	ret=$1
+	if [ "$ret" -ne 0 ]; then
+		echo "Validation of last command failed, ret(${ret})"
+		exit $ret
+	fi
+
+}
+
+function error_on_zero()
+{
+	ret=$1
+	if [ "$ret" -eq 0 ]; then
+		echo "Validation of last command failed, ret(${ret})"
+		exit $ret
+	fi
+
+}
+
+function exec_cmd()
+{
+	echo "executing $@"
+	$@ >/dev/null 2>&1
+
+	if [ $? -ne 0 ]; then
+		echo "Failed to execute $@"
+		exit 1
+	fi
+}
+
+function clean_stunc()
+{
+	make -C src clean
+	rm *.gcno
+	rm *.gcda
+}
+
+function build_bulkdata()
+{
+	export CFLAGS="-g -O0 -fprofile-arcs -ftest-coverage -D_GNU_SOURCE"
+	export LDFLAGS="--coverage"
+	make clean
+	make
+	exec_cmd ln -s ${PWD}/bulkdatad /usr/sbin/bulkdatad
+}
+
+function install_bbfdm()
+{
+	[ -d "/opt/dev/bbfdm" ] && rm -rf /opt/dev/bbfdm
+
+	if [ -n "${BBFDM_BRANCH}" ]; then
+		exec_cmd git clone -b ${BBFDM_BRANCH} https://dev.iopsys.eu/bbf/bbfdm.git /opt/dev/bbfdm
+	else
+		exec_cmd git clone https://dev.iopsys.eu/bbf/bbfdm.git /opt/dev/bbfdm
+	fi
+
+	cd /opt/dev/bbfdm
+	./gitlab-ci/install-dependencies.sh install
+	./gitlab-ci/setup.sh install
+}
diff --git a/src/utils.c b/src/utils.c
index 2462cd6..34ce612 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -17,7 +17,7 @@
 #include <libubox/blobmsg_json.h>
 #include "utils.h"
 
-#define USP_OBJECT_NAME "usp.raw"
+#define DM_OBJECT_NAME "bbfdm"
 
 static unsigned char LogLevel = DEFAULT_LOG_LEVEL;
 static bool g_usp_object_available = false;
@@ -199,8 +199,8 @@ static void get_value_single_cb(struct ubus_request *req, int type __attribute__
 		__P_MAX
 	};
 	const struct blobmsg_policy p[__P_MAX] = {
-		{ "parameter", BLOBMSG_TYPE_STRING },
-		{ "value", BLOBMSG_TYPE_STRING }
+		{ "path", BLOBMSG_TYPE_STRING },
+		{ "data", BLOBMSG_TYPE_STRING }
 	};
 
 	if (!msg)
@@ -245,8 +245,8 @@ static void get_value_group_cb(struct ubus_request *req, int type __attribute__(
 		__P_MAX
 	};
 	const struct blobmsg_policy p[__P_MAX] = {
-		{ "parameter", BLOBMSG_TYPE_STRING },
-		{ "value", BLOBMSG_TYPE_STRING },
+		{ "path", BLOBMSG_TYPE_STRING },
+		{ "data", BLOBMSG_TYPE_STRING },
 		{ "type", BLOBMSG_TYPE_STRING }
 	};
 
@@ -285,7 +285,7 @@ static int uspd_call(struct ubus_context *ctx, char *method,
 
 	uint32_t id;
 
-	if (ubus_lookup_id(ctx, USP_OBJECT_NAME, &id))
+	if (ubus_lookup_id(ctx, DM_OBJECT_NAME, &id))
 		return -1;
 
 	// Invoke Ubus to get data from uspd
@@ -309,7 +309,10 @@ int get_value_single(vendor_data_t *arg)
 	blob_buf_init(&bb, 0);
 
 	blobmsg_add_string(&bb, "path", arg->path);
+	void *table = blobmsg_open_table(&bb, "optional");
 	blobmsg_add_string(&bb, "proto", "cwmp");
+	blobmsg_add_string(&bb, "format", "raw");
+	blobmsg_close_table(&bb, table);
 
 	// Invoke Ubus to get data from uspd
 	res = uspd_call(ubus_ctx, "get", &bb, get_value_single_cb, arg);
@@ -333,7 +336,10 @@ int get_value_group(vendor_data_t *arg)
 	blob_buf_init(&bb, 0);
 
 	blobmsg_add_string(&bb, "path", arg->path);
+	void *table = blobmsg_open_table(&bb, "optional");
 	blobmsg_add_string(&bb, "proto", "cwmp");
+	blobmsg_add_string(&bb, "format", "raw");
+	blobmsg_close_table(&bb, table);
 
 	// Invoke Ubus to get data from uspd
 	res = uspd_call(ubus_ctx, "get", &bb, get_value_group_cb, arg);
@@ -717,7 +723,7 @@ static void lookup_event_cb(struct ubus_context *ctx __attribute__((unused)),
 		return;
 
 	path = blobmsg_data(attr);
-	if (strcmp(path, USP_OBJECT_NAME) == 0) {
+	if (strcmp(path, DM_OBJECT_NAME) == 0) {
 		g_usp_object_available = true;
 		uloop_end();
 	}
@@ -754,7 +760,7 @@ int wait_for_usp_raw_object(void)
 	ubus_register_event_handler(uctx, &add_event, "ubus.object.add");
 
 	// check if object already present
-	ret = ubus_lookup_id(uctx, USP_OBJECT_NAME, &ubus_id);
+	ret = ubus_lookup_id(uctx, DM_OBJECT_NAME, &ubus_id);
 	if (ret == 0) {
 		g_usp_object_available = true;
 		goto end;
@@ -772,7 +778,7 @@ end:
 	ubus_free(uctx);
 
 	if (g_usp_object_available == false) {
-		ERR("%s object not found", USP_OBJECT_NAME);
+		ERR("%s object not found", DM_OBJECT_NAME);
 		return -1;
 	}
 
diff --git a/test/files/etc/config/bulkdata b/test/files/etc/config/bulkdata
new file mode 100644
index 0000000..41329ba
--- /dev/null
+++ b/test/files/etc/config/bulkdata
@@ -0,0 +1,17 @@
+config bulkdata 'bulkdata'
+        option log_level '3'
+        option enable '1'
+
+config profile 'profile_1'
+        option name 'test'
+        option protocol 'HTTP'
+        option reporting_interval '60'
+        option encoding_type 'JSON'
+        option http_url 'http://localhost'
+        option http_method 'PUT'
+        option enable '1'
+
+config profile_parameter 'profile_1_parameter_1'
+        option dm_parent 'profile_1'
+        option name 'test_data'
+        option reference 'Device.DeviceInfo.Uptime'
-- 
GitLab