Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
util/SmartRestSocket.cpp
- Committer:
- xinlei
- Date:
- 2015-05-13
- Revision:
- 107:fc5f25f0e0d5
- Parent:
- 106:c61f0d62b625
- Child:
- 116:4eb3c7e945cf
File content as of revision 107:fc5f25f0e0d5:
#include <string.h>
#include "SmartRestSocket.h"
#include "SmartRestConf.h"
char SmartRestSocket::cachedIP[16] = {0};
int SmartRestSocket::connect()
{
extern MDMSerial *pMdm;
int n = -1;
ipLock.lock();
for (size_t i = 0; i < 3; ++i) {
if (cachedIP[0] == 0) {
MDMParser::IP ip = pMdm->gethostbyname(srHost);
if (ip == NOIP)
continue;
const unsigned char *c = (const unsigned char*)&ip;
snprintf(cachedIP, sizeof(cachedIP), "%u.%u.%u.%u", c[3], c[2], c[1], c[0]);
}
n = TCPSocketConnection::connect(cachedIP, srPort);
if (n >= 0) {
if (timeout == -1)
Socket::set_blocking(true);
else
Socket::set_blocking(false, timeout);
break;
} else {
cachedIP[0] = 0;
}
}
ipLock.unlock();
return n;
}
int SmartRestSocket::sendOnly(char *buf, int size)
{
int l = connect();
if (l < 0)
return -3;
l = send(buf, size);
close();
if (l < 0) {
return -2;
} else {
return l;
}
}
int SmartRestSocket::sendAndReceive(char *buf, int size, int maxSize)
{
int l = connect();
if (l < 0)
return -3;
l = send(buf, size);
if (l < 0) {
close();
return -2;
} else {
l = receive(buf, maxSize);
if (l >= 0 && l < maxSize)
buf[l] = 0;
close();
return l;
}
}

Cumulocity