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 SmartRestSocket.cpp Source File

SmartRestSocket.cpp

00001 #include <string.h>
00002 #include "SmartRestSocket.h"
00003 #include "SmartRestConf.h"
00004 
00005 
00006 int SmartRestSocket::connect()
00007 {
00008         extern MDMSerial *pMdm;
00009         int n = -1;
00010         for (size_t i = 0; i < 3; ++i) {
00011                 if (cachedIP[0] == 0) {
00012                         MDMParser::IP ip = pMdm->gethostbyname(srHost);
00013                         if (ip == NOIP)
00014                                 continue;
00015                         const unsigned char *c = (const unsigned char*)&ip;
00016                         snprintf(cachedIP, sizeof(cachedIP), "%u.%u.%u.%u", c[3], c[2], c[1], c[0]);
00017                 }
00018                 n = TCPSocketConnection::connect(cachedIP, srPort);
00019                 if (n == 0) {
00020                         break;
00021                 } else {
00022                         cachedIP[0] = 0;
00023                 }
00024         }
00025         return n;
00026 }
00027 
00028 int SmartRestSocket::sendOnly(char *buf, int size)
00029 {
00030         int l = connect();
00031         if (l < 0) {
00032                 close();
00033                 return -3;
00034         }
00035         l = send(buf, size);
00036         close();
00037         if (l < 0) {
00038                 return -2;
00039         } else {
00040                 return l;
00041         }
00042 }
00043 
00044 int SmartRestSocket::sendAndReceive(char *buf, int size, int maxSize)
00045 {
00046         int l = connect();
00047         if (l < 0) {
00048                 close();
00049                 return -3;
00050         }
00051         l = send(buf, size);
00052         if (l < 0) {
00053                 close();
00054                 return -2;
00055         } else {
00056                 l = receive(buf, maxSize);
00057                 close();
00058                 if (l >= 0 && l < maxSize)
00059                         buf[l] = 0;
00060                 return l;
00061         }
00062 }