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 |
|
---|
21 | #include "ESP8266.h"
|
---|
22 | #include "IPAddress.h"
|
---|
23 | #include "Client.h"
|
---|
24 |
|
---|
25 | class ESP8266Client: public Client {
|
---|
26 | public:
|
---|
27 | ESP8266Client(void) {
|
---|
28 | mux_id = 0;
|
---|
29 | mux = false;
|
---|
30 | pbuffer = &(WiFi.rx_buffer[0]);
|
---|
31 | }
|
---|
32 |
|
---|
33 | ESP8266Client(uint8_t _mux_id) {
|
---|
34 | mux_id = _mux_id;
|
---|
35 | mux = true;
|
---|
36 | pbuffer = &(WiFi.rx_buffer[mux_id]);
|
---|
37 | }
|
---|
38 |
|
---|
39 | /*
|
---|
40 | * @retval 0 : faild
|
---|
41 | * @retval 1 : success
|
---|
42 | */
|
---|
43 | int connect(IPAddress ip, uint16_t port) {
|
---|
44 | if (mux) {
|
---|
45 | if (WiFi.isConnected(mux_id)) return 1;
|
---|
46 | String ip_str;
|
---|
47 | ip_str.concat(ip[0]); ip_str.concat(".");
|
---|
48 | ip_str.concat(ip[1]); ip_str.concat(".");
|
---|
49 | ip_str.concat(ip[2]); ip_str.concat(".");
|
---|
50 | ip_str.concat(ip[3]);
|
---|
51 | return (WiFi.createTCP(mux_id, ip_str, port))? 1 : 0;
|
---|
52 | }
|
---|
53 | else {
|
---|
54 | if (WiFi.isConnected()) return 1;
|
---|
55 | String ip_str;
|
---|
56 | ip_str.concat(ip[0]); ip_str.concat(".");
|
---|
57 | ip_str.concat(ip[1]); ip_str.concat(".");
|
---|
58 | ip_str.concat(ip[2]); ip_str.concat(".");
|
---|
59 | ip_str.concat(ip[3]);
|
---|
60 | return (WiFi.createTCP(ip_str, port))? 1 : 0;
|
---|
61 | }
|
---|
62 | };
|
---|
63 |
|
---|
64 | int connect(const char *host, uint16_t port) {
|
---|
65 | if (mux) {
|
---|
66 | if (WiFi.isConnected(mux_id)) return 1;
|
---|
67 | String ip_str(host);
|
---|
68 | return (WiFi.createTCP(mux_id, ip_str, port))? 1 : 0;
|
---|
69 | }
|
---|
70 | else {
|
---|
71 | if (WiFi.isConnected()) return 1;
|
---|
72 | String ip_str(host);
|
---|
73 | return (WiFi.createTCP(ip_str, port))? 1 : 0;
|
---|
74 | }
|
---|
75 | };
|
---|
76 |
|
---|
77 | size_t write(uint8_t data) {
|
---|
78 | if (mux) {
|
---|
79 | if(WiFi.send(mux_id, &data, 1)){
|
---|
80 | return 1;
|
---|
81 | }
|
---|
82 | return 0;
|
---|
83 | }
|
---|
84 | else {
|
---|
85 | if(WiFi.send(&data, 1)){
|
---|
86 | return 1;
|
---|
87 | }
|
---|
88 | return 0;
|
---|
89 | }
|
---|
90 | };
|
---|
91 |
|
---|
92 | size_t write(const uint8_t *buf, size_t size){
|
---|
93 | if (mux) {
|
---|
94 | if(WiFi.send(mux_id, buf, size)){
|
---|
95 | return size;
|
---|
96 | }
|
---|
97 | return 0;
|
---|
98 | }
|
---|
99 | else {
|
---|
100 | if(WiFi.send(buf, size)){
|
---|
101 | return size;
|
---|
102 | }
|
---|
103 | return 0;
|
---|
104 | }
|
---|
105 | };
|
---|
106 |
|
---|
107 | int available(void){
|
---|
108 | if(mux) {
|
---|
109 | return WiFi.isDataAvailable(mux_id);
|
---|
110 | }
|
---|
111 | else {
|
---|
112 | return WiFi.isDataAvailable();
|
---|
113 | }
|
---|
114 | };
|
---|
115 |
|
---|
116 | int read(){
|
---|
117 | uint8_t data;
|
---|
118 | if(mux) {
|
---|
119 | if(WiFi.recv(mux_id, &data, 1) == 1) {
|
---|
120 | return data;
|
---|
121 | }
|
---|
122 | return (int)-1;
|
---|
123 | }
|
---|
124 | else {
|
---|
125 | if(WiFi.recv(&data, 1) == 1) {
|
---|
126 | return data;
|
---|
127 | }
|
---|
128 | return (int)-1;
|
---|
129 | }
|
---|
130 | };
|
---|
131 |
|
---|
132 | int read(uint8_t *buf, size_t size){
|
---|
133 | if(mux) {
|
---|
134 | return WiFi.recv(mux_id, buf, size);
|
---|
135 | }
|
---|
136 | else {
|
---|
137 | return WiFi.recv(buf, size);
|
---|
138 | }
|
---|
139 | };
|
---|
140 |
|
---|
141 | int peek(void){
|
---|
142 | return pbuffer->peek();
|
---|
143 | };
|
---|
144 |
|
---|
145 | void flush(void){};
|
---|
146 | void stop(void){
|
---|
147 | if(mux) {
|
---|
148 | if (WiFi.isConnected(mux_id)) {
|
---|
149 | WiFi.releaseTCP(mux_id);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | else {
|
---|
153 | if (WiFi.isConnected()) {
|
---|
154 | WiFi.releaseTCP();
|
---|
155 | }
|
---|
156 | }
|
---|
157 | };
|
---|
158 | uint8_t connected(void){
|
---|
159 | if(mux) {
|
---|
160 | return (WiFi.isConnected(mux_id))? 1 : 0;
|
---|
161 | }
|
---|
162 | else {
|
---|
163 | return (WiFi.isConnected())? 1 : 0;
|
---|
164 | }
|
---|
165 | };
|
---|
166 | operator bool(){
|
---|
167 | if(mux) {
|
---|
168 | return WiFi.isConnected(mux_id);
|
---|
169 | }
|
---|
170 | else {
|
---|
171 | return WiFi.isConnected();
|
---|
172 | }
|
---|
173 | };
|
---|
174 |
|
---|
175 | private:
|
---|
176 | ESP8266_RingBuffer *pbuffer;
|
---|
177 | uint8_t mux_id;
|
---|
178 | bool mux;
|
---|
179 | };
|
---|
180 |
|
---|
181 | #endif /* _CLIENT_ESP8366_H_ */
|
---|