Android platform read and write I2C Device Development Note 2

Source: Internet
Author: User

2. Use JNI to add service access interfaces at the application framework layer

The application cannot directly access the Hal layer. The JNI layer needs to access the Hal module and provide APIs. You can directly provide interfaces, but it is recommended that you provide access through services.

Let's first look at how JNI accessed the Hal module.

Go to the frameworks/base/service/JNI directory under the source code root directory and create com_android_server_iicservice.cpp. The Code is as follows:

# Include "jni. h "# include" JNIHelp. h "# include" android_runtime/AndroidRuntime. h "# include <utils/misc. h> # include <cutils/log. h> # include 

Then, you need to enable android to load the JNI module at startup.

Modify onload. cpp in the same directory:

Add a line of int register_android_server_iicservice (jnienv * env) to namespace android );

Add a line in the jni_onload method: register_android_server_iicservice (ENV );

Modify Android. mk in the same directory:

Local_src_files adds a line com_android_server_iicservice \

Compile command: Mmm frameworks/base/services/JNI

Note: Hal loads the corresponding module according to iic_hardware_module_id in iic_init.

Then, use aidl for inter-process communication so that the app can access custom hardware services.

We need to create iiicservice. aidl in frameworks/base/CORE/Java/Android/OS (note that it is III)

Package Android. OS;
Interface iiicservice {
Void setval (string Val, int slaveaddr, int regaddr, int Len );
String getval (INT slaveaddr, int Len );
}
It defines the service interface, which is implemented in iicservice and associated with the JNI local method.

At the same time, we need to modify the Android. mk compilation file under frameworkd/base, and add Core/Java/Android/OS/iiicservice. aidl to local_src_files.

Compile command: Mmm frameworks/base

The following is the aidl implementation method class: COM. Android. server. iicservice location: Frameworks/base/services/Java/COM/Android/server code:

Package com. android. server; import android. content. context; import android. OS. IIICService; import android. util. slog; public class IICService extends IIICService. stub {private static final String TAG = "IICService"; IICService () {init_native ();} public void setVal (String val, int slaveAddr, int regAddr, int len) {setVal_native (val, slaveAddr, regAddr, len);} public String getVal (int slaveAddr, int len) {return getVal_native (slaveAddr, len );} // local method private static native boolean init_native (); private static native void setVal_native (String val, int slaveAddr, int regAddr, int len); private static native String getVal_native (int slaveAddr, int len );};

From the code, we can see that it inherits IIICService. Stub and implements two interface methods. Hardware access usually needs to be placed in an independent thread. The proxy method is used to process the communication between the app and the hardware service.

Add the newly added IICService to ServiceManager so that it can be called through ServiceManager.

Modify systemserver. Java under frameworks/base/services/Java/COM/Android/Server
Add

Try {

Slog. I (TAG, "IIC service ");

Servicemanager. addservice ("IIC", new iicservice ());

} Catch (throwable e ){

Slog. E (TAG, "Failure starting IIC service", e );

}
Compile command: Mmm frameworks/base/services/Java

Or use another form to call the service: the same method as binding a service using the Binder Mechanism is not detailed.


Note: compilation may fail. Because the official Android API is modified here, you need to run make Update-API to update frameworks/base/API/current. xml.

After packaging, the app can use the iicservice interface to access the hardware.
The next section sends app-related code

(To be continued)

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.