Android2.3 gsensor Analysis

Source: Internet
Author: User

I analyzed gsensor today, but I didn't expect it to be difficult.
The source code of android2.2 and the source code of 2.3 are somewhat different in these places. I hope you can see whether your source code is 2.2 or 2.3 first.

Correct the analysis errors. Thank you!

Not all annotations are written. In some cases, the "you know" type is used. In some cases, you can check the source code by yourself.
System_init.cpp

Extern "C" status_t system_init (){.......... sensorservice: instantiate ();..........} instantiate is not the function of the sensorservice, but the functionclass sensorservice: Public binderservice <sensorservice>, public bnsensorserver, and protected threadbinderservice of the inherited binderservice. hclass binderservice {public: static status_t publish () {sp <iservicemanager> SM (defaultservicemanager (); Return Sm-> addservice (string16 (Service: getservicename ()), new Service ());}.............. static void instantiate () {publish ();}...............}

Sensordevice. cpp

Using (sensordevice) sensordevice: sensordevice (): msensordevice (0), msensormodule (0) {// # define sensors_hardware_module_id "sensors" // hw_get_module function is defined in hardware. in C, the following describes status_t err = hw_get_module (sensors_hardware_module_id, (hw_module_t const **) & msensormodule); loge_if (ERR, "couldn't load % S Module (% s) ", sensors_hardware_module_id, strerror (-err); If (msensormodule) {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; ssize_t COUNT = msensormodule-> get_sensors_list (msensormodule, & list );

Sensormanager. cpp

Android_singleton_static_instance (sensormanager) // single sensormanager: sensormanager (): msensorlist (0) {const string16 name ("sensorservice"); While (getservice (name, & msensorserver )! = No_error) {usleep (250000);} msensors = msensorserver-> getsensorlist (); size_t COUNT = msensors. size (); msensorlist = (sensor const **) malloc (count * sizeof (sensor *); For (size_t I = 0; I <count; I ++) {msensorlist [I] = msensors. array () + I ;}} sensorservice. cppvoid sensorservice: onfirstref () int COUNT = Dev. getsensorlist (& list); mlasteventseen. setcapacity (count); For (INT I = 0; I <count; I ++ ){ Registersensor (New hardwaresensor (list [I]); Switch (list [I]. Type) {Case sensor_type_gravity: Case sensitivity: Case sensor_type_rotation_vector: virtualsensorsneeds & = ~ (1 <list [I]. type); break ;}} getsensorlistssize_t sensordevice: getsensorlist (sensor_t const ** list) {If (! Msensormodule) return no_init; ssize_t COUNT = msensormodule-> get_sensors_list (msensormodule, list); Return count ;}

Sensor. h

/** * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM * and the fields of this data structure must begin with hw_module_t * followed by module specific information. */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);};struct sensors_module_t HAL_MODULE_INFO_SYM = {    .common = {        .tag = HARDWARE_MODULE_TAG,        .version_major = 1,        .version_minor = 0,        .id = SENSORS_HARDWARE_MODULE_ID,        .name = "MTK SENSORS Module",        .author = "The Android Open Source Project",        .methods = &sensors_module_methods,    },    .get_sensors_list = sensors__get_sensors_list,};

Hardware. c

/** Base path of the Hal modules */# define hal_library_path1 "/system/lib/HW" # define hal_library_path2 "/vendor/lib/HW" $ CD hwcd HW $ lslssensors. arm926.solights. default. sogralloc. default. soint hw_get_module (const char * ID, const struct hw_module_t ** module ){............ snprintf (path, sizeof (PATH), "% S/% S. % S. so ", // view the sensor from ls in the Android system directory above. arm926.so arm926 here is the type of your board hal_library_path1, ID, Prop); // # define hal_library_path1 "/system/lib/HW" If (access (path, r_ OK) = 0) break; snprintf (path, sizeof (PATH), "% S/% S. % S. so ", hal_library_path2, ID, Prop); // # define hal_library_path2"/vendor/lib/HW "If (access (path, r_ OK) = 0) break ;............ if (I 

It actually calls

Static int load (const char * ID, const char * path, const struct hw_module_t ** phmi) {struct hw_module_t * HMI; handle = dlopen (path, rtld_now ); // open the library ............. /* get the address of the struct hal_module_info. */const char * sym = role;/*** name of the hal_module_info as a string * // # define hal_module_info_sym_as_str "HMI" HMI = (struct hw_module_t *) dlsym (handle, sym); // here You have found the address of hw_module_t./* check that the ID matches * // check whether it matches if (strcmp (ID, HMI-> ID )! = 0) {LogE ("load: Id = % s! = HMI-> id = % s ", ID, HMI-> ID); status =-einval; goto done ;}............. * phmi = HMI; // do not forget to pass the obtained address back to the phmi parameter. It is actually the second parameter module of hw_get_module. // then return to the sensordevice above. in the constructor in CPP, check whether msensormodule is available. // then, the methods of msensormodule can be used}

Check that sensors_open in sensordevice. cpp calls the static function in sensors. h.

/** convenience API for opening and closing a device */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);}

