#!/usr/bin/perl -w

# Das Package BTAccess bietet Routinen, um mit BlueZ und 
# anderen Kommandozeilen Tools 
# ueber Bluetooth zu kommunizieren (Inquiry, SDP, OBEX)

# Copyright (C) 2006-2007 Holger Hammel

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU 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 General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

# GNU License Text: http://www.fsf.org/licensing/licenses/gpl.html



package BTAccess;
 
use strict;

# Inquiry
# ------------------------------------
sub getBTInquiry() {
	return qx(/usr/bin/hcitool inq --flush);
}

# Inquiry und alle Namen holen
# ------------------------------------
sub getBTInquiryNames() {
	return qx(/usr/bin/hcitool scan --flush);
}


# BT-Namen holen
# ------------------------------------
sub getBTName {
	my $adr = shift;	
	my $name = qx(/usr/bin/hcitool scan $adr);
	$name =~ m/$adr([\t ]*)(.*)/i;
	my $ret = "DUMMY";
	if($2) { $ret = $2; }
	return $ret;
}
 
# Major,Minor service und device class 
# nummer holen (aus hcitool inq ergebnis)
# ------------------------------------
# IMPL: TODO
sub getBTDeviceClass {
	my $adr=shift;
	my $inquiry = shift;

	if( $inquiry =~ m/$adr(.+)class:\s0x\w{3}(\w{0,3})/) {
		return $2;	
	}
	return "";
} 
 
 
# schnelle Infos holen
# ------------------------------------
sub getBTInfo {
	my $adr=shift;
	return qx(/usr/bin/hcitool info $adr);
}

# Service Discovery
# ------------------------------------
sub getBTSdp {
	my $adr = shift;
	return qx(/usr/bin/sdptool browse $adr);
}

# Bluetooth Connection aufbauen
# returns: 0 - fehler, 1-ok
# ------------------------------------
sub btConnect {
	my $adr=shift;
	my $ret = qx(/usr/bin/hcitool cc $adr);
	if($ret =~ m/Can\'t/i) {
	 	return 0;
	} else {
		return 1; 
	}
} 

# Bluetooth Connection beenden
# returns: 0 - fehler, 1-ok
# ------------------------------------
sub btDisconnect {
	my $adr=shift;
	my $ret = qx(/usr/bin/hcitool dc $adr);
	if($ret =~ m/Not/i) {
	 	return 0;
	} else {
		return 1; 
	}
} 

# RSSI wert holen
# returns: Wert oder -1000=nicht verbunden, 
# -1001=anderer fehler
# ------------------------------------
sub btRSSI {
	my $adr=shift;
	my $ret = qx(/usr/bin/hcitool rssi $adr);
	if($ret =~ m/Not connected/i) {
	 	return -1000;
	} else {
		if($ret=~ m/RSSI return value: ([\d-]+)/i) {
			return $1; 
		} else {
			return -1001;
		}
	}
} 


# Ret: OBEX Push Channel 
# -----------------------------------------
sub getObexPushChannel {
	my $sdp = shift;
	$sdp =~ /0x1105[\W\w\n]*?Channel:\s(\d{1,2})/g;	
	return $1;
}



# OBEX Push
# ------------------------------------
sub btOBEXPush {
	my $adr=shift;
	my $pushChannel=shift;
	my $localFile=shift;
	my $output = "";
	
	print "> BTAccess::btOBEXPush: obexftp -b $adr -U none -H -B $pushChannel -p $localFile\n";
	$output = qx(obexftp -b $adr -U none -H -B $pushChannel -p $localFile 2>&1);
	
	print "\n*********************************************\n";
	print $output;
	print "\n*********************************************\n";
	if($output =~ m/Sending.*?done/i) {
		return 1;
	}
	else {
		return 0;
	}
	
}






1;