source: rtos_arduino/trunk/arduino_lib/libraries/Milkcocoa_Arduino_SDK/Client_ESP8266.h@ 169

Last change on this file since 169 was 169, checked in by ertl-honda, 8 years ago

サーバーからクローズされた場合にavailable()にfalseを返すよう変更

File size: 3.4 KB
Line 
1/*
2 Copyright (C) 2015 Embedded and Real-Time Systems Laboratory
3 Graduate School of Information Science, Nagoya Univ., JAPAN
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 You should have received a copy of the GNU Lesser General Public
14 License along with this library; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18#ifndef _CLIENT_ESP8366_H_
19#define _CLIENT_ESP8366_H_
20#include "Print.h"
21#include "Stream.h"
22#include "IPAddress.h"
23#include "ESP8266.h"
24
25#define ESP8266CLIENT_BUFFER_LEN 512
26
27class ESP8266Client: public Client, public ESP8266 {
28
29 public:
30 /*
31 * @retval 0 : faild
32 * @retval 1 : success
33 */
34 int connect(IPAddress ip, uint16_t port) {
35 String ip_str;
36 bufferlen= 0;
37 ip_str.concat(ip[0]);
38 ip_str.concat(".");
39 ip_str.concat(ip[1]);
40 ip_str.concat(".");
41 ip_str.concat(ip[2]);
42 ip_str.concat(".");
43 ip_str.concat(ip[3]);
44 _connected = (createTCP(ip_str, port))? 1 : 0;
45 return (int)_connected;
46 };
47
48 int connect(const char *host, uint16_t port) {
49 String ip_str(host);
50 bufferlen= 0;
51 _connected = (createTCP(ip_str, port))? 1 : 0;
52 return (int)_connected;
53 };
54
55 size_t write(uint8_t data) {
56 if(send(&data, 1)){
57 return 1;
58 }
59 else {
60 return 0;
61 }
62 };
63 size_t write(const uint8_t *buf, size_t size){
64 if(send(buf, size)){
65 return size;
66 }
67 else {
68 return 0;
69 }
70 };
71 int available(){
72 if (bufferlen > 0) {
73 return 1;
74 }
75 if (dataAvailable()) {
76 bufferlen = recv(buffer, ESP8266CLIENT_BUFFER_LEN);
77 pbuffer = buffer;
78 //In case of that data is aviable but it is not valid data(not +IPD,...),
79 //connection is closed by server.(send "CLOSED")
80 if(bufferlen == 0){
81 _connected = 0;
82 releaseTCP();
83 return 0;
84 }
85 return 1;
86 }
87 return 0;
88 };
89 int read(){
90 if (bufferlen == 0) {
91 bufferlen = recv(buffer, ESP8266CLIENT_BUFFER_LEN);
92 pbuffer = buffer;
93 }
94 if (bufferlen > 0){
95 bufferlen--;
96 return *pbuffer++;
97 }
98 else {
99 return (int)-1;
100 }
101 };
102 int read(uint8_t *buf, size_t size){return recv(buf, size);};
103 int peek(){return 1;};
104 void flush(){};
105 void stop(){releaseTCP();};
106 uint8_t connected(){return _connected;};
107 operator bool(){return (bool)(_connected == 1);};
108 private:
109 uint8_t _connected;
110 uint8_t buffer[ESP8266CLIENT_BUFFER_LEN];
111 uint8_t bufferlen;
112 uint8_t *pbuffer;
113};
114
115#endif /* _CLIENT_ESP8366_H_ */
Note: See TracBrowser for help on using the repository browser.