1 /******************************************************************************* 2 * Copyright (c) 2014, 2017 IBM Corp. 3 * 4 * All rights reserved. This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License v1.0 6 * and Eclipse Distribution License v1.0 which accompany this distribution. 7 * 8 * The Eclipse Public License is available at 9 * http://www.eclipse.org/legal/epl-v10.html 10 * and the Eclipse Distribution License is available at 11 * http://www.eclipse.org/org/documents/edl-v10.php. 12 * 13 * Contributors: 14 * Ian Craggs - initial API and implementation and/or initial documentation 15 * Benjamin Cabe - generic IPStack 16 *******************************************************************************/ 17 18 #if !defined(IPSTACK_H) 19 #define IPSTACK_H 20 21 #ifndef WiFi_h 22 #include <SPI.h> 23 #endif 24 25 #include <Client.h> 26 27 class IPStack 28 { 29 public: IPStack(Client & client)30 IPStack(Client& client) : client(&client) 31 { 32 33 } 34 connect(char * hostname,int port)35 int connect(char* hostname, int port) 36 { 37 return client->connect(hostname, port); 38 } 39 connect(uint32_t hostname,int port)40 int connect(uint32_t hostname, int port) 41 { 42 return client->connect(hostname, port); 43 } 44 read(unsigned char * buffer,int len,int timeout)45 int read(unsigned char* buffer, int len, int timeout) 46 { 47 int interval = 10; // all times are in milliseconds 48 int total = 0, rc = -1; 49 50 if (timeout < 30) 51 interval = 2; 52 while (client->available() < len && total < timeout) 53 { 54 delay(interval); 55 total += interval; 56 } 57 if (client->available() >= len) 58 rc = client->readBytes((char*)buffer, len); 59 return rc; 60 } 61 write(unsigned char * buffer,int len,int timeout)62 int write(unsigned char* buffer, int len, int timeout) 63 { 64 client->setTimeout(timeout); 65 return client->write((uint8_t*)buffer, len); 66 } 67 disconnect()68 int disconnect() 69 { 70 client->stop(); 71 return 0; 72 } 73 74 private: 75 76 Client* client; 77 }; 78 79 #endif 80