Article reprinted to CSDN community Luo Shenyang's Android tour, original address: http://blog.csdn.net/luoshengyang/article/details/6571210
In the previous article, we described how to write a Linux kernel driver for an Android system on Ubuntu. In this Linux kernel driver called Hello, create three different file nodes for user space access, namely traditional device file/dev/hello, proc system file/proc/hello and DEVFS System Properties file/sys/class/ Hello/hello/val. Further, the/proc/hello and/sys/class/hello/hello/val files are also accessed directly through the Cat command to verify the correctness of the driver. In this article, we will access the device file/dev/hello through our own C executable program. Perhaps the reader will find it strange, how can you use C language to write applications in the Android system? Aren't applications on Android systems all Java applications? In fact, the reader may wish to use the ADB shell command to connect the Android emulator, in the/system/bin directory can see a lot of C executable programs, such as the Cat command. Today, let's learn how to add executable programs written in C in the Android system.
I. Refer to the article on writing Linux kernel drivers for Android systems on Ubuntu, and prepare the Linux drivers. Use the Android emulator to load the kernel file that contains this Linux driver, and use the ADB shell command to connect to the simulation to verify that the device file Hello exists in the/dev directory.
Two. Enter the external directory of the Android source code project to create the Hello directory:
[email protected]:~/android$ cd External
[Email protected]:~/android/external$ mkdir Hello
three. Create a new hello.c file in the Hello directory:
[CPP]View Plaincopy
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #define DEVICE_NAME "/dev/hello"
- int main (int argc, char** argv)
- {
- int fd =-1;
- int val = 0;
- FD = open (Device_name, O_RDWR);
- if (fd = =-1) {
- printf ("Failed to open Device%s.\n", device_name);
- return-1;
- }
- printf ("Read original value:\n");
- Read (FD, &val, sizeof (Val));
- printf ("%d.\n\n", Val);
- val = 5;
- printf ("Write value%d to%s.\n\n", Val, device_name);
- Write (FD, &val, sizeof (Val));
- printf ("Read the Value again:\n");
- Read (FD, &val, sizeof (Val));
- printf ("%d.\n\n", Val);
- Close (FD);
- return 0;
- }
The role of this program, open the/dev/hello file, and then read out the values in the/dev/hello file, then write the value 5 to/dev/hello, and finally read out the values in the/dev/hello file again to see if we have just written the value of 5. The value read and write from the/dev/hello file is actually the value of the register Val for our virtual hardware.
Four. Create a new android.mk file in the Hello directory:
Local_path: = $ (call My-dir)
Include $ (clear_vars)
Local_module_tags: = Optional
Local_module: = Hello
Local_src_files: = $ (call all-subdir-c-files)
Include $ (build_executable)
Note that build_executable indicates that we are compiling an executable program.
Five. Refer to the article on how to compile the module in the Android source code separately, compile with the MMM command:
[email protected]:~/android$ mmm./external/hello
Once the compilation is successful, you can see the executable hello in the Out/target/product/gerneric/system/bin directory.
Six. RePack Android system files System.img:
[email protected]:~/android$ make Snod
This way, the repackaged system.img file contains the hello executable file that was just compiled.
Seven. Run the emulator and use the/system/bin/hello executable to access the Linux kernel driver:
[Email protected]:~/android$ emulator-kernel./kernel/common/arch/arm/boot/zimage &
[Email protected]:~/android$ adb shell
[Email protected]:/# CD System/bin
[Email protected]:/system/bin #./hello
Read the original value:
0.
Write value 5 To/dev/hello.
Read The value again:
5.
Seeing this result, we wrote that our C executable program could access the Linux kernel driver we wrote.
After introducing how to use the executable program written in C to access our Linux kernel drivers, the reader may ask, can you provide the Java interface on Android application frameworks to access the Linux kernel driver? Yes, in the next few articles, we'll show you how to add Java interfaces to the Linux kernel driver in Android's application frameworks.
Lao Luo's Sina Weibo: Http://weibo.com/shengyangluo, welcome attention!
Test Linux kernel drivers for Android built-in C executable program on Ubuntu