Revision 37 logic/trunk/src/startup/main.c

View differences:

main.c
6 6
 */
7 7

  
8 8
#include "../hci_comm/hci_comm.h"
9
#include "../init/scenariolist.h"
10
#include "../init/devicelist.h"
11
#include "../logics/device_threads.h"
12
#include "../logics/scenario_threads.h"
9 13
#include <stdio.h>
10 14
#include <pthread.h>
11 15

  
12 16
// Inter-thread shared variables
13 17
// TODO: These variables could be configuration variables
14 18
//       (eg: static boolean loggingEnabled)
19
scenario *hScenario; //scenario list
20
scenario *hDevice;   //device list
15 21

  
16 22
// Crtitical section mutual exclusion security
17 23
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
......
23 29
int main(int argc, char *argv[]) {
24 30

  
25 31
    /* initialisation */
26
    setDeviceList();
27
    setNewScenarioList();
32
    hDevice   = setDeviceList();
33
    hScenario = setNewScenarioList();
28 34

  
35
    printf("Devices present:\t%d\n",   getNumberOfDevices());
36
    printf("Scenarios present:\t%d\n", getNumberOfScenarios());
37

  
29 38
    /* regular work */
30 39
    //*
31 40
	pthread_t hciConnectionThread;
32 41
	pthread_t decisionMakingThread;
33 42

  
34
	pthread_create(&hciConnectionThread,NULL,hciConnectionThread_routine,NULL);
43
	pthread_t *decisionMakingThread2;
44

  
45
	pthread_create(&hciConnectionThread, NULL,hciConnectionThread_routine, NULL);
35 46
	pthread_create(&decisionMakingThread,NULL,decisionMakingThread_routine,NULL);
36 47

  
37 48
	pthread_join(hciConnectionThread,NULL);
......
43 54
// Decision-making thread routine
44 55
// Should just call a function that implements decision making module of the logic
45 56
void *decisionMakingThread_routine(void *dummy) {
57
    printf("decisionMakingThread_routine started\n");
46 58

  
47
	// TODO: Implemet decision making
59
    /**dummy threads for input devices (sensors)**/
60
    // TODO check for possible BUG involving declaring pthread_t as pointer!
61
    pthread_t *deviceThread, *deviceThreadHead;
62
    deviceThreadHead = NULL;
48 63

  
64
    int i;
65
    for(i=0; i<getNumberOfDevices(); i++)
66
    {
67
        deviceThread = (pthread_t *)malloc(sizeof(pthread_t));
68
        pthread_create(&deviceThread,NULL,randomValThread_routine,NULL);
69

  
70
        if(deviceThreadHead==NULL)
71
            deviceThreadHead=deviceThread;
72
    }
73

  
74
    /**start scenario threads**/
75
    pthread_t *scenarioThread, *scenarioThreadHead;
76
    scenarioThreadHead = NULL;
77

  
78
    for(i=0; i<getNumberOfScenarios(); i++)
79
    {
80
        scenarioThread = (pthread_t *)malloc(sizeof(pthread_t));
81
        pthread_create(&scenarioThread,NULL,scenarioThread_routine,NULL);
82

  
83
        if(scenarioThreadHead==NULL)
84
            scenarioThreadHead=scenarioThread;
85
    }
86

  
87
    /**wait for threads to finish**/
88
    // TODO bad position of both for loops below, joining of theese threads should be handled in a different way
89
    deviceThread = deviceThreadHead;
90
    for(i=0; i<getNumberOfDevices(); i++)
91
    {
92
        pthread_join(deviceThread,NULL);
93
        deviceThread++;
94
    }
95

  
96
    scenarioThread = scenarioThreadHead;
97
    for(i=0; i<getNumberOfScenarios(); i++)
98
    {
99
        pthread_join(scenarioThread,NULL);
100
        scenarioThread++;
101
    }
102

  
103
    /*start some sort of control over scenarios? - hmm morda ne...bom jutri :)*/
104

  
49 105
	pthread_exit(0);
50

  
51 106
}
52 107

  
53 108
// HCI connection thread routine
54 109
// Start listening to some port
55 110
void *hciConnectionThread_routine(void *dummy) {
56

  
111
    printf("hciConnectionThread_routine started\n");
57 112
	int hciErrorCode = listenTo(1100);
58 113

  
59 114
	pthread_exit(0);
115
}
60 116

  
117
void *scenarioThread_routine(void *dummy)
118
{
119
    //get device
120
    scenario *scen;
121
    scen = getAvailableScenario(hScenario);
122
    if(!scen) pthread_exit(0);
123

  
124
    //set its value
125
    while(1) //TODO signal == true
126
    {
127
        /**main scenario loop**/
128
        printf("Scenario %s updated!\n", scen->scen_id);
129
        //printf("scenario id %s\n", scen->scen_id);
130
        sleep(random(100)%10+1);
131
    }
132
   	pthread_exit(0);
61 133
}
134

  
135
void *randomValThread_routine(void *dummy)
136
{
137
    //get device
138
    device *dev;
139
    dev = getAvailableDevice(hDevice);
140
    if(dev){}
141
    else
142
        pthread_exit(0);
143
    //set its value
144
    while(1) //TODO signal == true
145
    {
146
        dev->buf=random(100)%10+20;// random 20-30
147
        sleep(dev->readitv);
148
    }
149
   	pthread_exit(0);
150
}

Also available in: Unified diff