Return-Path: Message-ID: <6297d67c0606252100l772b6de2ida4b3dc07c2b89a6@mail.gmail.com> Date: Mon, 26 Jun 2006 09:30:44 +0530 From: "Mayank Batra" To: "BlueZ development" In-Reply-To: <200606251632.12624.g.issel@fh-wolfenbuettel.de> MIME-Version: 1.0 References: <200606251632.12624.g.issel@fh-wolfenbuettel.de> Subject: Re: [Bluez-devel] reading response on bluetooth socket Reply-To: BlueZ development List-Id: BlueZ development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: multipart/mixed; boundary="===============0032573926==" Sender: bluez-devel-bounces@lists.sourceforge.net Errors-To: bluez-devel-bounces@lists.sourceforge.net --===============0032573926== Content-Type: multipart/alternative; boundary="----=_Part_38147_14034279.1151294444825" ------=_Part_38147_14034279.1151294444825 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi Goetz, On 6/25/06, G=F6tz Issel wrote: > > After having set up a rfcommm connection from my notebook to a mobile > phone I > need to get the response data from the mobile device. I can read from the > socket using 'recv()' but if no data is there at thaht moment this will > block > the process or pause my program. What I need to do is get a signal on > 'data > available' on the socket for reading or a non blocking read. I heard ther= e > is > a function 'select()' .. but it does not seem to work or I don't > understand > it. It needs to be as simple as possible. This is correct. A read() will block your thread completely. You have two options: - Write from one thread and read from another (Doesn't sound interesting!) - Use select This is what I have now: > > // init + declare variables > fd_set readFds; > struct timeval timeout; > struct sockaddr_rc addr =3D { 0 }; > int rfcomm_sock, status; > > // allocate a socket > rfcomm_sock =3D socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)= ; > > // set the connection parameters (who to connect to) > addr.rc_family =3D AF_BLUETOOTH; > addr.rc_channel =3D (uint8_t) dest_channel; > str2ba( dest, &addr.rc_bdaddr ); > > // connect to server, here mobile phone > status =3D connect(rfcomm_sock, (struct sockaddr *)&addr, > sizeof(addr)); > > timeout.tv_sec=3D5; > timeout.tv_usec=3D0; > FD_ZERO(&readFds); > FD_SET(rfcomm_socket, &readFds); > if (select(rfcommsocket + 1, &readFds, NULL, NULL, &timeout) =3D= =3D 0) > { > printf("timed out"); > } > else > printf("data ready for read"); Ahh, I assume that rfcomm_sock, rfcomm_socket and rfcommsocket refer to the same thing. God! did the above code ever compile?? Try the following code snippet which works for me: fd_set readfds; FD_ZERO(&readfds); FD_SET(rfcomm_sock, &readfds); while(1) { int ready =3D select(FD_SETSIZE, &readfds, 0, 0, 0); if(ready =3D=3D 0) continue; if(ready =3D=3D -1 && errno !=3D EINTR) break; if(FD_ISSET(rfcomm_sock, &readfds)) { /* You've got something on rfcomm_sock, perform a read */ int size =3D 1024; size =3D read(rfcomm_sock, &buf, 1024);//buf is a char array declared somewhere /* Now, you have got the data in buf, parse it for the SAP header and send a response if required */ } } Hope that helps. Regards, Mayank ------=_Part_38147_14034279.1151294444825 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi Goetz,

On 6/25/06, = G=F6tz Issel <g.issel= @fh-wolfenbuettel.de> wrote:
After having set up a rfcommm co= nnection from my notebook to a mobile phone I
need to get the response d= ata from the mobile device. I can read from the
socket using 'recv()' but if no data is there at thaht moment this will= block
the process or pause my program. What I need to do is get a signa= l on 'data
available' on the socket for reading or a non blocking read. = I heard there is
a function 'select()' .. but it does not seem to work or I don't unders= tand
it. It needs to be as simple as possible.
 
This is correct. A read() will block your thread completely.
You have two options:
- Write from one thread and read from another (Doesn't sound interesti= ng!)
- Use select

This is what I have now:

= // init + declare variables
       fd_set = readFds;
       struct timeval timeout;
       struct sockaddr_rc addr =3D { 0 };=
       int rfcomm_sock, status;

//= allocate a socket
        rfcom= m_sock =3D socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

// set= the connection parameters (who to connect to)
       addr.rc_family =3D AF_BLUETOOTH;       addr.rc_channel =3D (uint8_t) dest_c= hannel;
       str2ba( dest, &addr.rc_= bdaddr );

// connect to server, here mobile phone
  &nb= sp;     status =3D connect(rfcomm_sock, (struct so= ckaddr *)&addr, sizeof(addr));

       timeout.tv_sec=3D5;
 &= nbsp;     timeout.tv_usec=3D0;
   &nb= sp;   FD_ZERO(&readFds);
     &nb= sp; FD_SET(rfcomm_socket, &readFds);
     &= nbsp; if (select(rfcommsocket + 1, &readFds, NULL, NULL, &timeout) = =3D=3D 0)
       {
     = ;          printf("timed = out");
       }
   =     else
        =        printf("data ready for read"= );
 
Ahh, I assume that rfcomm_sock, rfcomm_socket and rfcommsocket refer t= o the same thing.
God! did the above code ever compile??
 
Try the following code snippet which works for me:
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(rfcomm_sock, &= readfds);
while(1) {
  int ready =3D select(FD_SETSIZE, &readfds, 0, 0, 0);  if(ready =3D=3D 0)
   continue;
 &n= bsp;if(ready =3D=3D -1 && errno !=3D EINTR)
   br= eak;
  if(FD_ISSET(rfcomm_sock, &readfds)) {
    /* You've got something on rfcomm_sock, perfor= m a read */
   int size =3D 1024;
   size =3D read(rfc= omm_sock, &buf, 1024);//buf is a char array declared somewhere
   /* Now, you have got the data in buf, parse it for the SA= P header and send a response if required */
  }
}
 
 
Hope that helps.
 
Regards,
Mayank
------=_Part_38147_14034279.1151294444825-- --===============0032573926== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 --===============0032573926== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Bluez-devel mailing list Bluez-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bluez-devel --===============0032573926==--