Updates to MyTransport

This commit is contained in:
tekka007
2016-07-27 23:10:02 +02:00
parent dda9cefd93
commit 24e3dfa36c
3 changed files with 350 additions and 171 deletions
+142 -10
View File
@@ -20,8 +20,126 @@
/**
* @file MyTransport.h
*
* @defgroup MyTransportgrp MyTransport
* @ingroup internals
* @{
*
* Transport-related log messages, format: [!]SYSTEM:[SUB SYSTEM:]MESSAGE
* - [!] Exclamation mark is prepended in case of error
* - SYSTEM:
* - TSM: messages emitted by the transport state machine
* - TSF: messages emitted by transport support functions
* - SUB SYSTEMS:
* - Transport state machine (TSM)
* - TSM:INIT from <b>stInit</b> Initialize transport and radio
* - TSM:FPAR from <b>stParent</b> Find parent
* - TSM:ID from <b>stID</b> Check/request node ID, if dynamic node ID set
* - TSM:UPL from <b>stUplink</b> Verify uplink connection by pinging GW
* - TSM:READY from <b>stReady</b> Transport is ready and fully operational
* - TSM:FAILURE from <b>stFailure</b> Failure in transport link or transport HW
* - Transport support function (TSF)
* - TSF:CHKUPL from @ref transportCheckUplink(..), checks connection to GW
* - TSF:ASID from @ref transportAssignNodeID(..), assigns node ID
* - TSF:PING from @ref transportPingNode(..), pings a node
* - TSF:CRT from @ref transportClearRoutingTable(..), clears routing table stored in EEPROM
* - TSF:MSG from @ref transportProcessMessage(..), processes incoming message
* - TSF:SANCHK from @ref transportInvokeSanityCheck(..), calls transport-specific sanity check
* - TSF:ROUTE from @ref transportRouteMessage(..), sends message
* - TSF:SEND from @ref transportSendRoute(..), sends message if transport is ready (exposed)
*
* TSM state <b>stInit</b> log status / errors
* - TSM:INIT Transition to stInit state
* - TSM:INIT:STATID,ID=x Node ID x is static
* - TSM:INIT:TSP OK Transport device configured and fully operational
* - TSM:INIT:GW MODE Node is set up as GW, thus omitting ID and findParent states
* - !TSM:INIT:TSP FAIL Transport device initialization failed
*
* TSM state <b>stParent</b> log status / errors
* - TSM:FPAR Transition to stParent state
* - TSM:FPAR:STATP=x Static parent x set, skip finding parent
* - TSM:FPAR:OK Parent node identified
* - !TSM:FPAR:NO REPLY No potential parents replied to find parent request, retry
* - !TSM:FPAR:FAIL Finding parent failed, go to failure state
*
* TSM state <b>stID</b> log status / errors
* - TSM:ID Transition to stID state
* - TSM:ID:OK,ID=x Node ID x is valid
* - TSM:ID:REQ Request node ID from controller
* - !TSM:ID:FAIL,ID=x ID verification failed, ID x invalid
*
* TSM state <b>stUplink</b> log status / errors
* - TSM:UPL Transition to stUplink state
* - TSM:UPL:OK Uplink OK, GW returned ping
* - !TSM:UPL:FAIL Uplink check failed, i.e. GW could not be pinged
*
* TSM state <b>stReady</b> log status / errors
* - TSM:READY Transition to stReady, i.e. transport is ready and fully operational
* - !TSM:READY:UPL FAIL,SNP Too many failed uplink transmissions, search new parent
* - !TSM:READY:UPL FAIL,STATP Too many failed uplink transmissions, no SNP, static parent enforced
*
* TSM state <b>stFailure</b> information / status
* - TSM:FAILURE Transition to stFailure state
* - TSM:FAILURE:PDT Power-down transport
* - TSM:FAILURE:RE-INIT Attempt to re-initialize transport
*
* TSF information / status
* - TSF:CHKUPL:OK Uplink OK
* - TSF:CHKUPL:OK,FCTRL Uplink OK, flood control prevents pinging GW in too short intervals
* - TSF:CHKUPL:DGWC,O=x,N=y Checking uplink revealed a changed network topology, old distance x, new distance y
* - TSF:CHKUPL:FAIL No reply received when checking uplink
* - TSF:ASID:OK,ID=x Node ID x assigned
* - TSF:PING:SEND,TO=x Send ping to destination x
* - TSF:MSG:ACK REQ ACK message requested
* - TSF:MSG:ACK ACK message, do not proceed but forward to callback
* - TSF:MSG:FPAR RES,ID=x,D=y Response to find parent received from node x with distance y to GW
* - TSF:MSG:FPAR PREF FOUND Preferred parent found
* - TSF:MSG:FPAR OK,ID=x,D=y Find parent reponse from node x is valid, distance y to GW
* - TSF:MSG:FPAR INACTIVE Find parent response received, but no find parent request active, skip response
* - TSF:MSG:FPAR REQ,ID=x Find parent request from node x
* - TSF:MSG:PINGED,ID=x,HP=y Node pinged by node x with y hops
* - TSF:MSG:PONG RECV,HP=x Pinged node replied with x hops
* - TSF:MSG:BC Broadcast message received
* - TSF:MSG:GWL OK Link to GW ok
* - TSF:MSG:FWD BC MSG Controlled broadcast message forwarding
* - TSF:MSG:REL MSG Relay message
* - TSF:MSG:REL PxNG,HP=x Relay PING/PONG message, increment hop counter x
* - TSF:SANCHK:OK Sanity check passed. This ensures radio functionality and automatically attempts to re-initialize in case of failure
* - TSF:CRT:OK Clearing routing table successful
*
* Incoming / outgoing messages
*
* See <a href="https://www.mysensors.org/download/serial_api_20">here</a> for more detail on the format and definitons.
*
* Receiving a message
* - TSF:MSG:READ,sender-last-destination,s=%d,c=%d,t=%d,pt=%d,l=%d,sg=%d:%s
* Sending a message
* - [!]TSF:MSG:SEND,sender-last-next-destination,s=%d,c=%d,t=%d,pt=%d,l=%d,sg=%d,ft=%d,st=%s:%s
*
* - Message fields:
* - s=sensor ID
* - c=command
* - t=msg type
* - pt=payload type
* - l=length
* - ft=failed uplink transmission counter
* - sg=signing flag
*
* TSF errors
* - !TSF:ASID:FAIL,ID=x Assigned ID x is invalid, e.g. 0 (GATEWAY)
* - !TSF:ROUTE:FPAR ACTIVE Finding parent active, message not sent
* - !TSF:ROUTE:DST x UNKNOWN Routing for destination x unkown, send message to parent
* - !TSF:SEND:TNR Transport not ready, message cannot be sent
* - !TSF:MSG:PVER,x!=y Message protocol version mismatch
* - !TSF:MSG:SIGN VERIFY FAIL Signing verficiation failed
* - !TSF:MSG:REL MSG,NORP Node received a message for relaying, but node is not a repeater, message skipped
* - !TSF:MSG:SIGN FAIL Signing message failed
* - !TSF:MSG:GWL FAIL GW uplink failed
* - !TSF:SANCHK:FAIL Sanity check failed, attempt to re-intialize radio
*
* @brief API declaration for MyTransport
*
*/
#ifndef MyTransport_h
#define MyTransport_h
@@ -52,7 +170,7 @@
*
* This structure stores SM state definitions
*/
struct State {
struct transportState {
void(*Transition)(); //!< state transition function
void(*Update)(); //!< state update function
};
@@ -63,16 +181,21 @@ struct State {
* This structure stores transport status and SM variables
*/
typedef struct {
State* currentState; //!< pointer to current fsm state
transportState* currentState; //!< pointer to current fsm state
uint32_t stateEnter; //!< state enter timepoint
uint32_t lastUplinkCheck; //!< last uplink check, required to prevent GW flooding
uint32_t lastSanityCheck; //!< last sanity check
// 8 bits
bool findingParentNode : 1; //!< flag finding parent node is active
bool preferredParentFound : 1; //!< flag preferred parent found
bool uplinkOk : 1; //!< flag uplink ok
bool pingActive : 1; //!< flag ping active
bool transportActive : 1; //!< flag transport active
uint8_t reserved : 3; //!< reserved
// 8 bits
uint8_t retries : 4; //!< retries / state re-enter
uint8_t failedUplinkTransmissions : 4; //!< counter failed uplink transmissions
// 8 bits
uint8_t pingResponse; //!< stores hops received in I_PONG
} __attribute__((packed)) transportSM;
@@ -106,11 +229,11 @@ void stUplinkTransition();
/**
* @brief Set transport OK
*/
void stOKTransition();
void stReadyTransition();
/**
* @brief Monitor transport link
*/
void stOKUpdate();
void stReadyUpdate();
/**
* @brief Transport failure and power down radio
*/
@@ -123,7 +246,7 @@ void stFailureUpdate();
* @brief Switch SM state
* @param newState New state to switch SM to
*/
void transportSwitchSM(State& newState);
void transportSwitchSM(transportState& newState);
/**
* @brief Update SM state
*/
@@ -133,7 +256,10 @@ void transportUpdateSM();
* @return ms in current state
*/
uint32_t transportTimeInState();
/**
* @brief Call transport driver sanity check
*/
void transportInvokeSanityCheck();
/**
* @brief Process all pending messages in RX FIFO
*/
@@ -145,8 +271,9 @@ void transportProcessMessage();
/**
* @brief Assign node ID
* @param newNodeId New node ID
* @return true if node ID valid and successfully assigned
*/
void transportAssignNodeID(uint8_t newNodeId);
bool transportAssignNodeID(uint8_t newNodeId);
/**
* @brief Wait and process messages for a defined amount of time until specified message received
* @param ms Time to wait and process incoming messages in ms
@@ -164,7 +291,7 @@ uint8_t transportPingNode(uint8_t targetId);
/**
* @brief Send and route message according to destination
*
* This function is used in MyTransport and omits the transport state check, i.e. message can be sent even if transport is !OK
* This function is used in MyTransport and omits the transport state check, i.e. message can be sent even if transport is not ready
*
* @param message
* @return true if message sent successfully
@@ -201,10 +328,15 @@ void transportInitialize();
*/
void transportProcess();
/**
* @brief Verify transport status
* @brief Flag transport ready
* @return true if transport is initialize and ready
*/
bool isTransportOK();
bool isTransportReady();
/**
* @brief Flag searching parent ongoing
* @return true if transport is searching for parent
*/
bool isTransportSearchingParent();
/**
* @brief Clear routing table
*/