高通的HAL层其实分为两种,一种是直接从kernel这边报数据上来的,由sensor HAL层来监听,另一种是走ADSP的模式,HAL层是通过qmi的形式进行监听的;

走ADSP架构的可以看下面的博客:http://blog.csdn.net/u011006622/article/details/54598426

而msm8909架构下的便是以HAL层来监听数据的;

简介:

Google为Sensor提供了统一的HAL接口,不同的硬件厂商需要根据该接口来实现并完成具体的硬件抽象层,Android中Sensor的HAL接口定义在:hardware/libhardware/include/hardware/sensors.h:

为了了解HAL层的sensor,我们必须理解几个结构体:分别是sensor_type,sensor_t,sensors_module_t;

从下面可以看到此文件定义了sensor的type以及string type;

 1 /*
 2  * SENSOR_TYPE_ACCELEROMETER
 3  * reporting-mode: continuous
 4  *
 5  *  All values are in SI units (m/s^2) and measure the acceleration of the
 6  *  device minus the force of gravity.
 7  *
 8  *  Implement the non-wake-up version of this sensor and implement the wake-up
 9  *  version if the system possesses a wake up fifo.
10  */
11 #define SENSOR_TYPE_ACCELEROMETER                    (1)
12 #define SENSOR_STRING_TYPE_ACCELEROMETER             "android.sensor.accelerometer"

sensors_module_t结构体:

该结构体实际上是对标准硬件模块hw_module_t的一个扩展,增加一个get_sensor_list函数,用于获取传感器的列表,以及set_operation_mode设置为相关的mode;

 1 /**
 2  * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
 3  * and the fields of this data structure must begin with hw_module_t
 4  * followed by module specific information.
 5  */
 6 struct sensors_module_t {
 7     struct hw_module_t common;
 8
 9     /**
10      * Enumerate all available sensors. The list is returned in "list".
11      * @return number of sensors in the list
12      */
13     int (*get_sensors_list)(struct sensors_module_t* module,
14             struct sensor_t const** list);
15
16     /**
17      *  Place the module in a specific mode. The following modes are defined
18      *
19      *  0 - Normal operation. Default state of the module.
20      *  1 - Loopback mode. Data is injected for the supported
21      *      sensors by the sensor service in this mode.
22      * @return 0 on success
23      *         -EINVAL if requested mode is not supported
24      *         -EPERM if operation is not allowed
25      */
26     int (*set_operation_mode)(unsigned int mode);
27 };

sensors_module_t

