Apply next HAL architecture (hal api + backend)
[platform/adaptation/RPI3/device-manager-plugin-RPI3.git] / hw / touchscreen / touchscreen.c
1 /*
2  * device-node
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <linux/limits.h>
25 #include <dirent.h>
26
27 #include <hal/device/hal-touchscreen-interface.h>
28 #include <hal/hal-common-interface.h>
29 #include "common.h"
30
31 #define TOUCHSCREEN_CON_FILE    "/sys/devices/platform/rpi_ft5406/enable"
32
33 #define TURNON_TOUCHSCREEN     1
34 #define TURNOFF_TOUCHSCREEN    0
35
36 static int touchscreen_get_state(enum touchscreen_state *state)
37 {
38         int ret;
39         int val;
40
41         if (!state)
42                 return -EINVAL;
43
44         ret = sys_get_int(TOUCHSCREEN_CON_FILE, &val);
45         if (ret < 0) {
46                 _E("Failed to get touchscreen state (%d)", ret);
47                 return ret;
48         }
49
50         switch (val) {
51         case TURNOFF_TOUCHSCREEN:
52                 *state = TOUCHSCREEN_OFF;
53                 break;
54         case TURNON_TOUCHSCREEN:
55                 *state = TOUCHSCREEN_ON;
56                 break;
57         default:
58                 _E("Failed to get touchscreen state (%d)", val);
59                 return -EINVAL;
60         }
61
62         return 0;
63 }
64
65 static int touchscreen_set_state(enum touchscreen_state state)
66 {
67         int ret;
68         int val;
69
70         switch (state) {
71         case TOUCHSCREEN_OFF:
72                 val = TURNOFF_TOUCHSCREEN;
73                 break;
74         case TOUCHSCREEN_ON:
75                 val = TURNON_TOUCHSCREEN;
76                 break;
77         default:
78                 _E("Invalid input (%d)", state);
79                 return -EINVAL;
80         }
81
82         ret = sys_set_int(TOUCHSCREEN_CON_FILE, val);
83         if (ret < 0)
84                 _E("Failed to change touchscreen state (%d)", ret);
85
86         return ret;
87 }
88
89 static int touchscreen_init(void **data)
90 {
91         hal_backend_touchscreen_funcs *touchscreen_funcs;
92
93         touchscreen_funcs = calloc(1, sizeof(hal_backend_touchscreen_funcs));
94         if (!touchscreen_funcs)
95                 return -ENOMEM;
96
97         touchscreen_funcs->get_state = touchscreen_get_state;
98         touchscreen_funcs->set_state = touchscreen_set_state;
99
100         *data = (void *)touchscreen_funcs;
101
102         return 0;
103 }
104
105 static int touchscreen_exit(void *data)
106 {
107         if (!data)
108                 return 0;
109
110         free(data);
111         return 0;
112 }
113
114 hal_backend EXPORT hal_backend_device_touchscreen_data = {
115         .name = "touchscreen",
116         .vendor = "rpi3",
117         .abi_version = HAL_ABI_VERSION_TIZEN_6_5,
118         .init = touchscreen_init,
119         .exit = touchscreen_exit,
120 };