Index: source/ariba/utility/bootstrap/modules/bluetoothsdp/BluetoothSdp.cpp
===================================================================
--- source/ariba/utility/bootstrap/modules/bluetoothsdp/BluetoothSdp.cpp	(revision 4970)
+++ source/ariba/utility/bootstrap/modules/bluetoothsdp/BluetoothSdp.cpp	(revision 5316)
@@ -41,19 +41,19 @@
 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
 
-// Attribute descriptors for SDP
-// base was chosen randomly
-#define SDP_SPOVNET_BASE 			0x4000
-#define SDP_ATTR_SPOVNET_NAME		0x0000 + SDP_SPOVNET_BASE
-#define SDP_ATTR_SPOVNET_INFO1		0x0001 + SDP_SPOVNET_BASE
-#define SDP_ATTR_SPOVNET_INFO2		0x0002 + SDP_SPOVNET_BASE
-#define SDP_ATTR_SPOVNET_INFO3		0x0003 + SDP_SPOVNET_BASE
-
-// The SpoVNet unique identifier, this should be the same for all SpoVNet implementations
-const uint8_t svc_uuid_int[] = {0x59, 0x29, 0x24, 0x34, 0x69, 0x42, 0x11, 0xde, 0x94,
-	0x3e, 0x00, 0x21, 0x5d, 0xb4, 0xd8, 0x54};
-
-const char *service_name = "SpoVNet";
-const char *svc_dsc = "Spontaneous Virtual Network";
-const char *service_prov = "ITM Uni Karlsruhe";
+	// Attribute descriptors for SDP
+	// base was chosen randomly
+	#define SDP_SPOVNET_BASE 			0x4000
+	#define SDP_ATTR_SPOVNET_NAME		0x0000 + SDP_SPOVNET_BASE
+	#define SDP_ATTR_SPOVNET_INFO1		0x0001 + SDP_SPOVNET_BASE
+	#define SDP_ATTR_SPOVNET_INFO2		0x0002 + SDP_SPOVNET_BASE
+	#define SDP_ATTR_SPOVNET_INFO3		0x0003 + SDP_SPOVNET_BASE
+
+	// The SpoVNet unique identifier, this should be the same for all SpoVNet implementations
+	const uint8_t svc_uuid_int[] = {0x59, 0x29, 0x24, 0x34, 0x69, 0x42, 0x11, 0xde, 0x94,
+		0x3e, 0x00, 0x21, 0x5d, 0xb4, 0xd8, 0x54};
+
+	const char *service_name = "SpoVNet";
+	const char *svc_dsc = "www.ariba-underlay.org";
+	const char *service_prov = "ITM Uni Karlsruhe";
 
 #endif // HAVE_BLUETOOTH_BLUETOOTH_H
@@ -66,4 +66,5 @@
 BluetoothSdp::BluetoothSdp(BootstrapInformationCallback* _callback) :
 	BootstrapModule(_callback), scan_timer_(io_service_) {
+	srand( time(NULL) );
 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
 
@@ -88,5 +89,5 @@
 bool BluetoothSdp::isFunctional() {
 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
-	return true; // Not tested yet :)
+	return true;
 #else
 	return false;
@@ -118,4 +119,7 @@
 	t_.join();
 
+	if(sdp_session_ != NULL)
+		sdp_close(sdp_session_);
+
 #endif // HAVE_BLUETOOTH_BLUETOOTH_H
 }
@@ -130,5 +134,5 @@
 	 */
 
-	logging_info("Registering SDP service");
+	logging_debug("registering SDP service");
 
 	uint8_t rfcomm_channel = channel_;
@@ -142,7 +146,4 @@
 	sdp_session_ = 0;
 
-	bdaddr_t bdaddr_any = (bdaddr_t) { {0, 0, 0, 0, 0, 0}};
-	bdaddr_t bdaddr_local = (bdaddr_t) { {0, 0, 0, 0xff, 0xff, 0xff}};
-
 	// prepare the info attribute buffers
 	//string namebuf, info1buf, info2buf, info3buf;
@@ -155,19 +156,7 @@
 
 	if((namelen > 256) || (info1len > 256) || (info2len > 256) || (info3len > 256)) {
-		logging_error("String Argument too long, max size is 256!");
+		logging_error("string argument too long, max size is 256!");
 		return;
 	}
-
-	// we need to save the string len for sdp; do we?
-	/*
-	namebuf = (char)namelen;
-	namebuf.append(name);
-	info1buf = (char)info1len;
-	info1buf.append(info1);
-	info2buf = (char)info2len;
-	info2buf.append(info2);
-	info3buf = (char)info3len;
-	info3buf.append(info3);
-	*/
 
 	// set the general service ID
@@ -222,12 +211,19 @@
 			info3.data());
 
-	// connect to the local SDP server, register the service record,
-	// and disconnect
-	sdp_session_ = sdp_connect(&bdaddr_any, &bdaddr_local, SDP_RETRY_IF_BUSY);
-
-	if (sdp_session_ == 0) {
-		logging_error( "Something is wrong with your SDP server, nothing registered" );
+	// connect to the local SDP server, register the service record
+	if( sdp_session_ == NULL ){
+		sdp_session_ = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, SDP_RETRY_IF_BUSY);
+	}
+
+	if (sdp_session_ == NULL) {
+		logging_error( "something is wrong with your SDP server, nothing registered" );
 	} else {
-		sdp_record_register(sdp_session_, &record, 0);
+		int ret = sdp_record_register(sdp_session_, &record, 0);
+
+		if(ret < 0){
+			logging_error("failed registering sdp record");
+		}else{
+			logging_debug("sdp record registered using session " << sdp_session_);
+		}
 	}
 
