Subversion Repositories Projects

Rev

Rev 127 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 127 Rev 130
Line 3... Line 3...
3
int bt_device_count=0;
3
int bt_device_count=0;
Line 4... Line 4...
4
 
4
 
5
char names[MAX_BT_DEVICES][248];
5
char names[MAX_BT_DEVICES][248];
Line -... Line 6...
-
 
6
char addrs[MAX_BT_DEVICES][19];
-
 
7
 
-
 
8
char BtHostRxBuffer[150];
6
char addrs[MAX_BT_DEVICES][19];
9
 
7
 
10
 
8
int scan_bt()
11
int scan_bt()
Line 9... Line 12...
9
{
12
{
Line 46... Line 49...
46
 
49
 
47
  free( ii );
50
  free( ii );
48
  close( sock );
51
  close( sock );
49
  return 1;
52
  return 1;
-
 
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;