Revision 40 logic/trunk/src/startup/main.c
main.c | ||
---|---|---|
8 | 8 |
#include "../hci_comm/hci_comm.h" |
9 | 9 |
#include "../init/scenariolist.h" |
10 | 10 |
#include "../init/devicelist.h" |
11 |
#include "../logics/device_threads.h" |
|
12 |
#include "../logics/scenario_threads.h" |
|
11 |
//#include "../logics/device_threads.h"
|
|
12 |
//#include "../logics/scenario_threads.h"
|
|
13 | 13 |
#include <stdio.h> |
14 | 14 |
#include <pthread.h> |
15 | 15 |
|
16 | 16 |
// Inter-thread shared variables |
17 | 17 |
// TODO: These variables could be configuration variables |
18 | 18 |
// (eg: static boolean loggingEnabled) |
19 |
scenario *hScenario; //scenario list
|
|
20 |
scenario *hDevice; //device list
|
|
19 |
static device *hDevice; //device list
|
|
20 |
static scenario *hScenario; //scenario list
|
|
21 | 21 |
|
22 |
//extern scenario *test; //device list |
|
23 |
|
|
22 | 24 |
// Crtitical section mutual exclusion security |
23 | 25 |
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; |
24 | 26 |
|
25 | 27 |
void *decisionMakingThread_routine(void *dummy); |
26 | 28 |
void *hciConnectionThread_routine(void *dummy); |
29 |
void *randomValThread_routine(void *dummy); |
|
30 |
void *scenarioThread_routine(void *dummy); |
|
27 | 31 |
|
28 | 32 |
// Application entry point |
29 | 33 |
int main(int argc, char *argv[]) { |
... | ... | |
54 | 58 |
// Decision-making thread routine |
55 | 59 |
// Should just call a function that implements decision making module of the logic |
56 | 60 |
void *decisionMakingThread_routine(void *dummy) { |
57 |
printf("decisionMakingThread_routine started\n");
|
|
61 |
printf("-- decisionMakingThread_routine started --\n");
|
|
58 | 62 |
|
59 | 63 |
/**dummy threads for input devices (sensors)**/ |
60 | 64 |
// TODO check for possible BUG involving declaring pthread_t as pointer! |
... | ... | |
114 | 118 |
pthread_exit(0); |
115 | 119 |
} |
116 | 120 |
|
121 |
// scenario thread routine |
|
117 | 122 |
void *scenarioThread_routine(void *dummy) |
118 | 123 |
{ |
119 |
//get device |
|
120 | 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*/ |
|
121 | 131 |
scen = getAvailableScenario(hScenario); |
122 | 132 |
if(!scen) pthread_exit(0); |
123 | 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); |
|
124 | 138 |
//set its value |
139 |
//* |
|
125 | 140 |
while(1) //TODO signal == true |
126 | 141 |
{ |
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); |
|
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); |
|
131 | 172 |
} |
173 |
/**/ |
|
132 | 174 |
pthread_exit(0); |
133 | 175 |
} |
134 | 176 |
|
... | ... | |
144 | 186 |
while(1) //TODO signal == true |
145 | 187 |
{ |
146 | 188 |
dev->buf=random(100)%10+20;// random 20-30 |
189 |
//printf("device %s buf = %d\n", dev->id, dev->buf); |
|
147 | 190 |
sleep(dev->readitv); |
148 | 191 |
} |
149 | 192 |
pthread_exit(0); |
150 | 193 |
} |
194 |
|
Also available in: Unified diff