ofono_client: Implement network registration during connect()

A new mcc_mnc parameter is now optionally passed to connect() in order
to manually register to a specific network with a given MCC+MNC pair.
If no parameter is passed (or None), then the modem will be instructed
to attempt an automatic registration with any available network which
permits it.

We get the MCC+MNC parameter from the MSC/NITB and we pass it to the
modem object at connect time as shown in the modified tests. Two new
simple tests to check network registration is working are added in this
commit.

Ofono modems seem to be automatically registering at some point after
they are set Online=true, and we were actually using that 'feature'
before this patch. Thus, it is possible that a modem quickly becomes
registered, and we then check so before starting the scan+registration
process, which can take a few seconds.

The scanning method can take a few seconds to complete. To avoid
blocking in the dbus ofono Scan() method, this commit adds some code to
make use of glib/gdbus async methods, which are not yet supported
directly by pydbus. This way, we can continue polling while waiting for
the scan process to complete and we can register several modems in
parallel. When scan completes, a callback is run which attempts to
register. If no MCC+MNC was passed, as we just finished scanning the
modem should have enough fresh operator information to take good and
quick decisions on where to connect. If we have an MCC+MNC, then we check
the operator list received by Scan() method. If operator with desired
MCC+MNC is there, we register with it. If it's not there, we start
scanning() again asynchronously hoping the operator will show up in next
scan.

As scanning() and registration is done in the background, tests are
expected to call connect(), and then later on wait for the modem to
register by waiting/polling the method "modem.is_connected()". Tests
first check for the modem being connected and after with MSC
subscriber_attached(). The order is intentional because the later has to
poll through network and adds unneeded garbage to the pcap files bein
recorded.

Change-Id: I8d9eb47eac1044550d3885adb55105c304b0c15c
diff --git a/suites/aoip_debug/interactive.py b/suites/aoip_debug/interactive.py
index 7cc1b76..cfedd3a 100755
--- a/suites/aoip_debug/interactive.py
+++ b/suites/aoip_debug/interactive.py
@@ -18,7 +18,7 @@
 
 for m in modems:
   hlr.subscriber_add(m)
-  m.connect(bsc)
+  m.connect(msc.mcc_mnc())
 
 while True:
   cmd = prompt('Enter command: (q)uit (s)ms (g)et-registered (w)ait-registered')
@@ -30,6 +30,8 @@
     break
   elif 'wait-registered'.startswith(cmd):
     try:
+      for m in modems:
+          wait(m.is_connected, msc.mcc_mnc())
       wait(msc.subscriber_attached, *modems)
     except Timeout:
       print('Timeout while waiting for registration.')
diff --git a/suites/aoip_sms/mo_mt_sms.py b/suites/aoip_sms/mo_mt_sms.py
index b9383ea..8eba842 100755
--- a/suites/aoip_sms/mo_mt_sms.py
+++ b/suites/aoip_sms/mo_mt_sms.py
@@ -21,13 +21,15 @@
 hlr.subscriber_add(ms_mo)
 hlr.subscriber_add(ms_mt)
 
-ms_mo.connect(bsc)
-ms_mt.connect(bsc)
+ms_mo.connect(msc.mcc_mnc())
+ms_mt.connect(msc.mcc_mnc())
 
 ms_mo.log_info()
 ms_mt.log_info()
 
 print('waiting for modems to attach...')
+wait(ms_mo.is_connected, msc.mcc_mnc())
+wait(ms_mt.is_connected, msc.mcc_mnc())
 wait(msc.subscriber_attached, ms_mo, ms_mt)
 
 sms = ms_mo.sms_send(ms_mt)
diff --git a/suites/debug/interactive.py b/suites/debug/interactive.py
index 603e395..37076dc 100755
--- a/suites/debug/interactive.py
+++ b/suites/debug/interactive.py
@@ -13,7 +13,7 @@
 
 for m in modems:
   nitb.subscriber_add(m)
-  m.connect(nitb)
+  m.connect(nitb.mcc_mnc())
 
 while True:
   cmd = prompt('Enter command: (q)uit (s)ms (g)et-registered (w)ait-registered')
@@ -25,6 +25,8 @@
     break
   elif 'wait-registered'.startswith(cmd):
     try:
+      for m in modems:
+         wait(m.is_connected, nitb.mcc_mnc())
       wait(nitb.subscriber_attached, *modems)
     except Timeout:
       print('Timeout while waiting for registration.')
diff --git a/suites/netreg/register.py b/suites/netreg/register.py
new file mode 100755
index 0000000..9141986
--- /dev/null
+++ b/suites/netreg/register.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+from osmo_gsm_tester.test import *
+
+print('use resources...')
+nitb = suite.nitb()
+bts = suite.bts()
+ms = suite.modem()
+
+print('start nitb and bts...')
+nitb.bts_add(bts)
+nitb.start()
+bts.start()
+
+nitb.subscriber_add(ms)
+
+ms.connect(nitb.mcc_mnc())
+
+print(ms.info())
+
+wait(ms.is_connected, nitb.mcc_mnc())
+wait(nitb.subscriber_attached, ms)
diff --git a/suites/netreg/register_default.py b/suites/netreg/register_default.py
new file mode 100755
index 0000000..d15b3f5
--- /dev/null
+++ b/suites/netreg/register_default.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+from osmo_gsm_tester.test import *
+
+print('use resources...')
+nitb = suite.nitb()
+bts = suite.bts()
+ms = suite.modem()
+
+print('start nitb and bts...')
+nitb.bts_add(bts)
+nitb.start()
+bts.start()
+
+nitb.subscriber_add(ms)
+
+ms.connect()
+
+print(ms.info())
+
+wait(ms.is_connected)
+wait(nitb.subscriber_attached, ms)
diff --git a/suites/netreg/suite.conf b/suites/netreg/suite.conf
new file mode 100644
index 0000000..1bb1dbb
--- /dev/null
+++ b/suites/netreg/suite.conf
@@ -0,0 +1,10 @@
+resources:
+  ip_address:
+  - times: 1
+  bts:
+  - times: 1
+  modem:
+  - times: 1
+
+defaults:
+  timeout: 40s
diff --git a/suites/sms/mo_mt_sms.py b/suites/sms/mo_mt_sms.py
index 860f5e7..4e0ba08 100755
--- a/suites/sms/mo_mt_sms.py
+++ b/suites/sms/mo_mt_sms.py
@@ -14,13 +14,15 @@
 nitb.subscriber_add(ms_mo)
 nitb.subscriber_add(ms_mt)
 
-ms_mo.connect(nitb)
-ms_mt.connect(nitb)
+ms_mo.connect(nitb.mcc_mnc())
+ms_mt.connect(nitb.mcc_mnc())
 
 ms_mo.log_info()
 ms_mt.log_info()
 
 print('waiting for modems to attach...')
+wait(ms_mo.is_connected, nitb.mcc_mnc())
+wait(ms_mt.is_connected, nitb.mcc_mnc())
 wait(nitb.subscriber_attached, ms_mo, ms_mt)
 
 sms = ms_mo.sms_send(ms_mt)