• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 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 cdma_activate_machine
6import connect_cdma_machine
7import connect_machine
8import disable_machine
9import disconnect_machine
10import enable_machine
11import pm_constants
12import register_cdma_machine
13import register_machine
14
15class StateMachineFactory(object):
16    """
17    State machines are created by the |Modem| objects by calling methods from
18    an object of type StateMachineFactory.
19
20    To supply your own state machines, simply pass in your own subclass of
21    |StateMachineFactory| that provides your implementations of the
22    state machine.
23
24    This default implementation allows tailoring the different state machines to
25    be interactive as needed.
26
27    """
28    def __init__(self):
29        self._bus = None
30        self._interactive = set()
31
32
33    def SetBus(self, bus):
34        """
35        Set the default dbus bus.
36
37        @param bus: The dbus bus.
38
39        """
40        self._bus = bus
41
42
43    def SetInteractiveAll(self):
44        """
45        Set all machines to be launched in interactive mode.
46
47        All core pseudomodem machines should appear here. If you add a state
48        machine to pseudomodem, please add it here so that tests can easily run
49        it in interactive mode.
50
51        """
52        self.SetInteractive(pm_constants.STATE_MACHINE_CDMA_ACTIVATE)
53        self.SetInteractive(pm_constants.STATE_MACHINE_CONNECT)
54        self.SetInteractive(pm_constants.STATE_MACHINE_CONNECT_CDMA)
55        self.SetInteractive(pm_constants.STATE_MACHINE_DISABLE)
56        self.SetInteractive(pm_constants.STATE_MACHINE_DISCONNECT)
57        self.SetInteractive(pm_constants.STATE_MACHINE_ENABLE)
58        self.SetInteractive(pm_constants.STATE_MACHINE_REGISTER)
59        self.SetInteractive(pm_constants.STATE_MACHINE_REGISTER_CDMA)
60
61
62    def SetInteractive(self, machine_name):
63        """
64        Set the given machine to be launched in interative mode.
65
66        @param machine_name: The name of the machine to be launched in
67                interactive mode.
68
69        """
70        self._interactive.add(machine_name)
71
72
73    def CreateMachine(self, machine_name, *args, **kwargs):
74        """
75        Create an instance of the given machine.
76
77        @param machine_name: The name of the machine to be created. All
78                supported machine names are exported as constants in the
79                |pm_constants| module.
80        @param *args, **kwargs: Arguments to pass to the machine constructor.
81        @returns: A new instance of the deseried machine
82
83        """
84        if machine_name == pm_constants.STATE_MACHINE_CDMA_ACTIVATE:
85            machine = cdma_activate_machine.CdmaActivateMachine(*args, **kwargs)
86        elif machine_name == pm_constants.STATE_MACHINE_CONNECT:
87            machine = connect_machine.ConnectMachine(*args, **kwargs)
88        elif machine_name == pm_constants.STATE_MACHINE_CONNECT_CDMA:
89            machine = connect_cdma_machine.ConnectCdmaMachine(*args, **kwargs)
90        elif machine_name == pm_constants.STATE_MACHINE_DISABLE:
91            machine = disable_machine.DisableMachine(*args, **kwargs)
92        elif machine_name == pm_constants.STATE_MACHINE_DISCONNECT:
93            machine = disconnect_machine.DisconnectMachine(*args, **kwargs)
94        elif machine_name == pm_constants.STATE_MACHINE_ENABLE:
95            machine = enable_machine.EnableMachine(*args, **kwargs)
96        elif machine_name == pm_constants.STATE_MACHINE_REGISTER:
97            machine = register_machine.RegisterMachine(*args, **kwargs)
98        elif machine_name == pm_constants.STATE_MACHINE_REGISTER_CDMA:
99            machine = register_cdma_machine.RegisterCdmaMachine(*args, **kwargs)
100        else:
101            # Reaching here is a non recoverable programming error.
102            assert False
103
104        if machine_name in self._interactive:
105            machine.EnterInteractiveMode(self._bus)
106        return machine
107