h

Dependencies:   C027_Support mbed-dev

Fork of C027_SupportTest_coapp by Umar Naeem

Files at this revision

API Documentation at this revision

Comitter:
iftaziz
Date:
Fri Aug 25 10:40:13 2017 +0000
Parent:
39:4f3f7463e55f
Commit message:
iazi

Changed in this revision

C027_Support.lib Show annotated file Show diff for this revision Revisions of this file
Coap/coap_client.cpp Show annotated file Show diff for this revision Revisions of this file
drv_GNSS/drv_C027_GNSS.cpp Show annotated file Show diff for this revision Revisions of this file
drv_GNSS/drv_C027_GNSS.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
mbed-dev.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 4f3f7463e55f -r da5f84f19a24 C027_Support.lib
--- a/C027_Support.lib	Thu May 25 11:53:45 2017 +0000
+++ b/C027_Support.lib	Fri Aug 25 10:40:13 2017 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/ranaumarnaeem/code/C027_Support/#5a4bc07f8bd0
+https://developer.mbed.org/users/iftaziz/code/C027_Support/#acb569d2a138
diff -r 4f3f7463e55f -r da5f84f19a24 Coap/coap_client.cpp
--- a/Coap/coap_client.cpp	Thu May 25 11:53:45 2017 +0000
+++ b/Coap/coap_client.cpp	Fri Aug 25 10:40:13 2017 +0000
@@ -75,7 +75,7 @@
     num = send(client->sd, buf, num);
     if (num < 0)
     {
-        printf("send error\n");
+        printf("send error\r\n");
         return -errno;
     }
 
@@ -174,7 +174,7 @@
     int num = 0;
     int ret = 0;
 
-    printf("Rejecting confirmable message from host %s and port %s\n", client->server_host, client->server_port);
+    printf("Rejecting confirmable message from host %s and port %s\r\n", client->server_host, client->server_port);
     coap_msg_create(&rej);
     ret = coap_msg_set_type(&rej, COAP_MSG_RST);
     if (ret < 0)
@@ -208,7 +208,7 @@
  */
 static int coap_client_reject_non(coap_client_t *client, coap_msg_t *msg)
 {
-    printf("Rejecting non-confirmable message from host %s and port %s\n", client->server_host, client->server_port);
+    printf("Rejecting non-confirmable message from host %s and port %s\r\n", client->server_host, client->server_port);
     return 0;
 }
 
@@ -223,7 +223,7 @@
  */
 static int coap_client_reject_ack(coap_client_t *client, coap_msg_t *msg)
 {
-    printf("Rejecting acknowledgement message from host %s and port %s\n", client->server_host, client->server_port);
+    printf("Rejecting acknowledgement message from host %s and port %s\r\n", client->server_host, client->server_port);
     return 0;
 }
 
@@ -238,7 +238,7 @@
  */
 static int coap_client_reject_reset(coap_client_t *client, coap_msg_t *msg)
 {
-    printf("Rejecting reset message from host %s and port %s\n", client->server_host, client->server_port);
+    printf("Rejecting reset message from host %s and port %s\r\n", client->server_host, client->server_port);
     return 0;
 }
 
@@ -289,7 +289,7 @@
     int num = 0;
     int ret = 0;
 
-    printf("Acknowledging confirmable message from host %s and port %s\n", client->server_host, client->server_port);
+    printf("Acknowledging confirmable message from host %s and port %s\r\n", client->server_host, client->server_port);
     coap_msg_create(&ack);
     ret = coap_msg_set_type(&ack, COAP_MSG_ACK);
     if (ret < 0)
@@ -370,11 +370,11 @@
     op_num = coap_client_check_options(resp);
     if (op_num != 0)
     {
-        printf("Found bad option number %u in message from host %s and port %s\n", op_num, client->server_host, client->server_port);
+        printf("Found bad option number %u in message from host %s and port %s\r\n", op_num, client->server_host, client->server_port);
         coap_client_reject(client, resp);
         return -EBADMSG;
     }
-    printf("Received acknowledgement and response from Server\n");
+    printf("Received acknowledgement and response from Server\r\n");
     return 0;
 }
 
@@ -398,11 +398,11 @@
 
     if (coap_msg_get_type(resp) == COAP_MSG_CON)
     {
-        printf("Received confirmable response from host %s and port %s\n", client->server_host, client->server_port);
+        printf("Received confirmable response from host %s and port %s\r\n", client->server_host, client->server_port);
         op_num = coap_client_check_options(resp);
         if (op_num != 0)
         {
-            printf("Found bad option number %u in message from host %s and port %s\n", op_num, client->server_host, client->server_port);
+            printf("Found bad option number %u in message from host %s and port %s\r\n", op_num, client->server_host, client->server_port);
             coap_client_reject(client, resp);
             return -EBADMSG;
         }
@@ -410,11 +410,11 @@
     }
     else if (coap_msg_get_type(resp) == COAP_MSG_NON)
     {
-        printf("Received non-confirmable response from host %s and port %s\n", client->server_host, client->server_port);
+        printf("Received non-confirmable response from host %s and port %s\r\n", client->server_host, client->server_port);
         op_num = coap_client_check_options(resp);
         if (op_num != 0)
         {
-            printf("Found bad option number %u in message from host %s and port %s\n", op_num, client->server_host, client->server_port);
+            printf("Found bad option number %u in message from host %s and port %s\r\n", op_num, client->server_host, client->server_port);
             coap_client_reject(client, resp);
             return -EBADMSG;
         }
@@ -445,7 +445,7 @@
     int starttimeout,endtimeout;
 
     /* wait for a separate response to a confirmable request */
-    printf("Expecting response from host %s and port %s\n", client->server_host, client->server_port);
+    printf("Expecting response from host %s and port %s\r\n", client->server_host, client->server_port);
     while (1)
     {
         starttimeout = readseconds();
@@ -463,7 +463,7 @@
             endtimeout = readseconds();
             if((endtimeout - starttimeout) > COAP_CLIENT_RESP_TIMEOUT_SEC)
             {
-                printf("Timeout no data received\n");  
+                printf("Timeout no data received\r\n");  
                 return -1;  
             }
         }
@@ -473,7 +473,7 @@
             if (coap_msg_get_type(resp) == COAP_MSG_ACK)
             {
                 /* message deduplication */
-                printf("Received duplicate acknowledgement from host %s and port %s\n", client->server_host, client->server_port);
+                printf("Received duplicate acknowledgement from host %s and port %s\r\n", client->server_host, client->server_port);
                 continue;
             }
             else if (coap_msg_get_type(resp) == COAP_MSG_RST)
@@ -496,7 +496,7 @@
             return ret;
         }
     }
