r4871 - developers/erin_yueh/bt

erin_yueh at docs.openmoko.org erin_yueh at docs.openmoko.org
Mon Dec 15 20:54:15 CET 2008


Author: erin_yueh
Date: 2008-12-15 20:54:13 +0100 (Mon, 15 Dec 2008)
New Revision: 4871

Modified:
   developers/erin_yueh/bt/browse_device.py
   developers/erin_yueh/bt/bt_adapter.py
   developers/erin_yueh/bt/bt_main.py
   developers/erin_yueh/bt/send_file.py
Log:
add bonding property, use dbus only in bt_adapter... etc.


Modified: developers/erin_yueh/bt/browse_device.py
===================================================================
--- developers/erin_yueh/bt/browse_device.py	2008-12-14 11:10:55 UTC (rev 4870)
+++ developers/erin_yueh/bt/browse_device.py	2008-12-15 19:54:13 UTC (rev 4871)
@@ -10,11 +10,7 @@
    raise SystemExit
 
 import lightblue
-import bluetooth
-
 import bt_adapter
-DEVICE_TYPE = ('Miscellaneous','Computer','Phone','LAN/Network Access point','Audio/Video',
-    'Peripheral','Imaging','Wearable','Toy','Uncategorized')
 
 
 class BrowseDevice:
@@ -23,11 +19,10 @@
     def delete_event(self, widget, event, data=None):
         print 'delete_event:', widget,event
         self.w.destroy()
-        #gtk.main_quit()
         return False
 
-    def add_row(self, b):
-        
+    def start_searching(self, b):
+
         dev_list = lightblue.finddevices()
         print dev_list
         i0 = None
@@ -37,6 +32,8 @@
                 name = self.adapter.getRemoteName(addr)
                 dev_type = self.adapter.getRemoteMajorClass(addr)
                 pincode = self.adapter.getPinCodeLength(addr)
+                if self.adapter.hasBonding(addr) == True:
+                    dev_type = dev_type + " connected"
                 row = (addr,name,dev_type,pincode)
                 i0 = self.w.sm.get_model().append(row)
         # select the new row in each view
@@ -44,8 +41,38 @@
             sel = self.w.tv.get_selection()
             i1 = self.w.sm.convert_child_iter_to_iter(None, i0)
             sel.select_iter(i1)
-        
-    
+
+
+    def search(rows, func, data):
+       if not rows: return None
+       for row in rows:
+           if func(row, data):
+               return row
+           result = search(row.iterchildren(), func, data)
+           if result: return result
+       return None
+
+    def search_device_inTree(self, address):
+        addr_list = [ r[0] for r in self.liststore ]
+        if addr_list.__contains__(address):
+            return addr_list.index(address)
+        else:  
+            return None
+
+    def bonding_created_cb(self, address):
+        print 'bonding_created_cb', address
+        index = self.search_device_inTree(address)
+        if index != None:
+            dev_type = self.adapter.getRemoteMajorClass(address)
+            self.liststore[index][2] = dev_type + " connected"
+
+    def bonding_removed_cb(self, address):
+        print 'bonding_removed_cb', address
+        index = self.search_device_inTree(address)
+        if index != None:
+            dev_type = self.adapter.getRemoteMajorClass(address)
+            self.liststore[index][2] = dev_type + " disconnected"
+
     def get_BT_adapter(self):
         
         self.bt_addr = self.adapter.getAddress()
@@ -65,18 +92,19 @@
 
     def connect_to_dv(self,addr):
         print 'connecting....', addr
-        self.adapter.createBonding(addr)
+        if self.adapter.hasBonding(addr):
+            self.adapter.removeBonding(addr)
+        else:
+            self.adapter.createBonding(addr)
 
-    def __init__(self):
+    def __init__(self, bt):
         
         # retrieve BT adapter info
-        self.adapter = bt_adapter.BluetoothAdapter()
+        self.adapter = bt.bluetooth_adapter
 
         self.get_BT_adapter()
-        #print self.lastseen_name,self.lastseen_class
 
         # create a liststore with 2 columns
