Index: logic/trunk/src/logics/device_threads.h
===================================================================
--- logic/trunk/src/logics/device_threads.h	(revision 59)
+++ logic/trunk/src/logics/device_threads.h	(revision 60)
@@ -1,5 +1,11 @@
 #ifndef DEVICE_THREADS_H_INCLUDED
 #define DEVICE_THREADS_H_INCLUDED
+#include <pthread.h>
+
+typedef struct thread_id_holder_s
+{   pthread_t                 pthr_idx;
+    struct thread_id_holder_s *nxt;
+} thread_id_holder;
 
 void *randomValThread_routine(void *dummy);
 
Index: logic/trunk/src/startup/main.c
===================================================================
--- logic/trunk/src/startup/main.c	(revision 59)
+++ logic/trunk/src/startup/main.c	(revision 60)
@@ -8,7 +8,7 @@
 #include "../hci_comm/hci_comm.h"
 #include "../init/scenariolist.h"
 #include "../init/devicelist.h"
-//#include "../logics/device_threads.h"
+#include "../logics/device_threads.h"
 //#include "../logics/scenario_threads.h"
 #include <stdio.h>
 #include <pthread.h>
@@ -18,13 +18,16 @@
 // Inter-thread shared variables
 // TODO: These variables could be configuration variables
 //       (eg: static boolean loggingEnabled)
-static device   *hDevice;   //device list
-static scenario *hScenario; //scenario list
-
+static device           *hDevice;   //device list
+static scenario         *hScenario; //scenario list
+static thread_id_holder *hThrIDhold_dev;//holds IDs of running threads (devices)
+static thread_id_holder *hThrIDhold_scn;//holds IDs of running threads (scenarios)
 //extern scenario *test;   //device list
 
 // Crtitical section mutual exclusion security
 pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
+pthread_t       decisionMakingThread;
+pthread_t       hciConnectionThread;
 
 // Global variable to check, if HCI made any changes that need
 // to be updated by the logic.
@@ -48,6 +51,7 @@
 void *hciConnectionThread_routine(void *dummy);
 void *randomValThread_routine(void *dummy);
 void *scenarioThread_routine(void *dummy);
+int  reinitialiseLogic(); // on HCI request
 
 // Application entry point
 int main(int argc, char *argv[]) {
@@ -61,16 +65,11 @@
 
     /* regular work */
     //*
-	pthread_t hciConnectionThread;
-	pthread_t decisionMakingThread;
-
-	pthread_t *decisionMakingThread2;
-
 	pthread_create(&hciConnectionThread, NULL,hciConnectionThread_routine, NULL);
 	pthread_create(&decisionMakingThread,NULL,decisionMakingThread_routine,NULL);
 
 	pthread_join(hciConnectionThread,NULL);
-	pthread_join(decisionMakingThread,NULL);
+	//pthread_join(decisionMakingThread,NULL);
     /**/
 	exit(0);
 }
@@ -81,51 +80,45 @@
     printf("-- decisionMakingThread_routine started --\n");
 
     /**dummy threads for input devices (sensors)**/
-    // TODO check for possible BUG involving declaring pthread_t as pointer!
-    pthread_t *deviceThread, *deviceThreadHead;
-    deviceThreadHead = NULL;
+    hThrIDhold_dev = NULL;
+    thread_id_holder *t_holder, *t_holder_old; //just for list of IDs creation, for both scenarios and devices
+    t_holder_old = NULL;
 
     int i;
     for(i=0; i<getNumberOfDevices(); i++)
     {
-        deviceThread = (pthread_t *)malloc(sizeof(pthread_t));
-        pthread_create(&deviceThread,NULL,randomValThread_routine,NULL);
+        t_holder = (thread_id_holder *)malloc(sizeof(thread_id_holder));
+        pthread_create(&t_holder->pthr_idx,NULL,randomValThread_routine,NULL);
 
-        if(deviceThreadHead==NULL)
-            deviceThreadHead=deviceThread;
+        t_holder->nxt=NULL;
+        if(t_holder_old!=NULL)
+            t_holder_old->nxt=t_holder;
+        t_holder_old=t_holder;
+
+        if(hThrIDhold_dev==NULL)
+            hThrIDhold_dev=t_holder_old;
     }
 
     /**start scenario threads**/
-    pthread_t *scenarioThread, *scenarioThreadHead;
-    scenarioThreadHead = NULL;
+    t_holder_old = NULL;
+    hThrIDhold_scn=NULL;
 
     for(i=0; i<getNumberOfScenarios(); i++)
     {
-        scenarioThread = (pthread_t *)malloc(sizeof(pthread_t));
-        pthread_create(&scenarioThread,NULL,scenarioThread_routine,NULL);
+        t_holder = (thread_id_holder *)malloc(sizeof(thread_id_holder));
+        pthread_create(&t_holder->pthr_idx,NULL,scenarioThread_routine,NULL);
 
-        if(scenarioThreadHead==NULL)
-            scenarioThreadHead=scenarioThread;
-    }
+        t_holder->nxt=NULL;
+        if(t_holder_old!=NULL)
+            t_holder_old->nxt=t_holder;
+        t_holder_old=t_holder;
 