sensor_t结构体:

  1 struct sensor_t {
  2
  3     /* Name of this sensor.
  4      * All sensors of the same "type" must have a different "name".
  5      */
  6     const char*     name;  //传感器名字
  7
  8     /* vendor of the hardware part */
  9     const char*     vendor;  //生产厂家名字
 10
 11     /* version of the hardware part + driver. The value of this field
 12      * must increase when the driver is updated in a way that changes the
 13      * output of this sensor. This is important for fused sensors when the
 14      * fusion algorithm is updated.
 15      */
 16     int             version;  
 17
 18     /* handle that identifies this sensors. This handle is used to reference
 19      * this sensor throughout the HAL API.
 20      */
 21     int             handle;  //传感器handle句柄
 22
 23     /* this sensor's type. */
 24     int             type;   //传感器类型
 25
 26     /* maximum range of this sensor's value in SI units */
 27     float           maxRange;  //最大范围
 28
 29     /* smallest difference between two values reported by this sensor */
 30     float           resolution;  //解析度
 31
 32     /* rough estimate of this sensor's power consumption in mA */
 33     float           power;
 34
 35     /* this value depends on the reporting mode:
 36      *
 37      *   continuous: minimum sample period allowed in microseconds
 38      *   on-change : 0
 39      *   one-shot  :-1
 40      *   special   : 0, unless otherwise noted
 41      */
 42     int32_t         minDelay;
 43
 44     /* number of events reserved for this sensor in the batch mode FIFO.
 45      * If there is a dedicated FIFO for this sensor, then this is the
 46      * size of this FIFO. If the FIFO is shared with other sensors,
 47      * this is the size reserved for that sensor and it can be zero.
 48      */
 49     uint32_t        fifoReservedEventCount;
 50
 51     /* maximum number of events of this sensor that could be batched.
 52      * This is especially relevant when the FIFO is shared between
 53      * several sensors; this value is then set to the size of that FIFO.
 54      */
 55     uint32_t        fifoMaxEventCount;
 56
 57     /* type of this sensor as a string. Set to corresponding
 58      * SENSOR_STRING_TYPE_*.
 59      * When defining an OEM specific sensor or sensor manufacturer specific
 60      * sensor, use your reserve domain name as a prefix.
 61      * ex: com.google.glass.onheaddetector
 62      * For sensors of known type, the android framework might overwrite this
 63      * string automatically.
 64      */
 65     const char*    stringType;
 66
 67     /* permission required to see this sensor, register to it and receive data.
 68      * Set to "" if no permission is required. Some sensor types like the
 69      * heart rate monitor have a mandatory require_permission.
 70      * For sensors that always require a specific permission, like the heart
 71      * rate monitor, the android framework might overwrite this string
 72      * automatically.
 73      */
 74     const char*    requiredPermission;
 75
 76     /* This value is defined only for continuous mode and on-change sensors. It is the delay between
 77      * two sensor events corresponding to the lowest frequency that this sensor supports. When lower
 78      * frequencies are requested through batch()/setDelay() the events will be generated at this
 79      * frequency instead. It can be used by the framework or applications to estimate when the batch
 80      * FIFO may be full.
 81      *
 82      * NOTE: 1) period_ns is in nanoseconds where as maxDelay/minDelay are in microseconds.
 83      *              continuous, on-change: maximum sampling period allowed in microseconds.
 84      *              one-shot, special : 0
 85      *   2) maxDelay should always fit within a 32 bit signed integer. It is declared as 64 bit
 86      *      on 64 bit architectures only for binary compatibility reasons.
 87      * Availability: SENSORS_DEVICE_API_VERSION_1_3
 88      */
 89     #ifdef __LP64__
 90        int64_t maxDelay;
 91     #else
 92        int32_t maxDelay;
 93     #endif
 94
 95     /* Flags for sensor. See SENSOR_FLAG_* above. Only the least significant 32 bits are used here.
 96      * It is declared as 64 bit on 64 bit architectures only for binary compatibility reasons.
 97      * Availability: SENSORS_DEVICE_API_VERSION_1_3
 98      */
 99     #ifdef __LP64__
100        uint64_t flags;
101     #else
102        uint32_t flags;
103     #endif
104
105     /* reserved fields, must be zero */
106     void*           reserved[2];
107 };

sensor_t

高通HAL:

现在回到高通定制的sensor HAL层来:(代码位于hardware\qcom\sensors:)

Sensor HAL:

首先sensor这个模块这个id的定义,主要实现了sensors_module_t结构:

 1 struct sensors_module_t HAL_MODULE_INFO_SYM = {
 2         common: {
 3                 tag: HARDWARE_MODULE_TAG,
 4                 version_major: 1,
 5                 version_minor: 0,
 6                 id: SENSORS_HARDWARE_MODULE_ID,
 7                 name: "Quic Sensor module",
 8                 author: "Quic",
 9                 methods: &sensors_module_methods,
10                 dso: NULL,
11                 reserved: {0},
12         },
13         get_sensors_list: sensors__get_sensors_list,
14 };

sensors__get_sensors_list函数返回这个平台的所有sensor:

1 static int sensors__get_sensors_list(struct sensors_module_t*,
2                                  struct sensor_t const** list)
3 {
4     NativeSensorManager& sm(NativeSensorManager::getInstance());
5
6     return sm.getSensorList(list);
7 }

