Revision 7
logic/trunk/Makefile | ||
---|---|---|
1 |
# |
|
2 |
# (2011, Aleksander Besir (alex.besir@gmail.com) |
|
3 |
# |
|
4 |
|
|
5 |
CC=gcc |
|
6 |
CFLAGS=-Wall -I . |
|
7 |
BIN=bin |
|
8 |
SRC=src |
|
9 |
|
|
10 |
OBJ=$(SRC)/hci_comm/hci_comm_listener.o $(SRC)/startup/main.o |
|
11 |
OBJC=$(SRC)/buf.h $(SRC)/buf_nf.c $(SRC)/buf_sim.h $(SRC)/buf_sim.c |
|
12 |
|
|
13 |
all: $(BIN)/startup |
|
14 |
|
|
15 |
%.o: %.c |
|
16 |
$(CC) -c -o $@ $< $(CFLAGS) |
|
17 |
|
|
18 |
$(BIN)/Startup: $(OBJ) |
|
19 |
$(CC) -o $(BIN)/startup $^ $(CFLAGS) |
logic/trunk/src/hci_comm/hci_comm.h | ||
---|---|---|
1 |
/* |
|
2 |
* hci_comm.h |
|
3 |
* |
|
4 |
* 2011, Aleksander Besir (alex.besir@gmail.com) |
|
5 |
* |
|
6 |
*/ |
|
7 |
|
|
8 |
#ifndef _ENERAPTOR_HCI_COMM |
|
9 |
#define _ENERAPTOR_HCI_COMM |
|
10 |
|
|
11 |
// Error codes |
|
12 |
#define HCI_COMM_TERMINATED_BY_LOGIC 0 // Terminated by logic |
|
13 |
#define HCI_COMM_ERR_NEW_SOCKET_FAIL -1 // Cannot create new socket |
|
14 |
#define HCI_COMM_ERR_BIND_FAIL -2 // Cannot bind socket to port |
|
15 |
#define HCI_COMM_ERR_LISTEN_FAIL -3 // Cannot listen to port |
|
16 |
#define HCI_COMM_ERR_ACCEPT_FAIL -4 // Cannot accept request |
|
17 |
#define HCI_COMM_ERR_READ_FAIL -5 // Cannot read request |
|
18 |
#define HCI_COMM_ERR_WRITE_FAIL -6 // Cannot write response |
|
19 |
#define HCI_COMM_ERR_BUF_ALOC_FAIL -99 // Cannot alocate memory for buffer |
|
20 |
|
|
21 |
// Program codes |
|
22 |
#define HCI_COMM_TERMINATE -1 // Terminates the infinite listening loop |
|
23 |
|
|
24 |
// Constants |
|
25 |
#define HCI_COMM_REQUEST_BUFFER_SIZE 1024 // Bufer size in bytes |
|
26 |
// TODO: buffer size should be bigger! |
|
27 |
|
|
28 |
int listenTo(unsigned short int portNum); |
|
29 |
|
|
30 |
#endif |
logic/trunk/src/hci_comm/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 |
fpritf(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 |
stSockAddr.sin_family = AF_INET; |
|
38 |
stSockAddr.sin_port = htons(portNum); |
|
39 |
stSockAddr.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 |
fpritf(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 |
fpritf(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(true) { |
|
59 |
|
|
60 |
// Accept request |
|
61 |
int connectionHolder = accept(socketHolder,NULL,NULL); |
|
62 |
if(connectionHolder < 0) { |
|
63 |
fpritf(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 |
fpritf(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 |
fpritf(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 |
} |
logic/trunk/src/startup/main.c | ||
---|---|---|
1 |
|
|
2 |
#import "hci_comm/hci_comm.h" |
|
3 |
|
|
4 |
int main(int argc, char *argv[]) { |
|
5 |
|
|
6 |
listenTo(1100); |
|
7 |
return 0; |
|
8 |
|
|
9 |
} |
Also available in: Unified diff