List Info

Thread: BlueZ, testing socket




BlueZ, testing socket
user name
2007-03-31 08:24:43
Hi all,
This code below connects to the server socket and selects the protocol to be used (L2CAP), opens the socket for that protocol, creates a bluetooth address object and sets its port to the PSM and binds the socket to this address, tells the sockets to listen for incoming connections, creates a blank socket and pass it to the listening socket through accept (When this call completes the socket passed in a parameter is now fully connected and can be used to send and receive data)

QUESTION: How do I test this socket works? I want to send a messge like "Hello World";. Please help...anyone

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <malloc.h>
#include <syslog.h>
#include <signal.h >
#include <getopt.h>
#include <sys/poll.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

#include <linux/input.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h&gt;
#include <bluetooth/hci_lib.h>
#include <bluetooth/l2cap.h>
#include <bluetooth/sdp.h&gt;
#include <bluetooth/hidp.h&gt;

/*
&nbsp;* l2cap_listen(l2cap_channel)
&nbsp;*
 * Searches all sockets listening to the L2CAP protocol,
 * Checks for a socket waiting for an incoming connection
 * request.
 * Ensures that there is not already a connection
 * with the source address of the requesting Bluetooth device.
&nbsp;*
 ;* &nbsp;  Use this channel as a listening post. This will
&nbsp;* &nbsp;  result in calls to:
 *
 *&nbsp; &nbsp;  &nbsp;  proto->newconn(upper, laddr, raddr)
&nbsp;*
 * &nbsp;  for incoming connections matching the psm and local
 *&nbsp; &nbsp; address of the channel
&nbsp;*
 ;* &nbsp;  The upper layer should create and return a new channel.
 *
&nbsp;*   ; This channel cannot be used for anything else subsequent to this call
 ;*/
static int l2cap_listen(const bdaddr_t *bdaddr, unsigned short psm, int lm, int backlog)
{
 &nbsp;  struct sockaddr_l2 addr;
&nbsp; &nbsp; struct l2cap_options opts;
&nbsp; &nbsp; int sk;
   ;
 &nbsp;  // create socket
&nbsp; &nbsp; if ((sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) < 0)
 &nbsp;   &nbsp;  return -1;

 ; &nbsp; /* open connection to remote device */
 ; &nbsp; memset(&amp;addr, 0, sizeof(addr));
   ; addr.l2_family = AF_BLUETOOTH;
 &nbsp;  bacpy(&;addr.l2_bdaddr, bdaddr); /* update source address of socket */
 &nbsp;  addr.l2_psm = htobs(psm);
 &nbsp; 
 &nbsp;  // associate local address with socket
&nbsp;   if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
 &nbsp;   &nbsp;  perror(&quot;ERROR: Could not bind socket&quot;);
   ;  &nbsp;  close(sk);
 &nbsp;   &nbsp;  return -1;
   ; }

 &nbsp;  setsockopt(sk, SOL_L2CAP, L2CAP_LM, &lm, sizeof(lm));

 ; &nbsp; memset(&amp;opts, 0, sizeof(opts));
   ; opts.imtu = HIDP_DEFAULT_MTU;
&nbsp; &nbsp; opts.omtu = HIDP_DEFAULT_MTU;
&nbsp; &nbsp; opts.flush_to = 0xffff;
&nbsp; &nbsp;
 &nbsp;  setsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &opts, sizeof(opts));

&nbsp; &nbsp; if (listen(sk, backlog) < 0) {
 &nbsp;   &nbsp;  close(sk);
 &nbsp;   &nbsp;  return -1;
   ; }

 &nbsp;  return sk;
}

/*
 * l2cap_connect_req()
&nbsp;* Establishes connection with specific socket
&nbsp;* Request to establish connection from remote
&nbsp;* communication partner.
&nbsp;* Such a socket has to have been previously created
 * by an application with the command listen.
&nbsp;*/
static int l2cap_connect(bdaddr_t *src, bdaddr_t *dst, unsigned short psm)
{
 &nbsp;  struct sockaddr_l2 addr;
&nbsp; &nbsp; struct l2cap_options opts;
&nbsp; &nbsp; int sk;

 ; &nbsp; // socket allocation
 &nbsp;  if ((sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) < 0)
 &nbsp;   &nbsp;  perror(&quot;ERROR: Could not create socket&quot;);
&nbsp; &nbsp;  &nbsp;  return -1;

 ; &nbsp; memset(&amp;addr, 0, sizeof(addr));
   ; addr.l2_family  = AF_BLUETOOTH;
 &nbsp;  bacpy(&;addr.l2_bdaddr, src); /*update source address of socket */

   ; if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
   ;  &nbsp;  close(sk);
 &nbsp;   &nbsp;  return -1;
   ; }

 &nbsp;  memset(&amp;opts, 0, sizeof(opts));
   ; opts.imtu = HIDP_DEFAULT_MTU;
&nbsp; &nbsp; opts.omtu = HIDP_DEFAULT_MTU;
&nbsp; &nbsp; opts.flush_to = 0xffff;

&nbsp;   setsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &opts, sizeof(opts));

&nbsp; &nbsp; memset(&amp;addr, 0, sizeof(addr));
   ; addr.l2_family  = AF_BLUETOOTH;
 &nbsp;  bacpy(&;addr.l2_bdaddr, dst);
&nbsp; &nbsp; addr.l2_psm = htobs(psm);

 &nbsp;  if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
   ;  &nbsp;  close(sk);
 &nbsp;   &nbsp;  return -1;
   ; }

 &nbsp;  return sk;
}

/*
 * l2cap_accept()
 * Permits incoming connection
 * Function switches from BT_LISTEN to BT_CONNECTED
 */
static int l2cap_accept(int sk, bdaddr_t *bdaddr)
{
 &nbsp;  struct sockaddr_l2 addr;
&nbsp; &nbsp; socklen_t addrlen;
&nbsp;   int nsk;

&nbsp; &nbsp; memset(&amp;addr, 0, sizeof(addr));
   ; addrlen = sizeof(addr);

   ; // accept one connection
 &nbsp;  if ((nsk = accept(sk, (struct sockaddr *) &addr, &addrlen)) < 0)
 ; &nbsp;  &nbsp;  return -1;

 ; &nbsp; if (bdaddr)
&nbsp;    &nbsp;  bacpy(bdaddr, &addr.l2_bdaddr);

 &nbsp;  return nsk;
}
Re: BlueZ, testing socket
country flaguser name
Germany
2007-03-31 08:32:05
Hi Meenakshi,

> This code below connects to the server socket and
selects the protocol
> to be used (L2CAP), opens the socket for that protocol,
creates a
> bluetooth address object and sets its port to the PSM
and binds the
> socket to this address, tells the sockets to listen for
incoming
> connections, creates a blank socket and pass it to the
listening
> socket through accept (When this call completes the
socket passed in a
> parameter is now fully connected and can be used to
send and receive
> data) 
> 
> QUESTION: How do I test this socket works? I want to
send a messge
> like "Hello World". Please help...anyone

you should have a look at l2test.c and rctest.c from
bluez-utils.
However the simple answer is to use read() and write() on
that specific
socket descriptor.

Regards

Marcel



------------------------------------------------------------
-------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the
chance to share your
opinions on IT & business topics through brief
surveys-and earn cash
http://www.techsay.com/default.
php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Bluez-users mailing list
Bluez-userslists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-users


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )