Use SOCKET in Android to enable native and framework communication

Source: Internet
Author: User

Generally, the communication between native and framework is through JNI, but this is generally only the framework that calls native. How can we notify the upper layer of native if there is a message? The GSP module in Android provides a solution, but its implementation is somewhat complicated. Here we introduce a socket communication method to allow free communication between native and framework. The specific implementation is as follows:
In Android, JNI is used to encapsulate sockets in Linux. It is very convenient to use.
Because Android is based on Linux, the Linux code will be executed before java. Therefore, the native end is generally a server. The framework is a client.
Main Code of the Java layer:

LocalSocket s =null;LocalSocketAddress l;s = new LocalSocket();l = new LocalSocketAddress(SOCKET_NAME,LocalSocketAddress.Namespace.RESERVED);s.connect(l);

At this time, if there is no problem with the socket connection, it will be able to read and write normally.
Native layer main code:

s_fdListen = android_get_control_socket(SOCKET_NAME);ret = listen(s_fdListen, n);s_fdCommand = accept(s_fdListen, (sockaddr *) &peeraddr, &socklen);

If the connection is correct, you can use Write/read in Linux to read and write the socket;
The value of socket_name is a string defined in init. RC. In other words, we can apply for the required socket resources by modifying init. RC.
Here we use RIL as an example to describe:

service ril-daemon /system/bin/rildsocket rild stream 660 root radiosocket rild-debug stream 660 radio systemuser rootgroup radio cache inet misc audio

The above is an excerpt from System \ core \ rootdir \ init. RC in the Android 2.2 source code. For more information, see the init. C and system/CORE/init/readme.txt files. The role of the init. C to parse init. RC, and start a daemon named rild for us. It is an executable program. We can find the corresponding rild file in system/bin through the ADB shell. Socket indicates that a socket resource is allocated to the daemon, which can be found under/dev/socket. That is to say, the most important part of this article is whether the socket can communicate with each other and whether the daemon process can work well. The above socket_name is the string defined here (in RIL. Java and rIL. cpp, There Is A String constant socket_name_ril whose value is rild, which corresponds to the above ).
If we want to customize a socket for communication, we can add

service myserver-daemon /system/bin/serversocket server stream 666 oneshot

System/bin/server is the server program compiled by us, in which we call

s_fdListen = android_get_control_socket(“server”);ret = listen(s_fdListen, n);s_fdCommand = accept(s_fdListen, (sockaddr *) &peeraddr, &socklen);

You can create a server program.
Java only needs to use the top code to communicate with native. Note that the value of socket_name must be the same as that in init. Rc. Here it is "rild ". The oneshot here must be available. If not, your server may not be able to start.
Only compilation is left.
For compilation, refer to Android. mk, rild. C, and rIL. cpp in RIL and pick out the header file.
Compile your own modules with MM. After compilation, test the added modules and make Snod in the root directory of the source code. Add the compiled output file to system. IMG. Finally, copy system. IMG and randisk. IMG to the platform corresponding to the SDK. You can. The two imgfiles must be copied. system. IMG contains your executable program, while randisk. IMG contains your init. RC. Userdata. IMG is not sure.
In this case, you only need to write a client program in Java.

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.