Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

src/ConfigurationHandler/ConfigurationHandler.cpp

Committer:
jmarkel44
Date:
2016-09-08
Revision:
28:c410a61238bb
Parent:
20:653923c2f37a
Child:
37:7e6986b77f01

File content as of revision 28:c410a61238bb:

/******************************************************************************
 *
 * File:                ConfigurationHandler.cpp
 * Desciption:          source for the ICE Configuration Handler
 *
 *****************************************************************************/
#include "ConfigurationHandler.h"
#include "global.h"
#include "SetpointControl.h"
#include "TimerControl.h"

// setpoint control table
typedef map<string, SetpointControl*> StringSetpointMap;
static StringSetpointMap setpointTable;

// timer control table
typedef map<string, TimerControl*> StringTimerMap;
static StringTimerMap timerTable;

// local function prototypes
static int loadPersistentControls(void);

static int createControl(const Message_t *msg);
static int modifyControl(const Message_t *msg);
static int destroyControl(const Message_t *msg);

/*****************************************************************************
 * Function:            ConfigurationHandler()
 * Description:         The ICE Configuration Handler
 *
 * @param               args (unused)
 * @return              none
 *****************************************************************************/
void ConfigurationHandler(void const *args)
{
    UNUSED(args);
    loadPersistentControls();
    osSignalSet(mainThreadId, sig_continue);

    while ( true ) {
        // wait for an event
        osEvent evt = MailBox.get();
        if (evt.status == osEventMail) {
            Message_t *msg = (Message_t*) evt.value.p;

            printf("\r%s: mes->action      = %d\n", __func__, msg->action);
            printf("\r%s: msg->control     = %d\n", __func__, msg->control);
            printf("\r%s: msg->controlFile = %s\n", __func__, msg->controlFile);

            switch ( msg->action ) {
                case ACTION_CREATE: {
                    (void)createControl(msg);
                    break;
                }
                case ACTION_MODIFY: {
                    (void)modifyControl(msg);
                    break;
                }
                case ACTION_DESTROY: {
                    (void)destroyControl(msg);
                    break;
                }
                default:
                    break;
            }

            // free the message
            MailBox.free(msg);
        }
    }
}

/*****************************************************************************
 * Function:            ConfigurationHandler_showControls()
 * Description:         show the controls
 *
 * @param               msg
 * @return              none
 *****************************************************************************/
void ConfigurationHandler_showControls(void)
{
    if ( !timerTable.empty() ) {
        printf("\rTIMER CONTROLS\n");
        StringTimerMap::iterator pos;
        for ( pos = timerTable.begin();
                pos != timerTable.end();
                ++pos ) {
            printf("\r  control file: %32s\n", 
                pos->second->getControlFile().c_str());
        }
    }
    if ( !setpointTable.empty() ) {
        printf("\rSETPOINT CONTROLS\n");
        StringSetpointMap::iterator pos;
        for ( pos = setpointTable.begin();
                pos != setpointTable.end();
                ++pos ) {
            printf("\r      controlFile : %s   \n", pos->second->getControlFile().c_str());
            printf("\r               id : %s   \n", pos->second->getId().c_str());
            printf("\r             name : %s   \n", pos->second->getName().c_str());
            printf("\r         priority : %d   \n", pos->second->getPriority());
            printf("\r            input : %s   \n", pos->second->getInput().c_str());
            printf("\r           output : %s   \n", pos->second->getOutput().c_str());
            printf("\r         prodfact : %0.2f\n", pos->second->getProductFactor());
            printf("\r           halert : %0.2f\n", pos->second->getHighAlert());
            printf("\r           lalert : %0.2f\n", pos->second->getLowAlert());
            printf("\r              hfs : %0.2f\n", pos->second->getHighFailsafe());
            printf("\r              lfs : %0.2f\n", pos->second->getLowFailsafe());
            printf("\r              tol : %0.2f\n", pos->second->getTol());
            printf("\r\n");
        }
    }
}

/*****************************************************************************
 * Function:            loadPersistentControls()
 * Description:         load persistent controls from flash
 *
 * @param               none
 * @return              none
 *****************************************************************************/
static int loadPersistentControls(void)
{
    static bool loaded = false;

    if ( !loaded ) {        // lazy protection
        // do something
        loaded = true;
    }
    return 0;
}

/*****************************************************************************
 * Function:            createControl()
 * Description:         creates a new control
 *
 * @param               none
 * @return              none
 *****************************************************************************/
static int createControl(const Message_t *msg)
{
    printf("\r%s invoked\n", __func__);

    switch (msg->control) {
        case CONTROL_SETPOINT: {
            SetpointControl *setpointControl = new SetpointControl();
            bool rc = setpointControl->load(msg->controlFile);
            if ( rc != true ) {
                logError("%s: failed to load %s\n", __func__, msg->controlFile);
                delete setpointControl;
            } else {
                setpointTable[msg->controlFile] = setpointControl;
            }
            break;
        }
        case CONTROL_TIMER: {
            TimerControl *timerControl = new TimerControl();
            bool rc = timerControl->load(msg->controlFile);
            if ( rc != true ) {
                logError("%s: failed to load %s\n", __func__, msg->controlFile);
                delete timerControl;
            } else {
                timerTable[msg->controlFile] = timerControl;
            }
            break;
        }
        case CONTROL_PID:
        case CONTROL_MANUAL:
        case CONTROL_COMPOSITE:
        default:
            printf("\r%s: control type %d not implemented yet...\n", 
                __func__, msg->control);
            break;
    }
    return 0;
}

/*****************************************************************************
 * Function:            modifyControl()
 * Description:         modifies a control
 *
 * @param               msg
 * @return              none
 *****************************************************************************/
static int modifyControl(const Message_t *msg)
{
    printf("\r%s invoked\n", __func__);
    return 0;

}

/*****************************************************************************
 * Function:            destroyControl()
 * Description:         destroys a controls
 *
 * @param               msg
 * @return              none
 *****************************************************************************/
static int destroyControl(const Message_t *msg)
{
    printf("\r%s invoked\n", __func__);

    switch ( msg->control ) {
        case CONTROL_SETPOINT: {
            StringSetpointMap::iterator pos;
            pos = setpointTable.find(msg->controlFile);
            if ( pos != setpointTable.end() ) {
                delete (pos->second);
                setpointTable.erase(pos);
            }
            break;
        }
        case CONTROL_TIMER: {
            StringTimerMap::iterator pos;
            pos = timerTable.find(msg->controlFile);
            if ( pos != timerTable.end() ) {
                delete (pos->second);
                timerTable.erase(pos);
            }
            break;
        }
        default:
            break;
    }
    return 0;
}