Super Liu | 28754e3 | 2024-03-26 00:50:43 +0000 | [diff] [blame^] | 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 | |
| 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_state_prop[2] = { |
| 38 | [0] = "vendor.touch.gti0.ical.state", |
| 39 | [1] = "vendor.touch.gti1.ical.state", |
| 40 | }; |
| 41 | const char *ical_result_prop[2] = { |
| 42 | [0] = "vendor.touch.gti0.ical.result", |
| 43 | [1] = "vendor.touch.gti1.ical.result", |
| 44 | }; |
| 45 | const char *ical_sysfs[2] = { |
| 46 | [0] = "/sys/devices/virtual/goog_touch_interface/gti.0/interactive_calibrate", |
| 47 | [1] = "/sys/devices/virtual/goog_touch_interface/gti.1/interactive_calibrate", |
| 48 | }; |
| 49 | const char *ical_state_prop_path = ical_state_prop[0]; |
| 50 | const char *ical_result_prop_path = ical_result_prop[0]; |
| 51 | const char *ical_sysfs_path = ical_sysfs[0]; |
| 52 | |
| 53 | if (argc < 3) { |
| 54 | ALOGW("No target dev or command for interactive_calibrate sysfs.\n"); |
| 55 | property_set(ical_state_prop[0], "done"); |
| 56 | property_set(ical_state_prop[1], "done"); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | if (strncmp(argv[1], "1", strlen(argv[1])) == 0 || |
| 61 | strncmp(argv[1], "gti1", strlen(argv[1])) == 0 || |
| 62 | strncmp(argv[1], "gti.1", strlen(argv[1])) == 0) { |
| 63 | ical_state_prop_path = ical_state_prop[1]; |
| 64 | ical_result_prop_path = ical_result_prop[1]; |
| 65 | ical_sysfs_path = ical_sysfs[1]; |
| 66 | } |
| 67 | |
| 68 | property_set(ical_result_prop_path, "na"); |
| 69 | property_set(ical_state_prop_path, "running"); |
| 70 | if (access(ical_sysfs_path, F_OK | R_OK | W_OK)) { |
| 71 | ALOGW("Can't access %s\n", ical_sysfs_path); |
| 72 | property_set(ical_state_prop_path, "done"); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | ical_fd = fopen(ical_sysfs_path, "r+"); |
| 77 | if (ical_fd == NULL) { |
| 78 | ALOGW("Can't fopen %s\n", ical_sysfs_path); |
| 79 | property_set(ical_state_prop_path, "done"); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | if (strncmp(argv[2], "read", strlen(argv[2])) == 0) { |
| 84 | getline(&line, &len, ical_fd); |
| 85 | if (line != NULL) { |
| 86 | property_set(ical_state_prop_path, "read"); |
| 87 | property_set(ical_result_prop_path, line); |
| 88 | ALOGI("read: %s => %s", ical_sysfs_path, line); |
| 89 | free(line); |
| 90 | } |
| 91 | } else { |
| 92 | property_set(ical_state_prop_path, argv[2]); |
| 93 | fwrite(argv[2], 1, strlen(argv[2]), ical_fd); |
| 94 | ALOGI("write: %s => %s\n", argv[2], ical_sysfs_path); |
| 95 | } |
| 96 | property_set(ical_state_prop_path, "done"); |
| 97 | |
| 98 | fclose(ical_fd); |
| 99 | return 0; |
| 100 | } |
| 101 | |