def do_next(self, _exception=None):
     log.msg("%s: NEW MODE: registration_failed" % self)
     # set encoding back to UCS2
     self.device.sconn.set_charset("UCS2")
     if not _exception:
         self.deferred.errback(ex.NetworkRegistrationError())
     else:
         self.deferred.errback(_exception)
    def on_notification_received(self, notification):
        super(NetworkRegStateMachine,
              self).on_notification_received(notification)

        if (self.mode == 'wait_to_register'
                and isinstance(notification, N.NetworkRegNotification)):
            # we are only interested on NetworkRegNotifications and we
            # must be on the wait_to_register state

            # XXX: We could also detect whether we've been unregistered from
            # the network if, say, the local MSC is down or whatever
            status = notification.args
            if status == 1:
                # we are finally registered
                self.cancel_poll()
                #self.transitionTo('obtain_netinfo')
                self.transitionTo('registration_finished')
                self.do_next()

            elif status == 2:
                # still searching for a network, give it some time
                pass

            elif status == 3:
                # registration rejected
                self.cancel_poll()
                self.transitionTo('registration_failed')
                # send exception back to behaviour
                errmsg = 'Registration rejected +CREG: 3'
                self.do_next(ex.NetworkRegistrationError(errmsg))

            elif status == 4:
                # registration rejected
                self.cancel_poll()
                self.transitionTo('registration_failed')
                # send exception back to behaviour
                errmsg = 'Registration rejected by unknown reasons, +CREG: 4'
                self.do_next(ex.NetworkRegistrationError(errmsg))

            elif status == 5:
                # registered on foreign network (roaming)
                # our SIM is smart enough to discover itself which network
                # should register with
                self.cancel_poll()
                self.transitionTo('registration_finished')
                self.do_next()
 def get_netreg_status_cb(netregstatus):
     mode, status = netregstatus
     
     if status in [1, 5]: # registered in our home network or roaming
         # we are already registered
         self.transitionTo('registration_finished')
         self.do_next()
     else:
         self.transitionTo('registration_failed')
         msg = 'Timeout while waiting for registering with the network'
         self.do_next(ex.NetworkRegistrationError(msg))
    def _process_netreg_status(self, netregstatus):
        """
        Process get_netreg_status response from "check_registered" mode
        """
        # +CREG: 0,0 - Not registered and not scanning for a GSM network
        # +CREG: 0,1 - Registered on the "HOME" network of the SIM
        # +CREG: 0,2 - Not registered but is scanning for a GSM network
        # +CREG: 0,3 - Registration is denied (Manual attempt failed)
        # +CREG: 0,4 - Offically Unknown, but seen with Ericsson modules during radio power up
        # +CREG: 0,5 - Registered on to another network (roaming).

        mode, status = netregstatus
        if status == 0:
            # Not registered and not scanning for a GSM network. That means
            # that either there's a major problem with the local network or
            # that we're abroad and we have and old SIM with no CPOL list or
            # our SIM is new and doesn't have a clue which network should
            # register with
            self.device.sconn.set_netreg_notification(1)
            self.transitionTo('wait_to_register')
            self.do_next()

        elif status == 1:
            # we are already registered
            self.transitionTo('registration_finished')
            self.do_next()

        elif status == 2:
            # ask again in a while
            if mode == 0:
                self.device.sconn.set_netreg_notification(1)
            self.transitionTo('wait_to_register')
            self.do_next()

        elif status == 3:
            self.transitionTo('registration_failed')
            # send exception back to behaviour
            errmsg = 'Registration failed CREG=0,3'
            self.do_next(ex.NetworkRegistrationError(errmsg))

        elif status == 4:
            # ask again in a while
            if mode == 0:
                self.device.sconn.set_netreg_notification(1)
            self.transitionTo('wait_to_register')
            self.do_next()

        elif status == 5:
            # we are registered and roaming is enabled
            self.transitionTo('registration_finished')
            self.do_next()