root/network-scanner/branches/BTIntegration/umit/scan/bt/gui/btcore.py @ 5916

Revision 5916, 14.3 kB (checked in by luis, 2 years ago)

(Network Scanner - Bluetooth) Warning if the library does not exists

Line 
1#!/usr/bin/env python
2# Copyright (C) 2008 Adriano Monteiro Marques.
3#
4# Author: Devtar Singh <devtar@gmail.com>
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20import platform
21import os
22import sys
23
24import pygtk
25pygtk.require('2.0')
26import gtk
27
28from higwidgets.higdialogs import HIGDialog, HIGAlertDialog
29
30def missing_bt_library(library_name):
31    dlg = HIGAlertDialog(type=gtk.MESSAGE_WARNING, 
32                                 message_format=('Missing ' + library_name), 
33                                 secondary_text=("Bluetooth library could not be found"))
34    dlg.run()
35    dlg.destroy()
36     
37
38if(platform.system()=="Darwin"):
39   try:
40      import lightblue
41   except ImportError:
42      print >> sys.stderr, "Error loading LightBlue dependency.Exiting UmitBT..."
43      missing_bt_library("lightblue") 
44      #raise
45else:
46   try:
47      import bluetooth
48   except ImportError:
49      print >> sys.stderr, "Error loading PyBluez dependency. Exiting UmitBT..."
50      missing_bt_library("pybluez") 
51      raise
52
53#from umitCore.I18N import _
54
55
56import umit.scan.bt.gui.io
57import umit.scan.bt.core.path
58
59# globals used due to functions such as set_info and ease of access in io.py. needs redesigning
60btname = [""]
61btmac = [""]
62btmanu = [""]
63btmanumac = [""]
64btmanuname = [""]
65btsdp = [""]
66sdpstatus = ""
67
68class btcore:
69
70   def __init__(self):
71      global btname
72      global btmac
73      global btmanu
74      global btmanumac
75      global btmanuname
76      global btsdp
77      global sdpstatus
78
79   def scan(self, ScanNotebookPageBT):
80      print "Refresh ScanNotebookPageBT and Clear Cache"
81      self.clear(ScanNotebookPageBT)
82      global btname
83      global btmac
84      global btsdp
85      global sdpstatus
86      count = 1
87      # Use Lightblue backend on OSX
88      if(platform.system()=="Darwin"):
89         try:
90            btdevices = lightblue.finddevices(getnames=True, length=10)
91            for btfield in btdevices:
92               if(btfield[1] == ""):
93                  btfield[1] = "N/A"
94            #encode btname to utf-8
95            try:
96               btfield[1].encode("utf-8")
97            except:
98               btfiled[1] = "N/A"
99            btname.append(btfield[1])
100            btmac.append(btfield[0])
101            if (sdpstatus == "Enabled" and (len(btname) > 1)):       
102               self.sdp_scan(count)
103            else:                       
104               btsdp.append({"name":"SDP Disabled"})
105            count+=1
106         except:
107            dlg = HIGAlertDialog(type=gtk.MESSAGE_ERROR, 
108                                 message_format=('Scan Process'), 
109                                 secondary_text=("One or more Bluetooth devices could not be found"))
110            dlg.run()
111            dlg.destroy()
112            return
113      # Use PyBluez on Win32 or Linux
114      else:
115         try:
116            btdevices = bluetooth.discover_devices(flush_cache = True, lookup_names = True)
117            # first entry of btdevice is at index 1
118            for addr, name in btdevices:                     
119               if (name == ""):
120                  name = "N/A"
121               #encode btname to utf-8
122               try:
123                  name.encode("utf-8")
124               except:
125                  name = "N/A"
126               btname.append(name)
127               btmac.append(addr)                   
128               if (sdpstatus == "Enabled" and (len(btname) > 1)):       
129                  self.sdp_scan(count)
130               else:                       
131                  btsdp.append({"name":"SDP Disabled"})
132               count+=1
133         except:
134            dlg = HIGAlertDialog(type=gtk.MESSAGE_ERROR, 
135                                 message_format=('Scan Process'), 
136                                 secondary_text=("One or more Bluetooth devices could not be found"))
137            dlg.run()
138            dlg.destroy()
139            return                 
140
141      ScanNotebookPageBT.progb.set_text("40%")
142      ScanNotebookPageBT.progb.set_fraction(.4)
143      self.manufac()
144      ScanNotebookPageBT.progb.set_text("75%")
145      ScanNotebookPageBT.progb.set_fraction(.75)
146      self.map(ScanNotebookPageBT)
147      ScanNotebookPageBT.progb.set_text("100%")
148      ScanNotebookPageBT.progb.set_fraction(1)
149      message_id = ScanNotebookPageBT.status_bar.push(ScanNotebookPageBT.context_id, "Scanning Completed")
150
151   def sdp_scan(self, btid):
152      global btmac
153      global btsdp
154      print "BTID: " + str(btid) + "\n" + btmac[btid]
155      if(platform.system()=="Darwin"):
156         # Lightblue Output: [('00:0D:93:19:C8:68', 10, 'OBEX Object Push')]. Yet to be tested.
157         sdpservices = lightblue.findservices(address=btmac[btid])
158      else:     
159         sdpservices = bluetooth.find_service(address=btmac[btid])
160         btsdp.append(sdpservices)       
161
162   def load_db(self):
163      global btmanumac
164      global btmanuname
165      count = 0
166      if(os.path.exists(os.path.join(path.paths, "UmitBT", "config", "db", "btdb.db"))):
167         print "load database"
168         btdb_dir = os.path.abspath(os.path.join(path.paths, "UmitBT", "config", "db", "btdb.db"))
169         btdb = open(btdb_dir, "rb")
170         for line in btdb:
171            count+=1
172            btbuf1, btbuf2 = line.split(";")
173            btmanumac.append(btbuf1)
174            btmanuname.append(btbuf2)                   
175         btdb.close()
176      else:                     
177         print "ERROR: Can't open btdb!"
178         dlg = HIGAlertDialog(None, message_format=('I/O Error'), secondary_text=("Cant open btdb.db"))
179         dlg.run()
180         dlg.destroy()
181
182   def manufac(self):
183      global btname
184      global btmac
185      global btmanu
186      global btmacsearch
187      global btnamesearch
188      print "manufacturer detection"
189      print btmac[1]
190      count = 1
191      # Load btdb
192      self.load_db()
193      print "Len : " + str(len(btname))
194      while ((len(btname)) > count):
195         print "Count"+str(count)
196         if(btmanumac.count(btmac[count][:8]) > 0):
197            manuindex = btmanumac.index(btmac[count][:8])
198            btmanuname[manuindex] = btmanuname[manuindex].replace("\n", "")
199            btmanu.append(btmanuname[manuindex])
200         else:
201            print "Null: " + btmac[count][:8]
202            btmanu.append("null")
203         count+= 1
204
205   def map(self, ScanNotebookPageBT):   
206      global btname
207      global btmac
208      global btmanu
209      count = 1
210      while ((len(btname)) > count):         
211         if(btmanu[count].find("Apple") > -1 and btmanu[count].find("Applera") == -1):
212            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "apple.png"))
213            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])
214         elif(btmanu[count].find("Google") > -1 ):
215            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "google.png"))
216            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])   
217         elif(btmanu[count].find("Nokia") > -1 ):
218            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "nokia.png"))
219            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])
220         elif(btmanu[count].find("Microsoft") > -1 or btmanu[count].find("MICROSOFT") > -1):
221            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "microsoft.png"))
222            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])   
223         elif(btmanu[count].find("Motorola") > -1):
224            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "moto.png"))
225            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])
226         elif(btmanu[count].find("INTEL CORPORATION") > -1):
227            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "intel.png"))
228            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])   
229         elif(btmanu[count].find("Cisco") > -1 ):
230            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "cisco.png"))
231            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])
232         elif(btmanu[count].find("LG Electronics") > -1 ):
233            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "lg.png"))
234            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])           
235         elif(btmanu[count].find("Dell") > -1 ):
236            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "dell.png"))
237            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])   
238         elif(btmanu[count].find("D-Link") > -1 ):
239            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "dlink.png"))
240            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])
241         elif(btmanu[count].find("DoCoMo") > -1 ):
242            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "docomo.png"))
243            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])
244         elif(btmanu[count].find("Samsung") > -1):
245            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "samsung.png"))
246            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])   
247         #Filter Sony Erricson, Sony Computer Entertainment, then Sony Microsoft
248         elif(btmanu[count].find("Sony Ericsson") > -1):
249            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "sony-eric.png"))
250            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])
251         elif(btmanu[count].find("Sony Computer Entertainment") > -1):
252            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "sony-play.png"))
253            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])   
254         elif(btmanu[count].find("Sony") > -1):
255            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "sony.png"))
256            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])
257         elif(btmanu[count].find("Blaupunkt") > -1 ):
258            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "blaupunkt.png"))
259            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])   
260         else:
261            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join("share", "pixmaps", "umit", "bt.png"))
262            ScanNotebookPageBT.mapmodel.append([btname[count], pixbuf])
263         count+=1 
264
265   def set_info(self, ScanNotebookPageBT, btid):
266      global btname
267      global btmac
268      global btmanu
269      global btsdp
270      global sdpstatus
271      sdpbrowseinfo = ""
272      btid+=1
273      print "Selected: " + str(ScanNotebookPageBT.btmap.get_selected_items()) + "\n" + str(btid)         
274      ScanNotebookPageBT.label.set_text("Device Details\t\t\t\t\n\nName: " + btname[btid] + "\nMAC: " + btmac[btid] + "\nManufacturer: " + btmanu[btid])
275      print "Status_select: " + sdpstatus
276      print "Found " + str(len(btsdp[btid])) + " Services"           
277      if (sdpstatus == "Enabled"):
278         if (len(btsdp[btid]) < 1):
279            ScanNotebookPageBT.sdpview.get_buffer().set_text("no services found")
280            return
281         sdpbrowseinfo = "\nFound " + str(len(btsdp[btid])) + " Services"
282         for svc in btsdp[btid]:
283            try:
284               if(str(svc["name"])=="no services found" or str(svc["name"])== ""):
285                  ScanNotebookPageBT.sdpview.get_buffer().set_text("no services found")
286               else:
287                  try:
288                     sdpbrowseinfo += "\n\nService Name: " + str(svc["name"]) + "\n Description: " + str(svc["description"])
289                     sdpbrowseinfo += "\n Provided By: " + str(svc["provider"]) + "\n Protocol: " + str(svc["protocol"])
290                     sdpbrowseinfo += "\n channel/PSM: " + str(svc["port"]) + "\n svc classes: " + str(svc["service-classes"])
291                     sdpbrowseinfo += "\n profiles:  " + str(svc["profiles"]) + "\n service id: " + str(svc["service-id"])
292                     ScanNotebookPageBT.sdpview.get_buffer().set_text(sdpbrowseinfo)
293                  except: 
294                     ScanNotebookPageBT.sdpview.get_buffer().set_text("N/A")
295                     dlg = HIGAlertDialog(None, message_format=('Error'), secondary_text=("An error occurred while parsing SDP data"))
296                     dlg.run()
297                     dlg.destroy()
298            except: 
299               ScanNotebookPageBT.sdpview.get_buffer().set_text("no services found")                   
300
301   def set_sdp(self, status):
302      # Status of Enabled or Disabled
303      global sdpstatus
304      sdpstatus = status
305
306   def clear(self, ScanNotebookPageBT):   
307      global btname
308      global btmac
309      global btmanu
310      global btsdp
311
312      btname = [""]
313      btmac = [""]
314      btmanu = [""]
315      btsdp = [""]
316      ScanNotebookPageBT.progb.set_text("")                 
317      ScanNotebookPageBT.progb.set_fraction(0)
318      ScanNotebookPageBT.mapmodel.clear()
319
320   def save_scan(self, ScanNotebookPageBT):   
321      io.io().save()
322      ScanNotebookPageBT.progb.set_text("100%")
323      ScanNotebookPageBT.progb.set_fraction(1)
324      message_id = ScanNotebookPageBT.status_bar.push(ScanNotebookPageBT.context_id, "File Saved")
325
326   def load_scan(self, ScanNotebookPageBT, filename):     
327      #clear previous entries before loading *ubt
328      self.clear(ScanNotebookPageBT)
329      status = io.io().load(filename) 
330      if(str(status)!="None"):
331         ScanNotebookPageBT.progb.set_text("0%")
332         ScanNotebookPageBT.progb.set_fraction(0)
333         message_id = ScanNotebookPageBT.status_bar.push(ScanNotebookPageBT.context_id, "File Load Error Encountered")
334         return
335      self.map(ScanNotebookPageBT)
336      ScanNotebookPageBT.progb.set_text("100%")
337      ScanNotebookPageBT.progb.set_fraction(1)
338      message_id = ScanNotebookPageBT.status_bar.push(ScanNotebookPageBT.context_id, "File Loaded")
Note: See TracBrowser for help on using the browser.