Revision 62 logic/trunk/src/startup/main.c
main.c | ||
---|---|---|
4 | 4 |
* 2011, Aleksander Besir (alex.besir@gmail.com) |
5 | 5 |
* |
6 | 6 |
*/ |
7 |
|
|
7 |
#include <stdio.h> |
|
8 |
#include <stdlib.h> |
|
9 |
#include <pthread.h> |
|
10 |
#include <syslog.h> |
|
11 |
#include "../hci_comm/hciChanged.h" |
|
8 | 12 |
#include "../hci_comm/hci_comm.h" |
9 | 13 |
#include "../init/scenariolist.h" |
10 | 14 |
#include "../init/devicelist.h" |
11 | 15 |
#include "../logics/device_threads.h" |
12 | 16 |
//#include "../logics/scenario_threads.h" |
13 |
#include <stdio.h> |
|
14 |
#include <pthread.h> |
|
15 |
#include <syslog.h> |
|
16 |
#include "../hci_comm/hciChanged.h" |
|
17 | 17 |
|
18 | 18 |
// Inter-thread shared variables |
19 | 19 |
// TODO: These variables could be configuration variables |
20 | 20 |
// (eg: static boolean loggingEnabled) |
21 |
static device *hDevice; //device list |
|
22 |
static scenario *hScenario; //scenario list |
|
21 |
static device *hDevice; //device list
|
|
22 |
static scenario *hScenario; //scenario list
|
|
23 | 23 |
static thread_id_holder *hThrIDhold_dev;//holds IDs of running threads (devices) |
24 | 24 |
static thread_id_holder *hThrIDhold_scn;//holds IDs of running threads (scenarios) |
25 | 25 |
//extern scenario *test; //device list |
... | ... | |
52 | 52 |
void *randomValThread_routine(void *dummy); |
53 | 53 |
void *scenarioThread_routine(void *dummy); |
54 | 54 |
int reinitialiseLogic(); // on HCI request |
55 |
int shutDownLogic(); |
|
55 | 56 |
|
56 | 57 |
// Application entry point |
57 | 58 |
int main(int argc, char *argv[]) { |
59 |
/** //DAEMON |
|
60 |
pid_t pid, sid; |
|
58 | 61 |
|
62 |
pid = fork(); |
|
63 |
if (pid < 0) { |
|
64 |
exit(EXIT_FAILURE); |
|
65 |
} |
|
66 |
// EXIT PARENT |
|
67 |
if (pid > 0) { |
|
68 |
exit(EXIT_SUCCESS); |
|
69 |
} |
|
70 |
|
|
71 |
//umask(0); |
|
72 |
|
|
73 |
// Open any logs here |
|
74 |
|
|
75 |
// Create a new SID for the child process |
|
76 |
sid = setsid(); |
|
77 |
if (sid < 0) { |
|
78 |
// Log the failure |
|
79 |
exit(EXIT_FAILURE); |
|
80 |
} |
|
81 |
|
|
82 |
// Close out the standard file descriptors |
|
83 |
// Because daemons generally dont interact directly with user so there is no need of keeping these open |
|
84 |
close(STDIN_FILENO); |
|
85 |
close(STDOUT_FILENO); |
|
86 |
close(STDERR_FILENO); |
|
87 |
/**/ |
|
59 | 88 |
/* initialisation */ |
60 | 89 |
hDevice = setDeviceList(); |
61 | 90 |
hScenario = setNewScenarioList(hDevice); |
... | ... | |
174 | 203 |
{ |
175 | 204 |
// TODO syslog - "action:scen_ID:power_percentage" |
176 | 205 |
int power = getPowerPercentage(scen->minval, scen->maxval, scen->optval, &scen->buf, scen->func); |
177 |
syslog(LOG_MAIL, "%s:%s:%.2f", scen->action, scen->scen_id, power); |
|
178 |
printf("Zaganjam napravo %s z mocjo %d\n", scen->scen_id, power); |
|
179 |
/*TODO execute action with parameter(s)*/ |
|
206 |
|
|
207 |
/// Forking to execute action! |
|
208 |
pid_t pid, sid; |
|
209 |
|
|
210 |
pid = fork(); |
|
211 |
if (pid < 0) { |
|
212 |
exit(EXIT_FAILURE); |
|
213 |
} |
|
214 |
// ignore parent |
|
215 |
if (pid > 0) {} |
|
216 |
else |
|
217 |
{ |
|
218 |
/*TODO execute action with parameter(s)*/ |
|
219 |
syslog(LOG_MAIL, "%s:%s:%.2f", scen->action, scen->scen_id, power); |
|
220 |
|
|
221 |
execl(scen->action, power); |
|
222 |
printf("Zaganjam napravo %s z mocjo %d\n", scen->scen_id, power); |
|
223 |
|
|
224 |
shutDownLogic(); |
|
225 |
} |
|
226 |
|
|
180 | 227 |
} |
181 | 228 |
else |
182 | 229 |
printf("Scenario %s not updated!\n", scen->scen_id); |
... | ... | |
206 | 253 |
//set its value |
207 | 254 |
while(1) //TODO signal == true |
208 | 255 |
{ |
209 |
dev->buf=random(100)%10+20;// random 20-30
|
|
256 |
dev->buf=random()%10+20;// random 20-30 |
|
210 | 257 |
//printf("device %s buf = %d\n", dev->id, dev->buf); |
211 | 258 |
sleep(dev->readitv); |
212 | 259 |
} |
... | ... | |
280 | 327 |
|
281 | 328 |
return 1; //sucess, TODO add fail conditions |
282 | 329 |
} |
330 |
|
|
331 |
int shutDownLogic() |
|
332 |
{ |
|
333 |
thread_id_holder *hold; |
|
334 |
hold = hThrIDhold_dev; |
|
335 |
//hThrIDhold_dev = NULL; |
|
336 |
|
|
337 |
//kill device threads |
|
338 |
printf("Attempting to cancel threads!\n"); |
|
339 |
while(hold) |
|
340 |
{ |
|
341 |
printf("Cancelling dev thread %ld with return state %d\n", hold->pthr_idx,pthread_cancel(hold->pthr_idx)); |
|
342 |
//TODO free thread_id_holder list too! |
|
343 |
hold=hold->nxt; |
|
344 |
} |
|
345 |
|
|
346 |
//kill scenario threads |
|
347 |
hold = hThrIDhold_scn; |
|
348 |
//hThrIDhold_scn = NULL; |
|
349 |
while(hold) |
|
350 |
{ |
|
351 |
printf("Cancelling scn thread %ld with return state %d\n", hold->pthr_idx,pthread_cancel(hold->pthr_idx)); |
|
352 |
//TODO free thread_id_holder list too! |
|
353 |
hold=hold->nxt; |
|
354 |
} |
|
355 |
|
|
356 |
//free memory |
|
357 |
printf("Freeing memory!\n"); |
|
358 |
freeDeviceList(hDevice); |
|
359 |
freeScenarioList(hScenario); |
|
360 |
freeThreadIDList(hThrIDhold_dev); |
|
361 |
freeThreadIDList(hThrIDhold_scn); |
|
362 |
|
|
363 |
//stop main thread and HCI comm thread |
|
364 |
pthread_cancel(decisionMakingThread); |
|
365 |
pthread_cancel(hciConnectionThread); |
|
366 |
|
|
367 |
return 1; //sucess, TODO add fail conditions |
|
368 |
} |
Also available in: Unified diff