This open prototype is in sensors_hwsen.c.

/** Open a new instance of a sensor device using name */static int open_sensors(const struct hw_module_t* module, const char* name,        struct hw_device_t** device){    ..............    //#define HWM_SENSOR_DEV                  "/dev/hwmsensor"  ---hwmsensor.h    dev->device_io_fd = open(HWM_SENSOR_DEV, O_RDONLY);    .............    dev->activate =  hwm__activate;    dev->set_delay = hwm__set_delay;    dev->poll = hwm__poll;       dev->device.common.tag = HARDWARE_DEVICE_TAG;    dev->device.common.version  = 0;    dev->device.common.module   = (struct hw_module_t*)module;    dev->device.common.close    = data__close;    dev->device.activate        = control__activate;    dev->device.setDelay        = control__setDelay;    dev->device.poll            = data__poll;}static int data__poll(struct sensors_poll_device_t *dev,        sensors_event_t* data, int count){    struct sensors_data_context_t* ctx = (struct sensors_data_context_t*)dev;       return ctx->poll((struct sensors_data_context_t*)dev,data, count);}static int hwm__poll(struct sensors_data_context_t *dev, sensors_event_t* values, int count){    ..................}

JNI Layer

Using jintsensors_data_poll (jnienv * ENV, jclass clazz, jint nativequeue, jfloatarray values, jintarray status, jlongarray timestamp ){......... status_t res; asensorevent event; Res = queue-> Read (& event, 1); If (RES =-eagain) {res = queue-> waitforevent (); if (res! = No_error) Return-1; Res = queue-> Read (& event, 1);} jint accuracy = event. vector. status; // It is said that the following is "put all the values obtained by the sensor in the value array" // In the android2.2 source code, this function clearly indicates the ssensordevice called-> poll (ssensordevice, & data); // This is the source code of 2.3. I don't know how to do it here. I haven't seen it for a long time. I went crazy! (Is there any poll on the Hal layer ???) Env-> setfloatarrayregion (values, 0, 3, event. vector. v); env-> setintarrayregion (status, 0, 1, & accuracy); env-> setlongarrayregion (timestamp, 0, 1, & event. timestamp );..........}

This is a JNI function. The upper-layer Java needs to call sensors_data_poll.

Static jninativemethod gmethods [] = {"nativeclassinit", "() V", (void *) nativeclassinit}, {"sensors_module_init", "() I", (void *) sensors_module_init}, {"sensors_module_get_next_sensor", "(landroid/hardware/sensor; I) I", (void *) identifier}, {"sensors_create_queue", "() I ", (void *) sensors_create_queue}, {"sensors_destroy_queue", "(I) V", (void *) queue}, {"sensors_enable_sensor", "(iljava/lang/string; ii) Z ", (void *) sensors_enable_sensor}, {" sensors_data_poll "," (I [f [I [J) I ", (void *) sensors_data_poll },} in sensormanager. private class sensorthreadrunnable implements runnable {public void run () {While (true) {// cyclically call sensors_data_poll // wait for an event final int sensor = sensors_data_poll (squeue, values, status, timestamp); int accuracy = status [0]; ...... if (listener. hassensor (sensorobject) {// This is asynchronous (okay to call // With slisteners lock held ). listener. onsensorchangedlocked (sensorobject, values, timestamp, accuracy) ;}} void onsensorchangedlocked (sensor, float [] values, long [] timestamp, int accuracy) {sensorevent T = getfrompool (); Final float [] V = T. values; V [0] = values [0]; // here, the X, Y, and Z data are obtained V [1] = values [1]; V [2] = values [2]; T. timestamp = timestamp [0]; T. accuracy = accuracy; T. sensor = sensor; message MSG = message. obtain (); MSG. what = 0; MSG. OBJ = T; mhandler. sendmessage (MSG); // transmitted by message}

I have been analyzing it for half a day and don't want to proceed with the analysis.
Search for "G-sensor-related processes in Android" and you will find out.
The following links are all very good, and are basically analyzed on the basis of 2.2.
Http://blog.csdn.net/wgejygah/archive/2010/06/29/5700922.aspx
Http://hi.baidu.com/snownight/blog/item/d863d21bb7dd9bd8ad6e75d0.html
Http://wenku.baidu.com/view/e4196fd4b9f3f90f76c61b1f.html

Http://blog.csdn.net/qianjin0703/archive/2010/10/15/5942579.aspx

Article Source: http://hi.baidu.com/%D7%D4%D3%C9%B5%C6%CB%FE/blog/item/af3454038be40efb09fa930c.html

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.