Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Revision:
11:0695c1091129
Parent:
5:5e77a1db4d45
--- a/src/ConfigurationHandler/ConfigurationHandler.cpp	Tue Sep 06 19:35:50 2016 +0000
+++ b/src/ConfigurationHandler/ConfigurationHandler.cpp	Wed Sep 07 12:06:02 2016 +0000
@@ -7,6 +7,11 @@
 #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);
@@ -15,12 +20,20 @@
 static int modifyControl(const Message_t *msg);
 static int destroyControl(const Message_t *msg);
 
-static int loadPersistentControls(void)
-{
-    printf("\r%s: stubbed.\n", __func__);
-    return 0;
-}
+// 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();
@@ -59,19 +72,45 @@
     }
 }
 
+/*****************************************************************************
+ * 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 *setpointControl = new SetpointControl(msg->controlFile);
-            printf("\r%s: setpoint control file = %s\n", __func__, setpointControl->getControlFile().c_str());
-            
+            SetpointControl *SPcontrol = new SetpointControl(msg->controlFile);
+            setpointControlTable[msg->controlFile] = SPcontrol;
             break;
         }
         case CONTROL_TIMER: {
-            // do something similar here 
+            TimerControl *timerControl = new TimerControl(msg->controlFile);
+            timerControlTable[msg->controlFile] = timerControl;
             break;
         }
         default:
@@ -79,15 +118,83 @@
     }
     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());
+    }
 }