/*! \brief parse the incoming request * parse the HTML request and send file * * \param pxNetCon Input. The netconn to use to send and receive data. * */ static void prvweb_ParseHTMLRequest( struct netconn *pxNetCon ) { struct netbuf *pxRxBuffer; portCHAR *pcRxString; unsigned portSHORT usLength; static unsigned portLONG ulPageHits = 0; #if ( (LWIP_VERSION) == ((1U << 24) | (3U << 16) | (2U << 8) | (LWIP_VERSION_RC)) ) /* We expect to immediately get data. */ pxRxBuffer = netconn_recv( pxNetCon ); #else while(netconn_recv( pxNetCon, &pxRxBuffer) != ERR_OK) { vTaskDelay( webSHORT_DELAY ); } #endif if( pxRxBuffer != NULL ) { /* Where is the data? */ netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength ); /* Is this a GET? We don't handle anything else. */ if(( NULL != pcRxString ) && ( !strncmp( pcRxString, "GET", 3 ) )) { /* Update the hit count. */ ulPageHits++; sprintf( cPageHits, "%d", (int)ulPageHits ); /* Write out the HTTP OK header. */ netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY ); /* Generate the dynamic page... First the page header. */ strcpy( cDynamicPage, webHTML_START ); /* ... Then the hit count... */ strcat( cDynamicPage, cPageHits ); strcat( cDynamicPage, "<p><pre>Task State Priority Stack #<br>************************************************<br>" ); /* ... Then the list of tasks and their status... */ vTaskList( ( signed portCHAR * ) cDynamicPage + strlen( cDynamicPage ) ); /* ... Finally the page footer. */ strcat( cDynamicPage, webHTML_END ); /* Write out the dynamically generated page. */ netconn_write( pxNetCon, cDynamicPage, (u16_t) strlen( cDynamicPage ), NETCONN_COPY ); } netbuf_delete( pxRxBuffer ); } netconn_close( pxNetCon ); netconn_delete( pxNetCon ); }
void tcpecho6(void) { struct netconn *conn, *newconn; err_t err; conn = netconn_new(NETCONN_TCP_IPV6); netconn_bind_ip6(conn, IP6_ADDR_ANY, TCP_ECHO_PORT); netconn_listen(conn); while(1) { err = netconn_accept(conn, &newconn); if(err == ERR_OK) { struct netbuf *buf; void *data; u16_t len; while(netconn_recv(newconn, &buf) == ERR_OK) { do { netbuf_data(buf, &data, &len); err = netconn_write(newconn, data, len, NETCONN_COPY); if(err != ERR_OK) printf("netconn_write() error\n"); }while(netbuf_next(buf) >= 0); netbuf_delete(buf); } netconn_delete(newconn); } } }
void message_server_thread(void *arg) { struct netconn *conn; struct netbuf *buf; static uint8_t buffer[4096]; err_t err; LWIP_UNUSED_ARG(arg); chRegSetThreadName("rpc_message"); conn = netconn_new(NETCONN_UDP); if (conn == NULL) { chSysHalt("Cannot create SimpleRPC message server connection (out of memory)."); } netconn_bind(conn, NULL, MSG_SERVER_PORT); while (1) { err = netconn_recv(conn, &buf); if (err == ERR_OK) { netbuf_copy(buf, buffer, buf->p->tot_len); message_process(buffer, buf->p->tot_len, message_callbacks, message_callbacks_len); } netbuf_delete(buf); } }
static void lwip_perf_thread(void *arg) { static struct netconn *conn; static struct netbuf *buf; //static ip_addr_t *addr; static unsigned short port; char *buffer; lwip_perf_cmd_t cmd; err_t err = ERR_OK; uint16_t len, i, j; LWIP_UNUSED_ARG(arg); conn = netconn_new(NETCONN_UDP); LWIP_ASSERT("con != NULL", conn != NULL); netconn_bind(conn, NULL, 7); // echo port buffer = malloc(PERF_MAX_PAYLOAD_SIZE); assert(buffer); while (1) { err = netconn_recv(conn, &buf); if (err == ERR_OK) { //addr = netbuf_fromaddr(buf); port = netbuf_fromport(buf); uprintf(UPRINT_INFO, UPRINT_BLK_NET, "LWIP perf Rx: port=%d\n", port); len = netbuf_copy(buf, (char *)&cmd, sizeof(lwip_perf_cmd_t)); /* no need netconn_connect here, since the netbuf contains the address */ if(len != sizeof(lwip_perf_cmd_t)) { LWIP_DEBUGF(LWIP_DBG_ON, ("netbuf_copy failed\n")); } else { /* check packet size */ if(cmd.nSize>PERF_MAX_PAYLOAD_SIZE || cmd.nSize==0) cmd.nSize = PERF_MAX_PAYLOAD_SIZE; /* link buffer to netbuf */ err = netbuf_ref(buf, buffer, cmd.nSize); LWIP_DEBUGF(LWIP_DBG_ON, ("lwip perf: nPacket=%d nSize=%d nDelay=%d\n", cmd.nPacket, cmd.nSize, cmd.nDelay)); for(i=0; i<cmd.nPacket && err==ERR_OK; i++) { /* simulate buffer construction */ for(j=0; j<cmd.nSize; j++) buffer[j] = (j&0xFF); /* send packet now */ err = netconn_send(conn, buf); if(err != ERR_OK) { LWIP_DEBUGF(LWIP_DBG_ON, ("netconn_send failed: %d\n", (int)err)); } if(cmd.nDelay) task_delay(cmd.nDelay); } LWIP_DEBUGF(LWIP_DBG_ON, ("lwip perf: send %d packets\n", i)); } netbuf_delete(buf); } } free(buffer); }
/*-----------------------------------------------------------------------------------*/ static void udpecho_thread(void *arg) { static struct netconn *conn; static struct netbuf *buf; static struct ip_addr *addr; static unsigned short port; char buffer[256]; LWIP_UNUSED_ARG(arg); conn = netconn_new(NETCONN_UDP); /* Create a new netconnect */ LWIP_ASSERT("con != NULL", conn != NULL); netconn_bind(conn, IP_ADDR_ANY, UDP_ECHO_PORT); /* udpecho using port 7 */ while (1) { buf = netconn_recv(conn); /* received data to buffer */ if (buf != NULL) { addr = netbuf_fromaddr(buf); /* get client's IP address */ port = netbuf_fromport(buf); /* get client's port */ netconn_connect(conn, addr, port); /* connect to client */ netbuf_copy(buf, buffer, buf->p->tot_len); /* set echo data */ buffer[buf->p->tot_len] = '\0'; buf->addr = NULL; netconn_send(conn, buf); /* send data back to client */ LWIP_DEBUGF(LWIP_DBG_ON, ("got %s\n", buffer)); netbuf_delete(buf); /* release buffer */ } } }
void vAmpmNetTestTask( void *pvParameters ) { char *pt = pvParameters; struct netconn *ampm_net; struct ip_addr remote_addr; u16_t remote_port = 1880; struct netbuf *data; err_t err; ((uint8_t *)&remote_addr)[0] = 118; ((uint8_t *)&remote_addr)[1] = 69; ((uint8_t *)&remote_addr)[2] = 60; ((uint8_t *)&remote_addr)[3] = 174; ampm_net = netconn_new(NETCONN_TCP); err = netconn_connect(ampm_net, &remote_addr, remote_port); err = netconn_write(ampm_net, pt, strlen(pt), NETCONN_COPY); while(1) { data = netconn_recv(ampm_net); if(data) { //netconn_write(ampm_net, (uint8_t *)sendfile, strlen(sendfile), NETCONN_COPY); netconn_write(ampm_net, data->p->payload, data->p->len, NETCONN_COPY); netbuf_delete(data); } } }
int lwip_sock_read(void *conn, unsigned char *buff, unsigned long len) { struct netbuf *new_buf=0; unsigned char *data; int data_len=0; int ret,newret; SYSCALL_DEBUG(" SOCK read :%x len:%d \n",buff,len); mutexLock(g_netBH_lock); ret=netconn_recv(conn, &new_buf); mutexUnLock(g_netBH_lock); if (ret!=ERR_OK){ SYSCALL_DEBUG(" Fail to recv data: %x newret:%x(%d) \n",ret,-ret,-ret); return 0; } netbuf_data(new_buf,&data,&data_len); SYSCALL_DEBUG(" SUCESS to recv data:%d ret:%d\n",data_len,ret); if (data_len > 0){ ut_memcpy(buff,data,ut_min(data_len,len)); ret = ut_min(data_len,len); }else{ ret = 0; } mutexLock(g_netBH_lock); netbuf_delete(new_buf); mutexUnLock(g_netBH_lock); return ret; }
/*-----------------------------------------------------------------------------------*/ static void udpecho_thread(void *arg) { err_t err, recv_err; LWIP_UNUSED_ARG(arg); conn = netconn_new(NETCONN_UDP); if (conn!= NULL) { err = netconn_bind(conn, IP_ADDR_ANY, 7); if (err == ERR_OK) { while (1) { recv_err = netconn_recv(conn, &buf); if (recv_err == ERR_OK) { addr = netbuf_fromaddr(buf); port = netbuf_fromport(buf); netconn_connect(conn, addr, port); buf->addr.addr = 0; netconn_send(conn,buf); netbuf_delete(buf); } } } else { netconn_delete(conn); } } }
static int processSrqIo(user_data_t * user_data) { struct netbuf *inbuf; char* buf; u16_t buflen; if (netconn_recv(user_data->control_io, &inbuf) != ERR_OK) { goto fail1; } if (netconn_err(user_data->control_io) != ERR_OK) { goto fail2; } netbuf_data(inbuf, (void**) &buf, &buflen); if (buflen > 0) { // TODO process control } else { //goto fail2; } netbuf_delete(inbuf); return 0; fail2: netbuf_delete(inbuf); fail1: closeSrqIo(user_data); return 0; }
void udpecho_entry(void *parameter) { struct netconn *conn; struct netbuf *buf; struct ip_addr *addr; unsigned short port; conn = netconn_new(NETCONN_UDP); netconn_bind(conn, IP_ADDR_ANY, 7); while(1) { /* received data to buffer */ buf = netconn_recv(conn); addr = netbuf_fromaddr(buf); port = netbuf_fromport(buf); /* send the data to buffer */ netconn_connect(conn, addr, port); /* reset address, and send to client */ buf->addr = RT_NULL; netconn_send(conn, buf); /* release buffer */ netbuf_delete(buf); } }
static size_t read(void *ip, uint8_t *bp, size_t n) { NetStream *sp = ip; err_t err; /* If last input buffer was completely consumed, wait for a new packet. */ while (sp->inbuf == NULL) { /* Wait for new packet. */ err = netconn_recv(sp->conn, &sp->inbuf); if (err != ERR_OK) { /* Connection closed (or any other errors). */ return 0; } } netbuf_copy_partial(sp->inbuf, bp, n, sp->in_offset); sp->in_offset += n; /* Check if there is more data to read. */ if (sp->in_offset >= netbuf_len(sp->inbuf)) { n -= (sp->in_offset - netbuf_len(sp->inbuf)); netbuf_delete(sp->inbuf); sp->in_offset = 0; sp->inbuf = NULL; } return n; }
/* This function processes an incomming connection */ static void process_connection(struct netconn *conn) { struct netbuf *inbuf; char *rq; int len; /* Read data from the connection into the netbuf inbuf. * We assume that the full request is in the netbuf. */ inbuf = netconn_recv(conn); /* Get the pointer to the dta in the first netbuf * fragment which we hope contains the request.*/ netbuf_data(inbuf, &rq, &len); /* check if the request was an HTTP "GET"/\r\n". */ if(rq[0] == 'G' && rq[1] == 'E' && rq[2] == 'T' && rq[3] == ' ' && rq[4] == '/' && rq[5] == '\r' && rq[6] == '\n') { /* send the header.*/ netconn_write(conn, http_html_hdr, sizeof(http_html_hdr), NETCONN_NOCOPY); /* send the acutal web pages */ netconn_write(conn, indexdata, size(indexdata), NETCONN_NOCOPY); /* Close the connection */ netconn_close(conn); } }
void TaskLWIP(void * pvArg) { struct netconn *__pstConn, *__pstNewConn; struct netbuf *__pstNetbuf; char *rq; u16 len; // 初始化LwIP vlwIPInit(); // 设置LwIP,包括添加配置网络接口、建立接收任务等工作 SetLwIP(); __pstConn = netconn_new(NETCONN_TCP); netconn_bind(__pstConn, NULL,80); netconn_listen(__pstConn); while(1) { __pstNewConn = netconn_accept(__pstConn); if(__pstNewConn != NULL) { __pstNetbuf = netconn_recv(__pstNewConn); if(__pstNetbuf != NULL) { netbuf_data(__pstNetbuf,&rq,&len); netconn_write(__pstNewConn, rq, len, NETCONN_COPY); netbuf_delete(__pstNetbuf); } netconn_close(__pstNewConn); // while(netconn_delete(__pstNewConn) != ERR_OK) // OSTimeDly(10); } } }
static void data_udp_rx_serve(struct netconn *conn) { BaseSequentialStream *chp = getActiveUsbSerialStream(); static uint8_t count = 0; struct netbuf *inbuf; char *buf; uint16_t buflen = 0; uint16_t i = 0; err_t err; /* * Read the data from the port, blocking if nothing yet there. * We assume the request (the part we care about) is in one netbuf */ err = netconn_recv(conn, &inbuf); if (err == ERR_OK) { netbuf_data(inbuf, (void **)&buf, &buflen); chprintf(chp, "\r\nsensor rx (from FC): %d ", count++); for(i=0; i<buflen; ++i) { chprintf(chp, "%c", buf[i]); } chprintf(chp, "\r\n"); } netconn_close(conn); /* Delete the buffer (netconn_recv gives us ownership, * so we have to make sure to deallocate the buffer) */ netbuf_delete(inbuf); }
static int processIo(user_data_t * user_data) { struct netbuf *inbuf; char* buf; u16_t buflen; if (netconn_recv(user_data->io, &inbuf) != ERR_OK) { goto fail1; } if (netconn_err(user_data->io) != ERR_OK) { goto fail2; } netbuf_data(inbuf, (void**) &buf, &buflen); if (buflen > 0) { SCPI_Input(&scpi_context, buf, buflen); } else { //goto fail2; } netbuf_delete(inbuf); return 0; fail2: netbuf_delete(inbuf); fail1: closeIo(user_data); return 0; }
int netstack_socket_recv(struct netstack_socket *sk, struct netstack_socket_buf *buf, int timeout) { err_t err; struct netbuf *nb; struct netconn *conn; if (!sk || !sk->priv || !buf) { return VMM_EINVALID; } conn = sk->priv; if (0 < timeout) { netconn_set_recvtimeout(conn, timeout); } else { netconn_set_recvtimeout(conn, 0); } buf->data = NULL; buf->len = 0; err = netconn_recv(conn, &nb); if (err == ERR_TIMEOUT) { return VMM_ETIMEDOUT; } else if (err != ERR_OK) { return VMM_EFAIL; } netbuf_data(nb, &buf->data, &buf->len); buf->priv = nb; return VMM_OK; }
/* Private functions ---------------------------------------------------------*/ static void tcpecho_thread(void *arg) { struct netconn *conn, *newconn; err_t err, accept_err; struct netbuf *buf; void *data; u16_t len; LWIP_UNUSED_ARG(arg); /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); if (conn!=NULL) { /* Bind connection to well known port number 7. */ err = netconn_bind(conn, NULL, 7); if (err == ERR_OK) { /* Tell connection to go into listening mode. */ netconn_listen(conn); while (1) { /* Grab new connection. */ accept_err = netconn_accept(conn, &newconn); /* Process the new connection. */ if (accept_err == ERR_OK) { HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_SET); while (netconn_recv(newconn, &buf) == ERR_OK) { do { netbuf_data(buf, &data, &len); netconn_write(newconn, data, len, NETCONN_COPY); } while (netbuf_next(buf) >= 0); netbuf_delete(buf); } /* Close connection and discard connection identifier. */ netconn_close(newconn); netconn_delete(newconn); HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_RESET); } } } else { netconn_delete(newconn); } } }
/** Tests the fragmented IP packets. * @bug Doesn't pass yet. */ char* fragmented_packet_test(void) { ip_addr_t destination, self_ip; printf("%s()\n", __FUNCTION__); struct netconn *conn; err_t err; struct netbuf *buf; void *data; u16_t len; /* Payload longer than MTU, should get split. */ char test_str[2001]; test_str[0] = 0; char data_str[2001]; data_str[0] = 0; /* Fills the data pattern. */ while (strlen(test_str) < 1900) strcat(test_str, "data"); /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); /* Sets the device we want to connect to. */ IP4_ADDR(&destination, 10, 0, 0, 2); IP4_ADDR(&self_ip, 10, 0, 0, 3); /* Bind connection to well known port number 7. */ netconn_bind(conn, &self_ip, 1235); printf("Connecting...\n"); err = netconn_connect(conn, &destination, 1235); TEST_ASSERT("TCP connection failed.", err == ERR_OK); /* Don't send final \0 */ err = netconn_write(conn, test_str, strlen(test_str), NETCONN_NOCOPY); TEST_ASSERT("Netconn write failed.\n", err == ERR_OK); /* Reads whole response. */ int sum=0; while(sum < strlen(test_str) && (err = netconn_recv(conn, &buf)) == ERR_OK) { do { netbuf_data(buf, &data, &len); strncat(data_str, data, len); sum += len; } while (netbuf_next(buf) >= 0); netbuf_delete(buf); } TEST_ASSERT("Data is not echoed correctly", !strcmp(data_str, test_str)); netconn_close(conn); return TEST_SUCCESS; }
size_t rpc_transmit(uint8_t *input_buffer, size_t input_buffer_size, uint8_t *output_buffer, size_t output_buffer_size, ip_addr_t *addr, uint16_t port) { struct netconn *conn; int err; cmp_ctx_t ctx; /* For cmp_mem_access. */ cmp_mem_access_t mem; struct netbuf *buf; u16_t len; char *data; conn = netconn_new(NETCONN_TCP); if (conn == NULL) { return -1; } err = netconn_connect(conn, addr, port); if (err != ERR_OK) { goto fail; } serial_datagram_send((void *)input_buffer, input_buffer_size, netconn_serial_datagram_tx_adapter, (void *)conn); cmp_mem_access_init(&ctx, &mem, output_buffer, output_buffer_size); while (1) { err = netconn_recv(conn, &buf); /* If connection was closed by server, abort */ if (err != ERR_OK) { break; } do { netbuf_data(buf, (void **)&data, &len); /* Append data to buffer. */ ctx.write(&ctx, data, len); } while (netbuf_next(buf) >= 0); netbuf_delete(buf); } netconn_delete(conn); return cmp_mem_access_get_pos(&mem); fail: netconn_delete(conn); return -1; }
static void http_server_serve(struct netconn *conn) { struct netbuf *inbuf; char *buf; u16_t buflen; err_t err; char strBuf[64]; int strLen; /* Read the data from the port, blocking if nothing yet there. We assume the request (the part we care about) is in one netbuf */ err = netconn_recv(conn, &inbuf); if (err == ERR_OK) { netbuf_data(inbuf, (void **)&buf, &buflen); /* Is this an HTTP GET command? (only check the first 5 chars, since there are other formats for GET, and we're keeping it very simple )*/ if (buflen>=5 && buf[0]=='G' && buf[1]=='E' && buf[2]=='T' && buf[3]==' ' && buf[4]=='/' ) { /* Send the HTML header * subtract 1 from the size, since we dont send the \0 in the string * NETCONN_NOCOPY: our data is const static, so no need to copy it */ chThdSleep(100); netconn_write(conn, http_html_hdr, sizeof(http_html_hdr)-1, NETCONN_NOCOPY); /* Send our HTML page */ netconn_write(conn, http_index_html_1, sizeof(http_index_html_1)-1, NETCONN_NOCOPY); strLen = sprintf(strBuf, "%c% 2.2f", currentTemp > 0 ? '+' : '-', currentTemp); netconn_write(conn, strBuf, strLen, NETCONN_COPY); netconn_write(conn, http_index_html_2, sizeof(http_index_html_2)-1, NETCONN_NOCOPY); for (int i = 0; i < 12; i++) { strLen = sprintf(strBuf, "<tr><td>%u hour</td><td>%c% 2.2f°</td></tr>", i + 1, lastReadings[i] > 0 ? '+' : '-', lastReadings[i]); netconn_write(conn, strBuf, strLen, NETCONN_COPY); } netconn_write(conn, http_index_html_3, sizeof(http_index_html_3)-1, NETCONN_NOCOPY); } } /* Close the connection (server closes in HTTP) */ netconn_close(conn); /* Delete the buffer (netconn_recv gives us ownership, so we have to make sure to deallocate the buffer) */ netbuf_delete(inbuf); }
static void tcptask(void* arg) { static uint16_t port = 30000; HTTPClient* self = (HTTPClient*) arg; TCPData tcpData; for (;;) { if (xQueueReceive(self->qHandle, &tcpData, 100)) { port++; struct netconn *conn = netconn_new(NETCONN_TCP); err_t err; if (conn != NULL) { // Bind connection to the specified number os_printf("Binding port %d\n", port); err = netconn_bind(conn, NULL, port); if (err == ERR_OK) { struct ip_addr ip; ip.addr = tcpData.serverIP; os_printf("Connecting port %d\n", tcpData.serverPort); err = netconn_connect (conn, &ip, tcpData.serverPort); if (err == ERR_OK) { os_printf("Writing data!\n"); netconn_write(conn, tcpData.data, TCP_DATA_SIZE, NETCONN_COPY); struct netbuf *buf; char *data; u16_t len; uint32_t offset = 0; if ((buf = netconn_recv(conn)) != NULL) { do { netbuf_data(buf, (void**)&data, &len); if (self->rxBuffer != NULL && data != NULL) memcpy(self->rxBuffer + offset, data, len); else os_printf("HTTPClient::tcpTask self->rxBuffer or data is NULL!\n"); offset += len; os_printf("Netconn received %d bytes\n", len); } while (netbuf_next(buf) >= 0); self->onReceive(tcpData.receiveCallback, tcpData.obj, self->rxBuffer); netbuf_delete(buf); } } } } netconn_close (conn ); netconn_delete (conn ); } } vTaskDelete(NULL); }
/* ********************处理具体链接的函数******************************* **********tcp_netconn_thread接收到一个连接后创建此任务*************** *************** 该任务可重入函数 ******************************* */ static void NetUserProcess_thread(void *arg) { NET_CLIENT_USER_INFO_STRUCT *this_info = (NET_CLIENT_USER_INFO_STRUCT *)arg; struct netconn *t_conn = this_info->conn; //int count = 0; u16_t buflen=15; struct netbuf *inbuf; char* buf; char rsv_buf[100] = "wlcom to conect me^_^\r\n"; // 长度为100的接收缓冲区,存放接收到的数据 t_conn->recv_timeout = 50; //this_info->period = 10; //设置超时时间,不阻塞接收 netconn_write(t_conn,rsv_buf,strlen(rsv_buf),NETCONN_NOCOPY); while(1) { if(t_conn->err == ERR_CLSD) //连接已被关闭,退出该线程 { printf("a connect clsoed \r\n"); goto exit; } inbuf = netconn_recv(t_conn); //不阻塞接收数据 //处理接收到的命令 if (inbuf != NULL) { //LCD_DisplayStringLine(Line5, (uint8_t*)"get data!\0"); netbuf_data(inbuf, (void**)&buf, &buflen); //获取数据及长度 if(buflen>100) { printf("receive data is too long \r\n"); netbuf_delete(inbuf); } strncpy(rsv_buf,buf,buflen); //复制数据至 接收缓冲数组 rsv_buf netbuf_delete(inbuf); //释放内部分配的存储空间 rsv_buf[buflen]='\0'; //设置接收到的字符串结尾 //该处调用命令处理函数对命令进行处理 NetDataDecode(t_conn,rsv_buf); //数据处理函数 //netconn_write(t_conn,rsv_buf,buflen,NETCONN_NOCOPY); } //处理需要发送的数据 if(this_info->send_flag!=0) //有数据要发送 { netconn_write(t_conn,gNetBuffer,gNetDataSize,NETCONN_NOCOPY); this_info->send_flag = 0; } else { //nodata } //vTaskDelay(100); } exit: delete_conn_u(t_conn); vTaskDelete( NULL ); //return 0; }
void tcp_task(void) { struct netconn *conn, *newconn; err_t err; conn = netconn_new(NETCONN_TCP); if(conn != NULL) { err = netconn_bind(conn, NULL, 7); if (err == ERR_OK) { /* Tell connection to go into listening mode. */ netconn_listen(conn); while (1) { /* Grab new connection. */ newconn = netconn_accept(conn); /* Process the new connection. */ if (newconn) { struct netbuf *buf; void *data; u16_t len; while ((buf = netconn_recv(newconn)) != NULL) { do { netbuf_data(buf, &data, &len); netconn_write(newconn, data, len, NETCONN_COPY); } while(netbuf_next(buf) >= 0); netbuf_delete(buf); } /* Close connection and discard connection identifier. */ netconn_close(newconn); netconn_delete(newconn); } } } else { printf(" can not bind TCP netconn"); } } else { printf("can not create TCP netconn"); } }
/* * Process an incoming connection on port 80. * * This simply checks to see if the incoming data contains a GET request, and * if so sends back a single dynamically created page. The connection is then * closed. A more complete implementation could create a task for each * connection. */ static void vProcessConnection( struct netconn *pxNetCon ) { static char cDynamicPage[webMAX_PAGE_SIZE], cPageHits[11]; struct netbuf *pxRxBuffer; char *pcRxString; unsigned short usLength; static unsigned long ulPageHits = 0; /* We expect to immediately get data. */ pxRxBuffer = netconn_recv( pxNetCon ); if( pxRxBuffer != NULL ) { /* Where is the data? */ netbuf_data( pxRxBuffer, ( void * )&pcRxString, &usLength ); /* Is this a GET? We don't handle anything else. */ if( !strncmp( pcRxString, "GET", 3 ) ) { pcRxString = cDynamicPage; /* Update the hit count. */ ulPageHits++; sprintf( cPageHits, "%lu", ulPageHits ); /* Write out the HTTP OK header. */ netconn_write( pxNetCon, webHTTP_OK, ( u16_t ) strlen( webHTTP_OK ), NETCONN_COPY ); /* Generate the dynamic page... ... First the page header. */ strcpy( cDynamicPage, webHTML_START ); /* ... Then the hit count... */ strcat( cDynamicPage, cPageHits ); strcat( cDynamicPage, "<p><pre>Task State Priority Stack #<br>************************************************<br>" ); /* ... Then the list of tasks and their status... */ vTaskList( ( signed char * )cDynamicPage + strlen( cDynamicPage ) ); /* ... Finally the page footer. */ strcat( cDynamicPage, webHTML_END ); /* Write out the dynamically generated page. */ netconn_write( pxNetCon, cDynamicPage, ( u16_t ) strlen( cDynamicPage ), NETCONN_COPY ); } netbuf_delete( pxRxBuffer ); } netconn_close( pxNetCon ); }
int main(void) { struct netconn *server; /* Hardware initialization */ init(); proc_new(monitor_process, NULL, KERN_MINSTACKSIZE * 2, NULL); dhcp_start(&netif); /* * Here we wait for an ip address, but it's not strictly * necessary. The address is obtained in background and * as long as we don't use network functions, we could go * on with initialization */ while (!netif.ip_addr.addr) timer_delay(200); kprintf(">>> dhcp ok: ip = ip = %s (kernel %s)\n", ip_ntoa(&netif.ip_addr.addr), CONFIG_KERN_PREEMPT ? "preempt" : "coop"); server = netconn_new(NETCONN_TCP); netconn_bind(server, IP_ADDR_ANY, 80); netconn_listen(server); while (1) { struct netconn *client; struct netbuf *rx_buf_conn; char *rx_buf; u16_t len; client = netconn_accept(server); if (!client) continue; tot_req++; rx_buf_conn = netconn_recv(client); if (rx_buf_conn) { netbuf_data(rx_buf_conn, (void **)&rx_buf, &len); if (rx_buf) netconn_write(client, rx_buf, len, NETCONN_COPY); netbuf_delete(rx_buf_conn); } while (netconn_delete(client) != ERR_OK) cpu_relax(); } }
/** * Receives tcp/udp information copying to a static * array or use network buffer directly depending on flag var * Info is received thru tcp/udp/raw connection descriptor * Features: reentrant * * @param connection descriptor * @param static array to be used to copy network buffers * @param selector from apps array or use directly from lwIP network buffers * @param network buffer pointer of pointer * @return length of buffer. Read conn->err for details: * OK, (ERR_OK) CLSD (ERR_CLSD), TIMEOUT (ERR_TIMEOUT), OUT OF MEM (ERR_MEM) */ uint16_t netconn_rcv_req(void *connec, uint8_t *alloc_rq, void **nbuffer, uint8_t flag) { /*joining temp pbuf*/ struct netbuf *inbuf; struct pbuf *q; struct netconn *conn = (struct netconn *)connec; /*temporal len*/ uint16_t len = 0; /*FSL: receive the packet*/ inbuf = netconn_recv(conn); /*receiving from the buffer*/ if( inbuf != NULL ) { /*if receiver is expecting a big rx packet, use it directly from the network buffers*/ if(flag) { /*use buffer directly from lwIP network buffers*/ len = inbuf->ptr->tot_len; *nbuffer = (void *)inbuf; return len; } /*if not you can copy it to a small buffer*/ /*start segment index*/ q = inbuf->ptr; do { memcpy( &alloc_rq[len], q->payload, q->len ); len += q->len; } while( ( q = q->next ) != NULL ); /*NULL char terminator. Useful for ASCII transfers*/ alloc_rq[len] = '\0'; /*free pbuf memory*/ netbuf_delete(inbuf); } return len;/*return value*/ }
int main(void) { ip6_addr_t addr; struct netconn *conn; struct netbuf *buf; xtimer_sleep(5U); /* wait 5 sec to bootstrap network */ ip6addr_aton(TCP_SERVER_ADDR, &addr); conn = netconn_new(NETCONN_TYPE_IPV6 | NETCONN_TCP); netconn_connect(conn, &addr, TCP_SERVER_PORT); puts("Kill TCP server now"); netconn_recv(conn, &buf); return 0; }
// telnet-like server, reads text commands from an open network connection static void canBridge_serve (struct netconn *conn) { struct netbuf *inbuf; currConn = conn; for (;;) { err_t err = netconn_recv(conn, &inbuf); if (err != ERR_OK) break; char *buf; u16_t buflen; netbuf_data(inbuf, (void **)&buf, &buflen); parseCanCmds(buf, buflen); netbuf_delete(inbuf); } currConn = 0; netconn_close(conn); }
/*-----------------------------------------------------------------------------------*/ static void tcpecho_thread(void *arg) { struct netconn *conn, *newconn; err_t err; LWIP_UNUSED_ARG(arg); /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); /* Bind connection to well known port number TCP_ECHO_PORT. */ netconn_bind(conn, IP_ADDR_ANY, TCP_ECHO_PORT); /* Tell connection to go into listening mode. */ netconn_listen(conn); while (1) { /* Grab new connection. */ newconn = netconn_accept(conn); printf("accepted new connection %p\n", newconn); /* Process the new connection. */ if (newconn != NULL) { struct netbuf *buf; void *data; u16_t len; while ((buf = netconn_recv(newconn)) != NULL) { printf("Recved\n"); do { netbuf_data(buf, &data, &len); err = netconn_write(newconn, data, len, NETCONN_COPY); #if 0 if (err != ERR_OK) { printf("tcpecho: netconn_write: error \"%s\"\n", lwip_strerr(err)); } #endif } while (netbuf_next(buf) >= 0); netbuf_delete(buf); } printf("Got EOF, looping\n"); /* Close connection and discard connection identifier. */ netconn_close(newconn); netconn_delete(newconn); } } }
void http_server_serve(struct netconn *conn) { struct netbuf *inbuf; char *buf, *ptr; u16_t buflen; /* Read the data from the port, blocking if nothing yet there. We assume the request (the part we care about) is in one netbuf */ if (netconn_recv(conn, &inbuf) == ERR_OK) { netbuf_data(inbuf, (void **) &buf, &buflen); /* Is this an HTTP GET command? (only check the first 5 chars, since there are other formats for GET, and we're keeping it very simple )*/ if (buflen >= 17 && (ptr = strstr(buf, "/?nazwa=OPT")) != NULL){ if(*(ptr+11) == '1') LED_On(4); else LED_Off(4); netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY); /* Send our HTML page */ netconn_write(conn, http_index_html, sizeof(http_index_html) - 1, NETCONN_NOCOPY); netconn_write(conn, Title, strlen(Title), NETCONN_COPY); netconn_write(conn, http_index_html_2, sizeof(http_index_html_2) - 1, NETCONN_NOCOPY); }else if (buflen >= 5 && buf[0] == 'G' && buf[1] == 'E' && buf[2] == 'T' && buf[3] == ' ' && buf[4] == '/') { /* Send the HTML header * subtract 1 from the size, since we dont send the \0 in the string * NETCONN_NOCOPY: our data is const static, so no need to copy it */ netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY); /* Send our HTML page */ netconn_write(conn, http_index_html, sizeof(http_index_html) - 1, NETCONN_NOCOPY); netconn_write(conn, Title, strlen(Title), NETCONN_COPY); netconn_write(conn, http_index_html_2, sizeof(http_index_html_2) - 1, NETCONN_NOCOPY); } } /* Close the connection (server closes in HTTP) */ netconn_close(conn); /* Delete the buffer (netconn_recv gives us ownership, so we have to make sure to deallocate the buffer) */ netbuf_delete(inbuf); }