Android底層開發(二)之JNI層

來源:互聯網
上載者:User

標籤:

1 源檔案LedHalService.cpp

#include <stdlib.h>

#include <string.h>

#include <unistd.h>
#include <assert.h>
#include <jni.h>
#include <leds_hal.h>


struct led_control_device_t *led_hal_device = NULL;


static jboolean led_setOn(JNIEnv* env, jobject thiz, jint led)
{
LOGI("Led HAL JNI: led_setOn() is invoked.");


if (led_hal_device == NULL)
{
LOGI("Led HAL JNI: led_hal_device was not fetched correctly.");
return -1;
}
else
{
return led_hal_device->set_on(led_hal_device, led);
}
}


static jboolean led_setOff(JNIEnv* env, jobject thiz, jint led)
{
LOGI("Led HAL JNI: led_setOff() is invoked.");
if (led_hal_device == NULL)
{
LOGI("Led HAL JNI: led_hal_device was not fetched correctly.");
return -1;
}
else
{
return led_hal_device->set_off(led_hal_device, led);
}
}


static inline int led_control_open(const struct hw_module_t* module,
        struct led_control_device_t** device)
{
return module->methods->open(module, LED_HARDWARE_MODULE_ID,
       (struct hw_device_t**) device);
}


static jboolean led_init(JNIEnv *env, jclass clazz)
{
led_module_t* module;


LOGE("**********start find hal *********");
LOGE(LED_HARDWARE_MODULE_ID);


if (hw_get_module(LED_HARDWARE_MODULE_ID, (const hw_module_t**) &module)
       == 0)
{
LOGI("LedService JNI: LED Stub found.");
if (led_control_open(&module->hw_module, &led_hal_device) == 0)
{
LOGI("LedService JNI: Got Stub operations.");
return 0;
}
}


LOGE("LedService JNI: Get Stub operations failed.");
return -1;
}

//定義JNI函數映射
static const JNINativeMethod methods[] =
{
{ "_init", "()Z", (void *) led_init },
{ "_set_on", "(I)Z", (void *) led_setOn },
{ "_set_off", "(I)Z", (void *) led_setOff }, };

//將JNI程式與java類庫綁定
int register_led_hal_jni(JNIEnv* env)
{
static const char* const kClassName = "mobile/android/leds/hal/service/LedHalService";// 必須由該類調用當前的JNI程式庫.


jclass clazz;


clazz = env->FindClass(kClassName);
if (clazz == NULL)
{
LOGE("Can‘t find class %s\n", kClassName);
return -1;
}
if (env->RegisterNatives(clazz, methods,
       sizeof(methods) / sizeof(methods[0])) != JNI_OK)
{
LOGE("Failed registering methods for %s\n", kClassName);
return -1;
}


return 0;
}

//系統成功裝載後會成功調用JNI_OnLoad函數   用於JNI模組初始化
jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
jint result = -1;


if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
{
LOGE("GetEnv failed!");
return result;
}


register_led_hal_jni(env);


return JNI_VERSION_1_4;
}



2  Android.mk檔案

# Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng
LOCAL_MODULE:= led_hal_jni


# LOCAL_MODULE_PATH := /root/drivers/s3c6410_leds_hal/leds_hal_jni
LOCAL_MODULE_PATH := /home/litingting/妗岄潰/led_hal_jni
LOCAL_SRC_FILES:= LedHalService.cpp
   


LOCAL_SHARED_LIBRARIES := \
libandroid_runtime \
libcutils \
libhardware \
libhardware_legacy \
libnativehelper \
        libsystem_server \
libutils \
libui \
        libsurfaceflinger_client
       
  
LOCAL_C_INCLUDES += \
$(JNI_H_INCLUDE) \
        hardware/leds_hal


LOCAL_PRELINK_MODULE := false


include $(BUILD_SHARED_LIBRARY)


3 建立連結再編譯

先與安卓源碼建立連結   ln -s     ..../leds_hal_jni   ......Android_SC/leds_hal_jni  

再進行編譯  mm  產生led_hal_jni.so  、

將led_hal_jni.so 傳到開發板的/system/lib目錄





Android底層開發(二)之JNI層

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.