Port to C027 (using AppShield and Ethernet)

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

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 "Socket/Socket.h"
samdanbury 6:37b6d0d56190 19 #include "Socket/Endpoint.h"
samdanbury 6:37b6d0d56190 20 #include <cstring>
samdanbury 6:37b6d0d56190 21 #include <cstdio>
samdanbury 6:37b6d0d56190 22
samdanbury 6:37b6d0d56190 23 Endpoint::Endpoint() {
samdanbury 6:37b6d0d56190 24 reset_address();
samdanbury 6:37b6d0d56190 25 }
samdanbury 6:37b6d0d56190 26 Endpoint::~Endpoint() {}
samdanbury 6:37b6d0d56190 27
samdanbury 6:37b6d0d56190 28 void Endpoint::reset_address(void) {
samdanbury 6:37b6d0d56190 29 std::memset(&_remoteHost, 0, sizeof(struct sockaddr_in));
samdanbury 6:37b6d0d56190 30 _ipAddress[0] = '\0';
samdanbury 6:37b6d0d56190 31 }
samdanbury 6:37b6d0d56190 32
samdanbury 6:37b6d0d56190 33 #include "stdio.h"
samdanbury 6:37b6d0d56190 34
samdanbury 6:37b6d0d56190 35 int Endpoint::set_address(const char* host, const int port) {
samdanbury 6:37b6d0d56190 36 reset_address();
samdanbury 6:37b6d0d56190 37
samdanbury 6:37b6d0d56190 38 // IP Address
samdanbury 6:37b6d0d56190 39 char address[5];
samdanbury 6:37b6d0d56190 40 char *p_address = address;
samdanbury 6:37b6d0d56190 41
samdanbury 6:37b6d0d56190 42 // Dot-decimal notation
samdanbury 6:37b6d0d56190 43 int result = std::sscanf(host, "%3u.%3u.%3u.%3u",
samdanbury 6:37b6d0d56190 44 (unsigned int*)&address[0], (unsigned int*)&address[1],
samdanbury 6:37b6d0d56190 45 (unsigned int*)&address[2], (unsigned int*)&address[3]);
samdanbury 6:37b6d0d56190 46
samdanbury 6:37b6d0d56190 47 if (result != 4) {
samdanbury 6:37b6d0d56190 48 // Resolve address with DNS
samdanbury 6:37b6d0d56190 49 struct hostent *host_address = lwip_gethostbyname(host);
samdanbury 6:37b6d0d56190 50 if (host_address == NULL)
samdanbury 6:37b6d0d56190 51 return -1; //Could not resolve address
samdanbury 6:37b6d0d56190 52 p_address = (char*)host_address->h_addr_list[0];
samdanbury 6:37b6d0d56190 53 }
samdanbury 6:37b6d0d56190 54 std::memcpy((char*)&_remoteHost.sin_addr.s_addr, p_address, 4);
samdanbury 6:37b6d0d56190 55
samdanbury 6:37b6d0d56190 56 // Address family
samdanbury 6:37b6d0d56190 57 _remoteHost.sin_family = AF_INET;
samdanbury 6:37b6d0d56190 58
samdanbury 6:37b6d0d56190 59 // Set port
samdanbury 6:37b6d0d56190 60 _remoteHost.sin_port = htons(port);
samdanbury 6:37b6d0d56190 61
samdanbury 6:37b6d0d56190 62 return 0;
samdanbury 6:37b6d0d56190 63 }
samdanbury 6:37b6d0d56190 64
samdanbury 6:37b6d0d56190 65 char* Endpoint::get_address() {
samdanbury 6:37b6d0d56190 66 if ((_ipAddress[0] == '\0') && (_remoteHost.sin_addr.s_addr != 0))
samdanbury 6:37b6d0d56190 67 inet_ntoa_r(_remoteHost.sin_addr, _ipAddress, sizeof(_ipAddress));
samdanbury 6:37b6d0d56190 68 return _ipAddress;
samdanbury 6:37b6d0d56190 69 }
samdanbury 6:37b6d0d56190 70
samdanbury 6:37b6d0d56190 71 int Endpoint::get_port() {
samdanbury 6:37b6d0d56190 72 return ntohs(_remoteHost.sin_port);
samdanbury 6:37b6d0d56190 73 }