root / logic / trunk / src / startup / main.c @ 37
History | View | Annotate | Download (3.96 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 | #include "../logics/device_threads.h" |
||
12 | #include "../logics/scenario_threads.h" |
||
13 | 10 | Janez1 | #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 | 37 | Janez1 | scenario *hScenario; //scenario list
|
20 | scenario *hDevice; //device list
|
||
21 | 10 | Janez1 | |
22 | // Crtitical section mutual exclusion security
|
||
23 | static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
|
||
24 | |||
25 | void *decisionMakingThread_routine(void *dummy); |
||
26 | void *hciConnectionThread_routine(void *dummy); |
||
27 | |||
28 | // Application entry point
|
||
29 | int main(int argc, char *argv[]) { |
||
30 | |||
31 | 31 | Janez1 | /* initialisation */
|
32 | 37 | Janez1 | hDevice = setDeviceList(); |
33 | hScenario = setNewScenarioList(); |
||
34 | 31 | Janez1 | |
35 | 37 | Janez1 | printf("Devices present:\t%d\n", getNumberOfDevices());
|
36 | printf("Scenarios present:\t%d\n", getNumberOfScenarios());
|
||
37 | |||
38 | 31 | Janez1 | /* regular work */
|
39 | 21 | Janez1 | //*
|
40 | 10 | Janez1 | pthread_t hciConnectionThread; |
41 | pthread_t decisionMakingThread; |
||
42 | |||
43 | 37 | Janez1 | pthread_t *decisionMakingThread2; |
44 | |||
45 | pthread_create(&hciConnectionThread, NULL,hciConnectionThread_routine, NULL); |
||
46 | 10 | Janez1 | pthread_create(&decisionMakingThread,NULL,decisionMakingThread_routine,NULL); |
47 | |||
48 | pthread_join(hciConnectionThread,NULL);
|
||
49 | pthread_join(decisionMakingThread,NULL);
|
||
50 | 21 | Janez1 | /**/
|
51 | 10 | Janez1 | exit(0);
|
52 | } |
||
53 | |||
54 | // Decision-making thread routine
|
||
55 | // Should just call a function that implements decision making module of the logic
|
||
56 | void *decisionMakingThread_routine(void *dummy) { |
||
57 | 37 | Janez1 | printf("decisionMakingThread_routine started\n");
|
58 | 10 | Janez1 | |
59 | 37 | Janez1 | /**dummy threads for input devices (sensors)**/
|
60 | // TODO check for possible BUG involving declaring pthread_t as pointer!
|
||
61 | pthread_t *deviceThread, *deviceThreadHead; |
||
62 | deviceThreadHead = NULL;
|
||
63 | 10 | Janez1 | |
64 | 37 | Janez1 | int i;
|
65 | for(i=0; i<getNumberOfDevices(); i++) |
||
66 | { |
||
67 | deviceThread = (pthread_t *)malloc(sizeof(pthread_t));
|
||
68 | pthread_create(&deviceThread,NULL,randomValThread_routine,NULL); |
||
69 | |||
70 | if(deviceThreadHead==NULL) |
||
71 | deviceThreadHead=deviceThread; |
||
72 | } |
||
73 | |||
74 | /**start scenario threads**/
|
||
75 | pthread_t *scenarioThread, *scenarioThreadHead; |
||
76 | scenarioThreadHead = NULL;
|
||
77 | |||
78 | for(i=0; i<getNumberOfScenarios(); i++) |
||
79 | { |
||
80 | scenarioThread = (pthread_t *)malloc(sizeof(pthread_t));
|
||
81 | pthread_create(&scenarioThread,NULL,scenarioThread_routine,NULL); |
||
82 | |||
83 | if(scenarioThreadHead==NULL) |
||
84 | scenarioThreadHead=scenarioThread; |
||
85 | } |
||
86 | |||
87 | /**wait for threads to finish**/
|
||
88 | // TODO bad position of both for loops below, joining of theese threads should be handled in a different way
|
||
89 | deviceThread = deviceThreadHead; |
||
90 | for(i=0; i<getNumberOfDevices(); i++) |
||
91 | { |
||
92 | pthread_join(deviceThread,NULL);
|
||
93 | deviceThread++; |
||
94 | } |
||
95 | |||
96 | scenarioThread = scenarioThreadHead; |
||
97 | for(i=0; i<getNumberOfScenarios(); i++) |
||
98 | { |
||
99 | pthread_join(scenarioThread,NULL);
|
||
100 | scenarioThread++; |
||
101 | } |
||
102 | |||
103 | /*start some sort of control over scenarios? - hmm morda ne...bom jutri :)*/
|
||
104 | |||
105 | 10 | Janez1 | pthread_exit(0);
|
106 | } |
||
107 | |||
108 | // HCI connection thread routine
|
||
109 | // Start listening to some port
|
||
110 | void *hciConnectionThread_routine(void *dummy) { |
||
111 | 37 | Janez1 | printf("hciConnectionThread_routine started\n");
|
112 | 10 | Janez1 | int hciErrorCode = listenTo(1100); |
113 | |||
114 | pthread_exit(0);
|
||
115 | 37 | Janez1 | } |
116 | 10 | Janez1 | |
117 | 37 | Janez1 | void *scenarioThread_routine(void *dummy) |
118 | { |
||
119 | //get device
|
||
120 | scenario *scen; |
||
121 | scen = getAvailableScenario(hScenario); |
||
122 | if(!scen) pthread_exit(0); |
||
123 | |||
124 | //set its value
|
||
125 | while(1) //TODO signal == true |
||
126 | { |
||
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); |
||
131 | } |
||
132 | pthread_exit(0);
|
||
133 | 10 | Janez1 | } |
134 | 37 | Janez1 | |
135 | void *randomValThread_routine(void *dummy) |
||
136 | { |
||
137 | //get device
|
||
138 | device *dev; |
||
139 | dev = getAvailableDevice(hDevice); |
||
140 | if(dev){}
|
||
141 | else
|
||
142 | pthread_exit(0);
|
||
143 | //set its value
|
||
144 | while(1) //TODO signal == true |
||
145 | { |
||
146 | dev->buf=random(100)%10+20;// random 20-30 |
||
147 | sleep(dev->readitv); |
||
148 | } |
||
149 | pthread_exit(0);
|
||
150 | } |