Chinaunix首页 | 论坛 | 博客
  • 博客访问: 752322
  • 博文数量: 98
  • 博客积分: 4934
  • 博客等级: 上校
  • 技术积分: 1151
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-12 19:55
文章分类

全部博文(98)

文章存档

2014年(1)

2013年(2)

2012年(4)

2011年(25)

2010年(33)

2009年(33)

分类: 嵌入式

2012-09-27 14:53:08

二,硬件抽象层
 
1,在hardware\libhardware\include\hardware下新建swtled.h文件
swtled.h

点击(此处)折叠或打开

  1. #ifndef ANDROID_SWTLED_INTERFACE_H
  2. #define ANDROID_SWTLED_INTERFACE_H

  3. #include <hardware/hardware.h>

  4. __BEGIN_DECLS

  5. //module interface
  6. struct swtled_module_t
  7. {
  8.     struct hw_module_t common;
  9. };

  10. //device interface
  11. struct swtled_device_t
  12. {
  13.     struct hw_device_t common;
  14.     int fd;
  15.     int (*set_on)(struct swtled_device_t* dev,int number);
  16.     int (*set_off)(struct swtled_device_t* dev,int number);
  17. };


  18. #define SWTLED_HARDWARE_MODULE_ID "swtled"
  19. __END_DECLS

  20. #endif

2:在hardware/libhardware/module下新建swtled文件夹并新建swtled.c

点击(此处)折叠或打开

  1. #define LOG_TAG "swtledStub"

  2. #include <hardware/hardware.h>
  3. #include <hardware/swtled.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <cutils/log.h>
  7. #include <cutils/atomic.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/ioctl.h>


  12. #define LED_ON _IO('k',1)
  13. #define LED_OFF _IO('k',2)

  14. #define DEVICE_NAME "/dev/swt_led"
  15. #define MODULE_NAME "swtled"
  16. #define MODULE_AUTHOR "zc"



  17. //open or close device interface

  18. static int swtled_device_open(const struct hw_module_t* module,const char* name, struct hw_device_t** device);

  19. static int swtled_device_close(struct hw_device_t* device);


  20. //light-on light-off
  21. static int swtled_set_on(struct swtled_device_t* dev,int number);
  22. static int swtled_set_off(struct swtled_device_t* dev,int number);


  23. static int swtled_device_open(const struct hw_module_t* module,const char* name, struct hw_device_t** device)
  24. {

  25.     struct swtled_device_t * dev;
  26.     dev = (struct swtled_device_t*)malloc(sizeof(struct swtled_device_t));
  27.     if (!dev)
  28.     {
  29.      LOGE("Swtled Stub:failed to alloc space");
  30.         return -EFAULT;
  31.             
  32.     }
  33.     memset(dev,0,sizeof(struct swtled_device_t));
  34.     dev->common.tag = HARDWARE_DEVICE_TAG;
  35.     dev->common.version = 0;
  36.     dev->common.module = (hw_module_t*)module;
  37.     dev->common.close = swtled_device_close;
  38.     dev->set_on = swtled_set_on;
  39.     dev->set_off = swtled_set_off;

  40.     if ((dev->fd = open(DEVICE_NAME,O_RDWR)) == -1)
  41.     {
  42.         LOGE("Swtled Stub:failed to open /dev/swt_led -- %s.",strerror(errno));
  43.         free(dev);
  44.         return -EFAULT;
  45.     }

  46.     *device = &(dev->common);
  47.     LOGI("Swtled Stub: open /dev/swt_led successfully");

  48.     return 0;
  49. }

  50. static int swtled_device_close(struct hw_device_t* device)
  51. {
  52.     struct swtled_device_t* swtled_device = (struct swtled_device_t*)device;
  53.     if (swtled_device)
  54.     {
  55.         close(swtled_device->fd);
  56.         free(swtled_device);
  57.     }
  58.     return 0;
  59. }


  60. //------------------------------------------------------------------------------
  61. //number: 1: light one 2:light two
  62. //------------------------------------------------------------------------------
  63. static int swtled_set_on(struct swtled_device_t* device,int number)
  64. {
  65.     ioctl(device->fd,LED_ON,number);
  66.     return 0;
  67. }


  68. //------------------------------------------------------------------------------
  69. //number: 1: light one 2:light two
  70. //------------------------------------------------------------------------------
  71. static int swtled_set_off(struct swtled_device_t* device,int number)
  72. {
  73.     ioctl(device->fd,LED_OFF,number);
  74.     return 0;
  75. }



  76. //module method table
  77. static struct hw_module_methods_t swtled_module_methods =
  78. {
  79.     open: swtled_device_open
  80. };



  81. const struct swtled_module_t HAL_MODULE_INFO_SYM = {

  82.     common:
  83.     {
  84.         tag: HARDWARE_MODULE_TAG,
  85.         version_major: 1,
  86.         version_minor: 0,
  87.         id: SWTLED_HARDWARE_MODULE_ID,
  88.         name:MODULE_NAME,
  89.         author: MODULE_AUTHOR,
  90.         methods: &swtled_module_methods,        
  91.     }
  92.         
  93. };
这里主要是通过hw_module_t和hw_device_t结构体形式将内核中led的驱动封装一层,
导出set_on和set_off接口,到时供JNI层的CPP调用。由于权限问题,应用层在打开dev时可能提示permission deny,所以需要修改
system\core\rootdir下的ueventd.rc文件,在其中添加一句

点击(此处)折叠或打开

  1. /dev/swt_led 0666 root root
3,在同目录下添加Android.mk

点击(此处)折叠或打开

  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3. LOCAL_MODULE_TAGS := optional
  4. LOCAL_PRELINK_MODULE := false
  5. LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
  6. LOCAL_SHARED_LIBRARIES := liblog
  7. LOCAL_SRC_FILES := swtled.c
  8. LOCAL_MODULE := swtled.default
  9. 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) |
给主人留下些什么吧!~~