summaryrefslogtreecommitdiff
path: root/libraries/ESPAsyncTCP/src/ESPAsyncTCPbuffer.h
blob: 08a57c7e2daf1b64e815bae03ba49ff2cabd6154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
 * @file ESPAsyncTCPbuffer.h
 * @date  22.01.2016
 * @author Markus Sattler
 *
 * Copyright (c) 2015 Markus Sattler. All rights reserved.
 * This file is part of the Asynv TCP for ESP.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifndef ESPASYNCTCPBUFFER_H_
#define ESPASYNCTCPBUFFER_H_

//#define DEBUG_ASYNC_TCP(...)  while(((U0S >> USTXC) & 0x7F) != 0x00); os_printf( __VA_ARGS__ ); while(((U0S >> USTXC) & 0x7F) != 0x00)
//#define DEBUG_ASYNC_TCP ASYNC_TCP_DEBUG
#ifndef DEBUG_ASYNC_TCP
#define DEBUG_ASYNC_TCP(...)
#endif

#include <Arduino.h>
#include <cbuf.h>

#include "ESPAsyncTCP.h"



typedef enum {
    ATB_RX_MODE_NONE,
    ATB_RX_MODE_FREE,
    ATB_RX_MODE_READ_BYTES,
    ATB_RX_MODE_TERMINATOR,
    ATB_RX_MODE_TERMINATOR_STRING
} atbRxMode_t;

class AsyncTCPbuffer: public Print {

    public:

        typedef std::function<size_t(uint8_t * payload, size_t length)> AsyncTCPbufferDataCb;
        typedef std::function<void(bool ok, void * ret)> AsyncTCPbufferDoneCb;
        typedef std::function<bool(AsyncTCPbuffer * obj)> AsyncTCPbufferDisconnectCb;

        AsyncTCPbuffer(AsyncClient* c);
        virtual ~AsyncTCPbuffer();

        size_t write(String & data);
        size_t write(uint8_t data);
        size_t write(const char* data);
        size_t write(const char *data, size_t len);
        size_t write(const uint8_t *data, size_t len);

        void flush();

        void noCallback();

        void readStringUntil(char terminator, String * str, AsyncTCPbufferDoneCb done);

        // TODO implement read terminator non string
        //void readBytesUntil(char terminator, char *buffer, size_t length, AsyncTCPbufferDoneCb done);
        //void readBytesUntil(char terminator, uint8_t *buffer, size_t length, AsyncTCPbufferDoneCb done);

        void readBytes(char *buffer, size_t length, AsyncTCPbufferDoneCb done);
        void readBytes(uint8_t *buffer, size_t length, AsyncTCPbufferDoneCb done);

        // TODO implement
        // void setTimeout(size_t timeout);

        void onData(AsyncTCPbufferDataCb cb);
        void onDisconnect(AsyncTCPbufferDisconnectCb cb);

        IPAddress remoteIP();
        uint16_t  remotePort();
        IPAddress localIP();
        uint16_t  localPort();

        bool connected();

        void stop();
        void close();

    protected:
        AsyncClient* _client;
        cbuf * _TXbufferRead;
        cbuf * _TXbufferWrite;
        cbuf * _RXbuffer;
        atbRxMode_t _RXmode;
        size_t _rxSize;
        char _rxTerminator;
        uint8_t * _rxReadBytesPtr;
        String * _rxReadStringPtr;

        AsyncTCPbufferDataCb _cbRX;
        AsyncTCPbufferDoneCb _cbDone;
        AsyncTCPbufferDisconnectCb _cbDisconnect;

        void _attachCallbacks();
        void _sendBuffer();
        void _on_close();
        void _rxData(uint8_t *buf, size_t len);
        size_t _handleRxBuffer(uint8_t *buf, size_t len);

};

#endif /* ESPASYNCTCPBUFFER_H_ */