-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add luoshengyang aosp jni kernel source code
- Loading branch information
1 parent
29125db
commit 50b190f
Showing
4 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
...gyang/hello_drivers/aosp/frameworks/base/services/jni/com_android_server_HelloService.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#define LOG_TAG "HelloService" | ||
#include "jni.h" | ||
#include "JNIHelp.h" | ||
#include "android_runtime/AndroidRuntime.h" | ||
#include <utils/misc.h> | ||
#include <utils/Log.h> | ||
#include <hardware/hardware.h> | ||
#include <hardware/hello.h> | ||
#include <stdio.h> | ||
|
||
|
||
namespace android | ||
{ | ||
/*在硬件抽象层中定义的硬件访问结构体,参考<hardware/hello.h>*/ | ||
struct hello_device_t* hello_device = NULL; | ||
/*通过硬件抽象层定义的硬件访问接口设置硬件寄存器val的值*/ | ||
static void hello_setVal(JNIEnv* env, jobject clazz, jint value) { | ||
int val = value; | ||
LOGI("Hello JNI: set value %d to device.", val); | ||
if(!hello_device) { | ||
LOGI("Hello JNI: device is not open."); | ||
return; | ||
} | ||
|
||
hello_device->set_val(hello_device, val); | ||
} | ||
/*通过硬件抽象层定义的硬件访问接口读取硬件寄存器val的值*/ | ||
static jint hello_getVal(JNIEnv* env, jobject clazz) { | ||
int val = 0; | ||
if(!hello_device) { | ||
LOGI("Hello JNI: device is not open."); | ||
return val; | ||
} | ||
|
||
int result = hello_device->get_val(hello_device,val); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
LOGI("Hello JNI: get value %d from device.", result); | ||
|
||
return result; | ||
} | ||
/*通过硬件抽象层定义的硬件模块打开接口打开硬件设备*/ | ||
static inline int hello_device_open(const hw_module_t* module, struct hello_device_t** device) { | ||
return module->methods->open(module, HELLO_HARDWARE_MODULE_ID, (struct hw_device_t**)device); | ||
} | ||
/*通过硬件模块ID来加载指定的硬件抽象层模块并打开硬件*/ | ||
static jboolean hello_init(JNIEnv* env, jclass clazz) { | ||
hello_module_t* module; | ||
|
||
LOGI("Hello JNI: initializing......"); | ||
if(hw_get_module(HELLO_HARDWARE_MODULE_ID, (const struct hw_module_t**)&module) == 0) { | ||
LOGI("Hello JNI: hello Stub found."); | ||
if(hello_device_open(&(module->common), &hello_device) == 0) { | ||
LOGI("Hello JNI: hello device is open."); | ||
return 0; | ||
} | ||
LOGE("Hello JNI: failed to open hello device."); | ||
return -1; | ||
} | ||
LOGE("Hello JNI: failed to get hello stub module."); | ||
return -1; | ||
} | ||
/*JNI方法表*/ | ||
static const JNINativeMethod method_table[] = { | ||
{"init_native", "()Z", (void*)hello_init}, | ||
{"setVal_native", "(I)V", (void*)hello_setVal}, | ||
{"getVal_native", "()I", (void*)hello_getVal}, | ||
}; | ||
/*注册JNI方法*/ | ||
int register_android_server_HelloService(JNIEnv *env) { | ||
return jniRegisterNativeMethods(env, "com/android/server/HelloService", method_table, NELEM(method_table)); | ||
} | ||
}; | ||
|
68 changes: 68 additions & 0 deletions
68
...rce/learn_note_of_luoshengyang/hello_drivers/aosp/frameworks/base/services/jni/onload.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (C) 2009 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "JNIHelp.h" | ||
#include "jni.h" | ||
#include "utils/Log.h" | ||
#include "utils/misc.h" | ||
|
||
namespace android { | ||
int register_android_server_AlarmManagerService(JNIEnv* env); | ||
int register_android_server_BatteryService(JNIEnv* env); | ||
int register_android_server_InputApplicationHandle(JNIEnv* env); | ||
int register_android_server_InputWindowHandle(JNIEnv* env); | ||
int register_android_server_InputManager(JNIEnv* env); | ||
int register_android_server_LightsService(JNIEnv* env); | ||
int register_android_server_PowerManagerService(JNIEnv* env); | ||
int register_android_server_UsbDeviceManager(JNIEnv* env); | ||
int register_android_server_UsbHostManager(JNIEnv* env); | ||
int register_android_server_VibratorService(JNIEnv* env); | ||
int register_android_server_SystemServer(JNIEnv* env); | ||
int register_android_server_location_GpsLocationProvider(JNIEnv* env); | ||
int register_android_server_connectivity_Vpn(JNIEnv* env); | ||
int register_android_server_HelloService(JNIEnv *env); | ||
}; | ||
|
||
using namespace android; | ||
|
||
extern "C" 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; | ||
} | ||
LOG_ASSERT(env, "Could not retrieve the env!"); | ||
|
||
register_android_server_PowerManagerService(env); | ||
register_android_server_InputApplicationHandle(env); | ||
register_android_server_InputWindowHandle(env); | ||
register_android_server_InputManager(env); | ||
register_android_server_LightsService(env); | ||
register_android_server_AlarmManagerService(env); | ||
register_android_server_BatteryService(env); | ||
register_android_server_UsbDeviceManager(env); | ||
register_android_server_UsbHostManager(env); | ||
register_android_server_VibratorService(env); | ||
register_android_server_SystemServer(env); | ||
register_android_server_location_GpsLocationProvider(env); | ||
register_android_server_connectivity_Vpn(env); | ||
register_android_server_HelloService(env); | ||
|
||
return JNI_VERSION_1_4; | ||
} |
9 changes: 9 additions & 0 deletions
9
...gResource/learn_note_of_luoshengyang/hello_drivers/aosp/hardware/modules/hello/Android.mk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
LOCAL_PATH := $(call my-dir) | ||
include $(CLEAR_VARS) | ||
LOCAL_MODULE_TAGS := optional | ||
LOCAL_PRELINK_MODULE := false | ||
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw | ||
LOCAL_SHARED_LIBRARIES := liblog | ||
LOCAL_SRC_FILES := hello.c | ||
LOCAL_MODULE := hello.default | ||
include $(BUILD_SHARED_LIBRARY) |
101 changes: 101 additions & 0 deletions
101
...ningResource/learn_note_of_luoshengyang/hello_drivers/aosp/hardware/modules/hello/hello.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#define LOG_TAG "HelloStub" | ||
|
||
#include <hardware/hardware.h> | ||
#include <hardware/hello.h> | ||
#include <fcntl.h> | ||
#include <errno.h> | ||
#include <cutils/log.h> | ||
#include <cutils/atomic.h> | ||
|
||
#define DEVICE_NAME "/dev/hello" | ||
#define MODULE_NAME "Hello" | ||
#define MODULE_AUTHOR "shyluo@gmail.com" | ||
|
||
/*设备打开和关闭接口*/ | ||
static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device); | ||
static int hello_device_close(struct hw_device_t* device); | ||
|
||
/*设备访问接口*/ | ||
static int hello_set_val(struct hello_device_t* dev, int val); | ||
static int hello_get_val(struct hello_device_t* dev, int val); | ||
|
||
/*模块方法表*/ | ||
static struct hw_module_methods_t hello_module_methods = { | ||
open: hello_device_open | ||
}; | ||
|
||
/*模块实例变量*/ | ||
struct hello_module_t HAL_MODULE_INFO_SYM = { | ||
common: { | ||
tag: HARDWARE_MODULE_TAG, | ||
version_major: 1, | ||
version_minor: 0, | ||
id: HELLO_HARDWARE_MODULE_ID, | ||
name: MODULE_NAME, | ||
author: MODULE_AUTHOR, | ||
methods: &hello_module_methods, | ||
} | ||
}; | ||
|
||
static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) { | ||
struct hello_device_t* dev;dev = (struct hello_device_t*)malloc(sizeof(struct hello_device_t)); | ||
|
||
if(!dev) { | ||
LOGE("Hello Stub: failed to alloc space"); | ||
return -EFAULT; | ||
} | ||
|
||
memset(dev, 0, sizeof(struct hello_device_t)); | ||
dev->common.tag = HARDWARE_DEVICE_TAG; | ||
dev->common.version = 0; | ||
dev->common.module = (hw_module_t*)module; | ||
dev->common.close = hello_device_close; | ||
dev->set_val = hello_set_val;dev->get_val = hello_get_val; | ||
|
||
if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) { | ||
LOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev); | ||
return -EFAULT; | ||
} | ||
|
||
*device = &(dev->common); | ||
LOGI("Hello Stub: open /dev/hello successfully."); | ||
|
||
return 0; | ||
} | ||
|
||
static int hello_device_close(struct hw_device_t* device) { | ||
struct hello_device_t* hello_device = (struct hello_device_t*)device; | ||
|
||
if(hello_device) { | ||
close(hello_device->fd); | ||
free(hello_device); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static int hello_set_val(struct hello_device_t* dev, int val) { | ||
LOGI("Hello Stub: set value %d to device.", val); | ||
|
||
write(dev->fd, &val, sizeof(val)); | ||
|
||
return 0; | ||
} | ||
|
||
static int hello_get_val(struct hello_device_t* dev, int val) { | ||
//if(!val) { | ||
// LOGE("Hello Stub: error val pointer"); | ||
// return -EFAULT; | ||
//} | ||
|
||
int result = 0; | ||
|
||
|
||
int size = read(dev->fd, &result, sizeof(val)); | ||
|
||
LOGI("Hello Stub: get value %d from device size is %d", result,size); | ||
|
||
return result; | ||
} | ||
|
||
|
和罗老师原先代码不一样的地方:
还有hello.c的读取方法