IOP / WIZwiki-7500_Blynk

Dependents:   Blynk_Example_WIZwiki-W7500

Fork of Blynk by Volodymyr Shymanskyy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BlynkMbedClient.h Source File

BlynkMbedClient.h

00001 /**
00002  * @file       BlynkParam.h
00003  * @author     Volodymyr Shymanskyy
00004  * @license    This project is released under the MIT License (MIT)
00005  * @copyright  Copyright (c) 2015 Volodymyr Shymanskyy
00006  * @date       Jan 2015
00007  * @brief
00008  *
00009  */
00010 
00011 #ifndef BlynkMbedClient_h
00012 #define BlynkMbedClient_h
00013 
00014 #include <BlynkApiMbed.h >
00015 #include <Blynk/BlynkDebug.h>
00016 #include <TCPSocketConnection.h>
00017 
00018 #if defined(ESP8266) && !defined(BLYNK_NO_YIELD)
00019     #define YIELD_FIX() BLYNK_RUN_YIELD();
00020 #else
00021     #define YIELD_FIX()
00022 #endif
00023 
00024 template <typename Client>
00025 class BlynkMbedClientGen : public Socket
00026 {
00027 public:
00028     BlynkMbedClientGen(Client& c)
00029         : client(NULL), domain(NULL), port(0), isConn(false)
00030     {
00031         setClient(&c);
00032     }
00033 
00034     BlynkMbedClientGen()
00035         : client(NULL), domain(NULL), port(0), isConn(false)
00036     {}
00037 
00038     void setClient(Client* c) {
00039         client = c;
00040         client->set_blocking(true, BLYNK_TIMEOUT_MS*1000);
00041     }
00042 
00043     void begin(char* a, uint16_t p) {
00044         domain = NULL;
00045         port = p;
00046         addr = a;
00047     }
00048 
00049     void begin(const char* d, uint16_t p) {
00050         domain = d;
00051         port = p;
00052     }
00053 
00054     bool connect() {
00055         if (domain) {
00056             BLYNK_LOG4(BLYNK_F("Connecting to "), domain, ':', port);
00057             client->connect(domain, port);
00058             isConn = (1 == client->is_connected());
00059             return isConn;
00060         } else { //if (uint32_t(addr) != 0) {
00061             BLYNK_LOG_IP("Connecting to ", addr);
00062             isConn = (1 == client->connect(addr, port));
00063             return isConn;
00064         }
00065     }
00066 
00067     void disconnect() {
00068         isConn = false;
00069         if (client->getsock()) return;
00070         client->close();
00071         client->setsock(-1);
00072      }
00073 
00074 #ifdef BLYNK_ENC28J60_FIX
00075     size_t read(void* buf, size_t len) {
00076         while (client->available() < len) { }
00077         return client->read((uint8_t*)buf, len);
00078     }
00079 #else
00080     size_t read(void* buf, size_t len) {
00081         size_t res = client->receive((char*)buf, len);
00082         YIELD_FIX();
00083         return res;
00084     }
00085 #endif
00086 
00087 #ifdef BLYNK_RETRY_SEND
00088     size_t write(const void* buf, size_t len) {
00089         size_t sent = 0;
00090         int retry = 0;
00091         while (sent < len && ++retry < 10) {
00092             size_t w = client->write((const uint8_t*)buf+sent, len-sent);
00093             if (w != 0 && w != -1) {
00094                 sent += w;
00095             } else {
00096                 ::delay(50);
00097 #if defined(BLYNK_DEBUG) && defined(BLYNK_PRINT)
00098                 BLYNK_PRINT_TIME();
00099                 BLYNK_PRINT.print(BLYNK_F("Retry "));
00100                 BLYNK_PRINT.print(retry);
00101                 BLYNK_PRINT.print(BLYNK_F(" send: "));
00102                 BLYNK_PRINT.print(sent);
00103                 BLYNK_PRINT.print('/');
00104                 BLYNK_PRINT.println(len);
00105 #endif
00106             }
00107         }
00108         return sent;
00109     }
00110 #else
00111     size_t write(const void* buf, size_t len) {
00112         YIELD_FIX();
00113         size_t res = client->send((char*)buf, len);
00114         YIELD_FIX();
00115         return res;
00116     }
00117 #endif
00118 
00119     bool connected() { YIELD_FIX(); return isConn && client->is_connected(); }
00120     int available() {  YIELD_FIX(); 
00121     //printf("readable : %d, sock: %d\r\n", eth->wait_readable(_sock_fd, 3000000, 0),_sock_fd);  
00122     //return eth->wait_readable(_sock_fd, 300000, 0); 
00123     return eth->wait_readable(client->getsock() , 1000*60*5, 0); 
00124     }
00125 
00126 protected:
00127     Client*     client;
00128     char*       addr;
00129     const char* domain;
00130     uint16_t    port;
00131     bool        isConn;
00132 };
00133 
00134 typedef BlynkMbedClientGen<TCPSocketConnection> BlynkMbedClient;
00135 
00136 #endif