All of lore.kernel.org
 help / color / mirror / Atom feed
* [Bluez-devel] reading response on bluetooth socket
@ 2006-06-25 14:32 Götz Issel
  2006-06-25 14:52 ` Marcel Holtmann
  2006-06-26  4:00 ` Mayank Batra
  0 siblings, 2 replies; 3+ messages in thread
From: Götz Issel @ 2006-06-25 14:32 UTC (permalink / raw)
  To: bluez-devel

Hi all,

I would like a sample snipplet of code to get help on the following problem:

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 there 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 what I have now:

// init + declare variables
	fd_set readFds;
	struct timeval timeout;
    	struct sockaddr_rc addr = { 0 };
   	int rfcomm_sock, status; 
	
// allocate a socket
   	 rfcomm_sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

// set the connection parameters (who to connect to)
    	addr.rc_family = AF_BLUETOOTH;
    	addr.rc_channel = (uint8_t) dest_channel;
    	str2ba( dest, &addr.rc_bdaddr );

// connect to server, here mobile phone
   	 status = connect(rfcomm_sock, (struct sockaddr *)&addr, sizeof(addr));

	timeout.tv_sec=5;
	timeout.tv_usec=0;	
	FD_ZERO(&readFds);
	FD_SET(rfcomm_socket, &readFds);
	if (select(rfcommsocket + 1, &readFds, NULL, NULL, &timeout) == 0) 
	{
		printf("timed out");
	}
	else
		printf("data ready for read");


Anyone done this  before? I have seen something like this in the source file 
'sdp.c' but somehow I am missing something.

Thanks for your help in advance.

Goetz




	 

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
_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Bluez-devel] reading response on bluetooth socket
  2006-06-25 14:32 [Bluez-devel] reading response on bluetooth socket Götz Issel
@ 2006-06-25 14:52 ` Marcel Holtmann
  2006-06-26  4:00 ` Mayank Batra
  1 sibling, 0 replies; 3+ messages in thread
From: Marcel Holtmann @ 2006-06-25 14:52 UTC (permalink / raw)
  To: BlueZ development

Hi Goetz,

> 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 there 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 what I have now:
> 
> // init + declare variables
> 	fd_set readFds;
> 	struct timeval timeout;
>     	struct sockaddr_rc addr = { 0 };

don't do this. Use memset() instead.

>    	int rfcomm_sock, status; 
> 	
> // allocate a socket
>    	 rfcomm_sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
> 
> // set the connection parameters (who to connect to)
>     	addr.rc_family = AF_BLUETOOTH;
>     	addr.rc_channel = (uint8_t) dest_channel;

Why do you cast here. No need in doing this. Get you declarations right.

>     	str2ba( dest, &addr.rc_bdaddr );
> 
> // connect to server, here mobile phone
>    	 status = connect(rfcomm_sock, (struct sockaddr *)&addr, sizeof(addr));
> 
> 	timeout.tv_sec=5;
> 	timeout.tv_usec=0;	
> 	FD_ZERO(&readFds);
> 	FD_SET(rfcomm_socket, &readFds);

What is it? rfcomm_sock, rfcomm_socket or rfcommsocket. Get your code
clean.

> 	if (select(rfcommsocket + 1, &readFds, NULL, NULL, &timeout) == 0) 
> 	{
> 		printf("timed out");
> 	}
> 	else
> 		printf("data ready for read");

And you should check the manual page of select() or move over using
poll(). This might be a better example:

        while (1) {
                fd_set rset;

                FD_ZERO(&rset);
                FD_SET(sk, &rset);

                if (select(sk + 1, &rset, NULL, NULL, NULL) < 0)
                        break;

                if (!FD_ISSET(sk, &rset))
                        continue;

                ...
        }

Regards

Marcel



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
_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Bluez-devel] reading response on bluetooth socket
  2006-06-25 14:32 [Bluez-devel] reading response on bluetooth socket Götz Issel
  2006-06-25 14:52 ` Marcel Holtmann
@ 2006-06-26  4:00 ` Mayank Batra
  1 sibling, 0 replies; 3+ messages in thread
From: Mayank Batra @ 2006-06-26  4:00 UTC (permalink / raw)
  To: BlueZ development


[-- Attachment #1.1: Type: text/plain, Size: 2526 bytes --]

Hi Goetz,

On 6/25/06, Götz Issel <g.issel@fh-wolfenbuettel.de> 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 there
> 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 = { 0 };
>        int rfcomm_sock, status;
>
> // allocate a socket
>         rfcomm_sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
>
> // set the connection parameters (who to connect to)
>        addr.rc_family = AF_BLUETOOTH;
>        addr.rc_channel = (uint8_t) dest_channel;
>        str2ba( dest, &addr.rc_bdaddr );
>
> // connect to server, here mobile phone
>         status = connect(rfcomm_sock, (struct sockaddr *)&addr,
> sizeof(addr));
>
>        timeout.tv_sec=5;
>        timeout.tv_usec=0;
>        FD_ZERO(&readFds);
>        FD_SET(rfcomm_socket, &readFds);
>        if (select(rfcommsocket + 1, &readFds, NULL, NULL, &timeout) == 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 = select(FD_SETSIZE, &readfds, 0, 0, 0);
  if(ready == 0)
   continue;
  if(ready == -1 && errno != EINTR)
   break;
  if(FD_ISSET(rfcomm_sock, &readfds)) {
    /* You've got something on rfcomm_sock, perform a read */
   int size = 1024;
   size = 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

[-- Attachment #1.2: Type: text/html, Size: 4109 bytes --]

[-- Attachment #2: Type: text/plain, Size: 299 bytes --]

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

[-- Attachment #3: Type: text/plain, Size: 164 bytes --]

_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2006-06-26  4:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-06-25 14:32 [Bluez-devel] reading response on bluetooth socket Götz Issel
2006-06-25 14:52 ` Marcel Holtmann
2006-06-26  4:00 ` Mayank Batra

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.