Increasing time for which module remains in PSM.

Dependencies:   ublox-at-cellular-interface ublox-cellular-base ublox-cellular-base-n2xx ublox-at-cellular-interface-n2xx

Revision:
3:6debe6b322c4
Parent:
2:500c4fe5b9a4
Child:
4:574b2be59d12
--- a/main.cpp	Mon Jun 24 15:12:21 2019 +0500
+++ b/main.cpp	Thu Oct 31 07:31:20 2019 +0000
@@ -13,6 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#include <stdarg.h>
 #include "mbed.h"
 #include "UbloxATCellularInterface.h"
 #include "UbloxATCellularInterfaceN2xx.h"
@@ -71,6 +72,8 @@
 #endif
 #endif
 
+static rtos::Mutex trace_mutex;
+
 static void good() {
     ledGreen = 0;
     ledBlue = 1;
@@ -101,6 +104,16 @@
     ledGreen = 1;
 }
 
+static void print_function(const char *format, ...)
+{
+    trace_mutex.lock();
+    va_list arglist;
+    va_start( arglist, format );
+    vprintf(format, arglist);
+    va_end( arglist );
+    trace_mutex.unlock();
+}
+
 static void printNtpTime(char * buf, int len)
 {
     time_t timestamp = 0;
@@ -117,7 +130,7 @@
         localTime = localtime(&timestamp);
         if (localTime) {
             if (strftime(timeString, sizeof(timeString), "%a %b %d %H:%M:%S %Y", localTime) > 0) {
-                printf("NTP timestamp is %s.\n", timeString);
+                print_function("NTP timestamp is %s.\n", timeString);
             }
         }
     }
@@ -134,14 +147,14 @@
     for (x = 0; interface->connect(PIN) != 0; x++) {
         if (x > 0) {
             bad();
-            printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
+            print_function("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
         }
     }
 }
 #ifdef TARGET_UBLOX_C030_R412M
 void psm_going_in_cb(void *param)
 {
-    printf("PSM callback function:: Modem going in to sleep\n");
+    print_function("PSM callback function:: Modem going in to sleep\n");
     modem_asleep = true;
 }
 #ifdef CURRENT_MEASUREMENT
@@ -159,8 +172,8 @@
     icellular_val = (ain*1.8*1000)/7.0f;
 
 
-    printf("Voltage in mV: %f\n", icellular_val * 7.0f);
-    printf("Current draw in mA: %f\n\n", icellular_val);
+    print_function("Voltage in mV: %f\n", icellular_val * 7.0f);
+    print_function("Current draw in mA: %f\n\n", icellular_val);
 
     return icellular_val;
 }
@@ -223,135 +236,152 @@
     userButton.rise(&cbButton);
 
     good();
-    printf("Initializing modem, please wait.\n");
+    print_function("Initializing modem, please wait.\n");
     if (interface->init(PIN) == false) //setup modem
     {
-        bad();
-        printf("Failed to Initialize modem\n");
-        while(1);
+        int mno_profile;
+        int ret_val = false;
+        interface->get_mno_profile(&mno_profile);
+        if((UbloxATCellularInterface::MNOProfile)mno_profile == (UbloxATCellularInterface::MNOProfile)0)
+        {
+            print_function("Setting MNO profile..\r");
+            if(interface->set_mno_profile(UbloxATCellularInterface::STANDARD_EU))
+            {
+                print_function("MNO Profile set and rebooting the module\n");
+                interface->reboot_modem();
+                wait_ms(5000); //give modem a little time
+                ret_val = interface->init(PIN);
+            }
+        }
+        if(ret_val == false)        
+        {
+            bad();
+            print_function("Failed to Initialize modem\n");
+            while(1);
+        }
     }
-    printf("Initialization complete.\n");
+    print_function("Initialization complete.\n");
     pulseEvent();
 
 #ifdef TARGET_UBLOX_C030_R412M
