MyTetra Share
Делитесь знаниями!
server.c
Время создания: 23.11.2018 15:26
Раздел: Electronics - Microcontrollers - STM32 - Hunter - v3.1

#include "server.h"


/*==============================================

##################################################################################

EXTERNAL VARIABLES

##################################################################################

==============================================*/

extern UART_HandleTypeDef huart6;

extern USART_prop_ptr usartProp;

//extern char dataPacket[64];





/*==============================================

##################################################################################

INTERNAL GLOBAL VARIABLES

##################################################################################

==============================================*/

struct tcp_pcb* server_pcb;

struct ServerStruct *ss;


char tcpDataPacket[TCP_WND];

uint8_t serverHasConnection = 0;


/*==============================================

##################################################################################

STATIC FUNCTIONS DEFINITIONS

##################################################################################

==============================================*/

static err_t tcpServerAccept(void *arg, struct tcp_pcb *newpcb, err_t err);

static void tcpServerCloseConnection(struct tcp_pcb *tpcb, struct ServerStruct *es);

static void tcpServerSendData(struct tcp_pcb *tpcb, struct ServerStruct *es);

static void tcpServerErrorClbck(void *arg, err_t err);

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

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

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




/*==============================================

##################################################################################

STATIC FUNCTIONS

##################################################################################

==============================================*/

/*==============================================

LWIP Client Incoming


tcp_recv(…) - Is used to initialize callback function

tcp_sent(…) - Specify a callback function

tcp_sndbuf(…) - Get the maximum amount of data that can be sent

tcp_write(…) - To enqueue the data

tcp_output(…) - To force the data to be sent

==============================================*/


static err_t tcpServerAccept(void *arg, struct tcp_pcb *newpcb, err_t err)

{

err_t ret_err;

LWIP_UNUSED_ARG(arg);

LWIP_UNUSED_ARG(err);

tcp_setprio(newpcb, TCP_PRIO_MIN);


ss = (struct ServerStruct *)mem_malloc(sizeof(struct ServerStruct));

if(ss != NULL)

{

ss->state = ES_S_ACCEPTED;

ss->server_pcb = newpcb;

ss->retries = 0;

ss->p = NULL;

HAL_UART_Transmit(&huart6, (uint8_t*)"srvacc\n\r", 8, 0x0F);

tcp_arg(newpcb, ss); //pass newly allocated es as argument to newpcb

tcp_recv(newpcb, tcpServerRecvClbck); //initialize tcp_recv callback function

tcp_err(newpcb, tcpServerErrorClbck); //initialize tcp_err callback function

tcp_sent(newpcb, tcpServerSentClbck); //initialize tcp_sent callback function

tcp_poll(newpcb, tcpServerPollClbck, 0); //initialize tcp_poll callback function

HAL_UART_Transmit(&huart6, (uint8_t*)"clbckdone\n\r", 11, 0x0F);

++serverHasConnection;

ret_err = ERR_OK;

}

else

{

tcpServerCloseConnection(newpcb, ss);

ret_err = ERR_MEM;

}

return ret_err;

}


/*==============================================

TCP Close Sonnection

==============================================*/

static void tcpServerCloseConnection(struct tcp_pcb *tpcb, struct ServerStruct *es)

{

HAL_UART_Transmit(&huart6, (uint8_t*)"close\n\r", 7, 0x0F);


if(serverHasConnection)

{

--serverHasConnection;

}

//remove all callbacks

tcp_arg(es->server_pcb, NULL);

tcp_sent(es->server_pcb, NULL);

tcp_recv(es->server_pcb, NULL);

tcp_err(es->server_pcb, NULL);

tcp_poll(es->server_pcb, NULL, 0);

if(es != NULL)

{

mem_free(es);

}

tcp_close(tpcb);

}


/*==============================================

TCP Send Data from Server

==============================================*/

static void tcpServerSendData(struct tcp_pcb *tpcb, struct ServerStruct *es)

