ArticleDirectory
- I. Use Ubuntu Linux to test the Linux driver
- 2. Test the Linux driver through Native C program on the android Simulator
Develop an android driver that can count words
In the previous article, a simple Linux driver has been implemented.ProgramThe function of this driver is to count the number of words in a given string. In additionSource codeCompiled into the dynamic Linux driver module word_count.ko. The next step is to test this module. There are many testing methods. The most common method is to directly test in Ubuntu Linux and Android simulators. Of course, this is no problem for the Linux driver implemented in this chapter, but it is not convenient to test the driver that requires direct access to the hardware on Ubuntu Linux. In this case, you need to test the corresponding hardware.
For a Linux driver, you can perform preliminary development and testing on ubuntulinux at the beginning. You can use software to simulate the hardware access in Ubuntu Linux. After the basic development is complete, you need to use real hardware on the Development Board or Engineering Prototype for testing. Of course, we also need to test the final sale of mobile phones. After the final test is passed, the Linux driver can be truly developed. An important step in the development of the Linux driver is testing. This section describes how to test the Linux driver on different platforms based on the actual development process. These testing platforms include ubuntulinux, Android simulators, And the cloud6410 Development Board.
I. Use Ubuntu Linux to test the Linux driver
This section describes how to test the driver in Ubuntu Linux. The Linux Driver written in the previous section returns the number of words from the device file (/dev/wordcount) in four bytes, therefore, you cannot use the cat command to test the driver (the cat command will not restore the four bytes to the int type value for display ). However, you can use the following command to view the number of words in the log.
# Sh build. Sh # echo 'I love you.'>/dev/wordcount # dmesg
After the above command is executed, if the information in the white box shown in 6-13 is output, the driver has successfully counted the number of words.
Although the Linux driver can be tested using echo and dmesg commands, this method is not a real test. In order to bring the test effect closer to the real environment, a program dedicated for testing is generally required. This section will compile a dedicated test program (test_word_count.c) for the word_count driver ). Test_word_count.c interacts with the word_count driver by directly operating the/dev/wordcount device file. Testing ProgramCodeAs follows:
# Include <stdio. h> # include <fcntl. h> # include <unistd. h> # include <stdlib. h> # include <string. h> int main (INT argc, char * argv []) {int testdev; // open the device file (/dev/wordcount) handle unsigned char Buf [4]; // 4 bytes of the number of words // open the device file testdev = open ("/dev/wordcount", o_rdwr); // if the open function returns-1, failed to open the device file if (testdev =-1) {printf ("cann't open file \ n"); Return 0 ;} // If test_word_count is followed by a command line parameter, the program treats 1st parameter values as the string to be counted. // if no command line parameter exists, read Only the values in the device file if (argc> 1) {// write the string to be counted into the device file write (testdev, argv [1], strlen (argv [1]); // outputs the string printf ("string: % s \ n", argv [1]) to be counted;} // read the number of words in the device file (4 bytes) read (testdev, Buf, 4); int n = 0; // Number of words // restore four bytes to the int type value n = (INT) BUF [0]) <24 | (INT) BUF [1]) <16 | (INT) BUF [2]) <8 | (INT) BUF [3]); // output the 4-byte value printf ("word byte display: % d, % d \ n ", buf [0], Buf [1], Buf [2], Buf [3]); // outputs the number of statistical words printf ("word count: % d \ n ", n); // close the device file close (testdev); Return 0 ;}
The test_word_count program can have one command line parameter (multiple command line parameters only use 1st command line parameters ). If the command line parameter value contains spaces, you must use single quotation marks (') or double quotation marks (") to enclose the parameter values. You can use the following command to test the word_count driver.
# GCC test_word_count.c-O test_word_count # test_word_count "I love you ."
After the above command is executed, if the information shown in 6-14 is output (assuming that word_count has counted a string containing four words before), The word_count driver is successfully tested.
2. Test the Linux driver through Native C program on the android Simulator
Although we are developing a Linux driver, this book mainly introduces the android Linux kernel. Therefore, if the Linux driver is successfully tested only on Ubuntu Linux, it cannot be guaranteed that the driver works properly on the Android device. Therefore, the driver must be tested on the Android device. There are many types of Android devices, such as Android development boards and mobile phones or tablets running Android systems. But not these hardware devices, but Android simulators, are closest to us. The android simulator can simulate the vast majority of real environments. Therefore, the android simulator can be used to test the Linux kernel.
To test the Linux driver on the android simulator, you should first consider installing the word_count.ko driver module on the simulator. You may have used the ADB shell command. If the command prompt of the android simulator is "#", it means that the root permission is directly used to access the android simulator through the command line (the command prompt is "$", indicating that the root permission is not used ), therefore, theoretically, the word_count.ko driver module can be directly installed in the android simulator using the insmod command. Now let's test whether word_count.ko can be installed on the android simulator. Run the build. Sh script and select "android Simulator". The script automatically uploads the word_count.ko file to the/data/local directory of the android simulator and installs it. If the reader chooses a Development Board of, the following error message will be output when word_count.ko is installed, indicating that the Linux kernel version for compiling the Linux driver is different from the current Android Simulator version and cannot be installed. Therefore, when compiling the Linux driver, you must select the same Linux kernel version as the currently running Linux kernel. Otherwise, the Linux driver cannot be installed.
Insmod: init_module '/data/local/word_count.ko' failed (function not implemented)
Note: When uploading a file to the android simulator or development board, we recommend that you put the file in the/data/local directory. Many other directories in the system, such as/system/bin, are read-only, unless you package the word_count.ko file into system. IMG. Otherwise, you cannot write data to these directories, even if you have the root permission.
By default, the goldfish kernel for Android simulators does not allow dynamic loading of Linux driver modules. Therefore, you need to execute the following command to configure the Linux kernel before compiling the Linux kernel.
# Cd ~ /Kernel/goldfish # Make menuconfig
After the preceding command is executed, the setting interface shown in 6-15 is displayed. Press the Space key to select the second item "enable loadable module support" (In front of [*]), press "enter" to enter the sub-menu, and select the first three items, as shown in 6-16. Otherwise, the Linux driver module cannot be installed or uninstalled. Keep the settings when you exit the settings menu. Finally, recompile the Linux kernel as described in section 4.2.3,
After the kernel is compiled successfully, the android simulator can dynamically load the Linux driver module using the newly generated zimage Kernel File.
Execute build now. the sh script file completes compilation, upload, and installation of the word_count driver, and then enters the android simulator terminal. You can use the echo and dmesg commands to test the word_count driver and view the test results, the method is the same as in the previous section.
Note: To compile a Linux driver that can be run on the android simulator, use the goldfish kernel and use other Kernels to compile word_count.c. the following error occurs during installation.
Insmod: Error inserting 'word _ count. Ko ':-1 invalid module format
On the android simulator, you can not only use Linux commands to test the driver, but also use a local C/C ++ program like ubuntulinux. Some readers may ask, isn't Android only able to run APK programs compiled by Java? At most, the ndk code is embedded in the APK program. Can normal Linux programs be run directly? The answer is yes. However, the following two conditions must be met.
1. The root permission is required for Android simulators, development boards, or mobile phones.
2. executable files must be compiled using a cross compiler to support arm processors.
Now we use a cross compiler to compile the test_word_count.c file written in the previous section. To make the compilation steps as simple as possible, we use Android. mk to set the number of compilation parameters and use the make command to compile. First, create an android. mk file in the/root/Drivers/ch06/word_count directory and enter the content as shown in.
Local_path: = $ (call my-DIR) include $ (clear_vars) # specify the source code file local_src_files: = test_word_count.c # specify the module name, it is also the executable file name generated after compilation: local_module: = test_word_count local_module_tags: = optional include $ (build_executable)
The android. mk file must be described in the following two aspects.
Local_module_tags
Indicates the mode in which the current project (directory of the android. mk file) is compiled. If it is set to optional, the mode is not taken into account, that is to say, it will be compiled in any mode. This variable can be set to user, userdebug, Eng, optional. Eng is the default value.
1. User: restrict users' access to the Android system. It is suitable for publishing products.
2. userdebug: similar to the user mode, but has root access permissions and can obtain a large amount of debugging information from logs.
3. Eng: This mode is usually set during development. In addition to all the functions of userdebug, there will also be a large number of debugging tools.
The value of local_module_tags is related to the target_build_variant variable. The target_build_variant variable is used to set the current compilation mode. The values that can be set include user, userdebug, and ENG. To change the compilation mode, run the following command before compiling the android source code.
# Export target_build_variant = user
Or use the lunch command to set the compilation mode.
# Lunch full-Eng
In this example, "full" indicates the set-up goal. In addition to the full target (set up for all platforms), there is a full-x86 dedicated for x86. Detailed setting of the target will be listed after running the lunch command. As shown in Figure 4-8, the compilation Mode Supported by android4 is displayed. You can go to Chapter 2 to view the chart.
Include $ (build_executable)
Build_executable indicates creating executable files. The executable file path is <Android source code directory>/out/target/product/generic/system/bin/test_word_count. To compile the file into a dynamic library (. So), use include $ (build_shared_library ). The path of the dynamic library is <Android source code directory>/out/target/product/generic/system/lib/test_word_count.so. To compile a static library (. a) file, you can use include $ (build_static_library ). The path of the static library is <Android source code directory>/out/target/product/generic/obj/static_libraries/test_word_count_intermediates/test_word_count.
To compile the test_word_count.c file into an executable program that can be run on the android simulator, copy the word_count directory to a subdirectory of <Android source code directory>, you can also create a symbolic link for the word_count directory in the <Android source code directory>. For convenience, run the following command to create a symbolic link for the word_count directory in the <Android source code directory>/Development Directory (assume that the android source code directory is/sources/Android/android4/development/word_count ).
# Ln-S/root/Drivers/ch06/word_count/sources/Android/android4/development/word_count
Now go to the/sources/Android/android4 directory and execute the following command to initialize the compilation command.
# Source./build/envsetup. Sh
You can use the following two methods to compile test_word_count.c.
1. Enter the/sources/Android/android4/development/word_count directory and execute the following command.
# Mm
2. Run the following command in the/sources/Android/android4 directory.
# Mmm development/word_count
After compilation, you can find the test_word_count file in the <Android source code directory>/out/target/product/generic/system/bin directory. The compiled test_word_count Program (including emulator and Ubuntu Linux) has been included in the CD and simulation environment. The executable program generally does not need to consider the Linux kernel version, programs that support arm processors compiled by the cross compiler can be run on the android simulator, or on the cloud6410 Development Board or other mobile phones with root permissions.
Emulator version path
CD:<CD root directory>/sources/ch06/word_count/emulator/test_word_count
Simulated environment:/Root/Drivers/ch06/word_count/emulator/test_word_count
Path of Ubuntu Linux
CD:<CD root directory>/sources/ch06/word_count/Ubuntu/test_word_count
Simulated environment:/Root/Drivers/ch06/word_count/Ubuntu/test_word_count
Run the following command to upload the test_word_count file to the android simulator.
# ADB push./emulator/test_word_count/data/local
Then go to the android simulator terminal and run the following command to test the word_count Driver (you must first use the CHMOD command to set the executable permissions of test_word_count ).
# Chmod 777/data/local/test_word_count
#/Data/local/test_word_count
#/Data/local/test_word_count 'a bb ccc ddd eee'
After the preceding command is executed, if the number of words output is 5, the program test is successful.
Test the Linux driver using Android ndk and Java
This article is excerptedAndroid Deep Exploration (Volume 1): Hal and driver developmentThe next several articles will elaborate on how to develop the Linux driver of the ARM architecture, and test the Linux driver by using the android program, ndk, and executable files respectively. Can be in Ubuntu Linux, Android simulator and cloud6410 Development Board (you can buy OK6410-A Development Board, need to brush Android)