Webserver+3d print

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ppp_fsm.c Source File

ppp_fsm.c

Go to the documentation of this file.
00001 /**
00002  * @file ppp_fsm.c
00003  * @brief PPP finite state machine
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneTCP Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 //Switch to the appropriate trace level
00030 #define TRACE_LEVEL PPP_TRACE_LEVEL
00031 
00032 //Dependencies
00033 #include "core/net.h"
00034 #include "ppp/ppp_fsm.h"
00035 #include "debug.h"
00036 
00037 //Check TCP/IP stack configuration
00038 #if (PPP_SUPPORT == ENABLED)
00039 
00040 
00041 /**
00042  * @brief Process Up event
00043  * @param[in] context PPP context
00044  * @param[in,out] fsm Finite state machine
00045  * @param[in] callbacks FSM actions
00046  **/
00047 
00048 void pppUpEvent(PppContext *context, PppFsm *fsm,
00049    const PppCallbacks *callbacks)
00050 {
00051    //Check current state
00052    switch(fsm->state)
00053    {
00054    case PPP_STATE_0_INITIAL:
00055       //Switch to the Closed state
00056       pppChangeState(fsm, PPP_STATE_2_CLOSED);
00057       break;
00058    case PPP_STATE_1_STARTING:
00059        //Initialize restart counter
00060       callbacks->initRestartCount(context, PPP_MAX_CONFIGURE);
00061       //Send Configure-Request packet
00062       callbacks->sendConfigureReq(context);
00063       //Switch to the Req-Sent state
00064       pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00065       break;
00066    default:
00067       //This event cannot occur in a properly implemented automaton.
00068       //No transition is taken, and the implementation should not
00069       //reset or freeze
00070       break;
00071    }
00072 }
00073 
00074 
00075 /**
00076  * @brief Process Down event
00077  * @param[in] context PPP context
00078  * @param[in,out] fsm Finite state machine
00079  * @param[in] callbacks FSM actions
00080  **/
00081 
00082 void pppDownEvent(PppContext *context, PppFsm *fsm,
00083    const PppCallbacks *callbacks)
00084 {
00085    //Check current state
00086    switch(fsm->state)
00087    {
00088    case PPP_STATE_2_CLOSED:
00089       //Switch to the Initial state
00090       pppChangeState(fsm, PPP_STATE_0_INITIAL);
00091       break;
00092    case PPP_STATE_3_STOPPED:
00093       //Switch to the Starting state
00094       pppChangeState(fsm, PPP_STATE_1_STARTING);
00095       //Indicate to the lower layers that the automaton is entering the
00096       //Starting state. The lower layer is needed for the link
00097       callbacks->thisLayerStarted(context);
00098       break;
00099    case PPP_STATE_4_CLOSING:
00100       //Switch to the Initial state
00101       pppChangeState(fsm, PPP_STATE_0_INITIAL);
00102       break;
00103    case PPP_STATE_5_STOPPING:
00104    case PPP_STATE_6_REQ_SENT:
00105    case PPP_STATE_7_ACK_RCVD:
00106    case PPP_STATE_8_ACK_SENT:
00107       //Switch to the Starting state
00108       pppChangeState(fsm, PPP_STATE_1_STARTING);
00109       break;
00110    case PPP_STATE_9_OPENED:
00111       //Switch to the Starting state
00112       pppChangeState(fsm, PPP_STATE_1_STARTING);
00113       //Indicate to the upper layers that the automaton is leaving the Opened
00114       //state. The link is no longer available for network traffic
00115       callbacks->thisLayerDown(context);
00116       break;
00117    default:
00118       //This event cannot occur in a properly implemented automaton.
00119       //No transition is taken, and the implementation should not
00120       //reset or freeze
00121       break;
00122    }
00123 }
00124 
00125 
00126 /**
00127  * @brief Process Open event
00128  * @param[in] context PPP context
00129  * @param[in,out] fsm Finite state machine
00130  * @param[in] callbacks FSM actions
00131  **/
00132 
00133 void pppOpenEvent(PppContext *context, PppFsm *fsm,
00134    const PppCallbacks *callbacks)
00135 {
00136    //Check current state
00137    switch(fsm->state)
00138    {
00139    case PPP_STATE_0_INITIAL:
00140       //Switch to the Starting state
00141       pppChangeState(fsm, PPP_STATE_1_STARTING);
00142       //Indicate to the lower layers that the automaton is entering the
00143       //Starting state. The lower layer is needed for the link
00144       callbacks->thisLayerStarted(context);
00145       break;
00146    case PPP_STATE_1_STARTING:
00147       //Stay in current state
00148       break;
00149    case PPP_STATE_2_CLOSED:
00150       //Initialize restart counter
00151       callbacks->initRestartCount(context, PPP_MAX_CONFIGURE);
00152       //Send Configure-Request packet
00153       callbacks->sendConfigureReq(context);
00154       //Switch to the Req-Sent state
00155       pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00156       break;
00157    case PPP_STATE_3_STOPPED:
00158       //Stay in current state
00159       break;
00160    case PPP_STATE_4_CLOSING:
00161       //Switch to the Stopping state
00162       pppChangeState(fsm, PPP_STATE_5_STOPPING);
00163       break;
00164    case PPP_STATE_5_STOPPING:
00165    case PPP_STATE_6_REQ_SENT:
00166    case PPP_STATE_7_ACK_RCVD:
00167    case PPP_STATE_8_ACK_SENT:
00168    case PPP_STATE_9_OPENED:
00169       //Stay in current state
00170       break;
00171    default:
00172       //This event cannot occur in a properly implemented automaton.
00173       //No transition is taken, and the implementation should not
00174       //reset or freeze
00175       break;
00176    }
00177 }
00178 
00179 
00180 /**
00181  * @brief Process Close event
00182  * @param[in] context PPP context
00183  * @param[in,out] fsm Finite state machine
00184  * @param[in] callbacks FSM actions
00185  **/
00186 
00187 void pppCloseEvent(PppContext *context, PppFsm *fsm,
00188    const PppCallbacks *callbacks)
00189 {
00190    //Check current state
00191    switch(fsm->state)
00192    {
00193    case PPP_STATE_0_INITIAL:
00194       //Stay in current state
00195       break;
00196    case PPP_STATE_1_STARTING:
00197       //Switch to the Initial state
00198       pppChangeState(fsm, PPP_STATE_0_INITIAL);
00199       //Indicate to the lower layers that the automaton is entering the
00200       //Initial, Closed or Stopped states. The lower layer is no longer
00201       //needed for the link
00202       callbacks->thisLayerFinished(context);
00203       break;
00204    case PPP_STATE_2_CLOSED:
00205       //Stay in current state
00206       break;
00207    case PPP_STATE_3_STOPPED:
00208       //Switch to the Closed state
00209       pppChangeState(fsm, PPP_STATE_2_CLOSED);
00210       break;
00211    case PPP_STATE_4_CLOSING:
00212       //Stay in current state
00213       break;
00214    case PPP_STATE_5_STOPPING:
00215       //Switch to the Closing state
00216       pppChangeState(fsm, PPP_STATE_4_CLOSING);
00217       break;
00218    case PPP_STATE_6_REQ_SENT:
00219    case PPP_STATE_7_ACK_RCVD:
00220    case PPP_STATE_8_ACK_SENT:
00221       //Initialize restart counter
00222       callbacks->initRestartCount(context, PPP_MAX_TERMINATE);
00223       //Send Terminate-Request packet
00224       callbacks->sendTerminateReq(context);
00225       //Switch to the Closing state
00226       pppChangeState(fsm, PPP_STATE_4_CLOSING);
00227       break;
00228    case PPP_STATE_9_OPENED:
00229       //Initialize restart counter
00230       callbacks->initRestartCount(context, PPP_MAX_TERMINATE);
00231       //Send Terminate-Request packet
00232       callbacks->sendTerminateReq(context);
00233       //Switch to the Closing state
00234       pppChangeState(fsm, PPP_STATE_4_CLOSING);
00235       //Indicate to the upper layers that the automaton is leaving the Opened
00236       //state. The link is no longer available for network traffic
00237       callbacks->thisLayerDown(context);
00238       break;
00239    default:
00240       //This event cannot occur in a properly implemented automaton.
00241       //No transition is taken, and the implementation should not
00242       //reset or freeze
00243       break;
00244    }
00245 }
00246 
00247 
00248 /**
00249  * @brief Process Timeout event
00250  * @param[in] context PPP context
00251  * @param[in,out] fsm Finite state machine
00252  * @param[in] callbacks FSM actions
00253  **/
00254 
00255 void pppTimeoutEvent(PppContext *context, PppFsm *fsm,
00256    const PppCallbacks *callbacks)
00257 {
00258    //The restart counter is greater than zero (TO+ event)
00259    if(fsm->restartCounter > 0)
00260    {
00261       //Check current state
00262       switch(fsm->state)
00263       {
00264       case PPP_STATE_4_CLOSING:
00265       case PPP_STATE_5_STOPPING:
00266          //Send Terminate-Request packet
00267          callbacks->sendTerminateReq(context);
00268          //Stay in current state
00269          break;
00270       case PPP_STATE_6_REQ_SENT:
00271       case PPP_STATE_7_ACK_RCVD:
00272          //Send Configuration-Request packet
00273          callbacks->sendConfigureReq(context);
00274          //Switch to the Req-Sent state
00275          pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00276          break;
00277       case PPP_STATE_8_ACK_SENT:
00278          //Send Configuration-Request packet
00279           callbacks->sendConfigureReq(context);
00280          //Stay in current state
00281          break;
00282       default:
00283          //This event cannot occur in a properly implemented automaton.
00284          //No transition is taken, and the implementation should not
00285          //reset or freeze
00286          break;
00287       }
00288    }
00289    //The restart counter is not greater than zero (TO- event)
00290    else
00291    {
00292       //Check current state
00293       switch(fsm->state)
00294       {
00295       case PPP_STATE_4_CLOSING:
00296          //Switch to the Closed state
00297          pppChangeState(fsm, PPP_STATE_2_CLOSED);
00298          //Indicate to the lower layers that the automaton is entering the
00299          //Initial, Closed or Stopped states. The lower layer is no longer
00300          //needed for the link
00301          callbacks->thisLayerFinished(context);
00302          break;
00303       case PPP_STATE_5_STOPPING:
00304       case PPP_STATE_6_REQ_SENT:
00305       case PPP_STATE_7_ACK_RCVD:
00306       case PPP_STATE_8_ACK_SENT:
00307          //Switch to the Stopped state
00308          pppChangeState(fsm, PPP_STATE_3_STOPPED);
00309          //Indicate to the lower layers that the automaton is entering the
00310          //Initial, Closed or Stopped states. The lower layer is no longer
00311          //needed for the link
00312          callbacks->thisLayerFinished(context);
00313          break;
00314       default:
00315          //This event cannot occur in a properly implemented automaton.
00316          //No transition is taken, and the implementation should not
00317          //reset or freeze
00318          break;
00319       }
00320    }
00321 }
00322 
00323 
00324 /**
00325  * @brief Process Receive-Configure-Request event
00326  * @param[in] context PPP context
00327  * @param[in,out] fsm Finite state machine
00328  * @param[in] callbacks FSM actions
00329  * @param[in] configureReqPacket Configure-Request packet received from the peer
00330  * @param[in] code Tells whether the configuration options are acceptable
00331  **/
00332 
00333 void pppRcvConfigureReqEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks,
00334    const PppConfigurePacket *configureReqPacket, PppCode code)
00335 {
00336    //Check whether the configuration options are acceptable
00337    if(code == PPP_CODE_CONFIGURE_ACK)
00338    {
00339       //If every configuration option received in the Configure-Request is
00340       //recognizable and all values are acceptable, then the implementation
00341       //must transmit a Configure-Ack
00342       switch(fsm->state)
00343       {
00344       case PPP_STATE_2_CLOSED:
00345          //Send Terminate-Ack packet
00346          callbacks->sendTerminateAck(context, NULL);
00347          //Stay in current state
00348          break;
00349       case PPP_STATE_3_STOPPED:
00350          //Initialize restart counter
00351          callbacks->initRestartCount(context, PPP_MAX_CONFIGURE);
00352          //Send Configure-Request packet
00353          callbacks->sendConfigureReq(context);
00354          //Send Configure-Ack packet
00355          callbacks->sendConfigureAck(context, configureReqPacket);
00356          //Switch to the Ack-Sent state
00357          pppChangeState(fsm, PPP_STATE_8_ACK_SENT);
00358          break;
00359       case PPP_STATE_4_CLOSING:
00360       case PPP_STATE_5_STOPPING:
00361          //Stay in current state
00362          break;
00363       case PPP_STATE_6_REQ_SENT:
00364          //Send Configure-Ack packet
00365          callbacks->sendConfigureAck(context, configureReqPacket);
00366          //Switch to the Ack-Sent state
00367          pppChangeState(fsm, PPP_STATE_8_ACK_SENT);
00368          break;
00369       case PPP_STATE_7_ACK_RCVD:
00370          //Send Configure-Ack packet
00371          callbacks->sendConfigureAck(context, configureReqPacket);
00372          //Switch to the Opened state
00373          pppChangeState(fsm, PPP_STATE_9_OPENED);
00374          //Indicate to the upper layers that the automaton is entering the
00375          //Opened state. The link is available for network traffic
00376          callbacks->thisLayerUp(context);
00377          break;
00378       case PPP_STATE_8_ACK_SENT:
00379          //Send Configure-Ack packet
00380          callbacks->sendConfigureAck(context, configureReqPacket);
00381          //Stay in current state
00382          break;
00383       case PPP_STATE_9_OPENED:
00384          //Send Configure-Request packet
00385          callbacks->sendConfigureReq(context);
00386          //Send Configure-Ack packet
00387          callbacks->sendConfigureAck(context, configureReqPacket);
00388          //Switch to the Ack-Sent state
00389          pppChangeState(fsm, PPP_STATE_8_ACK_SENT);
00390          //Indicate to the upper layers that the automaton is leaving the Opened
00391          //state. The link is no longer available for network traffic
00392          callbacks->thisLayerDown(context);
00393          break;
00394       default:
00395          //This event cannot occur in a properly implemented automaton.
00396          //No transition is taken, and the implementation should not
00397          //reset or freeze
00398          break;
00399       }
00400    }
00401    else if(code == PPP_CODE_CONFIGURE_NAK)
00402    {
00403       //If all configuration options are recognizable, but some values are not
00404       //acceptable, then the implementation must transmit a Configure-Nak
00405       switch(fsm->state)
00406       {
00407       case PPP_STATE_2_CLOSED:
00408          //Send Terminate-Ack packet
00409          callbacks->sendTerminateAck(context, NULL);
00410          //Stay in current state
00411          break;
00412       case PPP_STATE_3_STOPPED:
00413          //Initialize restart counter
00414          callbacks->initRestartCount(context, PPP_MAX_CONFIGURE);
00415          //Send Configure-Request packet
00416          callbacks->sendConfigureReq(context);
00417          //Send Configure-Nak packet
00418          callbacks->sendConfigureNak(context, configureReqPacket);
00419          //Switch to the Req-Sent state
00420          pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00421          break;
00422       case PPP_STATE_4_CLOSING:
00423       case PPP_STATE_5_STOPPING:
00424          //Stay in current state
00425          break;
00426       case PPP_STATE_6_REQ_SENT:
00427       case PPP_STATE_7_ACK_RCVD:
00428          //Send Configure-Nak packet
00429          callbacks->sendConfigureNak(context, configureReqPacket);
00430          //Stay in current state
00431          break;
00432       case PPP_STATE_8_ACK_SENT:
00433          //Send Configure-Nak packet
00434          callbacks->sendConfigureNak(context, configureReqPacket);
00435          //Switch to the Req-Sent state
00436          pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00437          break;
00438       case PPP_STATE_9_OPENED:
00439          //Send Configure-Request packet
00440          callbacks->sendConfigureReq(context);
00441          //Send Configure-Nak packet
00442          callbacks->sendConfigureNak(context, configureReqPacket);
00443          //Switch to the Req-Sent state
00444          pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00445          //Indicate to the upper layers that the automaton is leaving the Opened
00446          //state. The link is no longer available for network traffic
00447          callbacks->thisLayerDown(context);
00448          break;
00449       default:
00450          //This event cannot occur in a properly implemented automaton.
00451          //No transition is taken, and the implementation should not
00452          //reset or freeze
00453          break;
00454       }
00455    }
00456    else if(code == PPP_CODE_CONFIGURE_REJ)
00457    {
00458       //If some configuration options received in the Configure-Request are not
00459       //recognizable or not acceptable for negotiation, then the implementation
00460       //must transmit a Configure-Reject
00461       switch(fsm->state)
00462       {
00463       case PPP_STATE_2_CLOSED:
00464          //Send Terminate-Ack packet
00465          callbacks->sendTerminateAck(context, NULL);
00466          //Stay in current state
00467          break;
00468       case PPP_STATE_3_STOPPED:
00469          //Initialize restart counter
00470          callbacks->initRestartCount(context, PPP_MAX_CONFIGURE);
00471          //Send Configure-Request packet
00472          callbacks->sendConfigureReq(context);
00473          //Send Configure-Reject packet
00474          callbacks->sendConfigureRej(context, configureReqPacket);
00475          //Switch to the Req-Sent state
00476          pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00477          break;
00478       case PPP_STATE_4_CLOSING:
00479       case PPP_STATE_5_STOPPING:
00480          //Stay in current state
00481          break;
00482       case PPP_STATE_6_REQ_SENT:
00483       case PPP_STATE_7_ACK_RCVD:
00484          //Send Configure-Reject packet
00485          callbacks->sendConfigureRej(context, configureReqPacket);
00486          //Stay in current state
00487          break;
00488       case PPP_STATE_8_ACK_SENT:
00489          //Send Configure-Reject packet
00490          callbacks->sendConfigureRej(context, configureReqPacket);
00491          //Switch to the Req-Sent state
00492          pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00493          break;
00494       case PPP_STATE_9_OPENED:
00495          //Send Configure-Request packet
00496          callbacks->sendConfigureReq(context);
00497          //Send Configure-Reject packet
00498          callbacks->sendConfigureRej(context, configureReqPacket);
00499          //Switch to the Req-Sent state
00500          pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00501          //Indicate to the upper layers that the automaton is leaving the Opened
00502          //state. The link is no longer available for network traffic
00503          callbacks->thisLayerDown(context);
00504          break;
00505       default:
00506          //This event cannot occur in a properly implemented automaton.
00507          //No transition is taken, and the implementation should not
00508          //reset or freeze
00509          break;
00510       }
00511    }
00512 }
00513 
00514 
00515 /**
00516  * @brief Process Receive-Configure-Ack event
00517  * @param[in] context PPP context
00518  * @param[in,out] fsm Finite state machine
00519  * @param[in] callbacks FSM actions
00520  **/
00521 
00522 void pppRcvConfigureAckEvent(PppContext *context, PppFsm *fsm,
00523    const PppCallbacks *callbacks)
00524 {
00525    //Check current state
00526    switch(fsm->state)
00527    {
00528    case PPP_STATE_2_CLOSED:
00529    case PPP_STATE_3_STOPPED:
00530       //Send Terminate-Ack packet
00531       callbacks->sendTerminateAck(context, NULL);
00532       //Stay in current state
00533       break;
00534    case PPP_STATE_4_CLOSING:
00535    case PPP_STATE_5_STOPPING:
00536       //Stay in current state
00537       break;
00538    case PPP_STATE_6_REQ_SENT:
00539       //Initialize restart counter
00540       callbacks->initRestartCount(context, PPP_MAX_CONFIGURE);
00541       //Switch to the Ack-Rcvd state
00542       fsm->state = PPP_STATE_7_ACK_RCVD;
00543       break;
00544    case PPP_STATE_7_ACK_RCVD:
00545       //Send Configure-Request packet
00546       callbacks->sendConfigureReq(context);
00547       //Switch to the Req-Sent state
00548       pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00549       break;
00550    case PPP_STATE_8_ACK_SENT:
00551       //Initialize restart counter
00552       callbacks->initRestartCount(context, PPP_MAX_CONFIGURE);
00553       //Switch to the Opened state
00554       pppChangeState(fsm, PPP_STATE_9_OPENED);
00555       //Indicate to the upper layers that the automaton is entering the
00556       //Opened state. The link is available for network traffic
00557       callbacks->thisLayerUp(context);
00558       break;
00559    case PPP_STATE_9_OPENED:
00560       //Send Configure-Request packet
00561       callbacks->sendConfigureReq(context);
00562       //Switch to the Req-Sent state
00563       pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00564       //Indicate to the upper layers that the automaton is leaving the Opened
00565       //state. The link is no longer available for network traffic
00566       callbacks->thisLayerDown(context);
00567       break;
00568    default:
00569       //This event cannot occur in a properly implemented automaton.
00570       //No transition is taken, and the implementation should not
00571       //reset or freeze
00572       break;
00573    }
00574 }
00575 
00576 
00577 /**
00578  * @brief Process Receive-Configure-Nak event
00579  * @param[in] context PPP context
00580  * @param[in,out] fsm Finite state machine
00581  * @param[in] callbacks FSM actions
00582  **/
00583 
00584 void pppRcvConfigureNakEvent(PppContext *context, PppFsm *fsm,
00585    const PppCallbacks *callbacks)
00586 {
00587    //Check current state
00588    switch(fsm->state)
00589    {
00590    case PPP_STATE_2_CLOSED:
00591    case PPP_STATE_3_STOPPED:
00592       //Send Terminate-Ack packet
00593       callbacks->sendTerminateAck(context, NULL);
00594       //Stay in current state
00595       break;
00596    case PPP_STATE_4_CLOSING:
00597    case PPP_STATE_5_STOPPING:
00598       //Stay in current state
00599       break;
00600    case PPP_STATE_6_REQ_SENT:
00601       //Initialize restart counter
00602       callbacks->initRestartCount(context, PPP_MAX_CONFIGURE);
00603       //Send Configure-Request packet
00604       callbacks->sendConfigureReq(context);
00605       //Stay in current state
00606       break;
00607    case PPP_STATE_7_ACK_RCVD:
00608       //Send Configure-Request packet
00609       callbacks->sendConfigureReq(context);
00610       //Switch to the Req-Sent state
00611       pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00612       break;
00613    case PPP_STATE_8_ACK_SENT:
00614       //Initialize restart counter
00615       callbacks->initRestartCount(context, PPP_MAX_CONFIGURE);
00616       //Send Configure-Request packet
00617       callbacks->sendConfigureReq(context);
00618       //Stay in current state
00619       break;
00620    case PPP_STATE_9_OPENED:
00621       //Send Configure-Request packet
00622       callbacks->sendConfigureReq(context);
00623       //Switch to the Req-Sent state
00624       pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00625       //Indicate to the upper layers that the automaton is leaving the Opened
00626       //state. The link is no longer available for network traffic
00627       callbacks->thisLayerDown(context);
00628       break;
00629    default:
00630       //This event cannot occur in a properly implemented automaton.
00631       //No transition is taken, and the implementation should not
00632       //reset or freeze
00633       break;
00634    }
00635 }
00636 
00637 
00638 /**
00639  * @brief Process Receive-Terminate-Req event
00640  * @param[in] context PPP context
00641  * @param[in,out] fsm Finite state machine
00642  * @param[in] callbacks FSM actions
00643  * @param[in] terminateReqPacket Terminate-Request packet received from the peer
00644  **/
00645 
00646 void pppRcvTerminateReqEvent(PppContext *context, PppFsm *fsm,
00647    const PppCallbacks *callbacks, const PppTerminatePacket *terminateReqPacket)
00648 {
00649    //Check current state
00650    switch(fsm->state)
00651    {
00652    case PPP_STATE_2_CLOSED:
00653    case PPP_STATE_3_STOPPED:
00654    case PPP_STATE_4_CLOSING:
00655    case PPP_STATE_5_STOPPING:
00656       //Send Terminate-Ack packet
00657       callbacks->sendTerminateAck(context, terminateReqPacket);
00658       //Stay in current state
00659       break;
00660    case PPP_STATE_6_REQ_SENT:
00661    case PPP_STATE_7_ACK_RCVD:
00662    case PPP_STATE_8_ACK_SENT:
00663       //Send Terminate-Ack packet
00664       callbacks->sendTerminateAck(context, terminateReqPacket);
00665       //Switch to the Req-Sent state
00666       pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00667       break;
00668    case PPP_STATE_9_OPENED:
00669       //Zero restart counter
00670       callbacks->zeroRestartCount(context);
00671       //Send Terminate-Ack packet
00672       callbacks->sendTerminateAck(context, terminateReqPacket);
00673       //Switch to the Stopping state
00674       pppChangeState(fsm, PPP_STATE_5_STOPPING);
00675       //Indicate to the upper layers that the automaton is leaving the Opened
00676       //state. The link is no longer available for network traffic
00677       callbacks->thisLayerDown(context);
00678       break;
00679    default:
00680       //This event cannot occur in a properly implemented automaton.
00681       //No transition is taken, and the implementation should not
00682       //reset or freeze
00683       break;
00684    }
00685 }
00686 
00687 
00688 /**
00689  * @brief Process Receive-Terminate-Ack event
00690  * @param[in] context PPP context
00691  * @param[in,out] fsm Finite state machine
00692  * @param[in] callbacks FSM actions
00693  **/
00694 
00695 void pppRcvTerminateAckEvent(PppContext *context, PppFsm *fsm,
00696    const PppCallbacks *callbacks)
00697 {
00698    //Check current state
00699    switch(fsm->state)
00700    {
00701    case PPP_STATE_2_CLOSED:
00702    case PPP_STATE_3_STOPPED:
00703       //Stay in current state
00704       break;
00705    case PPP_STATE_4_CLOSING:
00706       //Switch to the Closed state
00707       pppChangeState(fsm, PPP_STATE_2_CLOSED);
00708       //Indicate to the lower layers that the automaton is entering the
00709       //Initial, Closed or Stopped states. The lower layer is no longer
00710       //needed for the link
00711       callbacks->thisLayerFinished(context);
00712       break;
00713    case PPP_STATE_5_STOPPING:
00714       //Switch to the Stopped state
00715       pppChangeState(fsm, PPP_STATE_3_STOPPED);
00716       //Indicate to the lower layers that the automaton is entering the
00717       //Initial, Closed or Stopped states. The lower layer is no longer
00718       //needed for the link
00719       callbacks->thisLayerFinished(context);
00720       break;
00721    case PPP_STATE_6_REQ_SENT:
00722    case PPP_STATE_7_ACK_RCVD:
00723       //Switch to the Req-Sent state
00724       pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00725       break;
00726    case PPP_STATE_8_ACK_SENT:
00727       //Stay in current state
00728       break;
00729    case PPP_STATE_9_OPENED:
00730       //Send Configure-Req packet
00731       callbacks->sendConfigureReq(context);
00732       //Switch to the Req-Sent state
00733       pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00734       //Indicate to the upper layers that the automaton is leaving the Opened
00735       //state. The link is no longer available for network traffic
00736       callbacks->thisLayerDown(context);
00737       break;
00738    default:
00739       //This event cannot occur in a properly implemented automaton.
00740       //No transition is taken, and the implementation should not
00741       //reset or freeze
00742       break;
00743    }
00744 }
00745 
00746 
00747 /**
00748  * @brief Process Receive-Unknown-Code event
00749  * @param[in] context PPP context
00750  * @param[in,out] fsm Finite state machine
00751  * @param[in] callbacks FSM actions
00752  * @param[in] packet Un-interpretable packet received from the peer
00753  **/
00754 
00755 void pppRcvUnknownCodeEvent(PppContext *context, PppFsm *fsm,
00756    const PppCallbacks *callbacks, const PppPacket *packet)
00757 {
00758    //Check current state
00759    switch(fsm->state)
00760    {
00761    case PPP_STATE_2_CLOSED:
00762    case PPP_STATE_3_STOPPED:
00763    case PPP_STATE_4_CLOSING:
00764    case PPP_STATE_5_STOPPING:
00765    case PPP_STATE_6_REQ_SENT:
00766    case PPP_STATE_7_ACK_RCVD:
00767    case PPP_STATE_8_ACK_SENT:
00768    case PPP_STATE_9_OPENED:
00769       //Send Reject-Code packet
00770       callbacks->sendCodeRej(context, packet);
00771       //Stay in current state
00772       break;
00773    default:
00774       //This event cannot occur in a properly implemented automaton.
00775       //No transition is taken, and the implementation should not
00776       //reset or freeze
00777       break;
00778    }
00779 }
00780 
00781 /**
00782  * @brief Process Receive-Code-Reject or Receive-Protocol-Reject event
00783  * @param[in] context PPP context
00784  * @param[in,out] fsm Finite state machine
00785  * @param[in] callbacks FSM actions
00786  * @param[in] acceptable This parameter tells whether the rejected value
00787  *   is acceptable or catastrophic
00788  **/
00789 
00790 void pppRcvCodeRejEvent(PppContext *context, PppFsm *fsm,
00791    const PppCallbacks *callbacks, bool_t acceptable)
00792 {
00793    //Check whether the rejected value is acceptable or catastrophic
00794    if(acceptable)
00795    {
00796       //The RXJ+ event arises when the rejected value is acceptable, such
00797       //as a Code-Reject of an extended code, or a Protocol-Reject of a
00798       //NCP. These are within the scope of normal operation
00799       switch(fsm->state)
00800       {
00801       case PPP_STATE_2_CLOSED:
00802       case PPP_STATE_3_STOPPED:
00803       case PPP_STATE_4_CLOSING:
00804       case PPP_STATE_5_STOPPING:
00805       case PPP_STATE_6_REQ_SENT:
00806          //Stay in current state
00807          break;
00808       case PPP_STATE_7_ACK_RCVD:
00809          //Switch to the Req-Sent state
00810          pppChangeState(fsm, PPP_STATE_6_REQ_SENT);
00811          break;
00812       case PPP_STATE_8_ACK_SENT:
00813       case PPP_STATE_9_OPENED:
00814          //Stay in current state
00815          break;
00816       default:
00817          //This event cannot occur in a properly implemented automaton.
00818          //No transition is taken, and the implementation should not
00819          //reset or freeze
00820          break;
00821       }
00822    }
00823    else
00824    {
00825       //The RXJ- event arises when the rejected value is catastrophic,
00826       //such as a Code-Reject of Configure-Request, or a Protocol-Reject
00827       //of LCP! This event communicates an unrecoverable error that
00828       //terminates the connection
00829       switch(fsm->state)
00830       {
00831       case PPP_STATE_2_CLOSED:
00832       case PPP_STATE_3_STOPPED:
00833          //Indicate to the lower layers that the automaton is entering the
00834          //Initial, Closed or Stopped states. The lower layer is no longer
00835          //needed for the link
00836          callbacks->thisLayerFinished(context);
00837          //Stay in current state
00838          break;
00839       case PPP_STATE_4_CLOSING:
00840          //Switch to the Closed state
00841          pppChangeState(fsm, PPP_STATE_2_CLOSED);
00842          //Indicate to the lower layers that the automaton is entering the
00843          //Initial, Closed or Stopped states. The lower layer is no longer
00844          //needed for the link
00845          callbacks->thisLayerFinished(context);
00846          break;
00847       case PPP_STATE_5_STOPPING:
00848       case PPP_STATE_6_REQ_SENT:
00849       case PPP_STATE_7_ACK_RCVD:
00850       case PPP_STATE_8_ACK_SENT:
00851          //Switch to the Stopped state
00852          pppChangeState(fsm, PPP_STATE_3_STOPPED);
00853          //Indicate to the lower layers that the automaton is entering the
00854          //Initial, Closed or Stopped states. The lower layer is no longer
00855          //needed for the link
00856          callbacks->thisLayerFinished(context);
00857          break;
00858       case PPP_STATE_9_OPENED:
00859          //Initialize restart counter
00860          callbacks->initRestartCount(context, PPP_MAX_TERMINATE);
00861          //Send Terminate-Req packet
00862          callbacks->sendTerminateReq(context);
00863          //Switch to the Stopping state
00864          pppChangeState(fsm, PPP_STATE_5_STOPPING);
00865          //Indicate to the upper layers that the automaton is leaving the Opened
00866          //state. The link is no longer available for network traffic
00867          callbacks->thisLayerDown(context);
00868          break;
00869       default:
00870          //This event cannot occur in a properly implemented automaton.
00871          //No transition is taken, and the implementation should not
00872          //reset or freeze
00873          break;
00874       }
00875    }
00876 }
00877 
00878 
00879 /**
00880  * @brief Process Receive-Echo-Request event
00881  * @param[in] context PPP context
00882  * @param[in,out] fsm Finite state machine
00883  * @param[in] callbacks FSM actions
00884  * @param[in] echoReqPacket Echo-Request packet received from the peer
00885  **/
00886 
00887 void pppRcvEchoReqEvent(PppContext *context, PppFsm *fsm,
00888    const PppCallbacks *callbacks, const PppEchoPacket *echoReqPacket)
00889 {
00890    //Check current state
00891    switch(fsm->state)
00892    {
00893    case PPP_STATE_2_CLOSED:
00894    case PPP_STATE_3_STOPPED:
00895    case PPP_STATE_4_CLOSING:
00896    case PPP_STATE_5_STOPPING:
00897    case PPP_STATE_6_REQ_SENT:
00898    case PPP_STATE_7_ACK_RCVD:
00899    case PPP_STATE_8_ACK_SENT:
00900       //Stay in current state
00901       break;
00902    case PPP_STATE_9_OPENED:
00903       //Send Echo-Reply packet
00904       callbacks->sendEchoRep(context, echoReqPacket);
00905       //Stay in current state
00906       break;
00907    default:
00908       //This event cannot occur in a properly implemented automaton.
00909       //No transition is taken, and the implementation should not
00910       //reset or freeze
00911       break;
00912    }
00913 }
00914 
00915 
00916 /**
00917  * @brief Update PPP FSM state
00918  * @param[in,out] fsm Finite state machine
00919  * @param[in] newState New PPP state to switch to
00920  **/
00921 
00922 void pppChangeState(PppFsm *fsm, PppState newState)
00923 {
00924 #if (PPP_TRACE_LEVEL >= TRACE_LEVEL_INFO)
00925    //PPP FSM states
00926    static const char_t *stateLabel[] =
00927    {
00928       "INITIAL",  //0
00929       "STARTING", //1
00930       "CLOSED",   //2
00931       "STOPPED",  //3
00932       "CLOSING",  //4
00933       "STOPPING", //5
00934       "REQ_SENT", //6
00935       "ACK_RCVD", //7
00936       "ACK_SENT", //8
00937       "OPENED"    //9
00938    };
00939 
00940    //Sanity check
00941    if(fsm->state < arraysize(stateLabel) && newState < arraysize(stateLabel))
00942    {
00943       //Debug message
00944       TRACE_INFO("PPP FSM: %s (%u) -> %s (%u)\r\n", stateLabel[fsm->state],
00945          fsm->state, stateLabel[newState], newState);
00946    }
00947 #endif
00948 
00949    //Switch to the new state
00950    fsm->state = newState;
00951 }
00952 
00953 #endif
00954