root / logic / trunk / src / hci_comm / hci_comm_listener.c @ 51
History | View | Annotate | Download (3.83 KB)
1 | 10 | Janez1 | /*
|
---|---|---|---|
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 | 51 | alexbesir | #include "../mxml/mxml.h" |
18 | 10 | Janez1 | |
19 | int listenTo(unsigned short int portNum) { |
||
20 | |||
21 | 51 | alexbesir | // Holders
|
22 | char *attrVal = malloc(32*sizeof(char)); |
||
23 | char *name = malloc(32*sizeof(char)); |
||
24 | |||
25 | 10 | Janez1 | // Buffer
|
26 | char *requestBuffer = malloc(HCI_COMM_REQUEST_BUFFER_SIZE * sizeof(char)); |
||
27 | if(requestBuffer == NULL) { |
||
28 | fprintf(stderr,"HCI comm error: Cannot allocate buffer.\n");
|
||
29 | return HCI_COMM_ERR_BUF_ALOC_FAIL;
|
||
30 | } |
||
31 | |||
32 | // Socket holder
|
||
33 | int socketHolder = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||
34 | if(socketHolder < 0) { |
||
35 | fprintf(stderr,"HCI comm error: Cannot create a new socket.\n");
|
||
36 | return HCI_COMM_ERR_NEW_SOCKET_FAIL;
|
||
37 | } |
||
38 | |||
39 | // Socket address holder
|
||
40 | struct sockaddr_in socketAddressHolder;
|
||
41 | memset(&socketAddressHolder, 0, sizeof(socketAddressHolder)); |
||
42 | socketAddressHolder.sin_family = AF_INET; |
||
43 | socketAddressHolder.sin_port = htons(portNum); |
||
44 | socketAddressHolder.sin_addr.s_addr = INADDR_ANY; |
||
45 | |||
46 | // Bind socket to port
|
||
47 | int bindingResult = bind(socketHolder,(struct sockaddr *)&socketAddressHolder, sizeof(socketAddressHolder)); |
||
48 | if(bindingResult == -1) { |
||
49 | fprintf(stderr,"HCI comm error: Cannot bind socket to port %d.\n",portNum);
|
||
50 | close(socketHolder); |
||
51 | return HCI_COMM_ERR_BIND_FAIL;
|
||
52 | } |
||
53 | |||
54 | // Listen to port
|
||
55 | int listenResult = listen(socketHolder, 10); |
||
56 | if(listenResult == -1) { |
||
57 | fprintf(stderr,"HCI comm error: Cannot listen to port %d.\n",portNum);
|
||
58 | close(socketHolder); |
||
59 | return HCI_COMM_ERR_LISTEN_FAIL;
|
||
60 | } |
||
61 | |||
62 | // Infinite listening
|
||
63 | while(1) { |
||
64 | |||
65 | // Accept request
|
||
66 | int connectionHolder = accept(socketHolder,NULL,NULL); |
||
67 | if(connectionHolder < 0) { |
||
68 | fprintf(stderr,"HCI comm error: Error accepting request.\n");
|
||
69 | close(socketHolder); |
||
70 | return HCI_COMM_ERR_ACCEPT_FAIL;
|
||
71 | } |
||
72 | |||
73 | // Read request
|
||
74 | int readResult = read(connectionHolder,requestBuffer,HCI_COMM_REQUEST_BUFFER_SIZE);
|
||
75 | if(readResult < 0) { |
||
76 | fprintf(stderr,"HCI comm error: Error reading request.\n");
|
||
77 | close(socketHolder); |
||
78 | return HCI_COMM_ERR_READ_FAIL;
|
||
79 | } |
||
80 | |||
81 | 51 | alexbesir | // Parse request
|
82 | mxml_node_t *tree; |
||
83 | tree = mxmlLoadString(NULL, requestBuffer, MXML_TEXT_CALLBACK);
|
||
84 | |||
85 | 10 | Janez1 | // Create a response
|
86 | 51 | alexbesir | char *hciResponse = ""; |
87 | int responseLength = 0; |
||
88 | mxml_node_t *node; |
||
89 | node = (tree, tree, "hci-request", "type", NULL, MXML_DESCEND); |
||
90 | strcpy(attrVal,node->value.element.attrs->value); |
||
91 | 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); |
||
96 | } else if(strcmp(attrVal,"getConfig") == 0) { |
||
97 | // TODO
|
||
98 | } else if(strcmp(attrVal,"setConfig") == 0) { |
||
99 | // TODO
|
||
100 | } else if(strcmp(attrVal,"getScenarios") == 0) { |
||
101 | // TODO
|
||
102 | } else if(strcmp(attrVal,"setScenarios") == 0) { |
||
103 | // TODO
|
||
104 | } else if(strcmp(attrVal,"executeAction") == 0) { |
||
105 | // TODO
|
||
106 | } else {
|
||
107 | strcpy(hciResponse,"<logic-response type=\"error\">Invalid request!</logic-response>");
|
||
108 | responseLength = 62;
|
||
109 | } |
||
110 | 10 | Janez1 | |
111 | // Check if logic wants to terminate the infinite loop
|
||
112 | if(responseLength == HCI_COMM_TERMINATE) {
|
||
113 | break;
|
||
114 | } |
||
115 | |||
116 | // Send response
|
||
117 | int writeResult = write(connectionHolder,hciResponse,responseLength);
|
||
118 | if(writeResult < 0) { |
||
119 | fprintf(stderr,"HCI comm error: Error writting response.\n");
|
||
120 | close(socketHolder); |
||
121 | return HCI_COMM_ERR_WRITE_FAIL;
|
||
122 | } |
||
123 | |||
124 | } |
||
125 | |||
126 | // Terminate
|
||
127 | close(socketHolder); |
||
128 | free(requestBuffer); |
||
129 | requestBuffer = NULL;
|
||
130 | return HCI_COMM_TERMINATED_BY_LOGIC;
|
||
131 | |||
132 | } |