1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2020, Open Mobile Platform LLC
4  */
5 
6 #include <err.h>
7 #include <inttypes.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdbool.h>
11 #include <unistd.h>
12 
13 /* OP-TEE TEE client API (built by optee_client) */
14 #include <tee_client_api.h>
15 
16 /* For the UUID (found in the TA's h-file(s)) */
17 #include <plugin_ta.h>
18 
19 #define SLEEP_SEC 2
20 #define TA_PING_CNT 5
21 
main(void)22 int main(void)
23 {
24 	int i = 0;
25 	TEEC_Result res = TEEC_SUCCESS;
26 	TEEC_Context ctx = { };
27 	TEEC_Session sess = { };
28 	TEEC_Operation op = { };
29 	TEEC_UUID uuid = PLUGIN_TA_UUID;
30 	uint32_t err_origin = 0;
31 
32 	/* Initialize a context connecting us to the TEE */
33 	res = TEEC_InitializeContext(NULL, &ctx);
34 	if (res != TEEC_SUCCESS)
35 		errx(1, "TEEC_InitializeContext failed with code %#" PRIx32,
36 		     res);
37 
38 	/* Open a session to the "plugin" TA */
39 	res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
40 			       NULL, &err_origin);
41 	if (res != TEEC_SUCCESS)
42 		errx(1, "TEEC_Opensession failed with code %#" PRIx32 "origin %#" PRIx32,
43 		     res, err_origin);
44 
45 	/* Clear the TEEC_Operation struct */
46 	memset(&op, 0, sizeof(op));
47 	op.paramTypes =
48 		TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, TEEC_NONE);
49 
50 	/*
51 	 * TA will refer to the syslog plugin to print some log messages to REE.
52 	 *
53 	 * See the plugin code in the optee-client.
54 	 * See the log through 'journalctl'.
55 	 */
56 
57 	printf("Work logic: REE --> plugin TA --> syslog plugin in REE --> syslog\n");
58 	printf("See the log from TEE through 'journalctl'\n\n");
59 
60 	for (i = 0; i < TA_PING_CNT; ++i) {
61 		res = TEEC_InvokeCommand(&sess, PLUGIN_TA_PING, &op,
62 					 &err_origin);
63 
64 		printf("Attempt #%d: TEEC_InvokeCommand() %s; res=%#" PRIx32 " orig=%#" PRIx32 "\n",
65 		       i + 1, (res == TEEC_SUCCESS) ? "success" : "failed",
66 		       res, err_origin);
67 
68 		sleep(SLEEP_SEC);
69 	}
70 
71 	/*
72 	 * We're done with the TA, close the session and
73 	 * destroy the context.
74 	 */
75 
76 	TEEC_CloseSession(&sess);
77 	TEEC_FinalizeContext(&ctx);
78 
79 	return 0;
80 }
81