-        #self.liststore = gtk.ListStore(gtk.gdk.Pixbuf,str,str,str,gtk.gdk.Pixbuf)
         # addr,name,type,security
         self.liststore = gtk.ListStore(str,str,str,int)
         # Create new windows
@@ -98,7 +126,7 @@
         win.vbox.pack_start(win.sw)
         
         win.b = gtk.Button('Start Searching...')
-        win.b.connect('clicked', self.add_row)
+        win.b.connect('clicked', self.start_searching)
         win.vbox.pack_start(win.b, False)
         
         win.sw.add(win.tv)
@@ -124,12 +152,13 @@
         win.tv.column[1].set_attributes(win.tv.cell[1], stock_id=1)
         win.tv.append_column(win.tv.column[1])
         
-        
         # display last seen devices
         for addr in self.lastseen:
             name = self.adapter.getRemoteName(addr)
             dev_type = self.adapter.getRemoteMajorClass(addr)
             pincode = self.adapter.getPinCodeLength(addr)
+            if self.adapter.hasBonding(addr) == True:
+                dev_type = dev_type + " connected"
             row = (addr,name,dev_type,pincode)
             self.w.sm.get_model().append(row)
 

Modified: developers/erin_yueh/bt/bt_adapter.py
===================================================================
--- developers/erin_yueh/bt/bt_adapter.py	2008-12-14 11:10:55 UTC (rev 4870)
+++ developers/erin_yueh/bt/bt_adapter.py	2008-12-15 19:54:13 UTC (rev 4871)
@@ -1,18 +1,24 @@
 #!/usr/bin/env python
 
 import gtk, gobject
+import browse_device
+
 import dbus
 from dbus.mainloop.glib import DBusGMainLoop
 DBusGMainLoop(set_as_default=True)
 bus = dbus.SystemBus()
 
+
 class BluetoothAdapter(object):
 
-    def __init__(self):
+    def __init__(self,bt):
 
-        #self.btObj = bus.get_object( "org.freesmartphone.odeviced", "/org/freesmartphone/Device/PowerControl/Bluetooth")
-        #self.btInf = dbus.Interface( self.btObj, "org.freesmartphone.Device.PowerControl" )
+        self.bt_main = bt
 
+        self.btObj = bus.get_object( "org.freesmartphone.odeviced", "/org/freesmartphone/Device/PowerControl/Bluetooth")
+        self.btInf = dbus.Interface( self.btObj, "org.freesmartphone.Device.PowerControl" )
+        if self.btInf.GetPower() == 0:
+            self.btInf.SetPower(True)
         self.managerObj = bus.get_object('org.bluez', '/org/bluez')
         self.managerInf = dbus.Interface(self.managerObj, 'org.bluez.Manager')
         # /org/bluez/hci0
@@ -55,9 +61,13 @@
         return self.lastseen
 
     def bonding_created_signal(self,address):
+        # send a callback to browse device 
+        self.bt_main.browse.bonding_created_cb(address)
         print 'Signal: BondingCreated(%s)' % address
 
     def bonding_removed_signal(self,address):
+        # send a callback to browse device 
+        self.bt_main.browse.bonding_removed_cb(address)
         print 'Signal: BondingRemoved(%s)' % address
 
     def disc_started_signal(self,):
@@ -190,8 +200,8 @@
     
     def hasBonding(self,addr):
         try:
-            self.hasBonding = self.adapterInf.HasBonding(addr)
-            return self.hasBonding
+            self.bond = self.adapterInf.HasBonding(addr)
+            return self.bond
         except dbus.exceptions.DBusException,e:
             print 'hasBonding error: ',addr,e        
             return None
@@ -290,7 +300,7 @@
             
     def getRemoteServiceHandles(self,addr,match):
         try:
-            print addr,match
+            print 'getRemoteServiceHandles',addr,match
             self.RemoteServiceHandles = self.adapterInf.GetRemoteServiceHandles(addr,match)
             return self.RemoteServiceHandles
         except dbus.exceptions.DBusException,e:

