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); }
err_t __netconn_recv(struct netconn *netconn, struct netbuf **nb, int bsize) { size_t numRead = 0; char *buf = malloc(bsize + 1); if(!buf) { error("malloc(NULL)"); return -1; } struct netbuf *b = netbuf_new(); if(!b) { error("netbuf_new()"); return -1; } numRead = sk_readline(netconn->sk, buf, bsize); if(numRead == -1) { error("read(%d)", netconn->sk); return -1; } dprintf("line [%d]:", numRead); mem_printf(buf, MIN(numRead, bsize-1)); numRead -= (buf[MIN(numRead, bsize) - 1] == '\0'); if(netbuf_ref(b, buf, numRead) != ERR_OK) { error("netbuf_ref()"); return -1; } *nb = b; return 0; }
void message_transmit(uint8_t *input_buffer, size_t input_buffer_size, ip_addr_t *addr, uint16_t port) { struct netconn *conn; struct netbuf *buf; conn = netconn_new(NETCONN_UDP); if (conn == NULL) { // TODO: Do something useful return; } buf = netbuf_new(); if (buf == NULL) { // TODO: Do something useful return; } netbuf_ref(buf, input_buffer, input_buffer_size); netconn_sendto(conn, buf, addr, port); netbuf_delete(buf); netconn_delete(conn); }
nsapi_size_or_error_t LWIP::socket_sendto(nsapi_socket_t handle, const SocketAddress &address, const void *data, nsapi_size_t size) { struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle; ip_addr_t ip_addr; nsapi_addr_t addr = address.get_addr(); if (!convert_mbed_addr_to_lwip(&ip_addr, &addr)) { return NSAPI_ERROR_PARAMETER; } struct netbuf *buf = netbuf_new(); err_t err = netbuf_ref(buf, data, (u16_t)size); if (err != ERR_OK) { netbuf_free(buf); return err_remap(err); } err = netconn_sendto(s->conn, buf, &ip_addr, address.get_port()); netbuf_delete(buf); if (err != ERR_OK) { return err_remap(err); } return size; }
static void low_send_putc(char *ptr, int l) { if(ser_conn && buf && netconn_connect(ser_conn, IP_ADDR_BROADCAST, 1445)==ERR_OK) { netbuf_ref(buf, ptr, l); netconn_send(ser_conn, buf); } }
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; }
void uart_wifi_debug_send(u8_t* buf, u32_t length, char kind) { int i=0; if(!ot_api_is_online())return; if(length>(WIFI_UDP_BUF_MAX-WIFI_UDP_BUF_HEAD)) { length=(WIFI_UDP_BUF_MAX-WIFI_UDP_BUF_HEAD); } wifi_debug_buf[0]=kind; for(i=1; i<WIFI_UDP_BUF_HEAD; i++) { wifi_debug_buf[i]=pkt_index/tens[WIFI_UDP_BUF_HEAD-i-1]%10+'0'; } pkt_index++; for(i=0; i<length; i++) { wifi_debug_buf[i+WIFI_UDP_BUF_HEAD]=buf[i]; } netbuf_ref(uart_wifi_debug_buf,wifi_debug_buf,length+WIFI_UDP_BUF_HEAD); struct wlan_ip_config addr; int ret; ret = wlan_get_address(&addr); ip_addr_t addr_ip; addr_ip.addr = addr.ipv4.address; addr_ip.addr &= 0x00ffffff; addr_ip.addr |= 0xff000000; if (ret != WM_SUCCESS) { wmprintf("get addr error!\r\n"); return; } netconn_connect(uart_wifi_debug_base_conn,&addr_ip, 38938); netconn_send (uart_wifi_debug_base_conn,uart_wifi_debug_buf); addr_ip.addr &= 0x00ffffff; addr_ip.addr |= 0x36000000; netconn_connect(uart_wifi_debug_base_conn,&addr_ip, 38938); netconn_send (uart_wifi_debug_base_conn,uart_wifi_debug_buf); }
static int lwip_socket_sendto(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port, const void *data, unsigned size) { struct lwip_socket *s = (struct lwip_socket *)handle; if (addr.version != NSAPI_IPv4) { return NSAPI_ERROR_PARAMETER; } struct netbuf *buf = netbuf_new(); err_t err = netbuf_ref(buf, data, (u16_t)size); if (err != ERR_OK) { netbuf_free(buf); return lwip_err_remap(err);; } err = netconn_sendto(s->conn, buf, (ip_addr_t *)addr.bytes, port); netbuf_delete(buf); if (err != ERR_OK) { return lwip_err_remap(err); } return size; }
static nsapi_size_or_error_t mbed_lwip_socket_sendto(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port, const void *data, nsapi_size_t size) { struct lwip_socket *s = (struct lwip_socket *)handle; ip_addr_t ip_addr; if (!convert_mbed_addr_to_lwip(&ip_addr, &addr)) { return NSAPI_ERROR_PARAMETER; } struct netbuf *buf = netbuf_new(); err_t err = netbuf_ref(buf, data, (u16_t)size); if (err != ERR_OK) { netbuf_free(buf); return mbed_lwip_err_remap(err); } err = netconn_sendto(s->conn, buf, &ip_addr, port); netbuf_delete(buf); if (err != ERR_OK) { return mbed_lwip_err_remap(err); } return size; }