[144] | 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_
|
---|
[184] | 20 |
|
---|
[144] | 21 | #include "ESP8266.h"
|
---|
[189] | 22 | #include "IPAddress.h"
|
---|
| 23 | #include "Client.h"
|
---|
[144] | 24 |
|
---|
[212] | 25 | class ESP8266Client: public Client {
|
---|
[144] | 26 | public:
|
---|
[180] | 27 | ESP8266Client(void) {
|
---|
[212] | 28 | mux_id = 0;
|
---|
| 29 | mux = false;
|
---|
| 30 | pbuffer = &(WiFi.rx_buffer[0]);
|
---|
[180] | 31 | }
|
---|
[212] | 32 |
|
---|
| 33 | ESP8266Client(uint8_t _mux_id) {
|
---|
| 34 | mux_id = _mux_id;
|
---|
| 35 | mux = true;
|
---|
| 36 | pbuffer = &(WiFi.rx_buffer[mux_id]);
|
---|
| 37 | }
|
---|
[180] | 38 |
|
---|
[144] | 39 | /*
|
---|
| 40 | * @retval 0 : faild
|
---|
| 41 | * @retval 1 : success
|
---|
| 42 | */
|
---|
| 43 | int connect(IPAddress ip, uint16_t port) {
|
---|
[212] | 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 | }
|
---|
[144] | 62 | };
|
---|
| 63 |
|
---|
| 64 | int connect(const char *host, uint16_t port) {
|
---|
[212] | 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 | }
|
---|
[144] | 75 | };
|
---|
| 76 |
|
---|
| 77 | size_t write(uint8_t data) {
|
---|
[212] | 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 | }
|
---|
[144] | 90 | };
|
---|
[189] | 91 |
|
---|
[144] | 92 | size_t write(const uint8_t *buf, size_t size){
|
---|
[212] | 93 | if (mux) {
|
---|
| 94 | if(WiFi.send(mux_id, buf, size)){
|
---|
| 95 | return size;
|
---|
| 96 | }
|
---|
| 97 | return 0;
|
---|
[144] | 98 | }
|
---|
[212] | 99 | else {
|
---|
| 100 | if(WiFi.send(buf, size)){
|
---|
| 101 | return size;
|
---|
| 102 | }
|
---|
| 103 | return 0;
|
---|
| 104 | }
|
---|
[144] | 105 | };
|
---|
[180] | 106 |
|
---|
[212] | 107 | int available(void){
|
---|
| 108 | if(mux) {
|
---|
| 109 | return WiFi.isDataAvailable(mux_id);
|
---|
| 110 | }
|
---|
| 111 | else {
|
---|
| 112 | return WiFi.isDataAvailable();
|
---|
| 113 | }
|
---|
[169] | 114 | };
|
---|
[180] | 115 |
|
---|
[144] | 116 | int read(){
|
---|
[180] | 117 | uint8_t data;
|
---|
[212] | 118 | if(mux) {
|
---|
| 119 | if(WiFi.recv(mux_id, &data, 1) == 1) {
|
---|
| 120 | return data;
|
---|
| 121 | }
|
---|
| 122 | return (int)-1;
|
---|
[144] | 123 | }
|
---|
[212] | 124 | else {
|
---|
| 125 | if(WiFi.recv(&data, 1) == 1) {
|
---|
| 126 | return data;
|
---|
| 127 | }
|
---|
| 128 | return (int)-1;
|
---|
| 129 | }
|
---|
[144] | 130 | };
|
---|
[180] | 131 |
|
---|
| 132 | int read(uint8_t *buf, size_t size){
|
---|
[212] | 133 | if(mux) {
|
---|
| 134 | return WiFi.recv(mux_id, buf, size);
|
---|
| 135 | }
|
---|
| 136 | else {
|
---|
| 137 | return WiFi.recv(buf, size);
|
---|
| 138 | }
|
---|
[180] | 139 | };
|
---|
| 140 |
|
---|
[212] | 141 | int peek(void){
|
---|
[180] | 142 | return pbuffer->peek();
|
---|
| 143 | };
|
---|
| 144 |
|
---|
[212] | 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 |
|
---|
[144] | 175 | private:
|
---|
[180] | 176 | ESP8266_RingBuffer *pbuffer;
|
---|
[212] | 177 | uint8_t mux_id;
|
---|
| 178 | bool mux;
|
---|
[144] | 179 | };
|
---|
| 180 |
|
---|
| 181 | #endif /* _CLIENT_ESP8366_H_ */
|
---|