dbus.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package DBUS
  2. import (
  3. "fmt"
  4. "strings"
  5. //"strconv"
  6. "github.com/godbus/dbus"
  7. )
  8. type DbusInterface struct {
  9. Dest string
  10. Path dbus.ObjectPath
  11. Iface string
  12. Obj *dbus.Object
  13. SigFuncs map[string]interface{}
  14. }
  15. func NewDbusInterface(conn *dbus.Conn, dest string, path dbus.ObjectPath, iface string) *DbusInterface {
  16. m := &DbusInterface{}
  17. o := conn.Object(dest, path)
  18. m.Obj = o.(*dbus.Object)
  19. m.Dest = dest
  20. m.Path = path
  21. m.SigFuncs = make(map[string]interface{})
  22. if len(iface) > 2 {
  23. m.Iface = iface
  24. }
  25. return m
  26. }
  27. func (self *DbusInterface) Method(name string, args ...interface{}) *dbus.Call {
  28. var method string
  29. if self.Iface != "" {
  30. method = fmt.Sprintf("%s.%s.%s", self.Dest, self.Iface, name)
  31. } else {
  32. method = fmt.Sprintf("%s.%s", self.Dest, name)
  33. }
  34. if args != nil {
  35. return self.Obj.Call(method, 0, args...)
  36. } else {
  37. return self.Obj.Call(method, 0)
  38. }
  39. }
  40. func (self *DbusInterface) Get(thecall *dbus.Call, retvalues ...interface{}) error {
  41. if len(thecall.Body) == 0 {
  42. return nil
  43. }
  44. err := thecall.Store(retvalues...)
  45. if err != nil {
  46. panic(fmt.Sprintf("Failed: %s,%s", err, thecall.Method))
  47. }
  48. return err
  49. }
  50. func (self *DbusInterface) EnableSignal(signame string) {
  51. iface := self.Dest
  52. if self.Iface != "" {
  53. iface = iface + "." + self.Iface
  54. }
  55. self.Obj.AddMatchSignal(iface, signame)
  56. }
  57. func (self *DbusInterface) HandleSignal(sig *dbus.Signal) {
  58. iface := self.Dest
  59. if self.Iface != "" {
  60. iface = iface + "." + self.Iface
  61. }
  62. if strings.HasPrefix(sig.Name, iface) {
  63. func_name := strings.Replace(sig.Name, iface, "", -1)[1:]
  64. for k, v := range self.SigFuncs {
  65. if k == func_name {
  66. v.(func([]interface{}))(sig.Body)
  67. break
  68. }
  69. }
  70. }
  71. }
  72. type DBusInterface interface {
  73. WifiStrength() int
  74. IsWifiConnectedNow() bool
  75. }
  76. type DBus struct {
  77. Conn *dbus.Conn
  78. Daemon *DbusInterface
  79. Wifi *DbusInterface
  80. }
  81. func NewDBus() *DBus {
  82. d := &DBus{}
  83. return d
  84. }
  85. func (self *DBus) Init() {
  86. //conn_option := dbus.WithSignalHandler(self)
  87. conn, err := dbus.SystemBus()
  88. //conn,err := dbus.SystemBusPrivate(conn_option)
  89. if err != nil {
  90. panic(fmt.Sprintf("Failed to connect to system bus:", err))
  91. }
  92. self.Conn = conn
  93. self.Daemon = NewDbusInterface(conn, "org.wicd.daemon", "/org/wicd/daemon", "")
  94. self.Wifi = NewDbusInterface(conn, "org.wicd.daemon", "/org/wicd/daemon/wireless", "wireless")
  95. }
  96. func (self *DBus) WifiStrength() int {
  97. var fast bool
  98. var iwconfig string
  99. var sig_display_type int
  100. var strength int
  101. self.Daemon.Get(self.Daemon.Method("NeedsExternalCalls"), &fast)
  102. if fast == false {
  103. self.Wifi.Get(self.Wifi.Method("GetIwconfig"), &iwconfig)
  104. } else {
  105. iwconfig = ""
  106. }
  107. self.Daemon.Get(self.Daemon.Method("GetSignalDisplayType"), &sig_display_type)
  108. if sig_display_type == 0 {
  109. self.Wifi.Get(self.Wifi.Method("GetCurrentSignalStrength", iwconfig), &strength)
  110. } else {
  111. self.Wifi.Get(self.Wifi.Method("GetCurrentDBMStrength", iwconfig), &strength)
  112. }
  113. return strength
  114. }
  115. func (self *DBus) check_for_wireless(iwconfig string, wireless_ip string) bool {
  116. var network string
  117. var sig_display_type int
  118. var strength int
  119. if wireless_ip == "" {
  120. return false
  121. }
  122. self.Wifi.Get(self.Wifi.Method("GetCurrentNetwork", iwconfig), &network)
  123. self.Daemon.Get(self.Daemon.Method("GetSignalDisplayType"), &sig_display_type)
  124. if sig_display_type == 0 {
  125. self.Wifi.Get(self.Wifi.Method("GetCurrentSignalStrength", iwconfig), &strength)
  126. } else {
  127. self.Wifi.Get(self.Wifi.Method("GetCurrentDBMStrength", iwconfig), &strength)
  128. }
  129. if strength == 0 {
  130. return false
  131. }
  132. strength_str := ""
  133. self.Daemon.Get(self.Daemon.Method("FormatSignalForPrinting", strength), &strength_str)
  134. return true
  135. }
  136. func (self *DBus) GetWifiIP() string {
  137. var wireless_ip string
  138. if self.Wifi != nil {
  139. self.Wifi.Get(self.Wifi.Method("GetWirelessIP", ""), &wireless_ip)
  140. }
  141. return wireless_ip
  142. }
  143. func (self *DBus) IsWifiConnectedNow() bool {
  144. var fast bool
  145. var iwconfig string
  146. var wireless_connecting bool
  147. var wireless_ip string
  148. self.Wifi.Get(self.Wifi.Method("CheckIfWirelessConnecting"), &wireless_connecting)
  149. self.Daemon.Get(self.Daemon.Method("NeedsExternalCalls"), &fast)
  150. if wireless_connecting == true {
  151. return false
  152. } else {
  153. if fast == false {
  154. self.Wifi.Get(self.Wifi.Method("GetIwconfig"), &iwconfig)
  155. } else {
  156. iwconfig = ""
  157. }
  158. self.Wifi.Get(self.Wifi.Method("GetWirelessIP", iwconfig), &wireless_ip)
  159. if self.check_for_wireless(iwconfig, wireless_ip) == true {
  160. return true
  161. } else {
  162. return false
  163. }
  164. }
  165. }
  166. func (self *DBus) ListenSignal() {
  167. c := make(chan *dbus.Signal, 10)
  168. self.Conn.Signal(c)
  169. for v := range c {
  170. //fmt.Printf("%+v %#v\n",v,v)
  171. //fmt.Printf("body len:%d \n\n",len(v.Body))
  172. self.Wifi.HandleSignal(v)
  173. self.Daemon.HandleSignal(v)
  174. }
  175. }
  176. var DBusHandler *DBus //global
  177. func init() {
  178. if DBusHandler == nil {
  179. DBusHandler = NewDBus()
  180. DBusHandler.Init()
  181. go DBusHandler.ListenSignal()
  182. }
  183. }