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 5 # Setup wardmodem package root and other autotest paths. 6 import common 7 8 import state_machine 9 10 class NetworkRegistrationMachine(state_machine.StateMachine): 11 """ 12 This state machine controls registration with the selected network operator. 13 14 """ 15 REGISTRATION_STATUS_CODE = { 16 'NOT_REGISTERED': '0', 17 'HOME': '1', 18 'SEARCHING': '2', 19 'DENIED': '3', 20 'UNKNOWN': '4', 21 'ROAMING': '5', 22 'SMS_ONLY_HOME': '6', 23 'SMS_ONLY_ROAMING': '7', 24 'EMERGENCY': '8', 25 'NO_CSFB_HOME': '9', 26 'NO_CSFB_ROAMING': '10', 27 } 28 29 def __init__(self, state, transceiver, modem_conf): 30 """ 31 @param state: The GlobalState object shared by all state machines. 32 33 @param transceiver: The ATTransceiver object to interact with. 34 35 @param modem_conf: A ModemConfiguration object containing the 36 configuration data for the current modem. 37 38 """ 39 super(NetworkRegistrationMachine, self).__init__(state, transceiver, 40 modem_conf) 41 42 # Register all responses used by this machine. 43 self._add_response_function( 44 'wm_response_network_registration_status_not_registered') 45 self._add_response_function( 46 'wm_response_network_registration_status_0') 47 self._add_response_function( 48 'wm_response_network_registration_status_1') 49 self._add_response_function( 50 'wm_response_network_registration_status_2') 51 52 # Initialize state 53 self._state['registration_change_message_verbosity'] = 0 54 self.register() 55 56 def get_well_known_name(self): 57 """ Returns the well known name for this machine. """ 58 return 'network_registration_machine' 59 60 61 # ########################################################################## 62 # State machine API functions. 63 def set_registration_change_message_verbosity(self, verbosity_code): 64 """ 65 This sets the verbosity level of the messages sent when registration 66 state changes, or when mm explicitly requests registration status. 67 68 @param verbosity_code: A verbosity level in ['0', '1', '2'] 69 70 """ 71 try: 72 verbosity = int(verbosity_code) 73 except (TypeError, ValueError) as e: 74 self._raise_runtime_error(self._tag_with_name( 75 'Illegal verbosity code: |%s|' % verbosity_code)) 76 77 if verbosity < 0 or verbosity > 2: 78 self._raise_runtime_error(self._tag_with_name( 79 'Verbosity code must be in the range [0, 2]. Obtained: %d' % 80 verbosity)) 81 82 self._update_state({'registration_change_message_verbosity': verbosity}) 83 self._respond_ok() 84 85 86 def get_current_registration_status(self): 87 """ Respond to queries about the current registration status. """ 88 registration_status = self._state['registration_status'] 89 access_technology = self._state['access_technology'] 90 verbosity = self._state['registration_change_message_verbosity'] 91 technology = self._state['access_technology'] 92 93 if registration_status == 'NOT_REGISTERED': 94 self._respond( 95 self.wm_response_network_registration_status_not_registered, 96 0, verbosity) 97 else: 98 registration_status_code = self.REGISTRATION_STATUS_CODE[ 99 registration_status] 100 if verbosity == 0: 101 self._respond(self.wm_response_network_registration_status_0, 102 0, 103 registration_status_code) 104 elif verbosity == 1: 105 self._respond(self.wm_response_network_registration_status_1, 106 0, 107 registration_status_code) 108 else: 109 assert verbosity == 2 110 technology_code = \ 111 self._operator_machine().get_current_technology_code() 112 self._respond(self.wm_response_network_registration_status_2, 113 0, 114 registration_status_code, 115 self._get_tracking_area_code(), 116 self._get_cell_id(), 117 technology_code) 118 self._respond_ok() 119 120 121 # ########################################################################## 122 # API methods for other state machines. 123 def register(self): 124 """ 125 Register to the currently selected network operator. 126 127 This method is currently a stub. 128 129 """ 130 self._update_state({'registration_status': 'HOME'}) 131 132 133 def deregister(self): 134 """ 135 Deregister from the currently selected network operator. 136 137 This method is currently a stub. 138 139 """ 140 self._update_state({'registration_status': 'NOT_REGISTERED'}) 141 142 143 # ########################################################################## 144 # Helper functions. 145 def _get_tracking_area_code(self): 146 # This is a 4 character hex string. 147 # We currently return a fixed string. We might want to change this in 148 # the future to simulate a moving device. 149 return '1F00' 150 151 152 def _get_cell_id(self): 153 # This is a 8 character string corresponding to the cell id. 154 # We currently return a fixed string. We might want to change this in 155 # the future to simulate a moving device. 156 return '79D803' 157 158 159 def _operator_machine(self): 160 # This machine may not have been created when __init__ is executed. 161 # Obtain a fresh handle everytime we want to use it. 162 return self._transceiver.get_state_machine('network_operator_machine') 163