![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
Diff: main.cpp
- Revision:
- 105:c61f0d62b625
- Parent:
- 104:fd3571349e5d
- Child:
- 106:fc5f25f0e0d5
--- a/main.cpp Fri May 08 14:50:43 2015 +0000 +++ b/main.cpp Mon May 11 18:10:44 2015 +0000 @@ -4,6 +4,7 @@ #include "rtos.h" #include "MDM.h" #include "GPS.h" +#include "cyassl/ssl.h" #include "DeviceInfo.h" #include "Storage.h" @@ -23,10 +24,11 @@ //#define SIM_APN "" //#define SIM_USER "" //#define SIM_PASS "" -//MDMRtos<MDMSerial> *pMdm; -MDMSerial* pMdm; -unsigned short getMNCLen(const char *imsi) +MDMSerial* pMdm = NULL; +//CYASSL_CTX *pCtx = NULL; + +static unsigned short getMNCLen(const char *imsi) { if (strncmp(imsi, "310", 3) != 0) // Non American ISMI return 2; @@ -34,22 +36,53 @@ return 3; } -void enableDebug() +static void enableDebug() { setLevel(A_DEBUG); if (pMdm) pMdm->setDebug(3); } -void disableDebug() +static void disableDebug() { setLevel(A_NONE); if (pMdm) pMdm->setDebug(-1); } +static void shutdown() +{ +// CyaSSL_CTX_free(pCtx); +// CyaSSL_Cleanup(); + pMdm->disconnect(); + pMdm->powerOff(); +} + +//static int send(CYASSL *ssl, char *buf, int size, void* ctx) +//{ +// int sockfd = *(int*)ctx; +// int ret = pMdm->socketSend(sockfd, buf, size); +// printf("[send](%d, %p): %d/%d\n", sockfd, ssl, ret, size); +// if (ret >= 0) +// return ret; +// else +// return CYASSL_CBIO_ERR_GENERAL; +//} +// +//static int recv(CYASSL *ssl, char *buf, int maxSize, void* ctx) +//{ +// int sockfd = *(int*)ctx; +// int ret = pMdm->socketRecv(sockfd, buf, maxSize); +// printf("[recv](%d, %p): %d/%d\n", sockfd, ssl, ret, maxSize); +// if (ret >= 0) +// return ret; +// else +// return CYASSL_CBIO_ERR_GENERAL; +//} + int main() { + LCDDisplay::inst().setLines("Mbed Agent V2.1rc3", srHost); set_time(1256729737); MDMRtos<MDMSerial> mdm; pMdm = &mdm; @@ -58,13 +91,23 @@ joystickUp.rise(&enableDebug); joystickDown.rise(&disableDebug); +// CyaSSL_Init(); +// pCtx = CyaSSL_CTX_new(CyaTLSv1_2_client_method()); +// CyaSSL_Debugging_ON(); +// if (pCtx == NULL) { +// shutdown(); +// return 1; +// } +// CyaSSL_CTX_set_verify(pCtx, SSL_VERIFY_NONE, 0); +// CyaSSL_CTX_set_cipher_list(pCtx, "DHE-RSA-AES128-GCM-SHA256:ECDH-RSA-AES128-SHA:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA"); +// CyaSSL_SetIORecv(pCtx, recv); +// CyaSSL_SetIOSend(pCtx, send); + MDMParser::DevStatus devStatus; - LCDDisplay::inst().setLines("Mbed Agent V2.1rc3", srHost); if (!mdm.init(SIM_PIN, &devStatus)) { LCDDisplay::inst().setLines("Modem Init Failure", "No SIM card found", "Or SIM has PIN"); - mdm.disconnect(); - mdm.powerOff(); - return 1; + shutdown(); + return 2; } DigitalIn fireButton(D4); @@ -75,16 +118,14 @@ LCDDisplay::inst().setLines("Reset Failure"); } Thread::wait(2000); - mdm.disconnect(); - mdm.powerOff(); + shutdown(); return 0; } aInfo("Main Thread: %p\r\n", Thread::gettid()); LCDDisplay::inst().setLines("Register Network...", "IMEI", devStatus.imei); if (!mdm.registerNet()) { LCDDisplay::inst().setLines("No Network Coverage"); - mdm.disconnect(); - mdm.powerOff(); + shutdown(); return 3; } @@ -100,8 +141,7 @@ snprintf(s, sizeof(s), "%.*s-%.*s", 3, p, getMNCLen(p), p+3); } LCDDisplay::inst().setLines("Wrong APN Settting", "MCC-MNC:", s); - mdm.disconnect(); - mdm.powerOff(); + shutdown(); return 4; } @@ -111,8 +151,7 @@ LCDDisplay::inst().setLines("Agent Init"); if (!agent.init()) { - mdm.disconnect(); - mdm.powerOff(); + shutdown(); return 5; } LCDDisplay::inst().setLines("Agent Run"); @@ -129,14 +168,9 @@ break; } } - - switch (ret) { - case -1: LCDDisplay::inst().setLines("Bootstrap error"); break; - case -2: LCDDisplay::inst().setLines("Integrate failure"); break; - default: agent.loop(); - } - mdm.disconnect(); - mdm.powerOff(); + if (ret == 0) + agent.loop(); + shutdown(); return ret; } }