1 /*
2 * Copyright (C) 2016 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 #include <stddef.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <trusty/time.h>
21 #include <trusty_ipc.h>
22 #include <trusty_log.h>
23 #include <uapi/err.h>
24
25 #define TLOG_TAG "spinner-app"
26 #define TEST_CTRL_PORT "com.android.trusty.spinner"
27
28 #define MSEC 1000000ULL
29
_delay(int64_t ns_delay)30 static int _delay(int64_t ns_delay) {
31 int rc;
32 int64_t curr;
33 int64_t start;
34
35 rc = trusty_gettime(0, &start);
36 curr = start;
37 while (!rc && curr < start + ns_delay)
38 rc = trusty_gettime(0, &curr);
39 return rc;
40 }
41
main(void)42 int main(void) {
43 int rc;
44 handle_t hport;
45 uuid_t peer_uuid;
46
47 TLOGD("Starting spinner test app!!!\n");
48
49 /* create control port and wait on it */
50 rc = port_create(TEST_CTRL_PORT, 1, 1024, IPC_PORT_ALLOW_NS_CONNECT);
51 if (rc < 0) {
52 TLOGI("failed (%d) to create ctrl port\n", rc);
53 return rc;
54 }
55 hport = (handle_t)rc;
56
57 /* and just wait forever on control port */
58 for (;;) {
59 uevent_t uevt;
60 int rc = wait(hport, &uevt, -1);
61 if (rc == NO_ERROR) {
62 if (uevt.event & IPC_HANDLE_POLL_READY) {
63 /* got connection request */
64 rc = accept(uevt.handle, &peer_uuid);
65 if (rc >= 0) {
66 handle_t ctrl_chan = (handle_t)rc;
67
68 trusty_nanosleep(0, 0, 2 * 1000 * MSEC);
69 for (;;) {
70 rc = _delay(2 * 1000 * MSEC);
71 if (rc < 0)
72 break;
73
74 rc = wait(ctrl_chan, &uevt, 0);
75 if (rc == ERR_CHANNEL_CLOSED) {
76 break;
77 }
78 if (uevt.event & IPC_HANDLE_POLL_HUP) {
79 break;
80 }
81 }
82 close(ctrl_chan);
83 continue;
84 }
85 }
86 }
87 if (rc < 0)
88 break;
89 }
90 return rc;
91 }
92