sensors__get_sensors_list

里面使用了NativeSensorManager,sensors__get_sensors_list函数中调用单例模式创建了一个实例,然后再调用相应的成员函数获取传感器列表,并返回,返回值对应的sensor_t结构体;

NativeSensorManager统一管理着所有的传感器、物理和虚拟传感器。它的软件框架如下:

我们继续看sensors_module_methods:

1 static struct hw_module_methods_t sensors_module_methods = {
2         open: open_sensors
3 };

 1 /*****************************************************************************/
 2
 3 /** Open a new instance of a sensor device using name */
 4 static int open_sensors(const struct hw_module_t* module, const char*,
 5                         struct hw_device_t** device)
 6 {
 7         int status = -EINVAL;
 8         sensors_poll_context_t *dev = new sensors_poll_context_t();
 9         NativeSensorManager& sm(NativeSensorManager::getInstance());
10
11         memset(&dev->device, 0, sizeof(sensors_poll_device_1_ext_t));
12
13         dev->device.common.tag = HARDWARE_DEVICE_TAG;
14 #if defined(SENSORS_DEVICE_API_VERSION_1_3)
15         ALOGI("Sensors device API version 1.3 supported\n");
16         dev->device.common.version = SENSORS_DEVICE_API_VERSION_1_3;
17 #else
18         dev->device.common.version = SENSORS_DEVICE_API_VERSION_0_1;
19 #endif
20         dev->device.common.module   = const_cast<hw_module_t*>(module);
21         dev->device.common.close    = poll__close;
22         dev->device.activate        = poll__activate;
23         dev->device.setDelay        = poll__setDelay;
24         dev->device.poll        = poll__poll;
25         dev->device.calibrate        = poll_calibrate;
26 #if defined(SENSORS_DEVICE_API_VERSION_1_3)
27         dev->device.batch        = poll__batch;
28         dev->device.flush        = poll__flush;
29 #endif
30
31         *device = &dev->device.common;
32         status = 0;
33
34         return status;
35 }

open_sensors用来打开所有的sensor,并返回相应的状态;首先new了一个sensors_poll_context_t ,然后设置device,并返回;

对应的new sensors_poll_context_t,我们来看一下其实现:

 1 struct sensors_poll_context_t {
 2     // extension for sensors_poll_device_1, must be first
 3     struct sensors_poll_device_1_ext_t device;// must be first
 4     sensors_poll_context_t();
 5     ~sensors_poll_context_t();
 6     int activate(int handle, int enabled);
 7     int setDelay(int handle, int64_t ns);
 8     int pollEvents(sensors_event_t* data, int count);
 9     int calibrate(int handle, cal_cmd_t *para);
10     int batch(int handle, int sample_ns, int latency_ns);
11     int flush(int handle);
12
13 private:
14     static const size_t wake = MAX_SENSORS;
15     static const char WAKE_MESSAGE = 'W';
16     struct pollfd mPollFds[MAX_SENSORS+1];
17     int mWritePipeFd;
18     SensorBase* mSensors[MAX_SENSORS];
19     mutable Mutex mLock;
20 };

sensors_poll_context_t

 1 /*****************************************************************************/
 2
 3 sensors_poll_context_t::sensors_poll_context_t()
 4 {
 5     int number;
 6     int i;
 7     const struct sensor_t *slist;
 8     const struct SensorContext *context;
 9     NativeSensorManager& sm(NativeSensorManager::getInstance());
10   //获取sensor列表
11     number = sm.getSensorList(&slist);
12
13     /* use the dynamic sensor list */
14     for (i = 0; i < number; i++) {
15         context = sm.getInfoByHandle(slist[i].handle);  
16
17         mPollFds[i].fd = (context == NULL) ? -1 : context->data_fd;
18         mPollFds[i].events = POLLIN;
19         mPollFds[i].revents = 0;
20     }
21
22     ALOGI("The avaliable sensor handle number is %d",i);
23     int wakeFds[2];
24     int result = pipe(wakeFds);
25     ALOGE_IF(result<0, "error creating wake pipe (%s)", strerror(errno));
26     fcntl(wakeFds[0], F_SETFL, O_NONBLOCK);
27     fcntl(wakeFds[1], F_SETFL, O_NONBLOCK);
28     mWritePipeFd = wakeFds[1];
29
30     mPollFds[number].fd = wakeFds[0];
31     mPollFds[number].events = POLLIN;
32     mPollFds[number].revents = 0;
33 }