Modified: developers/erin_yueh/bt/bt_main.py
===================================================================
--- developers/erin_yueh/bt/bt_main.py	2008-12-14 11:10:55 UTC (rev 4870)
+++ developers/erin_yueh/bt/bt_main.py	2008-12-15 19:54:13 UTC (rev 4871)
@@ -1,90 +1,81 @@
 #!/usr/bin/env python
 
 import gtk, gobject
-import dbus
-import browse_device, pin, send_file, bt_adapter
+import browse_device, pin, send_file
+import bt_adapter 
 
 POWER_STATE = ('OFF', 'ON')
 
-
-
 class bt_main(object):
-    
+
     def power_status_cb(self,widget):
         print 'power_status_cb', widget
-        '''
-        curr_status = bluetooth_adapter.getPower()
-        if curr_status == 1:
-            bluetooth_adapter.setPower(False)
-            button.set_label("OFF")
+
+        self.curr_status = self.bluetooth_adapter.getPower()
+        if self.curr_status == 1:
+            self.bluetooth_adapter.setPower(False)
+            self.power_button.set_label("Power Status: OFF")
             print 'set Power OFF'
-        elif curr_status == 0:
-            bluetooth_adapter.setPower(True)
-            button.set_label("ON")
+        elif self.curr_status == 0:
+            self.bluetooth_adapter.setPower(True)
+            self.power_button.set_label("Power Status: ON")
             print 'set Power ON'
-        '''
-    
+
+
     def browse_device_cb(self,widget):
         print 'browse_device_cb', widget
-        browse = browse_device.BrowseDevice()
+        self.browse = browse_device.BrowseDevice(self)
 
+
     def send_file_cb(self,widget):
         print 'send_file_cb', widget
-        send_file.FileSelectionExample()
+        send_file.FileSelectionExample(self)
 
+
     def preferences_cb(self,widget):
         print 'send_preferences_cb', widget
-    
+
     def __init__(self):
         # build bluetooth adapter
-        self.bluetooth_adapter = bt_adapter.BluetoothAdapter()
+        self.bluetooth_adapter = bt_adapter.BluetoothAdapter(self)
 
         # Create the user interface
-        win = gtk.Window()
-        win.connect('delete-event', gtk.main_quit)
+        self.win = gtk.Window()
+        self.win.connect('delete-event', gtk.main_quit)
 
-        vbox = gtk.VBox()
-        win.add (vbox)
-        hbox = gtk.HBox()
-        vbox.pack_start (hbox)
+        self.vbox = gtk.VBox()
+        self.win.add (self.vbox)
+        self.hbox = gtk.HBox()
+        self.vbox.pack_start (self.hbox)
 
         # Bluetooth Power
         #status = bt_iface.GetPower()
         status = 1
-        power_button = gtk.Button()
-        power_button.set_label('Power Status: '+ POWER_STATE[status])
-        power_button.connect ("clicked", self.power_status_cb)
-        vbox.pack_start (power_button)
+        self.power_button = gtk.Button()
+        self.power_button.set_label('Power Status: '+ POWER_STATE[status])
+        self.power_button.connect ("clicked", self.power_status_cb)
+        self.vbox.pack_start (self.power_button)
 
         # Browse Devices
-        device_button = gtk.Button()
-        device_button.set_label('Browse Devices')
-        device_button.connect ("clicked", self.browse_device_cb)
-        vbox.pack_start (device_button)
-    
+        self.device_button = gtk.Button()
+        self.device_button.set_label('Browse Devices')
+        self.device_button.connect ("clicked", self.browse_device_cb)
+        self.vbox.pack_start (self.device_button)
+
         # Send File
-        file_button = gtk.Button()
-        file_button.set_label('Send File')
-        file_button.connect ("clicked", self.send_file_cb)
-        vbox.pack_start (file_button)
+        self.file_button = gtk.Button()
+        self.file_button.set_label('Send File')
+        self.file_button.connect ("clicked", self.send_file_cb)
+        self.vbox.pack_start (self.file_button)
 
         # preferences
