Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
127 | ligi | 1 | #include "bluetooth_handler.h" |
2 | |||
3 | int bt_device_count=0; |
||
4 | |||
5 | char names[MAX_BT_DEVICES][248]; |
||
6 | char addrs[MAX_BT_DEVICES][19]; |
||
7 | |||
130 | ligi | 8 | char BtHostRxBuffer[150]; |
9 | |||
10 | |||
127 | ligi | 11 | int scan_bt() |
12 | { |
||
13 | inquiry_info *ii = NULL; |
||
14 | |||
15 | int dev_id, sock, len, flags; |
||
16 | int i; |
||
17 | char addr[19] = { 0 }; |
||
18 | char name[248] = { 0 }; |
||
19 | |||
20 | dev_id = hci_get_route(NULL); |
||
21 | sock = hci_open_dev( dev_id ); |
||
22 | if (dev_id < 0 || sock < 0) { |
||
23 | perror("opening socket"); |
||
24 | return 0; |
||
25 | } |
||
26 | |||
27 | len = 8; |
||
28 | |||
29 | flags = IREQ_CACHE_FLUSH; |
||
30 | ii = (inquiry_info*)malloc(MAX_BT_DEVICES * sizeof(inquiry_info)); |
||
31 | |||
32 | bt_device_count = hci_inquiry(dev_id, len, MAX_BT_DEVICES, NULL, &ii, flags); |
||
33 | if( bt_device_count < 0 ) perror("hci_inquiry"); |
||
34 | |||
35 | for (i = 0; i < bt_device_count; i++) { |
||
36 | ba2str(&(ii+i)->bdaddr, addr); |
||
37 | sprintf(addrs[i],"%s",addr); |
||
38 | |||
39 | memset(name, 0, sizeof(name)); |
||
40 | |||
41 | if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), |
||
42 | name, 0) < 0) |
||
43 | sprintf(names[i],"[unknown]"); |
||
44 | else |
||
45 | sprintf(names[i],"%s",name); |
||
46 | |||
47 | } |
||
48 | |||
49 | |||
50 | free( ii ); |
||
51 | close( sock ); |
||
52 | return 1; |
||
53 | } |
||
130 | ligi | 54 | |
55 | struct timeval timeout; |
||
56 | fd_set readfds, writefds; |
||
57 | int s; |
||
58 | struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 }; |
||
59 | char buf[1024] = { 0 }; |
||
60 | int client, bytes_read, status; |
||
61 | unsigned int opt = sizeof(rem_addr); |
||
62 | |||
63 | int maxfd, sock_flags; |
||
64 | |||
65 | |||
66 | |||
67 | int bt_host_init() |
||
68 | { |
||
69 | |||
70 | // allocate socket |
||
71 | s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); |
||
72 | |||
73 | // bind socket to port 1 of the first available bluetooth adapter |
||
74 | loc_addr.rc_family = AF_BLUETOOTH; |
||
75 | loc_addr.rc_bdaddr = *BDADDR_ANY; |
||
76 | loc_addr.rc_channel = 1; |
||
77 | bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)); |
||
78 | |||
79 | // put server socket into listening mode |
||
80 | listen(s, 1); |
||
81 | |||
82 | // put server socket into nonblocking mode |
||
83 | sock_flags = fcntl( s, F_GETFL, 0 ); |
||
84 | fcntl( s, F_SETFL, sock_flags | O_NONBLOCK ); |
||
85 | |||
86 | return 1; |
||
87 | } |
||
88 | |||
89 | |||
90 | int bt_host_connected=0; |
||
91 | |||
92 | int bt_host_send(char RxBuffer[150],int len) |
||
93 | { |
||
94 | if (bt_host_connected) |
||
95 | { |
||
96 | send(client,"\r",1,0); |
||
97 | send(client,RxBuffer,len,0); |
||
98 | send(client,"\r",1,0); |
||
99 | } |
||
100 | return 1; |
||
101 | } |
||
102 | |||
103 | int bt_host_tick(int mk_fd) |
||
104 | { |
||
105 | |||
106 | // try to accept connection if none yet |
||
107 | if (!bt_host_connected) |
||
108 | { |
||
109 | timeout.tv_sec = 0; |
||
110 | timeout.tv_usec = 100; |
||
111 | FD_ZERO(&readfds); |
||
112 | FD_ZERO(&writefds); |
||
113 | FD_SET(s, &readfds); |
||
114 | maxfd = s; |
||
115 | |||
116 | printf("waiting for connection\n"); |
||
117 | status = select(maxfd + 1, &readfds, &writefds, 0, &timeout); |
||
118 | if( status > 0 && FD_ISSET( s, &readfds ) ) { |
||
119 | // incoming connection |
||
120 | client = accept( s, (struct sockaddr*)&rem_addr, &opt ); |
||
121 | if( client >= 0 ) |
||
122 | { |
||
123 | bt_host_connected=1; |
||
124 | |||
125 | // close the server socket, leaving only the client socket open |
||
126 | // close(s); |
||
127 | |||
128 | |||
129 | ba2str( &rem_addr.rc_bdaddr, buf ); |
||
130 | fprintf(stderr, "accepted connection from %s!!!!!!!\n", buf); |
||
131 | memset(buf, 0, sizeof(buf)); |
||
132 | |||
133 | // put client socket into nonblocking mode |
||
134 | sock_flags = fcntl( client, F_GETFL, 0 ); |
||
135 | fcntl( client, F_SETFL, sock_flags | O_NONBLOCK ); |
||
136 | } |
||
137 | |||
138 | } |
||
139 | |||
140 | } |
||
141 | else |
||
142 | { |
||
143 | printf("reading from bt_host"); |
||
144 | char in_char='#'; |
||
145 | int count=0; |
||
146 | int r=0; |
||
147 | |||
148 | timeout.tv_sec = 0; |
||
149 | timeout.tv_usec = 100; |
||
150 | FD_ZERO(&readfds); |
||
151 | FD_ZERO(&writefds); |
||
152 | FD_SET(client, &readfds); |
||
153 | maxfd = client; |
||
154 | |||
155 | printf("waiting for connection\n"); |
||
156 | status = select(maxfd + 1, &readfds, 0, 0, &timeout); |
||
157 | if( status >0 ) |
||
158 | { |
||
159 | send(mk_fd,"\r",1,0); |
||
160 | while(in_char!='\r') |
||
161 | { |
||
162 | |||
163 | count=read(client,&in_char,1); |
||
164 | if (count==-1) |
||
165 | { |
||
166 | bt_host_connected=0; |
||
167 | return 0; |
||
168 | } |
||
169 | printf("count: %d in %d chr %c\n",count,in_char,in_char); |
||
170 | send(mk_fd,&in_char,1,0); |
||
171 | BtHostRxBuffer[r]=in_char; |
||
172 | } |
||
173 | send(mk_fd,"\r",1,0); |
||
174 | } |
||
175 | else |
||
176 | return 0; |
||
177 | } |
||
178 | |||
179 | return 1; |
||
180 | } |