1 /*
2  ** Copyright 2024, The Android Open Source Project
3  **
4  ** Licensed under the Apache License, Version 2.0 (the "License");
5  ** you may not use this file except in compliance with the License.
6  ** You may obtain a copy of the License at
7  **
8  **     http://www.apache.org/licenses/LICENSE-2.0
9  **
10  ** Unless required by applicable law or agreed to in writing, software
11  ** distributed under the License is distributed on an "AS IS" BASIS,
12  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  ** See the License for the specific language governing permissions and
14  ** limitations under the License.
15  */
16 #define LOG_TAG "touch_gti_ical"
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #ifdef __ANDROID__
23 #include <cutils/properties.h>
24 #include <cutils/log.h>
25 #else
26 #define property_set
27 #define property_get
28 #define ALOGI printf
29 #define ALOGW printf
30 #endif
31 
main(int argc,char * argv[])32 int main(int argc, char *argv[])
33 {
34 	char *line = NULL;
35 	size_t len = 0;
36 	FILE *ical_fd;
37 	const char *ical_override_cmd_prop[2] = {
38 		[0] = "vendor.touch.gti0.ical.override.cmd",
39 		[1] = "vendor.touch.gti1.ical.override.cmd",
40 	};
41 	const char *ical_override_result_prop[2] = {
42 		[0] = "vendor.touch.gti0.ical.override.result",
43 		[1] = "vendor.touch.gti1.ical.override.result",
44 	};
45 	const char *ical_write_history_prop[2] = {
46 		[0] = "vendor.touch.gti0.ical.write.history",
47 		[1] = "vendor.touch.gti1.ical.write.history",
48 	};
49 	const char *ical_state_prop[2] = {
50 		[0] = "vendor.touch.gti0.ical.state",
51 		[1] = "vendor.touch.gti1.ical.state",
52 	};
53 	const char *ical_result_prop[2] = {
54 		[0] = "vendor.touch.gti0.ical.result",
55 		[1] = "vendor.touch.gti1.ical.result",
56 	};
57 	const char *ical_sysfs[2] = {
58 		[0] = "/sys/devices/virtual/goog_touch_interface/gti.0/interactive_calibrate",
59 		[1] = "/sys/devices/virtual/goog_touch_interface/gti.1/interactive_calibrate",
60 	};
61 	const char *ical_override_cmd_prop_path = ical_override_cmd_prop[0];
62 	const char *ical_override_result_prop_path = ical_override_result_prop[0];
63 	const char *ical_write_history_prop_path = ical_write_history_prop[0];
64 	const char *ical_state_prop_path = ical_state_prop[0];
65 	const char *ical_result_prop_path = ical_result_prop[0];
66 	const char *ical_sysfs_path = ical_sysfs[0];
67 	const char ical_override_all_cmd_prop_val[PROPERTY_VALUE_MAX] = "xxx";
68 	char ical_override_cmd_prop_val[PROPERTY_VALUE_MAX] = "\0";
69 	char ical_override_result_prop_val[PROPERTY_VALUE_MAX] = "\0";
70 	char ical_write_history_prop_val[PROPERTY_VALUE_MAX] = "\0";
71 
72 	if (argc < 3) {
73 		ALOGW("No target dev or command for interactive_calibrate sysfs.\n");
74 		property_set(ical_state_prop[0], "done");
75 		property_set(ical_state_prop[1], "done");
76 		return 0;
77 	}
78 
79 	if (strncmp(argv[1], "1", strlen(argv[1])) == 0 ||
80 		strncmp(argv[1], "gti1", strlen(argv[1])) == 0 ||
81 		strncmp(argv[1], "gti.1", strlen(argv[1])) == 0) {
82 		ical_override_cmd_prop_path = ical_override_cmd_prop[1];
83 		ical_override_result_prop_path = ical_override_result_prop[1];
84 		ical_write_history_prop_path = ical_write_history_prop[1];
85 		ical_state_prop_path = ical_state_prop[1];
86 		ical_result_prop_path = ical_result_prop[1];
87 		ical_sysfs_path = ical_sysfs[1];
88 	}
89 
90 	property_get(ical_override_cmd_prop_path, ical_override_cmd_prop_val, NULL);
91 	property_get(ical_override_result_prop_path, ical_override_result_prop_val, "0 - -2147483648");
92 	property_get(ical_write_history_prop_path, ical_write_history_prop_val, NULL);
93 
94 	property_set(ical_result_prop_path, "na");
95 	property_set(ical_state_prop_path, "running");
96 	if (access(ical_sysfs_path, F_OK | R_OK | W_OK)) {
97 		ALOGW("Can't access %s\n", ical_sysfs_path);
98 		property_set(ical_state_prop_path, "done");
99 		return 0;
100 	}
101 
102 	ical_fd = fopen(ical_sysfs_path, "r+");
103 	if (ical_fd == NULL) {
104 		ALOGW("Can't fopen %s\n", ical_sysfs_path);
105 		property_set(ical_state_prop_path, "done");
106 		return 0;
107 	}
108 
109 	if (strncmp(argv[2], "read", strlen(argv[2])) == 0) {
110 		getline(&line, &len, ical_fd);
111 		if (line != NULL) {
112 			property_set(ical_state_prop_path, "read");
113 			if (strncmp(ical_override_cmd_prop_val,
114 					ical_write_history_prop_val,
115 					strlen(ical_write_history_prop_path)) == 0 ||
116 				strncasecmp(ical_override_cmd_prop_val,
117 					ical_override_all_cmd_prop_val,
118 					strlen(ical_override_all_cmd_prop_val)) == 0) {
119 				property_set(ical_result_prop_path, ical_override_result_prop_val);
120 				ALOGW("read(original): %s => %s",
121 					ical_sysfs_path, line);
122 				ALOGW("read(override): %s => %s",
123 					ical_sysfs_path, ical_override_result_prop_val);
124 			} else {
125 				property_set(ical_result_prop_path, line);
126 				ALOGI("read: %s => %s", ical_sysfs_path, line);
127 			}
128 			free(line);
129 		}
130 	} else {
131 		property_set(ical_write_history_prop_path, argv[2]);
132 		property_set(ical_state_prop_path, argv[2]);
133 		fwrite(argv[2], 1, strlen(argv[2]), ical_fd);
134 		ALOGI("write: %s => %s\n", argv[2], ical_sysfs_path);
135 	}
136 	property_set(ical_state_prop_path, "done");
137 
138 	fclose(ical_fd);
139 	return 0;
140 }
141 
142