-    return 0;
+    //return 0;
 }
 
 /**
@@ -542,7 +542,7 @@
             endtimeout = readseconds();
             if((endtimeout - starttimeout) > doubletimeout)
             {
-                printf("Timeout no data received after %d seconds\n",endtimeout - starttimeout);
+                printf("Timeout no data received after %d seconds\r\n",endtimeout - starttimeout);
 
                 if (client->num_retrans >= COAP_CLIENT_MAX_RETRANSMIT)
                 {
@@ -571,7 +571,7 @@
                 if (coap_msg_is_empty(resp))
                 {
                     /* received ack message, wait for separate response message */
-                    printf("Received acknowledgement from host %s and port %s\n", client->server_host, client->server_port);
+                    printf("Received acknowledgement from host %s and port %s\r\n", client->server_host, client->server_port);
                     return coap_client_exchange_sep(client, req, resp);
                 }
                 else if (coap_client_match_token(req, resp))
@@ -606,7 +606,7 @@
             return ret;
         }
     }
-    return 0;
+    //return 0;
 }
 
 /**
@@ -629,7 +629,7 @@
     int ret = 0;
     int starttimeout,endtimeout;
 
-    printf("Expecting response from host %s and port %s\n", client->server_host, client->server_port);
+    printf("Expecting response from host %s and port %s\r\n", client->server_host, client->server_port);
     while (1)
     {
         starttimeout = readseconds();
@@ -647,7 +647,7 @@
             endtimeout = readseconds();
             if((endtimeout - starttimeout) > COAP_CLIENT_RESP_TIMEOUT_SEC)
             {
-                printf("Timeout no data received\n");    
+                printf("Timeout no data received\r\n");    
                 return -1;
             }
         }
@@ -674,7 +674,7 @@
             return ret;
         }
     }
-    return 0;
+    //return 0;
 }
 
 int coap_client_exchange(coap_client_t *client, coap_msg_t *req, coap_msg_t *resp)
@@ -735,9 +735,7 @@
  *
  *  @returns Test result
  */
