. 1 #ifndef LED_HAL_H__ 2 #define LED_HAL_H__ . 3 . 4 #include <Hardware / hardware.h> . 5 . 6 // name HAL layer defines the dynamic libraries . 7 #define LED_MODULE_ID "s5pv210_led_hal" . 8 . 9 // need to re-definition of the structure: module and device 10 . 11 // define the structure module 12 is struct led_hw_module_t { 13 is // inherited from the parent class 14 struct hw_module_t Common; // first member must be the parent class name must be: Common 15 16 // the following is their extended members - ---- custom 17 int serial_id; 18 is int led_id; . 19 }; 20 is 21 is // define the device structure 22 is struct led_hw_device_t { 23 is // inherited from the parent class 24 struct hw_device_t Common; // first member must be the parent class name must be: Common 25 26 is // the following extension members for their custom ----- 27 int (* open_dev) (); 28 int (* contrl_dev) ( int ON); 29 }; 30 31 is #endif
1 #define LOG_TAG "led_jni_log" 2 #include <utils/Log.h> 3 #include "jni.h" 4 #include "led_hal.h" 5 6 struct led_hw_module_t *pmodule = NULL; 7 struct led_hw_device_t *pdevice = NULL; 8 9 //3,实现接口函数 10 static jint open_led(JNIEnv *env, jobject thiz) 11 { 12 jint ret; 13 LOGI("-----------------^_^ %s---------------\n" , __ FUNCTION__); 14 // . 1, loading a dynamic library hal layer ------ acquisition module objects hal layer 15 // Name Parameter 1 ---- HAL layer dynamic library 16 // parameter 2 ---- object pointer points to the address module 17 // return value success ----- --- 0, --- failed negative 18 is RET = hw_get_module (LED_MODULE_ID, ( const struct hw_module_t **) & pmodule ); 19 IF (RET == 0 ) { // call succeeds --- get to the target module 20 LOGI ( " ----------------- hw_get_module the OK ----- ---------- \ n- " ); 21 is 22 is // 2, calling the open function module object acquisition device objects 23 is IF (pmodule! = NULL) { 24 // address parameters 1 --- hal object module layer 25 // Parameter 2 --- identifies the device, typically NULL 26 is // parameter pointing device objects 3 --- the address pointer 27 pmodule-> common.methods-> open (& pmodule-> Common, NULL, ( struct hw_device_t **) & pDevice); 28 29 30 // . 3, method calls the open target device, open the device: open_dev 31 is IF (pDevice =! NULL) 32 pdevice-> open_dev (); 33 is 34 is } 35 36 } the else { 37 LOGI("-----------------hw_get_module error---------------\n"); 38 return -1; 39 } 40 41 42 return 0; 43 } 44 45 static jint led_on(JNIEnv *env, jobject thiz) 46 { 47 48 LOGI("-----------------^_^ %s---------------\n",__FUNCTION__); 49 if(pdevice != NULL) 50 pdevice->contrl_dev(1); 51 return 0; 52 } 53 static jint led_off(JNIEnv *env, jobject thiz) 54 { 55 jint ret; 56 LOGI("-----------------^_^ %s---------------\n",__FUNCTION__); 57 if(pdevice != NULL) 58 pdevice->contrl_dev(0); 59 60 return 0; 61 } 62 63 static jint close_led(JNIEnv *env, jobject thiz) 64 { 65 LOGI("-----------------^_^ %s---------------\n",__FUNCTION__); 66 if(pdevice != NULL) 67 pdevice->common.close((struct hw_device_t*)pdevice); 68 return 0; 69 } 70 71 72 //2,创建映射表 73 const JNINativeMethod led_jni_methods[] = { 74 {"openDev", "()I", (void*)open_led}, 75 {"devOn", "()I", (void*)led_on}, 76 {"devOff", "()I", (void*)led_off}, 77 {"closeDev", "()I", (void*)close_led}, 78 }; 79 80 //1,实现JNI_Onload函数 81 jint JNI_OnLoad(JavaVM* vm, void* reserved) 82 { 83 84 the JNIEnv the env * = NULL; 85 86 LOGI ( " ----------------- ^ _ ^ S -------------% - \ n- " , __ FUNCTION__); 87 // . 1, environment variables objects ----- environment variable object encapsulates many functions required for programming jni 88 // parameter 1 - the environment variable address pointer 89 // parameter 2 - jni version 90 // return value: --0 success, failure - negative 91 is IF (vm-> GetEnv (( void ! **) & the env, JNI_VERSION_1_4) = JNI_OK) { 92 the LOGE ( " ERROR: vm-> GetEnv failed " ); 93 return -1 ; 94 } 95 96 // Class 2, obtaining the application layer method of local declaration app where 97 // path parameter ---- class of app 98 JClass env-CLZ => the FindClass ( " COM / Peter / lowlevel / LedNative " ); 99 IF (CLZ == NULL) { 100 the LOGE ( " ERROR: vm-> the FindClass failed " ); 101 return - . 1 ; 102 } 103 104 // . 3, and creates a mapping table to map register DVM in 105 // parameter 1 ----- java class declarations where the native methods 106 //Parameter 2 ----- address mapping table 107 // number of parameter 2 ----- method of mapping table 108 // Return Value: --0 success, failure - Negative 109 IF (env-> RegisterNatives (CLZ, led_jni_methods, the sizeof (led_jni_methods) / the sizeof (led_jni_methods [ 0 ])) < 0 ) { 110 the LOGE ( " ERROR: RegisterNatives failed " ); 111 return - . 1 ; 112 } 113 114 return JNI_VERSION_1_4; 115 1 16 }
1 LOCAL_PATH:= $(call my-dir) 2 include $(CLEAR_VARS) 3 4 LOCAL_MODULE_TAGS := optional 5 6 LOCAL_MODULE:= libled_jni 7 8 LOCAL_SRC_FILES:= led_jni.cpp 9 10 11 LOCAL_SHARED_LIBRARIES := \ 12 libutils \ 13 libhardware 14 15 LOCAL_C_INCLUDES += $(JNI_H_INCLUDE) //指定头文件:jni.h 的路径 16 17 include $(BUILD_SHARED_LIBRARY)