Test the Linux kernel driver for the Android system with built-in C executable program on Ubuntu

Source: Internet
Author: User

In the previous article, we introduced how to write the Linux kernel driver for the Android system on Ubuntu. In the Linux kernel driver named hello, create three different file nodes for user space access, these are the traditional device files/dev/Hello, Proc system files/proc/Hello, And devfs system property files/sys/class/Hello/Val. In addition, the cat command is used to directly access the/proc/hello and/sys/class/Hello/Val files to verify the correctness of the driver. In this article, we will use a self-compiled C executable program to access the device file/dev/hello. Readers may wonder how to write applications in the Android system using C language? Aren't all applications on Android systems Java applications? Actually, you can use the ADB shell command to connect to the android simulator. In the/system/bin directory, you can see a lot of C executable programs, such as CAT commands. Today, let's learn how to add executable programs written in C language to the Android system.

1. Prepare the Linux driver by referring to compiling the Linux kernel driver for the Android system on Ubuntu. Use the android simulator to load the kernel file containing the Linux driver, and run the ADB shell command to simulate the connection to verify that the device file Hello exists in the/dev directory.

2. Go to the external directory of the android source code project and create the hello directory:

User-name @ machine-Name :~ /Android $ CD external

User-name @ machine-Name :~ /Android/external $ mkdir hello

III.Create a 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 the role of this program, open the/dev/Hello file, first read the value in the/dev/Hello file, and then write the value 5 to/dev/hello, finally, read the value in the/dev/Hello file again to see if it is the value 5 we just wrote. The read and write values from/dev/Hello files are actually the values of the virtual hardware register Val.

4. Create an 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 indicates that we want to compile executable programs. 

5. Refer to the article on how to separately compile the modules in the android source code and use the mmm command for compilation:

User-name @ machine-Name :~ /Android $ mmm./external/Hello

After the compilation is successful, you can see the executable file hello in the out/target/product/gerneric/system/bin directory.

6. repackage the Android system file system. IMG:

User-name @ machine-Name :~ /Android $ make Snod

In this way, the system. imgfile after repackaging contains the compiled Hello executable file.

7. Run the simulator 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.

When we see this result, we can write a C executable program to access the Linux kernel driver.

After introducing how to use an executable program written in C language to access our Linux kernel driver, the reader may ask, can you provide a Java interface in Android Application Frameworks to access the Linux kernel driver? Yes. In the following articles, we will introduce how to add a Java interface in Android Application Frameworks to access the Linux kernel driver.

Lao Luo's Sina Weibo: http://weibo.com/shengyangluo. welcome to the attention!

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.