コード例 #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
/**
 * 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
}
コード例 #3
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");
}
コード例 #4
0
ファイル: ESP_RESTClient.cpp プロジェクト: nkolban/ESPLibs
void ESP_RestClient::_connect() {
	LOG("_connect: %i\n", &m_serverIP);
	memcpy(m_conn.proto.tcp->remote_ip, &m_serverIP, 4);
	m_conn.proto.tcp->remote_port = m_port;
	dumpEspConn(&m_conn);
	if (m_secure == false) {
		int rc = espconn_connect(&m_conn);
		if (rc != 0) {
			// Error
			LOG("Error with connect\n");
			system_os_post(REST_TASK_PRI, ESP_REST_CLIENT_ERROR_SIG, (os_param_t)this);
			return;
		}
	} else {
		//int rc = espconn_secure_connect(&m_conn);
	}
} // End of m_connect
コード例 #5
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");
}
コード例 #6
0
/**
 * ESP8266 callback function that is invoked when new data has arrived over
 * the TCP/IP connection.
 */
static void esp8266_callback_recvCB(
		void *arg,         //!< A pointer to a `struct espconn`.
		char *pData,       //!< A pointer to data received over the socket.
		unsigned short len //!< The length of the data.
	) {
	struct espconn *pEspconn = (struct espconn *)arg;
	struct socketData *pSocketData = (struct socketData *)pEspconn->reverse;

	assert(pSocketData != NULL);
	assert(pSocketData->state != SOCKET_STATE_UNUSED);

	os_printf(">> recvCB for socket=%d, length=%d\n", pSocketData->socketId, len);

	// If we don't have any existing unconsumed data then malloc some storage and
	// copy the received data into that storage.
	if (pSocketData->rxBufLen == 0) {
		pSocketData->rxBuf = (void *)os_malloc(len);
		memcpy(pSocketData->rxBuf, pData, len);
		pSocketData->rxBufLen = len;
	} else {
// Allocate a new buffer big enough for the original data and the new data
// Copy the original data to the start of the new buffer ...
// Copy the new new data to the offset into the new buffer just after
//   the original data.
// Release the original data.
// Update the socket data.
		uint8 *pNewBuf = (uint8 *)os_malloc(len + pSocketData->rxBufLen);
		memcpy(pNewBuf, pSocketData->rxBuf, pSocketData->rxBufLen);
		memcpy(pNewBuf + pSocketData->rxBufLen, pData, len);
		os_free(pSocketData->rxBuf);
		pSocketData->rxBuf = pNewBuf;
		pSocketData->rxBufLen += len;
	} // End of new data allocated.
	dumpEspConn(pEspconn);
	os_printf("<< recvCB\n");

}
コード例 #7
0
/**
 * Create a new socket.
 * if `ipAddress == 0`, creates a server otherwise creates a client (and automatically connects). Returns >=0 on success.
 */
int net_ESP8266_BOARD_createSocket(
		JsNetwork *net,     //!< The Network we are going to use to create the socket.
		uint32_t ipAddress, //!< The address of the partner of the socket or 0 if we are to be a server.
		unsigned short port //!< The port number that the partner is listening upon.
	) {
	os_printf("> net_ESP8266_BOARD_createSocket: host: %d.%d.%d.%d, port:%d \n", ((char *)(&ipAddress))[0], ((char *)(&ipAddress))[1], ((char *)(&ipAddress))[2], ((char *)(&ipAddress))[3], port);

	bool isServer = (ipAddress == 0);

	struct socketData *pSocketData = allocateNewSocket();
	if (pSocketData == NULL) { // No free socket
		os_printf("< net_ESP8266_BOARD_createSocket: No free sockets\n");
		return -1;
	}

	int newSocket = pSocketData->socketId;
	pSocketData->pEspconn = (struct espconn *)os_malloc(sizeof(struct espconn));
	assert(pSocketData->pEspconn);

	struct espconn *pEspconn = pSocketData->pEspconn;

	pEspconn->type      = ESPCONN_TCP;
	pEspconn->state     = ESPCONN_NONE;
	pEspconn->proto.tcp = (esp_tcp *)os_malloc(sizeof(esp_tcp));
	pEspconn->reverse   = pSocketData;
	assert(pEspconn->proto.tcp != NULL);
	os_memset(pEspconn->proto.tcp, 0, sizeof(esp_tcp));

	// NOTE: We must not call these functions until AFTER we have allocated storage
	// for the 'esp_tcp' structure.
  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);

	struct ip_info ipconfig;
	wifi_get_ip_info(STATION_IF, &ipconfig); // Get the local IP address
	os_memcpy(pEspconn->proto.tcp->local_ip, &ipconfig.ip, 4);

	// If we are not a server ...
	if (isServer == false) {
		pSocketData->state = SOCKET_STATE_CONNECTING;
		pSocketData->creationType = SOCKET_CREATED_OUTBOUND;
		pEspconn->proto.tcp->remote_port = port;
		pEspconn->proto.tcp->local_port  = espconn_port();

		*(uint32 *)(pEspconn->proto.tcp->remote_ip) = ipAddress;

		// Ensure that we have flagged this socket as NOT connected
		pSocketData->isConnected = false;

		espconn_regist_connectcb(pEspconn, esp8266_callback_connectCB_outbound);

		// Make a call to espconn_connect.
		int rc = espconn_connect(pEspconn);
		if (rc != 0) {
			os_printf("Err: net_ESP8266_BOARD_createSocket -> espconn_connect returned: %d.  Using local port: %d\n", rc, pEspconn->proto.tcp->local_port);
			setSocketInError(newSocket, "espconn_connect", rc);
		}
	}
	// 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 = port;

		espconn_regist_connectcb(pEspconn, esp8266_callback_connectCB_inbound);

		// Make a call to espconn_accept
		int rc = espconn_accept(pEspconn);
		if (rc != 0) {
			os_printf("Err: net_ESP8266_BOARD_createSocket -> espconn_accept returned: %d.  Using local port: %d\n", rc, pEspconn->proto.tcp->local_port);
			setSocketInError(newSocket, "espconn_accept", rc);
		}
	}

	dumpEspConn(pEspconn);
	os_printf("< net_ESP8266_BOARD_createSocket, socket=%d\n", newSocket);
	return newSocket;
}