[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 |
|
---|
| 25 | class ESP8266Client: public Client, public ESP8266 {
|
---|
| 26 | public:
|
---|
[180] | 27 | ESP8266Client(void) {
|
---|
| 28 | pbuffer = &rx_buffer[0];
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[144] | 31 | /*
|
---|
| 32 | * @retval 0 : faild
|
---|
| 33 | * @retval 1 : success
|
---|
| 34 | */
|
---|
| 35 | int connect(IPAddress ip, uint16_t port) {
|
---|
[189] | 36 | if (isConnected()) return 1;
|
---|
[144] | 37 | String ip_str;
|
---|
[180] | 38 | ip_str.concat(ip[0]); ip_str.concat(".");
|
---|
| 39 | ip_str.concat(ip[1]); ip_str.concat(".");
|
---|
| 40 | ip_str.concat(ip[2]); ip_str.concat(".");
|
---|
[144] | 41 | ip_str.concat(ip[3]);
|
---|
[180] | 42 | return (createTCP(ip_str, port))? 1 : 0;
|
---|
[144] | 43 | };
|
---|
| 44 |
|
---|
| 45 | int connect(const char *host, uint16_t port) {
|
---|
[189] | 46 | if (isConnected()) return 1;
|
---|
[144] | 47 | String ip_str(host);
|
---|
[180] | 48 | return (createTCP(ip_str, port))? 1 : 0;
|
---|
[144] | 49 | };
|
---|
| 50 |
|
---|
| 51 | size_t write(uint8_t data) {
|
---|
| 52 | if(send(&data, 1)){
|
---|
| 53 | return 1;
|
---|
[180] | 54 | }
|
---|
| 55 | return 0;
|
---|
[144] | 56 | };
|
---|
[189] | 57 |
|
---|
[144] | 58 | size_t write(const uint8_t *buf, size_t size){
|
---|
| 59 | if(send(buf, size)){
|
---|
| 60 | return size;
|
---|
| 61 | }
|
---|
[180] | 62 | return 0;
|
---|
[144] | 63 | };
|
---|
[180] | 64 |
|
---|
[169] | 65 | int available(){
|
---|
[180] | 66 | return isDataAvailable();
|
---|
[169] | 67 | };
|
---|
[180] | 68 |
|
---|
[144] | 69 | int read(){
|
---|
[180] | 70 | uint8_t data;
|
---|
| 71 | if(recv(&data, 1) == 1) {
|
---|
| 72 | return data;
|
---|
[144] | 73 | }
|
---|
[180] | 74 | return (int)-1;
|
---|
[144] | 75 | };
|
---|
[180] | 76 |
|
---|
| 77 | int read(uint8_t *buf, size_t size){
|
---|
| 78 | return recv(buf, size);
|
---|
| 79 | };
|
---|
| 80 |
|
---|
| 81 | int peek(){
|
---|
| 82 | return pbuffer->peek();
|
---|
| 83 | };
|
---|
| 84 |
|
---|
[144] | 85 | void flush(){};
|
---|
[189] | 86 | void stop(){ if (isConnected()) { releaseTCP(); }}
|
---|
[180] | 87 | uint8_t connected(){return (isConnected())? 1 : 0;};
|
---|
| 88 | operator bool(){return isConnected();};
|
---|
[144] | 89 | private:
|
---|
[180] | 90 | ESP8266_RingBuffer *pbuffer;
|
---|
[144] | 91 | };
|
---|
| 92 |
|
---|
| 93 | #endif /* _CLIENT_ESP8366_H_ */
|
---|