19 |
19 |
int listenTo(unsigned short int portNum) {
|
20 |
20 |
|
21 |
21 |
// Holders
|
22 |
|
char *attrVal = malloc(32*sizeof(char));
|
23 |
|
char *name = malloc(32*sizeof(char));
|
|
22 |
char *attrVal;
|
|
23 |
char *name;
|
24 |
24 |
|
25 |
25 |
// Buffer
|
26 |
26 |
char *requestBuffer = malloc(HCI_COMM_REQUEST_BUFFER_SIZE * sizeof(char));
|
27 |
27 |
if(requestBuffer == NULL) {
|
28 |
|
fprintf(stderr,"HCI comm error: Cannot allocate buffer.\n");
|
|
28 |
printf("HCI comm error: Cannot allocate buffer.\n");
|
29 |
29 |
return HCI_COMM_ERR_BUF_ALOC_FAIL;
|
30 |
30 |
}
|
|
31 |
char *responseBuffer = malloc(HCI_COMM_RESPONSE_BUFFER_SIZE * sizeof(char));
|
|
32 |
if(responseBuffer == NULL) {
|
|
33 |
printf("HCI comm error: Cannot allocate buffer.\n");
|
|
34 |
return HCI_COMM_ERR_BUF_ALOC_FAIL;
|
|
35 |
}
|
31 |
36 |
|
32 |
37 |
// Socket holder
|
33 |
38 |
int socketHolder = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
... | ... | |
54 |
59 |
// Listen to port
|
55 |
60 |
int listenResult = listen(socketHolder, 10);
|
56 |
61 |
if(listenResult == -1) {
|
57 |
|
fprintf(stderr,"HCI comm error: Cannot listen to port %d.\n",portNum);
|
|
62 |
printf("HCI comm error: Cannot listen to port %d.\n",portNum);
|
58 |
63 |
close(socketHolder);
|
59 |
64 |
return HCI_COMM_ERR_LISTEN_FAIL;
|
60 |
65 |
}
|
... | ... | |
62 |
67 |
// Infinite listening
|
63 |
68 |
while(1) {
|
64 |
69 |
|
|
70 |
printf("[hci_comm_listener] Waiting for new connection...\n");
|
|
71 |
|
65 |
72 |
// Accept request
|
66 |
73 |
int connectionHolder = accept(socketHolder,NULL,NULL);
|
67 |
74 |
if(connectionHolder < 0) {
|
68 |
|
fprintf(stderr,"HCI comm error: Error accepting request.\n");
|
|
75 |
printf("HCI comm error: Error accepting request.\n");
|
69 |
76 |
close(socketHolder);
|
70 |
77 |
return HCI_COMM_ERR_ACCEPT_FAIL;
|
71 |
78 |
}
|
72 |
79 |
|
|
80 |
printf("[hci_comm_listener] New connection accepted\n");
|
|
81 |
|
73 |
82 |
// Read request
|
74 |
83 |
int readResult = read(connectionHolder,requestBuffer,HCI_COMM_REQUEST_BUFFER_SIZE);
|
|
84 |
printf("[hci_comm_listener] Recieved request: %s \n",requestBuffer);
|
75 |
85 |
if(readResult < 0) {
|
76 |
|
fprintf(stderr,"HCI comm error: Error reading request.\n");
|
|
86 |
printf("HCI comm error: Error reading request.\n");
|
77 |
87 |
close(socketHolder);
|
78 |
88 |
return HCI_COMM_ERR_READ_FAIL;
|
79 |
89 |
}
|
80 |
90 |
|
81 |
91 |
// Parse request
|
82 |
92 |
mxml_node_t *tree;
|
83 |
|
tree = mxmlLoadString(NULL, requestBuffer, MXML_TEXT_CALLBACK);
|
|
93 |
tree = mxmlLoadString(NULL, requestBuffer, MXML_NO_CALLBACK);
|
|
94 |
if(tree == NULL) {
|
|
95 |
printf("[hci_comm_listener] Error parsing request!\n");
|
|
96 |
}
|
|
97 |
printf("[hci_comm_listener] Request parsed\n");
|
84 |
98 |
|
85 |
99 |
// Create a response
|
86 |
|
char *hciResponse = "";
|
|
100 |
printf("[hci_comm_listener] Creating response\n");
|
87 |
101 |
int responseLength = 0;
|
88 |
102 |
mxml_node_t *node;
|
89 |
|
node = (tree, tree, "hci-request", "type", NULL, MXML_DESCEND);
|
90 |
|
strcpy(attrVal,node->value.element.attrs->value);
|
|
103 |
node = mxmlFindElement(tree, tree, "hci-request", NULL, NULL, MXML_DESCEND);
|
|
104 |
if(node == NULL) {
|
|
105 |
printf("[hci_comm_listener] Error: Recieved node is NULL!\n");
|
|
106 |
}
|
|
107 |
printf("[hci_comm_listener] Main request node found\n");
|
|
108 |
printf("[hci_comm_listener] %d\n",node);
|
|
109 |
|
|
110 |
attrVal = (char *)malloc(sizeof(node->value.element.attrs->value)*strlen(mxmlElementGetAttr(node, "type")));
|
|
111 |
attrVal = mxmlElementGetAttr(node,"type");
|
|
112 |
printf("[hci_comm_listener] Got type\n");
|
91 |
113 |
if(strcmp(attrVal,"checkConnection") == 0) {
|
92 |
|
strcat(hciResponse,"<logic-response type=\"checkConnection\"><data>");
|
93 |
|
strcat(hciResponse,node->child->value.text.string);
|
94 |
|
strcat(hciResponse,"</data></logic-response>");
|
95 |
|
responseLength = strlen(hciResponse);
|
|
114 |
printf("[hci_comm_listener] checkConnection request\n");
|
|
115 |
strcpy(responseBuffer,"");
|
|
116 |
strcat(responseBuffer,"<logic-response type=\"checkConnection\"><data>");
|
|
117 |
mxml_node_t *child = node->child;
|
|
118 |
child = child->child;
|
|
119 |
strcat(responseBuffer,child->value.text.string);
|
|
120 |
strcat(responseBuffer,"</data></logic-response>");
|
|
121 |
printf("[hci_comm_listener] Response formed: %s\n",responseBuffer);
|
|
122 |
responseLength = strlen(responseBuffer);
|
|
123 |
printf("[hci_comm_listener] Response length formed: %d\n",responseLength);
|
96 |
124 |
} else if(strcmp(attrVal,"getConfig") == 0) {
|
97 |
125 |
// TODO
|
98 |
126 |
} else if(strcmp(attrVal,"setConfig") == 0) {
|
... | ... | |
104 |
132 |
} else if(strcmp(attrVal,"executeAction") == 0) {
|
105 |
133 |
// TODO
|
106 |
134 |
} else {
|
107 |
|
strcpy(hciResponse,"<logic-response type=\"error\">Invalid request!</logic-response>");
|
|
135 |
strcpy(responseBuffer,"<logic-response type=\"error\">Invalid request!</logic-response>");
|
108 |
136 |
responseLength = 62;
|
109 |
137 |
}
|
110 |
138 |
|
... | ... | |
114 |
142 |
}
|
115 |
143 |
|
116 |
144 |
// Send response
|
117 |
|
int writeResult = write(connectionHolder,hciResponse,responseLength);
|
|
145 |
int writeResult = write(connectionHolder,responseBuffer,responseLength);
|
|
146 |
printf("[hci_comm_listener] Written response to client\n");
|
118 |
147 |
if(writeResult < 0) {
|
119 |
|
fprintf(stderr,"HCI comm error: Error writting response.\n");
|
|
148 |
printf("HCI comm error: Error writting response.\n");
|
120 |
149 |
close(socketHolder);
|
121 |
150 |
return HCI_COMM_ERR_WRITE_FAIL;
|
122 |
151 |
}
|
123 |
152 |
|
|
153 |
// Close connection
|
|
154 |
free(attrVal);
|
|
155 |
shutdown(connectionHolder,SHUT_RDWR);
|
|
156 |
close(connectionHolder);
|
|
157 |
|
|
158 |
printf("[hci_comm_listener] Connection closed\n");
|
|
159 |
|
124 |
160 |
}
|
125 |
161 |
|
126 |
162 |
// Terminate
|