Example #1
0
/*-----------------------------------------------------------------------------------*/
err_t
netconn_close(struct netconn *conn)
{
  struct api_msg *msg;

  if (conn == NULL) {
    return ERR_VAL;
  }
  if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
    return (conn->err = ERR_MEM);
  }

  conn->state = NETCONN_CLOSE;
 again:
  msg->type = API_MSG_CLOSE;
  msg->msg.conn = conn;
  api_msg_post(msg);
  sys_mbox_fetch(conn->mbox, NULL);
  if (conn->err == ERR_MEM &&
     conn->sem != SYS_SEM_NULL) {
    sys_sem_wait(conn->sem);
    goto again;
  }
  conn->state = NETCONN_NONE;
  memp_freep(MEMP_API_MSG, msg);
  return conn->err;
}
Example #2
0
/*-----------------------------------------------------------------------------------*/
err_t
netconn_send(struct netconn *conn, struct netbuf *buf)
{
  struct api_msg *msg;

  if (conn == NULL) {
    return ERR_VAL;
  }

  if (conn->err != ERR_OK) {
    return conn->err;
  }

  if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
    return (conn->err = ERR_MEM);
  }

  DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %ld bytes\n", buf->p->tot_len));
  msg->type = API_MSG_SEND;
  msg->msg.conn = conn;
  msg->msg.msg.p = buf->p;
  api_msg_post(msg);

  sys_mbox_fetch(conn->mbox, NULL);
  memp_freep(MEMP_API_MSG, msg);
  return conn->err;
}
Example #3
0
/*-----------------------------------------------------------------------------------*/
struct
netconn *netconn_new(enum netconn_type t)
{
  struct netconn *conn;

  conn = memp_mallocp(MEMP_NETCONN);
  if (conn == NULL) {
    return NULL;
  }
  conn->type = t;
  conn->pcb.tcp = NULL;

  if ((conn->mbox = sys_mbox_new()) == SYS_MBOX_NULL) {
    memp_freep(MEMP_NETCONN, conn);
    return NULL;
  }
  conn->recvmbox = SYS_MBOX_NULL;
  conn->acceptmbox = SYS_MBOX_NULL;
  conn->sem = SYS_SEM_NULL;
  conn->state = NETCONN_NONE;
  conn->socket = 0;
  conn->callback = 0;
  conn->recv_avail = 0;
  return conn;
}
Example #4
0
/*-----------------------------------------------------------------------------------*/
err_t
netconn_listen(struct netconn *conn)
{
  struct api_msg *msg;

  if (conn == NULL) {
    return ERR_VAL;
  }

  if (conn->acceptmbox == SYS_MBOX_NULL) {
    conn->acceptmbox = sys_mbox_new();
    if (conn->acceptmbox == SYS_MBOX_NULL) {
      return ERR_MEM;
    }
  }
  
  if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
    return (conn->err = ERR_MEM);
  }
  msg->type = API_MSG_LISTEN;
  msg->msg.conn = conn;
  api_msg_post(msg);
  sys_mbox_fetch(conn->mbox, NULL);
  memp_freep(MEMP_API_MSG, msg);
  return conn->err;
}
Example #5
0
/*-----------------------------------------------------------------------------------*/
err_t
netconn_connect(struct netconn *conn, struct ip_addr *addr,
		   u16_t port)
{
  struct api_msg *msg;
  
  if (conn == NULL) {
    return ERR_VAL;
  }


  if (conn->recvmbox == SYS_MBOX_NULL) {
    if ((conn->recvmbox = sys_mbox_new()) == SYS_MBOX_NULL) {
      return ERR_MEM;
    }
  }
  
  if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
    return ERR_MEM;
  }
  msg->type = API_MSG_CONNECT;
  msg->msg.conn = conn;  
  msg->msg.msg.bc.ipaddr = addr;
  msg->msg.msg.bc.port = port;
  api_msg_post(msg);
  sys_mbox_fetch(conn->mbox, NULL);
  memp_freep(MEMP_API_MSG, msg);
  return conn->err;
}
Example #6
0
/*-----------------------------------------------------------------------------------*/
void
netbuf_chain(struct netbuf *head, struct netbuf *tail)
{
  pbuf_chain(head->p, tail->p);
  head->ptr = head->p;
  memp_freep(MEMP_NETBUF, tail);
}
Example #7
0
/*-----------------------------------------------------------------------------------*/
void
netbuf_delete(struct netbuf *buf)
{
  if (buf != NULL) {
    if (buf->p != NULL) {
      pbuf_free(buf->p);
      buf->p = buf->ptr = NULL;
    }
    memp_freep(MEMP_NETBUF, buf);
  }
}
Example #8
0
/*-----------------------------------------------------------------------------------*/
err_t
netconn_delete(struct netconn *conn)
{
  struct api_msg *msg;
  void *mem;
  
  if (conn == NULL) {
    return ERR_OK;
  }
  
  if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
    return ERR_MEM;
  }
  
  msg->type = API_MSG_DELCONN;
  msg->msg.conn = conn;
  api_msg_post(msg);  
  sys_mbox_fetch(conn->mbox, NULL);
  memp_freep(MEMP_API_MSG, msg);

  /* Drain the recvmbox. */
  if (conn->recvmbox != SYS_MBOX_NULL) {
    while (sys_arch_mbox_fetch(conn->recvmbox, &mem, 1) != SYS_ARCH_TIMEOUT) {
      if (conn->type == NETCONN_TCP) {
	pbuf_free((struct pbuf *)mem);
      } else {
	netbuf_delete((struct netbuf *)mem);
      }
    }
    sys_mbox_free(conn->recvmbox);
    conn->recvmbox = SYS_MBOX_NULL;
  }
 

  /* Drain the acceptmbox. */
  if (conn->acceptmbox != SYS_MBOX_NULL) {
    while (sys_arch_mbox_fetch(conn->acceptmbox, &mem, 1) != SYS_ARCH_TIMEOUT) {
      netconn_delete((struct netconn *)mem);
    }
    
    sys_mbox_free(conn->acceptmbox);
    conn->acceptmbox = SYS_MBOX_NULL;
  }

  sys_mbox_free(conn->mbox);
  conn->mbox = SYS_MBOX_NULL;
  if (conn->sem != SYS_SEM_NULL) {
    sys_sem_free(conn->sem);
  }
  /*  conn->sem = SYS_SEM_NULL;*/
  memp_free(MEMP_NETCONN, conn);
  return ERR_OK;
}
Example #9
0
/*-----------------------------------------------------------------------------------*/
uint8_t
pbuf_free(struct pbuf *p)
{
  struct pbuf *q;
  uint8_t count = 0;

  if(p == NULL) {
    return 0;
  }

  ASSERT("pbuf_free: sane flags", p->flags == PBUF_FLAG_POOL ||
         p->flags == PBUF_FLAG_ROM ||
         p->flags == PBUF_FLAG_RAM);

  ASSERT("pbuf_free: p->ref > 0", p->ref > 0);

  /* Decrement reference count. */
  p->ref--;

  q = NULL;
  /* If reference count == 0, actually deallocate pbuf. */
  if(p->ref == 0) {

    while(p != NULL) {
      /* Check if this is a pbuf from the pool. */
      if(p->flags == PBUF_FLAG_POOL) {
	p->len = p->tot_len = PBUF_POOL_BUFSIZE;
	p->payload = (void *)((uint8_t *)p + sizeof(struct pbuf));
	q = p->next;
	PBUF_POOL_FREE(p);
#ifdef PBUF_STATS
	--stats.pbuf.used;
#endif /* PBUF_STATS */
      } else if(p->flags == PBUF_FLAG_ROM) {
	q = p->next;
	memp_freep(MEMP_PBUF, p);
      } else {
	q = p->next;
	mem_free(p);
      }
      p = q;
      count++;
    }
    pbuf_refresh();
  }

  return count;
}
Example #10
0
err_t
netconn_disconnect(struct netconn *conn)
{
  struct api_msg *msg;
  
  if (conn == NULL) {
    return ERR_VAL;
  }

  if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
    return ERR_MEM;
  }
  msg->type = API_MSG_DISCONNECT;
  msg->msg.conn = conn;  
  api_msg_post(msg);
  sys_mbox_fetch(conn->mbox, NULL);
  memp_freep(MEMP_API_MSG, msg);
  return conn->err;

}
Example #11
0
/*-----------------------------------------------------------------------------------*/
static void
tcpip_thread(void *arg)
{
  struct tcpip_msg *msg;

  (void)arg;

  ip_init();
#if LWIP_UDP  
  udp_init();
#endif
#if LWIP_TCP
  tcp_init();
#endif
  if (tcpip_init_done != NULL) {
    tcpip_init_done(tcpip_init_done_arg);
  }

  while (1) {                          /* MAIN Loop */
    sys_mbox_fetch(mbox, (void *)&msg);
    switch (msg->type) {
    case TCPIP_MSG_API:
      DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
      api_msg_input(msg->msg.apimsg);
      break;
    case TCPIP_MSG_INPUT:
      DEBUGF(TCPIP_DEBUG, ("tcpip_thread: IP packet %p\n", (void *)msg));
      ip_input(msg->msg.inp.p, msg->msg.inp.netif);
      break;
    case TCPIP_MSG_CALLBACK:
      DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
      msg->msg.cb.f(msg->msg.cb.ctx);
      break;
    default:
      break;
    }
    memp_freep(MEMP_TCPIP_MSG, msg);
  }
}
Example #12
0
static void
tcpip_thread(void *arg)
{
  struct tcpip_msg *msg;

  ip_init();
  udp_init();
  tcp_init();

  sys_timeout(TCP_TMR_INTERVAL, (sys_timeout_handler)tcpip_tcp_timer, NULL);

  if(tcpip_init_done != NULL) {
    tcpip_init_done(tcpip_init_done_arg);
  }

  while(1) {                          /* MAIN Loop */
    sys_mbox_fetch(mbox, (void *)&msg);
    switch(msg->type) {
    case TCPIP_MSG_API:
      DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", msg));
      api_msg_input(msg->msg.apimsg);
      break;
    case TCPIP_MSG_INPUT:
      DEBUGF(TCPIP_DEBUG, ("tcpip_thread: IP packet %p\n", msg));
      ip_input(msg->msg.inp.p, msg->msg.inp.netif);
      break;
    case TCP_SHIM_MSG_INPUT:
      DEBUGF(TCPIP_DEBUG, ("tcpip_thread: TCP shim packet %p\n", msg));
      tcp_input(msg->msg.inp.p, msg->msg.inp.netif);
      break;
    default:
      break;
    }
    memp_freep(MEMP_TCPIP_MSG, msg);
  }
}
Example #13
0
/*-----------------------------------------------------------------------------------*/
err_t
netconn_write(struct netconn *conn, void *dataptr, u32_t size, u8_t copy)
{
  struct api_msg *msg;
  u32_t len;
  
  if (conn == NULL) {
    return ERR_VAL;
  }

  if (conn->err != ERR_OK) {
    return conn->err;
  }
  
  if (conn->sem == SYS_SEM_NULL) {
    conn->sem = sys_sem_new(0);
    if (conn->sem == SYS_SEM_NULL) {
      return ERR_MEM;
    }
  }

  if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
    return (conn->err = ERR_MEM);
  }
  msg->type = API_MSG_WRITE;
  msg->msg.conn = conn;
        

  conn->state = NETCONN_WRITE;
  while (conn->err == ERR_OK && size > 0) {
    msg->msg.msg.w.dataptr = dataptr;
    msg->msg.msg.w.copy = copy;
    
    if (conn->type == NETCONN_TCP) {
      if (tcp_sndbuf(conn->pcb.tcp) == 0) {
	sys_sem_wait(conn->sem);
	if (conn->err != ERR_OK) {
	  goto ret;
	}
      }
      if (size > tcp_sndbuf(conn->pcb.tcp)) {
	/* We cannot send more than one send buffer's worth of data at a
	   time. */
	len = tcp_sndbuf(conn->pcb.tcp);
      } else {
	len = size;
      }
    } else {
      len = size;
    }
    
    DEBUGF(API_LIB_DEBUG, ("netconn_write: writing %ld bytes (%d)\n", len, copy));
    msg->msg.msg.w.len = len;
    api_msg_post(msg);
    sys_mbox_fetch(conn->mbox, NULL);    
    if (conn->err == ERR_OK) {
      dataptr = (void *)((char *)dataptr + len);
      size -= len;
    } else if (conn->err == ERR_MEM) {
      conn->err = ERR_OK;
      sys_sem_wait(conn->sem);
    } else {
      goto ret;
    }
  }
 ret:
  memp_freep(MEMP_API_MSG, msg);
  conn->state = NETCONN_NONE;
  if (conn->sem != SYS_SEM_NULL) {
    sys_sem_free(conn->sem);
    conn->sem = SYS_SEM_NULL;
  }
  
  return conn->err;
}
Example #14
0
/*-----------------------------------------------------------------------------------*/
struct netbuf *
netconn_recv(struct netconn *conn)
{
  struct api_msg *msg;
  struct netbuf *buf;
  struct pbuf *p;
  u32_t len;
    
  if (conn == NULL) {
    return NULL;
  }
  
  if (conn->recvmbox == SYS_MBOX_NULL) {
    conn->err = ERR_CONN;
    return NULL;
  }

  if (conn->err != ERR_OK) {
    return NULL;
  }

  if (conn->type == NETCONN_TCP) {
    if (conn->pcb.tcp->state == LISTEN) {
      conn->err = ERR_CONN;
      return NULL;
    }


    buf = memp_mallocp(MEMP_NETBUF);

    if (buf == NULL) {
      conn->err = ERR_MEM;
      return NULL;
    }
    
    sys_mbox_fetch(conn->recvmbox, (void **)&p);

    if (p != NULL)
    {
        len = p->tot_len;
        conn->recv_avail -= len;
    }
    else
        len = 0;
    
    /* Register event with callback */
      if (conn->callback)
        (*conn->callback)(conn, NETCONN_EVT_RCVMINUS, len);

    /* If we are closed, we indicate that we no longer wish to receive
       data by setting conn->recvmbox to SYS_MBOX_NULL. */
    if (p == NULL) {
      memp_freep(MEMP_NETBUF, buf);
      sys_mbox_free(conn->recvmbox);
      conn->recvmbox = SYS_MBOX_NULL;
      return NULL;
    }

    buf->p = p;
    buf->ptr = p;
    buf->fromport = 0;
    buf->fromaddr = NULL;

    /* Let the stack know that we have taken the data. */
    if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
      conn->err = ERR_MEM;
      return buf;
    }
    msg->type = API_MSG_RECV;
    msg->msg.conn = conn;
    if (buf != NULL) {
      msg->msg.msg.len = buf->p->tot_len;
    } else {
      msg->msg.msg.len = 1;
    }
    api_msg_post(msg);

    sys_mbox_fetch(conn->mbox, NULL);
    memp_freep(MEMP_API_MSG, msg);
  } else {
    sys_mbox_fetch(conn->recvmbox, (void **)&buf);
	conn->recv_avail -= buf->p->tot_len;
    /* Register event with callback */
    if (conn->callback)
        (*conn->callback)(conn, NETCONN_EVT_RCVMINUS, buf->p->tot_len);
  }

  

    
  DEBUGF(API_LIB_DEBUG, ("netconn_recv: received %p (err %d)\n", (void *)buf, conn->err));


  return buf;
}
Example #15
0
/**
 * Free a pbuf (chain) from usage, de-allocate non-used head of chain.
 *
 * Decrements the pbuf reference count. If it reaches
 * zero, the pbuf is deallocated.
 *
 * For a pbuf chain, this is repeated for each pbuf in the chain, until
 * a non-zero reference count is encountered, or the end of the chain is
 * reached.
 *
 * @param pbuf pbuf (chain) to be freed from one user.
 *
 * @return the number of unreferenced pbufs that were de-allocated
 * from the head of the chain.
 *
 * @note May not be called on a packet queue.
 * @note the reference counter of a pbuf equals the number of pointers
 * that refer to the pbuf (or into the pbuf).
 *
 * @internal examples:
 *
 * 1->2->3 becomes ...1->3
 * 3->3->3 becomes 2->3->3
 * 1->1->2 becomes ....->1
 * 2->1->1 becomes 1->1->1
 * 1->1->1 becomes .......
 *
 */