通过context = sm.getInfoByHandle(slist[i].handle); 维系了一个handle对应的SensorContext对象指针的句柄;

(context是一个SensorContext结构体,SensorContext包含了一个SensorBase *driver指针;注意这个很重要!就是这个与具体的sensor产生相应的关联;而mPollFds是一个pollfd的结构。context保存着各个打开的sensor,mPollFds用来监听sensor的时候用的文件描述符;)

然后继续往下看,由上面代码可见:sensors_poll_device_t的activate、setDelay和poll的实现函数分别为:

(1)  poll__activate

(2)   poll__setDelay

(3)   poll__poll

 1 int sensors_poll_context_t::activate(int handle, int enabled) {
 2     int err = -1;
 3     NativeSensorManager& sm(NativeSensorManager::getInstance());
 4     Mutex::Autolock _l(mLock);
 5
 6     err = sm.activate(handle, enabled);
 7     if (enabled && !err) {
 8         const char wakeMessage(WAKE_MESSAGE);
 9         int result = write(mWritePipeFd, &wakeMessage, 1);
10         ALOGE_IF(result<0, "error sending wake message (%s)", strerror(errno));
11     }
12
13     return err;
14 }

sensors_poll_context_t::activate

1 int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
2     int err = -1;
3     NativeSensorManager& sm(NativeSensorManager::getInstance());
4     Mutex::Autolock _l(mLock);
5
6     err = sm.setDelay(handle, ns);
7
8     return err;
9 }

sensors_poll_context_t::setDelay

 1 int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
 2 {
 3     int nbEvents = 0;
 4     int n = 0;
 5     NativeSensorManager& sm(NativeSensorManager::getInstance());
 6     const sensor_t *slist;
 7     int number = sm.getSensorList(&slist);
 8
 9     do {
10         // see if we have some leftover from the last poll()
11         for (int i = 0 ; count && i < number ; i++) {
12             if ((mPollFds[i].revents & POLLIN) || (sm.hasPendingEvents(slist[i].handle))) {
13                 Mutex::Autolock _l(mLock);
14                 int nb = sm.readEvents(slist[i].handle, data, count);
15                 if (nb < 0) {
16                     ALOGE("readEvents failed.(%d)", errno);
17                     return nb;
18                 }
19                 if (nb <= count) {
20                     // no more data for this sensor
21                     mPollFds[i].revents = 0;
22                 }
23                 count -= nb;
24                 nbEvents += nb;
25                 data += nb;
26             }
27         }
28
29         if (count) {
30             // we still have some room, so try to see if we can get
31             // some events immediately or just wait if we don't have
32             // anything to return
33             do {
34                 n = poll(mPollFds, number + 1, nbEvents ? 0 : -1);
35             } while (n < 0 && errno == EINTR);
36             if (n<0) {
37                 ALOGE("poll() failed (%s)", strerror(errno));
38                 return -errno;
39             }
40             if (mPollFds[number].revents & POLLIN) {
41                 char msg;
42                 int result = read(mPollFds[number].fd, &msg, 1);
43                 ALOGE_IF(result<0, "error reading from wake pipe (%s)", strerror(errno));
44                 ALOGE_IF(msg != WAKE_MESSAGE, "unknown message on wake queue (0x%02x)", int(msg));
45                 mPollFds[number].revents = 0;
46             }
47         }
48         // if we have events and space, go read them
49     } while (n && count);
50
51     return nbEvents;
52 }

