1# Copyright 2014 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import dbus
6import time
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros import dbus_util
10
11MANAGER_INTERFACE = 'org.chromium.Buffet.Manager'
12SERVICE_NAME = 'org.chromium.Buffet'
13MANAGER_OBJECT_PATH = '/org/chromium/Buffet/Manager'
14COMMAND_INTERFACE = 'org.chromium.Buffet.Command'
15MANAGER_INTERFACE = 'org.chromium.Buffet.Manager'
16OBJECT_MANAGER_PATH = '/org/chromium/Buffet'
17DBUS_OBJECT_MANAGER_INTERFACE = 'org.freedesktop.DBus.ObjectManager'
18DBUS_PROPERTY_INTERFACE = 'org.freedesktop.DBus.Properties'
19
20TEST_MESSAGE = 'Hello world!'
21
22class BuffetDBusHelper(object):
23    """Delegate representing an instance of buffet."""
24
25    def __init__(self):
26        """Construct a BuffetDBusHelper.
27
28        You should probably use get_helper() above rather than call this
29        directly.
30
31        @param manager_proxy: DBus proxy for the Manager object.
32
33        """
34        start_time = time.time()
35        while time.time() - start_time < 10:
36            try:
37                self._init()
38                if self.manager.TestMethod(TEST_MESSAGE) == TEST_MESSAGE:
39                    return
40            except:
41                pass
42            time.sleep(0.5)
43        raise error.TestFail('Buffet failed to restart in time.')
44
45
46    def _init(self):
47        """Init members.
48
49        """
50        bus = dbus.SystemBus()
51        manager_proxy = bus.get_object(SERVICE_NAME, MANAGER_OBJECT_PATH)
52        self.manager = dbus.Interface(manager_proxy, MANAGER_INTERFACE)
53        self.properties = dbus.Interface(manager_proxy, DBUS_PROPERTY_INTERFACE)
54        self.object_manager = dbus.Interface(
55                bus.get_object(SERVICE_NAME, OBJECT_MANAGER_PATH),
56                dbus_interface=DBUS_OBJECT_MANAGER_INTERFACE)
57
58
59    def __getattr__(self, name):
60        components = name.split('_')
61        name = ''.join(x.title() for x in components)
62        dbus_value = self.properties.Get(MANAGER_INTERFACE, name)
63        return dbus_util.dbus2primitive(dbus_value)
64