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