In the previous article, we described how to write Linux kernel drivers for the Android system on Ubuntu. In this Linux kernel driver named Hello, three different file nodes are created for user space access, 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 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 the C executable program that we have written ourselves. Perhaps readers will find it strange how to write apps in C on Android. Aren't apps on the Android system all Java applications? In fact, readers may wish to use the ADB shell command to connect to the Android simulator, in the/system/bin directory can see a lot of C executable programs, such as Cat commands. Today, we're going to learn how to add an executable program written in C to the Android system.
A. Refer to the article on how to write Linux kernel driver implementations on the Ubuntu Android system, and prepare the Linux drivers . Use the Android emulator to load kernel files containing this Linux driver, and simulate using the ADB shell command connection to verify that there is a device file in the/dev directory hello.
Two. Enter the external directory of the Android source code to create the Hello directory:
user-name@machine-name:~/android$ CD External
user-name@machine-name:~/android/external$ mkdir Hello
Three. Create a new hello.c file in the Hello directory:
#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;
In this program, open the/dev/hello file, read the value in the/dev/hello file, write the value 5 to/dev/hello, and read the/dev/hello file again to see if the value we just wrote is 5. The value read and write from the/dev/hello file is actually the value of the register Val of 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: Build_executable says we're compiling an executable program.
Five. Refer to how to compile the module in the Android source code separately, using the MMM command to compile:
user-name@machine-name:~/android$ mmm./external/hello
After the compilation is successful, you can see the executable file in the Out/target/product/gerneric/system/bin directory hello.
Six. Repackage the Android system file System.img:
user-name@machine-name:~/android$ make Snod
This way, the repackaged system.img file contains the hello executable file that you just compiled.
Seven. Run the emulator and use the/system/bin/hello executable program to access the Linux kernel driver:
user-name@machine-name:~/android$ emulator-kernel./kernel/common/arch/arm/boot/zimage &
user-name@machine-name:~/android$ adb Shell
root@android:/# CD System/bin
Root@android:/system/bin #./hello
Read the original value:
0.
Write value 5 To/dev/hello.
Read The value again:
5.
Seeing this result, we wrote the C executable program to access the Linux kernel driver we wrote.
After describing how to use the executable program written in C to access our Linux kernel drivers, readers may ask if you can access the Linux kernel driver by providing a Java interface to the Android application frameworks. OK, in the next few articles, we'll look at how to add Java interfaces to the Android application frameworks to access Linux kernel drivers.
Follow-up to this part of the information, thank you for your support this site!