Пример #1
0
/**
 * Callback function registered to the ESP8266 environment that is
 * invoked when a new outbound connection has been formed.
 */
static void esp8266_callback_connectCB_outbound(
		void *arg //!< A pointer to a `struct espconn`.
	) {
	os_printf(">> connectCB_outbound\n");
	struct espconn *pEspconn = (struct espconn *)arg;
	assert(pEspconn != NULL);

	dumpEspConn(pEspconn);

	struct socketData *pSocketData = (struct socketData *)pEspconn->reverse;
	assert(pSocketData != NULL);

	esp8266_dumpSocket(pSocketData->socketId);

	// Flag the socket as connected to a partner.
	pSocketData->isConnected = true;

	assert(pSocketData->state == SOCKET_STATE_CONNECTING);
	if (pSocketData->shouldClose) {
		doClose(pSocketData->socketId);
	} else {
		pSocketData->state = SOCKET_STATE_IDLE;
	}
	os_printf("<< connectCB_outbound\n");
}
Пример #2
0
/**
 * Release the socket and return it to the free pool.
 */
static void releaseSocket(
		int socketId //!< The socket id of the socket to be released.
	) {
	os_printf("> releaseSocket: %d\n", socketId);
	esp8266_dumpSocket(socketId);

	struct socketData *pSocketData = getSocketData(socketId);
	assert(pSocketData->state != SOCKET_STATE_UNUSED);

	if (memoryBuffer_getSize(&pSocketData->txMemoryBuffer) > 0) {
		os_printf(" - Oh oh ... attempt to close socket while the TX memoryBuffer is not empty!\n");
	}
	if (pSocketData->rxBuf != NULL || pSocketData->rxBufLen != 0) {
		os_printf(" - Oh oh ... attempt to close socket while the rxBuffer is not empty!\n");
	}

	// If this socket is not an incoming socket that means that the espconn structure was created
	// by us and we should release the storage we allocated.
	if (pSocketData->creationType != SOCKET_CREATED_INBOUND) {
	  os_free(pSocketData->pEspconn->proto.tcp);
	  pSocketData->pEspconn->proto.tcp = NULL;
	  os_free(pSocketData->pEspconn);
	  pSocketData->pEspconn = NULL;
	}
	resetSocketByData(pSocketData);
	os_printf("< releaseSocket\n");
}
Пример #3
0
/**
 * Close a socket.
 */
void net_ESP8266_BOARD_closeSocket(
		JsNetwork *net, //!< The Network we are going to use to create the socket.
		int socketId    //!< The socket to be closed.
	) {
	os_printf("> net_ESP8266_BOARD_closeSocket, socket=%d\n", socketId);

	struct socketData *pSocketData = getSocketData(socketId);

	assert(pSocketData != NULL);
	assert(pSocketData->state != SOCKET_STATE_UNUSED);  // Shouldn't be closing an unused socket.

	dumpEspConn(pSocketData->pEspconn);
	esp8266_dumpSocket(socketId);

	// How we close the socket is a function of what kind of socket it is.
	if (pSocketData->creationType == SOCKET_CREATED_SERVER) {
		int rc = espconn_delete(pSocketData->pEspconn);
		if (rc != 0) {
			os_printf("espconn_delete: rc=%d\n", rc);
		}
	} // End this is a server socket
	else
	{
		if (pSocketData->state == SOCKET_STATE_IDLE || pSocketData->state == SOCKET_STATE_CLOSING) {
			doClose(socketId);
		} else {
			pSocketData->shouldClose = true;
		}
	} // End this is a client socket
}
Пример #4
0
/**
 * Send data to the partner.
 * The return is the number of bytes actually transmitted which may also be
 * 0 to indicate no bytes sent or -1 to indicate an error.  For the ESP8266 implementation we
 * will return 0 if the socket is not connected or we are in the `SOCKET_STATE_TRANSMITTING`
 * state.
 */
