Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.

Dependencies:   C12832 MQTT LM75B MMA7660

Dependents:   MFT_IoT_demo_USB400 IBM_RFID

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 <cstring>
samdanbury 6:37b6d0d56190 20
samdanbury 6:37b6d0d56190 21 using std::memset;
samdanbury 6:37b6d0d56190 22
samdanbury 6:37b6d0d56190 23 Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) {
samdanbury 6:37b6d0d56190 24
samdanbury 6:37b6d0d56190 25 }
samdanbury 6:37b6d0d56190 26
samdanbury 6:37b6d0d56190 27 void Socket::set_blocking(bool blocking, unsigned int timeout) {
samdanbury 6:37b6d0d56190 28 _blocking = blocking;
samdanbury 6:37b6d0d56190 29 _timeout = timeout;
samdanbury 6:37b6d0d56190 30 }
samdanbury 6:37b6d0d56190 31
samdanbury 6:37b6d0d56190 32 int Socket::init_socket(int type) {
samdanbury 6:37b6d0d56190 33 if (_sock_fd != -1)
samdanbury 6:37b6d0d56190 34 return -1;
samdanbury 6:37b6d0d56190 35
samdanbury 6:37b6d0d56190 36 int fd = lwip_socket(AF_INET, type, 0);
samdanbury 6:37b6d0d56190 37 if (fd < 0)
samdanbury 6:37b6d0d56190 38 return -1;
samdanbury 6:37b6d0d56190 39
samdanbury 6:37b6d0d56190 40 _sock_fd = fd;
samdanbury 6:37b6d0d56190 41 return 0;
samdanbury 6:37b6d0d56190 42 }
samdanbury 6:37b6d0d56190 43
samdanbury 6:37b6d0d56190 44 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
samdanbury 6:37b6d0d56190 45 return lwip_setsockopt(_sock_fd, level, optname, optval, optlen);
samdanbury 6:37b6d0d56190 46 }
samdanbury 6:37b6d0d56190 47
samdanbury 6:37b6d0d56190 48 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
samdanbury 6:37b6d0d56190 49 return lwip_getsockopt(_sock_fd, level, optname, optval, optlen);
samdanbury 6:37b6d0d56190 50 }
samdanbury 6:37b6d0d56190 51
samdanbury 6:37b6d0d56190 52 int Socket::select(struct timeval *timeout, bool read, bool write) {
samdanbury 6:37b6d0d56190 53 fd_set fdSet;
samdanbury 6:37b6d0d56190 54 FD_ZERO(&fdSet);
samdanbury 6:37b6d0d56190 55 FD_SET(_sock_fd, &fdSet);
samdanbury 6:37b6d0d56190 56
samdanbury 6:37b6d0d56190 57 fd_set* readset = (read ) ? (&fdSet) : (NULL);
samdanbury 6:37b6d0d56190 58 fd_set* writeset = (write) ? (&fdSet) : (NULL);
samdanbury 6:37b6d0d56190 59
samdanbury 6:37b6d0d56190 60 int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, timeout);
samdanbury 6:37b6d0d56190 61 return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
samdanbury 6:37b6d0d56190 62 }
samdanbury 6:37b6d0d56190 63
samdanbury 6:37b6d0d56190 64 int Socket::wait_readable(TimeInterval& timeout) {
samdanbury 6:37b6d0d56190 65 return select(&timeout._time, true, false);
samdanbury 6:37b6d0d56190 66 }
samdanbury 6:37b6d0d56190 67
samdanbury 6:37b6d0d56190 68 int Socket::wait_writable(TimeInterval& timeout) {
samdanbury 6:37b6d0d56190 69 return select(&timeout._time, false, true);
samdanbury 6:37b6d0d56190 70 }
samdanbury 6:37b6d0d56190 71
samdanbury 6:37b6d0d56190 72 int Socket::close(bool shutdown) {
samdanbury 6:37b6d0d56190 73 if (_sock_fd < 0)
samdanbury 6:37b6d0d56190 74 return -1;
samdanbury 6:37b6d0d56190 75
samdanbury 6:37b6d0d56190 76 if (shutdown)
samdanbury 6:37b6d0d56190 77 lwip_shutdown(_sock_fd, SHUT_RDWR);
samdanbury 6:37b6d0d56190 78 lwip_close(_sock_fd);
samdanbury 6:37b6d0d56190 79 _sock_fd = -1;
samdanbury 6:37b6d0d56190 80
samdanbury 6:37b6d0d56190 81 return 0;
samdanbury 6:37b6d0d56190 82 }
samdanbury 6:37b6d0d56190 83
samdanbury 6:37b6d0d56190 84 Socket::~Socket() {
samdanbury 6:37b6d0d56190 85 close(); //Don't want to leak
samdanbury 6:37b6d0d56190 86 }
samdanbury 6:37b6d0d56190 87
samdanbury 6:37b6d0d56190 88 TimeInterval::TimeInterval(unsigned int ms) {
samdanbury 6:37b6d0d56190 89 _time.tv_sec = ms / 1000;
samdanbury 6:37b6d0d56190 90 _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
samdanbury 6:37b6d0d56190 91 }