Sensor Local Package class Sensordevice
Sensordevice is an abstract type encapsulation of the sensor device in the native code, which encapsulates the hardware operation of the sensors hardware, which inherits the Singleton class and obtains a singleton mode device operation object through the GetInstance method:
@frameworks/base/services/sensorservice/sensordevice.h
classSensordevice: PublicSingleton<sensordevice>{friendclassSingleton<sensordevice>; structsensors_poll_device_t*Msensordevice; structsensors_module_t*Msensormodule; mutable Mutex MLock; //Protect Mactivationcount[].rates//fixed-size Array after construction structInfo {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_tConst**list); status_t Initcheck ()Const; ssize_t Poll (sensors_event_t*buffer, size_t count); status_t Activate (void* Ident,intHandleintenabled); status_t Setdelay (void* Ident,inthandle, int64_t NS); voidDump (string8& result,Char*buffer, size_t size);};
You can see the properties and methods that it contains through the definition of the Sensordevice class:
Property:
Msensordevice:sensor Device HAL Layer operation interface Package structure
Msensormodule:sensor Device HAL Hardware module package structure
Mactivationcount: Save active sensor device vector table
Method:
Sensordevice: Construction method
Getsensorlist: Obtaining a Sensor device list method
Multi-channel monitoring method for poll:sensor equipment
Activate: Device activation method
Setdelay: Device sensor Device delay method
From the previous analysis, Sensordevice is a singleton model, and its construction method is only called once:
@frameworks/base/services/sensorservice/sensordevice.cpp
Sensordevice::sensordevice (): Msensordevice (0), Msensormodule (0){ //finally see Hw_get_module, happy, happy, happy, meet when difficult to do not also difficult ...status_t err =Hw_get_module (sensors_hardware_module_id, (hw_module_tConst* *) &msensormodule); Loge_if (Err,"couldn ' t load%s module (%s)", sensors_hardware_module_id, Strerror (-err)); if(msensormodule) {//Open the module device and return to the module device's operating interface, saved in MsensordeviceErr = 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_tConst*list; //call the Get_sensors_list interface of the module devicessize_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); } } }}
In the Sensordevice construction method, the Hw_get_module of the HAL schema is called to obtain the sensor device module, and then the Sensors_open tool function is called to open the Sensor device module (calling its methods-> Open function pointer), returns the operating interface of the sensor device (these interfaces are implemented in the HAL layer), is stored in the Msensordevice, and invokes the Get_sensors_list method of the sensor module to obtain a list of sensors, The devices are then activated in turn and added to the Mactivationcount device information vector.
Sensor HAL Module code and open Module tool function Sensors_open:
@hardware/libhardware/include/hardware/sensors.h
structsensors_module_t {structhw_module_t Common; /** * Enumerate all available sensors. The list is returned in "list". * @return number of sensors in the list*/ int(*get_sensors_list) (structsensors_module_t*module,structsensor_tConst**list);}; ...StaticInlineintSensors_open (Const structhw_module_t*module,structsensors_poll_device_t**device) { returnModule->methods->Open (module, Sensors_hardware_poll, (structhw_device_t**) device);}
Sensordevice Several other methods are relatively simple:
ssize_t Sensordevice::getsensorlist (sensor_tConst**list) { if(!msensormodule)returnNo_init; //get the sensor list by calling the Get_sensors_list method of the module directlyssize_t count = msensormodule->get_sensors_list (Msensormodule, list); returncount;} ssize_t Sensordevice::p oll (sensors_event_t*buffer, size_t count) { if(!msensordevice)returnNo_init; ssize_t C; Do { //invokes the poll operator interface of the sensor device, which is implemented on the HAL layerc = msensordevice->Poll (msensordevice, buffer, count); } while(c = =-eintr); returnC;} status_t Sensordevice::activate (void* Ident,intHandleintenabled) { if(!msensordevice)returnNo_init; status_t err (NO_ERROR); BOOLActuatehardware =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 as the default delay level if(Info.rates.indexOfKey (Ident) <0) {Info.rates.add (ident, Default_events_period); if(info.rates.size () = =1) {Actuatehardware=true; } } Else { //sensor is 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, actually now HAL layerErr = msensordevice->Activate (Msensordevice, handle, enabled); if(enabled) {loge_if (err,"Error activating sensor%d (%s)", Handle, Strerror (-err)); if(Err = =0) { //enable sensor power in battery servicebatteryservice::getinstance (). Enablesensor (handle); } } Else { if(Err = =0) { //Turn off sensor power in battery servicebatteryservice::getinstance (). Disablesensor (handle); } } } { //scope for the lockmutex::autolock _l (mLock); nsecs_t NS=Info.selectdelay (); //Setting the delay valueMsensordevice->Setdelay (Msensordevice, handle, NS); } returnerr;}
By these several sensordevice methods, its specific implementation by the Msensordevice encapsulated device operation interface function implementation, these devices operating interface in the HAL layer implementation, in fact, Sensordevice just sensorservice device Operation object , which encapsulates the operation of the device, which actually "works" is the HAL layer code.
Analysis all the way over, has reached the HAL layer, we look back on the previous study.
Let's summarize from the Java application layer to the framework layer and then to the local code:
1. The Android application calls the Getsystemservice method to get the Sensormanager object, which is implemented in Contextimpl.java, which is the activity's abstract parent-class context implementation class.
2. Call Registerservice to create and register the Sensormanager when the application (Activity) is initialized
3. Create Sensormanager
4. In the Sensormanager constructor method, a local method called the Nativeclassinit () is used to initialize the native reference of the Java object sensor, allowing local code to manipulate the Java object.
5. In the Sensormanager constructor method, call Sensors_module_init () to create the Sensormanager local object.
8. Call the Sensors_module_get_next_sensor () method and return to the Java framework layer by populating the sensor device list with the sensor reference initialized in Nativeclassinit.
12. Save the list of devices obtained by Sensors_module_get_next_sensor () in Sfullsensorslist.
13. Create a Sensorthread thread to prepare for monitoring sensor hardware event changes.
14. The application uses Getdefaultsensor to obtain the object of the specified type of sensor
16. Register the sensor listener via Registerlistener.