mbed 5.4 with sleep mode

Dependencies:   C027_Support mbed-dev

Fork of C027_SupportTest_coap by Umar Naeem

main.cpp

Committer:
ranaumarnaeem
Date:
2017-03-24
Revision:
34:d6ce8f961b8b
Parent:
33:e27f40fada64
Child:
37:43d48521d4d7

File content as of revision 34:d6ce8f961b8b:

#include "mbed.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include "GPS.h"
#include "MDM.h"
#include "main.h"

extern "C" {
    #include "coap_msg.h"
    #include "coap_client.h"
}

//----------------------------------------------------------------------------------------------------------------------
#define SIMPIN     "1922"
#define APN         NULL
#define USERNAME    NULL
#define PASSWORD    NULL 
//----------------------------------------------------------------------------------------------------------------------
int pport = 5683;//12436;//5683;
const char* host = "leshan.eclipse.org";
//----------------------------------------------------------------------------------------------------------------------
int ssocket;
MDMParser::IP ipp;
MDMSerial* gmdm = NULL;
Timer t;
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
int main(void)
{
    MDMSerial mdm;            
    gmdm = &mdm;       
    t.start();

    if(initialize_modem())
    {
        //ussd_send();  
            
        ssocket = mdm.socketSocket(MDMParser::IPPROTO_UDP, pport);
        if (ssocket >= 0)
        {
            mdm.socketSetBlocking(ssocket, 100000);
            ipp = gmdm->gethostbyname(host);
               //ipp = 0x97092263;//0x052753CE;    
            char payload[] = "heeeelooo Worldsgfsj";
            if (PASS == test_exchange_func(payload,sizeof(payload)-1))
                printf("-----------------<pass>-----------------\n");
            else
                printf("*****************[FAIL]*****************\n");
                
            mdm.socketFree(ssocket);
        }
        mdm.disconnect();
    }
    mdm.powerOff();
    t.stop();
    return 0;
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------

int recv(int socket_d, char *buf, int len)
{
    int ret = gmdm->socketRecvFrom(ssocket, &ipp, &pport, buf, len);
    return ret;
}

int send(int socket_d, char *buf, int len)
{
    ipp = gmdm->gethostbyname(host);
    //ipp = 0x97092263;//0x052753CE;
    int ret = gmdm->socketSendTo(ssocket, ipp, pport, (char*)buf, len);
    return ret;
}

int getseconds(void)
{
    return t.read();
}

int initialize_modem(void)
{
    MDMParser::DevStatus devStatus = {};
    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("Not able to join network\n");
            return 0;
        }
        else
        {
            return 1;   
        }
    }
    else
    {
        return 0;
    }
}

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);
}