sensors_poll_context_t::pollEvents

1 static int poll__close(struct hw_device_t *dev)
2 {
3     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
4     if (ctx) {
5         delete ctx;
6     }
7     return 0;
8 }

poll__close

1 static int poll__activate(struct sensors_poll_device_t *dev,
2         int handle, int enabled) {
3     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
4     return ctx->activate(handle, enabled);
5 }

poll__activate

1 static int poll__setDelay(struct sensors_poll_device_t *dev,
2         int handle, int64_t ns) {
3     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
4     return ctx->setDelay(handle, ns);
5 }

poll__setDelay

1 static int poll__poll(struct sensors_poll_device_t *dev,
2         sensors_event_t* data, int count) {
3     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
4     return ctx->pollEvents(data, count);
5 }

poll__poll

以poll_poll为例,看看如何与具体的sensor建立连接的:

1 return ctx->pollEvents(data, count);

ctx->pollEvents函数又调用了NativeSensorManager::readEvents;

 1 int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
 2 {
 3     int nbEvents = 0;
 4     int n = 0;
 5     NativeSensorManager& sm(NativeSensorManager::getInstance());
 6     const sensor_t *slist;
 7     int number = sm.getSensorList(&slist);
 8
 9     do {
10         // see if we have some leftover from the last poll()
11         for (int i = 0 ; count && i < number ; i++) {
12             if ((mPollFds[i].revents & POLLIN) || (sm.hasPendingEvents(slist[i].handle))) {
13                 Mutex::Autolock _l(mLock);
14                 int nb = sm.readEvents(slist[i].handle, data, count);
15                 if (nb < 0) {
16                     ALOGE("readEvents failed.(%d)", errno);
17                     return nb;
18                 }
19                 if (nb <= count) {
20                     // no more data for this sensor
21                     mPollFds[i].revents = 0;
22                 }
23                 count -= nb;
24                 nbEvents += nb;
25                 data += nb;
26             }
27         }
28
29         if (count) {
30             // we still have some room, so try to see if we can get
31             // some events immediately or just wait if we don't have
32             // anything to return
33             do {
34                 n = poll(mPollFds, number + 1, nbEvents ? 0 : -1);
35             } while (n < 0 && errno == EINTR);
36             if (n<0) {
37                 ALOGE("poll() failed (%s)", strerror(errno));
38                 return -errno;
39             }
40             if (mPollFds[number].revents & POLLIN) {
41                 char msg;
42                 int result = read(mPollFds[number].fd, &msg, 1);
43                 ALOGE_IF(result<0, "error reading from wake pipe (%s)", strerror(errno));
44                 ALOGE_IF(msg != WAKE_MESSAGE, "unknown message on wake queue (0x%02x)", int(msg));
45                 mPollFds[number].revents = 0;
46             }
47         }
48         // if we have events and space, go read them
49     } while (n && count);
50
51     return nbEvents;
52 }

sensors_poll_context_t::pollEvents

NativeSensorManager::readEvents中又调用了:

1 nb = list->driver->readEvents(data, count);

记得上面说过什么?list->driver是一个SensorBase结构体,于是终于终于我们来到了SensorBase结构体的函数readEvents,接下来就是具体的sensor模块读取的任务了!!

其主要框架如下图所示:

下来继续看具体sensor的处理过程:http://www.cnblogs.com/linhaostudy/p/8432741.html

转载于:https://www.cnblogs.com/linhaostudy/p/8430583.html

