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; }
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; }
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); } } }
/* 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); } }
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); }
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 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; }
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 handle_someip_sd_packet(struct netbuf *buf) { unsigned char *recv_data, *data_ptr; ip_addr_t *addr; unsigned short port; unsigned long entries_len, len; someip_sd_header_t *sd_header; addr = netbuf_fromaddr(buf); port = netbuf_fromport(buf); netbuf_data(buf, &recv_data, &len); /* printf("\r\n(1) Received data %d\r\n", len); int i; for(i=0;i<len && i<90;++i){ if(i % 8 == 0) printf("\r\n"); printf("%02x ",recv_data[i]); } */ sd_header = (someip_sd_header_t *)recv_data; if(sd_header->proto_ver != 0x01 || sd_header->if_ver != 0x01 || sd_header->msg_type != 0x02 || sd_header->ret_code != 0x00) { printf("this packet may not be a someip-sd\r\n"); return; } data_ptr = recv_data + sizeof(someip_sd_header_t); entries_len = ntohl(*((unsigned long *)data_ptr)); data_ptr += sizeof(unsigned long); while(entries_len) { switch(*data_ptr ) { case 0x00: printf("Got findservice\r\n"); handle_findservice(addr, port, data_ptr, entries_len); break; case 0x01: printf("Got Offerservice\r\n"); handle_offerservice(addr, port, data_ptr, entries_len); break; default: printf("unsupported entry type\r\n"); } entries_len -= sizeof(someip_sd_entry_t); data_ptr += sizeof(someip_sd_entry_t); } }
/** 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; }
/* 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); } } }
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); }
/*! \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; }
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(); } }
// 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); }
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); } }
/******************************************************************************* * Function Name : vHandler_HTTP * Description : HTTP处理 * Input : - pstConn: 指向struct netconn结构的指针 * Output : None * Return : None * Attention : None *******************************************************************************/ static void vHandler_HTTP(struct netconn *pstConn) { struct netbuf *__pstNetbuf; INT8S *__pbData; u16_t __s32Len; __pstNetbuf = netconn_recv(pstConn); if(__pstNetbuf != NULL) { netbuf_data (__pstNetbuf, (void *)&__pbData, &__s32Len ); if( strstr( (void *)__pbData, "GET" ) != NULL ) { __Handler_HTTPGet(pstConn); } } netbuf_delete(__pstNetbuf); netconn_close(pstConn); }
static void data_udp_rx_serve(struct netconn *conn) { #if DEBUG_SENSOR_UDP static uint8_t count = 0; BaseSequentialStream *chp = getActiveUsbSerialStream(); #endif struct netbuf *inbuf; char *buf; uint16_t buflen = 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); //palClearPad(TIMEINPUT_PORT, TIMEINPUT_PIN); // negative pulse for input. #if DEBUG_SENSOR_UDP chprintf(chp, "\r\nsensor rx (from FC): %d ", count++); #endif //palSetPad(TIMEINPUT_PORT, TIMEINPUT_PIN); #if DEBUG_SENSOR_UDP uint16_t i = 0; for(i=0; i<buflen; ++i) { chprintf(chp, "%c", buf[i]); } chprintf(chp, "\r\n"); #endif data_udp_process_rx(buf, buflen); } netconn_close(conn); /* Delete the buffer (netconn_recv gives us ownership, * so we have to make sure to deallocate the buffer) */ netbuf_delete(inbuf); }
/** 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); }
void tcpecho_entry(void *parameter) { struct netconn *conn, *newconn; err_t err; /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); /* Bind connection to well known port number 7. */ netconn_bind(conn, NULL, TCP_ECHO_PORT); /* 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 != NULL) { struct netbuf *buf; void *data; u16_t len; while((buf = netconn_recv(newconn)) != NULL) { do { netbuf_data(buf, &data, &len); err = netconn_write(newconn, data, len, NETCONN_COPY); if(err != ERR_OK){} } while(netbuf_next(buf) >= 0); netbuf_delete(buf); } /* Close connection and discard connection identifier. */ netconn_delete(newconn); } } }
int netstack_socket_nextbuf(struct netstack_socket_buf *buf) { s8_t err; struct netbuf *nb; if (!buf || !buf->priv) { return VMM_EINVALID; } nb = buf->priv; err = netbuf_next(nb); if (err != 0 && err != 1) { return VMM_ENOENT; } netbuf_data(nb, &buf->data, &buf->len); buf->priv = nb; return VMM_OK; }
int lwip_sock_read_from(void *conn, unsigned char *buff, unsigned long len,struct sockaddr *sockaddr, int addr_len) { struct netbuf *new_buf=0; unsigned char *data; int data_len=0; int ret=0; SYSCALL_DEBUG(" SOCK recvfrom :%x len:%d \n",buff,len); mutexLock(g_netBH_lock); ret=netconn_recv(conn, &new_buf); mutexUnLock(g_netBH_lock); if (ret == ERR_TIMEOUT){ if (g_current_task->killed == 1){ return 0; } } if (ret!=ERR_OK){ SYSCALL_DEBUG(" Fail to recvfrom data: %x newret:%x(%d) \n",ret,-ret,-ret); return 0; } SYSCALL_DEBUG(" SUCESS to recv data:%d \n",ret); netbuf_data(new_buf,&data,&data_len); if (data_len > 0){ if (sockaddr != 0){ sockaddr->addr = new_buf->addr.addr; sockaddr->sin_port = new_buf->port; } 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; }
char* simple_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; const char *test_str = "data\n"; /* 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); err = netconn_recv(conn, &buf); TEST_ASSERT("Recv failed.", err == ERR_OK); netbuf_data(buf, &data, &len); TEST_ASSERT("Data is not echoed correctly", !strcmp(data, test_str)); netconn_close(conn); return TEST_SUCCESS; }