/* 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); } }
/** * @brief Create and send a dynamic Web Page. This page contains the list of * running tasks and the number of page hits. * @param conn pointer on connection structure * @retval None */ static void DynWebPage(struct netconn *conn) { portCHAR PAGE_BODY[800]; portCHAR pagehits[10] = {0}; memset(PAGE_BODY, 0,512); /* Update the hit count */ nPageHits++; sprintf(pagehits, "%d", (int)nPageHits); strcat(PAGE_BODY, pagehits); strcat((char *)PAGE_BODY, "<pre><br>Name State Priority Stack Num" ); strcat((char *)PAGE_BODY, "<br>---------------------------------------------<br>"); /* The list of tasks and their status */ vTaskList((signed char *)PAGE_BODY + strlen(PAGE_BODY)); strcat((char *)PAGE_BODY, "<br><br>---------------------------------------------"); strcat((char *)PAGE_BODY, "<br>B : Blocked, R : Ready, D : Deleted, S : Suspended<br>"); /* Send the dynamically generated page */ if((Global_Config.b.DistantControlEnabled != 0) && \ (Global_Config.b.BackgroundModeEnabled != 0) && \ (EthernetSettings.DistantControlEnabled == 1)) { netconn_write(conn, PAGE_START_DC, strlen((char*)PAGE_START_DC), NETCONN_COPY); } else { netconn_write(conn, PAGE_START, strlen((char*)PAGE_START), NETCONN_COPY); } netconn_write(conn, PAGE_BODY, strlen(PAGE_BODY), NETCONN_COPY); }
static void rawd_new(struct netconn *nc) { int i; int free = -1; chMtxLock(&rawd_mutex); /* check for existing connections */ for (i = 0; i < NC_COUNT; i++) { if (ncs[i] == NULL) { free = i; } else if (ncs[i]->pcb.tcp->state != ESTABLISHED) { /* kill stale connections */ netconn_close(ncs[i]); netconn_delete(ncs[i]); ncs[i] = NULL; free = i; } } if (free < 0) { netconn_write(nc, RAWD_FULL, sizeof(RAWD_FULL) - 1, NETCONN_COPY); netconn_close(nc); netconn_delete(nc); chMtxUnlock(); return; } ncs[free] = nc; chMtxUnlock(); netconn_write(nc, RAWD_READY, sizeof(RAWD_READY) - 1, NETCONN_COPY); }
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); } } }
static void http_serv_send_err_page(struct netconn *conn) { /* Send the HTML header */ netconn_write(conn, http_html_hdr_err, sizeof(http_html_hdr_err)-1, NETCONN_NOCOPY); /* Send our HTML page */ netconn_write(conn, http_index_html, sizeof(http_index_html)-1, NETCONN_NOCOPY); }
/*! \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 ); }
/* ********************处理具体链接的函数******************************* **********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; }
static void run_webserver(void *p) { struct ip_addr listenaddr = { 0 }; struct netconn *listener; struct netconn *session; struct timeval tv; err_t rc; DEBUG("Opening connection fir webserver \n"); listener = netconn_new(NETCONN_TCP); DEBUG("Connection at %x\n", &listener); rc = netconn_bind(listener, &listenaddr, 80); if (rc != ERR_OK) { DEBUG("Failed to bind connection: %i\n", rc); return; } rc = netconn_listen(listener); if (rc != ERR_OK) { DEBUG("Failed to listen on connection: %i\n", rc); return; } DEBUG("sucessfully listening the webserver \n"); while (1) { int i; session = netconn_accept(listener); if (session == NULL) continue; ut_sprintf(message, "<html><body><pre> Jiny Kernel Dmesg max_len:%d curr_len:%d \n",MAX_DMESG_LOG,g_dmesg_index); (void) netconn_write(session, message, ut_strlen(message), NETCONN_COPY); i=0; while (i<g_dmesg_index) { if (g_dmesg_index < MAX_DMESG_LOG) (void) netconn_write(session, &g_dmesg[i],100, NETCONN_COPY); i=i+100; } ut_sprintf(message, "</pre></body></html>"); (void) netconn_write(session, message, ut_strlen(message), NETCONN_COPY); (void) netconn_disconnect(session); (void) netconn_delete(session); } }
/* * 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 ); }
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 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 7. */ netconn_bind(conn, NULL, 7); /* Tell connection to go into listening mode. */ netconn_listen(conn); memset(data, 'F', DATA_SIZE); while (1) { /* Grab new connection. */ err = netconn_accept(conn, &newconn); /*printf("accepted new connection %p\n", newconn);*/ /* Process the new connection. */ if (err == ERR_OK) { err = netconn_write(newconn, data, DATA_SIZE, NETCONN_COPY); if (err != ERR_OK) { printf("tcpecho: netconn_write: error \"%s\"\n", lwip_strerr(err)); } netconn_close(newconn); netconn_delete(newconn); } } }
void DynWebPage(struct netconn *conn) { portCHAR PAGE_BODY[512]; uint8_t data1[4] = {0}; uint8_t data2[4] = {0}; uint8_t data3[4] = {0}; portCHAR pagehits[10] = {0}; int free_mem; memset(PAGE_BODY, 0,512); //sprintf(data2, "%d", nPageHits); // sprintf(data3, "%d", nPageHits); // // sprintf(pagehits, "%d", (int)nPageHits); // memcpy(PAGE_START + 1004, data1, 4); //memcpy(PAGE_START + 1004 + 76 , data2, 4); // //memcpy(PAGE_START + 1004 + 66 + 76, data3, 4); // //strcat(PAGE_START+1005, pagehits); // strcat((char *)PAGE_BODY, "<pre><br>Name State Priority Stack Num" ); // strcat((char *)PAGE_BODY, "<br>---------------------------------------------<br>"); // // /* The list of tasks and their status */ // //osThreadList((unsigned char *)(PAGE_BODY + strlen(PAGE_BODY))); // strcat((char *)PAGE_BODY, "<br><br>---------------------------------------------"); // strcat((char *)PAGE_BODY, "<br>B : Blocked, R : Ready, D : Deleted, S : Suspended<br>"); /* Send the dynamically generated page */ netconn_write(conn, PAGE_START, strlen((char*)PAGE_START), NETCONN_COPY); //netconn_write(conn, PAGE_BODY, strlen(PAGE_BODY), NETCONN_COPY); }
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); } } }
static void netconn_serial_datagram_tx_adapter(void *arg, const void *buffer, size_t buffer_len) { struct netconn *conn = (struct netconn *)arg; /* We don't know the lifetime of buffer so we must use NETCONN_COPY. */ netconn_write(conn, buffer, buffer_len, NETCONN_COPY); }
void FtpServer::sendWrite() { if( strlen( buf ) + 2 < FTP_BUF_SIZE ) strcat( buf, "\r\n" ); netconn_write( ctrlconn, buf, strlen( buf ), NETCONN_COPY ); COMMAND_PRINT( ">%u> %s", num, buf ); }
/** * \brief Send the chip ID information. * * \param name Not used. * \param recv_buf Receive buffer. * \param recv_len Receive buffer length. * * \return 0. */ static int cgi_chipInfo(struct netconn *client, const char *name, char *recv_buf, size_t recv_len) { (void)recv_buf; (void)recv_len; (void)name; /* Protect tx_buf buffer from concurrent access. */ sys_arch_sem_wait(&cgi_sem, 0); sprintf((char *)tx_buf, "{\"core_name\":\"%s\",\"arch_name\":\"%s\",\"sram_size\":\"%s\",\"flash_size\":\"%s\"}", chipid_eproc_name(CHIPID_EPRCOC), chipid_archnames(CHIPID_ARCH), chipid_sramsize(CHIPID_SRAMSIZ), chipid_nvpsize(CHIPID_NVPSIZ)); /* Send answer. */ http_sendOk(client, HTTP_CONTENT_JSON); /* Use NETCONN_COPY to avoid corrupting the buffer after releasing the semaphore. */ netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY); /* Release semaphore to allow further use of tx_buf. */ sys_sem_signal(&cgi_sem); return 0; }
/* 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 SCPI_Write(scpi_t * context, const char * data, size_t len) { if (context->user_context != NULL) { user_data_t * u = (user_data_t *)(context->user_context); if (u->io) { return (netconn_write(u->io, data, len, NETCONN_NOCOPY) == ERR_OK) ? len : 0; } } return 0; }
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); }
int lwip_send(int s, void *data, int size, unsigned int flags) { struct lwip_socket *sock; struct netbuf *buf; err_t err; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d, data=%p, size=%d, flags=0x%x)\n", s, data, size, flags)); sock = get_socket(s); if (!sock) { set_errno(EBADF); return -1; } switch (netconn_type(sock->conn)) { case NETCONN_RAW: case NETCONN_UDP: case NETCONN_UDPLITE: case NETCONN_UDPNOCHKSUM: /* create a buffer */ buf = netbuf_new(); if (!buf) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) ENOBUFS\n", s)); sock_set_errno(sock, ENOBUFS); return -1; } /* make the buffer point to the data that should be sent */ netbuf_ref(buf, data, size); /* send the data */ err = netconn_send(sock->conn, buf); /* deallocated the buffer */ netbuf_delete(buf); break; case NETCONN_TCP: err = netconn_write(sock->conn, data, size, NETCONN_COPY); break; default: err = ERR_ARG; break; } if (err != ERR_OK) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) err=%d\n", s, err)); sock_set_errno(sock, err_to_errno(err)); return -1; } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) ok size=%d\n", s, size)); sock_set_errno(sock, 0); return size; }
static void http_server_serve(struct netconn *conn) { struct netbuf *inbuf; char *buf; u16_t buflen; 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); ip_addr_t ip; uint16_t port; netconn_getaddr(conn, &ip, &port, 0); log_info( "Got request from %u.%u.%u.%u:%u", ip.addr & 0xFF, (ip.addr >> 8) & 0xFF, (ip.addr >> 16) & 0xFF, (ip.addr >> 24) & 0xFF, port); /* 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 */ 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); } }
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"); } }
/** Serve one HTTP connection accepted in the http thread */ static void http_server_netconn_serve(struct netconn *conn) { struct netbuf *inbuf; char *buf; u16_t buflen; 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); /* 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 */ printf(" Recieved a proper HTTP request sending out the http headers\n"); int ret = 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); } } /* 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 int lwip_socket_send(nsapi_stack_t *stack, nsapi_socket_t handle, const void *data, unsigned size) { struct lwip_socket *s = (struct lwip_socket *)handle; err_t err = netconn_write(s->conn, data, size, NETCONN_COPY); if (err != ERR_OK) { return lwip_err_remap(err); } return size; }
bool Web::parseResource(cu32 startOfURI) const { string uri; if('/' == _httpRequest[startOfURI] && ' ' == _httpRequest[startOfURI + 1]) { uri = defaultPage; } else { u32 endOfURI = _httpRequest.find(" ", startOfURI); uri = _httpRequest.substr(startOfURI, endOfURI - startOfURI); } u32 length = 0; u8* resource = NULL; if(FR_OK != readResource(uri.c_str(), resource, length)) {UARTprintf("Failed to get resource\n");} string header; makeHttpHeader(header, length); if (ERR_OK != netconn_write(_connectionFromClient, header.c_str(), header.size(), NETCONN_COPY)) {UARTprintf("Failed to send default page\n");} if (ERR_OK != netconn_write(_connectionFromClient, resource, length, NETCONN_NOCOPY)) {UARTprintf("Failed to send default page\n");} delete resource; return true; }
static msg_t net_put(void *instance, uint8_t c) { struct net_seq_stream *net = instance; net->buf[net->count++] = c; if (net->count == sizeof(net->buf)) { netconn_write(net->nc, net->buf, net->count, NETCONN_COPY); net->count = 0; } return c; }
T_uezError Network_lwIP_SocketWrite( void *aWorkspace, T_uezNetworkSocket aSocket, void *aData, TUInt32 aNumBytes, TBool aFlush, TUInt32 aTimeout) { T_Network_lwIP_Workspace *p = (T_Network_lwIP_Workspace *)aWorkspace; T_uezError error = UEZ_ERROR_UNKNOWN; T_lwIPSocket *p_socket = p->iSockets + aSocket; TUInt16 numWrite; PARAM_NOT_USED(aTimeout); if ((aSocket == 0) || (aSocket > NETWORK_LWIP_NUM_SOCKETS)) return UEZ_ERROR_HANDLE_INVALID; UEZSemaphoreGrab(p->iSem, UEZ_TIMEOUT_INFINITE); if (p_socket->iState == SOCKET_STATE_FREE) { error = UEZ_ERROR_HANDLE_INVALID; } else if ((p_socket->iFlags & SOCKET_FLAG_CONNECTED) == 0) { error = UEZ_ERROR_NOT_OPEN; } else { while (aNumBytes) { // Send data up to a 16-bit value if (aNumBytes > 0xFFFF) numWrite = 0xFFFF; else numWrite = (TUInt16)aNumBytes; aNumBytes -= numWrite; // Clean up any previous timeout errors if (p_socket->iNetconn->err == ERR_TIMEOUT) p_socket->iNetconn->err = ERR_OK; // Write out this segment (noting if there is data past this one) error = IConvertErrorCode(netconn_write(p_socket->iNetconn, aData, numWrite, NETCONN_COPY | ((aFlush) ? 0 : NETCONN_MORE))); // Stop on any errors if (error != UEZ_ERROR_NONE) break; aData = (void *)(((char *)aData) + numWrite); } } UEZSemaphoreRelease(p->iSem); return error; }
/** * @brief Create and send a dynamic Web Page. This page contains the list of * running tasks and the number of page hits. * @param conn pointer on connection structure * @retval None */ void DynWebPage(struct netconn *conn) { portCHAR PAGE_BODY[512]; portCHAR pagehits[10] = {0}; memset(PAGE_BODY, 0,512); /* Update the hit count */ nPageHits++; sprintf(pagehits, "%d", (int)nPageHits); strcat(PAGE_BODY, pagehits); strcat((char *)PAGE_BODY, "<pre><br>Name State Priority Stack Num" ); strcat((char *)PAGE_BODY, "<br>---------------------------------------------<br>"); /* The list of tasks and their status */ osThreadList((signed char *)(PAGE_BODY + strlen(PAGE_BODY))); strcat((char *)PAGE_BODY, "<br><br>---------------------------------------------"); strcat((char *)PAGE_BODY, "<br>B : Blocked, R : Ready, D : Deleted, S : Suspended<br>"); /* Send the dynamically generated page */ netconn_write(conn, PAGE_START, strlen((char*)PAGE_START), NETCONN_COPY); netconn_write(conn, PAGE_BODY, strlen(PAGE_BODY), NETCONN_COPY); }
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(); } }