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 - 2008 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': 1,
248 'Connection': connection
,
249 'LockDevice': self
.lock
,
250 'DebugLevel': self
.level
,
251 'Device': self
.device
,
255 if self
.level
== 'textall':
256 print 'Trying at %s using %s' % (self
.device
, connection
)
258 self
.listlock
.acquire()
263 gsm
.GetManufacturer()
265 self
.listlock
.release()
266 if self
.level
!= 'nothing':
267 print '!!Found model %s at %s using %s' % (
272 except gammu
.GSMError
:
273 if self
.level
== 'textall':
274 print 'Failed at %s using %s' % (self
.device
, connection
)
278 Tests all listed connections.
281 for conn
in self
.connections
:
282 self
.try_connection(conn
)
284 evt
= Wammu
.Events
.ExceptionEvent(data
= sys
.exc_info())
285 wx
.PostEvent(self
.win
, evt
)
287 class PhoneInfoThread(threading
.Thread
):
288 def __init__(self
, win
, device
, connection
):
289 threading
.Thread
.__init
__(self
)
291 self
.connection
= connection
296 res
= Wammu
.Utils
.CheckDeviceNode(self
.device
)
298 evt
= Wammu
.Events
.DataEvent(
300 error
= (res
[2], res
[3]))
301 wx
.PostEvent(self
.win
, evt
)
304 sm
= gammu
.StateMachine()
307 'UseGlobalDebugFile': 1,
310 'Connection': self
.connection
,
312 'DebugLevel': 'nothing',
313 'Device': self
.device
,
319 'Model': sm
.GetModel(),
320 'Manufacturer': sm
.GetManufacturer(),
322 evt
= Wammu
.Events
.DataEvent(data
= self
.result
)
323 wx
.PostEvent(self
.win
, evt
)
324 except gammu
.GSMError
, val
:
326 evt
= Wammu
.Events
.DataEvent(
328 error
= (_('Failed to connect to phone'),
329 Wammu
.Utils
.FormatError('', info
)
331 wx
.PostEvent(self
.win
, evt
)