input_hal branch
[staging/toyota.git] / src / input_hal.cpp
1 /*
2  * @copyright Copyright (c) 2017-2020 TOYOTA MOTOR CORPORATION.
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
17 #include "input_hal.h"
18
19 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "input_hal_debug.h"
25 #include "input_hal_internal.h"
26 #include "input_touch_ilitek.h"
27 #include "input_udev_monitor.h"
28
29 // Touch panel operation  function info
30 static struct TouchHal g_input_touch_info = { 0 };
31
32 char* g_app_name = NULL;
33 static bool g_touch_inited = false;
34 static bool g_input_inited = false;
35
36 extern bool g_break_from_watch;
37 extern pthread_t g_udev_monitor_thread;
38
39 // Environment key name
40 #define HAL_INPUT_TARGET_BOARD "TARGET_BOARD"
41 // Reference board environment value of HAL_INPUT_TARGET_BOARD
42 #define HAL_INPUT_REF_BOARD_NAME "agl_reference"
43 // Invalid status of report touch panel's touch event
44 #define HAL_INPUT_TOUCH_REPORT_INVALID  (-1)
45
46 /*
47  * Input device init.
48  */
49 int InitInput(const char* app_name) {
50   if (NULL == app_name) {
51     INPUT_ERROR_LOG("param is error");
52     return HAL_INPUT_RET_ERROR;
53   }
54
55   if (!g_touch_inited) {
56     INPUT_ERROR_LOG("call InitTouch first.");
57     return HAL_INPUT_RET_ERROR;
58   }
59
60   if (g_input_inited) {
61     INPUT_ERROR_LOG("input inited.");
62     return HAL_INPUT_RET_ERROR;
63   }
64
65   g_break_from_watch = false;
66   if (NULL != g_app_name) {
67     delete[] g_app_name;
68   }
69
70   g_app_name = new char[strlen(app_name) + 1];
71   snprintf(g_app_name, strlen(app_name) + 1, "%s", app_name);
72   if (HAL_INPUT_RET_ERROR == InputUdevMonitorThreadCreate()) {
73     delete[] g_app_name;
74     g_app_name = NULL;
75     return HAL_INPUT_RET_ERROR;
76   }
77
78   g_input_inited = true;
79   return HAL_INPUT_RET_NORMAL;
80 }
81
82 /*
83  * Deinit input device
84  */
85 int DeInitInput() {
86   g_break_from_watch = true;
87   void* ret_val = NULL;
88   if (NULL != g_app_name) {
89     delete[] g_app_name;
90     g_app_name = NULL;
91   }
92   if (g_udev_monitor_thread != static_cast<pthread_t>(-1)) {
93     pthread_join(g_udev_monitor_thread, &ret_val);
94   }
95   g_input_inited = false;
96   return HAL_INPUT_RET_NORMAL;
97 }
98
99 /*
100  * Init those operating function of touch panel driver
101  */
102 int InitTouch() {
103   int ret = InputTouchIlitekInit(&g_input_touch_info);
104   g_touch_inited = true;
105   return ret;
106 }
107
108 /*
109  * Make touch panel start work
110  */
111 int StartTouch() {
112   int ret = HAL_INPUT_RET_ERROR;
113
114   if (NULL != g_input_touch_info.start) {
115     ret = g_input_touch_info.start();
116   }
117
118   return ret;
119 }
120
121 /*
122  * Config touch panel
123  */
124 int ConfigTouch(const char *path , int reso_h, int reso_v) {
125   int ret = HAL_INPUT_RET_ERROR;
126
127   if (NULL != g_input_touch_info.config) {
128     ret = g_input_touch_info.config(path, reso_h, reso_v);
129   }
130
131   return ret;
132 }
133
134 /*
135  * Get touch panel device name
136  */
137 int GetPanelNameTouch(char* name, size_t buf_length) {
138   int ret = HAL_INPUT_RET_ERROR;
139
140   if (NULL != g_input_touch_info.get_touch_devicename) {
141     ret = g_input_touch_info.get_touch_devicename(name, buf_length);
142   }
143
144   return ret;
145 }
146
147 /*
148  * Get touch panel key device name
149  */
150 int GetKeyNameTouch(char* name, size_t buf_length) {
151   int ret = HAL_INPUT_RET_ERROR;
152
153   if (NULL != g_input_touch_info.get_key_devicename) {
154     ret = g_input_touch_info.get_key_devicename(name, buf_length);
155   }
156
157   return ret;
158 }
159
160 /*
161  * Execute touch panel self test
162  */
163 int SelfTestTouch(int id, void *result) {
164   int ret = HAL_INPUT_RET_ERROR;
165
166   if (NULL != g_input_touch_info.selftest) {
167     ret = g_input_touch_info.selftest(id, result);
168   }
169
170   return ret;
171 }
172
173 /*
174  * Get touch panel config status
175  */
176 int GetConfigStatusTouch(int *status) {
177   int ret = HAL_INPUT_RET_ERROR;
178
179   if (NULL != g_input_touch_info.get_config_status) {
180     ret = g_input_touch_info.get_config_status(status);
181   }
182
183   return ret;
184 }
185
186 /*
187  * Set whether the driver sends touch panel data or not
188  */
189 int LockTouch(int status) {
190   static int input_touch_lock_status = HAL_INPUT_TOUCH_REPORT_INVALID;
191
192   if (input_touch_lock_status == status) {
193     return HAL_INPUT_RET_NORMAL;
194   }
195
196   int ret = HAL_INPUT_RET_ERROR;
197   if (NULL != g_input_touch_info.set_touch_lock) {
198     ret = g_input_touch_info.set_touch_lock(status);
199     if (HAL_INPUT_RET_NORMAL == ret) {
200       input_touch_lock_status = status;
201     }
202   }
203
204   return ret;
205 }
206
207 /*
208  * Suspend touch panel
209  */
210 int SuspendTouch() {
211   int ret = HAL_INPUT_RET_ERROR;
212
213   if (NULL != g_input_touch_info.set_touch_suspend) {
214     ret = g_input_touch_info.set_touch_suspend();
215   }
216
217   return ret;
218 }
219
220 /*
221  * Set touch panel sensitivity level
222  */
223 int SetSensitivityLevelTouch(int level) {
224   int cur = HAL_INPUT_TOUCH_SENSITIVITY_LEVEL_NONE;
225
226   int ret = GetSensitivityLevelTouch(&cur);
227   if (HAL_INPUT_RET_NORMAL == ret) {
228     if (cur == level) {
229       // Don't need to update sensitivity level
230       INPUT_LOG_TRACE("already set level=%d", level);
231     } else {
232       if (NULL != g_input_touch_info.set_sensitivity_level) {
233         ret = g_input_touch_info.set_sensitivity_level(level);
234       } else {
235         ret = HAL_INPUT_RET_ERROR;
236       }
237     }
238   }
239
240   return ret;
241 }
242
243 /*
244  * Get touch panel sensitivity level
245  */
246 int GetSensitivityLevelTouch(int *level) {
247   int ret = HAL_INPUT_RET_ERROR;
248
249   if (NULL != g_input_touch_info.get_sensitivity_level) {
250     ret = g_input_touch_info.get_sensitivity_level(level);
251   }
252
253   return ret;
254 }
255
256 /*
257  * Notify radio scan frequency
258  */
259 int NotifyRadioScanFreqTouch(struct RadioInfoTouch *info) {
260   int ret = HAL_INPUT_RET_ERROR;
261
262   if (NULL != g_input_touch_info.notify_radio_scan_frequency) {
263     ret = g_input_touch_info.notify_radio_scan_frequency(info);
264   }
265
266   return ret;
267 }