Statistics
| Revision:

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

History | View | Annotate | Download (5.78 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
static device   *hDevice;   //device list
20
static scenario *hScenario; //scenario list
21

    
22
//extern scenario *test;   //device list
23

    
24
// Crtitical section mutual exclusion security
25
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
26

    
27
void *decisionMakingThread_routine(void *dummy);
28
void *hciConnectionThread_routine(void *dummy);
29
void *randomValThread_routine(void *dummy);
30
void *scenarioThread_routine(void *dummy);
31

    
32
// Application entry point
33
int main(int argc, char *argv[]) {
34

    
35
    /* initialisation */
36
    hDevice   = setDeviceList();
37
    hScenario = setNewScenarioList();
38

    
39
    printf("Devices present:\t%d\n",   getNumberOfDevices());
40
    printf("Scenarios present:\t%d\n", getNumberOfScenarios());
41

    
42
    /* regular work */
43
    //*
44
        pthread_t hciConnectionThread;
45
        pthread_t decisionMakingThread;
46

    
47
        pthread_t *decisionMakingThread2;
48

    
49
        pthread_create(&hciConnectionThread, NULL,hciConnectionThread_routine, NULL);
50
        pthread_create(&decisionMakingThread,NULL,decisionMakingThread_routine,NULL);
51

    
52
        pthread_join(hciConnectionThread,NULL);
53
        pthread_join(decisionMakingThread,NULL);
54
    /**/
55
        exit(0);
56
}
57

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

    
63
    /**dummy threads for input devices (sensors)**/
64
    // TODO check for possible BUG involving declaring pthread_t as pointer!
65
    pthread_t *deviceThread, *deviceThreadHead;
66
    deviceThreadHead = NULL;
67

    
68
    int i;
69
    for(i=0; i<getNumberOfDevices(); i++)
70
    {
71
        deviceThread = (pthread_t *)malloc(sizeof(pthread_t));
72
        pthread_create(&deviceThread,NULL,randomValThread_routine,NULL);
73

    
74
        if(deviceThreadHead==NULL)
75
            deviceThreadHead=deviceThread;
76
    }
77

    
78
    /**start scenario threads**/
79
    pthread_t *scenarioThread, *scenarioThreadHead;
80
    scenarioThreadHead = NULL;
81

    
82
    for(i=0; i<getNumberOfScenarios(); i++)
83
    {
84
        scenarioThread = (pthread_t *)malloc(sizeof(pthread_t));
85
        pthread_create(&scenarioThread,NULL,scenarioThread_routine,NULL);
86

    
87
        if(scenarioThreadHead==NULL)
88
            scenarioThreadHead=scenarioThread;
89
    }
90

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

    
100
    scenarioThread = scenarioThreadHead;
101
    for(i=0; i<getNumberOfScenarios(); i++)
102
    {
103
        pthread_join(scenarioThread,NULL);
104
        scenarioThread++;
105
    }
106

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

    
109
        pthread_exit(0);
110
}
111

    
112
// HCI connection thread routine
113
// Start listening to some port
114
void *hciConnectionThread_routine(void *dummy) {
115
    printf("hciConnectionThread_routine started\n");
116
        int hciErrorCode = listenTo(1100);
117

    
118
        pthread_exit(0);
119
}
120

    
121
// scenario thread routine
122
void *scenarioThread_routine(void *dummy)
123
{
124
    scenario *scen;
125
    device   *inDev;
126
    int      active = 0;
127
    float    scen_main_buff;
128
    //float    alpha = 0.7f; //TODO put this in xml also
129
    //printf("alpha %.2f\n", alpha);
130
    /*load scenario*/
131
    scen = getAvailableScenario(hScenario);
132
    if(!scen) pthread_exit(0);
133

    
134
    // TODO add pointer to this device (all of them actually) to scenario structure - problems with segfault
135
    inDev = getDevice(hDevice, scen->inDevice_id);
136
    scen_main_buff=inDev->buf;
137
    printf("Scenario %s buffer updated to %.2f!\n", scen->scen_id, scen_main_buff);
138
    //set its value
139
    //*
140
    while(1) //TODO signal == true
141
    {
142
        /// main scenario loop
143
        //printf("inDev->buf %.2f\n", 1.0f*inDev->buf);
144
        if(active)
145
            sleep(scen->actvChkItv);
146
        else
147
            sleep(scen->idleChkItv);
148
        //checking primary condition
149
        if(scen_main_buff) // is not null
150
        {
151
            scen_main_buff = scen_main_buff*scen->alpha+inDev->buf*(1-scen->alpha);
152
            printf("Scenario %s buffer updated to %f, alpha: %.2f!\n", scen->scen_id, scen_main_buff, scen->alpha);
153
            active = 1;
154

    
155
            if((scen_main_buff > scen->optval + scen->tolval) ||
156
               (scen_main_buff < scen->optval - scen->tolval))
157
            {
158
                if(evaluateAdditionalConditions(scen->cond_l, hDevice))
159
                    printf("Scenario %s updated!\n", scen->scen_id);
160
                    /*TODO calcualte parameters with given function*/
161
                    /*TODO execute action with parameter(s)*/
162
                else
163
                    printf("Scenario %s not updated!\n", scen->scen_id);
164
            }
165
            else
166
                active = 0;
167
        }
168
        if(active)
169
            printf("%s main buffer %.2f, sleeping %d\n",scen->scen_id, scen_main_buff, scen->actvChkItv);
170
        else
171
            printf("%s main buffer %.2f, sleeping %d\n",scen->scen_id, scen_main_buff, scen->idleChkItv);
172
    }
173
    /**/
174
           pthread_exit(0);
175
}
176

    
177
void *randomValThread_routine(void *dummy)
178
{
179
    //get device
180
    device *dev;
181
    dev = getAvailableDevice(hDevice);
182
    if(dev){}
183
    else
184
        pthread_exit(0);
185
    //set its value
186
    while(1) //TODO signal == true
187
    {
188
        dev->buf=random(100)%10+20;// random 20-30
189
        //printf("device %s buf = %d\n", dev->id, dev->buf);
190
        sleep(dev->readitv);
191
    }
192
           pthread_exit(0);
193
}
194