Android Sensor 适配层的书写--主要是对函数的介绍


Sensor 适配层的书写-----大家多讨论,前文说了,适配层的基本的接口。现在将接口中的内容扩展说下,其实在sensors.h文件中已经说的很明确了,这里只不过是怕自己有遗忘翻译了一下。 

这些函数的调用顺序是如何的呢?这就要看上层JAVA的调用了,反正JNI也指示函数的重定义。(当然这也只是我自己的理解) 其实到了这里大家也都清楚这些函数的作用,整个hal层的代码也只是这些函数的实现。其中这里hal和java的通信的话不得不考虑其中有一个native_handle这样的机制,下面再看看这个再说。

  1. /** 
  2.  * Every device data structure must begin with hw_device_t 
  3.  * followed by module specific public methods and attributes. 
  4.  */  
  5. struct sensors_control_device_t {  
  6.     struct hw_device_t common;  
  7.       
  8.     /** 
  9.      *返回一个native_handle_t的指针 
  10.      * 
  11.      * The caller takes ownership of this handle. This is intended to be 
  12.      * passed cross processes. 
  13.      * 
  14.      *成功返回native_handle_t,失败返回NULL 
  15.      */  
  16.     native_handle_t* (*open_data_source)(struct sensors_control_device_t *dev);  
  17.   
  18.     /** 
  19.      * 释放所有在open_data_source中申请的资源 
  20.      * 这个调用的函数是可选的,可以设置为NULL 
  21.      * 成功返回0,失败小于0 
  22.      * 
  23.      * @return 0 if successful, < 0 on error 
  24.      */  
  25.     int (*close_data_source)(struct sensors_control_device_t *dev);  
  26.   
  27.     /** 使一个sensor有效或者无效 
  28.      * 
  29.      * 参数: 
  30.      *     handle: 需要变化的sensor的handle 
  31.      *     enable: 1,使能;0,无效 
  32.      * 返回值: 
  33.      *     成功为0,失败为1 
  34.      */  
  35.     int (*activate)(struct sensors_control_device_t *dev,   
  36.             int handle, int enabled);  
  37.       
  38.     /** 
  39.      * 设置sensor event的间隔时间,单位ms 
  40.      * 
  41.      * 成功为0,失败小于0 
  42.      */  
  43.     int (*set_delay)(struct sensors_control_device_t *dev, int32_t ms);  
  44.   
  45.     /** 
  46.      * 促使sensors_data_device_t.poll()立即返回-EWOULDBLOCK 
  47.      */  
  48.     int (*wake)(struct sensors_control_device_t *dev);  
  49. };  
  50.   
  51. struct sensors_data_device_t {  
  52.     struct hw_device_t common;  
  53.   
  54.     /** 
  55.      * 准备读取sensor的数据 
  56.      * 
  57.      * This routine does NOT take ownership of the handle 
  58.      * and must not close it. Typically this routine would 
  59.      * use a duplicate of the nh parameter. 
  60.      *这个函数不能改变handle的权限,也不能关闭他。标准的做法,只是简单的将nh复制给dev使用; 
  61.      * 参数nh:来自于sensors_control_open 
  62.      * 返回值:0表示成功,小于0表示失败; 
  63.      */  
  64.     int (*data_open)(struct sensors_data_device_t *dev, native_handle_t* nh);  
  65.       
  66.     /** 
  67.      * 
  68.      * 调用者必须完成了sensor的数据读取才可以调用此函数。因此可以知道,此函数��将设备节点给关闭掉; 
  69.      * 调用此函数不会阻塞poll函数。 
  70.      * 成功返回0,失败小于0 
  71.      * / 
  72.     int (*data_close)(struct sensors_data_device_t *dev); 
  73.      
  74.     /** 
  75.      *  
  76.      * sensors_data_t指针返回指定的sensor的数据 
  77.      * 返回值:sensor的handle 
  78.      * poll的实现这里也说的很清楚,是返回指定的sensor的数据,也就是说这个poll不能一次把所有的sensor的数据都拿到 
  79.      */  
  80.     int (*poll)(struct sensors_data_device_t *dev,   
  81.             sensors_data_t* data);  
  82. };  

相关内容