In-depth introduction-Android system migration and platform development (14th)-sensor Hal framework analysis 4

Source: Internet
Author: User
Sensor local encapsulation class sensordevice

Sensordevice is the abstract type encapsulation of the sensorservice to the sensor device in local code. It encapsulates the hardware operations of the sensor hardware, which inherits the singleton class, the getinstance method is used to obtain the device operation object in singleton mode:

@ Frameworks/base/services/sensorservice/sensordevice. h

class SensorDevice : public Singleton<SensorDevice> {    friend class Singleton<SensorDevice>;   struct sensors_poll_device_t* mSensorDevice;    struct sensors_module_t* mSensorModule;    mutable Mutex mLock; // protect mActivationCount[].rates    // fixed-size array after construction    struct Info {        Info() : delay(0) { }        KeyedVector<void*, nsecs_t> rates;        nsecs_t delay;status_t setDelayForIdent(void* ident, int64_t ns);        nsecs_t selectDelay();    };    DefaultKeyedVector<int, Info> mActivationCount;SensorDevice();public:    ssize_t getSensorList(sensor_t const** list);    status_t initCheck() const;    ssize_t poll(sensors_event_t* buffer, size_t count);    status_t activate(void* ident, int handle, int enabled);    status_t setDelay(void* ident, int handle, int64_t ns);    void dump(String8& result, char* buffer, size_t SIZE);};

The attributes and methods contained in the sensordevice class can be seen through the definition of the sensordevice class:

Attribute:

Msensordevice: Hal layer operation interface encapsulation structure of the sensor device

Msensormodule: Hal hardware module encapsulation structure of the sensor device

Mactivationcount: Save the active sensor device vector table

Method:

Sensordevice: Constructor

Getsensorlist: method for obtaining the sensor device list

Poll: Multi-Channel listening method for sensor devices

Activate: device activation method

Setdelay: Device sensor device delay Method

According to the previous analysis, sensordevice is a singleton model and its construction method is called only once:

@ Frameworks/base/services/sensorservice/sensordevice. cpp

Sensordevice: sensordevice (): msensordevice (0), msensormodule (0) {// I finally saw hw_get_module. Happy, happy, and happy. It's hard to meet each other... Status_t err = hw_get_module (sensors_hardware_module_id, (hw_module_t const **) & msensormodule); loge_if (ERR, "couldn't load % S Module (% s)", comment, strerror (-err); If (msensormodule) {// open the module device, return the operation interface of the module device, and save it in msensordevice err = sensors_open (& msensormodule-> common, & msensordevice); loge_if (ERR, "couldn't open device for module % s (% s)", sensors_hardware_module_id, strerror (-err); If (msensordevice) {sensor_t const * List; // call the get_sensors_list interface of the module device ssize_t COUNT = msensormodule-> get_sensors_list (msensormodule, & list); mactivationcount. setcapacity (count); Info model; For (size_t I = 0; I <size_t (count); I ++) {mactivationcount. add (list [I]. handle, model); msensordevice-> activate (msensordevice, list [I]. handle, 0 );}}}}

Call hw_get_module of Hal architecture in the sensordevice construction method to obtain the sensor device module. Then call the sensors_open tool function to open the sensor device module (call its methods-> OPEN function pointer ), return the operation interfaces of the sensor device (these interfaces are implemented at the Hal layer), save them in msensordevice, and call the get_sensors_list method of the sensor module to obtain the sensor list, activate these devices in turn and add them to the mactivationcount device information vector.

Sensor Hal module code and Open Module tool function sensors_open:

@ Hardware/libhardware/include/hardware/sensors. h

struct sensors_module_t {    struct hw_module_t common;    /**     * Enumerate all available sensors. The list is returned in "list".     * @return number of sensors in the list     */    int (*get_sensors_list)(struct sensors_module_t* module,            struct sensor_t const** list);};……static inline int sensors_open(const struct hw_module_t* module,        struct sensors_poll_device_t** device) {    return module->methods->open(module,            SENSORS_HARDWARE_POLL, (struct hw_device_t**)device);}

Other methods of sensordevice are relatively simple:

Ssize_t sensordevice: getsensorlist (sensor_t const ** list) {If (! Msensormodule) return no_init; // directly call the get_sensors_list method of the module to obtain the sensor list ssize_t COUNT = msensormodule-> get_sensors_list (msensormodule, list); Return count;} ssize_t sensordevice :: poll (sensors_event_t * buffer, size_t count) {If (! Msensordevice) return no_init; ssize_t C; do {// call the poll operation interface of the sensor device, which is implemented in the Hal layer C = msensordevice-> poll (msensordevice, buffer, count);} while (C =-eintr); Return C;} status_t sensordevice: Activate (void * Ident, int handle, int enabled) {If (! Msensordevice) return no_init; status_t err (no_error); bool actuatehardware = false; info & info (mactivationcount. editvaluefor (handle); logd_if (debug_connections, "sensordevice: Activate: IDENT = % P, handle = 0x % 08x, enabled = % d, Count = % d", ident, handle, enabled, info. rates. size (); If (Enabled) {mutex: autolock _ L (mlock); logd_if (debug_connections ,"... index = % lD ", info. rates. indexofkey (ident); // set the device to the default latency level if (info. rates. indexofkey (ident) <0) {info. rates. add (Ident, default_events_period); If (info. rates. size () = 1) {actuatehardware = true;} else {// sensor was already activated for this ident} else {mutex: autolock _ L (mlock ); logd_if (debug_connections ,"... index = % lD ", info. rates. indexofkey (ident); ssize_t idx = info. rates. removeitem (ident); If (idx> = 0) {If (info. rates. size () = 0) {actuatehardware = true;} else {// sensor wasn't enabled for this ident} If (actuatehardware) {logd_if (debug_connections, "\ t >>> actuating H/W"); // call the sensor device activate operation interface. In fact, the Hal layer err = msensordevice-> activate (msensordevice, handle, enabled ); if (Enabled) {loge_if (ERR, "error activating sensor % d (% s)", handle, strerror (-err); If (ERR = 0) {// enable the sensor power batteryservice: getinstance () in batterservice (). enablesensor (handle) ;}} else {If (ERR = 0) {// disable the sensor power batteryservice: getinstance () in batterservice (). disablesensor (handle) ;}}{// scope for the lock mutex: autolock _ L (mlock); nsecs_t NS = info. selectdelay (); // sets the latency value msensordevice-> setdelay (msensordevice, handle, NS);} return err ;}

According to the sensordevice methods, the specific implementation is all implemented by the device operation interface function encapsulated by msensordevice. These device operation interfaces are implemented at the Hal layer, in fact, sensordevice is only the device operation object of sensorservice, which encapsulates the operation of the device. What these operations actually "work" is the Hal layer code.

The analysis has come to the Hal layer. let's review what we learned earlier.

Let's summarize from the Java application layer to the framework layer and then to the local code:

1. Android applications call the getsystemservice method to obtain the sensormanager object. This method is implemented in contextimpl. java. It is the implementation class of activity's abstract parent class context.

2. Call registerservice during application (activity) initialization to create and register sensormanager

3. Create sensormanager

4. In the sensormanager constructor, the local method nativeclassinit () is called, which is used to initialize the local reference of the Java object sensor and facilitate local code operations on the Java object.

5. In the sensormanager constructor, call sensors_module_init () to create a local sensormanager object.

8. Call the sensors_module_get_next_sensor () method, fill in the sensor device list through the sensor reference initialized in nativeclassinit, and return it to the Java framework layer.

12. Save the device list obtained by sensors_module_get_next_sensor () to sfullsensorslist.

13. Create a sensorthread thread to prepare to listen for changes to the sensor hardware events.

14. The application uses getdefasensensor to obtain the object of the specified type of sensor.

16. register the sensor listener through registerlistener.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.