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
samdanbury 6:37b6d0d56190 19 #include "Socket/UDPSocket.h"
samdanbury 6:37b6d0d56190 20
samdanbury 6:37b6d0d56190 21 #include <cstring>
samdanbury 6:37b6d0d56190 22
samdanbury 6:37b6d0d56190 23 using std::memset;
samdanbury 6:37b6d0d56190 24
samdanbury 6:37b6d0d56190 25 UDPSocket::UDPSocket() {
samdanbury 6:37b6d0d56190 26 }
samdanbury 6:37b6d0d56190 27
samdanbury 6:37b6d0d56190 28 int UDPSocket::init(void) {
samdanbury 6:37b6d0d56190 29 return init_socket(SOCK_DGRAM);
samdanbury 6:37b6d0d56190 30 }
samdanbury 6:37b6d0d56190 31
samdanbury 6:37b6d0d56190 32 // Server initialization
samdanbury 6:37b6d0d56190 33 int UDPSocket::bind(int port) {
samdanbury 6:37b6d0d56190 34 if (init_socket(SOCK_DGRAM) < 0)
samdanbury 6:37b6d0d56190 35 return -1;
samdanbury 6:37b6d0d56190 36
samdanbury 6:37b6d0d56190 37 struct sockaddr_in localHost;
samdanbury 6:37b6d0d56190 38 std::memset(&localHost, 0, sizeof(localHost));
samdanbury 6:37b6d0d56190 39
samdanbury 6:37b6d0d56190 40 localHost.sin_family = AF_INET;
samdanbury 6:37b6d0d56190 41 localHost.sin_port = htons(port);
samdanbury 6:37b6d0d56190 42 localHost.sin_addr.s_addr = INADDR_ANY;
samdanbury 6:37b6d0d56190 43
samdanbury 6:37b6d0d56190 44 if (lwip_bind(_sock_fd, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) {
samdanbury 6:37b6d0d56190 45 close();
samdanbury 6:37b6d0d56190 46 return -1;
samdanbury 6:37b6d0d56190 47 }
samdanbury 6:37b6d0d56190 48
samdanbury 6:37b6d0d56190 49 return 0;
samdanbury 6:37b6d0d56190 50 }
samdanbury 6:37b6d0d56190 51
samdanbury 6:37b6d0d56190 52 int UDPSocket::join_multicast_group(const char* address) {
samdanbury 6:37b6d0d56190 53 struct ip_mreq mreq;
samdanbury 6:37b6d0d56190 54
samdanbury 6:37b6d0d56190 55 // Set up group address
samdanbury 6:37b6d0d56190 56 mreq.imr_multiaddr.s_addr = inet_addr(address);
samdanbury 6:37b6d0d56190 57 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
samdanbury 6:37b6d0d56190 58
samdanbury 6:37b6d0d56190 59 return set_option(IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
samdanbury 6:37b6d0d56190 60 }
samdanbury 6:37b6d0d56190 61
samdanbury 6:37b6d0d56190 62 int UDPSocket::set_broadcasting(bool broadcast) {
samdanbury 6:37b6d0d56190 63 int option = (broadcast) ? (1) : (0);
samdanbury 6:37b6d0d56190 64 return set_option(SOL_SOCKET, SO_BROADCAST, &option, sizeof(option));
samdanbury 6:37b6d0d56190 65 }
samdanbury 6:37b6d0d56190 66
samdanbury 6:37b6d0d56190 67 // -1 if unsuccessful, else number of bytes written
samdanbury 6:37b6d0d56190 68 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) {
samdanbury 6:37b6d0d56190 69 if (_sock_fd < 0)
samdanbury 6:37b6d0d56190 70 return -1;
samdanbury 6:37b6d0d56190 71
samdanbury 6:37b6d0d56190 72 if (!_blocking) {
samdanbury 6:37b6d0d56190 73 TimeInterval timeout(_timeout);
samdanbury 6:37b6d0d56190 74 if (wait_writable(timeout) != 0)
samdanbury 6:37b6d0d56190 75 return 0;
samdanbury 6:37b6d0d56190 76 }
samdanbury 6:37b6d0d56190 77
samdanbury 6:37b6d0d56190 78 return lwip_sendto(_sock_fd, packet, length, 0, (const struct sockaddr *) &remote._remoteHost, sizeof(remote._remoteHost));
samdanbury 6:37b6d0d56190 79 }
samdanbury 6:37b6d0d56190 80
samdanbury 6:37b6d0d56190 81 // -1 if unsuccessful, else number of bytes received
samdanbury 6:37b6d0d56190 82 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) {
samdanbury 6:37b6d0d56190 83 if (_sock_fd < 0)
samdanbury 6:37b6d0d56190 84 return -1;
samdanbury 6:37b6d0d56190 85
samdanbury 6:37b6d0d56190 86 if (!_blocking) {
samdanbury 6:37b6d0d56190 87 TimeInterval timeout(_timeout);
samdanbury 6:37b6d0d56190 88 if (wait_readable(timeout) != 0)
samdanbury 6:37b6d0d56190 89 return 0;
samdanbury 6:37b6d0d56190 90 }
samdanbury 6:37b6d0d56190 91 remote.reset_address();
samdanbury 6:37b6d0d56190 92 socklen_t remoteHostLen = sizeof(remote._remoteHost);
samdanbury 6:37b6d0d56190 93 return lwip_recvfrom(_sock_fd, buffer, length, 0, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
samdanbury 6:37b6d0d56190 94 }