Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IPStack.h Source File

IPStack.h

00001 /*******************************************************************************
00002  * Copyright (c) 2014, 2017 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *    Benjamin Cabe - generic IPStack
00016  *******************************************************************************/
00017 
00018 #if !defined(IPSTACK_H)
00019 #define IPSTACK_H
00020 
00021 #ifndef WiFi_h
00022   #include <SPI.h>
00023 #endif
00024 
00025 #include <Client.h>
00026 
00027 class IPStack
00028 {
00029 public:
00030     IPStack(Client& client) : client(&client)
00031     {
00032 
00033     }
00034 
00035     int connect(char* hostname, int port)
00036     {
00037         return client->connect(hostname, port);
00038     }
00039 
00040     int connect(uint32_t hostname, int port)
00041     {
00042         return client->connect(hostname, port);
00043     }
00044 
00045     int read(unsigned char* buffer, int len, int timeout)
00046     {
00047         int interval = 10;  // all times are in milliseconds
00048         int total = 0, rc = -1;
00049 
00050         if (timeout < 30)
00051             interval = 2;
00052         while (client->available() < len && total < timeout)
00053         {
00054             delay(interval);
00055             total += interval;
00056         }
00057         if (client->available() >= len)
00058             rc = client->readBytes((char*)buffer, len);
00059         return rc;
00060     }
00061 
00062     int write(unsigned char* buffer, int len, int timeout)
00063     {
00064         client->setTimeout(timeout);
00065         return client->write((uint8_t*)buffer, len);
00066     }
00067 
00068     int disconnect()
00069     {
00070         client->stop();
00071         return 0;
00072     }
00073 
00074 private:
00075 
00076     Client* client;
00077 };
00078 
00079 #endif