Skip to content

Commit

Permalink
fixing bt browse for win32, updating for syncml
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed Mar 5, 2009
1 parent 8177c89 commit e312cbb
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 26 deletions.
85 changes: 69 additions & 16 deletions obexftp/bt_kit.c
Expand Up @@ -54,6 +54,22 @@
0x00, 0x02, 0xee, 0x00, 0x00, 0x01 }
#define SVC_UUID_PCSUITE ((const uint8_t []) __SVC_UUID_PCSUITE_bytes)

//Nokia SyncML Server UUID 128: 00005601-0000-1000-8000-0002ee000001

/* well known services 0x1000 to 0x12FF */
#define __SVC_UUID_BASE_bytes \
{ 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x10, 0x00, 0x80, 0x00, \
0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }
#define SVC_UUID_BASE ((const uint8_t []) __SVC_UUID_BASE_bytes)

/* 0x0001: server; 0x0002: client; 0x0003: DM server; 0x0004: DM client */
#define __SVC_UUID_SYNCML_bytes \
{ 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x10, 0x00, 0x80, 0x00, \
0x00, 0x02, 0xEE, 0x00, 0x00, 0x02 }
#define SVC_UUID_SYNCML ((const uint8_t []) __SVC_UUID_SYNCML_bytes)


/**
Allocate and setup the network stack.
Expand Down Expand Up @@ -236,39 +252,63 @@ char *btkit_getname(const char *src, const char *addr)
}


// http://msdn2.microsoft.com/en-us/library/aa362914.aspx
int btkit_browse(const char *src, const char *addr, int svclass)
{
unsigned int ret;
int port = 0;
WSAQUERYSET querySet;
char addressAsString[20]; // "(XX:XX:XX:XX:XX:XX)"
DWORD addressSize = sizeof(addressAsString);
snprintf(addressAsString, 20, "(%s)", addr);

/*
if (0 == WSAAddressToString(lpSockaddr, sizeof(bdaddr_t), NULL, addressAsString, &addressSize)) {
printf("search address: %s\n", addressAsString);
if (!addr || strlen(addr) != 17) {
DEBUG(1, "%s: bad address\n", __func__);
return -1;
}
*/

if ((svclass<0x0001 || svclass>0x0004)
&& (svclass<0x1000 || svclass>0x12FF)) {
DEBUG(1, "%s: bad service class\n", __func__);
return -1;
}

GUID baseServiceClassId = {
0x00000000,
0x0000,
0x1000,
{ 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }
}; // Bluetooth_Base_UUID
baseServiceClassId.Data1 = svclass;

GUID syncmlClassId = {
0x00000000,
0x0000,
0x1000,
{ 0x80, 0x00, 0x00, 0x02, 0xEE, 0x00, 0x00, 0x02 }
}; // common UUID for SyncML Client/Server
syncmlClassId.Data1 = svclass;

memset(&querySet, 0, sizeof(querySet));
querySet.dwSize = sizeof(querySet);
querySet.dwNameSpace = NS_BTH;
// querySet.lpServiceClassId = RFCOMM;
if (svclass>=0x0001 && svclass<=0x0004)
querySet.lpServiceClassId = &syncmlClassId;
else
querySet.lpServiceClassId = &baseServiceClassId;
querySet.lpszContext = addressAsString;

HANDLE hLookup;
DWORD flags = LUP_NOCONTAINERS | LUP_FLUSHCACHE | LUP_RETURN_ADDR | LUP_RES_SERVICE
| LUP_RETURN_NAME | LUP_RETURN_TYPE | LUP_RETURN_BLOB;
| LUP_RETURN_NAME;

ret = WSALookupServiceBegin(&querySet, flags, &hLookup);
if (ret != 0) {
DEBUG(2, "%s: WSALookupServiceBegin failed (%d)\n", __func__, WSAGetLastError());
return -1;
}

for (;;) {

BYTE buffer[1000];
BYTE buffer[4000];
DWORD bufferLength = sizeof(buffer);
WSAQUERYSET *pResults = (WSAQUERYSET*)&buffer;

Expand All @@ -278,16 +318,19 @@ int btkit_browse(const char *src, const char *addr, int svclass)
}
if (ret != 0) {
DEBUG(2, "%s: WSALookupServiceNext failed (%d)\n", __func__, WSAGetLastError());
break;
}

DEBUG(3, "%s: Found\t%s\n", __func__, pResults->lpszServiceInstanceName);
port = ((SOCKADDR_BTH*)pResults->lpcsaBuffer->RemoteAddr.lpSockaddr)->port;
DEBUG(3, "%s: Found\t%s\t%li\n", __func__, pResults->lpszServiceInstanceName, port);
}

ret = WSALookupServiceEnd(hLookup);
if (ret != 0) {
DEBUG(2, "%s: WSALookupServiceEnd failed (%d)\n", __func__, WSAGetLastError());
return -1;
}
return 0;
return port;
}


Expand Down Expand Up @@ -476,19 +519,29 @@ int btkit_browse(const char *src, const char *addr, int svclass)
// *res_bdaddr = batostr(&bdswap);
// fprintf(stderr, "Browsing %s ...\n", *res_bdaddr);

/* special case: SyncML */
if (svclass >= 0x0001 && svclass <= 0x0004) {
unsigned short data1 = svclass;
sdp_uuid128_create(&root_uuid, &SVC_UUID_SYNCML);
memcpy(&root_uuid->value.uuid128.data[2], &data1, 2);
res = browse_sdp_uuid(sess, &root_uuid);
sdp_close(sess);
return res;
}

