Official reference client implementation for Cumulocity SmartREST on u-blox C027.

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Vincent Wochnik

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <stdio.h>
00002 
00003 #include "mbed.h"
00004 #include "rtos.h"
00005 #include "MDM.h"
00006 #include "GPS.h"
00007 
00008 #include "DeviceInfo.h"
00009 #include "Storage.h"
00010 #include "MbedAgent.h"
00011 #include "GPSTracker.h"
00012 #include "LCDDisplay.h"
00013 #include "logging.h"
00014 
00015 /**
00016  * SIM PIN. Null for no pin.
00017  */
00018 #define SIM_PIN NULL
00019 
00020 /**
00021  * SIM GPRS login data. Leave commented out for automatic setting.
00022  */
00023 //#define SIM_APN ""
00024 //#define SIM_USER ""
00025 //#define SIM_PASS ""
00026 
00027 MDMSerial* pMdm = NULL;
00028 //CYASSL_CTX *pCtx = NULL;
00029 
00030 static unsigned short getMNCLen(const char *imsi)
00031 {
00032     if (strncmp(imsi, "310", 3) != 0) // Non American ISMI
00033         return 2;
00034     else
00035         return 3;
00036 }
00037 
00038 static void enableDebug()
00039 {
00040         setLevel(A_DEBUG);
00041         if (pMdm)
00042             pMdm->setDebug(3);
00043 }
00044 
00045 static void disableDebug()
00046 {
00047        setLevel(A_WARNING);
00048        if (pMdm)
00049            pMdm->setDebug(0);
00050 }
00051 
00052 static void shutdown()
00053 {
00054 //    CyaSSL_CTX_free(pCtx);
00055 //    CyaSSL_Cleanup();
00056     pMdm->disconnect();
00057     pMdm->powerOff();
00058 }
00059 
00060 //static int send(CYASSL *ssl, char *buf, int size, void* ctx)
00061 //{
00062 //        int sockfd = *(int*)ctx;
00063 //        int ret = pMdm->socketSend(sockfd, buf, size);
00064 //        printf("[send](%d, %p): %d/%d\n", sockfd, ssl, ret, size);
00065 //        if (ret >= 0)
00066 //                return ret;
00067 //        else
00068 //                return CYASSL_CBIO_ERR_GENERAL;
00069 //}
00070 //
00071 //static int recv(CYASSL *ssl, char *buf, int maxSize, void* ctx)
00072 //{
00073 //        int sockfd = *(int*)ctx;
00074 //        int ret = pMdm->socketRecv(sockfd, buf, maxSize);
00075 //        printf("[recv](%d, %p): %d/%d\n", sockfd, ssl, ret, maxSize);
00076 //        if (ret >= 0)
00077 //                return ret;
00078 //        else
00079 //                return CYASSL_CBIO_ERR_GENERAL;
00080 //}
00081 
00082 int main()
00083 {
00084     LCDDisplay::inst().setLines("Mbed Agent V2.2", srHost);
00085 //    set_time(1256729737);
00086     MDMRtos<MDMSerial> mdm;
00087     pMdm = &mdm;
00088     InterruptIn joystickUp(A2);
00089     InterruptIn joystickDown(A3);
00090 
00091 //    CyaSSL_Init();
00092 //    pCtx = CyaSSL_CTX_new(CyaTLSv1_2_client_method());
00093 //    CyaSSL_Debugging_ON();
00094 //    if (pCtx == NULL) {
00095 //        shutdown();
00096 //        return 1;
00097 //    }
00098 //    CyaSSL_CTX_set_verify(pCtx, SSL_VERIFY_NONE, 0);
00099 //    CyaSSL_CTX_set_cipher_list(pCtx, "DHE-RSA-AES128-GCM-SHA256:ECDH-RSA-AES128-SHA:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA");
00100 //    CyaSSL_SetIORecv(pCtx, recv);
00101 //    CyaSSL_SetIOSend(pCtx, send);
00102 
00103     MDMParser::DevStatus devStatus;
00104     if (!mdm.init(SIM_PIN, &devStatus)) {
00105         LCDDisplay::inst().setLines("Modem Init Failure", "No SIM card found", "Or SIM has PIN");
00106         shutdown();
00107         return 2;
00108     }
00109     joystickUp.rise(&enableDebug);
00110     joystickDown.rise(&disableDebug);
00111     
00112     DigitalIn fireButton(D4);
00113     if (fireButton) {
00114         LCDDisplay::inst().setLines("Factory resetting...");
00115         Thread::wait(2000); // wait for Disk I/O to be ready
00116         if (resetCredential()) {
00117             LCDDisplay::inst().setLines("Reset Success");
00118         } else {
00119             LCDDisplay::inst().setLines("Reset Failure");
00120         }
00121         shutdown();
00122         return 0;
00123     }
00124     LCDDisplay::inst().setLines("Register Network...", "IMEI", devStatus.imei);
00125     if (!mdm.registerNet()) {
00126         LCDDisplay::inst().setLines("No Network Coverage");
00127         shutdown();
00128         return 3;
00129     }
00130 
00131     LCDDisplay::inst().setLines("Join Network");
00132 #ifdef SIM_APN
00133     if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) {
00134 #else
00135     if (mdm.join() == NOIP) {
00136 #endif
00137         char s[8] = "Unknown";
00138         const char *p = devStatus.imsi;
00139         if (p) {
00140             snprintf(s, sizeof(s), "%.*s-%.*s", 3, p, getMNCLen(p), p+3);
00141         }
00142         LCDDisplay::inst().setLines("Unknown APN Settting", "MCC-MNC:", s);
00143         shutdown();
00144         return 4;
00145     }
00146 
00147     {
00148         DeviceInfo deviceInfo(devStatus);
00149         MbedAgent agent(deviceInfo);
00150 
00151         LCDDisplay::inst().setLines("Agent Init");
00152         if (!agent.init()) {
00153             shutdown();
00154             return 5;
00155         }
00156         LCDDisplay::inst().setLines("Agent Run");
00157         int ret = 0;
00158         for (uint8_t tries = 3; tries; --tries) {
00159             ret = agent.run();
00160             if (ret == 0) {
00161                 char status[27];
00162                 snprintf(status, sizeof(status), "Tenant: %s", srTenant);
00163                 LCDDisplay::inst().setFirstLine(status);
00164                 agent.loop();
00165                 break;
00166             }
00167         }
00168         shutdown();
00169         return ret;
00170     }
00171 }