Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
/**
2
 * @file ESPAsyncTCPbuffer.h
3
 * @date  22.01.2016
4
 * @author Markus Sattler
5
 *
6
 * Copyright (c) 2015 Markus Sattler. All rights reserved.
7
 * This file is part of the Asynv TCP for ESP.
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with this library; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22
 *
23
 */
24
 
25
#ifndef ESPASYNCTCPBUFFER_H_
26
#define ESPASYNCTCPBUFFER_H_
27
 
28
//#define DEBUG_ASYNC_TCP(...)  while(((U0S >> USTXC) & 0x7F) != 0x00); os_printf( __VA_ARGS__ ); while(((U0S >> USTXC) & 0x7F) != 0x00)
29
//#define DEBUG_ASYNC_TCP ASYNC_TCP_DEBUG
30
#ifndef DEBUG_ASYNC_TCP
31
#define DEBUG_ASYNC_TCP(...)
32
#endif
33
 
34
#include <Arduino.h>
35
#include <cbuf.h>
36
 
37
#include "ESPAsyncTCP.h"
38
 
39
 
40
 
41
typedef enum {
42
    ATB_RX_MODE_NONE,
43
    ATB_RX_MODE_FREE,
44
    ATB_RX_MODE_READ_BYTES,
45
    ATB_RX_MODE_TERMINATOR,
46
    ATB_RX_MODE_TERMINATOR_STRING
47
} atbRxMode_t;
48
 
49
class AsyncTCPbuffer: public Print {
50
 
51
    public:
52
 
53
        typedef std::function<size_t(uint8_t * payload, size_t length)> AsyncTCPbufferDataCb;
54
        typedef std::function<void(bool ok, void * ret)> AsyncTCPbufferDoneCb;
55
        typedef std::function<bool(AsyncTCPbuffer * obj)> AsyncTCPbufferDisconnectCb;
56
 
57
        AsyncTCPbuffer(AsyncClient* c);
58
        virtual ~AsyncTCPbuffer();
59
 
60
        size_t write(String & data);
61
        size_t write(uint8_t data);
62
        size_t write(const char* data);
63
        size_t write(const char *data, size_t len);
64
        size_t write(const uint8_t *data, size_t len);
65
 
66
        void flush();
67
 
68
        void noCallback();
69
 
70
        void readStringUntil(char terminator, String * str, AsyncTCPbufferDoneCb done);
71
 
72
        // TODO implement read terminator non string
73
        //void readBytesUntil(char terminator, char *buffer, size_t length, AsyncTCPbufferDoneCb done);
74
        //void readBytesUntil(char terminator, uint8_t *buffer, size_t length, AsyncTCPbufferDoneCb done);
75
 
76
        void readBytes(char *buffer, size_t length, AsyncTCPbufferDoneCb done);
77
        void readBytes(uint8_t *buffer, size_t length, AsyncTCPbufferDoneCb done);
78
 
79
        // TODO implement
80
        // void setTimeout(size_t timeout);
81
 
82
        void onData(AsyncTCPbufferDataCb cb);
83
        void onDisconnect(AsyncTCPbufferDisconnectCb cb);
84
 
85
        IPAddress remoteIP();
86
        uint16_t  remotePort();
87
        IPAddress localIP();
88
        uint16_t  localPort();
89
 
90
        bool connected();
91
 
92
        void stop();
93
        void close();
94
 
95
    protected:
96
        AsyncClient* _client;
97
        cbuf * _TXbufferRead;
98
        cbuf * _TXbufferWrite;
99
        cbuf * _RXbuffer;
100
        atbRxMode_t _RXmode;
101
        size_t _rxSize;
102
        char _rxTerminator;
103
        uint8_t * _rxReadBytesPtr;
104
        String * _rxReadStringPtr;
105
 
106
        AsyncTCPbufferDataCb _cbRX;
107
        AsyncTCPbufferDoneCb _cbDone;
108
        AsyncTCPbufferDisconnectCb _cbDisconnect;
109
 
110
        void _attachCallbacks();
111
        void _sendBuffer();
112
        void _on_close();
113
        void _rxData(uint8_t *buf, size_t len);
114
        size_t _handleRxBuffer(uint8_t *buf, size_t len);
115
 
116
};
117
 
118
#endif /* ESPASYNCTCPBUFFER_H_ */