Return-Path: Message-ID: Date: Sat, 31 Mar 2007 11:43:28 +0100 From: "Meenakshi Seeballack" To: bluez-devel@lists.sourceforge.net MIME-Version: 1.0 Subject: [Bluez-devel] BlueZ, testing socket Reply-To: BlueZ development List-Id: BlueZ development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: multipart/mixed; boundary="===============0821571106==" Sender: bluez-devel-bounces@lists.sourceforge.net Errors-To: bluez-devel-bounces@lists.sourceforge.net --===============0821571106== Content-Type: multipart/alternative; boundary="----=_Part_58092_15864964.1175337808135" ------=_Part_58092_15864964.1175337808135 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* * l2cap_listen(l2cap_channel) * * 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. * * Use this channel as a listening post. This will * result in calls to: * * proto->newconn(upper, laddr, raddr) * * for incoming connections matching the psm and local * address of the channel * * The upper layer should create and return a new channel. * * 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) { struct sockaddr_l2 addr; struct l2cap_options opts; int sk; // create socket if ((sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) < 0) return -1; /* open connection to remote device */ memset(&addr, 0, sizeof(addr)); addr.l2_family = AF_BLUETOOTH; bacpy(&addr.l2_bdaddr, bdaddr); /* update source address of socket */ addr.l2_psm = htobs(psm); // associate local address with socket if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) { perror("ERROR: Could not bind socket"); close(sk); return -1; } setsockopt(sk, SOL_L2CAP, L2CAP_LM, &lm, sizeof(lm)); memset(&opts, 0, sizeof(opts)); opts.imtu = HIDP_DEFAULT_MTU; opts.omtu = HIDP_DEFAULT_MTU; opts.flush_to = 0xffff; setsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &opts, sizeof(opts)); if (listen(sk, backlog) < 0) { close(sk); return -1; } return sk; } /* * l2cap_connect_req() * Establishes connection with specific socket * Request to establish connection from remote * communication partner. * Such a socket has to have been previously created * by an application with the command listen. */ static int l2cap_connect(bdaddr_t *src, bdaddr_t *dst, unsigned short psm) { struct sockaddr_l2 addr; struct l2cap_options opts; int sk; // socket allocation if ((sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) < 0) perror("ERROR: Could not create socket"); return -1; memset(&addr, 0, sizeof(addr)); addr.l2_family = AF_BLUETOOTH; bacpy(&addr.l2_bdaddr, src); /*update source address of socket */ if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) { close(sk); return -1; } memset(&opts, 0, sizeof(opts)); opts.imtu = HIDP_DEFAULT_MTU; opts.omtu = HIDP_DEFAULT_MTU; opts.flush_to = 0xffff; setsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &opts, sizeof(opts)); memset(&addr, 0, sizeof(addr)); addr.l2_family = AF_BLUETOOTH; bacpy(&addr.l2_bdaddr, dst); addr.l2_psm = htobs(psm); if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) { close(sk); return -1; } return sk; } /* * l2cap_accept() * Permits incoming connection * Function switches from BT_LISTEN to BT_CONNECTED */ static int l2cap_accept(int sk, bdaddr_t *bdaddr) { struct sockaddr_l2 addr; socklen_t addrlen; int nsk; memset(&addr, 0, sizeof(addr)); addrlen = sizeof(addr); // accept one connection if ((nsk = accept(sk, (struct sockaddr *) &addr, &addrlen)) < 0) return -1; if (bdaddr) bacpy(bdaddr, &addr.l2_bdaddr); return nsk; } ------=_Part_58092_15864964.1175337808135 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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>
#include <bluetooth/hci_lib.h>
#include <bluetooth/l2cap.h>
#include <bluetooth/sdp.h>
#include <bluetooth/hidp.h>

/*
 * l2cap_listen(l2cap_channel)
 *
 * 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.
 *
 *    Use this channel as a listening post. This will
 *    result in calls to:
 *
 *        proto->newconn(upper, laddr, raddr)
 *
 *    for incoming connections matching the psm and local
 *    address of the channel
 *
 *    The upper layer should create and return a new channel.
 *
 *    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)
{
    struct sockaddr_l2 addr;
    struct l2cap_options opts;
    int sk;
   
    // create socket
    if ((sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) < 0)
        return -1;

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

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

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

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

    return sk;
}

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

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

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

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

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

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

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

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

    return sk;
}

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

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

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

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

    return nsk;
}

------=_Part_58092_15864964.1175337808135-- --===============0821571106== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ------------------------------------------------------------------------- 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 --===============0821571106== 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 --===============0821571106==--