Rev 127 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 127 | Rev 130 | ||
---|---|---|---|
1 | #include "bluetooth_handler.h" |
1 | #include "bluetooth_handler.h" |
2 | 2 | ||
3 | int bt_device_count=0; |
3 | int bt_device_count=0; |
4 | 4 | ||
5 | char names[MAX_BT_DEVICES][248]; |
5 | char names[MAX_BT_DEVICES][248]; |
6 | char addrs[MAX_BT_DEVICES][19]; |
6 | char addrs[MAX_BT_DEVICES][19]; |
- | 7 | ||
- | 8 | char BtHostRxBuffer[150]; |
|
- | 9 | ||
7 | 10 | ||
8 | int scan_bt() |
11 | int scan_bt() |
9 | { |
12 | { |
10 | inquiry_info *ii = NULL; |
13 | inquiry_info *ii = NULL; |
11 | 14 | ||
12 | int dev_id, sock, len, flags; |
15 | int dev_id, sock, len, flags; |
13 | int i; |
16 | int i; |
14 | char addr[19] = { 0 }; |
17 | char addr[19] = { 0 }; |
15 | char name[248] = { 0 }; |
18 | char name[248] = { 0 }; |
16 | 19 | ||
17 | dev_id = hci_get_route(NULL); |
20 | dev_id = hci_get_route(NULL); |
18 | sock = hci_open_dev( dev_id ); |
21 | sock = hci_open_dev( dev_id ); |
19 | if (dev_id < 0 || sock < 0) { |
22 | if (dev_id < 0 || sock < 0) { |
20 | perror("opening socket"); |
23 | perror("opening socket"); |
21 | return 0; |
24 | return 0; |
22 | } |
25 | } |
23 | 26 | ||
24 | len = 8; |
27 | len = 8; |
25 | 28 | ||
26 | flags = IREQ_CACHE_FLUSH; |
29 | flags = IREQ_CACHE_FLUSH; |
27 | ii = (inquiry_info*)malloc(MAX_BT_DEVICES * sizeof(inquiry_info)); |
30 | ii = (inquiry_info*)malloc(MAX_BT_DEVICES * sizeof(inquiry_info)); |
28 | 31 | ||
29 | bt_device_count = hci_inquiry(dev_id, len, MAX_BT_DEVICES, NULL, &ii, flags); |
32 | bt_device_count = hci_inquiry(dev_id, len, MAX_BT_DEVICES, NULL, &ii, flags); |
30 | if( bt_device_count < 0 ) perror("hci_inquiry"); |
33 | if( bt_device_count < 0 ) perror("hci_inquiry"); |
31 | 34 | ||
32 | for (i = 0; i < bt_device_count; i++) { |
35 | for (i = 0; i < bt_device_count; i++) { |
33 | ba2str(&(ii+i)->bdaddr, addr); |
36 | ba2str(&(ii+i)->bdaddr, addr); |
34 | sprintf(addrs[i],"%s",addr); |
37 | sprintf(addrs[i],"%s",addr); |
35 | 38 | ||
36 | memset(name, 0, sizeof(name)); |
39 | memset(name, 0, sizeof(name)); |
37 | 40 | ||
38 | if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), |
41 | if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), |
39 | name, 0) < 0) |
42 | name, 0) < 0) |
40 | sprintf(names[i],"[unknown]"); |
43 | sprintf(names[i],"[unknown]"); |
41 | else |
44 | else |
42 | sprintf(names[i],"%s",name); |
45 | sprintf(names[i],"%s",name); |
43 | 46 | ||
44 | } |
47 | } |
45 | 48 | ||
46 | 49 | ||
47 | free( ii ); |
50 | free( ii ); |
48 | close( sock ); |
51 | close( sock ); |
49 | return 1; |
52 | return 1; |
50 | } |
53 | } |
- | 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 | } |
|
51 | 181 |