@@ -247,5 +243,5 @@
 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
 
-	logging_info("Unregistering SDP service");
+	logging_debug("unregistering SDP service");
 	sdp_close(sdp_session_);
 
@@ -256,12 +252,9 @@
 
 void BluetoothSdp::bt_scan() {
-
-
 	/*
 	 * Scans for other bluetooth devices and starts a SDP search on them.
-	 * Repeats 20 seconds after the end of the scan.
-	 */
-
-	logging_info("Scanning for peers");
+	 */
+
+	logging_debug("scanning for peers");
 
 	inquiry_info *ii = NULL;
@@ -276,6 +269,6 @@
 	sock = hci_open_dev(dev_id);
 	if (dev_id < 0 || sock < 0) {
-		logging_error("opening socket");
-		exit(1);
+		logging_error("opening socket for device " << dev_id << " failed. can not scan for peers");
+		return;
 	}
 
@@ -287,19 +280,10 @@
 	num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
 	if (num_rsp < 0)
-		logging_error("hci_inquiry");
+		logging_error("hci_inquiry failed with " << num_rsp);
 
 	for (i = 0; i < num_rsp; i++) {
-		/*
-		 ba2str(&(ii + i)->bdaddr, addr);
-		 memset(name, 0, sizeof(name));
-		 if (hci_read_remote_name(sock, &(ii + i)->bdaddr, sizeof(name),
-		 name, 0) < 0)
-		 strcpy(name, "[unknown]");
-		 printf("%s  %s\n", addr, name);
-		 */
 
 		address = (ii + i)->bdaddr;
-
-		logging_info("Found peer " << ba2string(&address) << ", querying SDP.")
+		logging_debug("found peer " << ba2string(&address) << ", querying SDP.")
 
 		// TODO: sdp_search can be very slow, fork it!
@@ -310,7 +294,8 @@
 	close(sock);
 
-	logging_info("Next scan in 20 seconds");
-
-	scan_timer_.expires_from_now(boost::posix_time::seconds(20));
+	int nextscan = (rand() % 40) + 5;
+	logging_debug("Next sdp scan in " << nextscan << " seconds");
+
+	scan_timer_.expires_from_now(boost::posix_time::seconds(nextscan));
 	scan_timer_.async_wait(boost::bind(&BluetoothSdp::bt_scan, this));
 }
@@ -326,8 +311,7 @@
 	uuid_t svc_uuid;
 	sdp_list_t *response_list, *search_list, *attrid_list;
-	sdp_session_t *session = 0;
+	sdp_session_t *session = NULL;
 	uint32_t range = 0x0000ffff;
 	uint8_t port = 0;
-	bdaddr_t any_addr = (bdaddr_t) { {0, 0, 0, 0, 0, 0}};
 
 	// prepare the buffers for the attributes
@@ -335,8 +319,9 @@
 
 	// connect to the SDP server running on the remote machine
-	session = sdp_connect(&any_addr, &target, 0);
-
-	if (session == 0) {
-		logging_error("Failed to connect to SDP server at " << ba2string(&target) <<".");
+	logging_debug("querying services from bt device " << ba2string(&target));
+	session = sdp_connect(BDADDR_ANY, &target, 0);
+
+	if (session == NULL) {
+		logging_error("failed to connect to SDP server at " << ba2string(&target) <<".");
 		return;
 	}
@@ -356,5 +341,5 @@
 
 		// go through each of the service records
-		for (; r; r = r->next) {
+		for ( ; r != NULL; r = r->next) {
 			sdp_record_t *rec = (sdp_record_t*) r->data;
 
@@ -368,7 +353,7 @@
 
 				sdp_get_string_attr(rec, SDP_ATTR_SPOVNET_NAME, (char*)&name, 256);
-				sdp_get_string_attr(rec, SDP_ATTR_SPOVNET_INFO1, (char*)&name, 256);
-				sdp_get_string_attr(rec, SDP_ATTR_SPOVNET_INFO2, (char*)&name, 256);
-				sdp_get_string_attr(rec, SDP_ATTR_SPOVNET_INFO3, (char*)&name, 256);
+				sdp_get_string_attr(rec, SDP_ATTR_SPOVNET_INFO1, (char*)&info1, 256);
+				sdp_get_string_attr(rec, SDP_ATTR_SPOVNET_INFO2, (char*)&info2, 256);
+				sdp_get_string_attr(rec, SDP_ATTR_SPOVNET_INFO3, (char*)&info3, 256);
 
 				logging_info("Remote peer name is: " << name);
@@ -379,14 +364,15 @@
 				// Callback
 				callback->onBootstrapServiceFound(name, info1, info2, info3);
-
 			}
 			sdp_record_free(rec);
 		}
-	}
+	} else {
+		logging_error("sdp_service_search_attr_req failed with timeout");
+	}
+
 	sdp_list_free(response_list, 0);
 	sdp_list_free(search_list, 0);
 	sdp_list_free(attrid_list, 0);
 	sdp_close(session);
-
 }
 
@@ -395,5 +381,6 @@
 	 * Returns a string holding the bt adress in human readable form.
 	 */
-	char *str;
+	char addr[32] = { 0 };
+	char str[32] = { 0 };
 	ba2str(ba, str);
 	string result = str;
