Revision 57
logic/trunk/src/hci_comm/hciChanged.h | ||
---|---|---|
1 |
|
|
2 |
/* |
|
3 |
* hciChanged.h |
|
4 |
* |
|
5 |
* 2011, Aleksander Besir (alex.besir@gmail.com) |
|
6 |
* |
|
7 |
*/ |
|
8 |
|
|
9 |
#ifndef _ENERAPTOR_HCI_HCICHANGED |
|
10 |
#define _ENERAPTOR_HCI_HCICHANGED |
|
11 |
|
|
12 |
// hciChanged value codes |
|
13 |
#define HCI_CHANGED_RESET 0 // No changes made. |
|
14 |
// Logic has to reset hciChanged to this value |
|
15 |
// whenever it reads it |
|
16 |
|
|
17 |
#define HCI_CHANGED_SCENARIOS -1 // HCI has changed scenariotree.xml contents |
|
18 |
|
|
19 |
#define HCI_CHANGED_DEVICES -2 // HCI has changed devicetree.xml contents |
|
20 |
|
|
21 |
#endif |
logic/trunk/src/hci_comm/hci_comm.h | ||
---|---|---|
22 | 22 |
#define HCI_COMM_TERMINATE -1 // Terminates the infinite listening loop |
23 | 23 |
|
24 | 24 |
// Constants |
25 |
#define HCI_COMM_REQUEST_BUFFER_SIZE 1024 // Bufer size in bytes |
|
26 |
#define HCI_COMM_RESPONSE_BUFFER_SIZE 1024 // Bufer size in bytes |
|
25 |
#define HCI_COMM_REQUEST_BUFFER_SIZE 1048576 // Bufer size in bytes |
|
26 |
#define HCI_COMM_RESPONSE_BUFFER_SIZE 1048576 // Bufer size in bytes |
|
27 |
#define HCI_COMM_LINE_BUFFER_SIZE 1048576 // Bufer size in bytes |
|
27 | 28 |
// TODO: buffer size should be bigger! |
28 | 29 |
|
29 | 30 |
int listenTo(unsigned short int portNum); |
logic/trunk/src/hci_comm/hci_comm_listener.c | ||
---|---|---|
15 | 15 |
#include <string.h> |
16 | 16 |
#include <unistd.h> |
17 | 17 |
#include "../mxml/mxml.h" |
18 |
#include "hciChanged.h" |
|
18 | 19 |
|
20 |
// Global variables from startup/main.c |
|
21 |
extern pthread_mutex_t mtx; |
|
22 |
extern int hciChanged; |
|
23 |
|
|
19 | 24 |
int listenTo(unsigned short int portNum) { |
20 | 25 |
|
21 | 26 |
// Holders |
... | ... | |
34 | 39 |
printf("HCI comm error: Cannot allocate buffer.\n"); |
35 | 40 |
return HCI_COMM_ERR_BUF_ALOC_FAIL; |
36 | 41 |
} |
42 |
char *lineBuffer = malloc(HCI_COMM_LINE_BUFFER_SIZE * sizeof(char)); |
|
43 |
if(lineBuffer == NULL) { |
|
44 |
printf("HCI comm error: Cannot allocate buffer.\n"); |
|
45 |
return HCI_COMM_ERR_BUF_ALOC_FAIL; |
|
46 |
} |
|
37 | 47 |
|
38 | 48 |
// Socket holder |
39 | 49 |
int socketHolder = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
... | ... | |
68 | 78 |
// Infinite listening |
69 | 79 |
while(1) { |
70 | 80 |
|
81 |
memset(responseBuffer,'\0',HCI_COMM_RESPONSE_BUFFER_SIZE); |
|
82 |
memset(requestBuffer,'\0',HCI_COMM_REQUEST_BUFFER_SIZE); |
|
83 |
memset(lineBuffer,'\0',HCI_COMM_LINE_BUFFER_SIZE); |
|
84 |
responseLength = 0; |
|
85 |
|
|
71 | 86 |
printf("[hci_comm_listener] Waiting for new connection...\n"); |
72 | 87 |
|
73 | 88 |
// Accept request |
74 | 89 |
int connectionHolder = accept(socketHolder,NULL,NULL); |
75 | 90 |
if(connectionHolder < 0) { |
76 | 91 |
printf("HCI comm error: Error accepting request.\n"); |
77 |
strcpy(responseBuffer,"<logic-response type=\"error\">Could not accept request!</logic-response>"); |
|
78 |
responseLength = strlen(responseBuffer); |
|
79 |
write(connectionHolder,responseBuffer,responseLength); |
|
80 | 92 |
shutdown(connectionHolder,SHUT_RDWR); |
81 | 93 |
close(connectionHolder); |
82 | 94 |
continue; |
... | ... | |
142 | 154 |
responseLength = strlen(responseBuffer); |
143 | 155 |
printf("[hci_comm_listener] Response length formed: %d\n",responseLength); |
144 | 156 |
} else if(strcmp(attrVal,"getConfig") == 0) { |
157 |
printf("[hci_comm_listener] getConfig request\n"); |
|
145 | 158 |
// TODO |
146 | 159 |
} else if(strcmp(attrVal,"setConfig") == 0) { |
160 |
printf("[hci_comm_listener] setConfig request\n"); |
|
147 | 161 |
// TODO |
148 | 162 |
} else if(strcmp(attrVal,"getScenarios") == 0) { |
149 |
// TODO |
|
163 |
printf("[hci_comm_listener] getScenarios request\n"); |
|
164 |
FILE *fp; |
|
165 |
if(fp = fopen("./scenariotree.xml", "r")) { |
|
166 |
printf("[hci_comm_listener] File opened\n"); |
|
167 |
strcat(responseBuffer,"<logic-response type=\"getScenarios\">"); |
|
168 |
while(fgets(lineBuffer,HCI_COMM_LINE_BUFFER_SIZE,fp)) { |
|
169 |
strcat(responseBuffer,lineBuffer); |
|
170 |
} |
|
171 |
strcat(responseBuffer,"</logic-response>"); |
|
172 |
responseLength = strlen(responseBuffer); |
|
173 |
fclose(fp); |
|
174 |
} else { |
|
175 |
printf("[hci_comm_listener] Error: Can't open file scenariotree.xml\n"); |
|
176 |
strcpy(responseBuffer,"<logic-response type=\"error\">Could not read file!</logic-response>"); |
|
177 |
responseLength = strlen(responseBuffer); |
|
178 |
} |
|
150 | 179 |
} else if(strcmp(attrVal,"setScenarios") == 0) { |
151 |
// TODO |
|
180 |
printf("[hci_comm_listener] setScenarios request\n"); |
|
181 |
// Prepare new content for scenariotree.xml |
|
182 |
strncpy(lineBuffer,requestBuffer+54,strlen(requestBuffer)-(54+14+2)); |
|
183 |
strcpy(requestBuffer,"<?xml version=\"1.0\"?>"); |
|
184 |
strcat(requestBuffer,lineBuffer); |
|
185 |
FILE *fp; |
|
186 |
if(fp = fopen("./scenariotree.xml", "w")) { |
|
187 |
fputs(requestBuffer,fp); |
|
188 |
fclose(fp); |
|
189 |
pthread_mutex_lock(&mtx); |
|
190 |
hciChanged = HCI_CHANGED_SCENARIOS; |
|
191 |
pthread_mutex_unlock(&mtx); |
|
192 |
strcpy(responseBuffer,"<logic-response type=\"setScenarios\"></logic-response>"); |
|
193 |
responseLength = strlen(responseBuffer); |
|
194 |
} else { |
|
195 |
printf("[hci_comm_listener] Error: Can't open file scenariotree.xml for writing\n"); |
|
196 |
strcpy(responseBuffer,"<logic-response type=\"error\">Could not write file!</logic-response>"); |
|
197 |
responseLength = strlen(responseBuffer); |
|
198 |
} |
|
152 | 199 |
} else if(strcmp(attrVal,"executeAction") == 0) { |
200 |
printf("[hci_comm_listener] executeAction request\n"); |
|
153 | 201 |
// TODO |
154 | 202 |
} else { |
155 | 203 |
strcpy(responseBuffer,"<logic-response type=\"error\">Invalid request!</logic-response>"); |
logic/trunk/src/startup/main.c | ||
---|---|---|
13 | 13 |
#include <stdio.h> |
14 | 14 |
#include <pthread.h> |
15 | 15 |
#include <syslog.h> |
16 |
#include "../hci_comm/hciChanged.h" |
|
16 | 17 |
|
17 | 18 |
// Inter-thread shared variables |
18 | 19 |
// TODO: These variables could be configuration variables |
... | ... | |
23 | 24 |
//extern scenario *test; //device list |
24 | 25 |
|
25 | 26 |
// Crtitical section mutual exclusion security |
26 |
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
|
|
27 |
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; |
|
27 | 28 |
|
29 |
// Global variable to check, if HCI made any changes that need |
|
30 |
// to be updated by the logic. |
|
31 |
// Changing this variable's value must be done using the |
|
32 |
// mutex mtx declared before. |
|
33 |
// To change variables value you should do this: |
|
34 |
// pthread_mutex_lock(&mtx); |
|
35 |
// hciChanged = 0; |
|
36 |
// pthread_mutex_unlock(&mtx); |
|
37 |
// The meaning of hciChanged possible values is in hci_comm/hciChanged.h. |
|
38 |
// HCI should always reset hciChanged value to HCI_CHANGED_RESET after |
|
39 |
// checking it's value. |
|
40 |
// To use mtx and hciChanged variables in other .c files, write |
|
41 |
// extern int hciChanged; |
|
42 |
// extern pthread_mutex_t mtx; |
|
43 |
// on top of the class. |
|
44 |
|
|
45 |
int hciChanged = HCI_CHANGED_RESET; |
|
46 |
|
|
28 | 47 |
void *decisionMakingThread_routine(void *dummy); |
29 | 48 |
void *hciConnectionThread_routine(void *dummy); |
30 | 49 |
void *randomValThread_routine(void *dummy); |
logic/trunk/eneraptor.cbp | ||
---|---|---|
38 | 38 |
<Add option="-lmxml" /> |
39 | 39 |
<Add library="pthread" /> |
40 | 40 |
</Linker> |
41 |
<Unit filename="src/hci_comm/hciChanged.h" /> |
|
41 | 42 |
<Unit filename="src/hci_comm/hci_comm.h" /> |
42 | 43 |
<Unit filename="src/hci_comm/hci_comm_listener.c"> |
43 | 44 |
<Option compilerVar="CC" /> |
Also available in: Unified diff