root / logic / trunk / src / startup / main.c @ 44
History | View | Annotate | Download (6.05 KB)
1 | 10 | Janez1 | /*
|
---|---|---|---|
2 | * main.c
|
||
3 | *
|
||
4 | * 2011, Aleksander Besir (alex.besir@gmail.com)
|
||
5 | *
|
||
6 | */
|
||
7 | |||
8 | #include "../hci_comm/hci_comm.h" |
||
9 | 37 | Janez1 | #include "../init/scenariolist.h" |
10 | #include "../init/devicelist.h" |
||
11 | 40 | Janez1 | //#include "../logics/device_threads.h"
|
12 | //#include "../logics/scenario_threads.h"
|
||
13 | 10 | Janez1 | #include <stdio.h> |
14 | #include <pthread.h> |
||
15 | 44 | Janez1 | #include <syslog.h> |
16 | 10 | Janez1 | |
17 | // Inter-thread shared variables
|
||
18 | // TODO: These variables could be configuration variables
|
||
19 | // (eg: static boolean loggingEnabled)
|
||
20 | 40 | Janez1 | static device *hDevice; //device list |
21 | static scenario *hScenario; //scenario list |
||
22 | 10 | Janez1 | |
23 | 40 | Janez1 | //extern scenario *test; //device list
|
24 | |||
25 | 10 | Janez1 | // 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 | 40 | Janez1 | void *randomValThread_routine(void *dummy); |
31 | void *scenarioThread_routine(void *dummy); |
||
32 | 10 | Janez1 | |
33 | // Application entry point
|
||
34 | int main(int argc, char *argv[]) { |
||
35 | |||
36 | 31 | Janez1 | /* initialisation */
|
37 | 37 | Janez1 | hDevice = setDeviceList(); |
38 | 44 | Janez1 | hScenario = setNewScenarioList(hDevice); |
39 | 31 | Janez1 | |
40 | 37 | Janez1 | printf("Devices present:\t%d\n", getNumberOfDevices());
|
41 | printf("Scenarios present:\t%d\n", getNumberOfScenarios());
|
||
42 | |||
43 | 31 | Janez1 | /* regular work */
|
44 | 21 | Janez1 | //*
|
45 | 10 | Janez1 | pthread_t hciConnectionThread; |
46 | pthread_t decisionMakingThread; |
||
47 | |||
48 | 37 | Janez1 | pthread_t *decisionMakingThread2; |
49 | |||
50 | pthread_create(&hciConnectionThread, NULL,hciConnectionThread_routine, NULL); |
||
51 | 10 | Janez1 | pthread_create(&decisionMakingThread,NULL,decisionMakingThread_routine,NULL); |
52 | |||
53 | pthread_join(hciConnectionThread,NULL);
|
||
54 | pthread_join(decisionMakingThread,NULL);
|
||
55 | 21 | Janez1 | /**/
|
56 | 10 | Janez1 | 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 | 40 | Janez1 | printf("-- decisionMakingThread_routine started --\n");
|
63 | 10 | Janez1 | |
64 | 37 | Janez1 | /**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 | 10 | Janez1 | |
69 | 37 | Janez1 | 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 | 10 | Janez1 | pthread_exit(0);
|
111 | } |
||
112 | |||
113 | // HCI connection thread routine
|
||
114 | // Start listening to some port
|
||
115 | void *hciConnectionThread_routine(void *dummy) { |
||
116 | 37 | Janez1 | printf("hciConnectionThread_routine started\n");
|
117 | 10 | Janez1 | int hciErrorCode = listenTo(1100); |
118 | |||
119 | pthread_exit(0);
|
||
120 | 37 | Janez1 | } |
121 | 10 | Janez1 | |
122 | 40 | Janez1 | // scenario thread routine
|
123 | 37 | Janez1 | void *scenarioThread_routine(void *dummy) |
124 | { |
||
125 | scenario *scen; |
||
126 | 40 | Janez1 | device *inDev; |
127 | int active = 0; |
||
128 | 44 | Janez1 | float scenarioBuffer;
|
129 | |||
130 | 40 | Janez1 | /*load scenario*/
|
131 | 37 | Janez1 | scen = getAvailableScenario(hScenario); |
132 | if(!scen) pthread_exit(0); |
||
133 | |||
134 | 40 | Janez1 | // TODO add pointer to this device (all of them actually) to scenario structure - problems with segfault
|
135 | 44 | Janez1 | //indev = scen->inDev;
|
136 | 40 | Janez1 | inDev = getDevice(hDevice, scen->inDevice_id); |
137 | 44 | Janez1 | scen->buf=inDev->buf; |
138 | printf("Scenario %s buffer updated to %.2f!\n", scen->scen_id, inDev->buf);
|
||
139 | 37 | Janez1 | //set its value
|
140 | 40 | Janez1 | //*
|
141 | 37 | Janez1 | while(1) //TODO signal == true |
142 | { |
||
143 | 40 | Janez1 | /// 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 | 44 | Janez1 | if(scen->buf) // is not null |
151 | 40 | Janez1 | { |
152 | 44 | Janez1 | 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 | 40 | Janez1 | active = 1;
|
156 | |||
157 | 44 | Janez1 | if((scen->buf > scen->optval + scen->tolval) ||
|
158 | (scen->buf < scen->optval - scen->tolval)) |
||
159 | 40 | Janez1 | { |
160 | if(evaluateAdditionalConditions(scen->cond_l, hDevice))
|
||
161 | 41 | Janez1 | { |
162 | 44 | Janez1 | // 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 | 40 | Janez1 | /*TODO execute action with parameter(s)*/
|
167 | 41 | Janez1 | } |
168 | 40 | Janez1 | else
|
169 | printf("Scenario %s not updated!\n", scen->scen_id);
|
||
170 | } |
||
171 | else
|
||
172 | active = 0;
|
||
173 | } |
||
174 | 44 | Janez1 | /*
|
175 | 40 | Janez1 | if(active)
|
176 | 44 | Janez1 | printf("%s main buffer %.2f, sleeping %d\n",scen->scen_id, scen->buf, scen->actvChkItv);
|
177 | 40 | Janez1 | else
|
178 | 44 | Janez1 | printf("%s main buffer %.2f, sleeping %d\n",scen->scen_id, scen->buf, scen->idleChkItv);
|
179 | /**/
|
||
180 | 37 | Janez1 | } |
181 | 40 | Janez1 | /**/
|
182 | 37 | Janez1 | pthread_exit(0);
|
183 | 10 | Janez1 | } |
184 | 37 | Janez1 | |
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 | 40 | Janez1 | //printf("device %s buf = %d\n", dev->id, dev->buf);
|
198 | 37 | Janez1 | sleep(dev->readitv); |
199 | } |
||
200 | pthread_exit(0);
|
||
201 | } |