Port to C027 (using AppShield and Ethernet)

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
icraggs
Date:
Wed Oct 01 13:27:35 2014 +0000
Revision:
8:80d49dd91542
Parent:
6:37b6d0d56190
Remove conditional compilation for IBM IoT settings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samdanbury 6:37b6d0d56190 1 /* Copyright (C) 2012 mbed.org, MIT License
samdanbury 6:37b6d0d56190 2 *
samdanbury 6:37b6d0d56190 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samdanbury 6:37b6d0d56190 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
samdanbury 6:37b6d0d56190 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
samdanbury 6:37b6d0d56190 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
samdanbury 6:37b6d0d56190 7 * furnished to do so, subject to the following conditions:
samdanbury 6:37b6d0d56190 8 *
samdanbury 6:37b6d0d56190 9 * The above copyright notice and this permission notice shall be included in all copies or
samdanbury 6:37b6d0d56190 10 * substantial portions of the Software.
samdanbury 6:37b6d0d56190 11 *
samdanbury 6:37b6d0d56190 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samdanbury 6:37b6d0d56190 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samdanbury 6:37b6d0d56190 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samdanbury 6:37b6d0d56190 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samdanbury 6:37b6d0d56190 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samdanbury 6:37b6d0d56190 17 */
samdanbury 6:37b6d0d56190 18 #include "TCPSocketConnection.h"
samdanbury 6:37b6d0d56190 19 #include <cstring>
samdanbury 6:37b6d0d56190 20
samdanbury 6:37b6d0d56190 21 using std::memset;
samdanbury 6:37b6d0d56190 22 using std::memcpy;
samdanbury 6:37b6d0d56190 23
samdanbury 6:37b6d0d56190 24 TCPSocketConnection::TCPSocketConnection() :
samdanbury 6:37b6d0d56190 25 _is_connected(false) {
samdanbury 6:37b6d0d56190 26 }
samdanbury 6:37b6d0d56190 27
samdanbury 6:37b6d0d56190 28 int TCPSocketConnection::connect(const char* host, const int port) {
samdanbury 6:37b6d0d56190 29 if (init_socket(SOCK_STREAM) < 0)
samdanbury 6:37b6d0d56190 30 return -1;
samdanbury 6:37b6d0d56190 31
samdanbury 6:37b6d0d56190 32 if (set_address(host, port) != 0)
samdanbury 6:37b6d0d56190 33 return -1;
samdanbury 6:37b6d0d56190 34
samdanbury 6:37b6d0d56190 35 if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
samdanbury 6:37b6d0d56190 36 close();
samdanbury 6:37b6d0d56190 37 return -1;
samdanbury 6:37b6d0d56190 38 }
samdanbury 6:37b6d0d56190 39 _is_connected = true;
samdanbury 6:37b6d0d56190 40
samdanbury 6:37b6d0d56190 41 return 0;
samdanbury 6:37b6d0d56190 42 }
samdanbury 6:37b6d0d56190 43
samdanbury 6:37b6d0d56190 44 bool TCPSocketConnection::is_connected(void) {
samdanbury 6:37b6d0d56190 45 return _is_connected;
samdanbury 6:37b6d0d56190 46 }
samdanbury 6:37b6d0d56190 47
samdanbury 6:37b6d0d56190 48 int TCPSocketConnection::send(char* data, int length) {
samdanbury 6:37b6d0d56190 49 if ((_sock_fd < 0) || !_is_connected)
samdanbury 6:37b6d0d56190 50 return -1;
samdanbury 6:37b6d0d56190 51
samdanbury 6:37b6d0d56190 52 if (!_blocking) {
samdanbury 6:37b6d0d56190 53 TimeInterval timeout(_timeout);
samdanbury 6:37b6d0d56190 54 if (wait_writable(timeout) != 0)
samdanbury 6:37b6d0d56190 55 return -1;
samdanbury 6:37b6d0d56190 56 }
samdanbury 6:37b6d0d56190 57
samdanbury 6:37b6d0d56190 58 int n = lwip_send(_sock_fd, data, length, 0);
samdanbury 6:37b6d0d56190 59 _is_connected = (n != 0);
samdanbury 6:37b6d0d56190 60
samdanbury 6:37b6d0d56190 61 return n;
samdanbury 6:37b6d0d56190 62 }
samdanbury 6:37b6d0d56190 63
samdanbury 6:37b6d0d56190 64 // -1 if unsuccessful, else number of bytes written
samdanbury 6:37b6d0d56190 65 int TCPSocketConnection::send_all(char* data, int length) {
samdanbury 6:37b6d0d56190 66 if ((_sock_fd < 0) || !_is_connected)
samdanbury 6:37b6d0d56190 67 return -1;
samdanbury 6:37b6d0d56190 68
samdanbury 6:37b6d0d56190 69 int writtenLen = 0;
samdanbury 6:37b6d0d56190 70 TimeInterval timeout(_timeout);
samdanbury 6:37b6d0d56190 71 while (writtenLen < length) {
samdanbury 6:37b6d0d56190 72 if (!_blocking) {
samdanbury 6:37b6d0d56190 73 // Wait for socket to be writeable
samdanbury 6:37b6d0d56190 74 if (wait_writable(timeout) != 0)
samdanbury 6:37b6d0d56190 75 return writtenLen;
samdanbury 6:37b6d0d56190 76 }
samdanbury 6:37b6d0d56190 77
samdanbury 6:37b6d0d56190 78 int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
samdanbury 6:37b6d0d56190 79 if (ret > 0) {
samdanbury 6:37b6d0d56190 80 writtenLen += ret;
samdanbury 6:37b6d0d56190 81 continue;
samdanbury 6:37b6d0d56190 82 } else if (ret == 0) {
samdanbury 6:37b6d0d56190 83 _is_connected = false;
samdanbury 6:37b6d0d56190 84 return writtenLen;
samdanbury 6:37b6d0d56190 85 } else {
samdanbury 6:37b6d0d56190 86 return -1; //Connnection error
samdanbury 6:37b6d0d56190 87 }
samdanbury 6:37b6d0d56190 88 }
samdanbury 6:37b6d0d56190 89 return writtenLen;
samdanbury 6:37b6d0d56190 90 }
samdanbury 6:37b6d0d56190 91
samdanbury 6:37b6d0d56190 92 int TCPSocketConnection::receive(char* data, int length) {
samdanbury 6:37b6d0d56190 93 if ((_sock_fd < 0) || !_is_connected)
samdanbury 6:37b6d0d56190 94 return -1;
samdanbury 6:37b6d0d56190 95
samdanbury 6:37b6d0d56190 96 if (!_blocking) {
samdanbury 6:37b6d0d56190 97 TimeInterval timeout(_timeout);
samdanbury 6:37b6d0d56190 98 if (wait_readable(timeout) != 0)
samdanbury 6:37b6d0d56190 99 return -1;
samdanbury 6:37b6d0d56190 100 }
samdanbury 6:37b6d0d56190 101
samdanbury 6:37b6d0d56190 102 int n = lwip_recv(_sock_fd, data, length, 0);
samdanbury 6:37b6d0d56190 103 _is_connected = (n != 0);
samdanbury 6:37b6d0d56190 104
samdanbury 6:37b6d0d56190 105 return n;
samdanbury 6:37b6d0d56190 106 }
samdanbury 6:37b6d0d56190 107
samdanbury 6:37b6d0d56190 108 // -1 if unsuccessful, else number of bytes received
samdanbury 6:37b6d0d56190 109 int TCPSocketConnection::receive_all(char* data, int length) {
samdanbury 6:37b6d0d56190 110 if ((_sock_fd < 0) || !_is_connected)
samdanbury 6:37b6d0d56190 111 return -1;
samdanbury 6:37b6d0d56190 112
samdanbury 6:37b6d0d56190 113 int readLen = 0;
samdanbury 6:37b6d0d56190 114 TimeInterval timeout(_timeout);
samdanbury 6:37b6d0d56190 115 while (readLen < length) {
samdanbury 6:37b6d0d56190 116 if (!_blocking) {
samdanbury 6:37b6d0d56190 117 //Wait for socket to be readable
samdanbury 6:37b6d0d56190 118 if (wait_readable(timeout) != 0)
samdanbury 6:37b6d0d56190 119 return readLen;
samdanbury 6:37b6d0d56190 120 }
samdanbury 6:37b6d0d56190 121
samdanbury 6:37b6d0d56190 122 int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
samdanbury 6:37b6d0d56190 123 if (ret > 0) {
samdanbury 6:37b6d0d56190 124 readLen += ret;
samdanbury 6:37b6d0d56190 125 } else if (ret == 0) {
samdanbury 6:37b6d0d56190 126 _is_connected = false;
samdanbury 6:37b6d0d56190 127 return readLen;
samdanbury 6:37b6d0d56190 128 } else {
samdanbury 6:37b6d0d56190 129 return -1; //Connnection error
samdanbury 6:37b6d0d56190 130 }
samdanbury 6:37b6d0d56190 131 }
samdanbury 6:37b6d0d56190 132 return readLen;
samdanbury 6:37b6d0d56190 133 }