#!/usr/bin/wish
#
# api: tcl
# type: application
# title: bluedial
# description: mobile phone bluetooth GPRS dialup with DUN/PPP
# config:
#   <var name="dev_hci" value="hci0" description="default bluetooth host adapter" />
#   <var name="dev_rfcomm" value="rfcomm7" title="RFCOMM device" description="bluetooth RFCOMM interface to aquire" />
#   <var name="mobile" value="00:00:00:00:00:00" title="phone bthw adr" description="bluetooth hardware address of communication device (mobile)" />
# depends: tk
# version: 0.3
# author: milki
# url: http://freshmeat.net/p/bluedial
# license: PD
#
#
# This tool allows to connect a mobile phone using bluetooth and
# DUN (dial-up networking) as serial port for PPP network access.
#
# Usually you can run this application just by double clicking. If
# it was freshly downloaded, you have to make it executable however.
# Right click the file > permissions > exectuable (for all users),
# or run "chmod a+x bluedial.tcl". Alternatively you can "open with"
# the "wish" program.
#
#
# Some basic settings can be configured on top of this script. Other
# settings can be made within the application.
# This tool handles temporary dialups, but can also set system-wide
# configs and install some base packages (for incomplete bluetooth
# setups).
#
# It was initially written for Ubuntu/Debian systems, but supposed to
# work on other Linux setups, too. It's using packagekit install when
# apt-get is not available. As long as application paths are somewhat
# sane, it should operate correctly with the bluetooth stack & tools.
#
#
# This application is released into the Public Domain. (That means
# no restrictions on use, modification, reuse, distribution, etc.)
# http://freshmeat.net/p/bluedial
#



# base application settings / standard configuration
set dev_hci "hci0"         ;# which bluetooth host controller to query
set dev_rfcomm "rfcomm7"   ;# used RFCOMM /device file name (set to a conflict-free default here)
set mobile "00:00:00:00:00:00"   ;# bluetooth hardware address of mobile phone
set rfchannel "3"          ;# used DUN profile BT channel, will be overriden/autodetected by sdptool
set dialcmd "wvdial bluedial"    ;# dialup app (you can use "exec gnome-ppp &" instead)



# other variables
set names(none) 00:00:00:00:00:00
set hwadr(00:00:00:00:00:00) none
global env
if {![info exists env(HOME)]} { set HOME_CONFIG "/tmp/" } elseif {![info exists env(XDG_CONFIG)]} { set HOME_CONFIG "$env(HOME)/.config" } else { set HOME_CONFIG "$env(HOME)/$env(XDG_CONFIG)" }

