bluetooth: fix adpater scan issue

Adjust sync code and add new dbus function DebugInfo().

tower: https://tower.im/projects/3b99b1824fd74228be438acd3189fb7b/todos/6cf807118b7646549c4cf5cb202e57a9/
Change-Id: Ib33c40acc35f196a3c275041c164667f8916156a
This commit is contained in:
Xu Fasheng 2015-03-18 14:55:39 +08:00
parent 58db8b4913
commit fa71b21b9a
3 changed files with 65 additions and 55 deletions

View File

@ -43,7 +43,7 @@ type adapter struct {
func newAdapter(apath dbus.ObjectPath) (a *adapter) {
a = &adapter{Path: apath}
a.bluezAdapter, _ = bluezNewAdapter(apath)
a.connectProeprties() // TODO
a.connectProperties()
a.adddress = a.bluezAdapter.Address.Get()
a.Alias = a.bluezAdapter.Alias.Get()
a.Powered = a.bluezAdapter.Powered.Get()
@ -66,43 +66,43 @@ func (a *adapter) notifyAdapterRemoved() {
dbus.Emit(bluetooth, "AdapterRemoved", marshalJSON(a))
bluetooth.setPropState()
}
func (a *adapter) notifyProeprtiesChanged() {
func (a *adapter) notifyPropertiesChanged() {
logger.Debug("AdapterPropertiesChanged", marshalJSON(a))
dbus.Emit(bluetooth, "AdapterPropertiesChanged", marshalJSON(a))
bluetooth.setPropState()
}
func (a *adapter) connectProeprties() {
func (a *adapter) connectProperties() {
a.bluezAdapter.Alias.ConnectChanged(func() {
a.Alias = a.bluezAdapter.Alias.Get()
a.notifyProeprtiesChanged()
a.notifyPropertiesChanged()
bluetooth.setPropAdapters()
})
a.bluezAdapter.Powered.ConnectChanged(func() {
a.Powered = a.bluezAdapter.Powered.Get()
logger.Infof("adapter powered changed %#v", a)
a.notifyProeprtiesChanged()
a.notifyPropertiesChanged()
bluetooth.setPropAdapters()
})
a.bluezAdapter.Discovering.ConnectChanged(func() {
a.Discovering = a.bluezAdapter.Discovering.Get()
a.notifyProeprtiesChanged()
a.notifyPropertiesChanged()
bluetooth.setPropAdapters()
})
a.bluezAdapter.Discoverable.ConnectChanged(func() {
a.Discoverable = a.bluezAdapter.Discoverable.Get()
a.notifyProeprtiesChanged()
a.notifyPropertiesChanged()
bluetooth.setPropAdapters()
})
a.bluezAdapter.DiscoverableTimeout.ConnectChanged(func() {
a.DiscoverableTimeout = a.bluezAdapter.DiscoverableTimeout.Get()
a.notifyProeprtiesChanged()
a.notifyPropertiesChanged()
bluetooth.setPropAdapters()
})
}
func (b *Bluetooth) addAdapter(apath dbus.ObjectPath) {
if b.isAdapterExists(apath) {
logger.Warning("repeat add adapter:", apath)
logger.Error("repeat add adapter", apath)
return
}
@ -124,25 +124,23 @@ func (b *Bluetooth) addAdapter(apath dbus.ObjectPath) {
func (b *Bluetooth) removeAdapter(apath dbus.ObjectPath) {
i := b.getAdapterIndex(apath)
if i < 0 {
logger.Warning("repeat remove adapter:", apath)
logger.Error("repeat remove adapter", apath)
return
}
b.adaptersLock.Lock()
defer b.adaptersLock.Unlock()
b.doRemoveAdapter(i)
b.setPropAdapters()
}
func (b *Bluetooth) doRemoveAdapter(i int) {
b.adapters[i].notifyAdapterRemoved()
destroyAdapter(b.adapters[i])
copy(b.adapters[i:], b.adapters[i+1:])
b.adapters[len(b.adapters)-1] = nil
b.adapters = b.adapters[:len(b.adapters)-1]
b.setPropAdapters()
}
func (b *Bluetooth) isAdapterExists(apath dbus.ObjectPath) bool {
if b.getAdapterIndex(apath) >= 0 {
return true
}
return false
}
func (b *Bluetooth) getAdapter(apath dbus.ObjectPath) (a *adapter, err error) {
i := b.getAdapterIndex(apath)
if i < 0 {
@ -156,6 +154,12 @@ func (b *Bluetooth) getAdapter(apath dbus.ObjectPath) (a *adapter, err error) {
a = b.adapters[i]
return
}
func (b *Bluetooth) isAdapterExists(apath dbus.ObjectPath) bool {
if b.getAdapterIndex(apath) >= 0 {
return true
}
return false
}
func (b *Bluetooth) getAdapterIndex(apath dbus.ObjectPath) int {
b.adaptersLock.Lock()
defer b.adaptersLock.Unlock()
@ -179,9 +183,13 @@ func (b *Bluetooth) RequestDiscovery(apath dbus.ObjectPath) (err error) {
return
}
err = bluezStartDiscovery(apath)
go func() {
time.Sleep(3 * time.Second) // waiting for adapter ready
err = bluezStartDiscovery(apath)
// adapter is not ready, retry again
if err != nil {
time.Sleep(3 * time.Second)
err = bluezStartDiscovery(apath)
}
if err == nil {
time.Sleep(20 * time.Second)

View File

@ -22,6 +22,7 @@
package bluetooth
import (
"fmt"
"pkg.linuxdeepin.com/lib/dbus"
)
@ -34,13 +35,16 @@ func (b *Bluetooth) OnPropertiesChanged(name string, oldv interface{}) {
logger.Debug("OnPropertiesChanged()", name)
}
// TODO: remove
func (b *Bluetooth) DebugInfo() (info string) {
info = fmt.Sprintf("adapters: %s\ndevices: %s", marshalJSON(b.adapters), marshalJSON(b.devices))
return
}
func (b *Bluetooth) setPropAdapters() {
b.Adapters = marshalJSON(b.adapters)
dbus.NotifyChange(b, "Adapters")
}
// TODO: remove
func (b *Bluetooth) setPropDevices() {
b.Devices = marshalJSON(b.devices)
dbus.NotifyChange(b, "Devices")

View File

@ -163,40 +163,33 @@ func (d *device) fixRssi() {
}
func (b *Bluetooth) addDevice(dpath dbus.ObjectPath, data map[string]dbus.Variant) {
d := newDevice(dpath, data)
if b.isDeviceExists(b.devices[d.AdapterPath], dpath) {
logger.Warning("repeat add device:", dpath)
if b.isDeviceExists(dpath) {
logger.Error("repeat add device", dpath)
return
}
b.devicesLock.Lock()
defer b.devicesLock.Unlock()
d.notifyDeviceAdded()
d := newDevice(dpath, data)
b.devices[d.AdapterPath] = append(b.devices[d.AdapterPath], d)
d.notifyDeviceAdded()
b.setPropDevices()
}
func (b *Bluetooth) removeDevice(dpath dbus.ObjectPath) {
// find adapter of the device
for apath, devices := range b.devices {
if b.isDeviceExists(devices, dpath) {
d, _ := b.getDevice(dpath)
d.notifyDeviceRemoved()
b.devices[apath] = b.doRemoveDevice(devices, dpath)
b.setPropDevices()
return
}
}
}
func (b *Bluetooth) doRemoveDevice(devices []*device, dpath dbus.ObjectPath) []*device {
i := b.getDeviceIndex(devices, dpath)
apath, i := b.getDeviceIndex(dpath)
if i < 0 {
logger.Warning("repeat remove device:", dpath)
return devices
logger.Error("repeat remove device", dpath)
return
}
b.devicesLock.Lock()
defer b.devicesLock.Unlock()
b.devices[apath] = b.doRemoveDevice(b.devices[apath], i)
b.setPropDevices()
return
}
func (b *Bluetooth) doRemoveDevice(devices []*device, i int) []*device {
devices[i].notifyDeviceRemoved()
destroyDevice(devices[i])
copy(devices[i:], devices[i+1:])
devices[len(devices)-1] = nil
@ -204,31 +197,36 @@ func (b *Bluetooth) doRemoveDevice(devices []*device, dpath dbus.ObjectPath) []*
return devices
}
func (b *Bluetooth) getDevice(dpath dbus.ObjectPath) (d *device, err error) {
for _, devices := range b.devices {
if i := b.getDeviceIndex(devices, dpath); i >= 0 {
d = devices[i]
return
}
apath, i := b.getDeviceIndex(dpath)
if i < 0 {
err = fmt.Errorf("device not found %s", dpath)
logger.Error(err)
return
}
err = fmt.Errorf("device not exists %s", dpath)
logger.Error(err)
b.devicesLock.Lock()
defer b.devicesLock.Unlock()
d = b.devices[apath][i]
return
}
func (b *Bluetooth) isDeviceExists(devices []*device, dpath dbus.ObjectPath) bool {
if b.getDeviceIndex(devices, dpath) >= 0 {
func (b *Bluetooth) isDeviceExists(dpath dbus.ObjectPath) bool {
_, i := b.getDeviceIndex(dpath)
if i >= 0 {
return true
}
return false
}
func (b *Bluetooth) getDeviceIndex(devices []*device, dpath dbus.ObjectPath) int {
func (b *Bluetooth) getDeviceIndex(dpath dbus.ObjectPath) (apath dbus.ObjectPath, index int) {
b.devicesLock.Lock()
defer b.devicesLock.Unlock()
for i, d := range devices {
if d.Path == dpath {
return i
for p, devices := range b.devices {
for i, d := range devices {
if d.Path == dpath {
return p, i
}
}
}
return -1
return "", -1
}
// GetDevices return all device objects that marshaled by json.