Android layered learning notes (2)

Source: Internet
Author: User

Android is JNI, which is an intermediary between Java and C. Java accesses C/C ++ functions through JNI.
JNI is easy to write, and a template can be applied. Just like writing 8-Unit documents, you don't have to fill in the content as beautifully as writing 8-Unit documents. Instead, you can complete the functions without a gorgeous appearance.

Create the framework under the Hal directory and create the service/JNI directory under the framework, that is
CD hal
Mkdir-P framework/JNI

Create the com_ask_gpio.cpp file in the JNI directory.

Header file:
# Include "utils/log. H" // used to view log information in logcat

# Include <stdlib. h>
# Include <string. h>
# Include <unistd. h>
# Include <assert. h>

# Include <JNI. h> // JNI-related functions
# Include <gpio. h> // gpio operation functions and variables (ID). This header file is actually the gpio. h file under the module/gpio/directory.

Enable gpio to set 1
Static jboolean ask_gpio_set (jnienv * ENV, jobject thiz, jint gpio)
{
Logi ("gpioservice JNI: ask_gpioset ()");

// APC

If (g_gpiodev = NULL)
{
Logi ("gpioservice JNI: Not gpio device .");
Return-1;
}
Else
{
Return g_gpiodev-> setgpio (g_gpiodev, gpio );
}
}

Achieve gpio clearing 0
Static jboolean ask_gpio_clr (jnienv * ENV, jobject thiz, jint gpio)
{
Logi ("gpioservice JNI: ask_gpioclr ()");

If (g_gpiodev = NULL)
{
Logi ("gpioservice JNI: Not gpio device .");
Return-1;
}
Else
{
Return g_gpiodev-> clrgpio (g_gpiodev, gpio );
}
}
When the gpio device is enabled on the JNI layer, the gpio_device_open function in module/gpio. C is called.
Static inline int ask_gpio_open (const struct hw_module_t * module, struct gpio_device_t ** device)
{
Return Module-> methods-> open (module, gpio_hardware_module_id, (struct hw_device_t **) device );
}

Interface functions called for the Java Layer
Static const jninativemethod gmethods [] = {
{"_ Gpio_init", "() Z", (void *) ask_gpio_init },
{"_ Gpio_set", "(I) Z", (void *) ask_gpio_set },
{"_ Gpio_clr", "(I) Z", (void *) ask_gpio_clr },
};
Note: The input of this struct involves the conversion from C type to Java type. Android defines bridge variables, such as () Z. The specific ing is as follows:
The struct variable members of jninativemethod are
Const char * Name; // name of the function in Java
Const char * signature; // signature, which describes the function parameters and return values using strings.
Void * fnptr; // fnptr is a function pointer pointing to the C function.

For signature, the content must be in the same format as that of the fnptr function pointer, that is
(1) If the fnptr function points to void fun (void), the signature content must be "()"
(2) If the fnptr function points to int func (int I, Int J), the content of signature must be "(ii) I"

The relationship between each character is as follows:
Character Java type C type
V: void <--> void
Z: jboolean <--> Boolean
I: jint <--> int
J: jlong long
D: jdouble <--> double
F: jfloat <--> float
B: jbyte <--> byte
C: jchar <--> char
S: jshort <--> short
The array starts with "[" and is represented by two characters.
[I: jintarray <--> int []
[F: jfloatarray <--> float []
[B: jbytearray <--> byte []
[C: jchararray <--> char []
[S: jshortarray <--> short []
[D: jdoublearray <--> double []
[J: jlongarray <--> long []
[Z: jbooleanarray <--> Boolean []
The above is the basic type. If the Java function parameter is a class, it will start with "L" and end with ";" and the package and class names separated. The corresponding C function name parameter is jobject. One exception is the string class, and its corresponding class is jstring.
Ljava/lang/string;: String <--> jstring
Ljava/NET/socket;: Socket <--> jobject
If a Java function is located in an embedded class, $ is used as the delimiter between class names.
For example, "(ljava/lang/string; landroid/OS/fileutils $ filestatus;) z"

Register an Access Method
Int register_ashals_server_gpioservice (jnienv * env ){
Static const char * const kclassname = "com/ask/Server/gpioservice ";
Jclass clazz;

/* Look up the class */
Clazz = env-> findclass (kclassname );
If (clazz = NULL)
{
LogE ("can't find Class % s/n", kclassname );
Return-1;
}

/* Register all the methods */
If (env-> registernatives (clazz, gmethods,
Sizeof (gmethods)/sizeof (gmethods [0])! = Jni_ OK)
{
LogE ("failed registering methods for % s/n", kclassname );
Return-1;
}

/* Fill out the rest of the ID cache */
Return 0;
}
The kclassname must be consistent with the name item in. xml when writing the Java code service. Otherwise, the class name cannot be found, which will be described in the next section.

Load the C Module and call the register_ashals_server_gpioservice function.
Jint jni_onload (JavaVM * Vm, void * Reserved)
{
Jnienv * Env = NULL;
Jint result =-1;

If (Vm-> getenv (void **) & ENV, jni_version_1_4 )! = Jni_ OK ){
LogE ("getenv failed! ");
Return result;
}
Log_assert (ENV, "cocould not retrieve the Env! ");

Register_ctophals_server_gpioservice (ENV );
Return jni_version_1_4;
}
This function is called when the system. load function is related to Java.

Write the CPP File
The rest is the Android. mk file.
As follows:
Local_path: = $ (call my-DIR)

Include $ (clear_vars)
Ask_module_h_dir: =/home/gium/android_src/vendor/ask/modules/gpio/
Local_src_files: =/
Com_ask_gpio.cpp

Local_c_includes + =/
$ (Jni_h_include )/
$ (Ask_module_h_dir)
Local_shared_libraries: =/
Libcutils/
Libhardware/
Libhardware_legacy/
Libnativehelper/
Libsystem_server/
Libutils/
Libui
Ifeq ($ (target_simulator), true)
Ifeq ($ (target_ OS), Linux)
Ifeq ($ (target_arch), x86)
Local_ldlibs + =-lpthread-LDL-LRT
Endif
Endif
Endif

Ifeq ($ (with_malloc_leak_check), true)
Local_cflags + =-dmalloc_leak_check
Endif

Local_module: = libaskgpio

Local_prelink_module: = false
Include $ (build_shared_library)

Note: ask_module_h_dir is a self-defined directory where the gpio module is located. It should be changed according to your actual situation, my current directory is/home/gium/android_src/vendor/ctop/modules/gpio/
In this way, the include header file can be written as include <gpio. h>

The following section describes how Java calls JNI.

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.