Revision 10 logic/trunk/src/hci_comm/hci_comm_listener.c
hci_comm_listener.c | ||
---|---|---|
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 |
|
|
18 |
int listenTo(unsigned short int portNum) { |
|
19 |
|
|
20 |
// Buffer |
|
21 |
char *requestBuffer = malloc(HCI_COMM_REQUEST_BUFFER_SIZE * sizeof(char)); |
|
22 |
if(requestBuffer == NULL) { |
|
23 |
fprintf(stderr,"HCI comm error: Cannot allocate buffer.\n"); |
|
24 |
return HCI_COMM_ERR_BUF_ALOC_FAIL; |
|
25 |
} |
|
26 |
|
|
27 |
// Socket holder |
|
28 |
int socketHolder = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
|
29 |
if(socketHolder < 0) { |
|
30 |
fprintf(stderr,"HCI comm error: Cannot create a new socket.\n"); |
|
31 |
return HCI_COMM_ERR_NEW_SOCKET_FAIL; |
|
32 |
} |
|
33 |
|
|
34 |
// Socket address holder |
|
35 |
struct sockaddr_in socketAddressHolder; |
|
36 |
memset(&socketAddressHolder, 0, sizeof(socketAddressHolder)); |
|
37 |
socketAddressHolder.sin_family = AF_INET; |
|
38 |
socketAddressHolder.sin_port = htons(portNum); |
|
39 |
socketAddressHolder.sin_addr.s_addr = INADDR_ANY; |
|
40 |
|
|
41 |
// Bind socket to port |
|
42 |
int bindingResult = bind(socketHolder,(struct sockaddr *)&socketAddressHolder, sizeof(socketAddressHolder)); |
|
43 |
if(bindingResult == -1) { |
|
44 |
fprintf(stderr,"HCI comm error: Cannot bind socket to port %d.\n",portNum); |
|
45 |
close(socketHolder); |
|
46 |
return HCI_COMM_ERR_BIND_FAIL; |
|
47 |
} |
|
48 |
|
|
49 |
// Listen to port |
|
50 |
int listenResult = listen(socketHolder, 10); |
|
51 |
if(listenResult == -1) { |
|
52 |
fprintf(stderr,"HCI comm error: Cannot listen to port %d.\n",portNum); |
|
53 |
close(socketHolder); |
|
54 |
return HCI_COMM_ERR_LISTEN_FAIL; |
|
55 |
} |
|
56 |
|
|
57 |
// Infinite listening |
|
58 |
while(1) { |
|
59 |
|
|
60 |
// Accept request |
|
61 |
int connectionHolder = accept(socketHolder,NULL,NULL); |
|
62 |
if(connectionHolder < 0) { |
|
63 |
fprintf(stderr,"HCI comm error: Error accepting request.\n"); |
|
64 |
close(socketHolder); |
|
65 |
return HCI_COMM_ERR_ACCEPT_FAIL; |
|
66 |
} |
|
67 |
|
|
68 |
// Read request |
|
69 |
int readResult = read(connectionHolder,requestBuffer,HCI_COMM_REQUEST_BUFFER_SIZE); |
|
70 |
if(readResult < 0) { |
|
71 |
fprintf(stderr,"HCI comm error: Error reading request.\n"); |
|
72 |
close(socketHolder); |
|
73 |
return HCI_COMM_ERR_READ_FAIL; |
|
74 |
} |
|
75 |
|
|
76 |
// Create a response |
|
77 |
char *hciResponse = "Works!"; // TODO |
|
78 |
int responseLength = 6; // TODO |
|
79 |
|
|
80 |
// Check if logic wants to terminate the infinite loop |
|
81 |
if(responseLength == HCI_COMM_TERMINATE) { |
|
82 |
break; |
|
83 |
} |
|
84 |
|
|
85 |
// Send response |
|
86 |
int writeResult = write(connectionHolder,hciResponse,responseLength); |
|
87 |
if(writeResult < 0) { |
|
88 |
fprintf(stderr,"HCI comm error: Error writting response.\n"); |
|
89 |
close(socketHolder); |
|
90 |
return HCI_COMM_ERR_WRITE_FAIL; |
|
91 |
} |
|
92 |
|
|
93 |
} |
|
94 |
|
|
95 |
// Terminate |
|
96 |
close(socketHolder); |
|
97 |
free(requestBuffer); |
|
98 |
requestBuffer = NULL; |
|
99 |
return HCI_COMM_TERMINATED_BY_LOGIC; |
|
100 |
|
|
101 |
} |
|
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 |
|
|
18 |
int listenTo(unsigned short int portNum) { |
|
19 |
|
|
20 |
// Buffer |
|
21 |
char *requestBuffer = malloc(HCI_COMM_REQUEST_BUFFER_SIZE * sizeof(char)); |
|
22 |
if(requestBuffer == NULL) { |
|
23 |
fprintf(stderr,"HCI comm error: Cannot allocate buffer.\n"); |
|
24 |
return HCI_COMM_ERR_BUF_ALOC_FAIL; |
|
25 |
} |
|
26 |
|
|
27 |
// Socket holder |
|
28 |
int socketHolder = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
|
29 |
if(socketHolder < 0) { |
|
30 |
fprintf(stderr,"HCI comm error: Cannot create a new socket.\n"); |
|
31 |
return HCI_COMM_ERR_NEW_SOCKET_FAIL; |
|
32 |
} |
|
33 |
|
|
34 |
// Socket address holder |
|
35 |
struct sockaddr_in socketAddressHolder; |
|
36 |
memset(&socketAddressHolder, 0, sizeof(socketAddressHolder)); |
|
37 |
socketAddressHolder.sin_family = AF_INET; |
|
38 |
socketAddressHolder.sin_port = htons(portNum); |
|
39 |
socketAddressHolder.sin_addr.s_addr = INADDR_ANY; |
|
40 |
|
|
41 |
// Bind socket to port |
|
42 |
int bindingResult = bind(socketHolder,(struct sockaddr *)&socketAddressHolder, sizeof(socketAddressHolder)); |
|
43 |
if(bindingResult == -1) { |
|
44 |
fprintf(stderr,"HCI comm error: Cannot bind socket to port %d.\n",portNum); |
|
45 |
close(socketHolder); |
|
46 |
return HCI_COMM_ERR_BIND_FAIL; |
|
47 |
} |
|
48 |
|
|
49 |
// Listen to port |
|
50 |
int listenResult = listen(socketHolder, 10); |
|
51 |
if(listenResult == -1) { |
|
52 |
fprintf(stderr,"HCI comm error: Cannot listen to port %d.\n",portNum); |
|
53 |
close(socketHolder); |
|
54 |
return HCI_COMM_ERR_LISTEN_FAIL; |
|
55 |
} |
|
56 |
|
|
57 |
// Infinite listening |
|
58 |
while(1) { |
|
59 |
|
|
60 |
// Accept request |
|
61 |
int connectionHolder = accept(socketHolder,NULL,NULL); |
|
62 |
if(connectionHolder < 0) { |
|
63 |
fprintf(stderr,"HCI comm error: Error accepting request.\n"); |
|
64 |
close(socketHolder); |
|
65 |
return HCI_COMM_ERR_ACCEPT_FAIL; |
|
66 |
} |
|
67 |
|
|
68 |
// Read request |
|
69 |
int readResult = read(connectionHolder,requestBuffer,HCI_COMM_REQUEST_BUFFER_SIZE); |
|
70 |
if(readResult < 0) { |
|
71 |
fprintf(stderr,"HCI comm error: Error reading request.\n"); |
|
72 |
close(socketHolder); |
|
73 |
return HCI_COMM_ERR_READ_FAIL; |
|
74 |
} |
|
75 |
|
|
76 |
// Create a response |
|
77 |
char *hciResponse = "Works!"; // TODO |
|
78 |
int responseLength = 6; // TODO |
|
79 |
|
|
80 |
// Check if logic wants to terminate the infinite loop |
|
81 |
if(responseLength == HCI_COMM_TERMINATE) { |
|
82 |
break; |
|
83 |
} |
|
84 |
|
|
85 |
// Send response |
|
86 |
int writeResult = write(connectionHolder,hciResponse,responseLength); |
|
87 |
if(writeResult < 0) { |
|
88 |
fprintf(stderr,"HCI comm error: Error writting response.\n"); |
|
89 |
close(socketHolder); |
|
90 |
return HCI_COMM_ERR_WRITE_FAIL; |
|
91 |
} |
|
92 |
|
|
93 |
} |
|
94 |
|
|
95 |
// Terminate |
|
96 |
close(socketHolder); |
|
97 |
free(requestBuffer); |
|
98 |
requestBuffer = NULL; |
|
99 |
return HCI_COMM_TERMINATED_BY_LOGIC; |
|
100 |
|
|
101 |
} |
Also available in: Unified diff