Test Linux kernel drivers for Android built-in C executable program on Ubuntu

Source: Internet
Author: User

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
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #define DEVICE_NAME "/dev/hello"
  5. int main (int argc, char** argv)
  6. {
  7. int fd =-1;
  8. int val = 0;
  9. FD = open (Device_name, O_RDWR);
  10. if (fd = =-1) {
  11. printf ("Failed to open Device%s.\n", device_name);
  12. return-1;
  13. }
  14. printf ("Read original value:\n");
  15. Read (FD, &val, sizeof (Val));
  16. printf ("%d.\n\n", Val);
  17. val = 5;
  18. printf ("Write value%d to%s.\n\n", Val, device_name);
  19. Write (FD, &val, sizeof (Val));
  20. printf ("Read the Value again:\n");
  21. Read (FD, &val, sizeof (Val));
  22. printf ("%d.\n\n", Val);
  23. Close (FD);
  24. return 0;
  25. }

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

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.