Esempio n. 1
0
/**
 * Much like tcpip_apimsg, but calls the lower part of a netifapi_*
 * function.
 *
 * @param netifapimsg a struct containing the function to call and its parameters
 * @return error code given back by the function that was called
 */
static err_t
tcpip_netifapi(tcpip_callback_fn fn, struct netifapi_msg* netifapimsg)
{
  err_t err;

  err = sys_sem_new(&netifapimsg->sem, 0);
  if (err != ERR_OK) {
    netifapimsg->err = err;
    return err;
  }

  if(tcpip_send_api_msg(fn, netifapimsg, &netifapimsg->sem) == ERR_OK)
  {
    sys_sem_free(&netifapimsg->sem);
    return netifapimsg->err;
  }
  return ERR_VAL;
}
Esempio n. 2
0
/**
 * Call the lower part of a netconn_* function
 * This function is then running in the thread context
 * of tcpip_thread and has exclusive access to lwIP core code.
 *
 * @param apimsg a struct containing the function to call and its parameters
 * @return ERR_OK if the function was called, another err_t if not
 */
static err_t
tcpip_apimsg(struct api_msg *apimsg)
{
#ifdef LWIP_DEBUG
  /* catch functions that don't set err */
  apimsg->msg.err = ERR_VAL;
#endif
#if LWIP_NETCONN_SEM_PER_THREAD
  apimsg->msg.op_completed_sem = LWIP_NETCONN_THREAD_SEM_GET();
  LWIP_ASSERT("netconn semaphore not initialized",
  sys_sem_valid(apimsg->msg.op_completed_sem));
#endif

#ifdef LWIP_ESP8266    
//#if 0
    sys_sem_t *op_sem_tmp = NULL;
    if(apimsg->function == lwip_netconn_do_write)
        op_sem_tmp = LWIP_API_MSG_SND_SEM(&apimsg->msg);
    else
        op_sem_tmp = LWIP_API_MSG_SEM(&apimsg->msg);
    if (tcpip_send_api_msg(apimsg->function, &apimsg->msg, op_sem_tmp) == ERR_OK) {
#else
    if (tcpip_send_api_msg(apimsg->function, &apimsg->msg, LWIP_API_MSG_SEM(&apimsg->msg)) == ERR_OK) {
#endif

        return apimsg->msg.err;
    }
    
  return ERR_VAL;
}
#endif /* !LWIP_TCPIP_CORE_LOCKING */

/**
 * Create a new netconn (of a specific type) that has a callback function.
 * The corresponding pcb is also created.
 *
 * @param t the type of 'connection' to create (@see enum netconn_type)
 * @param proto the IP protocol for RAW IP pcbs
 * @param callback a function to call on status changes (RX available, TX'ed)
 * @return a newly allocated struct netconn or
 *         NULL on memory error
 */
struct netconn*
netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto, netconn_callback callback)
{
  struct netconn *conn;
  API_MSG_VAR_DECLARE(msg);

  conn = netconn_alloc(t, callback);
  if (conn != NULL) {
    err_t err;
    API_MSG_VAR_ALLOC_DONTFAIL(msg);
    API_MSG_VAR_REF(msg).msg.msg.n.proto = proto;
    API_MSG_VAR_REF(msg).msg.conn = conn;
    TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_newconn, err);
    API_MSG_VAR_FREE(msg);
    if (err != ERR_OK) {
      LWIP_ASSERT("freeing conn without freeing pcb", conn->pcb.tcp == NULL);
      LWIP_ASSERT("conn has no recvmbox", sys_mbox_valid(&conn->recvmbox));
#if LWIP_TCP
      LWIP_ASSERT("conn->acceptmbox shouldn't exist", !sys_mbox_valid(&conn->acceptmbox));
#endif /* LWIP_TCP */
#if !LWIP_NETCONN_SEM_PER_THREAD
      LWIP_ASSERT("conn has no op_completed", sys_sem_valid(&conn->op_completed));
      sys_sem_free(&conn->op_completed);

#ifdef LWIP_ESP8266
        sys_sem_free(&conn->snd_op_completed);
#endif

#endif /* !LWIP_NETCONN_SEM_PER_THREAD */
      sys_mbox_free(&conn->recvmbox);
      memp_free(MEMP_NETCONN, conn);
      return NULL;
    }
  }
  return conn;
}

/**
 * Close a netconn 'connection' and free its resources.
 * UDP and RAW connection are completely closed, TCP pcbs might still be in a waitstate
 * after this returns.
 *
 * @param conn the netconn to delete
 * @return ERR_OK if the connection was deleted
 */
err_t
netconn_delete(struct netconn *conn)
{
  err_t err;
  API_MSG_VAR_DECLARE(msg);

  /* No ASSERT here because possible to get a (conn == NULL) if we got an accept error */
  if (conn == NULL) {
    return ERR_OK;
  }

  API_MSG_VAR_ALLOC(msg);
  API_MSG_VAR_REF(msg).msg.conn = conn;
#if LWIP_SO_SNDTIMEO || LWIP_SO_LINGER
  /* get the time we started, which is later compared to
     sys_now() + conn->send_timeout */
  API_MSG_VAR_REF(msg).msg.msg.sd.time_started = sys_now();
#else /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */
#if LWIP_TCP
  API_MSG_VAR_REF(msg).msg.msg.sd.polls_left =
    ((LWIP_TCP_CLOSE_TIMEOUT_MS_DEFAULT + TCP_SLOW_INTERVAL - 1) / TCP_SLOW_INTERVAL) + 1;
#endif /* LWIP_TCP */
#endif /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */
  TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_delconn, err);
  API_MSG_VAR_FREE(msg);

  if (err != ERR_OK) {
    return err;
  }

  netconn_free(conn);

  return ERR_OK;
}