1 Source Files 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 is 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 is 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;
}
Defining JNI Function Mappings
static const Jninativemethod methods[] =
{
{"_init", "() Z", (void *) Led_init},
{"_set_on", "(I) Z", (void *) Led_seton},
{"_set_off", "(I) Z", (void *) Led_setoff},};
Binding a JNI program to the Java class Library
int Register_led_hal_jni (jnienv* env)
{
static const char* Const KCLASSNAME ="Mobile/android/leds/hal/service/ledhalservice";//the current JNI library must be called by this class.
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;
}
Successfully called after the system is successfully mountedjni_onload function for JNI module initialization
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 File
# 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/Jean 岄 Bulb/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 Building links and Recompiling
First establish a link with Android source code ln-s ..../leds_hal_jni ... Android_sc/leds_hal_jni
Then compile mm to generate led_hal_jni.so,
upload led_hal_jni.so to the/system/lib directory of the Development Board
The JNI layer of Android underlying development (ii)