{

HAL_UART_Transmit(&huart6, (uint8_t*)"snddat\n\r", 8, 0x0F);

struct pbuf *ptr;

err_t wr_err = ERR_OK;

while((wr_err == ERR_OK) &&

(es->p != NULL) &&

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

{

ptr = es->p;

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

if(wr_err == ERR_OK)

{

u16_t plen;

u8_t freed;

plen = ptr->len;

es->p = ptr->next;

if(es->p != NULL)

{

pbuf_ref(es->p);

}

do

{

freed = pbuf_free(ptr);

} while(freed == 0);

tcp_recved(tpcb, plen);

}

else if(wr_err == ERR_MEM)

{

es->p = ptr;

}

else

{

//other problem

}

}

}


/*==============================================

TCP Server Error Callback

==============================================*/

static void tcpServerErrorClbck(void *arg, err_t err)

{

HAL_UART_Transmit(&huart6, (uint8_t*)"err\n\r", 5, 0x0F);

}


/*==============================================

TCP Server Receive Callback

==============================================*/

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

{

HAL_UART_Transmit(&huart6, (uint8_t*)"rcvclb\n\r", 8, 0x0F);

err_t ret_err;

struct ServerStruct *es;

LWIP_ASSERT("arg != NULL", arg != NULL);

es = (struct ServerStruct *)arg;

if(p == NULL) //buffer is empty

{

es->state = ES_S_CLOSING;

if(es->p == NULL)

{

tcp_recved(tpcb, p->tot_len);

}

else

{

//tcp_sent(tpcb, tcpServerSent); //acknowledge received packet

tcpServerSendData(tpcb, es); //send remaining data

}

ret_err = ERR_OK;

}

else if(err != ERR_OK)

{

if(p != NULL) //some data in buffer

{

es->p = NULL;

pbuf_free(p);

}

ret_err = err;

}

else if(es->state == ES_S_ACCEPTED) //good packet received

{

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

//some action after receiving new part of data:

tcpServerSendData(tpcb, es);

strncpy(tcpDataPacket, p->payload, p->len); //copy data to array

tcpDataPacket[p->len] = '\0';

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

pbuf_free(p);

tcp_recved(tpcb, p->tot_len); //

ret_err = ERR_OK;

}

else if(es->state == ES_S_RECEIVED) //data transmission

{

if(es->p == NULL)

{

//ret_err = ERR_OK;

}

else

{

struct pbuf* ptr;

ptr = es->p;

pbuf_chain(ptr, p);

//tcpClientSendString(tcpDataPacket);

//tcpServerSendString(tcpDataPacket);

}

ret_err = ERR_OK;

}

else if(es->state == ES_S_CLOSING) //free buffer

{

tcp_recved(tpcb, p->tot_len);

es->p = NULL;

pbuf_free(p);

ret_err = ERR_OK;

}

else //free buffer

{

tcp_recved(tpcb, p->tot_len);

es->p = NULL;

pbuf_free(p);

ret_err = ERR_OK;

}


return ret_err;

}


/*==============================================

TCP Server Sent Callback

==============================================*/

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

{

HAL_UART_Transmit(&huart6, (uint8_t*)"sntclb\n\r", 8, 0x0F);

struct ServerStruct *es;

LWIP_UNUSED_ARG(len);

es = (struct ServerStruct*)arg;

es->retries = 0;

if(es->p != NULL)

{

tcpServerSendData(tpcb, es);

}

else

{

if(es->state == ES_S_CLOSING)

{ tcpServerCloseConnection(tpcb, es); }

}

return ERR_OK;

}


/*==============================================

TCP Server Poll Callback

==============================================*/

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

{

err_t ret_err;

struct ServerStruct *es;

es = (struct ServerStruct*)arg;

//turn on LED

if(es != NULL)

{

if(es->p != NULL)

{

//some error?

}

else

{

if(es->state == ES_S_CLOSING)

{

tcpServerCloseConnection(tpcb, es);

}

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

// if(usartProp.isText)

// {

// usartProp.isText = 0;

// }

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

}

ret_err = ERR_OK;

}

else

{

tcp_abort(tpcb);

ret_err = ERR_ABRT;

}

return ret_err;

}




/*==============================================

##################################################################################

FUNCTIONS

##################################################################################

==============================================*/


/*==============================================

LWIP Server Initialization

==============================================*/

void tcpServerInit(void)

{

server_pcb = tcp_new(); //create new tcp pcb structure

if(server_pcb != NULL)

{

err_t err;

err = tcp_bind(server_pcb, IP_ADDR_ANY, SERVER_LISTEN_PORT); //bind tcp port

if(err == ERR_OK)

{

HAL_UART_Transmit(&huart6, (uint8_t*)"srvini\n\r", 8, 0x0F);

server_pcb = tcp_listen(server_pcb); //start tcp listening

tcp_accept(server_pcb, tcpServerAccept); //callback function tcpServerAccept when connection occurs

}

else

{

memp_free(MEMP_TCP_PCB, server_pcb); //deallocate pcb

}

}

}


/*==============================================

Send String to Server (NOT!!! to connected Host)

==============================================*/

void tcpServerSendString(char* bufStr) //send string to server

{

if(serverHasConnection)

{

HAL_UART_Transmit(&huart6, (uint8_t*)"strfoo\n\r", 8, 0x0F);

// usartProp.isText = 1;


tcp_sent(ss->server_pcb, tcpServerSentClbck);

tcp_write(ss->server_pcb, (void*)bufStr, strlen(bufStr), 1);

tcp_recved(ss->server_pcb, strlen(bufStr));

}

else

{

HAL_UART_Transmit(&huart6, (uint8_t*)"noCli\n\r", 7, 0x0F);

}

// usartProp.usartBufCnt = 0;

}


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