aboutsummaryrefslogtreecommitdiff
path: root/client/cros/cellular/pseudomodem/modem.py
blob: 11dd384ff08f2e47589e40a0230ed7f30c731a94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
# Lint as: python2, python3
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import dbus
import dbus.service
import dbus.types
# AU tests use ToT client code, but ToT -3 client version.
try:
    from gi.repository import GObject
except ImportError:
    import gobject as GObject
import logging
import random

import six

import common

from autotest_lib.client.cros.cellular.pseudomodem import bearer
from autotest_lib.client.cros.cellular.pseudomodem import dbus_std_ifaces
from autotest_lib.client.cros.cellular.pseudomodem import messaging
from autotest_lib.client.cros.cellular.pseudomodem import modem_simple
from autotest_lib.client.cros.cellular.pseudomodem import pm_constants
from autotest_lib.client.cros.cellular.pseudomodem import pm_errors
from autotest_lib.client.cros.cellular.pseudomodem import sms_handler
from autotest_lib.client.cros.cellular.pseudomodem import state_machine_factory as smf
from autotest_lib.client.cros.cellular.pseudomodem import utils

from autotest_lib.client.cros.cellular import mm1_constants
from autotest_lib.client.cros.cellular import net_interface

ALLOWED_BEARER_PROPERTIES = [
    'apn',
    'operator-id',
    'allowed-modes',
    'preferred-mode',
    'bands',
    'ip-type',
    'user',
    'password',
    'allow-roaming',
    'rm-protocol',
    'number'
]

