二,硬件抽象层
1,在hardware\libhardware\include\hardware下新建swtled.h文件
swtled.h
- #ifndef ANDROID_SWTLED_INTERFACE_H
- #define ANDROID_SWTLED_INTERFACE_H
- #include <hardware/hardware.h>
- __BEGIN_DECLS
- //module interface
- struct swtled_module_t
- {
- struct hw_module_t common;
- };
- //device interface
- struct swtled_device_t
- {
- struct hw_device_t common;
- int fd;
- int (*set_on)(struct swtled_device_t* dev,int number);
- int (*set_off)(struct swtled_device_t* dev,int number);
- };
- #define SWTLED_HARDWARE_MODULE_ID "swtled"
- __END_DECLS
- #endif
2:在hardware/libhardware/module下新建swtled文件夹并新建swtled.c
- #define LOG_TAG "swtledStub"
- #include <hardware/hardware.h>
- #include <hardware/swtled.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <cutils/log.h>
- #include <cutils/atomic.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/ioctl.h>
- #define LED_ON _IO('k',1)
- #define LED_OFF _IO('k',2)
- #define DEVICE_NAME "/dev/swt_led"
- #define MODULE_NAME "swtled"
- #define MODULE_AUTHOR "zc"
- //open or close device interface
- static int swtled_device_open(const struct hw_module_t* module,const char* name, struct hw_device_t** device);
- static int swtled_device_close(struct hw_device_t* device);
- //light-on light-off
- static int swtled_set_on(struct swtled_device_t* dev,int number);
- static int swtled_set_off(struct swtled_device_t* dev,int number);
- static int swtled_device_open(const struct hw_module_t* module,const char* name, struct hw_device_t** device)
- {
- struct swtled_device_t * dev;
- dev = (struct swtled_device_t*)malloc(sizeof(struct swtled_device_t));
- if (!dev)
- {
- LOGE("Swtled Stub:failed to alloc space");
- return -EFAULT;
-
- }
- memset(dev,0,sizeof(struct swtled_device_t));
- dev->common.tag = HARDWARE_DEVICE_TAG;
- dev->common.version = 0;
- dev->common.module = (hw_module_t*)module;
- dev->common.close = swtled_device_close;
- dev->set_on = swtled_set_on;
- dev->set_off = swtled_set_off;
- if ((dev->fd = open(DEVICE_NAME,O_RDWR)) == -1)
- {
- LOGE("Swtled Stub:failed to open /dev/swt_led -- %s.",strerror(errno));
- free(dev);
- return -EFAULT;
- }
- *device = &(dev->common);
- LOGI("Swtled Stub: open /dev/swt_led successfully");
- return 0;
- }
- static int swtled_device_close(struct hw_device_t* device)
- {
- struct swtled_device_t* swtled_device = (struct swtled_device_t*)device;
- if (swtled_device)
- {
- close(swtled_device->fd);
- free(swtled_device);
- }
- return 0;
- }
- //------------------------------------------------------------------------------
- //number: 1: light one 2:light two
- //------------------------------------------------------------------------------
- static int swtled_set_on(struct swtled_device_t* device,int number)
- {
- ioctl(device->fd,LED_ON,number);
- return 0;
- }
- //------------------------------------------------------------------------------
- //number: 1: light one 2:light two
- //------------------------------------------------------------------------------
- static int swtled_set_off(struct swtled_device_t* device,int number)
- {
- ioctl(device->fd,LED_OFF,number);
- return 0;
- }
- //module method table
- static struct hw_module_methods_t swtled_module_methods =
- {
- open: swtled_device_open
- };
- const struct swtled_module_t HAL_MODULE_INFO_SYM = {
- common:
- {
- tag: HARDWARE_MODULE_TAG,
- version_major: 1,
- version_minor: 0,
- id: SWTLED_HARDWARE_MODULE_ID,
- name:MODULE_NAME,
- author: MODULE_AUTHOR,
- methods: &swtled_module_methods,
- }
-
- };
这里主要是通过hw_module_t和hw_device_t结构体形式将内核中led的驱动封装一层,
导出set_on和set_off接口,到时供JNI层的CPP调用。由于权限问题,应用层在打开dev时可能提示permission deny,所以需要修改
system\core\rootdir下的ueventd.rc文件,在其中添加一句
- /dev/swt_led 0666 root root
3,在同目录下添加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 := swtled.c
- LOCAL_MODULE := swtled.default
- include $(BUILD_SHARED_LIBRARY)
4,编译
(1 进入到gingerbread目录下,执行mmm hardware/libhardware/modules/swtled会
编译成功后,就可以在out/target/product/generic/system/lib/hw目录下看到swtled.default.so文件了
(2 再执行make snod,使system.img镜像文件中包名硬件抽象模块swtled.default.so
阅读(2632) | 评论(0) | 转发(0) |