新增LED设备--从上层到底层理解安卓架构之JNI篇
扫描二维码
随时随地手机看文章
JNI的实现
1.frameworks/base/services/core/jni/com_android_server_TestLedService.cpp
#define LOG_TAG "TestLedServiceJni"
#include "jni.h"
#include "JNIHelp.h"
#include "android_runtime/AndroidRuntime.h"
#include
#include
#include
#include
#include
namespace android
{
struct testled_device_t* testled_device = NULL;
static void testled_setLed1(JNIEnv* env, jobject clazz, jint value) {
int val = value;
ALOGI("TestLed JNI: set value %d to led1.", val);
if(!testled_device) {
ALOGI("TestLed JNI: device is not open.");
return;
}
if(val > 0){
testled_device->control(0);//on
}else{
testled_device->control(1);//off
}
}
static void testled_setLed2(JNIEnv* env, jobject clazz, jint value)
{
int val = value;
ALOGI("TestLed JNI: set value %d to led2.", val);
if(!testled_device) {
ALOGI("TestLed JNI: device is not open.");
return;
}
if(val > 0){
testled_device->control(2);//on
}else{
testled_device->control(3);//off
}
}
static inline int testled_device_open(const hw_module_t* module, struct testled_device_t** device) {
return module->methods->open(module, LED_HAL_MODULE_ID, (struct hw_device_t**)device);
}
static jboolean testled_init(JNIEnv* env, jclass clazz) {
testled_module_t* module;
ALOGI("TestLed JNI: initializing......");
if(hw_get_module(LED_HAL_MODULE_ID, (const struct hw_module_t**)&module) == 0) {
ALOGI("TestLed JNI: testled Stub found.");
if(testled_device_open(&(module->common), &testled_device) == 0) {
ALOGI("TestLed JNI: testled device is open.");
//testled_device->open();
return 0;
}
ALOGE("TestLed JNI: failed to open testled device.");
return -1;
}
ALOGE("TestLed JNI: failed to get testled stub module.");
return -1;
}
static const JNINativeMethod method_table[] = {
{"testledinit_native", "()Z", (void*)testled_init},
{"setled1_native", "(I)V", (void*)testled_setLed1},
{"setled2_native", "(I)V", (void*)testled_setLed2},
};
int register_android_server_TestLedService(JNIEnv *env) {
return jniRegisterNativeMethods(env, "com/android/server/TestLedService", method_table, NELEM(method_table));
}
};
2.frameworks/base/services/core/jni/onload.cpp
namespace中增加函数声明
namespace android {
……
int register_android_server_TestLedService(JNIEnv *env);
};
JNI_OnLoad增加register_android_server_TestLedService的函数调用
extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
{
…
register_com_android_server_rkdisplay_RkDisplayModes(env);
register_android_server_TestLedService(env);
return JNI_VERSION_1_4;
}
LOCAL_SRC_FILES += \
……
$(LOCAL_REL_DIR)/com_android_server_TestLedService.cpp \
$(LOCAL_REL_DIR)/onload.cpp
扫码关注我们
看更多嵌入式案例
喜欢本篇内容请给我们点个再看
免责声明:本文内容由21ic获得授权后发布,版权归原作者所有,本平台仅提供信息存储服务。文章仅代表作者个人观点,不代表本平台立场,如有问题,请联系我们,谢谢!