Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Revision:
2:da28f21b72a1
Parent:
1:057d8fc6cb2f
Child:
3:8ea4db957749
--- a/src/ConfigurationHandler/ConfigurationHandler.cpp	Thu Sep 01 19:42:11 2016 +0000
+++ b/src/ConfigurationHandler/ConfigurationHandler.cpp	Thu Sep 01 20:32:42 2016 +0000
@@ -23,15 +23,15 @@
 static void     displayThreadTable( void );
 
 // control spawners
-static Thread*  spawnNewTimerControl( const char * );
-static Thread*  spawnNewPIDControl( const char * );
-static Thread*  spawnNewSetpointControl( const char * );
-static Thread*  spawnNewCompositeControl(const char *);
-static Thread*  spawnNewManualControl(const char *);
+static void     spawnNewTimerControl( const char *, Tcb_t *);
+static void     spawnNewPIDControl( const char *, Tcb_t * );
+static void     spawnNewSetpointControl( const char *, Tcb_t * );
+static void     spawnNewCompositeControl( const char *, Tcb_t *);
+static void     spawnNewManualControl( const char *, Tcb_t * );
 
 // control thread table
-typedef map<string, Thread *> StringThreadMap;
-static StringThreadMap threadTable;
+typedef map<string,  Tcb_t> StringTcbMap;
+static StringTcbMap threadTable;
 
 /*****************************************************************************
  * Function:             ConfigurationHandler
@@ -42,10 +42,9 @@
  *****************************************************************************/
 void ConfigurationHandler(void const *args)
 {
-    int rc;
     printf("\r%s is started...\n", __func__);
 
-    //loadPersistentControls();
+    loadPersistentControls();
 
     osSignalSet(mainThreadId, sig_continue);
 
@@ -66,14 +65,9 @@
                     break;
                 default:
                     printf("\r\n%s: ERROR\n", __FUNCTION__);
-                    rc = -1;
                     break;
             }
 
-            if ( rc != 0 ) {
-                // do something
-            }
-
             // free the allocated memory for the message
             MailBox.free(msg);
         }
@@ -115,34 +109,36 @@
     printf("\r\n%s: invoked with %s [%u]\n",
            __func__, msg->controlFile, msg->control);
     
+    Tcb_t   tcb;
+    
     switch (msg->control) {
         case CONTROL_TIMER: {
-            Thread *t = spawnNewTimerControl(msg->controlFile);
-            threadTable[msg->controlFile] = t;
+            spawnNewTimerControl(msg->controlFile, &tcb);
+            threadTable[msg->controlFile] = tcb;
             break;
         }
         case CONTROL_PID: {
-            Thread *t = spawnNewPIDControl(msg->controlFile);
-            threadTable[msg->controlFile] = t;
+            spawnNewPIDControl(msg->controlFile, &tcb);
+            threadTable[msg->controlFile] = tcb;
             break;
         }
         case CONTROL_SETPOINT: {
-            Thread *t = spawnNewSetpointControl(msg->controlFile);
-            threadTable[msg->controlFile] = t;
+            spawnNewSetpointControl(msg->controlFile, &tcb);
+            threadTable[msg->controlFile] = tcb;
             break;
         }
         case CONTROL_COMPOSITE: {
-            Thread *t = spawnNewCompositeControl(msg->controlFile);
-            threadTable[msg->controlFile] = t;
+            spawnNewCompositeControl(msg->controlFile, &tcb);
+            threadTable[msg->controlFile] = tcb;
             break;
         }
         case CONTROL_MANUAL: {
-            Thread *t = spawnNewManualControl(msg->controlFile);
-            threadTable[msg->controlFile] = t;
+            spawnNewManualControl(msg->controlFile, &tcb);
+            threadTable[msg->controlFile] = tcb;
             break;
         }
         default:
-            printf("%s: unkown control type (%u)", __func__, msg->control);
+            printf("\r%s: unknown control type (%u)\n", __func__, msg->control);
             break;
     }
 }
@@ -169,35 +165,52 @@
  *****************************************************************************/
 static void destroyHandler(const Message_t *msg)
 {
-    printf("\r\n%s: invoked with %s\n", __FUNCTION__, msg->controlFile);
+    printf("\r%s: invoked with %s\n", __FUNCTION__, msg->controlFile);
 
-    StringThreadMap::iterator pos;
+    StringTcbMap::iterator pos;
 
     // find the control thread, terminate it, then delete the entry
     // from the thread table.
     pos = threadTable.find(msg->controlFile);
     if ( pos != threadTable.end() ) {
-        Thread *t = pos->second;
-        t->terminate();
-        // perform cleanup
-        delete t;
+        Tcb_t t = (Tcb_t)pos->second;
+        t.thread->terminate();
+        delete t.thread;
+        free(t.args); 
         // remove the entry from the map
         threadTable.erase(pos);
     }
 }
 
