1# Copyright (c) 2013 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 logging
6
7import pm_errors
8import register_machine
9
10from autotest_lib.client.cros.cellular import mm1_constants
11
12class RegisterCdmaMachine(register_machine.RegisterMachine):
13    """
14    RegisterCdmaMachine handles the CDMA specific state transitions involved in
15    bringing the modem to the REGISTERED state.
16
17    """
18    def Cancel(self):
19        """
20        Cancel the current machine.
21
22        Overwritten from parent class.
23        """
24        logging.info('RegisterCdmaMachine: Canceling register.')
25        super(RegisterCdmaMachine, self).Cancel()
26        state = self._modem.Get(mm1_constants.I_MODEM, 'State')
27        reason = mm1_constants.MM_MODEM_STATE_CHANGE_REASON_USER_REQUESTED
28        if state == mm1_constants.MM_MODEM_STATE_SEARCHING:
29            logging.info('RegisterCdmaMachine: Setting state to ENABLED.')
30            self._modem.ChangeState(mm1_constants.MM_MODEM_STATE_ENABLED,
31                                    reason)
32            self._modem.SetRegistrationState(
33                mm1_constants.MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN)
34        self._modem.register_step = None
35        if self._raise_cb:
36            self._raise_cb(
37                    pm_errors.MMCoreError(pm_errors.MMCoreError.CANCELLED,
38                                          'Cancelled'))
39
40
41    def _GetModemStateFunctionMap(self):
42        return {
43            mm1_constants.MM_MODEM_STATE_ENABLED:
44                    RegisterCdmaMachine._HandleEnabledState,
45            mm1_constants.MM_MODEM_STATE_SEARCHING:
46                    RegisterCdmaMachine._HandleSearchingState
47        }
48
49
50    def _HandleEnabledState(self):
51        logging.info('RegisterCdmaMachine: Modem is ENABLED.')
52        logging.info('RegisterCdmaMachine: Setting state to SEARCHING.')
53        self._modem.ChangeState(
54                mm1_constants.MM_MODEM_STATE_SEARCHING,
55                mm1_constants.MM_MODEM_STATE_CHANGE_REASON_USER_REQUESTED)
56        return True
57
58
59    def _HandleSearchingState(self):
60        logging.info('RegisterCdmaMachine: Modem is SEARCHING.')
61        network = self._modem.GetHomeNetwork()
62        if not network:
63            logging.info('RegisterCdmaMachine: No network available.')
64            logging.info('RegisterCdmaMachine: Setting state to ENABLED.')
65            self._modem.ChangeState(mm1_constants.MM_MODEM_STATE_ENABLED,
66                mm1_constants.MM_MODEM_STATE_CHANGE_REASON_UNKNOWN)
67            if self._raise_cb:
68                self._raise_cb(
69                        pm_errors.MMMobileEquipmentError(
70                                pm_errors.MMMobileEquipmentError.NO_NETWORK,
71                                'No networks were found to register.'))
72            self._modem.register_step = None
73            return False
74
75        logging.info(
76            'RegisterMachineCdma: Registering to network: ' + str(network))
77        logging.info('RegisterMachineCdma: Setting state to REGISTERED.')
78        self._modem.SetRegistered(network)
79        self._modem.ChangeState(
80                mm1_constants.MM_MODEM_STATE_REGISTERED,
81                mm1_constants.MM_MODEM_STATE_CHANGE_REASON_USER_REQUESTED)
82        self._modem.register_step = None
83        if self._return_cb:
84            self._return_cb()
85        return False
86