# needs tk 8.5
if {$tcl_version < 8.5} {
  if {![file exists /usr/bin/wish8.5]} {
    exec apturl apt:tk8.5
  }
  if {$argc=="0"} {
    exec wish8.5 ./bluedial.tcl 1_no_loop
  }
}



 # set window defaults
 wm title . "Bluetooth mobile dialup (GPRS/EDGE) via DUN"
 option add *tearOff 0

 # add menu entries
 . configure -menu [menu .menu -bd 1]
 .menu add cascade -menu [menu .menu.b -bd 1] -label "Bluetooth"
 .menu add cascade -menu [menu .menu.m -bd 1] -label "Mobile"
 .menu add cascade -menu [menu .menu.p -bd 1] -label "PPP dialup"
 .menu add cascade -menu [menu .menu.s -bd 1] -label "Setup/Fixes"
 .menu add cascade -menu [menu .menu.h -bd 1] -label "Help"
 .menu.b add cascade -label "use hardware adapter" -menu [menu .menu.hw -bd 0]
 .menu.b add cascade -label "pairing (optional)" -menu [menu .menu.pair -bd 0]
 .menu.b add command -label "Exit" -command {exit} -background gray
 .menu.m add command -label "Select device..." -command {select_device}
 .menu.m add command -label "Bind/Connect for DUN" -command {connect} -background yellow
 .menu.m add command -label "Release /dev/$dev_rfcomm" -command {exec gksudo rfcomm release $dev_rfcomm ; print "\nReleased.\n"}
 .menu.m add separator
 .menu.m add cascade -label "Other Services" -menu [menu .menu.sdp -bd 1]
 .menu.p add command -label "call NetworkManager" -command {exec nm-connection-editor&}
 .menu.p add command -label "call Gnome-PPP" -command {exec gnome-ppp &}
 .menu.p add command -label "call KPPP" -command {exec kppp &}
 .menu.p add command -label "call GPPPON" -command {exec gpppon &}
 .menu.p add cascade -label "WVDIAL" -menu [menu .menu.wvdial -bd 1] -background green
 .menu.p add cascade -label "pon" -menu [menu .menu.pon -bd 1]
 .menu.p add separator
 .menu.p add command -label "disconnect PPP" -command {pexec poff -a ; rfcomm release $dev_rfcomm} -background gray
 .menu.s add command -label "Generic system check..." -command "generic_system_check"
 .menu.s add separator
 .menu.s add command -label "list bluetooth devices in range" -command "pexec hcitool scan"
 .menu.s add command -label "scan for DUN devices" -command "pexec sdptool search DUN"
 .menu.s add command -label "get info on selected device" -command "supexec hcitool info"
 .menu.s add command -label "hci0 capabilities" -command {pexec hciconfig -a $dev_hci}
 .menu.s add command -label "commencted rfcomm channels" -command "pexec rfcomm"
 .menu.s add separator
 .menu.s add command -label "install bluetooth base" -command "pkg_install bluetooth"
 .menu.s add command -label "install bluez-utils" -command "pkg_install bluez-utils"
 .menu.s add command -label "install gnome-bluetooth" -command "pkg_install bluez-gnome gnome-bluetooth"
 .menu.s add command -label "install ppp" -command "pkg_install ppp"
 .menu.s add command -label "install wvdial" -command "pkg_install wvdial"
 .menu.s add cascade -label "setup wvdial providers" -menu [menu .menu.wvsetup] -background gray
 .menu.s add separator
 .menu.s add command -label "load kernel drivers" -command "supexec modprobe bluetooth ; supexec modprobe btusb ; supexec modprobe l2cap ; supexec modprobe rfcomm"
 .menu.s add command -label "/etc/init.d/bluetooth restart" -command "supexec /etc/init.d/bluetooth restart"
 .menu.s add command -label "/etc/init.d/bluez-utils restart" -command "supexec /etc/init.d/bluez-utils restart"
 .menu.s add command -label "bluetooth applet" -command "exec bluetooth-applet &"
 .menu.s add command -label "bluetooth setup wizard" -command "exec bluetooth-wizard &"
 .menu.s add separator
 .menu.s add command -label "Bluetooth PIN utility fix" -command "exec gksudo -- passkey-agent --default /usr/bin/bluez-pin &"
 .menu.s add command -label "Bluetooth PIN config file" -command {print "\n\nEdit the 'passkey' line...\n" ; exec gksudo gedit /etc/bluetooth/hcid.conf &}
 .menu.s add command -label "Dialup privileges for user $env(USER)" -command "supexec adduser $env(USER) dip ; supexec adduser $env(USER) dialout"
 .menu.s add cascade -label "save current DUN connection" -menu [menu .menu.p.save -bd 1]
 # cu -l /dev/rfcomm0 -s 57600
 .menu.h add command -label "README" -command {readme}
 .menu.h add command -label "https://help.ubuntu.com/community/CategoryBluetooth" -command {exec firefox https://help.ubuntu.com/community/CategoryBluetooth &}
 .menu.h add command -label "http://wiki.ubuntuusers.de/Bluetooth/Mobile" -command {exec firefox http://wiki.ubuntuusers.de/Bluetooth/Mobile &}
 .menu.h add command -label "http://www.gentoo.org/doc/en/bluetooth-guide.xml" -command {exec firefox http://www.gentoo.org/doc/en/bluetooth-guide.xml &}
 .menu.h add command -label "http://www.spiration.co.uk/post/1307/" -command {exec firefox http://www.spiration.co.uk/post/1307/ &}
 .menu.h add command -label "http://www.linuxdevcenter.com/...-bluetooth.html" -command {exec firefox http://www.linuxdevcenter.com/pub/a/linux/2006/09/21/rediscovering-bluetooth.html &}
 .menu.h add command -label "http://www.free-it.de/archiv/talks/paper-10006/paper.html" -command {exec firefox http://www.free-it.de/archiv/talks/paper-10006/paper.html &}
 .menu.h add separator
 .menu.h add command -label "about" -command {tk_dialog .new About "bluedial                                \nversion 0.3 \n\nPublic Domain" "" 0 "ok"}
 .menu.hw add radiobutton -label "hci0 (default)" -variable dev_hci -value "hci0"
 .menu.hw add command -label "rescan devices" -command "hcitool_dev"
 .menu.pair add command -label "connect" -command "pairing cc"
 .menu.pair add command -label "enable encryption" -command "pairing enc on"
 .menu.pair add command -label "disable encryption" -command "pairing enc off"
 .menu.pair add command -label "change connection key" -command "pairing key"
 .menu.pair add command -label "disconnect" -command "pairing dc"
 .menu.pair add command -label "switch role: master" -command "pairing sr master"
 .menu.pair add command -label "switch role: slave" -command "pairing sr slave"
 .menu.p.save add cascade -label "as system setting into" -menu [menu .menu.p.save.sys]
 .menu.p.save.sys add cascade -label "/etc/bluetooth/rfcomm.conf" -menu [menu .menu.p.save.sys.really]
 .menu.p.save.sys.really add command -label "yes really, save" -command "save_rfcomm_conf"
 .menu.sdp add cascade -label "PAN" -menu [menu .menu.pan -bd 1]
 .menu.sdp add command -label "discover services..." -command { sdp_discover }
 .menu.sdp add command -label "close all rfcomms" -command { rfcomm_close }
 .menu.pan add command -label "install pand" -command {pkg_install pand}
 .menu.pan add command -label "start local server" -command { print "\n-- pand -s -r -NAP -M --\n[exec pand -s -r NAP -M]\n" }
 .menu.pan add command -label "connect to .." -command { print "\n-- pand --service -NAP -b $mobile --\n[exec pand --service NAP -b $mobile]\n" }
 .menu.pan add command -label "pand -Q" -command { exec pand --service NAP -Q }
 .menu.pan add command -label "ifconfig / dhcp" -command { exec ifconfig bnep0 192.168.0.7 netmask 255.255.255.0 up ; exec dhclient bnep0 }
# .menu.wvsetup add command -label "manually configure (wvdialconf)" -command "pexec xterm wvdialconf"
 
 # add window elements 
 grid [ttk::label .devlabel -text "mobile phone:"] -column 0 -row 0
 grid [ttk::combobox .devname -textvariable mobile] -column 1 -row 0
 grid [button .scan -text "scan" -command "select_device" -background orange -bd 1] -column 2 -row 0
 grid [button .connect -text "connect" -command "connect" -background yellow -bd 1] -column 3 -row 0
 grid [button .dial -text "dial" -command "$dialcmd" -background green -bd 1] -column 4 -row 0
 grid [ttk::button .exit -text "exit" -command "exit"] -column 5 -row 0
     
 # input/output window
 grid [text .t -background "dark blue" -foreground white -bd 1 -font "FreeSans 9"] -columnspan 6 -row 1
 .t tag configure command -foreground gray
 .t tag configure bold -font "arial 12 bold" -relief raised

 # special UI
 bind .devname <<ComboboxSelected>> { after 650 {devname_to_hwadr} }





# scans for new devices
# 
proc hcitool_dev {} {
  print "\n-- hcitool dev --\n"
  set hci ""
  foreach e [exec hcitool dev] {
     if {$e == "Devices:"} {
        #
     } elseif {[string length $hci]} {
        print "\t$hci\t$e\n"
        .menu.hw add radiobutton -label "$hci ($e)" -variable dev_hci -value "$hci"
        set hci ""
     } else {
        set hci "$e"
     }
  }
}


#-- query bluetooth environment for available devices
#
proc select_device {} {
   global mobile
   global names
   global .devname

   print "\nscanning for devices...\n>>> hcitool scan\n"
   set devs [exec hcitool scan]
#set devs "scanned...\n    06:05:04:03:02:01  credo2\n    07:08:99:99:99:99  Canon PSC-420\n    aa:bb:cc:dd:ee:ff xxyz_44\n"
   print $devs

   # make select window out of result
   toplevel .selwin
   pack [button .selwin.x -text "$mobile" -command {destroy .selwin}] -in .selwin

   # extract list from exec results
   set n 0
   set RX {(\w+:\w+:\w+:\w+:\w+:\w+)\s+(\w+[^\r\n]*)}
   while {[regexp $RX $devs -> hw name]} {
      set devs [regsub $RX $devs " ////////////////// "]
      set name [regsub -all {[^\w]} $name "_"]

      # add buttons to window
      pack [button .selwin.btn$n -text "$name ($hw)" -command "set mobile $hw ; destroy .selwin" ] -in .selwin

      # add to global names list
      set names($name) $hw
      set hwadr($hw) $name

      # loop limit
      incr n
      if {$n >= 20} { return 0 }
   }

   # add names array (phonename=>01:23:45:67:89:ab) to main combobox
   .devname configure -values [array get names]
}


#-- UI / cleaning
# changes bluetooth device names back into hwaddresses
#
proc devname_to_hwadr {} {
   global .devname
   global names
   global mobile

   if {![regexp {\w+:\w+:\w+:\w+:\w+:\w+} $mobile]} {
      if {[info exists names($mobile)]} {
         set mobile $names($mobile)
      }
##print [arrayval names]
   }
}


#-- unused
#
proc select_dun {} {
   print "\nscanning for devices...\n-- sdptool search DUN --\n"
   set duns [exec sdptool search DUN]
   
   #sudo /usr/bin/rfcomm bind 0 01:23:45:67:89:AB 4 
}


#-- main feature
#
proc connect {} {
   global mobile
   global dev_rfcomm
   global rfchannel
   global .t
      
   if {$mobile != "00:00:00:00:00:00"} {
   
      # search channel
      print "\n>>> sdptool search DUN $mobile\n"
      set res [exec sdptool search DUN $mobile]
      #print $res
      if {[regexp {Channel:\s+([0-9]+)} $res -> rfchannel]} {
         print "found DUN profile on channel $rfchannel\n"
      } else {
         set rfchannel 3
         print "\nWARNING: using predefined channel $rfchannel\n"
      }
      
      # bind
      print "\n>>> rfcomm bind $dev_rfcomm $mobile $rfchannel\n";
      set res [exec gksudo rfcomm bind $dev_rfcomm $mobile $rfchannel]
      print $res
      print "Mobile phone is now bound to "
      .t insert end "/dev/$dev_rfcomm" "bold"
      print "\nYou can start a PPP daemon on there now.\n"
      
      # save current device
      global HOME_CONFIG
      exec echo $mobile > $HOME_CONFIG/bluedial
      
   } else {
      print "\n\nYou must select a mobile first. Use the scan function.\n"
   }
}


#-- add list do sdp submenu
#
proc sdp_discover {} {
   global mobile
   global dev_hci
   global names
   global .menu.sdp
#   .menu.sdp delete 4 end

   # search profiles & channels   
   devname_to_hwadr  
   print "\n-- sdptool browse $mobile --\n"
   set result [exec sdptool browse $mobile]

   # loop through result list
   set service ""
   set channel ""
   foreach line [split $result "\n"] {
      if {$line == "" && $service != "" && $channel != ""} {
         .menu.sdp add command -label "connect $mobile $service RFCOMM ch$channel" -command "rfcomm bind rfcomm$channel $mobile $channel"
         print "profile '$service' found on channel '$channel' on mobile $mobile\n"
         set service ""
         set channel ""
      } elseif {[regexp {Service Name: ([\w ]+)} $line -> s]} {
         set service $s
      } elseif  {[regexp {Channel: (\d+)} $line -> c]} {
         set channel $c
      }
   }
   print "\n"
}



#-- close all rfcomm channels
# 
proc rfcomm_close {} {
   for {set n 0} {$n <= 15} {incr n} {
      if {[file exists /dev/rfcomm$n]} {
         rfcomm release rfcomm$n
      }
   }
}


#-- initiates rfcomm commands
#
proc rfcomm {cmd {arg1 ""} {arg2 ""} {arg3 ""} {arg4 ""} } {
   print "\n>>> rfcomm $cmd $arg1 $arg2 $arg3 $arg4\n" "command"
   print [exec gksudo rfcomm $cmd $arg1 $arg2 $arg3 $arg4]
   print " - /dev/$arg1\n"
}

#-- excutes command, and writes output into text window
#
proc pexec {cmd {arg1 ""} {arg2 ""} {arg3 ""} {arg4 ""} {arg5 ""} {arg6 ""} {arg7 ""}} {
   print "\n>>> $cmd $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 $arg7\n" "command"
   print [exec $cmd $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 $arg7]
   print "\n"
}


#-- gksudo executes
#
proc supexec {cmd {arg1 ""} {arg2 ""} {arg3 ""} {arg4 ""} {arg5 ""} {arg6 ""}} {
   pexec gksudo $cmd $arg1 $arg2 $arg3 $arg4 $arg5 $arg6
}


#-- package install commands
proc pkg_install {pkgs} {
   print "\n>>> installing packages: $pkgs\n" "command"
   foreach p $pkgs {
      if {[file exists /usr/bin/apturl]} {
         exec apturl apt:$p
      } else {
         exec pkcon install $p
      }
   }
}

#-- output text to the main windows text pane
#  
proc print {output {style ""}} {
   global .t
   .t insert end $output $style
   .t see end
}



# save current DUN/mobile configuration into system-wide settings file
# /etc/bluetooth/hcid.conf
#
proc save_rfcomm_conf {} {
   global dev_rfcomm
   global mobile
   global rfchannel
   sudo_write_file "/etc/bluetooth/rfcomm.conf"   "\n\n# written by bluedial.tcl:\n$dev_rfcomm {\n bind yes;\n device $mobile;\n channel $rfchannel;\n #comment ppp;\n}\n"
   #-- following format:--
   #  rfcomm7 {
   #    bind yes;
   #    device 00:00:00:00:00:00;
   #    channel 3;
   #  }
}


#-- bluetooth device pairing
#
proc pairing {cc {arg2 ""}} {
   global mobile
   global dev_hci
   devname_to_hwadr   
   supexec hcitool -i $dev_hci $cc $mobile $arg2
}



#-- write file
proc sudo_write_file {fn text} {
   exec gksudo -- sh -c "echo '$text' >> $fn"
}



#-- run ppp dialer
#
proc wvdial {provider} {
   pexec gksudo wvdial $provider
   # wvdial seems to require sudo for writing to /etc/ppp/pap-secrets
}


#-- appends dialup entries to /etc/wvdial.conf
#
proc wvsetup {provider} {
   global dev_rfcomm
   global mobile

   # defaults   
   set fn "/etc/wvdial.conf"
   set phone {*99***1#}
   set p(DE_blau.de)          "standard internet.eplus.de *99***1# eplus gprs url=http://erm.blau.de/knowledgebase/faces/KnowledgeBase?app=selfService\&domainId=4\&entryId=517"
   set p(DE_o2)               "standard surfo2 *99***1# o2 o2 url=http://www.o2online.de/nw/assets/tabellen/packs/datentarife/popup"
   set p(DE_Vodafone_D2)      "standard web.vodafone.de *99***1# blank blank url=http://www.vodafone.de/hilfe-support/infodoks-jugendschutz-sicherheit-netzabdeckung-kontakt/96991.html"
   set p(DE_Vodafone_Volume)  "standard volume.d2gprs.de *99***1# blank blank url=http://www.vodafone.de/business/tarife-sprache-daten-festnetz-dsl-ausland/101673.html"
   set p(DE_Vodafone_Prepaid) "standard event.vodafone.de *99***1#   "
   set p(DE_eplus)            "standard internet.eplus.de *99***1# eplus internet url=http://www.eplus.de/Unterhaltung_und_Dienste/Surf_und_Mail/Einstellungen/Einstellungen.asp"
   set p(DE_T-Mobile_D1_Flat) "standard internet.t-mobile *99***1# tm tm url=http://www.t-mobile.de/downloads/so_funktioniert/handy_als_netzwerkkarte.pdf"
   set p(DE_T-Mobile_D1_Mail) "standard email.t.mobile *99***1# t-mobile tm"
   set p(DE_Hansenet_Alice)   "hansenet internet.partner1 *99***1# alice alice"
   set p(AT_DREI)             "standard drei.at *99***1#   "
   set p(AT_Telering)         "standard WEB *99# web@telering.at web"
   set p(GB_o2)               "standard mobile.o2.co.uk *99***1# mobileweb password"
   set p(GB_Vodafone)         "standard internet *99***1# web web"
   set p(GB_Vodafone_Prepaid) "standard pp.vodafone.co.uk *99***1# web web"
   set p(GB_Orange)           "standard orangeinternet *99***1# user pass"
   set p(GB_T-Mobile)         "standard general.t-mobile.uk *99***1# user wap"
   set p(GB_Three)            "standard three.co.uk *99***1# guest guest"
   set p(GB_Tesco)            "standard prepay.tesco-mobile.com *99***1# tescowap password"
   set p(GB_Virgin)           "standard goto.virginmobile.uk *99***1# user blank"
   set p(GB_BT-Mobile)        "standard btmobile.bt.com *99***1# bt bt"
   set p(US_Cingular)         "standard WAP.CINGULAR *99***1# WAP@CINGULARGRPS.COM CINGULAR1"
   set p(US_T-Mobile_Zones)   "standard wap.voicestream.com *99***1#   "
   set p(US_T-Mobile_Full)    "standard internet3.voicestream.com *99***1#   "
   set p(CA_Fido)             "standard internet.fido.ca *99***2#   "
   set p(SA_Vodacom)          "standard internet *99***1#   "
   set p(AU_Three)            "standard 3netaccess *99# blank blank url=http://xseries.three.com.au/3/three/MobileBroadband/quickstart/D100%20Wi-Fi%20Router/3L5913%20Quick%20Starter%20Guide%20HIGH%20RES.pdf"
   set p(AU_Telstra)          "standard telstra.internet *99# blank blank url=http://telstramobile.custhelp.com/cgi-bin/telstramobile.cfg/php/enduser/std_adp.php?p_faqid=2855"
   set p(SE_Telia)            "standard online.telia.se *99# blank blank url=http://www.telia.se/privat/kundservice/support/mobilt/installningar_surfport.page"
   set p(SE_Tele2)            "standard isplnk1.swip.net *99***1# gprs internet url=http://www.das-grosse-schwedenforum.de/internet-ohne-festnetzanschluss-t8320-10.html"
   set p(CN_China_Mobile)     "standard cmnet *99# blank blank url=http://www.china-mobile-phones.com/china-mobile-gprs.html"
   set p(_GENERIC_APN_internet)   "standard internet *99# blank blank url=http://www.google.com/search?q=APN+internet+*99#"
   set p(FI_Sonera)           "standard internet *99# blank blank url=http://www.medialab.sonera.fi/demos/streaming/sonera_gprs_apns.html"
   set p(NL_KPN_Mobile)       "standard internet *99# KPN gprs"
   set p(SW_Swisscom)         "standard gprs swisscom.ch *99#   "
   set p(US_ATnT)             "standard public *99#   "
   set p(US_Verizon)          "standard  *99#   "


   # just generate menu
   if {$provider == "-list"} {
      global .menu.wvsetup
      foreach name [lsort [array names p]] {
         .menu.wvsetup add command -label "$name" -command "wvsetup $name"
      }
      .menu.wvsetup add command -label "edit manually.." -command "print {\nLook at the bottom of wvdial.conf...\n} ; exec gksudo gedit /etc/wvdial.conf &" -background red
      .menu.wvsetup add command -label "search other providers..." -command "exec firefox http://www.quickim.com/support/gprs-settings.html &"
      # |http://www.unlocks.co.uk/gprs_settings.php   |http://www.iphoneuserguide.com/apple/2008/06/04/iphone3g/apn-settings-for-iphone-3g/
      return 0
   }
   
   # check if given provider exists
   if {![info exists p($provider)]} {
      error no such provider entry in wvsetup database
   } else {
      # extract params
      set tmtype [lindex $p($provider) 0]
      set apn_gw [lindex $p($provider) 1]
      set number [lindex $p($provider) 2]
      set usernm [lindex $p($provider) 3]
      set passwd [lindex $p($provider) 4]
      set extra1 [lindex $p($provider) 5]
      set extra2 [lindex $p($provider) 6]
      set extra3 [lindex $p($provider) 7]
   }
#   if {["$usernm" = ""]} { set usernm "blank" }
#   if {["$passwd" = ""]} { set passwd "blank" }


   # patch wvdial entry template
   switch $tmtype {

      standard {
         set template "
\[Dialer bluedial\]
Title = \"$provider\"
New PPPD = yes
Modem Type = USB Modem
Baud = 460800
Modem = /dev/$dev_rfcomm
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+CGDCONT=1,\"IP\",\"$apn_gw\"
#-- some examples add ,\"0.0.0.0\",0,0
ISDN = 0
Phone = $phone
#-- try *99# alternatively, or *98*1# even
Password = $passwd
Username = $usernm
# Stupid Mode = no
\[/bluedial\]\n"
      }

      hansenet {
         set template "
\[Dialer bluedial\] 
# Hansenet Alice
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Dial Command = ATD
Modem Type = USB Modem
Baud = 460800
New PPPD = yes
Modem = /dev/rfcomm7
ISDN = 0
Phone = *99***1#
Password = Alice
Username = Alice
\[/bluedial\]\n"
      }

      alternative {
         set template "
\[Dialer bluedial\]
Title = \"$provider\"
New PPPD = yes
Modem Type = USB Modem
Baud = 460800
Modem = /dev/$dev_rfcomm
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+CGDCONT=2,\"IP\",\"$apn_gw\",\"0.0.0.0\",0,0
ISDN = 0
Phone = *99***2#
# $phone
Password = $passwd
Username = $usernm
Stupid Mode = yes
\[/bluedial\]\n"
      }

   }
   #switch


   # output
   print "\n>>> adding provider configuration to $fn" "command"
   print $template


   # read old config file
   set f [open $fn r]
   set configtxt [read $f]
   close $f
   # append/replace our template
   if {[regexp {\[Dialer bluedial} $configtxt]} {
      set configtxt [regsub {\[Dialer\sbluedial\].+\[/bluedial\]\n} $configtxt ""]
   }
   set configtxt "$configtxt\n$template"
   # make target file writeable
   set perm [file attributes $fn -permissions]
   exec gksudo chmod o+w $fn
   # write into temporary file
   set f [open $fn w]
   puts $f $configtxt
   close $f
   # undo  permissions
   exec gksudo chmod $perm $fn


   # update UI
   menu_entries_wvdial
}





#-- outputs a README into main window
#
proc readme {} {
   global .t
   .t insert end {
 ---------------------------------------------------------------------------

 README
 ======

 bluedial is a simple tool for bluetooth network dialups. It connects to
 mobile phones over bluetooth, and establishes a DUN connection for dialup
 via PPP. It's supposed to work with GPRS (and EDGE or even UMTS).

 Basically it is a wrapper around hcitool, sdptool, rfcomm and wvdial, pppd.
 It contains a few other functions, however.

 GETTING STARTED

 1. Use Mobile > Select device first.
    This scans for available modems in range.

 2. If it finds anything, go for Mobile > Connect.
    Now a DUN connection should be established on the device /dev/rfcomm7

 3. With a PPP dialer, a network connection can now be established. You
    cas use the WVDIAL entry for example.
    This needs to be setup once before, however:

 SETUP

 There are couple of configuration featurs in the Setup/Fixes menu. To set
 an wvdial entry, use the according menu entry. There are defaults for
 many providers in different countries.

 If bluetooth functions don't seem to work, or no mobile can be found,
 try following options:
 
 1. load drivers
 2. install bluetooth packages
 3. bluetooth/utils restart
 4. debug options (scan devices in range, etc.)

 USE OF /DEV/RFCOMM7

 The device name /dev/rfcomm7 is used per default by bluedial. It is not
 necessary to call the DUN connection that. You can adapt this name, if
 you want. The dialup scripts are however prepared for this value. On top
 of the bluedial.tcl script you can find the according config variable.
 Rfcomm7 was just meant as reasonably conflict-free default (if for example
 a rfcomm0 might already be in use).
 
 BLUETOOTH ADAPTERS

 "Use hardware adapter" from the bluetooth menu is actually hardly used
 inside bluedial. The bluez-tools automatically select the best host adapter
 for connections anyhow. And few systems will use more than "hci0".

 The pairing feature is also not typically used. If your mobile requests a
 PIN, a system popup will appear. Otherwise use Setup/Fixes>PIN.. to adapt.
 If your phone should ask for a PIN, it is usually just 1234 as set as
 default in /etc/bluetooth/hcid.conf
 If bonding is necessary, you might have to do it from the mobile first.

 PPP DIALUP SYSTEM

 bluedial does not itself perform much dialup configuration, and instead
 relegates to better tools like Gnome-PPP, Wvdial. The default application
 for the [dial] button can be configured as script variable on top of
 bluedial.tcl as well.

 As default, the bluedial setup option is saved as [Dialer bluedial] in
 /etc/wvdial.conf

 OTHER SERVICES

 Besides DUN dialup features, most mobile phones allow access to internal
 phone data, audio functions, etc. Therefore other features can be bound
 with Mobile > Other > Search | Select... on /dev/rfcommX devices.

 Once such a connection is established, you can run Wammu or Gnokii or
 other phone management software. Just supply the /dev/rfcommX device name
 there, so SMS or addressbooks can be accessed over the bluetooth link.

 SAVE DUN CONNECTION

 If you could establish the connection once, use the save feature. The
 parameters will be saved into /etc/bluetooth/rfcomm.conf, and the link
 for /dev/rfcomm7 established on every system boot.
 The phone doesn't dialup just from that bluetooth binding, so it's safe
 to make this setting.

 Running bluedial is then no longer necessary. The wvdial entry can be
 called from other PPP managers instead.

 CONFIG FILE
 
 Bluedial only creates a single configuration file:
   ~/.config/bluedial
 It contains the last connected to bluetooth address. So on a second
 run there's no need to search it again.

 ALTERNATIVES
 
 You might also want to check out "GPRS Easy Connect". It's not developed
 further currently, but still a really cool tool.
 http://www.gprsec.hu/
 It comes with a more exhaustive provider list, for example.
 
 CONTRIBUTE
 
 If you want to contribute another dialup setting, please do. (Albeit a
 Wiki would probably better serve this purpose..)
 Application design tips (yes, it's ugly) are also appreciated.
 <bluedial.15.mario17#spamgourmet.com>
 
}
}



# test various system settings for bluetooth support
# - recommends options from system/fixes menu then
#
proc generic_system_check {} {
   print "\nSystem check\n" command
   
   if {[file exists /usr/bin/hcitool]} {
      print "hcitool			ok\n"
   } else {
      print "hcitool			missing, use Setup>Install bluez-utils\n"
   }
   if {[file exists /usr/bin/sdptool]} {
      print "sdptool			ok\n"
   } else {
      print "sdptool			missing, use Setup>Install bluez-utils\n"
   }
   if {[file exists /usr/bin/rfcomm]} {
      print "rfcomm			ok\n"
   } else {
      print "rfcomm			missing, use Setup>Install bluez-utils\n"
   }

   if {[file exists /etc/init.d/bluetooth]} {
      print "bluetooth base			ok\n"
   } else {
      print "bluetooth base			missing, use Setup>Install bluetooth\n"
   }
   
   if {[file exists /usr/bin/bluetooth-applet]} {
      print "bluez-gnome			ok\n"
   } else {
      print "bluez-gnome			missing, use Setup>Install gnome-bluetooth\n"
   }

   if {[file exists /usr/sbin/pppd]} {
      print "PPP			ok\n"
   } else {
      print "PPP			missing, use Setup>Install PPP\n"
   }

   if {[file exists /usr/bin/wvdial]} {
      print "wvdial			ok\n"
   } else {
      print "wvdial			missing, use Setup>Install PPP\n"
   }

   if {[file exists /etc/bluetooth/hcid.conf]} {
      print "/etc/bluetooth/hcid.conf			exists\n"
   } else {
      print "/etc/bluetooth/hcid.conf			missing, install bluez, then debug\n"
   }
   
   if {[regexp {bluetooth} [read [open /proc/modules r]]]} {
      print "bluetooth kernel module			loaded\n"
   } else {
      print "bluetooth kernel moduele			absent, try Fixes>Load, else investigate your hardware\n"
   }
   
   

}


# run at startup, checks for clean state
#
proc basic_sanity_checks {} {
  global dev_rfcomm
  if {[file exists "/dev/$dev_rfcomm"]} { print "\nNOTE: Currently there is still a device connected on /dev/$dev_rfcomm.\nIf it's stale, use Mobile > Release first.\n" }
}

# read in last configured mobile phone
#
proc read_config_defaults {} {
  global HOME_CONFIG
  global mobile
  if {[file exists $HOME_CONFIG/bluedial]} {
     set mobile [exec cat $HOME_CONFIG/bluedial]
  } 
}


#-- add /etc/wvdial entries (to main menu)
#
proc menu_entries_wvdial {} {
  global .menu.wvdial
  .menu.wvdial delete 0 end
  if {[file exists /etc/wvdial.conf]} {
    set last 0
    foreach e [exec grep Dialer /etc/wvdial.conf] {
       if {$last} {
          set e [regsub {\]} $e ""]
          if {$e != "Default"} {
             .menu.wvdial add command -label "$e" -command "wvdial $e"
          }
          set last 0
       } else {
          set last 1
       }
    }
  }
}

 
#-- add /etc/ppp entries (to main menu)
#
proc menu_entries_pon {} {
  global .menu.pon
  .menu.pon delete 0 end
  if {[file exists /etc/ppp/peers/]} {
    foreach e [exec ls /etc/ppp/peers/] {
       .menu.pon add command -label "$e" -command "pon $e"
    }
  }
}

proc disclaimer {} { 
 print "\nDISCLAIMER: Internet dialup connections on mobile phones almost always incur extra charges.\nCheck contract details of your provider first and use this application AT YOUR OWN RISK.\n" command
}




 #-- some other startup code -----------------------------------------
 # set some defaults
 # extend menus
 #
 basic_sanity_checks 
 read_config_defaults
 menu_entries_wvdial
 menu_entries_pon
 wvsetup -list
 disclaimer
 