mbed 5.4 with sleep mode

Dependencies:   C027_Support mbed-dev

Fork of C027_SupportTest_coap by Umar Naeem

Revision:
5:5366d39d3719
Parent:
4:90ab1ec64b0e
Child:
6:71f6214d595e
diff -r 90ab1ec64b0e -r 5366d39d3719 main.cpp
--- a/main.cpp	Tue Apr 08 15:50:09 2014 +0000
+++ b/main.cpp	Wed Apr 09 11:48:52 2014 +0000
@@ -1,25 +1,3 @@
-/*
-  main.cpp
-  2013 Copyright (c) Seeed Technology Inc.  All right reserved.
-
-  Author:lawliet zou(lawliet.zou@gmail.com)
-  2014-02-18
-
-  This library is free software; you can redistribute it and/or
-  modify it under the terms of the GNU Lesser General Public
-  License as published by the Free Software Foundation; either
-  version 2.1 of the License, or (at your option) any later version.
-
-  This library is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public
-  License along with this library; if not, write to the Free Software
-  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-*/
-
 #include "mbed.h"
 #include "C027.h"
 #include "GPS.h"
@@ -27,6 +5,38 @@
 
 C027 c027;
 
+void printDeviceStatus(MDMParser::DevStatus* status) {
+    printf("Device Status:\r\n");
+    const char* txtModel[] = { "Unknown", "SARA-G350", "LISA-U200", "LISA-C200" };
+    if (status->model < sizeof(txtModel)/sizeof(*txtModel) && (status->model != MDMParser::MODEL_UNKNOWN))
+        printf("  Model: %s\r\n", txtModel[status->model]);
+    const char* txtSim[] = { "Unknown", "Pin", "Ready" };
+    if (status->sim < sizeof(txtSim)/sizeof(*txtSim) && (status->sim != MDMParser::SIM_UNKNOWN))
+        printf("  SIM:   %s\r\n", txtSim[status->sim]);
+    if (status->ccid)  
+        printf("  CCID:  %s\r\n", status->ccid);
+    if (status->imei) 
+        printf("  IMEI:  %s\r\n", status->imei);
+    if (status->imsi)  
+        printf("  IMSI:  %s\r\n", status->imsi);
+}
+
+void printNetStatus(MDMParser::NetStatus *status)
+{
+    printf("Network Status:\r\n");
+    const char* txtReg[] = { "Unknown", "Denied", "None", "Home", "Roaming" };
+    if (status->reg < sizeof(txtReg)/sizeof(*txtReg) && (status->reg != MDMParser::REG_UNKNOWN))
+        printf("  Registration:       %s\r\n", txtReg[status->reg]);
+    const char* txtAct[] = { "Unknown", "GSM", "Edge", "3G", "CDMA" };
+    if (status->act < sizeof(txtAct)/sizeof(*txtAct) && (status->act != MDMParser::ACT_UNKNOWN))
+        printf("  Access Technology:  %s\r\n", txtAct[status->act]);
+    if (status->rssi) 
+        printf("  Signal Strength:    %d dBm\r\n", status->rssi);
+    if (status->opr)  
+        printf("  Operator:           %s\r\n", status->opr);
+    if (status->num)  
+        printf("  Phone Number:       %s\r\n", status->num);
+}
 
 int main(void)
 {
@@ -36,7 +46,7 @@
     Serial pc(USBTX,USBRX);
     pc.baud(115200);
     
-    printf("Modem Example\n");
+    printf("C027 Support Example\r\n");
 
     c027.mdmPower(true);
     c027.gpsPower(true);
@@ -52,32 +62,27 @@
                 
     // initialize the modem 
     printf("Init\r\n");
-    MDMParser::DevStatus status;
-    if (mdm.init(NULL/*no SIM PIN*/, &status))
+    MDMParser::DevStatus devStatus;
+    MDMParser::NetStatus netStatus;
+    if (mdm.init(/* SIM PIN = */ NULL, &devStatus))
     {
-        printf("Device Status:\n");
-        const char* txtModel[] = { "Unknown", "SARA-G350", "LISA-U200", "LISA-C200" };
-        if (status.model < sizeof(txtModel)/sizeof(*txtModel) && (status.model != MDMParser::MODEL_UNKNOWN))
-            printf("  Model: %s\n", txtModel[status.model]);
-        const char* txtSim[] = { "Unknown", "Pin", "Ready" };
-        if (status.sim < sizeof(txtSim)/sizeof(*txtSim) && (status.sim != MDMParser::SIM_UNKNOWN))
-            printf("  SIM:   %s\n", txtSim[status.sim]);
-        if (status.ccid)  
-            printf("  CCID:  %s\n", status.ccid);
-        if (status.imei) 
-            printf("  IMEI:  %s\r\n", status.imei);
-        if (status.imsi)  
-            printf("  IMSI:  %s\n", status.imsi);
-            
+        printDeviceStatus(&devStatus);
+        
         // wait until we are connected
         printf("Network Check\r\n");
-        while (!mdm.checkNetStatus())
+        while (!mdm.checkNetStatus(&netStatus))
             wait_ms(1000);
     
+        printNetStatus(&netStatus);
         printf("Network Join\r\n");
         // join the internet connection 
-        if (mdm.join("gprs.swisscom.ch"))
+        MDMParser::IP ip = 
+            mdm.join(/* APN      = */ "gprs.swisscom.ch", 
+                     /* USER     = */ NULL, 
+                     /* PASSWORD = */ NULL);
+        if (ip != NOIP)
         {
+            printf("IP Address: " IPSTR "\r\n", IPNUM(ip));
             printf("Socket Create\r\n");
             int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
             if (socket >= 0)
@@ -112,22 +117,22 @@
             mdm.disconnect();
         }
         
-        const char* ussd = "*#134#";
+        const char* ussd = "*#134#"; // You may get answer "UNKNOWN APPLICATION"
         printf("Send Ussd Command %s\r\n", ussd);
-        ret = mdm.ussdCommand(ussd, buf, sizeof(buf));
+        ret = mdm.ussdCommand(ussd, buf);
         if (ret > 0) 
             printf("Got Ussd Answer: \"%*s\"\r\n", ret, buf);
             
         printf("Checking SMS and GPS\r\n");
         char link[128] = "";
-        int i = 0;
+        unsigned int i = 0xFFFFFFFF;
         const int wait = 100;
         while (1) {
             
             while ((ret = gps.getMessage(buf, sizeof(buf))) > 0)
             {
                 int len = LENGTH(ret);
-                //printf("NMEA: %.*s\n", len-2, msg); 
+                //printf("NMEA: %.*s\r\n", len-2, msg); 
                 if ((PROTOCOL(ret) == NMEA) && (len > 6) && !strncmp("$GPGLL", buf, 6))
                 {
                     double la = 0, lo = 0;
@@ -136,7 +141,7 @@
                         gps.getNmeaAngle(3,buf,len,lo) && 
                         gps.getNmeaItem(6,buf,len,ch) && ch == 'A')
                     {
-                        printf("GPS Location: %.5f %.5f\n", la, lo); 
+                        printf("GPS Location: %.5f %.5f\r\n", la, lo); 
                         sprintf(link, "I am here!\n"
                                       "https://maps.google.com/?q=%.5f,%.5f", la, lo); 
                     }
@@ -145,17 +150,22 @@
             
             if (i++ == 10000/wait) {
                 i = 0;
-                
-                // checking incoming sms
-                int cnt = mdm.smsCount();
-                // search through all the messages if there are some
-                for (int ix = 1; (cnt > 0) && (ix <= 350); ix ++) {
+                // check the network status
+                if (mdm.checkNetStatus(&netStatus))
+                    printNetStatus(&netStatus);
+                    
+                // checking unread sms
+                int ix[8];
+                int n = mdm.smsList("REC UNREAD", ix, 8);
+                if (8 < n) n = 8;
+                while (0 < n--)
+                {
                     char num[32];
-                    if (mdm.smsRead(ix, num, buf, sizeof(buf))) {
+                    printf("Unread SMS at index %d\r\n", ix[n]);
+                    if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) {
                         printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
-                        printf("Delete SMS at index %d\r\n", ix);
-                        mdm.smsDelete(ix);
-                        cnt --;
+                        printf("Delete SMS at index %d\r\n", ix[n]);
+                        mdm.smsDelete(ix[n]);
                         // provide a reply
                         const char* reply = "Hello my friend";
                         if (strstr(buf, /*w*/"here are you"))
@@ -164,30 +174,13 @@
                         mdm.smsSend(num, reply);
                     }
                 }
-                
-                // check the network status
-                MDMParser::NetStatus status;
-                if (mdm.checkNetStatus(&status))
-                {
-                    printf("Network Status:\n");
-                    const char* txtNet[] = { "Unknown", "Denied", "None", "Home", "Roaming" };
-                    if (status.net < sizeof(txtNet)/sizeof(*txtNet) && (status.net != MDMParser::NET_UNKNOWN))
-                        printf("  Network:            %s\n", txtNet[status.net]);
-                    const char* txtAct[] = { "Unknown", "GSM", "Edge", "3G", "CDMA" };
-                    if (status.act < sizeof(txtAct)/sizeof(*txtAct) && (status.act != MDMParser::ACT_UNKNOWN))
-                        printf("  Access Technology:  %s\n", txtAct[status.act]);
-                    if (status.rssi) 
-                        printf("  Signal Strength:    %d dBm\r\n", status.rssi);
-                    if (status.opr)  
-                        printf("  Operator:           %s\n", status.opr);
-                    if (status.num)  
-                        printf("  Phone Number:       %s\n", status.num);
-                }
             }
             wait_ms(wait);
         }  
         mdm.powerOff();
+        gps.powerOff();
     }
+    printf("done\r\n");
     // now it is safe to switch off
     c027.mdmPower(false);
     c027.gpsPower(false);