static inline bool send_packet(const char *name, struct net_context *ctx, int ipsum_len, int pos) { struct net_buf *buf; bool fail = false; buf = ip_buf_get_tx(ctx); if (buf) { uint8_t *ptr; int sending_len = ipsum_len - pos; ptr = net_buf_add(buf, sending_len); memcpy(ptr, lorem_ipsum + pos, sending_len); sending_len = buf->len; if (net_send(buf) < 0) { PRINT("%s: sending %d bytes failed\n", __func__, sending_len); ip_buf_unref(buf); fail = true; goto out; } else { PRINT("%s: sent %d bytes\n", __func__, sending_len); } } out: return fail; }
static void send_data(const char *taskname, struct net_context *ctx) { int len = strlen(lorem_ipsum); struct net_buf *buf; buf = ip_buf_get_tx(ctx); if (buf) { uint8_t *ptr; uint16_t sent_len; ptr = net_buf_add(buf, 0); memcpy(ptr, lorem_ipsum, len); ptr = net_buf_add(buf, len); ptr = net_buf_add(buf, 1); /* add \0 */ *ptr = '\0'; sent_len = buf->len; if (net_send(buf) < 0) { PRINT("%s: %s(): sending %d bytes failed\n", taskname, __func__, len); ip_buf_unref(buf); } else { PRINT("%s: %s(): sent %d bytes\n", taskname, __func__, sent_len); } } }
/*---------------------------------------------------------------------------*/ static int prepare_and_send_buf(coap_context_t *ctx, session_t *session, uint8_t *data, size_t len) { struct net_buf *buf; int max_data_len; /* This net_buf gets sent to network, so it is not released * by this function unless there was an error and buf was * not actually sent. */ buf = ip_buf_get_tx(ctx->net_ctx); if (!buf) { len = -ENOBUFS; goto out; } max_data_len = IP_BUF_MAX_DATA - UIP_IPUDPH_LEN; PRINTF("%s: reply to peer data %p len %d\n", __FUNCTION__, data, len); if (len > max_data_len) { PRINTF("%s: too much (%d bytes) data to send (max %d bytes)\n", __FUNCTION__, len, max_data_len); ip_buf_unref(buf); len = -EINVAL; goto out; } /* Note that we have reversed the addresses here * because net_reply() will reverse them again. */ #ifdef CONFIG_NETWORKING_WITH_IPV6 uip_ip6addr_copy(&NET_BUF_IP(buf)->destipaddr, (uip_ip6addr_t *)&ctx->my_addr.in6_addr); uip_ip6addr_copy(&NET_BUF_IP(buf)->srcipaddr, (uip_ip6addr_t *)&session->addr.ipaddr); #else uip_ip4addr_copy(&NET_BUF_IP(buf)->destipaddr, (uip_ip4addr_t *)&ctx->my_addr.in_addr); uip_ip4addr_copy(&NET_BUF_IP(buf)->srcipaddr, (uip_ip4addr_t *)&session->addr.ipaddr); #endif NET_BUF_UDP(buf)->destport = uip_ntohs(ctx->my_port); NET_BUF_UDP(buf)->srcport = session->addr.port; uip_set_udp_conn(buf) = net_context_get_udp_connection(ctx->net_ctx); memcpy(net_buf_add(buf, len), data, len); ip_buf_appdatalen(buf) = len; ip_buf_appdata(buf) = buf->data + ip_buf_reserve(buf); if (net_reply(ctx->net_ctx, buf)) { ip_buf_unref(buf); } out: return len; }
int tcp_tx(struct net_context *ctx, uint8_t *buf, size_t size) { struct net_buf *nbuf = NULL; uint8_t *ptr; int rc = 0; nbuf = ip_buf_get_tx(ctx); if (nbuf == NULL) { printk("[%s:%d] Unable to get buffer\n", __func__, __LINE__); return -EINVAL; } ptr = net_buf_add(nbuf, size); memcpy(ptr, buf, size); ip_buf_appdatalen(nbuf) = size; do { rc = net_send(nbuf); if (rc >= 0) { ip_buf_unref(nbuf); return 0; } switch (rc) { case -EINPROGRESS: printk("%s: no connection yet, try again\n", __func__); fiber_sleep(TCP_RETRY_TIMEOUT); break; case -EAGAIN: case -ECONNRESET: printk("%s: no connection, try again later\n", __func__); fiber_sleep(TCP_RETRY_TIMEOUT); break; default: printk("%s: sending %d bytes failed\n", __func__, size); ip_buf_unref(nbuf); return -EIO; } } while (1); return 0; }