File: linux.py

package info (click to toggle)
wader 0.5.10-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,560 kB
  • sloc: python: 16,384; makefile: 140; sh: 117
file content (501 lines) | stat: -rw-r--r-- 19,735 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# -*- coding: utf-8 -*-
# Copyright (C) 2006-2008  Vodafone España, S.A.
# Copyright (C) 2008-2009  Warp Networks, S.L.
# Author:  Pablo Martí
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""Linux-based OS plugin"""

from functools import partial
from os.path import join, exists
import re
import subprocess

from zope.interface import implements
from twisted.internet import defer, reactor, utils
from twisted.python import log, reflect
from twisted.python.procutils import which

from wader.common.interfaces import IHardwareManager
from core.hardware.base import identify_device, probe_ports
from core.plugin import PluginManager
from wader.common import consts
from core.oses.unix import UnixPlugin
from core.startup import setup_and_export_device
from core.serialport import Ports
from wader.common.utils import get_file_data, natsort


IDLE, BUSY = range(2)
ADD_THRESHOLD = 6

MODEL, VENDOR, DRIVER = "ID_MODEL_ID", "ID_VENDOR_ID", "ID_USB_DRIVER"

SUBSYSTEMS = ["tty", "usb", "net"]
REQUIRED_PROPS = [VENDOR, MODEL, DRIVER, "ID_BUS", "DEVNAME"]
BAD_DEVFILE = re.compile('^/dev/(tty\d*?|console|ptmx)$')


class HardwareManager(object):
    """
    I find and configure devices on Linux

    I am resilient to ports assigned in unusual locations
    and devices sharing ids.
    """

    implements(IHardwareManager)

    def __init__(self):
        super(HardwareManager, self).__init__()
        #: dictionary with all my configured clients
        self.clients = {}
        #: reference to StartupController
        self.controller = None
        self._waiting_deferred = None
        # remember the total client count for opath generation
        self._client_count = -1
        gudev = reflect.namedAny("gudev")
        self.gudev_client = gudev.Client(SUBSYSTEMS)
        # temporary place to store hotplugged devices to process
        self._hotplugged_devices = []
        self._call = None

        self._connect_to_signals()

    def _connect_to_signals(self):
        self.gudev_client.connect("uevent", self._on_uevent)

    def _on_uevent(self, client, action, device):
        msg = "UEVENT device: %s  action: %s"
        log.msg(msg % (device.get_sysfs_path(), action))
        if action == 'remove':
            # handle remove
            for opath, plugin in self.clients.items():
                if plugin.sysfs_path == device.get_sysfs_path():
                    self.controller.DeviceRemoved(plugin.opath)
                    self._unregister_client(plugin)

        elif action == 'add':
            # if valid, append it to the list of hotplugged devices
            # for later processing
            if self._is_valid_device(device):
                self._hotplugged_devices.append(device)

            if self._call is None:
                # the first time we set a small delay and whenever a device
                # is added we will reset the call 2 seconds
                self._call = reactor.callLater(2,
                                            self._process_hotplugged_devices)

            elif self._call.active():
                # XXX: this can be optimized by substracting x milliseconds
                # for every device added to the reset call. However it
                # introduces some more logic and perhaps should live outside.
                self._call.reset(ADD_THRESHOLD)

    def register_controller(self, controller):
        """
        See `IHardwareManager.register_controller`
        """
        self.controller = controller

    def get_devices(self):
        """See :meth:`wader.common.interfaces.IHardwareManager.get_devices`"""
        # If clients is an empty dict we assume that this is the first
        # time get_devices is executed. If not, we just return the current
        # devices. If get_devices is executed in the middle of a hotplugging
        # event, the "just added" device won't be returned, but it will be
        # processed in a few seconds by _process_hotplugged_devices anyway.
        if self.clients:
            return defer.succeed(self.clients.values())

        devices = []
        # get all the devices under the tty, usb and net subsystems
        for subsystem in SUBSYSTEMS:
            for device in self.gudev_client.query_by_subsystem(subsystem):
                if self._is_valid_device(device):
                    devices.append(device)

        return self._process_found_devices(devices)

    def _process_hotplugged_devices(self):
        # get DevicePlugin out of a list of gudev.Device
        self._process_found_devices(self._hotplugged_devices)
        self._hotplugged_devices, self._call = [], None

    def _process_found_devices(self, devices=None, emit=True):
        """
        Processes gudev ``devices`` and returns ``DevicePlugin``s

        Find devices with a common parent and merge them, identify
        the ones that need it, register and emit a signal if ``emit``
        is True.
        """
        deferreds = []
        for device in self._setup_devices(devices):
            d = identify_device(device)
            d.addCallback(self._register_client, emit=emit)
            deferreds.append(d)

        return defer.gatherResults(deferreds)

    def _is_valid_device(self, device):
        """Checks whether ``device`` is valid"""
        if not device.get_device_file():
            return False

        # before checking all the properties, filter out all the /dev/tty%d
        if BAD_DEVFILE.match(device.get_device_file()):
            return False

        # filter out /dev/usb/foo/bar/foo like too
        parts = device.get_device_file().split('/')
        if len(parts) > 3:
            return False

        # check that it has all the required properties
        # otherwise we are not interested on it
        props = device.get_property_keys()
        for prop in REQUIRED_PROPS:
            if prop not in props:
                return False

        return True

    def _setup_devices(self, devices):
        """Sets up ``devices``"""
        found_devices = {}
        for device in devices:
            props = {}

            for prop in REQUIRED_PROPS:
                value = device.get_property(prop)
                # values are either string or hex
                try:
                    props[prop] = int(value, 16)
                except ValueError:
                    props[prop] = value

            if 'DEVNAME' in props:
                abspath = device.get_device_file()
                if props['DEVNAME'] != abspath:
                    # Sometimes the DEVNAME property obtained from 'gudev' is
                    # just a relative pathname. This seems to occur on boot
                    # with the device already inserted, rather than upon a
                    # hotplug insertion event.
                    if abspath.endswith(props['DEVNAME']) and exists(abspath):
                        props['DEVNAME'] = abspath
                    else:
                        log.msg("DEVNAME != device.get_device_file() but " +
                                "fixup not possible")
                        log.msg("'%s' != '%s'" % (props['DEVNAME'], abspath))

            # if these properties are present, we should use them as the
            # data and control ports
            for mm_prop in ['ID_MM_PORT_TYPE_MODEM', 'ID_MM_PORT_TYPE_AUX']:
                if mm_prop in device.get_property_keys():
                    props[mm_prop] = bool(int(device.get_property(mm_prop)))

            # now find out the device parent
            parent = self._get_last_parent_that_matches_props(device, props)

            if parent in self.clients:
                # this device has already been setup
                return

            if parent in found_devices:
                # we have already found a device with the same parent, update
                # the attributes
                found_devices[parent].update(props)
            else:
                # a new parent has been found, store its sysfs_path as key
                # as all the childs have the same property DEVNAME, we need to
                # create a new and temporal property named DEVICES
                found_devices[parent] = props
                found_devices[parent]['DEVICES'] = []

            if 'DEVNAME' in props:
                # append the device name as usual
                found_devices[parent]['DEVICES'].append(props['DEVNAME'])
                # if any of this is present save them for later use
                for _prop in ['ID_MM_PORT_TYPE_MODEM', 'ID_MM_PORT_TYPE_AUX']:
                    if props.get(_prop, False):
                        found_devices[parent][_prop] = props['DEVNAME']

        result = []
        for sysfs_path, info in found_devices.items():
            device = self._get_device_from_info(sysfs_path, info)
            if device:
                result.append(device)
            else:
                log.msg("Unknown device: %s" % info)
                try:
                    lsusb_path = which('lsusb')[0]
                except IndexError:
                    lsusb_path = '/usr/bin/lsusb'
                try:
                    id_vendor = info['ID_VENDOR_ID']
                    id_model = info['ID_MODEL_ID']
                    p = subprocess.Popen([lsusb_path, '-v',
                                          '-d %x:%x' % (id_vendor, id_model)],
                                         bufsize=-1,
                                         stderr=subprocess.PIPE,
                                         stdout=subprocess.PIPE,
                                         universal_newlines=True,
                                         shell=False)
                    device_info, device_info_error = p.communicate()
                    log.msg('lsusb device info:%s\n\n%s' %
                            (device_info, device_info_error))
                except (OSError, ValueError):
                    pass
        return result

    def _get_last_parent_that_matches_props(self, device, props):
        parent = device.get_parent()
        while 1:
            properties = {}
            for key in parent.get_property_keys():
                properties[key] = parent.get_property(key)
                if key == "PRODUCT":
                    # udev seems to miss the ID_VENDOR_ID and ID_MODEL_ID attrs
                    # and all of the sudden a "PRODUCT" attribute appears with
                    # a value of ID_VENDOR_ID/ID_MODEL_ID/UNKNOWN.
                    vendor, model = parent.get_property(key).split('/')[:2]
                    properties[VENDOR] = int(vendor, 16)
                    properties[MODEL] = int(model, 16)

            if VENDOR in properties and MODEL in properties:
                if (props[VENDOR] != properties[VENDOR] and
                        props[MODEL] != properties[MODEL]):
                    break

            parent = parent.get_parent()
            if parent is None:
                path = device.get_sysfs_path()
                raise ValueError("Could not find %s parent" % path)

        # XXX: need to check with modemmanager if it matches
        return parent.get_sysfs_path()

    def _register_client(self, plugin, emit=False):
        """
        Registers `plugin` in `self.clients` indexes by its object path

        Will emit a DeviceAdded signal if emit is True
        """
        log.msg("registering plugin %s with opath %s" % (plugin, plugin.opath))
        self.clients[plugin.opath] = setup_and_export_device(plugin)

        if emit:
            self.controller.DeviceAdded(plugin.opath)

        return plugin

    def _unregister_client(self, client):
        """Removes client identified by ``opath``"""
        try:
            plugin = self.clients.pop(client.opath)
            plugin.close(removed=True)
        except KeyError:
            log.err("_unregister_client: Could not "
                    "unregister %s. Is not present." % client.opath)

    def _generate_opath(self):
        self._client_count += 1
        return "/org/freedesktop/ModemManager/Devices/%d" % self._client_count

    def _get_hso_ports(self, ports):
        dport = cport = None
        for port in ports:
            name = port.split('/')[-1]
            path = join('/sys/class/tty', name, 'hsotype')

            if exists(path):
                what = get_file_data(path).strip().lower()
                if what == 'modem':
                    dport = port
                elif what == 'application':
                    cport = port

                if dport and cport:
                    break

        return dport, cport

    def _get_hso_device(self, sysfs_path):
        for device in self.gudev_client.query_by_subsystem("net"):
            if device.get_sysfs_path().startswith(sysfs_path):
                return device.get_property("INTERFACE")

        raise ValueError("Cannot find hso device for device %s" % sysfs_path)

    def _get_device_from_info(self, sysfs_path, info):
        """Returns a `DevicePlugin` out of ``info``"""
        # order the ports before probing
        ports = info['DEVICES']
        natsort(ports)

        query = [info.get(key) for key in [VENDOR, MODEL]]
        plugin = PluginManager.get_plugin_by_vendor_product_id(*query)
        if plugin:
            dport = cport = None

            plugin.sysfs_path = sysfs_path
            plugin.opath = self._generate_opath()
            set_property = partial(plugin.set_property, emit=False)
            # set DBus properties (Modem interface)
            set_property(consts.MDM_INTFACE, 'IpMethod',
                         consts.MM_IP_METHOD_PPP)
            set_property(consts.MDM_INTFACE, 'MasterDevice',
                         'udev:%s' % sysfs_path)
            # XXX: Fix CDMA
            set_property(consts.MDM_INTFACE, 'Type',
                         consts.MM_MODEM_TYPE_REV['GSM'])
            set_property(consts.MDM_INTFACE, 'Driver', info[DRIVER])

            # import here else we start the dbus too early in startup
            from dbus import Boolean
            set_property(consts.MDM_INTFACE, 'Enabled', Boolean(False))

            # set to unknown
            set_property(consts.NET_INTFACE, 'AccessTechnology', 0)
            # set to -1 so any comparison will fail and will update it
            set_property(consts.NET_INTFACE, 'AllowedMode', -1)

            # preprobe stuff
            if hasattr(plugin, 'preprobe_init'):
                # this plugin requires special initialisation before probing
                plugin.preprobe_init(ports, info)

            # now get the ports
            ports_need_probe = True

            if info[DRIVER] == 'hso':
                dport, cport = self._get_hso_ports(ports)
                ports_need_probe = False

            # if these two properties are present, use them right away and
            # do not probe
            if ('ID_MM_PORT_TYPE_MODEM' in info
                    or 'ID_MM_PORT_TYPE_AUX' in info):
                try:
                    dport = info['ID_MM_PORT_TYPE_MODEM']
                    log.msg("%s: ID_MM_PORT_TYPE_MODEM" % dport)
                except KeyError:
                    pass
                try:
                    cport = info['ID_MM_PORT_TYPE_AUX']
                    log.msg("%s: ID_MM_PORT_TYPE_AUX" % cport)
                except KeyError:
                    pass
                ports_need_probe = False

            if ports_need_probe:
                # the ports were not hardcoded nor was an HSO device
                dport, cport = probe_ports(ports)

            if not dport and not cport:
                # this shouldn't happen
                msg = 'No data port and no control port with ports: %s'
                raise RuntimeError(msg % ports)

            set_property(consts.MDM_INTFACE, 'Device', dport.split('/')[-1])

            if info[DRIVER] == 'cdc_acm':
                # MBM device
                # XXX: Not all CDC devices support DHCP, to override see
                #      plugin attribute 'ipmethod'
                # XXX: Also need to support Ericsson devices via 'hso' dialer
                #      so that we can use the plain backend. At least F3607GW
                #      supports a get_ip4_config() style AT command to get
                #      network info, else we need to implement a DHCP client
                # set DBus properties (Modem.Gsm.Hso interface)
                hso_device = self._get_hso_device(sysfs_path)
                set_property(consts.MDM_INTFACE, 'Device', hso_device)

                set_property(consts.MDM_INTFACE, 'IpMethod',
                             consts.MM_IP_METHOD_DHCP)

            if plugin.dialer in 'hso':
                # set DBus properties (Modem.Gsm.Hso interface)
                hso_device = self._get_hso_device(sysfs_path)
                set_property(consts.MDM_INTFACE, 'Device', hso_device)

                if hasattr(plugin, 'ipmethod'):
                    # allows us to specify a method in a driver independent way
                    set_property(consts.MDM_INTFACE, 'IpMethod',
                                 plugin.ipmethod)

            plugin.ports = Ports(dport, cport)
            return plugin

        log.msg("Could not find a plugin with info %s" % info)
        return None


def get_hw_manager():
    try:
        return HardwareManager()
    except:
        return None


class LinuxPlugin(UnixPlugin):
    """OSPlugin for Linux-based distros"""

    dialer = None
    hw_manager = get_hw_manager()

    def __init__(self):
        super(LinuxPlugin, self).__init__()

    def is_valid(self):
        raise NotImplementedError()

    def add_default_route(self, iface):
        """See :meth:`wader.common.interfaces.IOSPlugin.add_default_route`"""
        args = ['add', 'default', 'dev', iface]
        return utils.getProcessValue('/sbin/route', args, reactor=reactor)

    def delete_default_route(self, iface):
        """
        See :meth:`wader.common.interfaces.IOSPlugin.delete_default_route`
        """
        args = ['delete', 'default', 'dev', iface]
        return utils.getProcessValue('/sbin/route', args, reactor=reactor)

    def configure_iface(self, iface, ip='', action='up'):
        """See :meth:`wader.common.interfaces.IOSPlugin.configure_iface`"""
        assert action in ['up', 'down']
        if action == 'down':
            args = [iface, action]
        else:
            args = [iface, ip, 'netmask', '255.255.255.255', '-arp', action]

        return utils.getProcessValue('/sbin/ifconfig', args, reactor=reactor)

    def get_iface_stats(self, iface):
        """See :meth:`wader.common.interfaces.IOSPlugin.get_iface_stats`"""
        stats_path = "/sys/class/net/%s/statistics" % iface
        rx_b = join(stats_path, 'rx_bytes')
        tx_b = join(stats_path, 'tx_bytes')
        try:
            return map(int, [get_file_data(rx_b), get_file_data(tx_b)])
        except (IOError, OSError):
            return 0, 0

    def get_additional_wvdial_ppp_options(self):
        return ""