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-07
Revision:
11:0695c1091129
Parent:
5:5e77a1db4d45

File content as of revision 11:0695c1091129:

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

using namespace std;

// 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);

// dynamic control lists
typedef map<string, SetpointControl*> SetpointControlMap;
static SetpointControlMap setpointControlTable;

typedef map<string, TimerControl *> TimerControlMap;
static TimerControlMap timerControlTable;

/*****************************************************************************
 * Function:            ConfigurationHandler()
 * Description:         Configuration Handler thread entry point
 *
 * @param               none
 * @return              none
 *****************************************************************************/
void ConfigurationHandler(void const *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:            loadPersistentControls()
 * Description:         load persistent controls from flash
 *
 * @param               none
 * @return              none
 *****************************************************************************/
static int loadPersistentControls(void)
{
    static bool loaded = false;

    if ( !loaded ) {    // lazy protection
        // load the controls from flash
        printf("\r%s: stubbed.\n", __func__);
        loaded = true;
    }
    return 0;
}

/*****************************************************************************
 * Function:            createControl()
 * Description:         create a new control
 *
 * @param               msg <Message_t> : message from a data handler
 * @return              <some error code>
 *****************************************************************************/
static int createControl(const Message_t *msg)
{
    printf("\r%s invoked\n", __func__);

    switch (msg->control) {
        case CONTROL_SETPOINT: {
            SetpointControl *SPcontrol = new SetpointControl(msg->controlFile);
            setpointControlTable[msg->controlFile] = SPcontrol;
            break;
        }
        case CONTROL_TIMER: {
            TimerControl *timerControl = new TimerControl(msg->controlFile);
            timerControlTable[msg->controlFile] = timerControl;
            break;
        }
        default:
            break;
    }
    return 0;
}

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

}

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

    switch (msg->control) {
        case CONTROL_SETPOINT: {
            SetpointControlMap::iterator sp_iter;
            sp_iter = setpointControlTable.find(msg->controlFile);
            if ( sp_iter != setpointControlTable.end() ) {
                // free the object and delete the entry
                delete (sp_iter->second);
                setpointControlTable.erase(sp_iter);
            }
            break;
        }
        case CONTROL_TIMER: {
            TimerControlMap::iterator tc_iter;
            tc_iter = timerControlTable.find(msg->controlFile);
            if ( tc_iter != timerControlTable.end() ) {
                // free the timer object and delete the entry
                delete (tc_iter->second);
                timerControlTable.erase(tc_iter);
            }
            break;
        }
        default:
            break;
    }
    return 0;
}

/*****************************************************************************
 * Function:            DisplayControlLists()
 * Description:         Display the control lists
 *
 * @param               none
 * @return              none
 *****************************************************************************/
void DisplayControlLists(void)
{
    SetpointControlMap::iterator sp_iter;
    TimerControlMap::iterator    tc_iter;

    printf("\rSETPOINT CONTROLS:\n");
    for ( sp_iter = setpointControlTable.begin();
            sp_iter != setpointControlTable.end();
            ++sp_iter ) {
        SetpointControl *s = sp_iter->second;
        printf("\r  control file : %32s\n", s->getControlFile().c_str());
    }
    printf("\rTIMER CONTROLS:\n");
    for ( tc_iter = timerControlTable.begin();
            tc_iter != timerControlTable.end();
            ++tc_iter) {
        TimerControl *t = tc_iter->second;
        printf("\r  control file : %32s\n", t->getControlFile().c_str());
    }
}