-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbtN900.c
More file actions
128 lines (114 loc) · 2.8 KB
/
Copy pathbtN900.c
File metadata and controls
128 lines (114 loc) · 2.8 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
/*
* =====================================================================================
*
* Filename: btN900.c
*
* Description:
*
* Version: 1.0
* Created: 09/15/11 04:15:39
* Revision: none
* Compiler: gcc
*
* Author: Tom Xue (), tom.xue@nokia.com
* Company: Nokia
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
int main (int argc,char **argv)
{
struct sockaddr_rc loc_addr ={0},rem_addr={0};
char buf[1024] ={0},*addr;
int s,client, bytes_read,result;
int opt = sizeof(rem_addr);
printf("Creating socket...\n");
s=socket(AF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);
if(s<0)
{
perror("create socket error");
exit(1);
}
else
{
printf("success!\n");
}
loc_addr.rc_family=AF_BLUETOOTH;
loc_addr.rc_bdaddr=*BDADDR_ANY;
loc_addr.rc_channel=(uint8_t)1;
printf("Binding socket...\n");
result=bind(s,(struct sockaddr *)&loc_addr, sizeof(loc_addr));
if(result<0)
{
perror("bind socket error:");
exit(1);
}
else
{
printf("success!\n");
}
void baswap(bdaddr_t *dst, const bdaddr_t *src)
{
register unsigned char *d = (unsigned char *) dst;
register const unsigned char *s = (const unsigned char *) src;
register int i;
for (i = 0; i < 6; i++)
d[i] = s[5-i];
}
int ba2str(const bdaddr_t *ba, char *str)
{
uint8_t b[6];
baswap((bdaddr_t *) b, ba);
return sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
b[0], b[1], b[2], b[3], b[4], b[5]);
}
/*result=ba2str(&loc_addr.rc_bdaddr,addr);
if(result<0)
{
perror("addr convert error");
exit(1);
}
printf("local addr is:%s\n",addr);*/
printf("Listen... ");
result=listen(s,1);
if(result<0)
{
printf("error:%d\n:",result);
perror("listen error:");
exit(1);
}
else
{
printf("requested!\n");
}
printf("Accepting...\n");
client= accept(s,(struct sockaddr *)&rem_addr,&opt);
if(client<0)
{
perror("accept error");
exit(1);
}
else
{
printf("OK!\n");
}
ba2str(&rem_addr.rc_bdaddr,buf);
fprintf(stderr,"accepted connection from %s \n",buf);
memset(buf,0,sizeof(buf));
while(1)
{
bytes_read = read(client,buf,sizeof(buf));
if(bytes_read>0){
printf("received[%s]\n",buf);
exit(1);
}
}
close(client);
close(s);
return 0 ;
}