高通HAL层之Sensor HAL相关推荐

  1. 高通平台SEE架构-sensor学习

    https://blog.csdn.net/liuei633/article/details/80935491---高通SDM845平台Sensor学习--1.框架 https://blog.csdn ...

  2. 高通平台 input类 sensor驱动分析 : 光感/距感 stk3x1x driver分析

    stk3x1x driver分析 1:注册驱动 定义 i2c_driver static struct i2c_driver stk_ps_driver = {.driver = {.name = D ...

  3. 高通ADSP抓取sensor init log

    高通平台adsp sensor的驱动log需要使用QXDM抓取,sensor init是sensor初始化的log信息,这时候QXDM还没有识别到diag口,无法直接获取. 高通支持如下两种获取ads ...

  4. 高通平台SSC架构-sensor学习

    SSC软件架构学习参考 https://www.cnblogs.com/linhaostudy/archive/2018/03/16/8578414.html King_Alexwang-系列文章 h ...

  5. 高通Audio中ASOC的machine驱动

    高通Audio中ASOC的machine驱动 233333发表于linux驱动个人学习已订阅 1.1K ASoC被分为Machine.Platform和Codec三大部分,其中的Machine驱动负责 ...

  6. 高通SDM845平台Sensor学习——2.Hal层

    二:Sensor Hal层代码分析 Hal code放在/vendor/qcom/proprietary/sensors-see/中 sensors-hal文件夹中包含framework和sensor ...

  7. 高通SDM845平台Sensor学习——2.Hal层--

    二:Sensor Hal层代码分析 Hal code放在/vendor/qcom/proprietary/sensors-see/中 sensors-hal文件夹中包含framework和sensor ...

  8. 【SPRD CAMERA】1 HAL层初始化流程

    一.前言 根据我的理解以前android在启动camera service 会直接去操作hal层,这样hal层和framework就耦合在一起了.现在 Android O 中,加入了Camera Pr ...

  9. 高通SDM845平台Sensor学习——3.SLPI(Physical Sensor)

    ####三:Sensor SLPI层代码分析 #### 在学习SLPI侧代码前我们先了解下SEE的registry&config. registry 放在/persist/sensors/re ...

最新文章

  1. 服务化部署框架Paddle Serving
  2. 【机器视觉案例】(10) AI视觉搭积木,手势移动虚拟物体,附python完整代码
  3. python 多维数组删除重复
  4. 学习笔记Spark(二)—— Spark集群的安装配置
  5. 理解单例模式、单例类
  6. sql语句在navicat中可以查询到所有数据但是在idea程序中不行_数据迁移测试实施方案...
  7. 线程销毁_多线程(2)-Java高级知识(9)
  8. Redis主从配置详细过程
  9. GlobalSight在翻译社区中与开源大放异彩
  10. go tcp客户端自动重连_阿里面试: HTTP、HTTPS、TCP/IP、三次握手四次挥手过程?(附全网最具深度讲解)
  11. java定时器只执行一次_搞定Java并发:为什么说只有1种实现线程的方法?(下)...
  12. python matplotlib绘图显示中文
  13. Android移植之自定义ProgressBar
  14. 2017百度之星资格赛:1005. 寻找母串(卡特兰数+分块打表)
  15. 微软私有云分享(R2)24 审核WDS部署
  16. 【php写日志】php将日志写入文件
  17. C语言程序设计 | 猜数字游戏
  18. h3c 出口路由器mac地址过滤
  19. deeplabv3+ 跑 cityscape 数据集
  20. 超声图像拼接及三维重建

热门文章

  1. MyBatis→优缺点、select延迟加载、接口式MyBatis编程、一级缓存、二级缓存、集成Redis自定义缓存、Log4j
  2. 计算机安装时的用户权限,如何设置win10系统使得电脑在安装软件前需要管理员的权限...
  3. java set的作用,Java的自学之路-构造方法 的作用以及与set方法的区别
  4. python和selenium的关系_selenium之python源码解读-webdriver继承关系
  5. ubuntu上安装python3.7教程_给ubuntu18安装python3.7的详细教程
  6. p71_文件传送协议FTP
  7. Kruskal/prim--最小生成树
  8. python网络爬虫系列教程——python网络数据爬虫误区,让你的爬虫更像人类
  9. python应用系列教程——python使用SocketServer实现网络服务器,socket实现客户端
  10. python实现图片自动轮番播放