/* determine the service class we're looking for */
if ((svclass != IRMC_SYNC_SVCLASS_ID) &&
(svclass != OBEX_OBJPUSH_SVCLASS_ID) &&
(svclass != OBEX_FILETRANS_SVCLASS_ID)) {
if (svclass < 0x1000 || svclass > 0x12FF) {
svclass = OBEX_FILETRANS_SVCLASS_ID;
/* or OBEX_FILETRANS_PROFILE_ID? */
}

/* prefer PCSUITE over FTP */
if (svclass == OBEX_FILETRANS_SVCLASS_ID) {
sdp_uuid128_create(&root_uuid, &SVC_UUID_PCSUITE);
res = browse_sdp_uuid(sess, &root_uuid);
if (res > 0) return res;
if (res > 0) {
sdp_close(sess);
return res;
}
}

/* browse for the service class */
Expand Down
12 changes: 12 additions & 0 deletions obexftp/bt_kit.h
Expand Up @@ -29,6 +29,18 @@

#ifdef HAVE_BLUETOOTH

/* Service Class UUIDs for bt browse. */
/* Only Service Class UUID-16s are accepted (0x1000-0x12FF). Esp. not Protocol UUIDs. */
/* Also SYNCML UUID-16s (0x0001-0x0004) are translated to correct UUID-128s. */
#define BTKIT_SPP_SERVICE (0x1101) /* aka SerialPortServiceClassID_UUID16 */
#define BTKIT_SYNC_SERVICE (0x1104) /* aka IrMCSyncServiceClassID_UUID16 */
#define BTKIT_PUSH_SERVICE (0x1105) /* aka OBEXObjectPushServiceClassID_UUID16 */
#define BTKIT_FTP_SERVICE (0x1106) /* aka OBEXFileTransferServiceClassID_UUID16 */
#define BTKIT_SYNCML_SERVER (0x0001) /* aka SyncMLServer_UUID */
#define BTKIT_SYNCML_CLIENT (0x0002) /* aka SyncMLClient_UUID */
#define BTKIT_SYNCML_DM_SERVER (0x0003) /* aka SyncMLDMServer_UUID */
#define BTKIT_SYNCML_DM_CLIENT (0x0004) /* aka SyncMLDMClient_UUID */

//#ifndef _WIN32
//#include <sys/socket.h>
//#include <arpa/inet.h>
Expand Down
49 changes: 39 additions & 10 deletions obexftp/bt_kit_test.c
@@ -1,3 +1,24 @@
/**
\file obexftp/bt_kit_test.c
Bluetooth, SDP, HCI kit for Linux, FreeBSD, NetBSD and Win32.
ObexFTP library - language bindings for OBEX file transfer.
Copyright (c) 2007 Christian W. Zuckschwerdt <zany@triq.net>
ObexFTP is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with ObexFTP. If not, see <http://www.gnu.org/>.
*/

/* gcc -Wall -I. -I../includes -DOBEXFTP_DEBUG=3 -DHAVE_BLUETOOTH -o bt_kit_test bt_kit.c bt_kit_test.c */
/* on win32 add: -lws2_32 */

Expand All @@ -11,36 +32,44 @@ int main(int argc, char *argv[])
int ret, i;
char **list, *device;
char *hci = NULL;
int svclass = BTKIT_FTP_SERVICE;

if (argc > 1) {
hci = argv[1];
}
if (argc > 2) {
svclass = atoi(argv[2]);
}

ret = btkit_init();
fprintf(stderr, "Initializing stack\n"); fflush(stderr);
ret = btkit_init(); /* !!! */
if (ret != 0) {
fprintf(stderr, "btkit_init failed (%d)\n", ret);
fprintf(stderr, "btkit_init failed (%d)\n", ret); fflush(stderr);
}


list = btkit_discover(hci);
fprintf(stderr, "Discovering devices\n"); fflush(stderr);
list = btkit_discover(hci); /* !!! */
if (list == NULL) {
fprintf(stderr, "btkit_discover failed\n");
fprintf(stderr, "btkit_discover failed\n"); fflush(stderr);
} else {
for (i=0 ; list[i]; i++) {
fprintf(stderr, "device %d: \"%s\"\n", i, list[i]);
fprintf(stderr, "device %d: \"%s\"\n", i, list[i]); fflush(stderr);

device = btkit_getname(hci, list[i]);
fprintf(stderr, "bt name %d: \"%s\"\n", i, device);
device = btkit_getname(hci, list[i]); /* !!! */
fprintf(stderr, "bt name %d: \"%s\"\n", i, device); fflush(stderr);

// ret = btkit_browse(hci, device, 0x1106 /*OBEX FTP*/);
ret = btkit_browse(hci, list[i], svclass); /* !!! */
fprintf(stderr, "service channel: %i\n", ret); fflush(stderr);
}
free(list);
}


ret = btkit_exit();
fprintf(stderr, "Disposing stack\n"); fflush(stderr);
ret = btkit_exit(); /* !!! */
if (ret != 0) {
fprintf(stderr, "btkit_exit failed (%d)\n", ret);
fprintf(stderr, "btkit_exit failed (%d)\n", ret); fflush(stderr);
}

return 0;
Expand Down

0 comments on commit e312cbb

Please sign in to comment.