Rev 127 |
Blame |
Last modification |
View Log
| RSS feed
#include "bluetooth_handler.h"
int bt_device_count
=0;
char names
[MAX_BT_DEVICES
][248];
char addrs
[MAX_BT_DEVICES
][19];
char BtHostRxBuffer
[150];
int scan_bt
()
{
inquiry_info
*ii
= NULL
;
int dev_id
, sock
, len
, flags
;
int i
;
char addr
[19] = { 0 };
char name
[248] = { 0 };
dev_id
= hci_get_route
(NULL
);
sock
= hci_open_dev
( dev_id
);
if (dev_id
< 0 || sock
< 0) {
perror("opening socket");
return 0;
}
len
= 8;
flags
= IREQ_CACHE_FLUSH
;
ii
= (inquiry_info
*)malloc(MAX_BT_DEVICES
* sizeof(inquiry_info
));
bt_device_count
= hci_inquiry
(dev_id
, len
, MAX_BT_DEVICES
, NULL
, &ii
, flags
);
if( bt_device_count
< 0 ) perror("hci_inquiry");
for (i
= 0; i
< bt_device_count
; i
++) {
ba2str
(&(ii
+i
)->bdaddr
, addr
);
sprintf(addrs
[i
],"%s",addr
);
memset(name
, 0, sizeof(name
));
if (hci_read_remote_name
(sock
, &(ii
+i
)->bdaddr
, sizeof(name
),
name
, 0) < 0)
sprintf(names
[i
],"[unknown]");
else
sprintf(names
[i
],"%s",name
);
}
free( ii
);
close
( sock
);
return 1;
}
struct timeval timeout
;
fd_set readfds
, writefds
;
int s
;
struct sockaddr_rc loc_addr
= { 0 }, rem_addr
= { 0 };
char buf
[1024] = { 0 };
int client
, bytes_read
, status
;
unsigned int opt
= sizeof(rem_addr
);
int maxfd
, sock_flags
;
int bt_host_init
()
{
// allocate socket
s
= socket
(AF_BLUETOOTH
, SOCK_STREAM
, BTPROTO_RFCOMM
);
// bind socket to port 1 of the first available bluetooth adapter
loc_addr.
rc_family = AF_BLUETOOTH
;
loc_addr.
rc_bdaddr = *BDADDR_ANY
;
loc_addr.
rc_channel = 1;
bind
(s
, (struct sockaddr
*)&loc_addr
, sizeof(loc_addr
));
// put server socket into listening mode
listen
(s
, 1);
// put server socket into nonblocking mode
sock_flags
= fcntl
( s
, F_GETFL
, 0 );
fcntl
( s
, F_SETFL
, sock_flags
| O_NONBLOCK
);
return 1;
}
int bt_host_connected
=0;
int bt_host_send
(char RxBuffer
[150],int len
)
{
if (bt_host_connected
)
{
send
(client
,"\r",1,0);
send
(client
,RxBuffer
,len
,0);
send
(client
,"\r",1,0);
}
return 1;
}
int bt_host_tick
(int mk_fd
)
{
// try to accept connection if none yet
if (!bt_host_connected
)
{
timeout.
tv_sec = 0;
timeout.
tv_usec = 100;
FD_ZERO
(&readfds
);
FD_ZERO
(&writefds
);
FD_SET
(s
, &readfds
);
maxfd
= s
;
printf("waiting for connection\n");
status
= select
(maxfd
+ 1, &readfds
, &writefds
, 0, &timeout
);
if( status
> 0 && FD_ISSET
( s
, &readfds
) ) {
// incoming connection
client
= accept
( s
, (struct sockaddr
*)&rem_addr
, &opt
);
if( client
>= 0 )
{
bt_host_connected
=1;
// close the server socket, leaving only the client socket open
// close(s);
ba2str
( &rem_addr.
rc_bdaddr, buf
);
fprintf(stderr
, "accepted connection from %s!!!!!!!\n", buf
);
memset(buf
, 0, sizeof(buf
));
// put client socket into nonblocking mode
sock_flags
= fcntl
( client
, F_GETFL
, 0 );
fcntl
( client
, F_SETFL
, sock_flags
| O_NONBLOCK
);
}
}
}
else
{
printf("reading from bt_host");
char in_char
='#';
int count
=0;
int r
=0;
timeout.
tv_sec = 0;
timeout.
tv_usec = 100;
FD_ZERO
(&readfds
);
FD_ZERO
(&writefds
);
FD_SET
(client
, &readfds
);
maxfd
= client
;
printf("waiting for connection\n");
status
= select
(maxfd
+ 1, &readfds
, 0, 0, &timeout
);
if( status
>0 )
{
send
(mk_fd
,"\r",1,0);
while(in_char
!='\r')
{
count
=read
(client
,&in_char
,1);
if (count
==-1)
{
bt_host_connected
=0;
return 0;
}
printf("count: %d in %d chr %c\n",count
,in_char
,in_char
);
send
(mk_fd
,&in_char
,1,0);
BtHostRxBuffer
[r
]=in_char
;
}
send
(mk_fd
,"\r",1,0);
}
else
return 0;
}
return 1;
}