-    /**wait for threads to finish**/
-    // TODO bad position of both for loops below, joining of theese threads should be handled in a different way
-    deviceThread = deviceThreadHead;
-    for(i=0; i<getNumberOfDevices(); i++)
-    {
-        pthread_join(deviceThread,NULL);
-        deviceThread++;
+        if(hThrIDhold_scn==NULL)
+            hThrIDhold_scn=t_holder_old;
     }
 
-    scenarioThread = scenarioThreadHead;
-    for(i=0; i<getNumberOfScenarios(); i++)
-    {
-        pthread_join(scenarioThread,NULL);
-        scenarioThread++;
-    }
-
     /*start some sort of control over scenarios? - hmm morda ne...bom jutri :)*/
-
+    printf("Exiting main thread\n");
 	pthread_exit(0);
 }
 
@@ -145,6 +138,7 @@
     device   *inDev;
     int      active = 0;
     float    scenarioBuffer;
+    //printf("My scen ID> %ld\n",pthread_self());
 
     /*load scenario*/
     scen = getAvailableScenario(hScenario);
@@ -219,3 +213,70 @@
    	pthread_exit(0);
 }
 
+void freeDeviceList(device *dev)
+{
+    if(dev->nxt!=NULL)
+        freeDeviceList(dev->nxt);
+    free(dev);
+}
+
+void freeScenarioList(scenario *scn)
+{
+    if(scn->nxt!=NULL)
+        freeScenarioList(scn->nxt);
+    free(scn);
+}
+
+void freeThreadIDList(thread_id_holder *tih)
+{
+    if(tih->nxt!=NULL)
+        freeThreadIDList(tih->nxt);
+    free(tih);
+}
+
+int reinitialiseLogic()
+{
+    thread_id_holder *hold;
+    hold = hThrIDhold_dev;
+    //hThrIDhold_dev = NULL;
+
+    //kill device threads
+    printf("Attempting to cancel threads!\n");
+    while(hold)
+    {
+        printf("Cancelling dev thread %ld with return state %d\n", hold->pthr_idx,pthread_cancel(hold->pthr_idx));
+        //TODO free thread_id_holder list too!
+        hold=hold->nxt;
+    }
+
+    //kill scenario threads
+    hold = hThrIDhold_scn;
+    //hThrIDhold_scn = NULL;
+    while(hold)
+    {
+        printf("Cancelling scn thread %ld with return state %d\n", hold->pthr_idx,pthread_cancel(hold->pthr_idx));
+        //TODO free thread_id_holder list too!
+        hold=hold->nxt;
+    }
+
+    //free memory
+    printf("Freeing memory!\n");
+    freeDeviceList(hDevice);
+    freeScenarioList(hScenario);
+    freeThreadIDList(hThrIDhold_dev);
+    freeThreadIDList(hThrIDhold_scn);
+
+    //reinit devices
+    printf("Reinitializing devices!\n");
+    hDevice   = setDeviceList();
+
+    //reinit scenarios
+    printf("Reinitializing scenarios!\n");
+    hScenario = setNewScenarioList(hDevice);
+
+    //restart main thread
+    printf("Restarting main thread!\n");
+	pthread_create(&decisionMakingThread,NULL,decisionMakingThread_routine,NULL);
+
+	return 1; //sucess, TODO add fail conditions
+}
Index: logic/trunk/src/init/devicelist.h
===================================================================
--- logic/trunk/src/init/devicelist.h	(revision 59)
+++ logic/trunk/src/init/devicelist.h	(revision 60)
@@ -18,7 +18,7 @@
 device *getDevice(device *dev, char *devName);
 device *getAvailableDevice(device *dev);
 int     getLocalFormattedTime();
-
+int     deleteDeviceList(device *dev);
 /*
 void updateDeviceList(device_d *deviceList); // called on HMI change
 char *getDeviceName();
Index: logic/trunk/src/init/devicelist.c
===================================================================
--- logic/trunk/src/init/devicelist.c	(revision 59)
+++ logic/trunk/src/init/devicelist.c	(revision 60)
@@ -146,6 +146,11 @@
     return tm_p->tm_hour*100+tm_p->tm_min;
 }
 
+int deleteDeviceList(device *dev)
+{
+
+    return 1;
+}
 /*
 void updateDeviceList(device_d *deviceList)
 {
