此博客按我的编写过程记录将optee example 中的helloworld程序移植为android APP的过程
在正确编译及烧写optee+asop,启动开发板,进入控制台可以成功运行xtest 以及实例程序。对应的客户端程序在/vendor/bin下,对应的TA程序*.ta在/vendor/lib/optee_armtz。(当时找这个找了好久)。在控制台下运行客户端程序例如xtest optee_example_helloworld, 会与对应的ta程序会话完成ta中相应的功能,并获得ta返回的结果。如下:

如果要编写android下的程序实现上述的功能,则android app就对应非安全世界的程序,即控制台界面下的optee_example_hello_world 程序,android app与对应的ta程序会话,获取安全世界的执行结果。
移植过程:
1 在Android Studio下新建一个工程,这里我取名为Jniteeeest(因为调用本地接口需要使用jni) 一切使用默认的配置,选择添加一个空的Activity, 并为该Activity创建布局文件,修改布局文件,为textview增加id属性:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/onlytv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
2 新建一个JniUtils类, 用来调用本地方法:

JniUtils.java类文件的代码为:
package com.example.wangzh.jniteeeest;
public class JniUtils {
static {
System.loadLibrary("test");
}
public native int getIntFromTA();
}
在该类中声明了一个本地方法getIntFromTA(),表示从TA中获取处理后的结果值。静态块加载test共享库文件,因此该本地方法需要编译进libtest.so里面。
编译整个项目,会在build目录下生成对应的JniUtils.class文件,如下:

将目录切换到上图中的debug一级下,打开shell或者控制台,执行
javah -o opteeheader.h com.example.wangzh.jniteeeest.JniUtils
该命令会该类中声明的本地方法,生成c语言的头文件,头文件里有函数原型的声明:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_wangzh_jniteeeest_JniUtils */
#ifndef _Included_com_example_wangzh_jniteeeest_JniUtils
#define _Included_com_example_wangzh_jniteeeest_JniUtils
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_wangzh_jniteeeest_JniUtils
* Method: getIntFromTA
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_example_wangzh_jniteeeest_JniUtils_getIntFromTA
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
接下来要做的是,用c语言实现JniUtils类中声明的native方法。
待续。。。
本文档详细记录了如何将OPTEE的HelloWorld示例移植为Android应用的过程。首先确保OPTEE和AOSP正确编译并烧录到开发板上,然后在Android Studio中创建新工程,利用JNI调用安全世界中的TA程序。通过声明本地方法并生成C语言头文件,开始实现Android应用与TA间的交互功能。
1309





