站在華為上汽面試官的角度去想問(wèn)題-ATM狀態(tài)機(jī)又撒了壹萬(wàn)億
引言
記得年輕的時(shí)候去面試一家大廠的嵌入式軟件工程師的職位,面試官讓很多候選人三人一組討論去設(shè)計(jì)一款ATM機(jī),當(dāng)時(shí)我們那組討論的熱火朝天,什么把Linux操作系統(tǒng)內(nèi)核移植過(guò)來(lái),加入互鎖機(jī)制確保取錢(qián)的安全性,什么多線程加快速度等等天馬行空的方案。最終默默地在等待Offer,就一直默默而無(wú)下文了。現(xiàn)在站在大廠的角度想一想,可能是通過(guò)一些試題想考察你對(duì)一個(gè)項(xiàng)目的軟件思維能力。啥叫軟件思維能力。就是從軟件的角度去解讀需求。別囫圇吞棗,大而空地泛泛之談。你能想得越細(xì),設(shè)計(jì)的代碼覆蓋的case越多,設(shè)計(jì)的代碼簡(jiǎn)單且RAM,ROM越節(jié)省,執(zhí)行效率越高,這才是去做嵌入式軟件工程師的正確解法。我們從狀態(tài)機(jī)的角度去分析一下解題步驟:
-
全面詳盡的弄清楚客戶的需求,分解成相應(yīng)的功能或多個(gè)子功能需求 -
弄清楚這些功能中的狀態(tài),并確定狀態(tài)的轉(zhuǎn)移條件(及狀態(tài)的進(jìn)入和退出條件或事件),設(shè)計(jì)出系統(tǒng)的狀態(tài)骨架 -
測(cè)試每個(gè)轉(zhuǎn)移的有效性 -
針對(duì)每個(gè)狀態(tài),結(jié)合功能需求細(xì)化狀態(tài)中需要做哪些事情 -
集成進(jìn)行系統(tǒng)的集成測(cè)試
ATM狀態(tài)機(jī)
當(dāng)您設(shè)計(jì)ATM 的功能時(shí),設(shè)想一下你是小明。小明走到ATM機(jī)面前,一系列的事件(事件是指在某個(gè)時(shí)刻發(fā)生的事情 )發(fā)生了,插入卡片(Card_Insert_Event),輸入密碼(Pin_Enter_Even),選擇辦理事項(xiàng)(Option_Selection_Event:取錢(qián)/存錢(qián)/查詢余額),輸入數(shù)額(Amount_Enter_Event)并按確認(rèn),ATM吐出所需錢(qián)財(cái)(Amount_Dispatch_Event)。如下圖為從ATM取錢(qián)的一個(gè)狀態(tài)機(jī)模型, 描述了ATM狀態(tài)及轉(zhuǎn)移條件。
狀態(tài)作為系統(tǒng)的一種特定階段的狀態(tài),則會(huì)持續(xù)一段時(shí)間,直到特定的觸發(fā)事件導(dǎo)致?tīng)顟B(tài)的轉(zhuǎn)移的特定點(diǎn)為止。例如夜深人靜的時(shí)候ATM大概率處于空閑狀態(tài)。過(guò)節(jié)的時(shí)候(是蠻多和女生相關(guān)的且需要花錢(qián)的節(jié)日的),你走到ATM插入卡片后的那個(gè)時(shí)間點(diǎn),ATM從空閑狀態(tài)切換到了檢測(cè)到卡片插入狀態(tài)。
對(duì)事件發(fā)生所執(zhí)行的事件處理是當(dāng)前狀態(tài)和輸入事件的一個(gè)函數(shù)。此處的名字叫做EventHandle, 還可叫做Action(有三類:entry,during,exit)。其可存在轉(zhuǎn)移的分支上,或者是位于狀態(tài)的進(jìn)入或退出動(dòng)作或during標(biāo)簽中,如下圖所示。
-
entry: 當(dāng)進(jìn)入一個(gè)狀態(tài)的時(shí)候執(zhí)行該標(biāo)簽下的action,該action在狀態(tài)中其它任何action之前執(zhí)行。 -
during: 當(dāng)狀態(tài)處于激活時(shí)執(zhí)行during 標(biāo)簽下的action , during活動(dòng)在進(jìn)入活動(dòng)之后執(zhí)行,并且—直運(yùn)行到它本身完成為止(同步執(zhí)行的動(dòng)作)。 -
exit: 當(dāng)離開(kāi)—個(gè)狀態(tài)的時(shí)候觸發(fā)執(zhí)行該標(biāo)簽下的action,該活動(dòng)在該狀態(tài)結(jié)束之前并且所有其它action都完成后觸發(fā)執(zhí)行。
一般狀態(tài)之間的轉(zhuǎn)換不僅依賴于Event的發(fā)生,還需要[Condition Expression]這個(gè)門(mén)控條件成立,才能發(fā)生轉(zhuǎn)移。如下圖中的轉(zhuǎn)移的方式:轉(zhuǎn)移條件分別為Condition和Event。
狀態(tài)機(jī)實(shí)現(xiàn)方式
如果你用C語(yǔ)言建模的話,一般推薦下面兩種方式
-
實(shí)現(xiàn)方式1:switch-case結(jié)構(gòu)。
實(shí)現(xiàn)方式簡(jiǎn)單,Case變多的時(shí)候不易維護(hù),并且圈復(fù)雜度較高。
#include <stdio.h>
//Different state of ATM machine
typedef enum
{
Idle_State,
Card_Inserted_State,
Pin_Eentered_State,
Option_Selected_State,
Amount_Entered_State,
} eSystemState;
//Different type events
typedef enum
{
Card_Insert_Event,
Pin_Enter_Event,
Option_Selection_Event,
Amount_Enter_Event,
Amount_Dispatch_Event
} eSystemEvent;
//Prototype of eventhandlers
eSystemState AmountDispatchHandler(void)
{
return Idle_State;
}
eSystemState EnterAmountHandler(void)
{
return Amount_Entered_State;
}
eSystemState OptionSelectionHandler(void)
{
return Option_Selected_State;
}
eSystemState EnterPinHandler(void)
{
return Pin_Eentered_State;
}
eSystemState InsertCardHandler(void)
{
return Card_Inserted_State;
}
int main(int argc, char *argv[])
{
eSystemState eNextState = Idle_State;
eSystemEvent eNewEvent;
while(1)
{
//Read system Events
eSystemEvent eNewEvent = ReadEvent();
switch(eNextState)
{
case Idle_State:
{
if(Card_Insert_Event == eNewEvent)
{
eNextState = InsertCardHandler();
}
}
break;
case Card_Inserted_State:
{
if(Pin_Enter_Event == eNewEvent)
{
eNextState = EnterPinHandler();
}
}
break;
case Pin_Eentered_State:
{
if(Option_Selection_Event == eNewEvent)
{
eNextState = OptionSelectionHandler();
}
}
break;
case Option_Selected_State:
{
if(Amount_Enter_Event == eNewEvent)
{
eNextState = EnterAmountHandler();
}
}
break;
case Amount_Entered_State:
{
if(Amount_Dispatch_Event == eNewEvent)
{
eNextState = AmountDispatchHandler();
}
}
break;
default:
break;
}
}
return 0;
}
-
實(shí)現(xiàn)方式2:查表方式
易于維護(hù),可以方便你增加新的狀態(tài)或事件。減少代碼的長(zhǎng)度,通過(guò)函數(shù)指針和事件及狀態(tài)進(jìn)行綁定,非要說(shuō)個(gè)缺點(diǎn)就是使用了指針,在汽車Misra C標(biāo)準(zhǔn)中不太推薦使用指針。
#include <stdio.h>
//Different state of ATM machine
typedef enum
{
Idle_State,
Card_Inserted_State,
Pin_Eentered_State,
Option_Selected_State,
Amount_Entered_State,
last_State
} eSystemState;
//Different type events
typedef enum
{
Card_Insert_Event,
Pin_Enter_Event,
Option_Selection_Event,
Amount_Enter_Event,
Amount_Dispatch_Event,
last_Event
} eSystemEvent;
//typedef of function pointer
typedef eSystemState (*pfEventHandler)(void);
//structure of state and event with event handler
typedef struct
{
eSystemState eStateMachine;
eSystemEvent eStateMachineEvent;
pfEventHandler pfStateMachineEvnentHandler;
} sStateMachine;
//function call to dispatch the amount and return the ideal state
eSystemState AmountDispatchHandler(void)
{
return Idle_State;
}
//function call to Enter amount and return amount entered state
eSystemState EnterAmountHandler(void)
{
return Amount_Entered_State;
}
//function call to option select and return the option selected state
eSystemState OptionSelectionHandler(void)
{
return Option_Selected_State;
}
//function call to enter the pin and return pin entered state
eSystemState EnterPinHandler(void)
{
return Pin_Eentered_State;
}
//function call to processing track data and return card inserted state
eSystemState InsertCardHandler(void)
{
return Card_Inserted_State;
}
//Initialize array of structure with states and event with proper handler
sStateMachine asStateMachine [] =
{
{Idle_State,Card_Insert_Event,InsertCardHandler},
{Card_Inserted_State,Pin_Enter_Event,EnterPinHandler},
{Pin_Eentered_State,Option_Selection_Event,OptionSelectionHandler},
{Option_Selected_State,Amount_Enter_Event,EnterAmountHandler},
{Amount_Entered_State,Amount_Dispatch_Event,AmountDispatchHandler}
};
//main function
int main(int argc, char *argv[])
{
eSystemState eNextState = Idle_State;
while(1)
{
//Api read the event
eSystemEvent eNewEvent = read_event();
if((eNextState < last_State) && (eNewEvent < last_Event)&& (asStateMachine[eNextState].eStateMachineEvent == eNewEvent) && (asStateMachine[eNextState].pfStateMachineEvnentHandler != NULL))
{
// function call as per the state and event and return the next state of the finite state machine
eNextState = (*asStateMachine[eNextState].pfStateMachineEvnentHandler)();
}
else
{
//Invalid
}
}
return 0;
}
AUTOSAR ASILD級(jí)別安全軟件模塊的安全機(jī)制介紹
基于Autosar軟件的功能安全開(kāi)發(fā)的介紹
AUTOSAR模式管理經(jīng)驗(yàn)總結(jié)
使用AURIX TOM模塊生成交流電機(jī)的PWM驅(qū)動(dòng)
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺(tái)僅提供信息存儲(chǔ)服務(wù)。文章僅代表作者個(gè)人觀點(diǎn),不代表本平臺(tái)立場(chǎng),如有問(wèn)題,請(qǐng)聯(lián)系我們,謝謝!