touch: init service to access gti interactive_calibrate sysfs

Write usage: setprop vendor.touch.gti0.ical VALUE

Any VALUE(e.g. 20X or 30X) set through this property will write
into gti interactive_calibrate sysfs except for the VALUE `read`.

Read usage: setprop vendor.touch.gti0.ical read
            getprop vendor.touch.gti0.ical.result
setprop with VALUE `read` will read the gti
interactive_calibrate sysfs and save the result to
vendor.touch.gti0.ical.result.

Besides, please add your DOMAIN to access vendor_gti_prop.
set_prop(DOMAIN, vendor_gti_prop)

Bug: 331139251
Test: Manually verify the result.
Change-Id: I6296b9533dd0209e66832bfbbed6030a30077b96
Signed-off-by: Super Liu <supercjliu@google.com>
diff --git a/touch/gti/Android.bp b/touch/gti/Android.bp
index c066d0d..7f2eb66 100644
--- a/touch/gti/Android.bp
+++ b/touch/gti/Android.bp
@@ -17,3 +17,19 @@
     vendor: true,
     sub_dir: "dump",
 }
+
+cc_binary {
+    name: "touch_gti_ical",
+    srcs: ["touch_gti_ical.cpp"],
+    cflags: [
+        "-Wall",
+        "-Wextra",
+        "-Werror",
+    ],
+    shared_libs: [
+        "libbase",
+        "libcutils",
+        "liblog",
+    ],
+    vendor: true,
+}
diff --git a/touch/gti/gti.mk b/touch/gti/gti.mk
index 171534f..a2c7cf7 100644
--- a/touch/gti/gti.mk
+++ b/touch/gti/gti.mk
@@ -1,3 +1,4 @@
 BOARD_VENDOR_SEPOLICY_DIRS += device/google/gs-common/touch/gti/sepolicy
 
 PRODUCT_PACKAGES += dump_gti0.sh
+PRODUCT_PACKAGES += touch_gti_ical
diff --git a/touch/gti/init.touch.gti0.rc b/touch/gti/init.touch.gti0.rc
index 31856ea..5f8318e 100644
--- a/touch/gti/init.touch.gti0.rc
+++ b/touch/gti/init.touch.gti0.rc
@@ -24,3 +24,15 @@
     chown system system /proc/goog_touch_interface/gti.0/ss_base
     chown system system /proc/goog_touch_interface/gti.0/ss_diff
     chown system system /proc/goog_touch_interface/gti.0/ss_raw
+
+on property:vendor.touch.gti0.ical=*
+    setprop vendor.touch.gti0.ical.state init
+    start touch_gti0_ical
+    wait_for_prop vendor.touch.gti0.ical.state done
+
+service touch_gti0_ical /vendor/bin/touch_gti_ical 0 ${vendor.touch.gti0.ical}
+    class main
+    user system
+    group system shell
+    disabled
+    oneshot
diff --git a/touch/gti/init.touch.gti1.rc b/touch/gti/init.touch.gti1.rc
index a4de328..ba99210 100644
--- a/touch/gti/init.touch.gti1.rc
+++ b/touch/gti/init.touch.gti1.rc
@@ -24,3 +24,15 @@
     chown system system /proc/goog_touch_interface/gti.1/ss_base
     chown system system /proc/goog_touch_interface/gti.1/ss_diff
     chown system system /proc/goog_touch_interface/gti.1/ss_raw
+
+on property:vendor.touch.gti1.ical=*
+    setprop vendor.touch.gti1.ical.state init
+    start touch_gti1_ical
+    wait_for_prop vendor.touch.gti1.ical.state done
+
+service touch_gti1_ical /vendor/bin/touch_gti_ical 1 ${vendor.touch.gti1.ical}
+    class main
+    user system
+    group system shell
+    disabled
+    oneshot
diff --git a/touch/gti/sepolicy/file_contexts b/touch/gti/sepolicy/file_contexts
index 5c429b7..37e3733 100644
--- a/touch/gti/sepolicy/file_contexts
+++ b/touch/gti/sepolicy/file_contexts
@@ -1,2 +1,2 @@
 /vendor/bin/dump/dump_gti0\.sh      u:object_r:dump_gti_exec:s0
