如借用其它人员研究的wifi层次,对比android 下载后的源码。可看到android一层一层的架构层次是怎么样的。
并了解一个android系统是怎么跑起来的。
所以平时我的的android开发大部分是在java应用层,一些深层次点的就是ndk编码,会通过进行ndk编码。如proxydroid.
他的代理功能其实就是用到了ndk,通过jndi接口,调用到一个c++层,然后c++层次可以用到一些c++的类库,会fork一个后台进程
去进行iptable的代理。
如执行下述命令
iptables -t nat -A OUTPUT -p tcp --dport 80 -j DNAT --to-destination 127.0.0.1:8123
会用过jndi调用 c++的
android_os_Exec_createSubProcess方法
最后调用 create_subprocess方法
static int create_subprocess(const int rdt, const char *cmd, char *const argv[],
char *const envp[], const char* scripts, int* pProcessId)
{
pid_t pid;
int pfds[2];
int pfds2[2];
pipe(pfds);
if (rdt) {
pipe(pfds2);
}
pid = fork();
if(pid < 0) {
LOGE("- fork failed: %s -\n", strerror(errno));
return -1;
}
if(pid == 0){
signal(SIGPIPE, SIG_IGN);
if (envp) {
for (; *envp; ++envp) {
putenv(*envp);
}
}
dup2(pfds[0], 0);
close(pfds[1]);
if (rdt) {
close(1);
close(2);
dup2(pfds2[1], 1);
dup2(pfds2[1], 2);
close(pfds2[0]);
}
execv(cmd, argv);
fflush(NULL);
exit(0);
} else {
signal(SIGPIPE, SIG_IGN);
*pProcessId = (int) pid;
dup2(pfds[1], 1);
close(pfds[0]);
write(pfds[1], scripts, strlen(scripts)+1);
if (rdt) {
close(pfds2[1]);
return pfds2[0];
} else {
return -1;
}
}
}
Wifi System:
Java应用层
Java Framework层
JNI层
适配器层
wpa_supplicant程序
Kernel层
Java应用层
-- Settings, WifiSwitcher等应用
--> /system/app
Java Framework层
-- frameworks/base/wifi/java/android/net/wifi/* (android.net.wifi)
-- frameworks/base/services/java/com/android/server/WifiService.java
frameworks/base/services/java/com/android/server/WifiWatchdogService.java
--> /system/framework/framework.jar
/system/framework/services.jar
JNI层
-- frameworks/base/core/jni/android_net_wifi_Wifi.cpp
--> /system/lib/libandroid_runtime.so
适配器层
-- hardware/libhardware_legacy/wifi/wifi.c
--> /system/lib/libhardware_legacy.so
wpa_supplicant程序
-- external/wpa_supplicant
-- external/wpa_supplicant_6
--> /system/bin/wpa_supplicant
/system/lib/libwpa_client.so
Kernel层
-- kernel wifi driver
Enable Wifi 的过程:
Settings/WifiEnabler.java
mWifiManager.setWifiEnabled(enable);
-->
frameworks/base/wifi/java/android/net/wifi/WifiManager.java
public boolean setWifiEnabled(boolean enabled) {
mService.setWifiEnabled(enabled);
}
-->
frameworks/base/services/java/com/android/server/WifiService.java
public boolean setWifiEnabled(boolean enable) {
sendEnableMessage(enable, true, Binder.getCallingUid());
}
->
setWifiEnabledBlocking() {
if (enable) {
mWifiStateTracker.loadDriver();
mWifiStateTracker.startSupplicant();
} else {
mWifiStateTracker.stopSupplicant();
mWifiStateTracker.unloadDriver();
}
}
-->
frameworks/base/wifi/java/android/net/wifi/WifiStateTracker.java
public synchronized boolean loadDriver() {
return WifiNative.loadDriver();
}
public synchronized boolean startSupplicant() {
return WifiNative.startSupplicant();
}
-->
frameworks/base/wifi/java/android/net/wifi/WifiNative.java
public native static boolean loadDriver();
public native static boolean startSupplicant();
-->
frameworks/base/core/jni/android_net_wifi_Wifi.cpp
static jboolean android_net_wifi_loadDriver(JNIEnv* env, jobject clazz)
{
return (jboolean)(::wifi_load_driver() == 0);
}
static jboolean android_net_wifi_startSupplicant(JNIEnv* env, jobject clazz)
{
return (jboolean)(::wifi_start_supplicant() == 0);
}
-->
hardware/libhardware_legacy/wifi/wifi.c
int wifi_load_driver(){
//for build in driver, do nothing
//for .ko driver, insmod/load firmware...
}
int wifi_start_supplicant(){
property_set("ctl.start", "wpa_supplicant");
}
-->
/init.rc
service wpa_supplicant /system/bin/wpa_supplicant ...