Revision 57 logic/trunk/src/hci_comm/hci_comm_listener.c
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>"); |
Also available in: Unified diff