Erick / Mbed 2 deprecated ICE-F412

Dependencies:   mbed-rtos mbed

ICE-Application/src/ControlTask/ControlTask.cpp

Committer:
jmarkel44
Date:
2017-01-24
Revision:
0:61364762ee0e

File content as of revision 0:61364762ee0e:

/******************************************************************************
 *
 * File:                ControlTask.cpp
 * Desciption:          source for the ICE Control task
 *
 *****************************************************************************/

#include "ControlTask.h"
#include <stdio.h>
#include "rtos.h"
#include "ConfigurationHandler.h"
#include "ICELog.h"
#include "global.h"

// control functions
static void serviceManualControls(void);
static void serviceTimerControls(void);
static void serviceSetpointControls(void);
static void serviceCompositeControls(void);
static void serviceFailsafeControls(void);
static void serviceSequenceControls(void);

static void sendMail(std::string controlFile, Action_t action, Control_t control);

//
// Task:               ControlTask
// Description:        This task will loop through all of the configued control
//                     and run the updater
//
// @param[in]          none
// @param[out]         none
// @return             none
//
void ControlTask(void const *args)
{
    printf("\r%s has started...\n", __func__);

    while ( true ) {

        serviceTimerControls();
        serviceManualControls();
        serviceSetpointControls();
        serviceCompositeControls();
        serviceFailsafeControls();
        serviceSequenceControls();
    }
}

//
// function:            serviceManualControls()
// description:         run the updater on the manual controls
//
// @param[in]           none
// @param[out]          none
// @return              none
//
static void serviceManualControls(void)
{
    ManualControlError_t rc;

    manual_mutex.lock();
    if ( !manualTable.empty() ) {
        StringManualMap::iterator pos;
        for ( pos = manualTable.begin(); pos != manualTable.end(); ++pos) {
            rc = pos->second->update();
            if ( rc == MANUAL_CONTROL_DESTROY ) {
                // send mail to the configuration handler to destroy this control
                sendMail(pos->second->getControlFile(), ACTION_DESTROY, CONTROL_MANUAL);
            } else if ( rc != MANUAL_CONTROL_OK ) {
                logError("%s: error %d updating the manual control", __func__, rc);
            }
        }
    }
    manual_mutex.unlock();
}

//
// function:            serviceSetpointControls
// description:         run the updater on the setpoint controls
//
// @param[in]           none
// @param[out]          none
// @return              none
//
static void serviceSetpointControls(void)
{
    SetpointControlError_t  rc;

    setpoint_mutex.lock();
    if ( !setpointTable.empty() ) {
        StringSetpointMap::const_iterator pos;
        for ( pos = setpointTable.begin(); pos != setpointTable.end(); ++pos ) {
            // run the updater
            rc = pos->second->update();
            if ( rc != SETPOINT_CONTROL_OK ) {
                logError("%s: error (%d) updating setpoint control %s",
                         __func__, rc, pos->second->getId().c_str());
            }
        }
    }
    setpoint_mutex.unlock();
}

//
// function:            serviceTimerControls
// description:         run the updater on the timer controls
//
// @param[in]           none
// @param[out]          none
// @return              none
//
static void serviceTimerControls(void)
{
    TimerControlError_t rc;

    timer_mutex.lock();
    if ( !timerTable.empty() ) {
        StringTimerMap::const_iterator pos;
        for ( pos = timerTable.begin(); pos != timerTable.end(); ++pos ) {
            // run the updater
            rc = pos->second->update();
            if ( rc != TIMER_CONTROL_OK ) {
                logError("%s: error (%d) updating timer control %s",
                         __func__, rc, pos->second->getId().c_str());
            }
        }
    }
    timer_mutex.unlock();
}

//
// function:           serviceCompositeControls
// description:        run the updater on the composite controls
//
// @param[in]          none
// @param[out]         none
// @return             none
//
static void serviceCompositeControls(void)
{
    CompositeControlError_t rc;

    // service the composite controls
    composite_mutex.lock();
    if ( !compositeTable.empty() ) {
        StringCompositeMap::const_iterator pos;
        for ( pos = compositeTable.begin(); pos != compositeTable.end(); ++pos ) {
            // run the updater
            rc = pos->second->update();
            if ( rc != COMPOSITE_CONTROL_OK ) {
                logError("%s: error (%d) updating composite control %s",
                         __func__, rc, pos->second->getId().c_str());
            }
        }
    }
    composite_mutex.unlock();
}

//
// function:           serviceFailsafeControls
// description:        run the updater on the failsafe controls
//
// @param[in]          none
// @param[out]         none
// @return             none
//
static void serviceFailsafeControls(void)
{
    FailsafeControlError_t  rc;

    // service the setpoint controls
    failsafe_mutex.lock();
    if ( !failsafeTable.empty() ) {
        StringFailsafeMap::const_iterator pos;
        for ( pos = failsafeTable.begin(); pos != failsafeTable.end(); ++pos ) {
            // run the updater
            rc = pos->second->update();
            if ( rc != FAILSAFE_CONTROL_OK ) {
                logError("%s: error (%d) updating failsafe control %s",
                         __func__, rc, pos->second->getId().c_str());
            }
        }
    }
    failsafe_mutex.unlock();
}

static void serviceSequenceControls(void)
{
    SequenceControlError_t rc;

    // service the sequence controls
    sequence_mutex.lock();
    if ( !sequenceTable.empty() ) {
        StringSequenceMap::const_iterator pos;
        for ( pos = sequenceTable.begin(); pos != sequenceTable.end(); ++pos ) {
            rc = pos->second->run();
            if ( rc != SEQUENCE_CONTROL_OK ) {
                logError("%s: error (%d) running sequence control %s",
                         __func__, rc, pos->second->getId().c_str());
            }
        }
    }
    sequence_mutex.unlock();
}

//
// function:            sendMail
// desctiption:         send mail to the configuration handler
//
// @param[in]           controlFile -> key
// @param[in]           action -> destroy, create, modify, etc.
// @param[in]           control -> manual, setpoint...
// @param[out]          none
// @return              none
//
static void sendMail(std::string controlFile, Action_t action, Control_t control)
{
    ConfigMessage_t *msg  = ConfigHandlerMailBox.alloc();
    memset(msg, 0, sizeof(ConfigMessage_t));
    msg->action  = action;
    msg->control = control;
    strncpy(msg->controlFile, controlFile.c_str(), sizeof(msg->controlFile)-1);

    printf("%s: Sending a destroy request for control %s\r\n",
           __func__, msg->controlFile);

    ConfigHandlerMailBox.put(msg);
}