第八期 基于模拟器的Helloworld HAL接口 《手机就是开发板》
2016-12-11 07:18
441 查看
Android 下的驱动总能和HAL放在一起被提起,估计当年Google就是因为HAL被Linux开源社区踢出去的。大家知道 驱动程序可以分成两个部分:硬件控制和业务逻。因为驱动要编译进内核,即使编译成模块也受GPL制约,所以Googl将android系统的驱动程序分成两层,硬件控制层放到内核驱动里,业务逻辑部分放到HAL里,可以不用开源,保护了部分芯片商的利益,估计这事是高通和Google一起想出来的,因为他是最大的受益者。
天天说HAL云里雾里的,这一期我们动手实践一个HAL。同样,源码在https://github.com/aggresss/PHDemo
的Code目录下的 hello_HAL 文件下,或者访问链接https://github.com/aggresss/PHDemo/tree/master/Code/hello_HAL
需要进入 ./hardware/libhardware/include/hardware 目录,新建 hello.h 文件
然后 进入到 hardware/libhardware/modules 目录,新建hello目录,并添加hello.c文件
继续在hello目录下新建Android.mk文件:
回到 hardware/libhardware/modules 目录 编辑目录下 Android.mk 文件,在 hardware_modules 变量中添加 hello 变量,这样hello模块就会自动编译
为了保证dev/hello设备正常访问,我们这里修改一下/dev/hello的权限,从AOSP根目录开始,进入到system/core/rootdir目录,里面有一个名为ueventd.rc文件,往里面添加一行:
下面开始编译:
编译成功后,就可以在out/target/product/generic/system/lib/hw目录下看到hello.default.so文件了。
最后,我们运行一下虚拟机,看看HAL是否存在。
执行 make snod 重新生成镜像。
如上图,红色方框中的hello.default.so就是我们编译生成的HAL,通过这个实验我们可以看到,Android中的HAL实质上是以 /system/lib/hw 目录中 共享链接库 的形式存在。下一期我们通过JNI方法来调用这个共享库。
天天说HAL云里雾里的,这一期我们动手实践一个HAL。同样,源码在https://github.com/aggresss/PHDemo
的Code目录下的 hello_HAL 文件下,或者访问链接https://github.com/aggresss/PHDemo/tree/master/Code/hello_HAL
需要进入 ./hardware/libhardware/include/hardware 目录,新建 hello.h 文件
#ifndef ANDROID_HELLO_INTERFACE_H #define ANDROID_HELLO_INTERFACE_H #include <hardware/hardware.h> __BEGIN_DECLS /*定义模块ID*/ #define HELLO_HARDWARE_MODULE_ID "hello" /*硬件模块结构体*/ struct hello_module_t { struct hw_module_t common; }; /*硬件接口结构体*/ struct hello_device_t { struct hw_device_t common; int fd; int (*set_val)(struct hello_device_t* dev, int val); int (*get_val)(struct hello_device_t* dev, int* val); }; __END_DECLS #endif
然后 进入到 hardware/libhardware/modules 目录,新建hello目录,并添加hello.c文件
#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) { 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); ALOGI("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) { ALOGI("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) { ALOGE("Hello Stub: error val pointer"); return -EFAULT; } read(dev->fd, val, sizeof(*val)); ALOGI("Hello Stub: get value %d from device", *val); return 0; }
继续在hello目录下新建Android.mk文件:
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)
回到 hardware/libhardware/modules 目录 编辑目录下 Android.mk 文件,在 hardware_modules 变量中添加 hello 变量,这样hello模块就会自动编译
为了保证dev/hello设备正常访问,我们这里修改一下/dev/hello的权限,从AOSP根目录开始,进入到system/core/rootdir目录,里面有一个名为ueventd.rc文件,往里面添加一行:
/dev/hello 0666 root root
下面开始编译:
mmm hardware/libhardware/modules/hello
编译成功后,就可以在out/target/product/generic/system/lib/hw目录下看到hello.default.so文件了。
最后,我们运行一下虚拟机,看看HAL是否存在。
执行 make snod 重新生成镜像。
emulator -kernel ./kernel3.4/arch/arm/boot/zImage & adb shell cd /system/lib/hw ls
如上图,红色方框中的hello.default.so就是我们编译生成的HAL,通过这个实验我们可以看到,Android中的HAL实质上是以 /system/lib/hw 目录中 共享链接库 的形式存在。下一期我们通过JNI方法来调用这个共享库。
相关文章推荐
- 第十期 基于模拟器的Helloworld Framework接口 《手机就是开发板》
- 第九期 基于模拟器的Helloworld JNI方法 《手机就是开发板》
- 第五期 基于模拟器上的实践和学习规划 《手机就是开发板》
- 第七期 基于模拟器的Helloworld 可执行程序 《手机就是开发板》
- 第六期 基于模拟器的Helloworld 内核驱动 《手机就是开发板》
- 第十一期 基于模拟器的Helloworld APP 访问硬件服务 《手机就是开发板》
- 第二十四期 OpenWrt ipk helloworld 《路由器就是开发板》
- 第八期 RT3052F芯片分析 《路由器就是开发板》
- 实现一个基于串口的手机AT信令模拟器
- Windows mobile 开发入门—第一个程序"hello world",链接调试,模拟器的使用
- 基于百度接口的手机无广告浏览器设计
- Android照相功能驱动层中HAL的实现(基于OK6410开发板+OV9650摄像头)
- Java系列--第二篇 基于Maven的Android开发HelloAndroidWorld
- 实现一个基于串口的手机AT信令模拟器
- 基于Android Studio 的NDK之 “Hello,World!”
- SAP接口编程-RFC系列01 : RFC Hello World
- 全民付手机接口开发生产环境error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
- 实现一个基于串口的手机AT信令模拟器
- Android照相功能驱动层中HAL的实现(基于OK6410开发板+OV9650摄像头)
- Android照相功能驱动层中HAL的实现(基于OK6410开发板+OV9650摄像头)