-        pref_button = gtk.Button()
-        pref_button.set_label('Perferences')
-        pref_button.connect ("clicked", self.preferences_cb)
-        vbox.pack_start (pref_button)
-    
-        '''
-        # PassKeyEvent
+        self.pref_button = gtk.Button()
+        self.pref_button.set_label('Perferences')
+        self.pref_button.connect ("clicked", self.preferences_cb)
+        self.vbox.pack_start (self.pref_button)
 
-        PATH = '/my/PasskeyAgent'
-        handler = pin.PasskeyAgent(PATH)
-        adapter = bus.get_object('org.bluez', '/org/bluez/hci0')
-        secuirty = dbus.Interface(adapter, 'org.bluez.Security')
-        secuirty.RegisterDefaultPasskeyAgent(PATH)
-        '''
         # Show all widgets and start the main loop
-        win.show_all()
+        self.win.show_all()
 
 def main():
     gtk.main()

Modified: developers/erin_yueh/bt/send_file.py
===================================================================
--- developers/erin_yueh/bt/send_file.py	2008-12-14 11:10:55 UTC (rev 4870)
+++ developers/erin_yueh/bt/send_file.py	2008-12-15 19:54:13 UTC (rev 4871)
@@ -10,22 +10,20 @@
    raise SystemExit
 
 from xml.dom.minidom import parseString
-
 import lightblue
-import bt_adapter
 
 class FileSelectionExample:
     # Get the selected filename and print it to the console
     def file_ok_sel(self, w):
         print "%s" % self.filew.get_filename()
-        dev = SelectDevice(self.filew.get_filename())
+        dev = SelectDevice(self.bt,self.filew.get_filename())
         self.filew.destroy()
         
     def destroy(self, widget):
         self.filew.destroy()
 
-    def __init__(self):
-        
+    def __init__(self,bt):
+        self.bt = bt
         # Create a new file selection widget
         self.filew = gtk.FileSelection("File selection")
 
@@ -67,12 +65,14 @@
             if node.nodeType == node.ELEMENT_NODE and node.getAttribute('id') == '0x0004':
                 val = node.getElementsByTagName('uint8')
                 return int(val[0].getAttribute('value'), 16)
-                   
+
     def resolveService(self, addr, service='FTP' ):
         
-        service_handle = self.adapter.getRemoteServiceHandles(addr,'FTP')
+        #service_handle = self.adapter.GetRemoteServiceHandles(addr,'')
+        service_handle = self.bt.bluetooth_adapter.getRemoteServiceHandles(addr,'FTP')
+        print 'service_handle: ',service_handle
         if service_handle:
-            xml= self.adapter.getRemoteServiceRecordAsXML(addr,service_handle[0])
+            xml= self.bt.bluetooth_adapter.getRemoteServiceRecordAsXML(addr,service_handle[0])
             doc=parseString(xml)
             return self.__browsexml(doc)
         else:
@@ -94,10 +94,10 @@
     def start_sending(self, widget):
         print 'starting_sending'
         
-    def __init__(self,name):
+    def __init__(self,bt,name):
         print 'SelectDevice: dev_list'
         self.sendfname = name
-
+        self.bt = bt
         # create a liststore with 2 columns
         self.liststore = gtk.ListStore(str, str)
         
@@ -137,16 +137,13 @@
             win.tv.column[i].set_attributes(win.tv.cell[i], text=i)
 
         # display ListBondings
-        #self.obj = bus.get_object('org.bluez', '/org/bluez/hci0')
-        #self.adapter = dbus.Interface(self.obj, 'org.bluez.Adapter')
-        self.adapter = bt_adapter.BluetoothAdapter()
-        list_bonding = self.adapter.listBondings()
+        list_bonding = self.bt.bluetooth_adapter.listBondings()
         print list_bonding
         self.dev_list = {}
         
         for addr in list_bonding:
-            name = self.adapter.getRemoteName(addr)
-            dtype = self.adapter.getRemoteMajorClass(addr)
+            name = self.bt.bluetooth_adapter.getRemoteName(addr)
+            dtype = self.bt.bluetooth_adapter.getRemoteMajorClass(addr)
             self.w.sm.get_model().append([name,dtype])
             self.dev_list[name] = addr
 




More information about the commitlog mailing list