Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 3 Oct 2009 14:06:10 +0000
From:      Masoom Shaikh <masoom.shaikh@gmail.com>
To:        freebsd-bluetooth@freebsd.org
Subject:   remote_name_request, using libbluetooth
Message-ID:  <b10011eb0910030706i381052felb65318164195d80d@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
Hi,

today i spent ages hacking hccontrol to do something similar it does when
invoked as below

hccontrol remote_name_request <BD_ADDR>

somehow i couldn't succeed ;-(
what really i want is to search for devices and request their names
i have a ruby script which does the same by using hccontrol and manipulating
it console output
but am interested in C version.

pasted below is full source.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <bluetooth.h>

/*
 * removes duplicate entries from result and returns the new size
 * free()'s the original array, result is calloc()ed
 * TODO: implement in a better way
 */
int do_uniq( const int size, struct bt_devinquiry** result)
{
    struct bt_devinquiry* newResult = (struct bt_devinquiry*)calloc( size,
sizeof(struct bt_devinquiry));

    struct bt_devinquiry* srcCurr = *result;
    struct bt_devinquiry* dstCurr = newResult;

    int count = 0;
    int index = 0;
    for ( ; index < size; ++index)
    {
        int j = 0;
        int found = 0;
        while ( j < count)
        {
            if ( bdaddr_same( &( newResult[j++].bdaddr), &(
srcCurr->bdaddr)))
            {
                found = 1;
                break;
            }
        }

        if ( !found)
        {
            *dstCurr = *srcCurr;
            ++dstCurr;
            ++count;
        }
        ++srcCurr;
    }
    free(*result);
    *result = newResult;
    return count;
}

int main( int argc, char* argv[])
{
    /* search devices */
    struct bt_devinquiry* result = 0;
    int num = bt_devinquiry( 0, 0, 0, &result);
    if ( num <= 0)
    {
        if ( h_errno)
            herror( "no devices found");
        else
            printf( "no devices found\n");
        return num;
    }
    /* remove duplicate entries */
    num = do_uniq( num, &result);
    printf( "%d device(s) found\n", num);

    /* try to query device's name */
    int s = bt_devopen( "ubt0hci");
    if ( s == -1)
    {
        if ( h_errno)
            herror( "bt_devopen error\n");
        else
            printf( "bt_devopen error\n");
        return -1;
    }
    int i = 0;
    for ( ; i < num; ++i)
    {
        struct bt_devreq request;
        memset( &request, 0, sizeof(request));
        request.opcode = NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
NG_HCI_OCF_REMOTE_NAME_REQ);
        request.event = NG_HCI_EVENT_REMOTE_NAME_REQ_COMPL;

        ng_hci_remote_name_req_cp cp;
        memset(&cp, 0, sizeof(cp));
        bdaddr_copy( &cp.bdaddr, &result->bdaddr);
        cp.page_scan_rep_mode = NG_HCI_SCAN_REP_MODE0;
        cp.page_scan_mode = NG_HCI_MANDATORY_PAGE_SCAN_MODE;
        request.cparam = (void*)&cp;
        request.clen = sizeof(cp);

        char buffer[512];
        memset( buffer, 0, 512);
        request.rparam = (void*)buffer;
        request.rlen = 512;

        int status = bt_devreq( s, &request, 0);
        if ( status == 0)
        {
            ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t*)buffer;
            ng_hci_remote_name_req_compl_ep *ep =
(ng_hci_remote_name_req_compl_ep*)(e + 1);
            printf( "status: %d\n", ep->status);
            printf( "name: %s\n", ep->name);
        }
        else if (status == -1)
        {
            if ( h_errno)
                herror( "bt_devreq error\n");
            else
                printf( "bt_devreq error\n");
        }
        else
        {
            printf("bt_devreq unknown return value\n");
        }
    }
    bt_devclose(s);
    return 0;
}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?b10011eb0910030706i381052felb65318164195d80d>