import exynos 7570 bsp
[GitHub/LineageOS/android_hardware_samsung_slsi_exynos7580.git] / mobicore / TuiService / jni / tlcTui.cpp
CommitLineData
cd9434cc
T
1/*
2 * Copyright (c) 2013-2015 TRUSTONIC LIMITED
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the TRUSTONIC LIMITED nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <stdlib.h>
33#include <jni.h>
34#include <pthread.h>
35#include <fcntl.h>
36#include <sys/syscall.h> /* For SYS_xxx definitions */
37#include <sys/types.h>
38#include <sys/stat.h>
39#include <unistd.h>
40#include <sys/mman.h>
41#include <sys/ioctl.h>
42
43#include "tui_ioctl.h"
44
45#include "tlcTui.h"
46#include "tlcTuiJni.h"
47
48#define LOG_TAG "TlcTui"
49#include "log.h"
50
51/* ------------------------------------------------------------- */
52/* Globals */
53bool testGetEvent = false;
54/* ------------------------------------------------------------- */
55/* Static */
56static pthread_t threadId;
57static int32_t drvFd = -1;
58
59/* ------------------------------------------------------------- */
60/* Static functions */
61static void *mainThread(void *);
62
63/* Functions */
64/* ------------------------------------------------------------- */
65/**
66 * TODO.
67 */
68static void *mainThread(void *) {
69// int32_t fd;
70// struct stat st;
71// uint32_t size;
72// void *address;
73 uint32_t cmdId;
74
75 LOG_D("mainThread: TlcTui start!");
76
77/* Android APP has no right to load a .ko. It must be loaded prior to starting the app.
78 // Load the k-TLC
79 fd = open("/data/app/tlckTuiPlay.ko", O_RDONLY);
80 if (fd < 0) {
81 LOG_E("mainThread: Could not find k-tlc file!");
82 exit(1);
83 }
84 else {
85 fstat(fd, &st);
86 size = st.st_size;
87 address = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
88 if (syscall(__NR_init_module, address, size, NULL)) {
89 int32_t errsv = errno;
90 LOG_E("mainThread: Load k-tlc failed with errno %s!", strerror(errsv));
91 exit(1);
92 }
93 close(fd);
94 }
95*/
96 drvFd = open("/dev/" TUI_DEV_NAME, O_NONBLOCK);
97 if (drvFd < 0) {
98 LOG_E("mainThread: open k-tlc device failed with errno %s.", strerror(errno));
99 exit(1);
100 }
101
102 /* TlcTui main thread loop */
103 for (;;) {
104 /* Wait for a command from the k-TLC*/
105 if (false == tlcWaitCmdFromDriver(&cmdId)) {
106 break;
107 }
108 /* Something has been received, process it. */
109 if (false == tlcProcessCmd(cmdId)) {
110 break;
111 }
112 }
113
114 // Close
115 close(drvFd);
116
117 // close_module() ?
118 return NULL;
119}
120/* ------------------------------------------------------------- */
121bool tlcLaunch(void) {
122
123 bool ret = false;
124 /* Create the TlcTui Main thread */
125 if (pthread_create(&threadId, NULL, &mainThread, NULL) != 0) {
126 LOG_E("tlcLaunch: pthread_create failed!");
127 ret = false;
128 } else {
129 ret = true;
130 }
131
132 return ret;
133}
134
135/* ------------------------------------------------------------- */
136bool tlcWaitCmdFromDriver(uint32_t *pCmdId) {
137 uint32_t cmdId = 0;
138 int ioctlRet = 0;
139
140 /* Wait for ioctl to return from k-tlc with a command ID */
141 /* Loop if ioctl has been interrupted. */
142 do {
143 ioctlRet = ioctl(drvFd, TUI_IO_WAITCMD, &cmdId);
144 } while((EINTR == errno) && (-1 == ioctlRet));
145
146 if (-1 == ioctlRet) {
147 LOG_E("TUI_IO_WAITCMD ioctl failed with errno %s.", strerror(errno));
148 return false;
149 }
150 *pCmdId = cmdId;
151 return true;
152}
153
154/* ------------------------------------------------------------- */
155bool tlcNotifyEvent(uint32_t eventType) {
156
157 if (-1 == ioctl(drvFd, TUI_IO_NOTIFY, eventType)) {
158 LOG_E("TUI_IO_NOTIFY ioctl failed with errno %s.", strerror(errno));
159 return false;
160 }
161
162 return true;
163}
164
165/* ------------------------------------------------------------- */
166bool tlcProcessCmd(uint32_t commandId) {
167 uint32_t ret = TLC_TUI_ERROR;
168 struct tlc_tui_response_t response;
169
170 bool acknowledge = true;
171
172 switch (commandId) {
173 case TLC_TUI_CMD_NONE:
174 LOG_I("tlcProcessCmd: TLC_TUI_CMD_NONE.");
175 acknowledge = false;
176 break;
177
178 case TLC_TUI_CMD_START_ACTIVITY:
179 LOG_D("tlcProcessCmd: TLC_TUI_CMD_START_ACTIVITY.");
180 ret = tlcStartTuiSession();
181 LOG_D("tlcStartTuiSession returned %d", ret);
182 if (ret == TUI_JNI_OK) {
183 ret = TLC_TUI_OK;
184 } else {
185 ret = TLC_TUI_ERROR;
186 acknowledge = false;
187 }
188 break;
189
190 case TLC_TUI_CMD_STOP_ACTIVITY:
191 LOG_D("tlcProcessCmd: TLC_TUI_CMD_STOP_ACTIVITY.");
192 ret = tlcFinishTuiSession();
193 LOG_D("tlcFinishTuiSession returned %d", ret);
194 if (ret == TUI_JNI_OK) {
195 ret = TLC_TUI_OK;
196 } else {
197 ret = TLC_TUI_ERROR;
198 }
199 break;
200
201 default:
202 LOG_E("tlcProcessCmd: Unknown command %d", commandId);
203 acknowledge = false;
204 ret = TLC_TUI_ERR_UNKNOWN_CMD;
205 break;
206 }
207
208 // Send command return code to the k-tlc
209 response.id = commandId;
210 response.return_code = ret;
211 if (acknowledge) {
212 if (-1 == ioctl(drvFd, TUI_IO_ACK, &response)) {
213 LOG_E("TUI_IO_ACK ioctl failed with errno %s.", strerror(errno));
214 return false;
215 }
216 }
217
218 LOG_D("tlcProcessCmd: ret = %d", ret);
219 return true;
220}
221