2 * Copyright (c) 2013-2015 TRUSTONIC LIMITED
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
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.
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.
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.
36 #include <sys/syscall.h> /* For SYS_xxx definitions */
37 #include <sys/types.h>
41 #include <sys/ioctl.h>
43 #include "tui_ioctl.h"
46 #include "tlcTuiJni.h"
48 #define LOG_TAG "TlcTui"
51 /* ------------------------------------------------------------- */
53 bool testGetEvent
= false;
54 /* ------------------------------------------------------------- */
56 static pthread_t threadId
;
57 static int32_t drvFd
= -1;
59 /* ------------------------------------------------------------- */
60 /* Static functions */
61 static void *mainThread(void *);
64 /* ------------------------------------------------------------- */
68 static void *mainThread(void *) {
75 LOG_D("mainThread: TlcTui start!");
77 /* Android APP has no right to load a .ko. It must be loaded prior to starting the app.
79 fd = open("/data/app/tlckTuiPlay.ko", O_RDONLY);
81 LOG_E("mainThread: Could not find k-tlc file!");
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));
96 drvFd
= open("/dev/" TUI_DEV_NAME
, O_NONBLOCK
);
98 LOG_E("mainThread: open k-tlc device failed with errno %s.", strerror(errno
));
102 /* TlcTui main thread loop */
104 /* Wait for a command from the k-TLC*/
105 if (false == tlcWaitCmdFromDriver(&cmdId
)) {
108 /* Something has been received, process it. */
109 if (false == tlcProcessCmd(cmdId
)) {
120 /* ------------------------------------------------------------- */
121 bool tlcLaunch(void) {
124 /* Create the TlcTui Main thread */
125 if (pthread_create(&threadId
, NULL
, &mainThread
, NULL
) != 0) {
126 LOG_E("tlcLaunch: pthread_create failed!");
135 /* ------------------------------------------------------------- */
136 bool tlcWaitCmdFromDriver(uint32_t *pCmdId
) {
140 /* Wait for ioctl to return from k-tlc with a command ID */
141 /* Loop if ioctl has been interrupted. */
143 ioctlRet
= ioctl(drvFd
, TUI_IO_WAITCMD
, &cmdId
);
144 } while((EINTR
== errno
) && (-1 == ioctlRet
));
146 if (-1 == ioctlRet
) {
147 LOG_E("TUI_IO_WAITCMD ioctl failed with errno %s.", strerror(errno
));
154 /* ------------------------------------------------------------- */
155 bool tlcNotifyEvent(uint32_t eventType
) {
157 if (-1 == ioctl(drvFd
, TUI_IO_NOTIFY
, eventType
)) {
158 LOG_E("TUI_IO_NOTIFY ioctl failed with errno %s.", strerror(errno
));
165 /* ------------------------------------------------------------- */
166 bool tlcProcessCmd(uint32_t commandId
) {
167 uint32_t ret
= TLC_TUI_ERROR
;
168 struct tlc_tui_response_t response
;
170 bool acknowledge
= true;
173 case TLC_TUI_CMD_NONE
:
174 LOG_I("tlcProcessCmd: TLC_TUI_CMD_NONE.");
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
) {
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
) {
202 LOG_E("tlcProcessCmd: Unknown command %d", commandId
);
204 ret
= TLC_TUI_ERR_UNKNOWN_CMD
;
208 // Send command return code to the k-tlc
209 response
.id
= commandId
;
210 response
.return_code
= ret
;
212 if (-1 == ioctl(drvFd
, TUI_IO_ACK
, &response
)) {
213 LOG_E("TUI_IO_ACK ioctl failed with errno %s.", strerror(errno
));
218 LOG_D("tlcProcessCmd: ret = %d", ret
);