2011-03-03 00:50:01

by Han

[permalink] [raw]
Subject: cannot setup multiple connection using RFCOMM

Hi,

I have a client / server application running over bluetooth using
RFCOMM as transport. I am using socket API in C with AF_BLUETOOTH,
SOCK_STREAM and BTPROTO_RFCOMM.

One issue I see is that I cannot have more than 1 client connected to
the same server over the RFCOMM. The server listens on channel 1 and
forks a child process after accepting the connection, and the parent
process continues to listen. But the 2nd client always gets "Device or
resource busy". Is there something I need to do to enable multiple
client connections ?

thanks.
Han


2011-03-03 08:59:56

by Han

[permalink] [raw]
Subject: Re: cannot setup multiple connection using RFCOMM

Brad,

I tried the L2CAP as the transport and now I can have multiple client
connected at the same time. Thanks !

Han

On Wed, Mar 2, 2011 at 8:45 PM, Han <[email protected]> wrote:
> On Wed, Mar 2, 2011 at 6:31 PM, Brad Midgley <[email protected]> wrote:
>> Han
>>
>>> I have a client / server application running over bluetooth using
>>> RFCOMM as transport. ?I am using socket API in C with AF_BLUETOOTH,
>>> SOCK_STREAM and BTPROTO_RFCOMM.
>>
>> rfcomm is the wrong protocol if you want to have multiple connections
>> to one server.
>
> Thanks Brad. What should be the right protocol to use?
>
>>
>> In case you have no choice, I've appended a very old howto suggesting
>> a way to multiplex rfcomm for gps, but it's generally unreliable
>> because clients cache sdp records unpredictably.
>
> thanks for the detailed info. ?It seems that rfcomm.conf has to
> include one entry for each connection with different channel? In my
> case, I really don't know how many (parallel) ?client connections in
> advance. It's all dynamic.
>
> Han
>
>>
>> ================================
>>
>> Bluetooth & GPS
>> I would like to have a multifunction GPS unit... logging, repeating
>> gps location data over bluetooth for two clients. gpsd is running and
>> receiving from the local gps.
>>
>> Next, to make it repeat the gps stream, I started by installing gpspipe.
>>
>> start with two rfcomm listeners in /etc/bluetooth/rfcomm.conf:
>>
>> #
>> # cat /etc/bluetooth/rfcomm.conf
>> #
>> # RFCOMM configuration file.
>> #
>>
>> rfcomm0 {
>> ? ? ? ?listen yes;
>>
>> ? ? ? ?# Bluetooth address of the device
>> ? ? ? ?device hci0;
>>
>> ? ? ? ?# RFCOMM channel for the connection
>> ? ? ? ?channel 1;
>>
>> ? ? ? ?# Description of the connection
>> ? ? ? ?comment "gps repeater channel 1";
>> }
>>
>> rfcomm1 {
>> ? ? ? ?listen yes;
>>
>> ? ? ? ?# Bluetooth address of the device
>> ? ? ? ?device hci0;
>>
>> ? ? ? ?# RFCOMM channel for the connection
>> ? ? ? ?channel 2;
>>
>> ? ? ? ?# Description of the connection
>> ? ? ? ?comment "gps repeater channel 2";
>> }
>>
>> next, in /etc/bluetooth/rfcomm/rfcomm-gps-repeater:
>>
>> #!/bin/sh
>> # use: rfcomm-gps-repeater rfcommx next-port-number
>> # eg: rfcomm-gps-repeater rfcomm0 2 & rfcomm-gps-repeater rfcomm1 1 &
>> while true;
>> ? ? ? ?do
>> ? ? ? ?rfcomm show | grep $1 > /dev/null 2> /dev/null
>> ? ? ? ?if [ "$?" != "1" ] ; then
>> ? ? ? ? ? ? ? ?/usr/bin/sdptool add --channel=$2 SP
>> ? ? ? ? ? ? ? ?/usr/bin/gpspipe -r >/dev/$1;
>> ? ? ? ?fi
>> ? ? ? ?sleep 1;
>> done
>>
>> I run two of these in parallel, pointing each one to the *next* rfcomm
>> channel it should advertise via sdp. This is so the next bluetooth
>> client to scan sdp will find the available repeater.
>>
>> It seems the rfcomm listener is not prepared to serve two ports. The
>> new /etc/bluetooth/rfcomm/rfcomm-listen:
>>
>> #!/bin/sh
>> while $1 -r -f $2 listen $3;do continue;done
>>
>> Finally, execute everything properly in /etc/init.d/S30bluetooth:
>>
>> ...
>> RFCOMM_GPS_REPEATER=/etc/bluetooth/rfcomm/rfcomm-gps-repeater
>> ...
>> ? ? ? ? ? ? ? ?#$RFCOMM_LISTEN $RFCOMM_EXEC $RFCOMM_CONFIG > /dev/null 2>&1 &
>> ? ? ? ? ? ? ? ?$RFCOMM_LISTEN $RFCOMM_EXEC $RFCOMM_CONFIG rfcomm0 > /dev/null 2>&1 &
>> ? ? ? ? ? ? ? ?$RFCOMM_LISTEN $RFCOMM_EXEC $RFCOMM_CONFIG rfcomm1 >
>> /dev/null 2>&1 &
>> ? ? ? ? ? ? ? ?#$RFCOMM_GETTY > /dev/null 2>&1 &
>> ? ? ? ? ? ? ? ?$RFCOMM_GPS_REPEATER rfcomm0 2 > /dev/null 2>&1 &
>> ? ? ? ? ? ? ? ?$RFCOMM_GPS_REPEATER rfcomm1 1 > /dev/null 2>&1 &
>> ...
>>
>> You may have to change hcid.conf to contain "lm accept,master" so the
>> board insists on being the bluetooth master device. I was getting i/o
>> errors before I did that.
>>
>> --
>> Brad Midgley
>>
>

