Statistics
| Revision:

root / logic / trunk / src / startup / main.c @ 37

History | View | Annotate | Download (3.96 KB)

1
/*
2
 * main.c
3
 *
4
 * 2011, Aleksander Besir (alex.besir@gmail.com)
5
 *
6
 */
7

    
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"
13
#include <stdio.h>
14
#include <pthread.h>
15

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

    
22
// Crtitical section mutual exclusion security
23
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
24

    
25
void *decisionMakingThread_routine(void *dummy);
26
void *hciConnectionThread_routine(void *dummy);
27

    
28
// Application entry point
29
int main(int argc, char *argv[]) {
30

    
31
    /* initialisation */
32
    hDevice   = setDeviceList();
33
    hScenario = setNewScenarioList();
34

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

    
38
    /* regular work */
39
    //*
40
        pthread_t hciConnectionThread;
41
        pthread_t decisionMakingThread;
42

    
43
        pthread_t *decisionMakingThread2;
44

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

    
48
        pthread_join(hciConnectionThread,NULL);
49
        pthread_join(decisionMakingThread,NULL);
50
    /**/
51
        exit(0);
52
}
53

    
54
// Decision-making thread routine
55
// Should just call a function that implements decision making module of the logic
56
void *decisionMakingThread_routine(void *dummy) {
57
    printf("decisionMakingThread_routine started\n");
58

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

    
105
        pthread_exit(0);
106
}
107

    
108
// HCI connection thread routine
109
// Start listening to some port
110
void *hciConnectionThread_routine(void *dummy) {
111
    printf("hciConnectionThread_routine started\n");
112
        int hciErrorCode = listenTo(1100);
113

    
114
        pthread_exit(0);
115
}
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);
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
}