Simple Jni and Aidl implementation

First explain Jni. In my opinion, Jni is a tool for Java and Native interaction in the Android system.

So what is AIDL? Android is a unique AIDL communication mechanism, so that code implemented in other processes can also call an interface.

Then talk about why you want to write this Jni and AIDL, this is because there was a problem at that time, you need to get a data in a so, the so must be obtained through jni. Specifically why you have to pass jni, not much to say here.

Finally, let's talk about the basic idea. First call the method of obtaining data from the C file, and then pass it to the upper layer through jni, and the upper layer can obtain data in real time in each process through aidi

The main code is as follows

\device\mediatek\common\sepolicy\full\service.te

# Add a system service
type here illa common_service, app_api_service, system_server_service, service_manager_type;

\device\mediatek\common\sepolicy\full\service_contexts

# add by illa 
illacommon    u:object_r:illacommon_service:s0

device\mediatek\common\sepolicy\full\system_server.te

# add by illa
allow system_server illacommon_service:service_manager add;

frameworks\base\services\core\java\com\android\server\illaCommonService.java

public class illaCommonService extends IillaCommonService.Stub {//add by illa

illaCommonService() {}




public int get_teekey_status(){
return get_teekey_status_native();
}

    private static native int get_teekey_status_native();

};

add an aidl file

frameworks\base\core\java\android\os\IillaCommonService.aidl

interface IillaCommonService { 
//add by illa
int get_teekey_status();
//add by illa
}

\frameworks\base\services\core\jni\Android.mk

LOCAL_SRC_FILES += \

 $(LOCAL_REL_DIR)/com_android_server_illaCommonService.cpp \


LOCAL_SHARED_LIBRARIES += \ import the lib to be used here


frameworks\base\services\core\jni\com_android_server_illaCommonService.cpp

namespace android

struct tee_device_t* tee_device = NULL;

    static inline int tee_device_open(const hw_module_t* module, struct tee_device_t** device) {  
        return module->methods->open(module, TEE_HARDWARE_MODULE_ID, (struct hw_device_t**)device);  
    }

/**
which
1 sharp ic
2 elan ic
enable
true open the ic function
false close the ic function
Method to open or close the target ic
*/
    static jint enable(JNIEnv* env, jobject clazz, jint which, jboolean enable) { 
        int err = 3;
hw_module_t* module;  
ALOGD("tee JNI: 222222222initializing......");   
if (0 == hw_get_module(TEE_HARDWARE_MODULE_ID, (hw_module_t const**)&module)) {
   ALOGD("tee JNI: tee Stub found.");
   if(tee_device_open(module, &tee_device) == 0) {
       ALOGD("tee JNI: tee device is open.");
       err = tee_device->tee_en(tee_device, which, enable ? 1 : 0);
   }else{
ALOGD("tee JNI: failed to open tee device.");  
}
}else{
ALOGD("tee JNI: failed to get tee stub module.");
}
        return err;  
    }  
   
    static jint get_teekey_status(JNIEnv* env, jobject clazz) { 
        int nret = -1;
//hw_module_t* module;  
ALOGD("tee JNI: 11111111initializing......");   
//if (0 == hw_get_module(TEE_HARDWARE_MODULE_ID, (hw_module_t const**)&module)) {
   ALOGD("tee JNI: tee Stub found.");
   //if(tee_device_open(module, &tee_device) == 0) {
       ALOGD("tee JNI: tee device is open.");
       nret = verify_tee_all();
   //}else{
// ALOGD("tee JNI: failed to open tee device.");  
//}
//}else{
// ALOGD("tee JNI: failed to get tee stub module.");
//}

return nret;
    }    
    static const JNINativeMethod method_table[] = {
        {"enable_native", "(IZ)I", (void*)enable},
        {"get_teekey_status_native", "()I", (void*)get_teekey_status} 
    };    
  
    int register_com_android_server_illaCommonService(JNIEnv *env) {  
            return jniRegisterNativeMethods(env, "com/android/server/illaCommonService", method_table, NELEM(method_table));  
    }  


};



\frameworks\base\services\core\jni\onload.cpp


namespace android {

...

//add by gaoshifeng for tee test
int register_com_android_server_illaCommonService(JNIEnv *env);
//add by gaoshifeng for tee test

}


extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
{

...

//add by gaoshifeng for tee test
register_com_android_server_illaCommonService(env);
//add by gaoshifeng for tee test
    return JNI_VERSION_1_4;
}



frameworks\base\services\java\com\android\server\SystemServer.java

            traceBeginAndSlog("StartVibratorService");
            vibrator = new VibratorService(context);
            ServiceManager.addService("vibrator", vibrator);
            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
//add by gaoshifeng for tee test
Slog.i(TAG, "illacommon Service");
            ServiceManager.addService("illacommon", newillaCommonService());
//add by gaoshifeng for tee test


\frameworks\base\Android.mk


LOCAL_SRC_FILES += \

core/java/android/os/IillaCommonService.aidl \  

Add a reference to aidl here

The java file can directly call the aidl interface

try {
mTee = android.os.IillaCommonService.Stub.asInterface(android.os.ServiceManager.getService("illacommon")).get_teekey_status();
} catch (Exception e) {
e.printStackTrace ();
}




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325795065&siteId=291194637