2011-03-03 04:45:15

by Han

[permalink] [raw]
Subject: Re: cannot setup multiple connection using RFCOMM

On Wed, Mar 2, 2011 at 6:31 PM, Brad Midgley <[email protected]> wrote:
> Han
>
>> I have a client / server application running over bluetooth using
>> RFCOMM as transport. ?I am using socket API in C with AF_BLUETOOTH,
>> SOCK_STREAM and BTPROTO_RFCOMM.
>
> rfcomm is the wrong protocol if you want to have multiple connections
> to one server.

Thanks Brad. What should be the right protocol to use?

>
> In case you have no choice, I've appended a very old howto suggesting
> a way to multiplex rfcomm for gps, but it's generally unreliable
> because clients cache sdp records unpredictably.

thanks for the detailed info. It seems that rfcomm.conf has to
include one entry for each connection with different channel? In my
case, I really don't know how many (parallel) client connections in
advance. It's all dynamic.

Han

>
> ================================
>
> Bluetooth & GPS
> I would like to have a multifunction GPS unit... logging, repeating
> gps location data over bluetooth for two clients. gpsd is running and
> receiving from the local gps.
>
> Next, to make it repeat the gps stream, I started by installing gpspipe.
>
> start with two rfcomm listeners in /etc/bluetooth/rfcomm.conf:
>
> #
> # cat /etc/bluetooth/rfcomm.conf
> #
> # RFCOMM configuration file.
> #
>
> rfcomm0 {
> ? ? ? ?listen yes;
>
> ? ? ? ?# Bluetooth address of the device
> ? ? ? ?device hci0;
>
> ? ? ? ?# RFCOMM channel for the connection
> ? ? ? ?channel 1;
>
> ? ? ? ?# Description of the connection
> ? ? ? ?comment "gps repeater channel 1";
> }
>
> rfcomm1 {
> ? ? ? ?listen yes;
>
> ? ? ? ?# Bluetooth address of the device
> ? ? ? ?device hci0;
>
> ? ? ? ?# RFCOMM channel for the connection
> ? ? ? ?channel 2;
>
> ? ? ? ?# Description of the connection
> ? ? ? ?comment "gps repeater channel 2";
> }
>
> next, in /etc/bluetooth/rfcomm/rfcomm-gps-repeater:
>
> #!/bin/sh
> # use: rfcomm-gps-repeater rfcommx next-port-number
> # eg: rfcomm-gps-repeater rfcomm0 2 & rfcomm-gps-repeater rfcomm1 1 &
> while true;
> ? ? ? ?do
> ? ? ? ?rfcomm show | grep $1 > /dev/null 2> /dev/null
> ? ? ? ?if [ "$?" != "1" ] ; then
> ? ? ? ? ? ? ? ?/usr/bin/sdptool add --channel=$2 SP
> ? ? ? ? ? ? ? ?/usr/bin/gpspipe -r >/dev/$1;
> ? ? ? ?fi
> ? ? ? ?sleep 1;
> done
>
> I run two of these in parallel, pointing each one to the *next* rfcomm
> channel it should advertise via sdp. This is so the next bluetooth
> client to scan sdp will find the available repeater.
>
> It seems the rfcomm listener is not prepared to serve two ports. The
> new /etc/bluetooth/rfcomm/rfcomm-listen:
>
> #!/bin/sh
> while $1 -r -f $2 listen $3;do continue;done
>
> Finally, execute everything properly in /etc/init.d/S30bluetooth:
>
> ...
> RFCOMM_GPS_REPEATER=/etc/bluetooth/rfcomm/rfcomm-gps-repeater
> ...
> ? ? ? ? ? ? ? ?#$RFCOMM_LISTEN $RFCOMM_EXEC $RFCOMM_CONFIG > /dev/null 2>&1 &
> ? ? ? ? ? ? ? ?$RFCOMM_LISTEN $RFCOMM_EXEC $RFCOMM_CONFIG rfcomm0 > /dev/null 2>&1 &
> ? ? ? ? ? ? ? ?$RFCOMM_LISTEN $RFCOMM_EXEC $RFCOMM_CONFIG rfcomm1 >
> /dev/null 2>&1 &
> ? ? ? ? ? ? ? ?#$RFCOMM_GETTY > /dev/null 2>&1 &
> ? ? ? ? ? ? ? ?$RFCOMM_GPS_REPEATER rfcomm0 2 > /dev/null 2>&1 &
> ? ? ? ? ? ? ? ?$RFCOMM_GPS_REPEATER rfcomm1 1 > /dev/null 2>&1 &
> ...
>
> You may have to change hcid.conf to contain "lm accept,master" so the
> board insists on being the bluetooth master device. I was getting i/o
> errors before I did that.
>
> --
> Brad Midgley
>

