Labels

new blog 2.0

2009/06/20

Find Huawei interfaces on Linux

After two years of null-activity in the area of Huawei devices I'm resuming the work. Right now I'm in a process of coding a python package, PyHumod, that will talk to Huawei (and compatible) modems. I've just jotted a detection tool for Huawei interfaces on a HAL-enabled Linux.

Run it as follows:
$ python find_huawei_iface.py
It should come up with something like that as response:
5 Huawei interfaces detected.
    E220 HSDPA Modem : /dev/ttyUSB4
    E220 HSDPA Modem : /dev/ttyUSB3
      E620 USB Modem : /dev/ttyUSB1
      E620 USB Modem : /dev/ttyUSB2
      E620 USB Modem : /dev/ttyUSB0
Tested on Huawei E220, K3520 and E270. It's meant to work with all models.
#!/usr/bin/python
# 
# Copyright (c) 2009, Slawek Ligus 
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
# 
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS
#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
#  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
#  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
#  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 

"""find_huawei_iface.py finds active USB huawei interfaces."""

import dbus

BUS_NAME = 'org.freedesktop.Hal'
MGR_OBJ = '/org/freedesktop/Hal/Manager'
HAL_DEV_IFACE = 'org.freedesktop.Hal.Device'
HAL_MGR_IFACE = 'org.freedesktop.Hal.Manager'

bus = dbus.SystemBus()

def find_huawei_devices():
    """Find Huawei devices."""

    # Huawei vendor ID
    vendor_id = '12d1'
    hal_mgr_obj = bus.get_object(BUS_NAME, MGR_OBJ)
    hal_mgr = dbus.Interface(hal_mgr_obj, HAL_MGR_IFACE)
    all_dev = hal_mgr.FindDeviceByCapability('serial')
    devices = list()
    for device in all_dev:
        if vendor_id in device:
            devices.append(device)
    return devices

def get_hal_info(udi):
    """Return Huawei interface name and short description."""

    hal_dev = bus.get_object(BUS_NAME, udi)
    dev_property = hal_dev.GetProperty
    serial_port = dev_property('serial.device', dbus_interface=HAL_DEV_IFACE)
    info_product = dev_property('info.product', dbus_interface=HAL_DEV_IFACE)

    return info_product, serial_port

def main():
    """Find Serial interfaces for Huawei USB modems on a system."""

    devices = find_huawei_devices()
    if devices:
        l = len(devices)
        print '%s Huawei interface%s detected.' % (l, l > 1 and 's' or '')
        for dev in devices:
            print "%20s : %s" % get_hal_info(dev)
    else:
        print 'No Huawei devices found.'

if __name__ == '__main__':
    main()

4 comments:

Anonymous said...

copy / paste and i get this, and its an E220

it works out of the box, nice python code, simple and effective, i think as i dont understand python fully.

Regards


Andrew Smalley
Computer Shop V2
19 Rowlands Road
Worthing
West Sussex BN11 3JJ

andrew@akadata:~$ vi find_huawei_iface.py
andrew@akadata:~$ python find_huawei_iface.py
1 Huawei interface detected.
E220 HSDPA Modem / E270 HSDPA/HSUPA Modem : /dev/ttyUSB0
andrew@akadata:~$
andrew@akadata:~$

OOZIE said...

Thanks Andrew! I'm glad the program works for you, but from what I can see your system creates only one device for your modem (it should create data and control ports, it only shows one). Can you connect with this modem?

Anonymous said...

hi..
first thnx for all you do ..
as i see in the model of huawei modem I'm not find my modem model :
Huawei BM325 Wimax Usb Modem.
I use Ubuntu 9.10
thnx..

Thomas said...

As with the post above, I am investigating the Huawei BM325 (and BM355) Wimax Usb Modem. Where I live there is a new 4G (wiMax) network but the company (as usual) does not support Linux. I am extrmely interested if anyone can point me in the right direction to get one of these babies to work with Ubuntu as the data plan seems fantastic and I want to know if it is a worthwhile purchase.