コード例 #1
0
/**
 * Error handler callback.
 * Although this is called reconnect by Espressif, this is really an error handler
 * routine.  It will be called when an error is detected.
 */
static void esp8266_callback_reconnectCB(
		void *arg, //!< A pointer to a `struct espconn`.
		sint8 err  //!< The error code.
	) {
	os_printf(">> reconnectCB:  Error code is: %d - %s\n", err, esp8266_errorToString(err));
	os_printf("<< reconnectCB");
}
コード例 #2
0
/**
 * Error handler callback.
 * Although this is called `reconnect` by Espressif, this is really a connection reset callback.
 */
static void esp8266_callback_reconnectCB(
    void *arg, //!< A pointer to a `struct espconn`.
    sint8 err  //!< The error code.
) {
    struct espconn *pEspconn = (struct espconn *)arg;
    struct socketData *pSocketData = (struct socketData *)pEspconn->reverse;
    if (pSocketData == NULL) return; // we already closed this.
    //if (pEspconn != pSocketData->pEspconn) DBG("%s: pEspconn changed in reconnectCB ***\n", DBG_LIB);
    DBG("%s: socket %d connection reset: Err %d - %s\n", DBG_LIB,
        pSocketData->socketId, err, esp8266_errorToString(err));
    // Do the same as for a disconnect
    esp8266_callback_disconnectCB(arg);
    // Set the socket state as in error (unless it got freed by esp8266_callback_disconnectCB)
    if (pSocketData->state != SOCKET_STATE_UNUSED)
        setSocketInError(pSocketData, err);
    //DBG("%s: ret from reconnectCB\n", DBG_LIB);
}
コード例 #3
0
/**
 * Continue creating a socket, the name resolution having completed
 */
static int connectSocket(
    struct socketData *pSocketData //!< Allocated socket data structure
) {
    struct espconn *pEspconn = pSocketData->pEspconn;
    bool isServer = *(uint32_t *)&pEspconn->proto.tcp->remote_ip == 0;

    int newSocket = pSocketData->socketId;
    assert(pSocketData->rxBufQ == NULL);
    assert(pSocketData->currentTx == NULL);

    // If we are a client
    if (!isServer) {
        pSocketData->state = SOCKET_STATE_CONNECTING;
        pSocketData->creationType = SOCKET_CREATED_OUTBOUND;

        espconn_regist_connectcb(pEspconn, esp8266_callback_connectCB_outbound);
        espconn_regist_disconcb(pEspconn, esp8266_callback_disconnectCB);
        espconn_regist_reconcb(pEspconn, esp8266_callback_reconnectCB);
        espconn_regist_sentcb(pEspconn, esp8266_callback_sentCB);
        espconn_regist_recvcb(pEspconn, esp8266_callback_recvCB);

        // Make a call to espconn_connect.
#if 0
        DBG("%s: connecting socket %d/%p/%p to %d.%d.%d.%d:%d from :%d\n",
            DBG_LIB, pSocketData->socketId, pSocketData, pEspconn,
            IP2STR(pEspconn->proto.tcp->remote_ip), pEspconn->proto.tcp->remote_port,
            pEspconn->proto.tcp->local_port);
#endif
        int rc = espconn_connect(pEspconn);
        if (rc != 0) {
            DBG("%s: error %d connecting socket %d: %s\n", DBG_LIB,
                rc, pSocketData->socketId, esp8266_errorToString(rc));
            releaseEspconn(pSocketData);
            releaseSocket(pSocketData);
            return rc;
        }
        DBG("%s: connecting socket %d to %d.%d.%d.%d:%d\n", DBG_LIB, pSocketData->socketId,
            IP2STR(pEspconn->proto.tcp->remote_ip), pEspconn->proto.tcp->remote_port);
    }

    // If the ipAddress IS 0 ... then we are a server.
    else {
        // We are going to set ourselves up as a server
        pSocketData->state        = SOCKET_STATE_IDLE;
        pSocketData->creationType = SOCKET_CREATED_SERVER;
        pEspconn->proto.tcp->local_port = pEspconn->proto.tcp->remote_port;
        pEspconn->proto.tcp->remote_port = 0;

        espconn_regist_connectcb(pEspconn, esp8266_callback_connectCB_inbound);

        // Make a call to espconn_accept (this should really be called espconn_listen, sigh)
        int rc = espconn_accept(pEspconn);
        if (rc != 0) {
            DBG("%s: error %d creating listening socket %d: %s\n", DBG_LIB,
                rc, pSocketData->socketId, esp8266_errorToString(rc));
            releaseEspconn(pSocketData);
            releaseSocket(pSocketData);
            return rc;
        }
        espconn_regist_time(pEspconn, 600, 0);
        DBG("%s: listening socket %d on port %d\n", DBG_LIB,
            pSocketData->socketId, pEspconn->proto.tcp->local_port);
    }

    return newSocket;
}