2011-03-03 02:31:41

by Brad Midgley

[permalink] [raw]
Subject: Re: cannot setup multiple connection using RFCOMM

Han

> I have a client / server application running over bluetooth using
> RFCOMM as transport. ?I am using socket API in C with AF_BLUETOOTH,
> SOCK_STREAM and BTPROTO_RFCOMM.

rfcomm is the wrong protocol if you want to have multiple connections
to one server.

In case you have no choice, I've appended a very old howto suggesting
a way to multiplex rfcomm for gps, but it's generally unreliable
because clients cache sdp records unpredictably.

================================

Bluetooth & GPS
I would like to have a multifunction GPS unit... logging, repeating
gps location data over bluetooth for two clients. gpsd is running and
receiving from the local gps.

Next, to make it repeat the gps stream, I started by installing gpspipe.

start with two rfcomm listeners in /etc/bluetooth/rfcomm.conf:

#
# cat /etc/bluetooth/rfcomm.conf
#
# RFCOMM configuration file.
#

rfcomm0 {
listen yes;

# Bluetooth address of the device
device hci0;

# RFCOMM channel for the connection
channel 1;

# Description of the connection
comment "gps repeater channel 1";
}

rfcomm1 {
listen yes;

# Bluetooth address of the device
device hci0;

# RFCOMM channel for the connection
channel 2;

# Description of the connection
comment "gps repeater channel 2";
}

next, in /etc/bluetooth/rfcomm/rfcomm-gps-repeater:

#!/bin/sh
# use: rfcomm-gps-repeater rfcommx next-port-number
# eg: rfcomm-gps-repeater rfcomm0 2 & rfcomm-gps-repeater rfcomm1 1 &
while true;
do
rfcomm show | grep $1 > /dev/null 2> /dev/null
if [ "$?" != "1" ] ; then
/usr/bin/sdptool add --channel=$2 SP
/usr/bin/gpspipe -r >/dev/$1;
fi
sleep 1;
done

I run two of these in parallel, pointing each one to the *next* rfcomm
channel it should advertise via sdp. This is so the next bluetooth
client to scan sdp will find the available repeater.

It seems the rfcomm listener is not prepared to serve two ports. The
new /etc/bluetooth/rfcomm/rfcomm-listen:

#!/bin/sh
while $1 -r -f $2 listen $3;do continue;done

Finally, execute everything properly in /etc/init.d/S30bluetooth:

...
RFCOMM_GPS_REPEATER=/etc/bluetooth/rfcomm/rfcomm-gps-repeater
...
#$RFCOMM_LISTEN $RFCOMM_EXEC $RFCOMM_CONFIG > /dev/null 2>&1 &
$RFCOMM_LISTEN $RFCOMM_EXEC $RFCOMM_CONFIG rfcomm0 > /dev/null 2>&1 &
$RFCOMM_LISTEN $RFCOMM_EXEC $RFCOMM_CONFIG rfcomm1 >
/dev/null 2>&1 &
#$RFCOMM_GETTY > /dev/null 2>&1 &
$RFCOMM_GPS_REPEATER rfcomm0 2 > /dev/null 2>&1 &
$RFCOMM_GPS_REPEATER rfcomm1 1 > /dev/null 2>&1 &
...

You may have to change hcid.conf to contain "lm accept,master" so the
board insists on being the bluetooth master device. I was getting i/o
errors before I did that.

--
Brad Midgley