Thursday, August 19, 2010

DeviceKit

DeviceKit is a modular hardware abstraction layer designed for use in Linux systems that is designed to simplify device management and replace the current monolithic Linux HAL. DeviceKit includes the ability to enumerate system devices and send notifications when hardware is added or removed from the computer system.

DBus Interfaces provided by DeviceKit are following

  • org.freedesktop.DeviceKit.Power - Power interface
  • org.freedesktop.DeviceKit.Power.Device - Device interface
  • org.freedesktop.DeviceKit.Power.QoS- QoS interface

Samples using python

Enumerate all power objects on the system

Running ths script in virtual machine will show only one device AC. Running on real device will show other devices as well e.g. battery etc.

import dbus
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.DeviceKit.Power', '/org/freedesktop/DeviceKit/Power')
iface = dbus.Interface(proxy, 'org.freedesktop.DeviceKit.Power')
lst = iface.EnumerateDevices()
print str(lst)

Retrieving battery state

State is numberical value with following assignments

  • 0: Unknown
  • 1: Charging
  • 2: Discharging
  • 3: Empty
  • 4: Fully charged
  • 5: Pending charge
  • 6: Pending discharge
    import dbus
    def printPropValue(prop):
    bus = dbus.SystemBus()
    proxy = bus.get_object('org.freedesktop.DeviceKit.Power', '/org/freedesktop/DeviceKit/Power')
    iface = dbus.Interface(proxy, 'org.freedesktop.DeviceKit.Power')
    lst = iface.EnumerateDevices()
    id = len(lst) - 1
    proxy1 = bus.get_object('org.freedesktop.DeviceKit.Power', lst\[id\])
    iface1 = dbus.Interface(proxy1, 'org.freedesktop.DBus.Properties')
    val = iface1.Get(lst\[id\], prop)
    print prop + '\t' + str(val)
    printPropValue('State')

Getting Battery Percentage

That to get the battery percentage we need to read "Percentage" parameter of "org.freedesktop.DeviceKit.Power" interface.

Use the same function above passing "Percentage" argument

printPropValue('Percentage')

Help

http://people.freedesktop.org/~hughsient/DeviceKit-power/Device.html

http://people.freedesktop.org/~hughsient/DeviceKit-power/ref-dbus.html

http://hal.freedesktop.org/docs/DeviceKit/

No comments:

Post a Comment