/**
 *   This programis supplied to you by the Author in consideration of your agreement to the following terms, and your use or installation of 
 *   The Software and the use of The Documentation constitutes acceptance of these terms.
 *   If you do not agree with these terms, please do not use or install The Software.
 *   The Author grants you a personal, non-exclusive license, under author's copyrights in this original software, to use The Software.
 *   Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by the Author, including but not limited to any
 *   patent rights that may be infringed by your derivative works or by other works in which The Software may be incorporated.
 *   The Software and the Documentation are provided by the Author on an "AS IS" basis.  THE AUTHOR MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT
 *   LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE SOFTWARE OR ITS USE AND OPERATION
 *   ALONE OR IN COMBINATION WITH YOUR PRODUCTS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
 *   REPRODUCTION AND MODIFICATION OF THE SOFTWARE AND OR OF THE DOCUMENTATION, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
 *   STRICT LIABILITY OR OTHERWISE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 **/

// For more information write to fabboco@gmail.com


#ifndef TCPCLIENTH
#define TCPCLIENTH

#include <string.h>
#include <time.h>

#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"

#include "lwip/pbuf.h"
#include "lwip/tcp.h"

#define BUF_SIZE 2048

typedef struct TCP_CLIENT_T_
{
    struct tcp_pcb *tcp_pcb;
    ip_addr_t remote_addr;
    uint8_t buffer[BUF_SIZE];

    volatile int buffer_len;
    volatile bool data_ready;

    
    int sent_len;
    bool complete;
    int run_count;
    bool connected;
} TCP_CLIENT_T;

TCP_CLIENT_T *tcp_client_init(const char *tcp_address, u16_t tcp_port);
bool tcp_client_open(void *arg);
err_t tcp_client_close(void *arg);
err_t tcp_result(void *arg, int status);
err_t tcp_client_send(void *arg);
err_t tcp_client_send_and_wait(TCP_CLIENT_T *state, const uint8_t *tx, uint16_t tx_len, char *rx_out, uint16_t rx_max, uint32_t timeout_ms);

#endif