1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
7 __author__
= 'Michal Čihař'
8 __email__
= 'michal@cihar.com'
10 Copyright © 2003 - 2010 Michal Čihař
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License version 2 as published by
14 the Free Software Foundation.
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 if Wammu
.gammu_error
== None:
35 from Wammu
.Locales
import StrConv
40 import Wammu
.BluezDiscovery
45 class AllSearchThread(threading
.Thread
):
47 Root thread for phone searching. It spawns other threads for testing each
56 noticecallback
= None,
58 threading
.Thread
.__init
__(self
)
62 self
.listlock
= threading
.Lock()
65 self
.callback
= callback
66 self
.msgcallback
= msgcallback
67 self
.noticecallback
= noticecallback
70 def create_search_thread(self
, device
, connections
, name
):
72 Creates single thread for searching phone on device using listed
73 connections. Name is just text which will be shown to user.
75 newthread
= SearchThread(
82 newthread
.setName(name
)
83 if self
.msgcallback
!= None:
88 self
.threads
.append(newthread
)
91 def search_bt_device(self
, address
, name
):
93 Searches single Bluetooth device.
95 connections
= Wammu
.Data
.Conn_Bluetooth_All
96 vendorguess
= _('Could not guess vendor')
97 # Use better connection list for some known manufacturers
98 for vendor
in Wammu
.Data
.MAC_Prefixes
.keys():
99 if address
[:8].upper() in Wammu
.Data
.MAC_Prefixes
[vendor
]:
100 connections
= Wammu
.Data
.Conn_Bluetooth
[vendor
]
101 vendorguess
= _('Guessed as %s') % vendor
103 self
.create_search_thread(
106 '%s (%s) - %s - %s' % (
112 def check_device(self
, curdev
):
114 Checks whether it makes sense to perform searching on this device and
115 possibly warns user about misconfigurations.
117 res
= Wammu
.Utils
.CheckDeviceNode(curdev
)
123 if res
[1] != '' and self
.msgcallback
!= None:
124 self
.msgcallback(res
[1])
125 if res
[2] != '' and self
.noticecallback
!= None:
126 self
.noticecallback(res
[2], res
[3])
129 def search_device(self
, curdev
, dev
):
131 Performs search on one real device.
133 if len(curdev
) > 0 and curdev
[0] == '/':
134 if not self
.check_device(curdev
):
137 self
.create_search_thread(
140 '%s - %s' % (curdev
, str(dev
[0])))
142 def listed_device_search(self
):
144 Initiates searching of devices defined in Wammu.Data.AllDevices.
146 for dev
in Wammu
.Data
.AllDevices
:
147 if not (self
.limit
== 'all' or self
.limit
in dev
[3]):
149 if dev
[1].find('%d') >= 0:
150 for i
in range(*dev
[2]):
152 self
.search_device(curdev
, dev
)
154 self
.search_device(dev
[1], dev
)
156 def bluetooth_device_search_bluez(self
):
158 Initiates searching for Bluetooth devices using PyBluez stack.
161 if self
.msgcallback
!= None:
162 self
.msgcallback(_('Scanning for Bluetooth devices using %s') %
166 discovery
= Wammu
.BluezDiscovery
.Discovery(self
)
167 discovery
.find_devices()
168 discovery
.process_inquiry()
169 if len(discovery
.names_found
) == 0 and self
.msgcallback
!= None:
170 self
.msgcallback(_('No Bluetooth device found'))
171 if self
.msgcallback
!= None:
172 self
.msgcallback(_('Bluetooth device scan completed'))
173 except bluetooth
.BluetoothError
, txt
:
174 if self
.msgcallback
!= None:
176 _('Could not access Bluetooth subsystem (%s)') %
179 def bluetooth_device_search(self
):
181 Initiates searching for Bluetooth devices.
183 if not self
.limit
in ['all', 'bluetooth']:
185 if BLUETOOTH
== 'bluez':
186 self
.bluetooth_device_search_bluez()
188 if self
.msgcallback
!= None:
189 self
.msgcallback(_('PyBluez not found, it is not possible to scan for Bluetooth devices.'))
190 if self
.noticecallback
!= None:
192 _('No Bluetooth searching'),
193 _('PyBluez not found, it is not possible to scan for Bluetooth devices.'))
197 self
.listed_device_search()
198 self
.bluetooth_device_search()
201 while len(self
.threads
) > 0:
202 if self
.threads
[i
].isAlive():
205 if self
.msgcallback
!= None:
206 self
.msgcallback(_('Finished %s') %
207 StrConv(self
.threads
[i
].getName()))
209 if i
>= len(self
.threads
):
211 if self
.msgcallback
!= None:
212 self
.msgcallback(_('All finished, found %d phones') %
214 if self
.callback
!= None:
215 self
.callback(self
.list)
217 evt
= Wammu
.Events
.ExceptionEvent(data
= sys
.exc_info())
218 wx
.PostEvent(self
.win
, evt
)
220 class SearchThread(threading
.Thread
):
229 threading
.Thread
.__init
__(self
)
231 self
.connections
= connections
236 self
.listlock
= listlock
238 def try_connection(self
, connection
):
240 Performs test on single connection.
242 gsm
= gammu
.StateMachine()
245 'UseGlobalDebugFile': True,
248 'Connection': connection
,
249 'LockDevice': self
.lock
,
250 'DebugLevel': self
.level
,
251 'Device': self
.device
,
255 # Compatibility with old Gammu versions
256 cfg
= Wammu
.Utils
.CompatConfig(cfg
)
258 gsm
.SetConfig(0, cfg
)
260 # Compatibility with old Gammu versions
261 cfg
= Wammu
.Utils
.CompatConfig(cfg
)
264 if self
.level
== 'textall':
265 print 'Trying at %s using %s' % (self
.device
, connection
)
267 self
.listlock
.acquire()
272 gsm
.GetManufacturer()
274 self
.listlock
.release()
275 if self
.level
!= 'nothing':
276 print '!!Found model %s at %s using %s' % (
281 except gammu
.GSMError
:
282 if self
.level
== 'textall':
283 print 'Failed at %s using %s' % (self
.device
, connection
)
287 Tests all listed connections.
290 for conn
in self
.connections
:
291 self
.try_connection(conn
)
293 evt
= Wammu
.Events
.ExceptionEvent(data
= sys
.exc_info())
294 wx
.PostEvent(self
.win
, evt
)
296 class PhoneInfoThread(threading
.Thread
):
297 def __init__(self
, win
, device
, connection
):
298 threading
.Thread
.__init
__(self
)
300 self
.connection
= connection
305 if self
.connection
.lower().find('blue') == -1 and self
.connection
.lower().find('irda') == -1:
306 res
= Wammu
.Utils
.CheckDeviceNode(self
.device
)
308 evt
= Wammu
.Events
.DataEvent(
310 error
= (res
[2], res
[3]))
311 wx
.PostEvent(self
.win
, evt
)
314 sm
= gammu
.StateMachine()
317 'UseGlobalDebugFile': True,
320 'Connection': self
.connection
,
322 'DebugLevel': 'nothing',
323 'Device': self
.device
,
327 # Compatibility with old Gammu versions
328 cfg
= Wammu
.Utils
.CompatConfig(cfg
)
333 'Model': sm
.GetModel(),
334 'Manufacturer': sm
.GetManufacturer(),
336 evt
= Wammu
.Events
.DataEvent(data
= self
.result
)
337 wx
.PostEvent(self
.win
, evt
)
338 except gammu
.GSMError
, val
:
340 evt
= Wammu
.Events
.DataEvent(
342 error
= (_('Failed to connect to phone'),
343 Wammu
.Utils
.FormatError('', info
)
345 wx
.PostEvent(self
.win
, evt
)