Index: source/ariba/utility/bootstrap/modules/bluetoothsdp/BluetoothSdp.cpp
===================================================================
--- source/ariba/utility/bootstrap/modules/bluetoothsdp/BluetoothSdp.cpp	(revision 4840)
+++ source/ariba/utility/bootstrap/modules/bluetoothsdp/BluetoothSdp.cpp	(revision 4870)
@@ -39,38 +39,364 @@
 #include "BluetoothSdp.h"
 
+#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";
+
+#endif // HAVE_BLUETOOTH_BLUETOOTH_H
+
+
 namespace ariba {
 namespace utility {
-
-use_logging_cpp(BluetoothSdp);
-
-BluetoothSdp::BluetoothSdp(BootstrapInformationCallback* _callback) : BootstrapModule(_callback) {
-}
-
-BluetoothSdp::~BluetoothSdp(){
-}
-
-string BluetoothSdp::getName(){
+use_logging_cpp(BluetoothSdp)
+;
+
+//TODO: figure out this compiler flag stuff
+
+BluetoothSdp::BluetoothSdp(BootstrapInformationCallback* _callback) :
+	BootstrapModule(_callback), scan_timer_(io_service_) {
+#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
+
+	// TODO: think about this!
+	channel_ = 7;
+	// TODO: what about the callback?
+
+#endif // HAVE_BLUETOOTH_BLUETOOTH_H
+}
+
+BluetoothSdp::~BluetoothSdp() {
+}
+
+string BluetoothSdp::getName() {
 	return "BluetoothSdp";
 }
 
-string BluetoothSdp::getInformation(){
+string BluetoothSdp::getInformation() {
 	return "bootstrap module based on bluetooth service discovery protocol";
 }
 
-bool BluetoothSdp::isFunctional(){
+bool BluetoothSdp::isFunctional() {
+#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
+	return false; // Not tested yet :)
+#else
 	return false;
-}
-
-void BluetoothSdp::start(){
-}
-
-void BluetoothSdp::stop(){
-}
-
-void BluetoothSdp::publishService(string name, string info1, string info2, string info3){
-}
-
-void BluetoothSdp::revokeService(string name){
-}
-
-}} //namespace ariba, utility
+#endif
+}
+
+void BluetoothSdp::start() {
+#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
+
+	/*
+	 * Initializes and forks the scanner.
+	 */
+
+	io_service_.post(boost::bind(&BluetoothSdp::bt_scan, this));
+	t_ = boost::thread(boost::bind(&boost::asio::io_service::run, &io_service_));
+
+#endif // HAVE_BLUETOOTH_BLUETOOTH_H
+}
+
+void BluetoothSdp::stop() {
+#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
+
+	/*
+	 * Stops the scanner.
+	 */
+
+	// not sure if this is thread safe
+	io_service_.stop();
+	t_.join();
+
+#endif // HAVE_BLUETOOTH_BLUETOOTH_H
+}
+
+void BluetoothSdp::publishService(string name, string info1, string info2,
+		string info3) {
+#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
+
+	/*
+	 * Publishes an SpoVNet SDP Service and
+	 * adds the arguments as info attributes.
+	 */
+
+	logging_info("Registering SDP service.\n");
+
+	uint8_t rfcomm_channel = channel_;
+
+	uuid_t root_uuid, l2cap_uuid, rfcomm_uuid, svc_uuid, svc_class_uuid;
+	sdp_list_t *l2cap_list = 0, *rfcomm_list = 0, *root_list = 0, *proto_list =
+	0, *access_proto_list = 0, *svc_class_list = 0, *profile_list = 0;
+	sdp_data_t *channel = 0;
+	sdp_profile_desc_t profile;
+	sdp_record_t record = {0};
+	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
+	namelen = (uint8_t)name.length();
+	info1len = (uint8_t)info1.length();
+	info2len = (uint8_t)info2.length();
+	info3len = (uint8_t)info3.length();
+
+	if((namelen > 256) || (info1len > 256) || (info2len > 256) || (info3len > 256)) {
+		logging_error("String Argument too long, max size is 256!");
+		return;
+	}
+
+	// we need to save the string len for sdp
+	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
+	sdp_uuid128_create(&svc_uuid, &svc_uuid_int);
+	sdp_set_service_id(&record, svc_uuid);
+
+	// set the service class
+	sdp_uuid16_create(&svc_class_uuid, SERIAL_PORT_SVCLASS_ID);
+	svc_class_list = sdp_list_append(0, &svc_class_uuid);
+	sdp_set_service_classes(&record, svc_class_list);
+
+	// set the Bluetooth profile information
+	sdp_uuid16_create(&profile.uuid, SERIAL_PORT_PROFILE_ID);
+	profile.version = 0x0100;
+	profile_list = sdp_list_append(0, &profile);
+	sdp_set_profile_descs(&record, profile_list);
+
+	// make the service record publicly browsable
+	sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
+	root_list = sdp_list_append(0, &root_uuid);
+	sdp_set_browse_groups(&record, root_list);
+
+	// set l2cap informatiint argc, char* argv[]on
+	sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
+	l2cap_list = sdp_list_append(0, &l2cap_uuid);
+	proto_list = sdp_list_append(0, l2cap_list);
+
+	// register the RFCOMM channel for RFCOMM sockets
+	sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
+	channel = sdp_data_alloc(SDP_UINT8, &rfcomm_channel);
+	rfcomm_list = sdp_list_append(0, &rfcomm_uuid);
+	sdp_list_append(rfcomm_list, channel);
+	sdp_list_append(proto_list, rfcomm_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);
+
+
+	// add the info attributes
+	uint8_t namelen, info1len, info2len, info3len;
+	string namebuf, info1buf, info2buf, info3buf;
+
+
+	sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_NAME, SDP_TEXT_STR32,
+			namebuf.data());
+
+	info1len = info1.length();
+	info1buf.append((const char*) &info1len, 4);
+	info1buf.append(info1);
+	sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_INFO1, SDP_TEXT_STR32,
+			info1buf.data());
+
+	info2len = info2.length();
+	info2buf.append((const char*) &info2len, 4);
+	info2buf.append(info2);
+	sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_INFO2, SDP_TEXT_STR32,
+			info2buf.data());
+
+	info3len = info3.length();
+	info3buf.append((const char*) &info3len, 4);
+	info3buf.append(info3);
+	sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_INFO3, SDP_TEXT_STR32,
+			info3buf.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) {
+		std::cout
+		<< "Something is wrong with your SDP server, nothing registered.";
+	} else {
+		sdp_record_register(sdp_session_, &record, 0);
+	}
+
+	// cleanup
+	sdp_data_free(channel);
+	sdp_list_free(l2cap_list, 0);
+	sdp_list_free(rfcomm_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);
+
+#endif // HAVE_BLUETOOTH_BLUETOOTH_H
+}
+
+void BluetoothSdp::revokeService(string name) {
+#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
+
+	logging_info("Unregistering SDP service.\n");
+	sdp_close(sdp_session_);
+
+#endif // HAVE_BLUETOOTH_BLUETOOTH_H
+}
+
+#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
+
+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.\n");
+
+	inquiry_info *ii = NULL;
+	int max_rsp, num_rsp;
+	int dev_id, sock, len, flags;
+	int i;
+
+	/*
+	 char addr[19] = { 0 };
+	 char name[248] = { 0 };
+	 */
+	bdaddr_t address;
+	uint8_t channel;
+
+	dev_id = hci_get_route(NULL);
+	sock = hci_open_dev(dev_id);
+	if (dev_id < 0 || sock < 0) {
+		perror("opening socket");
+		exit(1);
+	}
+
+	len = 8;
+	max_rsp = 255;
+	flags = IREQ_CACHE_FLUSH;
+	ii = (inquiry_info*) malloc(max_rsp * sizeof(inquiry_info));
+
+	num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
+	if (num_rsp < 0)
+	perror("hci_inquiry");
+
+	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;
+		// TODO: sdp_search can be very slow, fork it!
+		sdp_search(address);
+	}
+
+	free(ii);
+	close(sock);
+
+	logging_info("Next scan in 20 seconds.\n");
+
+	scan_timer_.expires_from_now(boost::posix_time::seconds(20));
+	scan_timer_.async_wait(boost::bind(&BluetoothSdp::bt_scan, this));
+
+
+}
+
+void BluetoothSdp::sdp_search(bdaddr_t target) {
+
+	/*
+	 * Searches target for SDP records with the SpoVnet uuid
+	 * and extracts its info attributes.
+	 */
+
+	int status;
+	uuid_t svc_uuid;
+	sdp_list_t *response_list, *search_list, *attrid_list;
+	sdp_session_t *session = 0;
+	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
+	string name, info1, info2, info3;
+	name.reserve(2**32);
+
+	// connect to the SDP server running on the remote machine
+	session = sdp_connect(&any_addr, &target, 0);
+
+	if (session == 0) {
+		// TODO: put the remote's address here
+		logging_error("Failed to connect to remote SDP server.");
+		return;
+	}
+
+	sdp_uuid128_create(&svc_uuid, &svc_uuid_int);
+	search_list = sdp_list_append(0, &svc_uuid);
+	attrid_list = sdp_list_append(0, &range);
+
+	// get a list of service records that have UUID uuid_
+	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 *proto_list = NULL;
+		sdp_list_t *r = response_list;
+
+		// go through each of the service records
+		for (; r; r = r->next) {
+			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) {
+
+				// get the RFCOMM port number
+				port = sdp_get_proto_port(proto_list, RFCOMM_UUID);
+
+				sdp_list_free(proto_list, 0);
+
+				//TODO: callback here + extract info's and stuff
+			}
+			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);
+
+}
+
+#endif // HAVE_BLUETOOTH_BLUETOOTH_H
+
+}
+} //namespace ariba, utility
