Subversion Repositories Projects

Rev

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
 
483 ligi 55
//struct timeval timeout;
130 ligi 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
  {
483 ligi 109
    //    timeout.tv_sec = 0;
110
    // timeout.tv_usec = 100;
130 ligi 111
    FD_ZERO(&readfds);
112
    FD_ZERO(&writefds);
113
    FD_SET(s, &readfds);
114
    maxfd = s;
115
 
116
    printf("waiting for connection\n");
483 ligi 117
    //    status = select(maxfd + 1, &readfds, &writefds, 0, &timeout);
118
    status = select(maxfd + 1, &readfds, &writefds, 0,& (struct timeval) { 0, 100 });
130 ligi 119
    if( status > 0 && FD_ISSET( s, &readfds ) ) {
120
      // incoming connection
121
      client = accept( s, (struct sockaddr*)&rem_addr, &opt );
122
      if( client >= 0 )
123
        {
124
          bt_host_connected=1;
125
 
126
          // close the server socket, leaving only the client socket open
127
          //      close(s);
128
 
129
 
130
          ba2str( &rem_addr.rc_bdaddr, buf );
131
          fprintf(stderr, "accepted connection from %s!!!!!!!\n", buf);
132
          memset(buf, 0, sizeof(buf));
133
 
134
          // put client socket into nonblocking mode
135
          sock_flags = fcntl( client, F_GETFL, 0 );
136
          fcntl( client, F_SETFL, sock_flags | O_NONBLOCK );
137
        }
138
 
139
    }
140
 
141
  }
142
  else
143
    {
144
      printf("reading from bt_host");
145
      char in_char='#';
146
      int count=0;
147
      int r=0;
148
 
483 ligi 149
      //      timeout.tv_sec = 0;
150
      //timeout.tv_usec = 100;
130 ligi 151
      FD_ZERO(&readfds);
152
      FD_ZERO(&writefds);
153
      FD_SET(client, &readfds);
154
      maxfd = client;
155
 
156
      printf("waiting for connection\n");
483 ligi 157
      //status = select(maxfd + 1, &readfds, 0, 0, &timeout);
130 ligi 158
      if( status >0 )
159
        {
160
          send(mk_fd,"\r",1,0);
161
          while(in_char!='\r')
162
            {
163
 
164
              count=read(client,&in_char,1);
165
              if (count==-1)  
166
                {
167
                  bt_host_connected=0;
168
                  return 0;    
169
                }
170
              printf("count: %d in  %d chr %c\n",count,in_char,in_char);
171
              send(mk_fd,&in_char,1,0);
172
              BtHostRxBuffer[r]=in_char;
173
            }
174
          send(mk_fd,"\r",1,0);
175
        }
176
          else
177
        return 0;
178
    }
179
 
180
  return 1;    
181
}