-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbluetooth_by_c.c
More file actions
128 lines (115 loc) · 3.16 KB
/
bluetooth_by_c.c
File metadata and controls
128 lines (115 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/rfcomm.h>
#include <omp.h>
int main(int argc, char **argv)
{
inquiry_info *ii = NULL;
int max_rsp, num_rsp;
int dev_id, sock, len, flags;
int i;
char addr[19] = { 0 };
char jason_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");
exit(1);
}
len = 8;
max_rsp = 255;
flags = IREQ_CACHE_FLUSH;
ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));
printf("Searching for devices\n");
num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
if( num_rsp < 0 ) perror("hci_inquiry");
for (i = 0; i < num_rsp; i++) {
ba2str(&(ii+i)->bdaddr, addr);
memset(name, 0, sizeof(name));
if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name),
name, 0) < 0)
strcpy(name, "[unknown]");
printf("Found device: %s %s\n", addr, name);
if(strcmp(name,"Jason-bt00")==0){
memcpy(jason_addr,addr,19);
}
}
if(strcmp(jason_addr,"")==0){
printf("Device not found\n");
return 0;
}
// New socket
int s,status;
struct sockaddr_rc addr_full = { 0 };
char buff[100];
s = socket(AF_BLUETOOTH, SOCK_STREAM & ~O_NONBLOCK, BTPROTO_RFCOMM);
addr_full.rc_family = AF_BLUETOOTH;
addr_full.rc_channel = (uint8_t) 1;
str2ba(jason_addr,&addr_full.rc_bdaddr);
status = connect(s, (struct sockaddr *)&addr_full, sizeof(addr_full));
struct timeval t;
t.tv_sec = 2;
t.tv_usec = 0;
status=setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const void *)(&t),sizeof(t));
if( status < 0 )
perror("uh oh");
int j=0;
#pragma omp parallel sections
{
#pragma omp section
{
int k=0;
while(1){
k++;
sleep(1);
printf("%d\n",k);
}
}
#pragma omp section
{
while(1) {
j++;
status = recv(s, buff, 1, 0);
buff[16]=0;
int i=0;
unsigned short prev;
unsigned short cur;
int bad = 1;
//printf("%d\n",j);
for(i=0;i<16;i++)
//printf("%02x ",buff[i]&0xff);
if(i&&(buff[i]&0xff)==0xf1 && (buff[i-1]&0xff)==0x00){
//printf("%02x %02x\n",buff[i]&0xff,buff[i+1]&0xff);
cur = *(unsigned short *)(buff+1+i);
if((cur&0xff )== 0x00 && (0xff&prev) == 0xff)
bad = 0;
else if(cur == prev+1)
bad = 0;
else if(cur == prev)
bad =2;
prev = cur;
break;
}
if(bad == 1)
printf("loss\n");
//if(bad == 2)
// printf("dup\n");
//printf("\n");
}
}
}
printf("%d\n",status);
close(s);
free( ii );
close( sock );
return 0;
}