MyTetra Share
Делитесь знаниями!
client.c
Время создания: 28.11.2018 17:34
Раздел: Electronics - Microcontrollers - STM32 - Hunter - v3.2

#include "client.h"


extern UART_HandleTypeDef huart6;

extern USART_prop_ptr usartProp;

extern char dataPacket[64];


//u8_t data[100];


struct tcp_pcb *client_pcb;


__IO uint32_t messageCounter = 0;


struct ClientStruct *cs;


uint8_t clientHasConnection = 0;


//static void tcpClientCloseConnection(struct tcp_pcb *tpcb, struct ClientStruct *es);

static err_t tcpClinetState(void *arg, struct tcp_pcb *tcpb, err_t err);

static err_t tcpClientRecv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);

static void tcpClientTransmit(struct tcp_pcb *tpcb, struct ClientStruct *es);

static err_t tcpClientSent(void *arg, struct tcp_pcb *tpcb, u16_t len);

static err_t tcpClientPoll(void *arg, struct tcp_pcb *tpcb);


void tcpClientCloseConnection(struct tcp_pcb *tpcb, struct ClientStruct *es)

{

if(clientHasConnection)

--clientHasConnection;

tcp_recv(tpcb, NULL);

tcp_sent(tpcb, NULL);

tcp_poll(tpcb, NULL, 0);

if(es != NULL)

{

mem_free(es);

}

tcp_close(tpcb);

}


static err_t tcpClinetState(void *arg, struct tcp_pcb *tpcb, err_t err)

{

struct ClientStruct *es = NULL;

if(err == ERR_OK) //no error

{

es = (struct ClientStruct *)mem_malloc(sizeof(struct ClientStruct));

if(es != NULL)

{

es->state = ES_CONNECTED;

es->pcb = tpcb;

es->p_tx = pbuf_alloc(PBUF_TRANSPORT, strlen((char*)dataPacket), PBUF_POOL);

if(es->p_tx) //if memory allocated successfully

{

pbuf_take(es->p_tx, (char*)dataPacket, strlen((char*)dataPacket)); //copy data to pbuf

tcp_arg(tpcb, es); //pass newly allocated es structure as argument to tpcb

tcp_recv(tpcb, tcpClientRecv); //init LwIP tcp_recv callback function

tcp_sent(tpcb, tcpClientSent); //init LwIP tcp_sent callback function

tcp_poll(tpcb, tcpClientPoll, 1); //0=0.5ms, 1=1ms, 2=2ms

tcpClientTransmit(tpcb, es); //send data

++clientHasConnection;

return ERR_OK;

}

}

else

{

tcpClientCloseConnection(client_pcb, cs);

return ERR_MEM;

}

}

else //disconnect if error

{

tcpClientCloseConnection(client_pcb, cs);

}

return err;

}


static err_t tcpClientRecv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)

{

struct ClientStruct *es;

err_t ret_err;

es = (struct ClientStruct*)arg;

if(p == NULL)

{

es->state = ES_CLOSING;

if(es->p_tx == NULL)

{

tcpClientCloseConnection(tpcb, es);

}

ret_err = ERR_OK;

}

else if(err != ERR_OK)

{

if(p != NULL)

{

pbuf_free(p);

}

ret_err = err;

}

else if(es->state == ES_CONNECTED)

{

++messageCounter;

tcp_recved(tpcb, p->tot_len);

es->p_tx = p;

strncpy(dataPacket, es->p_tx->payload, es->p_tx->len);

dataPacket[es->p_tx->len] = '\0';

/////////////////////=========================////////////////////////

//+++++++++++++++++++++++++++++++++++++++++++++

//-------------some action after data receiving

// HAL_UART_Transmit(&huart6, (uint8_t*)dataPacket, es->p_tx->len, 0x1000);

// usartProp.isText = 1;

//HAL_UART_Transmit(&huart6, (uint8_t*)"\r", 2, 0x1000);

// dataPacket[0] = '\0';

// usartProp.isText = 0;

//---------------------------------------------

//+++++++++++++++++++++++++++++++++++++++++++++

/////////////////////=========================////////////////////////

ret_err = ERR_OK;

}

else if(es->state == ES_RECEIVED)

{

ret_err = ERR_OK;

}

else

{

tcp_recved(tpcb, p->tot_len); //ACK data reception

pbuf_free(p);

ret_err = ERR_OK;

}

return ret_err;

}


static void tcpClientTransmit(struct tcp_pcb *tpcb, struct ClientStruct *es)

{

struct pbuf *ptr;

err_t wr_err = ERR_OK;

while((wr_err == ERR_OK) &&

(es->p_tx != NULL) &&

(es->p_tx->len <= tcp_sndbuf(tpcb)))

{

ptr = es->p_tx;

wr_err = tcp_write(tpcb, ptr->payload, ptr->len, 1);

if(wr_err == ERR_OK)

{

es->p_tx = ptr->next;

if(es->p_tx != NULL)

{

pbuf_ref(es->p_tx);

}

pbuf_free(ptr);

}

else if(wr_err == ERR_MEM)

{

es->p_tx = ptr;

}

else //other problem

{

}

}

}


static err_t tcpClientSent(void *arg, struct tcp_pcb *tpcb, u16_t len)

{

struct ClientStruct *es;

LWIP_UNUSED_ARG(len);

es = (struct ClientStruct*)arg;

if(es->p_tx != NULL)

{

tcpClientTransmit(tpcb, es);

}

return ERR_OK;

}


static err_t tcpClientPoll(void *arg, struct tcp_pcb *tpcb)

{

struct ClientStruct *es;

err_t ret_err;

es = (struct ClientStruct*)arg;

if(es != NULL)

{

if(es->p_tx != NULL)

{

}

else

{

if(es->state == ES_CLOSING) //server sent closing request

{

tcpClientCloseConnection(tpcb, es);

}

}

ret_err = ERR_OK;

}

else

{

tcp_abort(tpcb);

ret_err = ERR_ABRT;

}

return ret_err;

}


void tcpClientConnect(uint8_t* ipAddrDest, uint16_t portDest)

{

ip_addr_t destIP;

usartProp.isTcpConnect = TCP_TRY_CONN;

client_pcb = tcp_new();

if(client_pcb != NULL)

{

IP4_ADDR(&destIP, ipAddrDest[0],ipAddrDest[1],ipAddrDest[2],ipAddrDest[3]);

//IP4_ADDR(&destIP, 10,124,4,25);

tcp_connect(client_pcb, &destIP, portDest, tcpClinetState);

}

}


void tcpClientSendString(char* bufStr)

{

tcp_sent(client_pcb, tcpClientSent);

tcp_write(client_pcb, (void*)bufStr, strlen(bufStr), 0);

tcp_output(client_pcb);

usartProp.usartBufCnt = 0;

usartProp.isText = 0;

}


void tcpClientSendInteger(uint32_t val)

{

tcp_sent(client_pcb, tcpClientSent);

tcp_write(client_pcb, (void*)val, 4, 2);

tcp_output(client_pcb);

}


Так же в этом разделе:
 
MyTetra Share v.0.53
Яндекс индекс цитирования