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