感觉要理解android系统的架构,最好的办法是调通一个最简单程序的工作流程
android 之hello world理解
1.java app层
private IHelloService Led_demo_service = null;
Led_demo_service = IHelloService.Stub.asInterface(ServiceManager.getService("hello"));
Led_demo_service.setVal(1);
2.中间层
\a33-dvk3\frameworks\base\core\java\android\os\IHelloService.aidl
package android.os;
/** {@hide} */
interface IHelloService {
//dfdfd
void setVal(int val);
int getVal();
}
3.java服务层
\a33-dvk3\frameworks\base\services\java\com\android\server
HelloService() {
init_native();
}
public void setVal(int val) {
setVal_native(val);
}
public int getVal() {
return getVal_native();
}
private static native boolean init_native();
private static native void setVal_native(int val);
private static native int getVal_native();
4.JNI层
连接java与c层
\a33-dvk3\frameworks\base\services\jni\onload.cpp
register_android_server_HelloService(env);
\a33-dvk3\frameworks\base\services\jni\com_android_server_HelloService.cpp
/*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},
};
int register_android_server_HelloService(JNIEnv *env) {
return jniRegisterNativeMethods(env, "com/android/server/HelloService", method_table, NELEM(method_table));
}
static void hello_setVal(JNIEnv* env, jobject clazz, jint value) {
int val = value;
ALOGE("Hello JNI: set value %d to device.", val);
if(!hello_device) {
ALOGE("Hello JNI: device is not open.");
return;
}
hello_device->set_val(hello_device, val);
5.hal层
各个厂商可封装的层
\a33-dvk3\hardware\libhardware\modules\hello\hello.c
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) {
ALOGE("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) {
ALOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev);
return -EFAULT;
}
*device = &(dev->common);
ALOGE("Hello Stub: open /dev/hello successfully.");
return 0;
}
static int hello_get_val(struct hello_device_t* dev, int* val) {
if(!val) {
ALOGE("Hello Stub: error val pointer");
return -EFAULT;
}
read(dev->fd, val, sizeof(*val));
ALOGE("Hello Stub: get value %d from device", *val);
return 0;
}
if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {
ALOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev);
return -EFAULT;
}
#define DEVICE_NAME "/dev/hello"
#define MODULE_NAME "Hello"
6.linux内核态
上面5层均为用户态
-->"/dev/hello"
linux字符设备驱动程序......