📄 exm.c
字号:
switch( Exm.byRecAnswID ) // check the answer ID
{
case WtIfcDef_eDLLCmd_RxPacket: // if we are receiving data packet
s_FillPktBuf( byData ); // fill the packet buffer
break;
case WtIfcDef_eDLLCmd_Responce: // if we are receiving a responce for a command
case WtIfcDef_eDLLCmd_ExtResponce:
s_FillAnswBuf( byData ); // fill the answer buffer
break;
default: // in any other case
s_OtherType(); // process the error case
break;
}
}
else
{
Exm.eRecState = Exm_eRecState_Attn; // if we have received wrong length reset the receiver state
}
break;
default:
Exm.eRecState = Exm_eRecState_Attn; // should not to come here
break;
}
}
// ****************************************************************************************************
// Wait for the modem answer
//
// Description: This function waits for answer message from the modem
//
// Arguments: eCmdID is ID of the command that has been sent to the modem
//
// Returns: TRUE if the answer result is okay
//
// ****************************************************************************************************
BOOL s_WaitForAnswer( WtIfcDef_ePcCmd_t eCmdID )
{
BOOL bWait = TRUE; // the function is waitung when the flag is TRUE
BOOL bResult = TRUE;
while( bWait )
{
if( Exm.bRecAnsw ) // if the interupt handler detected a modem answer
{
if( Exm.sRecAnswBuf[Exm_ANSW_PL_IDX_CMDID] == (BYTE)eCmdID ) // check the answer ID
{
bWait = FALSE; // if it is the same as we are expecting
switch( eCmdID )
{
case WtIfcDef_ePcCmd_RST:
{
bResult
= ( Exm.sRecAnswBuf[Exm_ANSW_PL_IDX_RESULT]
== (BYTE)DLHdi_eCode_DatalinkOkay );
Exm.bResetDetected = FALSE;
break;
}
case WtIfcDef_ePcCmd_SSD:
case WtIfcDef_ePcCmd_CW:
case WtIfcDef_ePcCmd_SRC:
case WtIfcDef_ePcCmd_SC:
case WtIfcDef_ePcCmd_ADE:
case WtIfcDef_ePcCmd_RPT:
case WtIfcDef_ePcCmd_REP:
case WtIfcDef_ePcCmd_WEP:
case WtIfcDef_ePcCmd_RAD:
case WtIfcDef_ePcCmd_SCD:
case WtIfcDef_ePcCmd_GCD:
{
// check command execution the result
bResult = (Exm.sRecAnswBuf[Exm_ANSW_PL_IDX_RESULT] != 0 );
break;
}
case WtIfcDef_ePcCmd_PT:
{
// check the packet transmission result
bResult
= ( Exm.sRecAnswBuf[Exm_ANSW_PL_IDX_RESULT]
== DLHdi_SpV1_Res_Success );
break;
}
default:
{
break;
}
}
}
Exm.bRecAnsw = FALSE;
}
}
return bResult;
}
// ****************************************************************************************************
// Send command or packet to the modem
//
// Description: This function sends a command or packet to the modem.
//
// Arguments: pCommand is pointer to the command payload buffer
//
// Returns: TRUE if the modem answer has success code
//
// ****************************************************************************************************
BOOL Exm_Send( PBYTE pCommand )
{
BOOL bResult;
WORD wLength = pCommand[Exm_CMDIDX_LENLO] + Exm_LEN_HEADER; // get command length from the payload
Exm.bRecAnsw = FALSE; // clear the answer flag
HalExm_SendData( pCommand, wLength ); // send the data to the modem
bResult = s_WaitForAnswer( pCommand[Exm_CMDIDX_ID] ); // wait for the modem answer
return bResult;
}
// ****************************************************************************************************
// wait for the reset notofication
//
// Description: This function waits for reset notification from the modem
//
// ****************************************************************************************************
void s_WaitForReset( void )
{
s_WaitForAnswer( WtIfcDef_ePcCmd_RST );
Exm.bResetDetected = FALSE;
}
// ****************************************************************************************************
// Configure the data link layer
//
// Description: This function sends configuration commands to the modem. It starts the single device mode,
// sets the transmission modulation, repeats and retries limits and sets the address of the
// device in the network
//
// ****************************************************************************************************
void s_ConfigureModem( void )
{
Exm_Send( Exm_Cmd_StartSdm );
Exm_Send( Exm_Cmd_SendCfgPacket );
Exm_Send( Exm_Cmd_Modulation );
Exm_Send( Exm_Cmd_Repeats );
Exm_Send( Exm_Cmd_SetAddress );
}
// ****************************************************************************************************
// when the button pressed ...
//
// Description: This function toggles the virtual switch flag when the user presses the button
//
//
// ****************************************************************************************************
void HalExm_CB_OnButtonPressed( void )
{
Exm.bSwState ^= 1;
}
// ****************************************************************************************************
// when the button released ...
//
// Description: This function is called when the user releases the button.
//
// ****************************************************************************************************
void HalExm_CB_OnButtonReleased( void )
{
}
// ****************************************************************************************************
// Change the lamp state
//
// Description: This function sends commands to the target device via the network untill the state of
// the "lamp" becomes the same as the application wants.
//
// ****************************************************************************************************
#define Exm_PKT_RET_TIMEOUT 10000L // timeout for waiting the answer packet from the target
void s_ChangeLampState( void )
{
BOOL bContinue = TRUE; // this flag is true untill the device receives confirmation from the target
PBYTE pPacket = Exm.bSwState ? Exm_LampOnPkt : Exm_LampOffPkt; // set the pointer to one of the two packets
while( bContinue )
{
DWORD dwTimeOutCnt; // counter for wating an answer packet
Exm_Send( pPacket ); // send the command to the target ("lamp")
for( dwTimeOutCnt = Exm_PKT_RET_TIMEOUT;
(dwTimeOutCnt != 0) && (Exm.bSwState != Exm.bRemoteLampState)
&& (Exm.bLampOnPktReceived == FALSE) && (Exm.bLampOffPktReceived == FALSE);
dwTimeOutCnt-- )
{
// wait for the answer from the "lamp" or stop waiting if the counter reaches zero
}
if( Exm.bSwState == Exm.bRemoteLampState ) // if the "lamp" is in the same state as our virtual lamp flag
{
bContinue = FALSE; // do not send more command packets
}
else
{
if( (Exm.bSwState == TRUE) && (Exm.bLampOnPktReceived == TRUE) ) // if we had told the lamp to turn on
{ // and the lamp has turned on
Exm.bRemoteLampState = Exm.bSwState; // change the virtual lamp flag in our application
Exm.bLampOnPktReceived = FALSE; // clear the packet flag, because we do not need it any more
bContinue = FALSE; // do not send commands
}
if( (Exm.bSwState == FALSE) && (Exm.bLampOffPktReceived == TRUE) ) // if we had told the lamp to turn off
{ // and the lamp has turned off
Exm.bRemoteLampState = Exm.bSwState; // change the virtual lamp flag in our application
Exm.bLampOffPktReceived = FALSE; // clear the packet flag, because we do not need it any more
bContinue = FALSE; // do not send commands
}
}
}
}
// ****************************************************************************************************
// Modem functions usage demo
//
// Description: The function is created in order to demonstrate the modem interface usage. These interface
// functions are not needed in the current application.
//
//
// ****************************************************************************************************
void s_TestInterface( void )
{
Exm_Send( Exm_Cmd_GetDLLParam );
Exm_Send( Exm_Cmd_WriteEEPROM );
Exm_Send( Exm_Cmd_ReadEEPROM );
}
// ****************************************************************************************************
// main function of the application
//
// Description: The function runc infinite loop. During its run it checks the application flags and
// performs operations for each flag
//
// Returns: never reaches the end
//
// ****************************************************************************************************
int main( void )
{
HalExm_Init(); // initialize the hardware abstraction layer
HalExm_DebugLED( TRUE );
Exm_Send( Exm_Cmd_Reset ); // reset the modem
Exm.bResetDetected = FALSE;
HalExm_DebugLED( FALSE ); // * for debugging only
s_ConfigureModem(); // set the device parameters
s_TestInterface();
while( 1 )
{
if( Exm.bResetDetected ) // if we has received the reset notification from the modem (for example, somebody hit the reset button on the box)
{
HalExm_DebugLED( TRUE ); // * for debugging only
s_ConfigureModem(); // set the device parameters again
HalExm_DebugLED( FALSE ); // * for debugging only
Exm.bResetDetected = FALSE; // clear the reset flag
}
if( Exm.bSwState ) // if the user presses the button
{
HalExm_LampOn(); // change the indicator state
}
else
{
HalExm_LampOff();
}
if( Exm.bSwState != Exm.bRemoteLampState ) // if the state of the virtual switch does not equal to the state of the virtual lamp
{
s_ChangeLampState(); // send commands untio the state of the lamp changes
}
}
return 0;
}
// ****************************************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -