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-20
Revision:
93:1553fb156915
Parent:
90:7d9731dec0da
Child:
97:5cf6ab71dcd0

File content as of revision 93:1553fb156915:

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

StringSetpointMap   setpointTable;          // setpoint control object table
StringTimerMap      timerTable;             // timer control object table
//StringManualMap     manualTable;          // manual control object table

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

// local helper functions
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_config_continue);

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

            logInfo("\r%s: mes->action      = %d\n", __func__, msg->action);
            logInfo("\r%s: msg->control     = %d\n", __func__, msg->control);
            logInfo("\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 ) {
            pos->second->display();
        }
    }
}

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

    if ( !loaded ) {        // lazy protection
    
        printf("\rLoading persistent controls: \n");
        std::vector<mDot::mdot_file> file_list = GLOBAL_mdot->listUserFiles();

        for (std::vector<mDot::mdot_file>::iterator i = file_list.begin(); i != file_list.end(); ++i) {
            if( strncmp( i->name, CONTROL_SP_STR, strlen(CONTROL_SP_STR)) == 0 ) {
                // create the setpoint control
                Message_t msg;
                msg.control = CONTROL_SETPOINT;
                strncpy(msg.controlFile, i->name, sizeof(msg.controlFile));
                int rc = createControl(&msg);
                if ( rc != 0 ) {
                } else {
                    printf("\r  control %s loaded.\n", msg.controlFile);
                }
            } else if ( strncmp( i->name, CONTROL_TM_STR, strlen(CONTROL_TM_STR)) == 0  ) {
                // create the timer control 
                Message_t msg;
                msg.control = CONTROL_TIMER;
                strncpy(msg.controlFile, i->name, sizeof(msg.controlFile));
                int rc = createControl(&msg);
                if ( rc != 0 ) {
                } else {
                    printf("\r  control %s loaded.\n", msg.controlFile);
                }
            } else if ( strncmp( i->name, CONTROL_MN_STR, strlen(CONTROL_MN_STR)) == 0 ) {
                // TODO: load the manual control -> these may not persist??
                // do nothing 
            } else {
                // not a control file
            }
        }
    }
    return 0;
}

/*****************************************************************************
 * Function:            createControl()
 * Description:         creates a new control
 *
 * @param               none
 * @return              none
 *****************************************************************************/
static int createControl(const Message_t *msg)
{
    logInfo("\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;
                // start the setpoint control
                setpointControl->start();
            }
            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_MANUAL: {
#if 0
            ManualControl *manualControl = new ManualControl;
            bool rc = manualControl->load(msg->controlFile);
            if ( rc != true ) {
                logError("%s: failed to load %s\n", __func__, msg->controlFile);
                delete manualControl;
            } else {
                manualTable[msg->controlFile] = manualControl;
            }
#endif
            break;
        }
        case CONTROL_PID:
        case CONTROL_COMPOSITE:
        default:
            logInfo("\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)
{
    logInfo("\r%s invoked\n", __func__);

    // TODO: can we delete the current object and start a new one?
    return 0;

}

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

    switch ( msg->control ) {
        case CONTROL_SETPOINT: {
            StringSetpointMap::iterator pos;
            pos = setpointTable.find(msg->controlFile);
            if ( pos != setpointTable.end() ) {
                pos->second->unregisterControl();
                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;
        }
        case CONTROL_MANUAL: {
#if 0
            StringManualMap::iterator pos;
            pos = manualTable.find(msg->controlFile);
            if ( pos != manualTable.end() ) {
                delete (pos->second);
                manualTable.erase(pos);
            }
#endif
            break;
        }
        default:
            break;
    }
    return 0;
}