-static Thread *spawnNewTimerControl( const char *controlFile)
+/*****************************************************************************
+ * Function:             spawnNewTimerControl
+ * Description:          spawn a new timer control 
+ *
+ * @param                (IN) args (user-defined arguments)
+ * @return               none
+ *****************************************************************************/
+static void spawnNewTimerControl( const char *controlFile, Tcb_t *tcb)
 {
-    // TODO: Be sure to free these args when the thread dies 
     Args_t *args = (Args_t*)malloc(sizeof(Args_t));
     
     strncpy(args->controlFile, controlFile, sizeof(args->controlFile));
     args->reserved = 0;
     
     Thread *t = new Thread(TimerControl, (void *)args);
-    return t;
+    
+    tcb->thread = t;
+    tcb->args   = args;
+    return;
 }
-static Thread*  spawnNewPIDControl( const char *controlFile )
+
+/*****************************************************************************
+ * Function:             spawnNewPIDControl
+ * Description:          spawn a new PID control 
+ *
+ * @param                (IN) args (user-defined arguments)
+ * @return               none
+ *****************************************************************************/
+static void spawnNewPIDControl( const char *controlFile, Tcb_t *tcb )
 {
     Args_t *args = (Args_t *)malloc(sizeof(Args_t));
     
@@ -205,10 +218,20 @@
     args->reserved = 0;
     
     Thread *t = new Thread(PIDControl, (void *)args);
-    return t;
+    
+    tcb->thread = t;
+    tcb->args   = args; 
+    return;
 }
 
-static Thread*  spawnNewSetpointControl( const char *controlFile )
+/*****************************************************************************
+ * Function:             spawnNewSetpointControl
+ * Description:          spawn a new setpoint control 
+ *
+ * @param                (IN) args (user-defined arguments)
+ * @return               none
+ *****************************************************************************/
+static void spawnNewSetpointControl( const char *controlFile, Tcb_t *tcb )
 {
     Args_t *args = (Args_t *)malloc(sizeof(Args_t));
     
@@ -216,10 +239,20 @@
     args->reserved = 0;
     
     Thread *t = new Thread(SetpointControl, (void *)args);
-    return t;
+    
+    tcb->thread = t;
+    tcb->args   = args;
+    return;
 }
 
-static Thread* spawnNewCompositeControl(const char *controlFile)
+/*****************************************************************************
+ * Function:             spawnNewCompositeControl
+ * Description:          spawn a new composite control 
+ *
+ * @param                (IN) args (user-defined arguments)
+ * @return               none
+ *****************************************************************************/
+static void spawnNewCompositeControl(const char *controlFile, Tcb_t *tcb )
 {
     Args_t *args = (Args_t *)malloc(sizeof(Args_t));
     
@@ -227,10 +260,21 @@
     args->reserved = 0;
 
     Thread *t = new Thread(CompositeControl, (void *)args);
-    return t;
+    
+    tcb->thread = t;
+    tcb->args   = args;
+    
+    return;
 }
 
-static Thread*  spawnNewManualControl(const char *controlFile)
+/*****************************************************************************
+ * Function:             spawnNewManualControl
+ * Description:          spawn a new manual control 
+ *
+ * @param                (IN) args (user-defined arguments)
+ * @return               none
+ *****************************************************************************/
+static void spawnNewManualControl(const char *controlFile, Tcb_t *tcb)
 {
     Args_t *args = (Args_t *)malloc(sizeof(Args_t));
     
@@ -238,7 +282,11 @@
     args->reserved = 0;
 
     Thread *t = new Thread(ManualControl, (void *)args);
-    return t;
+    
+    tcb->thread = t;
+    tcb->args   = args;
+    
+    return;
 }
 
 /*****************************************************************************
@@ -250,7 +298,7 @@
  *****************************************************************************/
 static void displayThreadTable(void)
 {
-    StringThreadMap::iterator pos;
+    StringTcbMap::iterator pos;
 
     // this must exactly match the OS thread states in Thread.h
     const char *stateMapper[] = {   "Inactive",
@@ -273,15 +321,14 @@
 
     printf("\rControl Thread Table\n");
     for ( pos = threadTable.begin(); pos != threadTable.end(); ++pos ) {
-        Thread *t = (Thread *)pos->second;
+        Tcb_t t = (Tcb_t)pos->second;
         printf("\r[%32s]->\tpri=%u\tstate=%s\tstack_size=%u\tfree=%u\tused=%u\tmax=%u\r\n",
                pos->first.c_str(),
-               t->get_priority(),
-               stateMapper[t->get_state()],
-               t->stack_size(),
-               t->free_stack(),
-               t->used_stack(),
-               t->max_stack());
+               t.thread->get_priority(),
+               stateMapper[t.thread->get_state()],
+               t.thread->stack_size(),
+               t.thread->free_stack(),
+               t.thread->used_stack(),
+               t.thread->max_stack());
     }
-}
-
+}
\ No newline at end of file