Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
util/SmartRestSocket.cpp@93:61d44636f020, 2015-04-20 (annotated)
- Committer:
- xinlei
- Date:
- Mon Apr 20 15:04:23 2015 +0000
- Revision:
- 93:61d44636f020
- Child:
- 96:ea056f6be2e8
Integration of new device push code base.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xinlei | 93:61d44636f020 | 1 | #include <string.h> |
xinlei | 93:61d44636f020 | 2 | #include "SmartRestSocket.h" |
xinlei | 93:61d44636f020 | 3 | #include "SmartRestConf.h" |
xinlei | 93:61d44636f020 | 4 | |
xinlei | 93:61d44636f020 | 5 | //memset((void*)SmartRestSocket::cachedIP, 0, sizeof(SmartRestSocket::cachedIP)); |
xinlei | 93:61d44636f020 | 6 | char SmartRestSocket::cachedIP[16] = {0}; |
xinlei | 93:61d44636f020 | 7 | |
xinlei | 93:61d44636f020 | 8 | int SmartRestSocket::connect() |
xinlei | 93:61d44636f020 | 9 | { |
xinlei | 93:61d44636f020 | 10 | int n = -1; |
xinlei | 93:61d44636f020 | 11 | ipLock.lock(); |
xinlei | 93:61d44636f020 | 12 | for (size_t i = 0; i < 3; ++i) { |
xinlei | 93:61d44636f020 | 13 | if (cachedIP[0] == '\0') { |
xinlei | 93:61d44636f020 | 14 | MDMParser::IP ip = mdm.gethostbyname(getHost()); |
xinlei | 93:61d44636f020 | 15 | if (ip == NOIP) |
xinlei | 93:61d44636f020 | 16 | continue; |
xinlei | 93:61d44636f020 | 17 | const unsigned char *c = (const unsigned char*)&ip; |
xinlei | 93:61d44636f020 | 18 | snprintf(cachedIP, sizeof(cachedIP), "%u.%u.%u.%u", c[3], c[2], c[1], c[0]); |
xinlei | 93:61d44636f020 | 19 | } |
xinlei | 93:61d44636f020 | 20 | n = TCPSocketConnection::connect(cachedIP, getPort()); |
xinlei | 93:61d44636f020 | 21 | if (n >= 0) { |
xinlei | 93:61d44636f020 | 22 | break; |
xinlei | 93:61d44636f020 | 23 | } else { |
xinlei | 93:61d44636f020 | 24 | cachedIP[0] = '\0'; |
xinlei | 93:61d44636f020 | 25 | } |
xinlei | 93:61d44636f020 | 26 | } |
xinlei | 93:61d44636f020 | 27 | ipLock.unlock(); |
xinlei | 93:61d44636f020 | 28 | return n; |
xinlei | 93:61d44636f020 | 29 | } |
xinlei | 93:61d44636f020 | 30 | |
xinlei | 93:61d44636f020 | 31 | int SmartRestSocket::sendOnly(char *buf, int size) |
xinlei | 93:61d44636f020 | 32 | { |
xinlei | 93:61d44636f020 | 33 | int l = connect(); |
xinlei | 93:61d44636f020 | 34 | if (l < 0) |
xinlei | 93:61d44636f020 | 35 | return -3; |
xinlei | 93:61d44636f020 | 36 | l = send(buf, size); |
xinlei | 93:61d44636f020 | 37 | close(); |
xinlei | 93:61d44636f020 | 38 | if (l < 0) { |
xinlei | 93:61d44636f020 | 39 | return -2; |
xinlei | 93:61d44636f020 | 40 | } else { |
xinlei | 93:61d44636f020 | 41 | return l; |
xinlei | 93:61d44636f020 | 42 | } |
xinlei | 93:61d44636f020 | 43 | } |
xinlei | 93:61d44636f020 | 44 | |
xinlei | 93:61d44636f020 | 45 | int SmartRestSocket::sendAndReceive(char *buf, int size, int maxSize) |
xinlei | 93:61d44636f020 | 46 | { |
xinlei | 93:61d44636f020 | 47 | int l = connect(); |
xinlei | 93:61d44636f020 | 48 | if (l < 0) |
xinlei | 93:61d44636f020 | 49 | return -3; |
xinlei | 93:61d44636f020 | 50 | l = send(buf, size); |
xinlei | 93:61d44636f020 | 51 | if (l < 0) { |
xinlei | 93:61d44636f020 | 52 | close(); |
xinlei | 93:61d44636f020 | 53 | return -2; |
xinlei | 93:61d44636f020 | 54 | } else { |
xinlei | 93:61d44636f020 | 55 | l = receive(buf, maxSize); |
xinlei | 93:61d44636f020 | 56 | close(); |
xinlei | 93:61d44636f020 | 57 | return l; |
xinlei | 93:61d44636f020 | 58 | } |
xinlei | 93:61d44636f020 | 59 | } |