Statistics
| Revision:

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

History | View | Annotate | Download (6.05 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
#include <syslog.h>
16

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

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

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

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

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

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

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

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

    
48
        pthread_t *decisionMakingThread2;
49

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

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

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

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

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

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

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

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

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

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

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

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

    
110
        pthread_exit(0);
111
}
112

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

    
119
        pthread_exit(0);
120
}
121

    
122
// scenario thread routine
123
void *scenarioThread_routine(void *dummy)
124
{
125
    scenario *scen;
126
    device   *inDev;
127
    int      active = 0;
128
    float    scenarioBuffer;
129

    
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 = scen->inDev;
136
    inDev = getDevice(hDevice, scen->inDevice_id);
137
    scen->buf=inDev->buf;
138
    printf("Scenario %s buffer updated to %.2f!\n", scen->scen_id, inDev->buf);
139
    //set its value
140
    //*
141
    while(1) //TODO signal == true
142
    {
143
        /// main scenario loop
144
        //printf("inDev->buf %.2f\n", 1.0f*inDev->buf);
145
        if(active)
146
            sleep(scen->actvChkItv);
147
        else
148
            sleep(scen->idleChkItv);
149
        //checking primary condition
150
        if(scen->buf) // is not null
151
        {
152
            scen->buf = scen->buf*scen->alpha+inDev->buf*(1-scen->alpha);
153
            //printf("Scenario %s buffer updated to %f, alpha: %.2f!\n", scen->scen_id, scenarioBuffer, scen->alpha);
154
            syslog(LOG_MAIL, "%s:%.2f", scen->inDevice_id, scen->buf);
155
            active = 1;
156

    
157
            if((scen->buf > scen->optval + scen->tolval) ||
158
               (scen->buf < scen->optval - scen->tolval))
159
            {
160
                if(evaluateAdditionalConditions(scen->cond_l, hDevice))
161
                {
162
                    // TODO syslog - "action:scen_ID:power_percentage"
163
                    int power = getPowerPercentage(scen->minval, scen->maxval, scen->optval, &scen->buf, scen->func);
164
                    syslog(LOG_MAIL, "%s:%s:%.2f", scen->action, scen->scen_id, power);
165
                    printf("Zaganjam napravo %s z mocjo %d\n", scen->scen_id, power);
166
                    /*TODO execute action with parameter(s)*/
167
                }
168
                else
169
                    printf("Scenario %s not updated!\n", scen->scen_id);
170
            }
171
            else
172
                active = 0;
173
        }
174
        /*
175
        if(active)
176
            printf("%s main buffer %.2f, sleeping %d\n",scen->scen_id, scen->buf, scen->actvChkItv);
177
        else
178
            printf("%s main buffer %.2f, sleeping %d\n",scen->scen_id, scen->buf, scen->idleChkItv);
179
        /**/
180
    }
181
    /**/
182
           pthread_exit(0);
183
}
184

    
185
void *randomValThread_routine(void *dummy)
186
{
187
    //get device
188
    device *dev;
189
    dev = getAvailableDevice(hDevice);
190
    if(dev){}
191
    else
192
        pthread_exit(0);
193
    //set its value
194
    while(1) //TODO signal == true
195
    {
196
        dev->buf=random(100)%10+20;// random 20-30
197
        //printf("device %s buf = %d\n", dev->id, dev->buf);
198
        sleep(dev->readitv);
199
    }
200
           pthread_exit(0);
201
}
202