隨著Android安卓手機風(fēng)靡全球,Android安卓手機成為日常生活的一部分,今天筆者同大家一起來了解一下,Android安卓手機開機過程中無線模塊的初始化過程;如果sim卡鎖開啟,或者pin被鎖住的時候,會要求輸入pin或者puk,但是這個解鎖動作必須在系統(tǒng)初始化完成以后才能進(jìn)行。(圖形系統(tǒng)都還沒有初始化怎么輸入密碼阿?)當(dāng)系統(tǒng)初始化完成以后會調(diào)用 wm.systemReady()來通知大家。這時候該做什么就做什么。
開機過程中無線模塊的初始化過程:
rild 調(diào)用參考實現(xiàn) Reference-ril.c (hardware\ril\reference-ril) 中的函數(shù):
const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
static void *mainLoop(void *param)
ret = at_open(fd, onUnsolicited);
RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
在 initializeCallback 函數(shù)中對貓進(jìn)行了初始化。
static void initializeCallback(void *param)
{
ATResponse *p_response = NULL;
int err;
setRadioState (RADIO_STATE_OFF);
at_handshake();
/* note: we don't check errors here. Everything important will
be handled in onATTimeout and onATReaderClosed */
/* atchannel is tolerant of echo but it must */
/* have verbose result codes */
at_send_command("ATE0Q0V1", NULL);
/* No auto-answer */
at_send_command("ATS0=0", NULL);
/* Extended errors */
at_send_command("AT+CMEE=1", NULL);
/* Network registration events */
err = at_send_command("AT+CREG=2", &p_response);
/* some handsets -- in tethered mode -- don't support CREG=2 */
if (err < 0 || p_response->success == 0) {
at_send_command("AT+CREG=1", NULL);
}
at_response_free(p_response);
/* GPRS registration events */
at_send_command("AT+CGREG=1", NULL);
/* Call Waiting notifications */
at_send_command("AT+CCWA=1", NULL);
/* Alternating voice/data off */
at_send_command("AT+CMOD=0", NULL);
/* Not muted */
at_send_command("AT+CMUT=0", NULL);
/* +CSSU unsolicited supp service notifications */
at_send_command("AT+CSSN=0,1", NULL);
/* no connected line identification */
at_send_command("AT+COLP=0", NULL);
/* HEX character set */
at_send_command("AT+CSCS=\"HEX\"", NULL);
/* USSD unsolicited */
at_send_command("AT+CUSD=1", NULL);
/* Enable +CGEV GPRS event notifications, but don't buffer */
at_send_command("AT+CGEREP=1,0", NULL);
/* SMS PDU mode */
at_send_command("AT+CMGF=0", NULL);
#ifdef USE_TI_COMMANDS
at_send_command("AT%CPI=3", NULL);
/* TI specific -- notifications when SMS is ready (currently ignored) */
at_send_command("AT%CSTAT=1", NULL);
#endif /* USE_TI_COMMANDS */
/* assume radio is off on error */
if (isRadioOn() > 0) {
setRadioState (RADIO_STATE_SIM_NOT_READY);
}
}
默認(rèn)狀況下假設(shè)射頻模塊是好的,
通過 setRadioState (RADIO_STATE_SIM_NOT_READY) 來觸發(fā)對無線模塊的初始化。
通過 static void onRadioPowerOn() 對無線模塊初始化。
首先通過 pollSIMState(NULL); 輪詢 sim卡狀態(tài) 。
static void pollSIMState (void *param)
{
ATResponse *p_response;
int ret;
if (sState != RADIO_STATE_SIM_NOT_READY) {
// no longer valid to poll
return;
}
switch(getSIMStatus()) {
case RIL_SIM_ABSENT:
case RIL_SIM_PIN:
case RIL_SIM_PUK:
case RIL_SIM_NETWORK_PERSONALIZATION:
default:
setRadioState(RADIO_STATE_SIM_LOCKED_OR_ABSENT);
return;
case RIL_SIM_NOT_READY:
RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
return;
case RIL_SIM_READY:
setRadioState(RADIO_STATE_SIM_READY);
return;
}
}
讀取sim卡狀態(tài)的函數(shù)是:getSIMStatus()
err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
它向貓發(fā)送了at命令 AT+CPIN? 來查詢無線模塊的狀態(tài),如果無線模塊還沒有就緒,那么他隔1秒鐘繼續(xù)調(diào)用
sim卡狀態(tài)輪詢函數(shù) pollSIMState,直到獲得sim卡狀態(tài)。
當(dāng)sim卡狀態(tài)為就緒,那么通過 setRadioState(RADIO_STATE_SIM_READY) 設(shè)置變量 sState 為:
RADIO_STATE_SIM_READY,這時候會調(diào)用函數(shù) static void onSIMReady()來進(jìn)一步初始化無線模塊。
發(fā)送的at命令有:
at_send_command_singleline("AT+CSMS=1", "+CSMS:", NULL);
at_send_command("AT+CNMI=1,2,2,1,1", NULL);
如果sim卡鎖開啟,或者pin被鎖住的時候,會要求輸入pin或者puk,但是這個解鎖動作必須在系統(tǒng)初始化完成以后才能
進(jìn)行。(圖形系統(tǒng)都還沒有初始化怎么輸入密碼阿?)當(dāng)系統(tǒng)初始化完成以后會調(diào)用 wm.systemReady()來通知大家。
這時候該做什么就做什么。
wm.systemReady()的調(diào)用會觸發(fā)解鎖界面。具體流程如下:
因為有: WindowManagerService wm = null;所以 wm.systemReady()
調(diào)用的是 WindowManagerService 中的函數(shù):
public void systemReady() {
mPolicy.systemReady();
}
WindowManagerService 中有:
final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
PolicyManager.makeNewWindowManager 調(diào)用的是文件 PolicyManager.java 中的函數(shù):
public static WindowManagerPolicy makeNewWindowManager() {
return sPolicy.makeNewWindowManager();
}
sPolicy.makeNewWindowManager 調(diào)用的是文件 Policy.java 中的函數(shù):
public PhoneWindowManager makeNewWindowManager() {
return new PhoneWindowManager();
}
因為 PhoneWindowManager 繼承自 WindowManagerPolicy
所以 mPolicy.systemReady() 最終調(diào)用的是文件 PhoneWindowManager.java 中的函數(shù):
public void systemReady()
mKeyguardMediator.onSystemReady();
doKeyguard();
showLocked();
Message msg = mHandler.obtainMessage(SHOW);
mHandler.sendMessage(msg);
發(fā)送 SHOW 的消息。
文件 KeyguardViewMediator.java 中的消息處理函數(shù):
public void handleMessage(Message msg) 對 SHOW 消息進(jìn)行了處理。
如果 msg.what 等于 SHOW 那么執(zhí)行:
handleShow();
private void handleShow()
...
mCallback.onKeyguardShow();
mKeyguardViewManager.show();
mShowing = true;
mKeyguardViewManager.show() 調(diào)用的是文件 KeyguardViewManager.java 中的函數(shù):
public synchronized void show()
...
mKeyguardView = mKeyguardViewProperties.createKeyguardView(mContext, mUpdateMonitor, this);
...
mKeyguardViewProperties.createKeyguardView 調(diào)用的是文件 LockPatternKeyguardViewProperties.java
中的函數(shù):
public KeyguardViewBase createKeyguardView(Context context,
KeyguardUpdateMonitor updateMonitor,
KeyguardWindowController controller) {
return new LockPatternKeyguardView(context, updateMonitor,
mLockPatternUtils, controller);
}
new LockPatternKeyguardView 調(diào)用了類 LockPatternKeyguardView 的構(gòu)造函數(shù):
public LockPatternKeyguardView(
Context context,
KeyguardUpdateMonitor updateMonitor,
LockPatternUtils lockPatternUtils,
KeyguardWindowController controller)
...
mLockScreen = createLockScreen();
addView(mLockScreen);
final UnlockMode unlockMode = getUnlockMode();
mUnlockScreen = createUnlockScreenFor(unlockMode);
mUnlockScreenMode = unlockMode;
addView(mUnlockScreen);
updateScreen(mMode);
執(zhí)行上面的程序然后彈出解鎖界面,getUnlockMode 獲得鎖類型,通常有三種:
enum UnlockMode {
Pattern, //圖案鎖
SimPin, //輸入pin或者puk
Account //賬號鎖
}
通過上面的過程我們可以知道,在系統(tǒng)初始化階段啟動rild的時候,rild與貓進(jìn)行了通信,并對貓進(jìn)行初始化。
保存了網(wǎng)絡(luò)的一系列狀態(tài)。
=========
待機狀態(tài)下,飛行模式切換流程分析:
飛行模式切換比較復(fù)雜,它狀態(tài)改變時涉及到極大模塊狀態(tài)切換:
GSM模塊,藍(lán)牙模塊,wifi模塊。
飛行模式的enabler層會發(fā)送廣播消息:ACTION_AIRPLANE_MODE_CHANGED
private void setAirplaneModeOn(boolean enabling) {
mCheckBoxPref.setEnabled(false);
mCheckBoxPref.setSummary(enabling ? R.string.airplane_mode_turning_on
: R.string.airplane_mode_turning_off);
// Change the system setting
Settings.System.putInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
enabling ? 1 : 0);
// Post the intent
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", enabling);
mContext.sendBroadcast(intent);
}
因為GSM ,藍(lán)牙,wifi模塊分別注冊了對 ACTION_AIRPLANE_MODE_CHANGED 消息的監(jiān)測,所以收到
該消息后,模塊會進(jìn)行切換。
BluetoothDeviceService.java
開啟藍(lán)牙:enable(false);
關(guān)閉藍(lán)牙:disable(false);
PhoneApp.java (packages\apps\phone\src\com\android\phone)
設(shè)置GSM模塊狀態(tài) phone.setRadioPower(enabled);
WifiService.java
設(shè)置 wifi 狀態(tài) setWifiEnabledBlocking(wifiEnabled, false, Process.myUid());
===
GSM模塊切換過程分析:
phone.setRadioPower(enabled)調(diào)用的是:
文件 GSMPhone.java 中的的函數(shù):
public void setRadioPower(boolean power)
mSST.setRadioPower(power);
因為有 ServiceStateTracker mSST;
mSST.setRadioPower 調(diào)用的是文件 ServiceStateTracker.java 中的函數(shù):
public void setRadioPower(boolean power)
mDesiredPowerState = power;
setPowerStateToDesired();
cm.setRadioPower(true, null);
或者
cm.setRadioPower(false, null);
因為有:
CommandsInterface cm;
public final class RIL extends BaseCommands implements CommandsInterface
所以 cm.setRadioPower 調(diào)用的是文件 RIL.java 中的函數(shù):
public void setRadioPower(boolean on, Message result)
RILRequest rr = RILRequest.obtain(RIL_REQUEST_RADIO_POWER, result);
rr.mp.writeInt(1);
...
send(rr)
通過 send 向 rild 發(fā)送 RIL_REQUEST_RADIO_POWER 請求來開啟或者關(guān)閉GSM模塊。
rild 數(shù)據(jù)接收流程:
收到 RIL_REQUEST_RADIO_POWER 執(zhí)行:
requestRadioPower(data, datalen, t);
然后根據(jù)條件往無線模塊發(fā)送模塊開啟和關(guān)閉請求
主要的at命令有:
err = at_send_command("AT+CFUN=0", &p_response);
err = at_send_command("AT+CFUN=1", &p_response);
===
藍(lán)牙模塊切換過程分析:
enable(false);
藍(lán)牙開啟調(diào)用文件 BluetoothDeviceService.java 中的函數(shù):
public synchronized boolean enable(boolean saveSetting)
setBluetoothState(BluetoothDevice.BLUETOOTH_STATE_TURNING_ON);
mEnableThread = new EnableThread(saveSetting);
mEnableThread.start();
----
disable(false)
藍(lán)牙關(guān)閉調(diào)用文件 中的函數(shù):
public synchronized boolean disable(boolean saveSetting)
setBluetoothState(BluetoothDevice.BLUETOOTH_STATE_TURNING_OFF);
===
wifi模塊切換過程分析:
廣播 wifi 狀態(tài)改變的消息:WIFI_STATE_CHANGED_ACTION
setWifiEnabledState(enable ? WIFI_STATE_ENABLING : WIFI_STATE_DISABLING, uid);
更新 wifi 狀態(tài):
private void updateWifiState()
如果需要使能開啟 wifi 那么會發(fā)送:
sendEnableMessage(true, false, mLastEnableUid);
sendStartMessage(strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
mWifiHandler.sendEmptyMessage(MESSAGE_STOP_WIFI);
消息循環(huán)中處理命令消息:
public void handleMessage(Message msg)
如果使能wifi:setWifiEnabledBlocking(true, msg.arg1 == 1, msg.arg2);
開啟wifi: mWifiStateTracker.setScanOnlyMode(msg.arg1 != 0);
setWifiEnabledBlocking(false, msg.arg1 == 1, msg.arg2);
斷開 mWifiStateTracker.disconnectAndStop();
開啟過程步驟:
1> 裝載 wifi 驅(qū)動: WifiNative.loadDriver()
2> 啟動后退 daemo supplicant: WifiNative.startSupplicant()
關(guān)閉過程步驟:
1> 停止后退 daemo supplicant:WifiNative.stopSupplicant()
2> 卸載 wifi 驅(qū)動: WifiNative.unloadDriver()
如果 wifi 狀態(tài)默認(rèn)為開啟那么 WifiService 服務(wù)的構(gòu)造函數(shù):
WifiService(Context context, WifiStateTracker tracker)
boolean wifiEnabled = getPersistedWifiEnabled();
setWifiEnabledBlocking(wifiEnabled, false, Process.myUid());
會開啟wifi模塊。