-
+/vendor/bin/touch_gti_ical          u:object_r:gti_ical_exec:s0
diff --git a/touch/gti/sepolicy/gti_ical.te b/touch/gti/sepolicy/gti_ical.te
new file mode 100644
index 0000000..228782c
--- /dev/null
+++ b/touch/gti/sepolicy/gti_ical.te
@@ -0,0 +1,8 @@
+type gti_ical, domain;
+type gti_ical_exec, vendor_file_type, exec_type, file_type;
+init_daemon_domain(gti_ical)
+
+allow gti_ical sysfs_touch_gti:file rw_file_perms;
+allow gti_ical sysfs_touch_gti:dir search;
+
+set_prop(gti_ical, vendor_gti_prop)
diff --git a/touch/gti/sepolicy/property.te b/touch/gti/sepolicy/property.te
new file mode 100644
index 0000000..2a71d74
--- /dev/null
+++ b/touch/gti/sepolicy/property.te
@@ -0,0 +1 @@
+system_public_prop(vendor_gti_prop)
diff --git a/touch/gti/sepolicy/property_contexts b/touch/gti/sepolicy/property_contexts
new file mode 100644
index 0000000..e3badcd
--- /dev/null
+++ b/touch/gti/sepolicy/property_contexts
@@ -0,0 +1 @@
+vendor.touch.gti0.     u:object_r:vendor_gti_prop:s0
diff --git a/touch/gti/sepolicy/vendor_init.te b/touch/gti/sepolicy/vendor_init.te
new file mode 100644
index 0000000..ed0ebda
--- /dev/null
+++ b/touch/gti/sepolicy/vendor_init.te
@@ -0,0 +1 @@
+set_prop(vendor_init, vendor_gti_prop)
diff --git a/touch/gti/sepolicy_gti_dual/property_contexts b/touch/gti/sepolicy_gti_dual/property_contexts
new file mode 100644
index 0000000..c3530ff
--- /dev/null
+++ b/touch/gti/sepolicy_gti_dual/property_contexts
@@ -0,0 +1 @@
+vendor.touch.gti1.     u:object_r:vendor_gti_prop:s0
diff --git a/touch/gti/touch_gti_ical.cpp b/touch/gti/touch_gti_ical.cpp
new file mode 100644
index 0000000..0aabd9e
--- /dev/null
+++ b/touch/gti/touch_gti_ical.cpp
@@ -0,0 +1,101 @@
+/*
+ ** Copyright 2024, The Android Open Source Project
+ **
+ ** Licensed under the Apache License, Version 2.0 (the "License");
+ ** you may not use this file except in compliance with the License.
+ ** You may obtain a copy of the License at
+ **
+ **     http://www.apache.org/licenses/LICENSE-2.0
+ **
+ ** Unless required by applicable law or agreed to in writing, software
+ ** distributed under the License is distributed on an "AS IS" BASIS,
+ ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ** See the License for the specific language governing permissions and
+ ** limitations under the License.
+ */
+#define LOG_TAG "touch_gti_ical"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#ifdef __ANDROID__
+#include <cutils/properties.h>
+#include <cutils/log.h>
+#else
+#define property_set
+#define property_get
+#define ALOGI printf
+#define ALOGW printf
+#endif
+
+int main(int argc, char *argv[])
+{
+	char *line = NULL;
+	size_t len = 0;
+	FILE *ical_fd;
+	const char *ical_state_prop[2] = {
+		[0] = "vendor.touch.gti0.ical.state",
+		[1] = "vendor.touch.gti1.ical.state",
+	};
+	const char *ical_result_prop[2] = {
+		[0] = "vendor.touch.gti0.ical.result",
+		[1] = "vendor.touch.gti1.ical.result",
+	};
+	const char *ical_sysfs[2] = {
+		[0] = "/sys/devices/virtual/goog_touch_interface/gti.0/interactive_calibrate",
+		[1] = "/sys/devices/virtual/goog_touch_interface/gti.1/interactive_calibrate",
+	};
+	const char *ical_state_prop_path = ical_state_prop[0];
+	const char *ical_result_prop_path = ical_result_prop[0];
+	const char *ical_sysfs_path = ical_sysfs[0];
+
+	if (argc < 3) {
+		ALOGW("No target dev or command for interactive_calibrate sysfs.\n");
+		property_set(ical_state_prop[0], "done");
+		property_set(ical_state_prop[1], "done");
+		return 0;
+	}
+
+	if (strncmp(argv[1], "1", strlen(argv[1])) == 0 ||
+		strncmp(argv[1], "gti1", strlen(argv[1])) == 0 ||
+		strncmp(argv[1], "gti.1", strlen(argv[1])) == 0) {
+		ical_state_prop_path = ical_state_prop[1];
+		ical_result_prop_path = ical_result_prop[1];
+		ical_sysfs_path = ical_sysfs[1];
+	}
+
+	property_set(ical_result_prop_path, "na");
+	property_set(ical_state_prop_path, "running");
+	if (access(ical_sysfs_path, F_OK | R_OK | W_OK)) {
+		ALOGW("Can't access %s\n", ical_sysfs_path);
+		property_set(ical_state_prop_path, "done");
+		return 0;
+	}
+
+	ical_fd = fopen(ical_sysfs_path, "r+");
+	if (ical_fd == NULL) {
+		ALOGW("Can't fopen %s\n", ical_sysfs_path);
+		property_set(ical_state_prop_path, "done");
+		return 0;
+	}
+
+	if (strncmp(argv[2], "read", strlen(argv[2])) == 0) {
+		getline(&line, &len, ical_fd);
+		if (line != NULL) {
+			property_set(ical_state_prop_path, "read");
+			property_set(ical_result_prop_path, line);
+			ALOGI("read: %s => %s", ical_sysfs_path, line);
+			free(line);
+		}
+	} else {
+		property_set(ical_state_prop_path, argv[2]);
+		fwrite(argv[2], 1, strlen(argv[2]), ical_fd);
+		ALOGI("write: %s => %s\n", argv[2], ical_sysfs_path);
+	}
+	property_set(ical_state_prop_path, "done");
+
+	fclose(ical_fd);
+	return 0;
+}
+