This blog post to be written today is of great significance and is very rare on the internet, a technique that I learned in my work, and of course it is a combination of simple problems. How to write C language program in wirelessly, debug Android driver, test program of an important skill, below I will not say nonsense, directly say practical, how to use this thing.
On this issue, I believe that a lot of Android will pay attention to this problem, we generally have a question: How does Android run?
In the simplest case, the Android system is loaded like this:
Bootloader------Kernel (the Linux kernel corresponding to the platform version)------FileSystem file System (this is Android)
Therefore, Android is not an operating system, but a Linux kernel-based file system.
So, what we're talking about in the title, we're going to run our program at boot time without choosing to get into Android, so how do we do that? First, you need to understand a problem, how filesystem is started.
Information from the Internet and some books about Andorid systems, The file system is loaded from the beginning of the init.rc, to create the directory required for the file system, as well as to these directories plus permissions, copy the relevant programs, wait for the operation, the most important, a file system to run the need to load these services can be implemented. As for how to load, the principle I will not say, a long story, recommend you can search init.rc,init.c such words can understand.
What we're talking about now is how to implement the bin file without getting into Android.
1, in order to distinguish between my service and other services, my service is independent of a on to trigger.
On Yyxclass_start coreservice Pppservice/system/bin/pppreboot class core user root group root OneShot
I wrote this service name is called Pppservice, the service corresponding to the need to execute the bin file is Pppreboot, the implementation is a tablet boot print statement and then restart, has been repeated, so that will not enter the Android file system, but also do not see our normal use of the Android UI interface, But a dense one, but you can see the debug information output on the serial port, of course, if you understand the driver application programming, you can at this time to draw something on the screen, do your own interface, is actually writing C or C + + program, is to write Linux applications.
2. The source code is as follows (location: EXTERNAL/TEST/PPP.C)
#include <stdio.h> #include <stdlib.h> #include <android/log.h> #define LOGD (...) __android_log_ Print (Android_log_debug, "Keymatch", __va_args__) int main (void) {int i; Freopen ("/dev/ttymt0", "a", stdout); Setbuf (stdout, null); Freopen ("/dev/ttymt0", "a", stderr); Setbuf (stderr, NULL); LOGD ("YYX---->REBOOT SYSTEM!!!!!! --->201612.1\n "); System (" reboot "); return 0;}
The corresponding android.mk
local_path:=$ (call My-dir) include $ (clear_vars) Local_module_tags: = englocal_ldlibs: =-lloglocal_src_files:= ppp.clocal_module:= Ppprebootinclude $ (build_executable) #include $ (build_shared_library)
1. Compile the C program, compile it with the command mm or MMM, compile the generated pppreboot in the system/bin/directory of the Android file system that we boot.
2. Call the trigger command in init.rc to trigger this on yyx, as long as the appropriate position plus: trigger Yyx can be
3. Compilation: Make–j8
Download the corresponding file to the Android tablet, and you will see that the tablet can be printed and the information can be restarted continuously.
If this does not add:
Freopen ("/dev/ttymt0", "a", stdout); Setbuf (stdout, NULL);
Freopen ("/dev/ttymt0", "a", stderr); Setbuf (stderr, NULL);
You will not see any output information on the serial port of Android, because the serial port has already done this step setting in Init.cpp, see the System/core/init/init.cpp code for details:
Open_devnull_stdio ();
The purpose of this sentence is to redirect the standard input output from Android to/dev/null, which is known to be known as an empty file of Linux, which can receive information about everything, like a trash bin. But there is a characteristic, it is only not into the.
Therefore, we write in the C program or C + + programs need to redirect to the standard input and output, the role of Freopen is like this, you should also pay attention to the port of your board output device node is what, my is/dev/ttymt0, it is possible that you are not the same as me.
About this application, the scope of application, some PCBA test tools can be implemented at this time, or manual OTA update, etc., the application is very wide, need everyone in the work of the one who can be born skillfully.
Write C language Program on Android system--Boot The program does not enter the Android system