get_bluetooth_connection 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/sh
  2. #Barry Kauler GPL 2010.
  3. #Argument 1: bluetooth service name:
  4. # DUN - Dial-up Networking
  5. # SP - Serial Port
  6. # HF - HF Voice Gateway
  7. # HS - HS Voice Gateway
  8. # OPUSH - OBEX Object Push
  9. # FTP - OBEX File Transfer
  10. # OBEX-SyncML Client
  11. # OBEX IrMC Sync Server
  12. #Optional argument: device RFCOMM*, the current device, in case multiple instances found.
  13. which rfcomm >/dev/null || exit 1
  14. which sdptool >/dev/null || exit 1
  15. BTSVCNAME="$1"
  16. CURRENTDEV="$2"
  17. SVCRFCOMMDEVICES=""
  18. #Determine whether a bluetooth modem is available. However you do that.
  19. LOCALDEVS="`hcitool dev 3>&1 >/dev/null 2>&3 | tr -s ' ' | tr '\n' ' '`" #edit?
  20. echo "get_bluetooth_connection ($PPID): `date | tr -s ' ' | cut -f 4 -d ' '` - LOCALDEVS: $LOCALDEVS" >> /tmp/udevtrace-modem.log #DEBUG
  21. for ONELOCALDEV in $LOCALDEVS;do
  22. #Scan for devices within range...
  23. REMOTEBTADDRS="`hcitool -i $ONELOCALDEV scan 3>&1 >/dev/null 2>&3 | grep -o '..:..:..:..:..:..'`" #ex: xx:xx:xx:xx:xx:xx iPAQ PocketPC
  24. echo "get_bluetooth_connection ($PPID): `date | tr -s ' ' | cut -f 4 -d ' '` - REMOTEBTADDRS: $REMOTEBTADDRS" >> /tmp/udevtrace-modem.log #DEBUG
  25. for ONEREMBTADDR in $REMOTEBTADDRS;do
  26. SVCCHANNEL="`sdptool search $ONEREMBTADDR $BTSVCNAME 3>&1 >/dev/null 2>&3 | sed -n -e 's/^ Channel: \([0-9]*\).*/\1/p' | head -n 1`"
  27. echo "get_bluetooth_connection ($PPID): `date | tr -s ' ' | cut -f 4 -d ' '` - SVCCHANNEL: $SVCCHANNEL" >> /tmp/udevtrace-modem.log #DEBUG
  28. if [ "$SVCCHANNEL" != "" ];then
  29. RFCOMMDEVINFO="`rfcomm -a | grep "$ONEREMBTADDR" | grep " channel $SVCCHANNEL " | cut -f 1 -d :`"
  30. echo "get_bluetooth_connection ($PPID): `date | tr -s ' ' | cut -f 4 -d ' '` - RFCOMMDEVINFO: $RFCOMMDEVINFO" >> /tmp/udevtrace-modem.log #DEBUG
  31. if [ "$RFCOMMDEVINFO" = "" ];then
  32. RFCOMMNUM=0
  33. RFCOMMNUMS="`rfcomm -a | sed -e 's/^rfcomm\([0-9][0-9]*\).*/\1/'`"
  34. echo "get_bluetooth_connection ($PPID): `date | tr -s ' ' | cut -f 4 -d ' '` - RFCOMMNUMS: $RFCOMMNUMS" >> /tmp/udevtrace-modem.log #DEBUG
  35. #Stubbed out here. Comment out the echo and exit lines to allow debugging.
  36. echo "get_bluetooth_connection ($PPID): Exit stub." >> /tmp/udevtrace-modem.log #DEBUG
  37. exit 1
  38. #End of stub code.
  39. if [ "$RFCOMMNUMS" != ""];then
  40. for ONERFCOMMNUM in $RFCOMMNUMS;do
  41. while [ $RFCOMMNUM -lt $ONERFCOMMNUM ];do
  42. pup_event_backend_modprobe_protect --bluetooth_rfcomm_number=$RFCOMMNUM && break
  43. RFCOMMNUM=`expr $RFCOMMNUM + 1`
  44. done
  45. [ $RFCOMMNUM -lt $ONERFCOMMNUM ] && break
  46. RFCOMMNUM=`expr $RFCOMMNUM + 1`
  47. done
  48. fi
  49. if [ "$RFCOMMNUMS" = "" ] || [ $RFCOMMNUM -gt $ONERFCOMMNUM ];then
  50. while ! pup_event_backend_modprobe_protect --bluetooth_rfcomm_number=$RFCOMMNUM;do
  51. RFCOMMNUM=`expr $RFCOMMNUM + 1`
  52. [ $RFCOMMNUM -gt 10 ] && exit 1
  53. done
  54. fi
  55. [ ! -e /dev/rfcomm$RFCOMMNUM ] \
  56. && mknod /dev/rfcomm$RFCOMMNUM c 216 $RFCOMMNUM
  57. rfcomm bind $RFCOMMNUM $ONEREMBTADDR $SVCCHANNEL
  58. RFCOMMDEVINFO="`rfcomm $RFCOMMNUM | grep 'clean' | cut -f 1 -d :`"
  59. echo -e "get_bluetooth_connection ($PPID): `date | tr -s ' ' | cut -f 4 -d ' '` - RFCOMMNUM: $RFCOMMNUM ONEREMBTADDR: $ONEREMBTADDR SVCCHANNEL: $SVCCHANNEL : $ RFCOMMDEVINFO:\n $RFCOMMDEVINFO" >> /tmp/udevtrace-modem.log #DEBUG
  60. [ "$RFCOMMDEVINFO" != "" ] \
  61. && SVCRFCOMMDEVICES="$SVCRFCOMMDEVICES$RFCOMMDEVINFO "
  62. else
  63. [ "`echo "$RFCOMMDEVINFO" | grep "$ONELOCALDEV"`" = "" ] && continue
  64. SVCRFCOMMDEVICES="$SVCRFCOMMDEVICES$RFCOMMDEVINFO "
  65. fi
  66. fi
  67. done
  68. done
  69. echo $SVCRFCOMMDEVICES
  70. exit 1 #DEBUG always fail, for now