u8_t
pbuf_free(struct pbuf *p)
{
#ifdef PBUF_DEBUG
	  char text[200];
#endif

	  struct pbuf *q;
	  u8_t count;
	  SYS_ARCH_DECL_PROTECT(old_level);
	
	  if (p == NULL) {
	    LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_free(p == NULL) was called.\n"));
	    return 0;
	  }
	  LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_free(%p)\n", (void *)p));
	
	  PERF_START;
	
	  LWIP_ASSERT("pbuf_free: sane flags",
	    p->flags == PBUF_FLAG_RAM || p->flags == PBUF_FLAG_ROM ||
	    p->flags == PBUF_FLAG_REF || p->flags == PBUF_FLAG_POOL);
	
	  count = 0;

#ifdef PBUF_DEBUG
	  if(p == NULL) {
		pbufstats_print_pbuf_counter("pbuf_free: p is NULL!");
		pbufstats_pbuf_counter--;
	  }
#endif

	  /* Since decrementing ref cannot be guaranteed to be a single machine operation
	   * we must protect it. Also, the later test of ref must be protected.
	   */
	  SYS_ARCH_PROTECT(old_level);
	  /* de-allocate all consecutive pbufs from the head of the chain that
	   * obtain a zero reference count after decrementing*/
	  while (p != NULL) {
		    /* all pbufs in a chain are referenced at least once */
		    LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0);

#ifdef PBUF_DEBUG
			sprintf(text, "pbuf_free: p=%p, p->flags=%s", (void *)p, pbufstats_text_pbuf_flags[p->flags]);
			pbufstats_print_pbuf_counter(text);
			pbufstats_pbuf_counter--;

			pbufstats_delptr(p);
#endif

		    /* decrease reference count (number of pointers to pbuf) */
		    p->ref--;
		    /* this pbuf is no longer referenced to? */
		    if (p->ref == 0) 
		    {
			      /* remember next pbuf in chain for next iteration */
			      q = p->next;
			      LWIP_DEBUGF( PBUF_DEBUG | 2, ("pbuf_free: deallocating %p\n", (void *)p));
			      /* is this a pbuf from the pool? */
			      if (p->flags == PBUF_FLAG_POOL) 
			      {
				        p->len = p->tot_len = PBUF_POOL_BUFSIZE;
				        p->payload = (void *)((u8_t *)p + sizeof(struct pbuf));
				        PBUF_POOL_FREE(p);
				      /* a ROM or RAM referencing pbuf */
			      } 
			      else if (p->flags == PBUF_FLAG_ROM || p->flags == PBUF_FLAG_REF) 
			      {
				        memp_freep(MEMP_PBUF, p);
				      /* p->flags == PBUF_FLAG_RAM */
			      } 
			      else {
					mem_free(p);
			      }
			      count++;
			      /* proceed to next pbuf */
			      p = q;
		    /* p->ref > 0, this pbuf is still referenced to */
		    /* (and so the remaining pbufs in chain as well) */
		    } 
		    else {
			      LWIP_DEBUGF( PBUF_DEBUG | 2, ("pbuf_free: %p has ref %u, ending here.\n", (void *)p, (unsigned int)p->ref));
			      /* stop walking through chain */
			      p = NULL;
		    }
	  }
	  SYS_ARCH_UNPROTECT(old_level);
	  PERF_STOP("pbuf_free");
	  /* return number of de-allocated pbufs */
	  return count;
}