int net_ESP8266_BOARD_send(
		JsNetwork *net,  //!< The Network we are going to use to create the socket.
		int sckt,        //!< The socket over which we will send data.
		const void *buf, //!< The buffer containing the data to be sent.
		size_t len       //!< The length of data in the buffer to send.
	) {
	os_printf("> net_ESP8266_BOARD_send: Request to send data to socket %d of size %d: ", sckt, len);

	struct socketData *pSocketData = getSocketData(sckt);
	assert(pSocketData->state != SOCKET_STATE_UNUSED);

	// If we are not connected, then we can't currently send data.
	if (pSocketData->isConnected == false) {
		os_printf(" - Not connected\n");
		return 0;
	}

	// If we are currently sending data, we can't send more.
	if (pSocketData->state == SOCKET_STATE_TRANSMITTING) {
		os_printf(" - Currently transmitting\n");
		return 0;
	}

	// Log the content of the data we are sening.
	esp8266_board_writeString(buf, len);

	os_printf("\n");

	assert(pSocketData->state == SOCKET_STATE_IDLE);

	pSocketData->state = SOCKET_STATE_TRANSMITTING;

	// Copy the data that was passed to us to a private area.  We do this because we must not
	// assume that the data passed in will be available after this function returns.  It may have
	// been passed in on the stack.
	assert(pSocketData->currentTx == NULL);
	pSocketData->currentTx = (uint8_t *)os_malloc(len);
	memcpy(pSocketData->currentTx, buf, len);

	// Send the data over the ESP8266 SDK.
	int rc = espconn_send(pSocketData->pEspconn, pSocketData->currentTx, len);
	if (rc < 0) {
		setSocketInError(sckt, "espconn_send", rc);
		os_free(pSocketData->currentTx);
		pSocketData->currentTx = NULL;
		return -1;
	}

	esp8266_dumpSocket(sckt);
	os_printf("< net_ESP8266_BOARD_send\n");
	return len;
}
Пример #5
0
/**
 * Callback function registered to the ESP8266 environment that is
 * invoked when a new inbound connection has been formed.
 * A new connection
 * can occur when the ESP8266 makes a call out to a partner (in that
 * case the ESP8266 is acting as a client) or a new connection can
 * occur when a partner calls into a listening ESP8266.  In that case
 * the ESP8266 is acting as a server.
 */
static void esp8266_callback_connectCB_inbound(
		void *arg //!<
	) {
	os_printf(">> connectCB_inbound\n");
	struct espconn *pEspconn = (struct espconn *)arg;
	assert(pEspconn != NULL);

  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);
  espconn_regist_write_finish(pEspconn, esp8266_callback_writeFinishedCB);

	dumpEspConn(pEspconn);

	int inboundSocket = getServerSocketByLocalPort(pEspconn->proto.tcp->local_port);
	assert(inboundSocket != -1);
	struct socketData *pSocketData = getSocketData(inboundSocket);
	assert(pSocketData != NULL);

	esp8266_dumpSocket(pSocketData->socketId);

	os_printf("** new client has connected to us **\n");

	if ((pSocketData->acceptedSocketsHead + 1) % MAX_ACCEPTED_SOCKETS == pSocketData->acceptedSocketsTail) {
		os_printf("WARNING!! - Discarding inbound client because we have too many accepted clients.\n");
		os_printf("<< connectCB_inbound\n");
		return;
	}

	struct socketData *pClientSocketData = allocateNewSocket();
	if (pClientSocketData == NULL) {
		os_printf("!!! Ran out of sockets !!!\n");
		return;
	}
	assert(pClientSocketData != NULL);
	pClientSocketData->pEspconn          = pEspconn;
	pClientSocketData->pEspconn->reverse = pClientSocketData;
	pClientSocketData->creationType      = SOCKET_CREATED_INBOUND;
	pClientSocketData->isConnected       = true;
	pClientSocketData->state             = SOCKET_STATE_IDLE;

	pSocketData->acceptedSockets[pSocketData->acceptedSocketsHead] = pClientSocketData->socketId;
	pSocketData->acceptedSocketsHead = (pSocketData->acceptedSocketsHead + 1) % MAX_ACCEPTED_SOCKETS;

	os_printf("<< connectCB_inbound\n");
}
Пример #6
0
/**
 * Callback function registered to the ESP8266 environment that is
 * Invoked when a previous connection has been disconnected.
 */
static void esp8266_callback_disconnectCB(
		void *arg //!< A pointer to a `struct espconn`.
	) {
	struct espconn *pEspconn = (struct espconn *)arg;
	struct socketData *pSocketData = (struct socketData *)pEspconn->reverse;
	assert(pSocketData != NULL);
	assert(pSocketData->state != SOCKET_STATE_UNUSED);

	os_printf(">> disconnectCB\n");
	dumpEspConn(pEspconn);
	esp8266_dumpSocket(pSocketData->socketId);

	// If the socket state is SOCKET_STATE_CLOSING then that means we can release the socket.  The reason
	// for this is that the last thing the user did was request an explicit socket close.
	if (pSocketData->state == SOCKET_STATE_CLOSING) {
		releaseSocket(pSocketData->socketId);
	} else {
		pSocketData->state       = SOCKET_STATE_CLOSING;
		pSocketData->isConnected = false;
	}
	os_printf("<< disconnectCB\n");
}
Пример #7
0
void jswrap_ESP8266WiFi_dumpSocket(
    JsVar *socketId //!< The socket to be dumped.
  ) {
  esp8266_dumpSocket(jsvGetInteger(socketId)-1);
}