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
17 #define TLOG_TAG "hwcrypto_srv"
18
19 #include <assert.h>
20 #include <inttypes.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <uapi/err.h>
24
25 #include <hwcrypto/hwrng_dev.h>
26 #include <lib/tipc/tipc.h>
27 #include <trusty_log.h>
28
29 #include "hwkey_srv_priv.h"
30 #include "hwrng_srv_priv.h"
31
32 #include "keybox/srv.h"
33
34 /*
35 * Dispatch event
36 */
dispatch_event(const uevent_t * ev)37 static void dispatch_event(const uevent_t* ev) {
38 assert(ev);
39
40 if (ev->event == IPC_HANDLE_POLL_NONE) {
41 /* not really an event, do nothing */
42 TLOGI("got an empty event\n");
43 return;
44 }
45
46 /* check if we have handler */
47 struct tipc_event_handler* handler = ev->cookie;
48 if (handler && handler->proc) {
49 /* invoke it */
50 handler->proc(ev, handler->priv);
51 return;
52 }
53
54 /* no handler? close it */
55 TLOGE("no handler for event (0x%x) with handle %d\n", ev->event,
56 ev->handle);
57
58 close(ev->handle);
59
60 return;
61 }
62
63 /*
64 * Main application event loop
65 */
main(void)66 int main(void) {
67 int rc;
68 uevent_t event;
69
70 TLOGD("Initializing\n");
71
72 /* initialize service providers */
73 rc = hwrng_start_service();
74 if (rc != NO_ERROR) {
75 TLOGE("Failed (%d) to initialize HWRNG service\n", rc);
76 goto out;
77 }
78 hwkey_init_srv_provider();
79
80 #if defined(WITH_FAKE_KEYBOX)
81 rc = keybox_start_service();
82 if (rc != NO_ERROR) {
83 TLOGE("Failed (%d) to initialize Keybox service\n", rc);
84 goto out;
85 }
86 #endif
87
88 TLOGD("enter main event loop\n");
89
90 /* enter main event loop */
91 while (1) {
92 event.handle = INVALID_IPC_HANDLE;
93 event.event = 0;
94 event.cookie = NULL;
95
96 rc = wait_any(&event, INFINITE_TIME);
97 if (rc < 0) {
98 TLOGE("wait_any failed (%d)\n", rc);
99 break;
100 }
101
102 if (rc == NO_ERROR) { /* got an event */
103 dispatch_event(&event);
104 }
105 }
106
107 out:
108 return rc;
109 }
110