-    printf("Enabling PSM...\n");
+    print_function("Enabling PSM...\n");
     if (interface->set_power_saving_mode(120, 30)) { //enable PSM
-        printf("PSM enabled. Attaching CB function and rebooting the module\n");
+        print_function("PSM enabled. Attaching CB function and rebooting the module\n");
         interface->attach_cb_psm_going_in(&psm_going_in_cb, NULL); //register callback
 
         //reset modem so that PSM settings can take effect
         interface->reboot_modem();
         wait_ms(5000); //give modem a little time
 
-        printf("please wait up to 180 seconds for network registration to complete...\n");
+        print_function("please wait up to 180 seconds for network registration to complete...\n");
         //try to re-init modem and perform registration
         for (x = 0; interface->connect(PIN) != 0; x++) {
             if (x > 0) {
                 bad();
-                printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
+                print_function("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
             }
         }
 
         interface->get_power_saving_mode(&status, &pt, &at); //read assigned values
-        printf("PSM status: %s\nAssigned Periodic TAU: %d\nAssigned Active time: %d\n", status ? "enabled" : "disabled", pt, at);
+        print_function("PSM status: %s\nAssigned Periodic TAU: %d\nAssigned Active time: %d\n", status ? "enabled" : "disabled", pt, at);
         pulseEvent();
     }
 
     //sometimes modem goes in to PSM before we can do any UDP/TCP transfers
     if (modem_asleep == true) {
-        printf("Modem is in PSM, waking up and initializing it\n");
+        print_function("Modem is in PSM, waking up and initializing it\n");
         interface->wakeup_modem(); //this wakes up the modem and also CellularBase gets synced with modem state.
         init_modem(interface);
         modem_asleep = false;
-        printf("Initialization complete\n");
+        print_function("Initialization complete\n");
     }
 #endif
 
-    printf("\nGetting the IP address of \"" TCP_SERVER "\" and \"2.pool.ntp.org\"...\n");
+    print_function("\nGetting the IP address of \"" TCP_SERVER "\" and \"2.pool.ntp.org\"...\n");
     if ((interface->gethostbyname("2.pool.ntp.org", &udpServer) == 0) &&
         (interface->gethostbyname(TCP_SERVER, &tcpServer) == 0)) {
         pulseEvent();
 
         udpServer.set_port(123);
-        printf("\"2.pool.ntp.org\" address: %s on port %d.\n", udpServer.get_ip_address(), udpServer.get_port());
-        printf("\"" TCP_SERVER "\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port());
+        print_function("\"2.pool.ntp.org\" address: %s on port %d.\n", udpServer.get_ip_address(), udpServer.get_port());
+        print_function("\"" TCP_SERVER "\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port());
         tcpServer.set_port(80);
 
-        printf("Performing socket operations in a loop (until the user button is pressed on C030 or forever on C027)...\n");
+        print_function("Performing socket operations in a loop (until the user button is pressed on C030 or forever on C027)...\n");
         while (!buttonPressed) {
 #ifdef TARGET_UBLOX_C030_R412M
             if (modem_asleep == true) {
-                printf("Modem is in PSM, waking up and initializing it\n");
+                print_function("Modem is in PSM, waking up and initializing it\n");
                 interface->wakeup_modem(); //this wakes up the modem and also CellularBase gets synced with modem state.
                 init_modem(interface);
                 modem_asleep = false;
-                printf("Initialization complete\n");
+                print_function("Initialization complete\n");
                 wait_ms(5000);
             } else {
 #endif
-                printf("Opening a UDP socket...\n");
+                print_function("Opening a UDP socket...\n");
                 if ((sockUdp.open(interface)) == 0) {
                     // UDP Sockets
                     pulseEvent();
-                    printf("UDP socket open.\n");
+                    print_function("UDP socket open.\n");
                     sockUdp.set_timeout(20000);
-                    printf("Sending time request to \"2.pool.ntp.org\" over UDP socket...\n");
+                    print_function("Sending time request to \"2.pool.ntp.org\" over UDP socket...\n");
                     memset (buf, 0, sizeof(buf));
                     *buf = '\x1b';
                     if (sockUdp.sendto(udpServer, (void *) buf, 48) == 48) {
                         pulseEvent();
-                        printf("Socket send completed, waiting for UDP response...\n");
+                        print_function("Socket send completed, waiting for UDP response...\n");
                         x = sockUdp.recvfrom(&udpSenderAddress, buf, sizeof (buf));
                         if (x > 0) {
                             pulseEvent();
-                            printf("Received %d byte response from server %s on UDP socket:\n"
+                            print_function("Received %d byte response from server %s on UDP socket:\n"
                                 "-------------------------------------------------------\n",
                                 x, udpSenderAddress.get_ip_address());
                             printNtpTime(buf, x);
-                            printf("-------------------------------------------------------\n");
+                            print_function("-------------------------------------------------------\n");
                         }
                     }
-                    printf("Closing socket...\n");
+                    print_function("Closing socket...\n");
                     sockUdp.close();
                     pulseEvent();
-                    printf("Socket closed.\n");
+                    print_function("Socket closed.\n");
                 }
 #ifndef TARGET_UBLOX_C030_N211
-                printf("Opening a TCP socket...\n");
+                print_function("Opening a TCP socket...\n");
                 if ((sockTcp.open(interface)) == 0) {
                     // TCP Sockets
                     pulseEvent();
-                    printf("TCP socket open.\n");
+                    print_function("TCP socket open.\n");
                     sockTcp.set_timeout(10000);
-                    printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port());
+                    print_function("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port());
                     if (sockTcp.connect(tcpServer) == 0) {
                         pulseEvent();
-                        printf("Connected, sending HTTP GET request to \"developer.mbed.org\" over socket...\n");
+                        print_function("Connected, sending HTTP GET request to \"developer.mbed.org\" over socket...\n");
                         strcpy (buf, "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n");
                         // Note: since this is a short string we can send it in one go as it will
                         // fit within the default buffer sizes.  Normally you should call sock.send()
                         // in a loop until your entire buffer has been sent.
                         if (sockTcp.send((void *) buf, strlen(buf)) == (int) strlen(buf)) {
                             pulseEvent();
-                            printf("Socket send completed, waiting for response...\n");
+                            print_function("Socket send completed, waiting for response...\n");
                             x = sockTcp.recv(buf, sizeof (buf));
                             if (x > 0) {
                                 pulseEvent();
-                                printf("Received %d byte response from server on TCP socket:\n"
+                                print_function("Received %d byte response from server on TCP socket:\n"
                                     "----------------------------------------------------\n%.*s"
                                     "----------------------------------------------------\n",
                                         x, x, buf);
                             }
                         }
                     }
-                    printf("Closing socket...\n");
+                    print_function("Closing socket...\n");
                     sockTcp.close();
                     pulseEvent();
-                    printf("Socket closed.\n");
+                    print_function("Socket closed.\n");
                 }
 #ifdef TARGET_UBLOX_C030_R412M
                 while(modem_asleep == false) { //modem is awake, let it go to sleep again
-                    printf("Waiting for modem to go to PSM sleep\n");
+                    print_function("Waiting for modem to go to PSM sleep\n");
                     wait_ms(5000);
                 }
             }
@@ -360,18 +390,18 @@
 
             wait_ms(5000);
 #ifndef TARGET_UBLOX_C027
-            printf("\n\n[Checking if user button has been pressed]\n");
+            print_function("\n\n[Checking if user button has been pressed]\n");
 #endif
         }
 
         pulseEvent();
-        printf("User button was pressed, stopping...\n");
+        print_function("User button was pressed, stopping...\n");
         interface->disconnect();
         ledOff();
-        printf("Stopped.\n");
+        print_function("Stopped.\n");
     } else {
         bad();
-        printf("Unable to get IP address of \"developer.mbed.org\" or \"2.pool.ntp.org\".\n");
+        print_function("Unable to get IP address of \"developer.mbed.org\" or \"2.pool.ntp.org\".\n");
     }
 }