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

来源:互联网 发布:证件照片排版软件 编辑:程序博客网 时间:2024/06/05 05:01
[cpp] view plaincopy
  1. Sensor 适配层的书写-----大家多讨论  
  2.   
  3. 前文说了,适配层的基本的接口。现在将接口中的内容扩展说下,其实在sensors.h文件中已经说的很明确了,这里只不过是怕自己有遗忘翻译了一下。  
  4. /** 
  5.  * Every device data structure must begin with hw_device_t 
  6.  * followed by module specific public methods and attributes. 
  7.  */  
  8. struct sensors_control_device_t {  
  9.     struct hw_device_t common;  
  10.       
  11.     /** 
  12.      *返回一个native_handle_t的指针 
  13.      * 
  14.      * The caller takes ownership of this handle. This is intended to be 
  15.      * passed cross processes. 
  16.      * 
  17.      *成功返回native_handle_t,失败返回NULL 
  18.      */  
  19.     native_handle_t* (*open_data_source)(struct sensors_control_device_t *dev);  
  20.   
  21.     /** 
  22.      * 释放所有在open_data_source中申请的资源 
  23.      * 这个调用的函数是可选的,可以设置为NULL 
  24.      * 成功返回0,失败小于0 
  25.      * 
  26.      * @return 0 if successful, < 0 on error 
  27.      */  
  28.     int (*close_data_source)(struct sensors_control_device_t *dev);  
  29.   
  30.     /** 使一个sensor有效或者无效 
  31.      * 
  32.      * 参数: 
  33.      *     handle: 需要变化的sensor的handle 
  34.      *     enable: 1,使能;0,无效 
  35.      * 返回值: 
  36.      *     成功为0,失败为1 
  37.      */  
  38.     int (*activate)(struct sensors_control_device_t *dev,   
  39.             int handle, int enabled);  
  40.       
  41.     /** 
  42.      * 设置sensor event的间隔时间,单位ms 
  43.      * 
  44.      * 成功为0,失败小于0 
  45.      */  
  46.     int (*set_delay)(struct sensors_control_device_t *dev, int32_t ms);  
  47.   
  48.     /** 
  49.      * 促使sensors_data_device_t.poll()立即返回-EWOULDBLOCK 
  50.      */  
  51.     int (*wake)(struct sensors_control_device_t *dev);  
  52. };  
  53.   
  54. struct sensors_data_device_t {  
  55.     struct hw_device_t common;  
  56.   
  57.     /** 
  58.      * 准备读取sensor的数据 
  59.      * 
  60.      * This routine does NOT take ownership of the handle 
  61.      * and must not close it. Typically this routine would 
  62.      * use a duplicate of the nh parameter. 
  63.      *这个函数不能改变handle的权限,也不能关闭他。标准的做法,只是简单的将nh复制给dev使用; 
  64.      * 参数nh:来自于sensors_control_open 
  65.      * 返回值:0表示成功,小于0表示失败; 
  66.      */  
  67.     int (*data_open)(struct sensors_data_device_t *dev, native_handle_t* nh);  
  68.       
  69.     /** 
  70.      * 
  71.      * 调用者必须完成了sensor的数据读取才可以调用此函数。因此可以知道,此函数会将设备节点给关闭掉; 
  72.      * 调用此函数不会阻塞poll函数。 
  73.      * 成功返回0,失败小于0 
  74.      * / 
  75.     int (*data_close)(struct sensors_data_device_t *dev); 
  76.      
  77.     /** 
  78.      *  
  79.      * sensors_data_t指针返回指定的sensor的数据 
  80.      * 返回值:sensor的handle 
  81.      * poll的实现这里也说的很清楚,是返回指定的sensor的数据,也就是说这个poll不能一次把所有的sensor的数据都拿到 
  82.      */  
  83.     int (*poll)(struct sensors_data_device_t *dev,   
  84.             sensors_data_t* data);  
  85. };  
这些函数的调用顺序是如何的呢?这就要看上层JAVA的调用了,反正JNI也指示函数的重定义。(当然这也只是我自己的理解)其实到了这里大家也都清楚这些函数的作用,整个hal层的代码也只是这些函数的实现。其中这里hal和java的通信的话不得不考虑其中有一个native_handle这样的机制,下面在看看这个再说。

原创粉丝点击