-test_result_t test_exchange_func(char* buf,int buf_len,char* returncode)
-{
-    printf("----------------------------------------\n");
+test_result_t test_exchange_func(char* buf,int buf_len,char* returncode){
     test_result_t result = PASS;
     coap_client_t client = {0};
     coap_msg_t resp = {0};
@@ -796,7 +794,7 @@
         //printf("exchange function\n");
         if (ret != PASS)
         {
-            printf("exchange function fail\n");
+            printf("exchange function fail\r\n");
             coap_msg_destroy(&resp);
             coap_msg_destroy(&req);
             coap_client_destroy(&client);
@@ -846,20 +844,20 @@
     char *token = NULL;
     //char *val = NULL;
 
-    printf("%s\n", str);
-    printf("ver:         0x%02x\n", coap_msg_get_ver(msg));
-    printf("type:        0x%02x\n", coap_msg_get_type(msg));
-    printf("token_len:   %d\n", coap_msg_get_token_len(msg));
-    printf("code_class:  %d\n", coap_msg_get_code_class(msg));
-    printf("code_detail: %d\n", coap_msg_get_code_detail(msg));
-    printf("msg_id:      0x%04x\n", coap_msg_get_msg_id(msg));
+    printf("%s\r\n", str);
+    printf("ver:         0x%02x\r\n", coap_msg_get_ver(msg));
+    printf("type:        0x%02x\r\n", coap_msg_get_type(msg));
+    printf("token_len:   %d\r\n", coap_msg_get_token_len(msg));
+    printf("code_class:  %d\r\n", coap_msg_get_code_class(msg));
+    printf("code_detail: %d\r\n", coap_msg_get_code_detail(msg));
+    printf("msg_id:      0x%04x\r\n", coap_msg_get_msg_id(msg));
     printf("token:      ");
     token = coap_msg_get_token(msg);
     for (i = 0; i < coap_msg_get_token_len(msg); i++)
     {
         printf(" 0x%02x", (unsigned char)token[i]);
     }
-    printf("\n");
+    printf("\r\n");
     /*op = coap_msg_get_first_op(msg);
     while (op != NULL)
     {
@@ -889,11 +887,14 @@
         }
         returncode[i] = '\0';
     }
-    printf("\n");
+    printf("\r\n");
     
-    printf("payload_len: %zu\n", coap_msg_get_payload_len(msg));
+    printf("payload_len: %zu\r\n", coap_msg_get_payload_len(msg));
 }
 //----------------------------------------------------------------------------------------------------------------------
+
+
+
 //----------------------------------------------------------------------------------------------------------------------
 /**
  *  @brief Populate a request message with details from a test request message structure
@@ -910,19 +911,19 @@
     ret = coap_msg_set_type(req, test_req->type);
     if (ret < 0)
     {
-        printf("Error  : %s\n",strerror(-ret));
+        printf("Error  : %s\r\n",strerror(-ret));
         return FAIL;
     }
     ret = coap_msg_set_code(req, test_req->code_class, test_req->code_detail);
     if (ret < 0)
     {
-        printf("Error  : %s\n",strerror(-ret));
+        printf("Error  : %s\r\n",strerror(-ret));
         return FAIL;
     }
     ret = coap_msg_add_op(req, test_req->ops.num, test_req->ops.len, test_req->ops.val);
     if (ret < 0)
     {
-        printf("Error  : %s\n",strerror(-ret));
+        printf("Error  : %s\r\n",strerror(-ret));
         return FAIL;
     }
     if (test_req->payload)
@@ -930,7 +931,7 @@
         ret = coap_msg_set_payload(req, test_req->payload, test_req->payload_len);
         if (ret < 0)
         {
-            printf("Error  : %s\n",strerror(-ret));
+            printf("Error  : %s\r\n",strerror(-ret));
             return FAIL;
         }
     }
@@ -956,14 +957,14 @@
     result = populate_req(test_req, req);
     if (result != PASS)
     {
-        printf("populate_req FAIL\n");
+        printf("populate_req FAIL\r\n");
         return result;
     }
     ret = coap_client_exchange(client, req, resp);
     if (ret < 0)
     {
-        printf("coap_client_exchange FAIL\n");
-        printf("Error  : %s\n",strerror(-ret));
+        printf("coap_client_exchange FAIL\r\n");
+        printf("Error  : %s\r\n",strerror(-ret));
         return FAIL;
     }
     //printf("coap_client_exchange PASS\n");
@@ -1008,19 +1009,12 @@
  *
  *  @returns Test result
  */
-test_result_t check_resp(test_coap_client_msg_t *test_resp, coap_msg_t *resp)
-{
+test_result_t check_resp(test_coap_client_msg_t *test_resp, coap_msg_t *resp){
     if (test_resp->type != coap_msg_get_type(resp))
-    {
         return FAIL;
-    }
     if (test_resp->code_class != coap_msg_get_code_class(resp))
-    {
         return FAIL;
-    }
     if (test_resp->code_detail != coap_msg_get_code_detail(resp))
-    {
         return FAIL;
-    }
     return PASS;
 }
diff -r 4f3f7463e55f -r da5f84f19a24 drv_GNSS/drv_C027_GNSS.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/drv_GNSS/drv_C027_GNSS.cpp	Fri Aug 25 10:40:13 2017 +0000
@@ -0,0 +1,101 @@
+#include "drv_C027_GNSS.h"
+#include "MDM.h"
+#include "C027_api.h"
+#include <ctype.h>
+
+const char* findNmeaItemPos(int ix, const char* start, const char* end)
+{
+    // find the start
+    for (; (start < end) && (ix > 0); start ++)
+    {
+        if (*start == ',')
+            ix --;
+    }
+    // found and check bounds
+    if ((ix == 0) && (start < end) && 
+        (*start != ',') && (*start != '*') && (*start != '\r') && (*start != '\n'))
+        return start;
+    else 
+        return NULL;
+}
+
+void setNmeaSentenses( void ){
+    /*
+    prtNmea.printf("$PUBX,40,RMC,0,0,0,0,0,0*47\r\n");      //Turn OFF GPRMC string
+    wait(1);
+    prtNmea.printf("$PUBX,40,RMC,0,0,0,0,0,0*47\r\n");      //Turn OFF GPRMC string
+    wait(1);
+    prtNmea.printf("$PUBX,40,GSV,0,0,0,0,0,0*59\r\n");      //Turn OFF GPGSV string
+    wait(1);
+    prtNmea.printf("$PUBX,40,GSA,0,0,0,0,0,0*4E\r\n");      //Turn OFF GPGSA string
+    wait(1);
+    prtNmea.printf("$PUBX,40,VTG,0,0,0,0,0,0*5E\r\n");      //Turn OFF GPVTG string
+    wait(1);
+    prtNmea.printf("$PUBX,40,GGA,0,0,0,0,0,0*5A\r\n");      //Turn OFF GPVTG string
+    wait(1);
+    */
+}
+
+bool getNmeaItem(int ix, char* buf, int len, double& val){
+    char* end = &buf[len];
+    const char* pos = findNmeaItemPos(ix, buf, end);
+    // find the start
+    if (!pos)
+        return false;
+    val = strtod(pos, &end);
+    // restore the last character
+    return (end > pos);
+}
+
+bool getNmeaItem(int ix, char* buf, int len, int& val, int base /*=10*/){
+    char* end = &buf[len];
+    const char* pos = findNmeaItemPos(ix, buf, end);
+    // find the start
+    if (!pos)
+        return false;
+    val = (int)strtol(pos, &end, base);
+    return (end > pos);
+}
+
+bool getNmeaItem(int ix, char* buf, int len, char& val)
+{
+    const char* end = &buf[len];
+    const char* pos = findNmeaItemPos(ix, buf, end);
+    // find the start
+    if (!pos)
+        return false;
+    // skip leading spaces
+    while ((pos < end) && isspace(*pos))
+        pos++;
+    // check bound
+    if ((pos < end) && 
+        (*pos != ',') && (*pos != '*') && (*pos != '\r') && (*pos != '\n'))
+    {
+        val = *pos;
+        return true;
+    }
+    return false;
+}
+
+bool getNmeaAngle(int ix, char* buf, int len, double& val)
+{
+    char ch;
+    if (getNmeaItem(ix,buf,len,val) && getNmeaItem(ix+1,buf,len,ch) && 
+        ((ch == 'S') || (ch == 'N') || (ch == 'E') || (ch == 'W')))
+    {
+        val *= 0.01;
+        int i = (int)val;
+        val = (val - i) / 0.6 + i;
+        if (ch == 'S' || ch == 'W')
+            val = -val;
+        return true;
+    }
+    return false;
+}
+
+void arrayInit2Zero(char* buffer,int length)
+{
+    for(int i= 0;i<length;i++)
+        buffer[i] = 0;
+}
+
diff -r 4f3f7463e55f -r da5f84f19a24 drv_GNSS/drv_C027_GNSS.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/drv_GNSS/drv_C027_GNSS.h	Fri Aug 25 10:40:13 2017 +0000
@@ -0,0 +1,16 @@
+#ifndef C027GPS_H
+#define C027GPS_H
+
+#include "drv_C027_GNSS.h"
+#include <ctype.h>
+
+const char* findNmeaItemPos(int ix, const char* start, const char* end);
+bool getNmeaItem(int ix, char* buf, int len, double& val);
+bool getNmeaItem(int ix, char* buf, int len, int& val, int base /*=10*/);
+bool getNmeaItem(int ix, char* buf, int len, char& val);
+bool getNmeaAngle(int ix, char* buf, int len, double& val);
+void arrayInit2Zero(char* buffer,int length);
+void setNmeaSentenses( void );
+
+#endif
+
diff -r 4f3f7463e55f -r da5f84f19a24 main.cpp
--- a/main.cpp	Thu May 25 11:53:45 2017 +0000
+++ b/main.cpp	Fri Aug 25 10:40:13 2017 +0000
@@ -1,3 +1,4 @@
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 #include "mbed.h"
 #include <stdlib.h>
 #include <stdio.h>
@@ -9,270 +10,294 @@
 #include "main.h"
 #include "coap_msg.h"
 #include "coap_client.h"
-    
+#include "drv_C027_GNSS.h"
+#include "C027_api.h"
+
 extern "C" {
-    
-    #include "rtc_api.h"
+#include "rtc_api.h"
 }
-//
-//----------------------------------------------------------------------------------------------------------------------
-#define SIMPIN          "1922"
-#define APN             NULL
-#define USERNAME        NULL
-#define PASSWORD        NULL 
-//----------------------------------------------------------------------------------------------------------------------
-int pport = 5685;
-const char* host = "coap.me";
-//----------------------------------------------------------------------------------------------------------------------
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+#define     APN                     "giffgaff.com"
+#define     USERNAME                "giffgaff"
+#define     PASSWORD                NULL
+
+//#define     APN                     "jtm2m"
+//#define     USERNAME                NULL
+//#define     PASSWORD                NULL
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+#define     SIMPIN                  ""
+#define     SIZE_BUFF_RX_GPS        128
+#define     SIZE_BUFF_TX_ASSETS     256
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+char                    ucReturnCode[10], caLssFinalAssetMsg[SIZE_BUFF_TX_ASSETS], buffer[SIZE_BUFF_RX_GPS], bytRxGps0=0, bytRxGps1=0, bytRxGps2=0;
+unsigned long           ulGPSRxCntr=0, ulGPSTxCntr=0, ulGSMRxCntr=0, ulGSMTxCntr=0, ulTotalRxCntr=0, ulTotalTxCntr=0;
+double                  dLatitude=0.0, dLongitude=0.0, dBtteryLevel=0.0;
+
+Serial                  prtGps( GPSTXD,GPSRXD );
+MDMSerial               mdm;
+MDMSerial*              gmdm=&mdm;
+
 int                     ssocket;
 MDMParser::IP           ipp;
-MDMSerial*              gmdm = NULL;
-DigitalOut              myled(LED);
-unsigned long           ulGPSRxCntr=0, ulGPSTxCntr=0 ;      //-- Asset
-unsigned long           ulGSMRxCntr=0, ulGSMTxCntr=0 ;      //-- Asset
-unsigned long           ulTotalRxCntr=0,ulTotalTxCntr=0;    //-- Asset
-char                    caLssFinalAssetMsg[150];
-double                  dLatitude=31.47691,dLongitude=74.34259;
+DigitalOut              myled( LED );
+AnalogIn                ain( A0 );
 static unsigned int     uiWakeupCounter=0,uiWatchdogCounter=0;
-unsigned char           ucBtteryLevel=0;
-MDMParser::DevStatus    devStatus={};
-
-bool                    bSendingDataFlag = true;
-bool                    bSleepModeFlag = true;
-//bool                  bHeartBeatFlag = false;
-//bool                  bAlarmIntervalFlag = true;
+Ethernet                eth;
+DigitalIn               PinDriftLoc(P1_20);
+DigitalIn               PinWakeupCntr(P1_23);
+DigitalIn               PinNoTransmission(P2_11);
 
-//unsigned int          uiAlarmIntervalCounter = 0;
-//unsigned int          uiHeartBeatCounter = 0;
+MDMParser::DevStatus    devStatus= {};
+bool                    bSleepModeFlag=true, bSendingDataFlag=true, extintrupt=false;
+int                     pport = 5685, alarmtime = 1495698480;//2017-3-29-11:56:7
+const char*             host = "coap.me";
+unsigned int            uiNormalSleepTime = 60, uiHeartBeatSleepTime = 3600;
 
-char ucReturnCode[10];     //Return code array
-int alarmtime = 1495698480;//2017-3-29-11:56:7
-//----------------------------------------------------------------------------------------------------------------------
-//----------------------------------------------------------------------------------------------------------------------
-/*
-void timer0_init(int sec)
-{
-    LPC_SC->PCONP |=1<1;            //timer0 power on
-    LPC_TIM0->MR0 = sec * 23980000; //1sec * sec
-    LPC_TIM0->MCR = 1;              //interrupt and reset control
-                                    //3 = Interrupt & reset timer0 on match
-                                    //1 = Interrupt only, no reset of timer0
-    NVIC_EnableIRQ(TIMER0_IRQn);    //enable timer0 interrupt
-    LPC_TIM0->TCR = 1;              //enable Timer0
-    //printf("Done timer_init\n\r");
-}
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
-extern "C" void TIMER0_IRQHandler (void)
-{
-    if((LPC_TIM0->IR & 0x01) == 0x01)   // if MR0 interrupt, proceed
-    {
-        LPC_TIM0->IR |= 1 << 0;         // Clear MR0 interrupt flag
-        printf("Timer 0 interrupt generated 10 sec\r\n");
-        LPC_TIM0->TCR = 0; 
-    }
-}
-*/
-//----------------------------------------------------------------------------------------------------------------------
-//----------------------------------------------------------------------------------------------------------------------
-
-void rtc_setup(time_t t, unsigned int sec)
-{
-    struct tm *timeinfo = localtime(&t);
-    unsigned int min;
-    
-    rtc_init();
-
-    rtc_write(alarmtime);//2017-3-29-11:56:7
-    
-    //min = sec/60;
-    //sec = sec%60;
-     //set alarm time
-    LPC_RTC->ALSEC = timeinfo->tm_sec+sec;//10 sec
-    //LPC_RTC->ALMIN = timeinfo->tm_min+min;//10 sec
-
-    LPC_RTC->AMR &= (~(1 << 0)) & 0xFF;
-
-    LPC_RTC->ILR = (1 << 0) | (1 << 1);
-    
-    printf("RTC Alarm Time Set\r\n");
-
-    NVIC_EnableIRQ(RTC_IRQn);
-
-    do 
-    {
-        LPC_RTC->CCR |= (1 << 0);
-    } 
-    while ((LPC_RTC->CCR & (1 << 0)) == 0);
-
-    printf("RTC setup successfully\r\n");
-}
-
-extern "C" void RTC_IRQHandler (void)
-{
-    // Check for alarm match
-    if (LPC_RTC->ILR & (1 << 1))
-    {
-        LPC_RTC->ILR = (1 << 1);
-        printf("RTC interrupt generated 10 sec\r\n");
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+int main( void ){
+    //--   
+    us_ticker_init();
+    c027_gps_powerOn();
+    //--
+    while( true ){
+        wait( 5 );
+        if( initialize_modem() ){
+            if( bSendingDataFlag ){
+                CollectNmeaMessagesProcess();
+                dBtteryLevel = ain.read() * 4.030 * 3.3;                                    //-- ASSET
+                //--
+                //-- Ethernet Interface
+                if( eth.link() ){
+                    printf("INFO> Ethernet attached\r\n");
+                    ulGSMTxCntr += 1000; 
+                }
+                else{
+                    printf("INFO> Ethernet de-ttached\r\n");
+                }
+                //--
+                if( PinWakeupCntr ){
+                    printf("INFO> PinWakeupCntr\r\n");    
+                    uiWakeupCounter += 10;
+                }
+                else{
+                }
+                //--
+                CreateFinalAssetMessage();
+            } 
+            else{
+                sprintf( caLssFinalAssetMsg, "$AQLSS,02,%s,%s*xx\r\n",devStatus.imei,devStatus.imsi);
+                printf( "MCU> HeartBeat: %s\r\n", caLssFinalAssetMsg );
+            }
+            //--     
+            if( PinNoTransmission ){
+                printf("INFO> PinNoTransmission\r\n");
+            }
+            else{
+                SendViaModuleSocket();
+            }
+            //--
+            if( bSleepModeFlag )
+                LssSleepMode();
+        }
     }
 }
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
-//----------------------------------------------------------------------------------------------------------------------
-//----------------------------------------------------------------------------------------------------------------------
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+void rtc_setup( time_t t, unsigned int sec ){
+    struct tm *timeinfo = localtime( &t );
+    rtc_init();
+    rtc_write( alarmtime );//2017-3-29-11:56:7
+
+    LPC_RTC->ALSEC = timeinfo->tm_sec + sec;            //-- Set alarm time
 
-int main(void)
-{    
-    int ret;
-    char buf[150] = "";
+    LPC_RTC->AMR &= (~(1 << 0)) & 0xFF;
+    LPC_RTC->ILR = (1 << 0) | (1 << 1);
+    NVIC_EnableIRQ( RTC_IRQn );
+    do{
+        LPC_RTC->CCR |= (1 << 0);
+    } while( (LPC_RTC->CCR&(1<<0))==0 );
+}
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+extern "C" void RTC_IRQHandler( void ){
+    if( LPC_RTC->ILR & (1<<1) ) 
+        LPC_RTC->ILR = (1<<1);
+}
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+void LssSleepMode( void ){
+    /*
+    unsigned int sleeptime = 0;
+    if( bSendingDataFlag )      sleeptime = uiNormalSleepTime;
+    else                        sleeptime = uiHeartBeatSleepTime;
+
+    sleeptime = sleeptime/50;
 
-    // Create the GPS object
-#if 1   // use GPSI2C class
-    GPSI2C gps;
-#else   // or GPSSerial class 
-    GPSSerial gps; 
-#endif
+    printf("MCU> Entering Sleep Mode\r\n");
+    int i=0;
+    for( i=0;i<sleeptime;i++ ){
+        rtc_setup(alarmtime,50);
+        hal_sleep();
+        if( extintrupt )
+            break;
+    }
+    extintrupt = false;
+    printf( "MCU> Exit Sleep Mode after %d seconds\r\n", i*50 );
+    myled = !myled;
+    uiWakeupCounter++;
+    */
+    
+    //unsigned int sleeptime = 0;
+    //if( bSendingDataFlag )      sleeptime = uiNormalSleepTime;
+    //else                        sleeptime = uiHeartBeatSleepTime;
+
+    //sleeptime = sleeptime/50;
 
-    MDMSerial mdm;            
-    gmdm = &mdm;
-    us_ticker_init();
+    printf("MCU> Entering Sleep Mode\r\n");
+    //int i=0;
+    //for( i=0;i<sleeptime;i++ ){
+        rtc_setup( alarmtime,30 );
+        hal_sleep();
+        //if( extintrupt )
+        //    break;
+    //}
+    //extintrupt = false;
+    printf( "MCU> Exit Sleep Mode after %d seconds\r\n", 30 );
+    myled = !myled;
+    uiWakeupCounter++;
     
+}
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+void CollectNmeaMessagesProcess( void ){
+    //--
+    int bufferIndex = 0;
+    bool bIsStartCollecting=false, bIsPktRxDone=false;
+    while( bufferIndex<SIZE_BUFF_RX_GPS && !bIsPktRxDone ) {
+        if( prtGps.readable() ) {
+            bytRxGps0 = bytRxGps1;
+            bytRxGps1 = bytRxGps2;
+            bytRxGps2 = prtGps.getc();
 
-while(1)
-{
-    wait(5);
-    //if(bAlarmIntervalFlag || bHeartBeatFlag)
-    //{ 
-        if(initialize_modem())
-        {
-            if(bSendingDataFlag)//&& bAlarmIntervalFlag)
-            {
-                //bAlarmIntervalFlag = false;
-                while( (ret = gps.getMessage(buf, sizeof(buf))) > 0)
-                {
-                    int len = LENGTH(ret);
-                    ulGPSRxCntr = len;
-                    if( (PROTOCOL(ret)==GPSParser::NMEA) && (len > 6) )
-                    {
-                        // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS
-                        if( (buf[0]=='$') || buf[1]=='G' )
-                        {
-                            #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2]))
-                            if( _CHECK_TALKER("GLL") ) 
-                            {
-                                char ch;
-                                if( gps.getNmeaAngle(1,buf,len,dLatitude) && gps.getNmeaAngle(3,buf,len,dLongitude) && gps.getNmeaItem(6,buf,len,ch) && ch == 'A' ) 
-                                {
-                                    printf( "GPS Location: %.5f %.5f\r\n", dLatitude, dLongitude );
-                                    break;
-                                    //sprintf(link, "I am here!\n" "https://maps.google.com/?q=%.5f,%.5f", la, lo);
-                                } else 
-                                {
-                                    dLatitude = 0.0;
-                                    dLongitude = 0.0;
-                                }
-                            } 
-                            //else if(_CHECK_TALKER("GGA") || _CHECK_TALKER("GNS") ) 
-                            //{
-                                //double a = 0;
-                                //if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m]
-                                //    printf("GPS Altitude: %.1f\r\n", a);
-                            //} 
-                            //else if( _CHECK_TALKER("VTG") ) 
-                            //{
-                                //double s = 0;
-                                //if( gps.getNmeaItem(7,buf,len,s) ) // speed [km/h]
-                                //    printf( "GPS Speed: %.1f\r\n", s );
-                            //}
-                        }
-                    } 
-                    else 
-                    {
-                        printf("GPS Packet error\r\n");
-                        dLatitude = 0.0;
-                        dLongitude = 0.0;
-                    }
-                }
-                memset(caLssFinalAssetMsg, '\0', sizeof(caLssFinalAssetMsg));
-                sprintf( caLssFinalAssetMsg, "$AQLSS,01,%s,%s,%.5f,%.5f,%lu,%lu,%d,%d,%d*xx", 
-                                            devStatus.imei,devStatus.imsi, dLatitude,dLongitude,ulGPSRxCntr,ulGPSTxCntr,uiWakeupCounter,uiWatchdogCounter,ucBtteryLevel);
-                printf( "AssetMsg: %s\r\n", caLssFinalAssetMsg );
+            if( bytRxGps0=='*' && bIsStartCollecting ) {
+                bIsStartCollecting = false;
+                bIsPktRxDone = true;
+                ulGPSRxCntr += bufferIndex;
+                break;
+            } else if( bIsStartCollecting ){
+                buffer[bufferIndex++] = bytRxGps2;
+            }
+            else if( bytRxGps0=='G' && bytRxGps1=='L' && bytRxGps2=='L' ) {
+                bIsStartCollecting = true;
+                bufferIndex = 0;
+                ulGPSRxCntr += 3;    //-- iazi UK
             }
-            else // else if(bHeartBeatFlag)
-            {
-                //bHeartBeatFlag = false;
-                sprintf( caLssFinalAssetMsg, "$AQLSS,02,%s,%s*xx",devStatus.imei,devStatus.imsi);
-                printf( "HeartBeat: %s\r\n", caLssFinalAssetMsg );
-            }
-            
-            ssocket = mdm.socketSocket(MDMParser::IPPROTO_UDP, pport);
-            if( ssocket >= 0 ) 
-            {
-                mdm.socketSetBlocking(ssocket, 10);
-                ipp = 0x97092263;//0x052753CE;
-                if (PASS == test_exchange_func(caLssFinalAssetMsg,strlen(caLssFinalAssetMsg),ucReturnCode))
-                {
-                    printf("\r\n-----------------<pass>-----------------\n");           
-                }                
-                else
-                    printf("\r\n-----------------<fail>-----------------\n");
+        }
+    }
+    //--
+    if( getNmeaAngle( 1,buffer,SIZE_BUFF_RX_GPS,dLatitude ) && getNmeaAngle( 3,buffer,SIZE_BUFF_RX_GPS,dLongitude ) ){
+        printf( "NMEA> %.5f %.5f\r\n", dLatitude, dLongitude );
+        //--
+        if( PinDriftLoc ){
+            printf("INFO> PinDriftLoc=1 / Tempering Lat/Long Values\r\n");
+            dLatitude  = 52.2120475;
+            dLongitude = -0.0728461;
+        }
+        else{
+            printf("INFO> PinDriftLoc=0\r\n");
+        }
+    }
+    for( int i=0; i<SIZE_BUFF_RX_GPS; i++ )                         //-- EMPTY BUFFER
+        buffer[i] = 0x00;
+}
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
-                mdm.socketFree(ssocket);
-                responce_checker(); 
-            }
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+void CreateFinalAssetMessage( void ){
+    //--
+    ulTotalRxCntr = ulGPSRxCntr + ulGSMRxCntr;                                  //-- ASSET
+    ulTotalTxCntr = ulGPSTxCntr + ulGSMTxCntr;                                  //-- ASSET
+    //--
+    printf( "INFO> ulGPSRxCntr:%d\r\n", ulGPSRxCntr );
+    printf( "INFO> ulGSMRxCntr:%d\r\n", ulGSMRxCntr );
+    printf( "INFO> ulGPSTxCntr:%d\r\n", ulGPSTxCntr );
+    printf( "INFO> ulGSMTxCntr:%d\r\n", ulGSMTxCntr );
+    //--
+    memset(caLssFinalAssetMsg, '\0', sizeof(caLssFinalAssetMsg));
+    sprintf( caLssFinalAssetMsg, "$AQLSS,01,%s,%s,%.5f,%.5f,%lu,%lu,%d,%d,%.1f*xx\r\n", devStatus.imei,devStatus.imsi, dLatitude,dLongitude,ulTotalRxCntr,ulTotalTxCntr,uiWakeupCounter,uiWatchdogCounter,dBtteryLevel);
+    printf( "LSS> %s\r\n", caLssFinalAssetMsg );
+}
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
-            mdm.disconnect();
-            /*mdm.powerOff();*/
-        
-            if(bSleepModeFlag)
-            {
-                 rtc_setup(alarmtime,10);
-                printf("Enter Sleep Mode\r\n");
-                hal_sleep();
-                printf("Exit Sleep Mode\r\n");
-                myled = !myled;
-                uiWakeupCounter++;
-            }
-            
-            /*if(bSendingDataFlag)
-                wait( 10 );
-            else
-                wait( 60 );
-            myled = !myled;*/
-        }
-    //}
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+void SendViaModuleSocket( void ){
+    ssocket = mdm.socketSocket(MDMParser::IPPROTO_UDP, pport);
+    if( ssocket >= 0 ) {
+        mdm.socketSetBlocking(ssocket, 10);
+        ipp = 0x97092263;//0x052753CE;
+        if (PASS == test_exchange_func(caLssFinalAssetMsg,strlen(caLssFinalAssetMsg),ucReturnCode))
+            printf("RESULT> -----------------< PASS >-----------------\r\n");
+        else
+            printf("RESULT> -----------------< FAIL >-----------------\r\n");
+
+        mdm.socketFree(ssocket);
+        responce_checker();
+    }
+    mdm.disconnect();
+    //-- mdm.powerOff();
+
 }
-    return 0;
-}
-//----------------------------------------------------------------------------------------------------------------------
-//----------------------------------------------------------------------------------------------------------------------l
-int initialize_modem(void)
-{
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+int initialize_modem( void ){
+    //--
+    //mdm.setDebug(4);
+    MDMParser::NetStatus netStatus = {};
+
     bool mdmOk = gmdm->init(SIMPIN, &devStatus);
-    gmdm->dumpDevStatus(&devStatus);
-    if (mdmOk)
-    {
-        MDMParser::IP ipp = gmdm->join(APN,USERNAME,PASSWORD);  // join the internet connection 
-        if (ipp == NOIP)
-        {
-            printf( "AQ-LSS: Modem NOT initialized\n" );
+    gmdm->dumpDevStatus( &devStatus );
+    
+    if (mdmOk) {
+        mdmOk = mdm.registerNet(&netStatus);
+        mdm.dumpNetStatus(&netStatus);
+    }    
+    
+    if( mdmOk ) {
+        MDMParser::IP ipp = gmdm->join( APN,USERNAME,PASSWORD );  // join the internet connection
+        if( ipp==NOIP ) {
+            printf( "LSS> Modem NOT Initialized\r\n" );
             gmdm->powerOff();
             return 0;
-        }
-        else
-        {
-            printf( "AQ-LSS: Modem initialized\n" );  
+        } else {
+            printf( "LSS> Modem Initialized\r\n" );
             //ussd_send();
-            return 1;   
+            return 1;
         }
-    }
-    else
-    {
+    } else {
         return 0;
     }
 }
-int recv(int socket_d, char *buf, int len)
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+
+
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+int recv( int socket_d, char *buf, int len )
 {
     int ret = gmdm->socketRecvFrom(ssocket, &ipp, &pport, buf, len);
+    //ulGSMRxCntr += len;               //-- iazi UK increasing very fast sometimes.
     return ret;
 }
 
@@ -281,6 +306,7 @@
     ipp = gmdm->gethostbyname(host);
     ipp = 0x97092263;//0x052753CE;
     int ret = gmdm->socketSendTo(ssocket, ipp, pport, (char*)buf, len);
+    ulGSMTxCntr += len;
     return ret;
 }
 
@@ -289,65 +315,34 @@
     unsigned int seconds = us_ticker_read()/1000000;
     return seconds;
 }
-
-/*
-void ussd_send(void)
-{
-    int ret;
-    char buffer[512];
-    const char* ussd = "*222#";//"*100#";
-    printf("Ussd Send Command %s\r\n", ussd);
-    ret = gmdm->ussdCommand(ussd, buffer);
-    if (ret > 0) 
-       printf("Ussd Got Answer: \"%s\"\r\n", buffer);
-}
-*/
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
-void responce_checker(void)
-{    
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+void responce_checker( void ){
     if(!strcmp((const char*)ucReturnCode,"RC 000"))
-    {
-        printf("Acknowledged RC 000\r\n");
-    }
+        printf("COAP> Acknowledged RC 000\r\n");
     else if(!strcmp((const char*)ucReturnCode,"RC 001"))
-    {
-        printf("Acknowledged with Data RC 001\r\n");
-    }
-    else if(!strcmp((const char*)ucReturnCode,"RC 002"))
-    {
-        printf("Start Sending Data RC 002\r\n");
+        printf("COAP> Acknowledged with Data RC 001\r\n");
+    else if(!strcmp((const char*)ucReturnCode,"RC 002")) {
+        printf("COAP> Start Sending Data RC 002\r\n");
         bSendingDataFlag = true;
-    }
-    else if(!strcmp((const char*)ucReturnCode,"RC 003"))
-    {
-        printf("Stop Sending Data RC 003\r\n");
+    } else if(!strcmp((const char*)ucReturnCode,"RC 003")) {
+        printf("COAP> Stop Sending Data RC 003\r\n");
         bSendingDataFlag = false;
-    }
-    else if(!strcmp((const char*)ucReturnCode,"RC 004"))
-    {
-        printf("Enable Sleep mode RC 004\r\n");
+    } else if(!strcmp((const char*)ucReturnCode,"RC 004")) {
+        printf("COAP> Enable Sleep mode RC 004\r\n");
         bSleepModeFlag = true;
-    }
-    else if(!strcmp((const char*)ucReturnCode,"RC 005"))
-    {
-        printf("Disable Sleep mode RC 005\r\n");
+    } else if(!strcmp((const char*)ucReturnCode,"RC 005")) {
+        printf("COAP> Disable Sleep mode RC 005\r\n");
         bSleepModeFlag = false;
-    }
-    else if(!strcmp((const char*)ucReturnCode,"RC 006"))
-    {
-        printf("Reboot Client Return Code 006\r\n");
+    } else if(!strcmp((const char*)ucReturnCode,"RC 006")) {
+        printf("COAP> Reboot Client Return Code 006\r\n");
         NVIC_SystemReset();
-    }
-    else if(!strcmp((const char*)ucReturnCode,"EC 000")) 
-    {
-        printf("Error Code 000 Received Connection with web server failed\r\n");
-    }
+    } else if(!strcmp((const char*)ucReturnCode,"EC 000"))
+        printf("COAP> Error Code 000 Received Connection with web server failed\r\n");
     else if(!strcmp((const char*)ucReturnCode,"EC 001"))
-    {
-        printf("Error Code 001 Received Node IMEI is not registered on Server\r\n");
-    }
+        printf("COAP> Error Code 001 Received Node IMEI is not registered on Server\r\n");
     else if(!strcmp((const char*)ucReturnCode,"EC 002"))
-    {
-        printf("Error Code 002 Received Payload is corrupted\r\n");
-    }
+        printf("COAP> Error Code 002 Received Payload is corrupted\r\n");
 }
+//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
diff -r 4f3f7463e55f -r da5f84f19a24 main.h
--- a/main.h	Thu May 25 11:53:45 2017 +0000
+++ b/main.h	Fri Aug 25 10:40:13 2017 +0000
@@ -2,6 +2,8 @@
 #define MAIN_H
 
 #include <time.h>
+//#include "drv_C027_GNSS.h"
+#include "coap_msg.h"
 
 #define HOST             "5.39.83.206"//coap.me ip address//"127.0.0.1"         /**< Host address of the server */
 #define PORT             "5683"//coap.me port//"12436"                          /**< UDP port number of the server */
@@ -9,10 +11,10 @@
 #ifdef __cplusplus
 extern "C" {
 #endif
-#include "coap_msg.h"
+
 extern int recv(int socket_d, char *buf, int len);
 extern int send(int socket_d, char *buf, int len);
-extern int readseconds(void);
+extern int readseconds( void );
 
 #ifdef __cplusplus
 }
@@ -20,13 +22,16 @@
 
 int recv(int socket_d, char *buf, int len);
 int send(int socket_d, char *buf, int len);
-
-void ussd_send(void);
-void sleep_mode(void);
-void responce_checker(void);
+void ussd_send( void );
+void sleep_mode( void );
+void responce_checker( void );
+void collect_assets( void );
+void send_packet( void );
+int initialize_modem( void );
+void CreateFinalAssetMessage( void );
+void CollectNmeaMessagesProcess( void );
+void SendViaModuleSocket( void );
+void rtc_setup( time_t t, unsigned int sec );
+void LssSleepMode( void );
 
-void collect_assets(void);
-void send_packet(void);
-int initialize_modem(void);
 #endif
-
diff -r 4f3f7463e55f -r da5f84f19a24 mbed-dev.lib
--- a/mbed-dev.lib	Thu May 25 11:53:45 2017 +0000
+++ b/mbed-dev.lib	Fri Aug 25 10:40:13 2017 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/ranaumarnaeem/code/mbed-dev/#2dd56e6daeec
+https://developer.mbed.org/users/iftaziz/code/mbed-dev/#33361e55dd8c