Update BT code to advertise and search sdp records and use psm reported to work with new Palm

code.  Log btaddr after converting from name.
This commit is contained in:
ehouse 2007-11-05 03:38:00 +00:00
parent de640cda98
commit 3060888264
2 changed files with 151 additions and 10 deletions

View file

@ -1,6 +1,7 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; compile-command: "make MEMDEBUG=TRUE";-*- */
/*
* Copyright 2006 by Eric House (xwords@eehouse.org). All rights reserved.
* Copyright 2006-2007 by Eric House (xwords@eehouse.org). All rights
* reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -19,12 +20,19 @@
#ifdef XWFEATURE_BLUETOOTH
/*
http://www.btessentials.com/examples/ is good for some of this stuff.
Copyright allows free use.
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/l2cap.h>
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
#include "linuxbt.h"
#include "comms.h"
@ -45,6 +53,7 @@ typedef struct LinBtStuff {
int listener; /* socket */
XP_U16 nSocks;
XP_Bool threadDie;
sdp_session_t* session;
} master;
} u;
@ -108,27 +117,86 @@ lbt_make( MPFORMAL XP_Bool amMaster )
return btStuff;
} /* lbt_make */
static struct sockaddr_l2*
getL2Addr( const CommsAddrRec const* addrP, struct sockaddr_l2* const saddr )
{
LOG_FUNC();
struct sockaddr_l2* result = NULL;
uint8_t svc_uuid_int[] = XW_BT_UUID;
int status;
bdaddr_t target;
uuid_t svc_uuid;
sdp_list_t *response_list, *search_list, *attrid_list;
sdp_session_t *session = 0;
uint32_t range = 0x0000ffff;
XP_MEMCPY( &target, &addrP->u.bt.btAddr, sizeof(target) );
// connect to the SDP server running on the remote machine
session = sdp_connect( BDADDR_ANY, &target, 0 );
if ( NULL == session ) {
XP_LOGF( "%s: sdp_connect->%s", __FUNCTION__, strerror(errno) );
} else {
sdp_uuid128_create( &svc_uuid, &svc_uuid_int );
search_list = sdp_list_append( 0, &svc_uuid );
attrid_list = sdp_list_append( 0, &range );
response_list = NULL;
status = sdp_service_search_attr_req( session, search_list,
SDP_ATTR_REQ_RANGE, attrid_list,
&response_list );
if( status == 0 ) {
sdp_list_t *r;
// go through each of the service records
for ( r = response_list;; r; r = r->next ) {
sdp_list_t *proto_list = NULL;
sdp_record_t *rec = (sdp_record_t*) r->data;
// get a list of the protocol sequences
if( sdp_get_access_protos( rec, &proto_list ) == 0 ) {
unsigned short psm = sdp_get_proto_port( proto_list,
L2CAP_UUID );
sdp_list_free( proto_list, 0 );
saddr->l2_family = AF_BLUETOOTH;
saddr->l2_psm = htobs( psm );
XP_MEMCPY( &saddr->l2_bdaddr, &addrP->u.bt.btAddr,
sizeof(saddr->l2_bdaddr) );
result = saddr;
}
sdp_record_free( rec );
}
}
sdp_list_free( response_list, 0 );
sdp_list_free( search_list, 0 );
sdp_list_free( attrid_list, 0 );
sdp_close( session );
}
return result;
} /* getL2Addr */
static void
lbt_connectSocket( LinBtStuff* btStuff, const CommsAddrRec* addrP )
{
struct sockaddr_l2 saddr;
int sock;
XP_MEMSET( &saddr, 0, sizeof(saddr) );
// allocate a socket
sock = socket( AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP );
if ( sock < 0 ) {
XP_LOGF( "%s: socket->%s", __FUNCTION__, strerror(errno) );
} else {
struct sockaddr_l2 saddr;
XP_MEMSET( &saddr, 0, sizeof(saddr) );
if ( (NULL != getL2Addr( addrP, &saddr ) )
// set the connection parameters (who to connect to)
saddr.l2_family = AF_BLUETOOTH;
saddr.l2_psm = htobs( XW_PSM ); /* need to get this psm via uuid lookup */
XP_MEMCPY( &saddr.l2_bdaddr, &addrP->u.bt.btAddr,
sizeof(saddr.l2_bdaddr) );
// connect to server
if ( 0 == connect( sock, (struct sockaddr *)&saddr, sizeof(saddr) ) ) {
&& (0 == connect( sock, (struct sockaddr *)&saddr, sizeof(saddr) )) ) {
CommonGlobals* globals = btStuff->globals;
(*globals->socketChanged)( globals->socketChangedClosure,
-1, sock );
@ -168,6 +236,67 @@ lbt_accept( int listener, void* ctxt )
return success;
} /* lbt_accept */
static void
lbt_register( LinBtStuff* btStuff, const struct sockaddr_l2* const saddr )
{
LOG_FUNC();
if ( NULL == btStuff->u.master.session ) {
uint8_t svc_uuid_int[] = XW_BT_UUID;
const char *service_name = XW_BT_NAME;
const char *svc_dsc = "An experimental plumbing router";
const char *service_prov = "Roto-Rooter";
uuid_t l2cap_uuid, svc_uuid;
sdp_list_t *l2cap_list = 0,
*root_list = 0,
*proto_list = 0,
*access_proto_list = 0,
*svc_class_list = 0,
*profile_list = 0;
sdp_data_t *psm = 0;
sdp_record_t record = { 0 };
sdp_session_t *session = NULL;
// set the general service ID
sdp_uuid128_create( &svc_uuid, &svc_uuid_int );
sdp_set_service_id( &record, svc_uuid );
// set l2cap information
sdp_uuid16_create( &l2cap_uuid, L2CAP_UUID );
l2cap_list = sdp_list_append( 0, &l2cap_uuid );
/* from pybluez source */
unsigned short l2cap_psm = saddr->l2_psm;
psm = sdp_data_alloc( SDP_UINT16, &l2cap_psm );
sdp_list_append( l2cap_list, psm );
proto_list = sdp_list_append( 0, l2cap_list );
access_proto_list = sdp_list_append( 0, proto_list );
sdp_set_access_protos( &record, access_proto_list );
// set the name, provider, and description
sdp_set_info_attr(&record, service_name, service_prov, svc_dsc);
// connect to the local SDP server, register the service record,
// and disconnect
session = sdp_connect( BDADDR_ANY, BDADDR_LOCAL, SDP_RETRY_IF_BUSY );
if ( NULL == session ) {
XP_LOGF( "%s: sdp_connect->%s", __FUNCTION__, strerror(errno) );
}
XP_ASSERT( NULL != session );
sdp_record_register( session, &record, 0 );
// cleanup
sdp_data_free( psm );
sdp_list_free( l2cap_list, 0 );
sdp_list_free( root_list, 0 );
sdp_list_free( access_proto_list, 0 );
sdp_list_free( svc_class_list, 0 );
sdp_list_free( profile_list, 0 );
btStuff->u.master.session = session;
}
} /* lbt_register */
static void
lbt_listenerSetup( CommonGlobals* globals )
{
@ -186,6 +315,8 @@ lbt_listenerSetup( CommonGlobals* globals )
listen( listener, MAX_CLIENTS );
lbt_register( btStuff, &saddr );
(*globals->addAcceptor)( listener, lbt_accept, globals );
} /* lbt_listenerSetup */
@ -219,6 +350,9 @@ linux_bt_close( CommonGlobals* globals )
if ( !!btStuff ) {
if ( btStuff->amMaster ) {
close( btStuff->u.master.listener );
sdp_close( btStuff->u.master.session );
XP_LOGF( "sleeping for Palm's sake..." );
sleep( 2 ); /* see if this gives palm a chance to not hang */
}
XP_FREE( globals->params->util->mpool, btStuff );
globals->btStuff = NULL;

View file

@ -30,6 +30,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <syslog.h>
#ifdef XWFEATURE_BLUETOOTH
# include <bluetooth/bluetooth.h>
@ -721,6 +722,9 @@ nameToBtAddr( const char* name, bdaddr_t* ba )
XP_MEMCPY( ba, &inqInfo[i].bdaddr, sizeof(*ba) );
success = XP_TRUE;
XP_LOGF( "%s: matched %s", __FUNCTION__, name );
char addrStr[32];
ba2str(ba, addrStr);
XP_LOGF( "bt_addr is %s", addrStr );
break;
}
}
@ -749,6 +753,9 @@ main( int argc, char** argv )
#endif
XP_LOGF( "main started: pid = %d", getpid() );
#ifdef DEBUG
syslog( LOG_DEBUG, "main started: pid = %d", getpid() );
#endif
#ifdef DEBUG
{