PUD: fix updating substate related information in transmitGpsInformation
authorFerry Huberts <ferry.huberts@pelagic.nl>
Wed, 14 Mar 2012 12:27:07 +0000 (13:27 +0100)
committerFerry Huberts <ferry.huberts@pelagic.nl>
Wed, 14 Mar 2012 14:22:29 +0000 (15:22 +0100)
Signed-off-by: Ferry Huberts <ferry.huberts@pelagic.nl>
lib/pud/src/receiver.c
lib/pud/src/state.c
lib/pud/src/state.h

index 6a365d9..074dfb8 100644 (file)
@@ -348,6 +348,7 @@ static void doImmediateTransmit(MovementState externalState, bool externalStateC
  */
 static void pud_gateway_timer_callback(void *context __attribute__ ((unused))) {
        union olsr_ip_addr bestGateway;
+       bool subStateExternalStateChange;
        bool externalStateChange;
        MovementState externalState;
        TristateBoolean movingNow = TRISTATE_BOOLEAN_UNSET;
@@ -368,15 +369,15 @@ static void pud_gateway_timer_callback(void *context __attribute__ ((unused))) {
         * State Determination
         */
 
-       externalStateChange = determineStateWithHysteresis(SUBSTATE_GATEWAY, movingNow, &externalState);
+       determineStateWithHysteresis(SUBSTATE_GATEWAY, movingNow, &externalState, &externalStateChange,
+                       &subStateExternalStateChange);
 
        /*
         * Update transmitGpsInformation
         */
 
-       if ((externalState == MOVEMENT_STATE_MOVING) || externalStateChange) {
+       if ((externalState == MOVEMENT_STATE_MOVING) || subStateExternalStateChange) {
                transmitGpsInformation.txGateway = bestGateway;
-               transmitGpsInformation.updated = true;
        }
 
        (void) pthread_mutex_unlock(&transmitGpsInformation.mutex);
@@ -687,7 +688,8 @@ bool receiverUpdateGpsInformation(unsigned char * rxBuffer, size_t rxCount) {
        PositionUpdateEntry * incomingEntry;
        PositionUpdateEntry * posAvgEntry;
        MovementType movementResult;
-       bool externalStateChange = false;
+       bool subStateExternalStateChange;
+       bool externalStateChange;
        bool updateTransmitGpsInformation = false;
        PositionUpdateEntry txPosition;
        MovementState externalState;
@@ -761,13 +763,14 @@ bool receiverUpdateGpsInformation(unsigned char * rxBuffer, size_t rxCount) {
         * State Determination
         */
 
-       externalStateChange = determineStateWithHysteresis(SUBSTATE_POSITION, movementResult.moving, &externalState);
+       determineStateWithHysteresis(SUBSTATE_POSITION, movementResult.moving, &externalState, &externalStateChange,
+                       &subStateExternalStateChange);
 
        /*
         * Update transmitGpsInformation
         */
 
-       updateTransmitGpsInformation = externalStateChange
+       updateTransmitGpsInformation = subStateExternalStateChange
                        || (positionValid(posAvgEntry) && !positionValid(&txPosition))
                        || (movementResult.inside == TRISTATE_BOOLEAN_SET);
 
index 12fa3a6..06cab4c 100644 (file)
@@ -88,12 +88,15 @@ MovementState getExternalState(void) {
  * the movement result of the sub-state
  * @param externalState
  * a pointer to the variable in which to store the new external state
+ * @param externalStateChange
+ * a pointer to the variable in which to store whether the external state changed
+ * @param subStateExternalStateChange
+ * a pointer to the variable in which to store whether the sub-state external state changed
  */
-bool determineStateWithHysteresis(SubStateIndex subStateIndex, TristateBoolean movingNow,
-               MovementState * externalState) {
+void determineStateWithHysteresis(SubStateIndex subStateIndex, TristateBoolean movingNow, MovementState * externalState,
+               bool * externalStateChange, bool * subStateExternalStateChange) {
        MovementState newState;
        bool internalStateChange;
-       bool externalStateChange;
        SubStateType * subState = &state.substate[subStateIndex];
 
        (void) pthread_mutex_lock(&state.mutex);
@@ -159,14 +162,15 @@ bool determineStateWithHysteresis(SubStateIndex subStateIndex, TristateBoolean m
                }
        }
 
-       externalStateChange = (subState->externalState != newState);
+       *subStateExternalStateChange = (subState->externalState != newState);
        subState->externalState = newState;
 
        /*
         * external state may transition into MOVING when either one of the sub-states say so (OR), and
         * may transition into STATIONARY when all of the sub-states say so (AND)
         */
-       if (externalStateChange) {
+       *externalStateChange = false;
+       if (*subStateExternalStateChange) {
                bool transition;
 
                if (newState == MOVEMENT_STATE_STATIONARY) {
@@ -183,17 +187,14 @@ bool determineStateWithHysteresis(SubStateIndex subStateIndex, TristateBoolean m
                }
 
                if (transition) {
-                       externalStateChange = (state.externalState != newState);
+                       *externalStateChange = (state.externalState != newState);
                        state.externalState = newState;
                }
        }
 
-
        *externalState = state.externalState;
 
        (void) pthread_mutex_unlock(&state.mutex);
-
-       return externalStateChange;
 }
 
 MovementState getInternalState(SubStateIndex subStateIndex) {
index cff8288..a365187 100644 (file)
@@ -47,7 +47,7 @@ bool initState(void);
 void destroyState(void);
 MovementState getExternalState(void);
 MovementState getInternalState(SubStateIndex subStateIndex);
-bool determineStateWithHysteresis(SubStateIndex subStateIndex, TristateBoolean movingNow,
-               MovementState * externalState);
+void determineStateWithHysteresis(SubStateIndex subStateIndex, TristateBoolean movingNow, MovementState * externalState,
+               bool * externalStateChange, bool * subStateExternalStateChange);
 
 #endif /* _PUD_STATE_H_ */