Statistics
| Revision:

root / logic / trunk / src / hci_comm / hci_comm_listener.c @ 53

History | View | Annotate | Download (5.28 KB)

1
/*
2
 * hci_comm_listener.c
3
 *
4
 * 2011, Aleksander Besir (alex.besir@gmail.com)
5
 *
6
 */
7

    
8
#include "hci_comm.h"
9
#include <sys/types.h>
10
#include <sys/socket.h>
11
#include <netinet/in.h>
12
#include <arpa/inet.h>
13
#include <stdio.h>
14
#include <stdlib.h>
15
#include <string.h>
16
#include <unistd.h>
17
#include "../mxml/mxml.h"
18

    
19
int listenTo(unsigned short int portNum) {
20

    
21
    // Holders
22
    char *attrVal;
23
    char *name;
24

    
25
        // Buffer
26
        char *requestBuffer = malloc(HCI_COMM_REQUEST_BUFFER_SIZE * sizeof(char));
27
        if(requestBuffer == NULL) {
28
                printf("HCI comm error: Cannot allocate buffer.\n");
29
                return HCI_COMM_ERR_BUF_ALOC_FAIL;
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
        }
36

    
37
        // Socket holder
38
        int socketHolder = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
39
        if(socketHolder < 0) {
40
                fprintf(stderr,"HCI comm error: Cannot create a new socket.\n");
41
                return HCI_COMM_ERR_NEW_SOCKET_FAIL;
42
        }
43

    
44
        // Socket address holder
45
        struct sockaddr_in socketAddressHolder;
46
        memset(&socketAddressHolder, 0, sizeof(socketAddressHolder));
47
        socketAddressHolder.sin_family      = AF_INET;
48
            socketAddressHolder.sin_port        = htons(portNum);
49
            socketAddressHolder.sin_addr.s_addr = INADDR_ANY;
50

    
51
        // Bind socket to port
52
        int bindingResult = bind(socketHolder,(struct sockaddr *)&socketAddressHolder, sizeof(socketAddressHolder));
53
        if(bindingResult == -1) {
54
                fprintf(stderr,"HCI comm error: Cannot bind socket to port %d.\n",portNum);
55
                close(socketHolder);
56
                return HCI_COMM_ERR_BIND_FAIL;
57
        }
58

    
59
        // Listen to port
60
        int listenResult = listen(socketHolder, 10);
61
        if(listenResult == -1) {
62
                printf("HCI comm error: Cannot listen to port %d.\n",portNum);
63
                close(socketHolder);
64
                return HCI_COMM_ERR_LISTEN_FAIL;
65
        }
66

    
67
        // Infinite listening
68
        while(1) {
69

    
70
            printf("[hci_comm_listener] Waiting for new connection...\n");
71

    
72
                // Accept request
73
                int connectionHolder = accept(socketHolder,NULL,NULL);
74
                if(connectionHolder < 0) {
75
                        printf("HCI comm error: Error accepting request.\n");
76
                        close(socketHolder);
77
                        return HCI_COMM_ERR_ACCEPT_FAIL;
78
                }
79

    
80
                printf("[hci_comm_listener] New connection accepted\n");
81

    
82
                // Read request
83
                int readResult = read(connectionHolder,requestBuffer,HCI_COMM_REQUEST_BUFFER_SIZE);
84
                printf("[hci_comm_listener] Recieved request: %s \n",requestBuffer);
85
                if(readResult < 0) {
86
                        printf("HCI comm error: Error reading request.\n");
87
                        close(socketHolder);
88
                        return HCI_COMM_ERR_READ_FAIL;
89
                }
90

    
91
        // Parse request
92
        mxml_node_t *tree;
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");
98

    
99
                // Create a response
100
                printf("[hci_comm_listener] Creating response\n");
101
                int responseLength = 0;
102
                mxml_node_t *node;
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");
113
        if(strcmp(attrVal,"checkConnection") == 0) {
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);
124
        } else if(strcmp(attrVal,"getConfig") == 0) {
125
            // TODO
126
        } else if(strcmp(attrVal,"setConfig") == 0) {
127
            // TODO
128
        } else if(strcmp(attrVal,"getScenarios") == 0) {
129
            // TODO
130
        } else if(strcmp(attrVal,"setScenarios") == 0) {
131
            // TODO
132
        } else if(strcmp(attrVal,"executeAction") == 0) {
133
            // TODO
134
        } else {
135
            strcpy(responseBuffer,"<logic-response type=\"error\">Invalid request!</logic-response>");
136
            responseLength = 62;
137
        }
138

    
139
                // Check if logic wants to terminate the infinite loop
140
                if(responseLength == HCI_COMM_TERMINATE) {
141
                        break;
142
                }
143

    
144
                // Send response
145
                int writeResult = write(connectionHolder,responseBuffer,responseLength);
146
                printf("[hci_comm_listener] Written response to client\n");
147
                if(writeResult < 0) {
148
                        printf("HCI comm error: Error writting response.\n");
149
                        close(socketHolder);
150
                        return HCI_COMM_ERR_WRITE_FAIL;
151
                }
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

    
160
        }
161

    
162
        // Terminate
163
        close(socketHolder);
164
        free(requestBuffer);
165
        requestBuffer = NULL;
166
        return HCI_COMM_TERMINATED_BY_LOGIC;
167

    
168
}