class Modem(dbus_std_ifaces.DBusProperties,
            modem_simple.ModemSimple,
            messaging.Messaging):
    """
    Pseudomodem implementation of the org.freedesktop.ModemManager1.Modem
    interface. This class serves as the abstract base class of all fake modem
    implementations.

    """

    SUPPORTS_MULTIPLE_OBJECT_PATHS = True

    def __init__(self,
                 state_machine_factory=None,
                 bus=None,
                 device='pseudomodem0',
                 device_port_type=mm1_constants.MM_MODEM_PORT_TYPE_AT,
                 index=0,
                 roaming_networks=None,
                 config=None):
        """
        Initializes the fake modem object. kwargs can contain the optional
        argument |config|, which is a dictionary of property-value mappings.
        These properties will be added to the underlying property dictionary,
        and must be one of the properties listed in the ModemManager Reference
        Manual. See _InitializeProperties for all of the properties that belong
        to this interface. Possible values for each are enumerated in
        mm1_constants.py.

        """
        if state_machine_factory:
            self._state_machine_factory = state_machine_factory
        else:
            self._state_machine_factory = smf.StateMachineFactory()
        self.device = device
        self.device_port_type = device_port_type
        self.index = index
        self.sim = None

        # The superclass construct will call _InitializeProperties
        dbus_std_ifaces.DBusProperties.__init__(self,
            mm1_constants.MM1 + '/Modem/' + str(index), bus, config)

        if roaming_networks is None:
            roaming_networks = []
        self.roaming_networks = roaming_networks

        self.bearers = {}
        self.active_bearers = {}
        self.enable_step = None
        self.disable_step = None
        self.connect_step = None
        self.disconnect_step = None
        self.register_step = None

        self._modemmanager = None
        self.resetting = False

        self._sms_handler = sms_handler.SmsHandler(self, bus)


    def _InitializeProperties(self):
        """ Sets up the default values for the properties. """
        props = {
            'Manufacturer' : 'Banana Technologies', # be creative here
            'Model' : 'Banana Peel 3000', # yep
            'Revision' : '1.0',
            'DeviceIdentifier' : 'Banana1234567890',
            'Device' : self.device,
            'Ports': [dbus.types.Struct(
                              [self.device,
                               dbus.types.UInt32(self.device_port_type)],
                              signature='su'),
                      dbus.types.Struct(
                              [net_interface.PseudoNetInterface.IFACE_NAME,
                               dbus.types.UInt32(
                                       mm1_constants.MM_MODEM_PORT_TYPE_NET)],
                              signature='su')],
            'Drivers' : ['FakeDriver'],
            'Plugin' : 'Banana Plugin',
            'UnlockRequired' :
                    dbus.types.UInt32(mm1_constants.MM_MODEM_LOCK_NONE),
            'UnlockRetries' : dbus.Dictionary(signature='uu'),
            'State' : dbus.types.Int32(mm1_constants.MM_MODEM_STATE_DISABLED),
            'SignalQuality' : dbus.types.Struct(
                                      [dbus.types.UInt32(100), True],
                                      signature='ub'),
            'OwnNumbers' : ['5555555555'],
            'PowerState' :
                    dbus.types.UInt32(mm1_constants.MM_MODEM_POWER_STATE_ON),
            'SupportedIpFamilies' :
                dbus.types.UInt32(mm1_constants.MM_BEARER_IP_FAMILY_ANY),
            'Bearers' : dbus.Array([], signature='o'),

            # specified by subclass:
            'SupportedCapabilities' :
                    [dbus.types.UInt32(mm1_constants.MM_MODEM_CAPABILITY_NONE)],
            'CurrentCapabilities' :
                    dbus.types.UInt32(mm1_constants.MM_MODEM_CAPABILITY_NONE),
            'MaxBearers' : dbus.types.UInt32(0),
            'MaxActiveBearers' : dbus.types.UInt32(0),
            'EquipmentIdentifier' : '',
            'AccessTechnologies' :
                    dbus.types.UInt32(
                            mm1_constants.MM_MODEM_ACCESS_TECHNOLOGY_UNKNOWN),
            'SupportedModes' : [
                    dbus.types.Struct(
                            [dbus.types.UInt32(
                                    mm1_constants.MM_MODEM_MODE_NONE),
                             dbus.types.UInt32(
                                    mm1_constants.MM_MODEM_MODE_NONE)],
                            signature='uu')
            ],
            'CurrentModes' :
                    dbus.types.Struct(
                            [dbus.types.UInt32(
                                    mm1_constants.MM_MODEM_MODE_NONE),
                             dbus.types.UInt32(
                                    mm1_constants.MM_MODEM_MODE_NONE)],
                            signature='uu'),
            'SupportedBands' :
                    [dbus.types.UInt32(mm1_constants.MM_MODEM_BAND_UNKNOWN)],
            'CurrentBands' :
                    [dbus.types.UInt32(mm1_constants.MM_MODEM_BAND_UNKNOWN)],
            'Sim' : dbus.types.ObjectPath(mm1_constants.ROOT_PATH)
        }
        return {
            mm1_constants.I_MODEM : props,
            mm1_constants.I_MODEM_SIMPLE : {}
        }


    def IncrementPath(self):
        """
        Increments the current index at which this modem is exposed on DBus.
        E.g. if the current path is org/freedesktop/ModemManager/Modem/0, the
        path will change to org/freedesktop/ModemManager/Modem/1.

        Calling this method does not remove the object from its current path,
        which means that it will be available via both the old and the new
        paths. This is currently only used by Reset, in conjunction with
        dbus_std_ifaces.DBusObjectManager.[Add|Remove].

        """
        self.index += 1
        path = mm1_constants.MM1 + '/Modem/' + str(self.index)
        logging.info('Modem coming back as: ' + path)
        self.SetPath(path)


    @property
    def manager(self):
        """
        The current modemmanager.ModemManager instance that is managing this
        modem.

        @returns: A modemmanager.ModemManager object.

        """
        return self._modemmanager


    @manager.setter
    def manager(self, manager):
        """
        Sets the current modemmanager.ModemManager instance that is managing
        this modem.

        @param manager: A modemmanager.ModemManager object.

        """
        self._modemmanager = manager


    @property
    def sms_handler(self):
        """
        @returns: sms_handler.SmsHandler responsible for handling SMS.

        """
        return self._sms_handler


    def IsPendingEnable(self):
        """
        @returns: True, if a current enable state machine is active and hasn't
                been cancelled.

        """
        return self.enable_step and not self.enable_step.cancelled


    def IsPendingDisable(self):
        """
        @returns: True, if a current disable state machine is active and hasn't
                been cancelled.

        """
        return self.disable_step and not self.disable_step.cancelled


    def IsPendingConnect(self):
        """
        @returns: True, if a current connect state machine is active and hasn't
                been cancelled.

        """
        return self.connect_step and not self.connect_step.cancelled


    def IsPendingDisconnect(self):
        """
        @returns: True, if a current disconnect state machine is active and
                hasn't been cancelled.

        """
        return self.disconnect_step and not self.disconnect_step.cancelled


    def IsPendingRegister(self):
        """
        @returns: True, if a current register state machine is active and hasn't
                been cancelled.

        """
        return self.register_step and not self.register_step.cancelled


    def CancelAllStateMachines(self):
        """ Cancels all state machines that are active. """
        if self.IsPendingEnable():
            self.enable_step.Cancel()
        if self.IsPendingDisable():
            self.disable_step.Cancel()
        if self.IsPendingConnect():
            self.connect_step.Cancel()
        if self.IsPendingDisconnect():
            self.disconnect_step.Cancel()
        if self.IsPendingRegister():
            self.register_step.Cancel()


    def SetSignalQuality(self, quality):
        """
        Sets the 'SignalQuality' property to the given value.

        @param quality: An integer value in the range 0-100.
        Emits:
            PropertiesChanged

        """
        self.Set(mm1_constants.I_MODEM, 'SignalQuality', (dbus.types.Struct(
            [dbus.types.UInt32(quality), True], signature='ub')))


    def ChangeState(self, state, reason):
        """
        Changes the modem state and emits the StateChanged signal.

        @param state: A MMModemState value.
        @param reason: A MMModemStateChangeReason value.
        Emits:
            PropertiesChanged
            StateChanged

        """
        old_state = self.Get(mm1_constants.I_MODEM, 'State')
        self.SetInt32(mm1_constants.I_MODEM, 'State', state)
        self.StateChanged(old_state, state, dbus.types.UInt32(reason))


    def SetSIM(self, sim):
        """
        Assigns a SIM object to this Modem. It exposes the SIM object via DBus
        and sets 'Sim' property of this Modem to the path of the SIM.

        @param sim: An instance of sim.SIM.
        Emits:
            PropertiesChanged

        """
        self.sim = sim
        if not sim:
            val = mm1_constants.ROOT_PATH
        else:
            val = sim.path
            self.sim.SetBus(self.bus)
            self.sim.modem = self
            self.UpdateLockStatus()
        self.Set(mm1_constants.I_MODEM, 'Sim', dbus.types.ObjectPath(val))


    def SetBus(self, bus):
        """
        Overridden from dbus_std_ifaces.DBusProperties.

        @param bus

        """
        dbus_std_ifaces.DBusProperties.SetBus(self, bus)
        self._state_machine_factory.SetBus(bus)
        self._sms_handler.bus = bus


    def UpdateLockStatus(self):
        """
        Tells the modem to update the current lock status. This method will
        update the modem state and the relevant modem properties.

        """
        if not self.sim:
            logging.info('SIM lock is the only kind of lock that is currently '
                         'supported. No SIM present, nothing to do.')
            return
        self.SetUInt32(mm1_constants.I_MODEM, 'UnlockRequired',
                       self.sim.lock_type)
        self.Set(mm1_constants.I_MODEM, 'UnlockRetries',
                 self.sim.unlock_retries)
        if self.sim.locked:
            def _SetLocked():
                logging.info('There is a SIM lock in place. Setting state to '
                             'LOCKED')
                self.ChangeState(
                        mm1_constants.MM_MODEM_STATE_LOCKED,
                        mm1_constants.MM_MODEM_STATE_CHANGE_REASON_UNKNOWN)

            # If the modem is currently in an enabled state, disable it before
            # setting the modem state to LOCKED.
            if (self.Get(mm1_constants.I_MODEM, 'State') >=
                    mm1_constants.MM_MODEM_STATE_ENABLED):
                logging.info('SIM got locked. Disabling modem.')
                self.Enable(False, return_cb=_SetLocked)
            else:
                _SetLocked()
        elif (self.Get(mm1_constants.I_MODEM, 'State') ==
                mm1_constants.MM_MODEM_STATE_LOCKED):
            # Change the state to DISABLED. Shill will see the property change
            # and automatically attempt to enable the modem.
            logging.info('SIM became unlocked! Setting state to INITIALIZING.')
            self.ChangeState(mm1_constants.MM_MODEM_STATE_INITIALIZING,
                             mm1_constants.MM_MODEM_STATE_CHANGE_REASON_UNKNOWN)
            logging.info('SIM became unlocked! Setting state to DISABLED.')
            self.ChangeState(mm1_constants.MM_MODEM_STATE_DISABLED,
                             mm1_constants.MM_MODEM_STATE_CHANGE_REASON_UNKNOWN)


    @utils.log_dbus_method(return_cb_arg='return_cb', raise_cb_arg='raise_cb')
    @dbus.service.method(mm1_constants.I_MODEM,
                         in_signature='b', async_callbacks=('return_cb',
                                                            'raise_cb'))
    def Enable(self, enable, return_cb=None, raise_cb=None):
        """
        Enables or disables the modem.

        When enabled, the modem's radio is powered on and data sessions, voice
        calls, location services, and Short Message Service may be available.

        When disabled, the modem enters low-power state and no network-related
        operations are available.

        @param enable: True to enable the modem and False to disable it.
        @param return_cb: The asynchronous callback to invoke on success.
        @param raise_cb: The asynchronous callback to invoke on failure. Has to
                take a python Exception or Error as its single argument.

        """
        if enable:
            logging.info('Modem enable')
            machine = self._state_machine_factory.CreateMachine(
                    pm_constants.STATE_MACHINE_ENABLE,
                    self,
                    return_cb,
                    raise_cb)
        else:
            logging.info('Modem disable')
            machine = self._state_machine_factory.CreateMachine(
                    pm_constants.STATE_MACHINE_DISABLE,
                    self,
                    return_cb,
                    raise_cb)
        machine.Start()


    def RegisterWithNetwork(
            self, operator_id="", return_cb=None, raise_cb=None):
        """
        Register with the network specified by the given |operator_id|.
        |operator_id| should be an MCCMNC value (for 3GPP) or an empty string.
        An implementation of this method must set the state to SEARCHING first,
        and eventually to REGISTERED, also setting technology specific
        registration state properties. Technology specific error cases need to
        be handled here (such as activation, the presence of a valid SIM card,
        etc).

        Must be implemented by a subclass.

        @param operator_id: String containing the operator code. This method
                will typically initiate a network scan, yielding a list of
                networks. If |operator_id| is non-empty, the modem will register
                with the network in the scanned list that matches |operator_id|.
                An empty |operator_id| means that registration should be
                "automatic". In this case the implementation would typically
                register with the home network. If a home network is not
                available than any network that is returned by a network scan
                can be registered with.

                Note: CDMA doesn't support a network scan. In this case, the
                only possible option is to register with the home network and
                ignore the value of |operator_id|.
        @param return_cb: Async success callback.
        @param raise_cb: Async failure callback.

        """
        raise NotImplementedError()


    def UnregisterWithNetwork(self):
        """
        Unregisters with the currently registered network. This should
        transition the modem to the ENABLED state.

        Must be implemented by a subclass.

        """
        raise NotImplementedError()


    def ValidateBearerProperties(self, properties):
        """
        The default implementation makes sure that all keys in properties are
        one of the allowed bearer properties. Subclasses can override this
        method to provide CDMA/3GPP specific checks.

        @param properties: The dictionary of properties and values to validate.
        @raises: MMCoreError, if one or more properties are invalid.

        """
        for key in six.iterkeys(properties):
            if key not in ALLOWED_BEARER_PROPERTIES:
                raise pm_errors.MMCoreError(
                        pm_errors.MMCoreError.INVALID_ARGS,
                        'Invalid property "%s", not creating bearer.' % key)


    @utils.log_dbus_method()
    @dbus.service.method(mm1_constants.I_MODEM, out_signature='ao')
    def ListBearers(self):
        """
        Lists configured packet data bearers (EPS Bearers, PDP Contexts, or
        CDMA2000 Packet Data Sessions).

        @returns: A list of bearer object paths.

        """
        return self.Get(mm1_constants.I_MODEM, 'Bearers')


    @utils.log_dbus_method()
    @dbus.service.method(mm1_constants.I_MODEM, in_signature='a{sv}',
                         out_signature='o')
    def CreateBearer(self, properties):
        """
        Creates a new packet data bearer using the given characteristics.

        This request may fail if the modem does not support additional bearers,
        if too many bearers are already defined, or if properties are invalid.

        @param properties: A dictionary containing the properties to assign to
                the bearer after creating it. The allowed property values are
                contained in modem.ALLOWED_PROPERTIES.
        @returns: On success, the object path of the newly created bearer.

        """
        logging.info('CreateBearer')
        maxbearers = self.Get(mm1_constants.I_MODEM, 'MaxBearers')
        if len(self.bearers) == maxbearers:
            raise pm_errors.MMCoreError(
                    pm_errors.MMCoreError.TOO_MANY,
                    ('Maximum number of bearers reached. Cannot create new '
                     'bearer.'))
        else:
            self.ValidateBearerProperties(properties)
            bearer_obj = bearer.Bearer(self.bus, properties)
            logging.info('Created bearer with path "%s".', bearer_obj.path)
            self.bearers[bearer_obj.path] = bearer_obj
            self._UpdateBearersProperty()
            return bearer_obj.path


    def ActivateBearer(self, bearer_path):
        """
        Activates a data bearer by setting its 'Connected' property to True.

        This request may fail if the modem does not support additional active
        bearers, if too many bearers are already active, if the requested
        bearer doesn't exist, or if the requested bearer is already active.

        @param bearer_path: DBus path of the bearer to activate.

        """
        logging.info('ActivateBearer: %s', bearer_path)
        bearer = self.bearers.get(bearer_path, None)
        if bearer is None:
            message = 'Could not find bearer with path "%s"' % bearer_path
            logging.info(message)
            raise pm_errors.MMCoreError(pm_errors.MMCoreError.NOT_FOUND,
                                        message)

        max_active_bearers = self.Get(mm1_constants.I_MODEM, 'MaxActiveBearers')
        if len(self.active_bearers) >= max_active_bearers:
            message = ('Cannot activate bearer: maximum active bearer count '
                       'reached.')
            logging.info(message)
            raise pm_errors.MMCoreError(pm_errors.MMCoreError.TOO_MANY, message)
        if bearer.IsActive():
            message = 'Bearer with path "%s" already active.', bearer_path
            logging.info(message)
            raise pm_errors.MMCoreError(pm_errors.MMCoreError.CONNECTED,
                                        message)

        self.active_bearers[bearer_path] = bearer
        bearer.Connect()


    def DeactivateBearer(self, bearer_path):
        """
        Deactivates data bearer by setting its 'Connected' property to False.

        This request may fail if the modem with the requested path doesn't
        exist, or if the bearer is not active.

        @param bearer_path: DBus path of the bearer to activate.

        """
        logging.info('DeactivateBearer: %s', bearer_path)
        bearer = self.bearers.get(bearer_path, None)
        if bearer is None:
            raise pm_errors.MMCoreError(
                    pm_errors.MMCoreError.NOT_FOUND,
                    'Could not find bearer with path "%s".' % bearer_path)
        if not bearer.IsActive():
            assert bearer_path not in self.active_bearers
            raise pm_errors.MMCoreError(
                    pm_errors.MMCoreError.WRONG_STATE,
                    'Bearer with path "%s" is not active.' % bearer_path)
        assert bearer_path in self.active_bearers
        bearer.Disconnect()
        self.active_bearers.pop(bearer_path)


    @utils.log_dbus_method()
    @dbus.service.method(mm1_constants.I_MODEM, in_signature='o')
    def DeleteBearer(self, bearer):
        """
        Deletes an existing packet data bearer.

        If the bearer is currently active, it will be deactivated.

        @param bearer: Object path of the bearer to delete.

        """
        logging.info('Modem.DeleteBearer: ' + str(bearer))
        if not bearer in self.bearers:
            logging.info('Unknown bearer. Nothing to do.')
            return
        bearer_object = self.bearers[bearer]
        bearer_object.remove_from_connection()
        self.bearers.pop(bearer)
        self._UpdateBearersProperty()
        if bearer in self.active_bearers:
            self.active_bearers.pop(bearer)


    def ClearBearers(self):
        """ Deletes all bearers that are managed by this modem. """
        for b in self.bearers.keys():
            self.DeleteBearer(b)


    @utils.log_dbus_method()
    @dbus.service.method(mm1_constants.I_MODEM)
    def Reset(self):
        """
        Clears non-persistent configuration and state, and returns the device to
        a newly-powered-on state.

        As a result of this operation, the modem will be removed from its
        current path and will be exposed on an incremented path. It will be
        enabled afterwards.

        """
        logging.info('Resetting modem.')

        if self.resetting:
            raise pm_errors.MMCoreError(pm_errors.MMCoreError.IN_PROGRESS,
                                        'Reset already in progress.')

        self.resetting = True

        self.CancelAllStateMachines()

        def _ResetFunc():
            # Disappear.
            manager = self.manager
            if manager:
                manager.Remove(self)
                if self.sim:
                    manager.Remove(self.sim)

            self.ClearBearers()

            # Reappear.
            def _DelayedReappear():
                self.IncrementPath()

                # Reset to defaults.
                if self.sim:
                    self.sim.Reset()
                self._properties = self._InitializeProperties()
                if self.sim:
                    self.Set(mm1_constants.I_MODEM,
                             'Sim',
                             dbus.types.ObjectPath(self.sim.path))
                    self.UpdateLockStatus()

                if manager:
                    manager.Add(self)

                self.resetting = False

                def _DelayedEnable():
                    state = self.Get(mm1_constants.I_MODEM, 'State')
                    if not self.IsPendingEnable() and \
                            state == mm1_constants.MM_MODEM_STATE_DISABLED:
                        self.Enable(True)
                    return False

                GObject.timeout_add(1000, _DelayedEnable)
                return False

            GObject.timeout_add(2000, _DelayedReappear)

        def _ErrorCallback(error):
            raise error

        if (self.Get(mm1_constants.I_MODEM, 'State') ==
                mm1_constants.MM_MODEM_STATE_CONNECTED):
            self.Disconnect('/', _ResetFunc, _ErrorCallback)
        else:
            GObject.idle_add(_ResetFunc)


    @utils.log_dbus_method()
    @dbus.service.method(mm1_constants.I_MODEM, in_signature='s')
    def FactoryReset(self, code):
        """
        Clears the modem's configuration (including persistent configuration and
        state), and returns the device to a factory-default state.

        If not required by the modem, code may be ignored.

        This command may or may not power-cycle the device.

        @param code: Carrier specific activation code.

        """
        raise NotImplementedError()


    @utils.log_dbus_method()
    @dbus.service.method(mm1_constants.I_MODEM, in_signature='(uu)')
    def SetCurrentModes(self, modes):
        """
        Sets the access technologies (eg 2G/3G/4G preference) the device is
        currently allowed to use when connecting to a network.

        @param modes: Specifies all the modes allowed in the modem as a bitmask
                of MMModemModem values.
        @param preferred: Specific MMModemMode preferred among the ones allowed,
                if any.

        """
        allowed = self.Get(mm1_constants.I_MODEM, 'SupportedModes')
        if not modes in allowed:
            raise pm_errors.MMCoreError(pm_errors.MMCoreError.FAILED,
                                        'Mode not supported: ' + repr(modes))
        self.Set(mm1_constants.I_MODEM, 'CurrentModes', modes)


    @utils.log_dbus_method()
    @dbus.service.method(mm1_constants.I_MODEM, in_signature='au')
    def SetCurrentBands(self, bands):
        """
        Sets the radio frequency and technology bands the device is currently
        allowed to use when connecting to a network.

        @param bands: Specifies the bands to be used as a list of MMModemBand
                values.

        """
        band_list = [dbus.types.UInt32(band) for band in bands]
        self.Set(mm1_constants.I_MODEM, 'CurrentBands', band_list)


    @utils.log_dbus_method()
    @dbus.service.method(mm1_constants.I_MODEM, in_signature='su',
                         out_signature='s')
    def Command(self, cmd, timeout):
        """
        Allows clients to send commands to the modem. By default, this method
        does nothing, but responds by telling the client's fortune to brighten
        the client's day.

        @param cmd: Command to send to the modem.
        @param timeout: The timeout interval for the command.
        @returns: A string containing the response from the modem.

        """
        messages = ['Bananas are tasty and fresh. Have one!',
                    'A soft voice may be awfully persuasive.',
                    'Be careful or you could fall for some tricks today.',
                    'Believe in yourself and others will too.',
                    'Carve your name on your heart and not on marble.']
        return random.choice(messages)


    @utils.log_dbus_method()
    @dbus.service.method(mm1_constants.I_MODEM, in_signature='u')
    def SetPowerState(self, power_state):
        """
        Sets the power state of the modem. This action can only be run when the
        modem is in the MM_MODEM_STATE_DISABLED state.

        @param power_state: Specifies the desired power state as a
                MMModemPowerState value.
        @raises: MMCoreError if state is not DISABLED.

        """
        if (self.Get(mm1_constants.I_MODEM, 'State') !=
                mm1_constants.MM_MODEM_STATE_DISABLED):
            raise pm_errors.MMCoreError(
                    pm_errors.MMCoreError.WRONG_STATE,
                    'Cannot set the power state if modem is not DISABLED.')
        self.SetUInt32(mm1_constants.I_MODEM, 'PowerState', power_state);


    @utils.log_dbus_method()
    @dbus.service.method(mm1_constants.I_MODEM, in_signature='u')
    def SetCurrentCapabilities(self, capabilities):
        """
        Set the capabilities of the device. A restart of the modem may be
        required.

        @param capabilities: Bitmask of MMModemCapability values, to specify the
                capabilities to use.

        """
        supported = self.Get(mm1_constants.I_MODEM, 'SupportedCapabilities')
        if not capabilities in supported:
            raise pm_errors.MMCoreError(
                    pm_errors.MMCoreError.FAILED,
                    'Given capabilities not supported: ' + capabilities)
        self.SetUInt32(mm1_constants.I_MODEM, 'CurrentCapabilities',
                       capabilities)


    @dbus.service.signal(mm1_constants.I_MODEM, signature='iiu')
    def StateChanged(self, old, new, reason):
        """
        Signals that the modem's 'State' property has changed.

        @param old: Specifies the old state, as a MMModemState value.
        @param new: Specifies the new state, as a MMModemState value.
        @param reason: Specifies the reason for this state change as a
                MMModemStateChangeReason value.

        """
        logging.info('Modem state changed from %u to %u for reason %u',
                old, new, reason)


    # org.freedesktop.ModemManager1.Messaging

    def List(self):
        """
        Overriden from messaging.Messaging.

        """
        return self._sms_handler.list_messages()


    def Delete(self, path):
        """
        Overriden from messaging.Messaging.

        @param path

        """
        self._sms_handler.delete_message(path)


    @dbus.service.signal(mm1_constants.I_MODEM_MESSAGING, signature='ob')
    def Added(self, path, received):
        """
        Overriden from messaging.Messaging.

        @param path
        @param received

        """
        logging.info('New SMS added: path: ' + path + ' received: ' +
                     str(received))


    def _UpdateBearersProperty(self):
        """
        Update the 'Bearers' property on |I_MODEM| interface to match the
        internal list.

        """
        bearers = dbus.Array(
                [dbus.types.ObjectPath(key) for key in six.iterkeys(self.bearers)],
                signature='o')
        self.Set(mm1_constants.I_MODEM, 'Bearers', bearers)