void HIDServer::onSocketDisconnected()
{
    QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
    if (socket == m_interruptSocket) {
        releaseSocket(m_interruptSocket);

    } else if (socket == m_controlSocket) {
        releaseSocket(m_controlSocket);
    }

    updateConnectionState();
}
Beispiel #2
0
/**
 * Perform an actual closure of the socket by calling the ESP8266 disconnect API.
 * This is broken out into its own function because this can happen in
 * a number of possible places.
 */
static void doClose(
		int socketId //!< The socket id to be closed.
	) {

  // If the socket is NOT in state closing then ask espconn_disconnect to close
  // the socket and set the state to be closing.
  // If the socket IS in state closing, then we are ready to release.

	struct socketData *pSocketData = getSocketData(socketId);

	if (pSocketData->state != SOCKET_STATE_CLOSING) {
		int rc = espconn_disconnect(pSocketData->pEspconn);
		pSocketData->state = SOCKET_STATE_CLOSING;

		if (rc != 0) {
			os_printf("espconn_disconnect: rc=%d\n", rc);
			setSocketInError(socketId, "espconn_disconnect", rc);
		}
	}
	// Our existing state on entry was SOCKET_STATE_CLOSING which means that we got here
	// because we were previously flagged as closing.
	else {
		releaseSocket(socketId);
	}
}
/**
 * Callback handler for espconn_gethostbyname.
 */
static void dnsFoundCallback(const char *hostName, ip_addr_t *ipAddr, void *arg) {
    assert(arg != NULL); // arg points to the espconn struct where the resolved IP address needs to go
    struct espconn *pEspconn = arg;
    struct socketData *pSocketData = pEspconn->reverse;

    if (pSocketData->state == SOCKET_STATE_DISCONNECTING) {
        // the sockte library closed the socket while we were resolving, we now need to deallocate
        releaseEspconn(pSocketData);
        releaseSocket(pSocketData);
        return;
    }
    if (pSocketData->state != SOCKET_STATE_HOST_RESOLVING) return; // not sure what happened

    // ipAddr will be NULL if the IP address can not be resolved.
    if (ipAddr != NULL) {
        *(uint32_t *)&pEspconn->proto.tcp->remote_ip = ipAddr->addr;
        if (pSocketData != NULL) connectSocket(pSocketData);
    } else {
        releaseEspconn(pSocketData);
        if (pSocketData != NULL) {
            setSocketInError(pSocketData, SOCKET_ERR_NOT_FOUND);
            pSocketData->state = SOCKET_STATE_CLOSED;
        }
    }
}
/**
 * Callback function registered to the ESP8266 environment that is
 * Invoked when a connection has been disconnected. This does get invoked if we
 * initiated the disconnect (new since SDK 1.5?).
 */
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;
    if (pSocketData == NULL) return;
    //if (pEspconn != pSocketData->pEspconn) DBG("%s: pEspconn changed in disconnectCB ***\n", DBG_LIB);
    assert(pSocketData->state != SOCKET_STATE_UNUSED);
    DBG("%s: socket %d disconnected\n", DBG_LIB, pSocketData->socketId);

    // we can deallocate the espconn structure
    releaseEspconn(pSocketData);

    // if we were in SOCKET_STATE_DISCONNECTING the socket lib is already done with this socket,
    // so we can free the whole thing. Otherwise, we transition to SOCKET_STATE_CLOSED because
    // we will need to tell the socket lib about the disconnect.
    if (pSocketData->state == SOCKET_STATE_DISCONNECTING) {
        releaseSocket(pSocketData);
    } else {
        // we can deallocate the tx buffer
        if (pSocketData->currentTx != NULL) {
            //DBG("%s: freeing tx buf %p\n", DBG_LIB, pSocketData->currentTx);
            os_free(pSocketData->currentTx);
            pSocketData->currentTx = NULL;
        }

        pSocketData->state = SOCKET_STATE_CLOSED;
    }
}
void HIDServer::stop()
{
    if (m_state == HIDServer::IDLE)
        return;

    qDebug() << "stoping server...";
    disconnect(&m_interruptChannel, SIGNAL(newConnection()), this, SLOT(onInterruptChannelConnected()));
    disconnect(&m_controlChannel, SIGNAL(newConnection()), this, SLOT(onControlChannelConnected()));

    if (m_interruptSocket)
        releaseSocket(m_interruptSocket);

    if (m_controlSocket)
        releaseSocket(m_controlSocket);

    m_serviceDescriptor.unregisterService();

    setState(HIDServer::IDLE);
}
bool KBEGameSocket::connectionServer(const char* ipStr, uint32 port)
{
    ip = ipStr;
    port = port;
    releaseSocket();
    createSocket();
    if(gameSocket->connect(ip.c_str(), port))
    {
        return true;
    }
    return false;
}
/**
 * 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.
) {
    // allocate a socket data structure
    struct socketData *pSocketData = allocateNewSocket();
    if (pSocketData == NULL) { // No free socket
        DBG("%s: No free sockets for outbound connection\n", DBG_LIB);
        return SOCKET_ERR_MAX_SOCK;
    }

    // allocate espconn data structure and initialize it
    struct espconn *pEspconn = os_zalloc(sizeof(struct espconn));
    esp_tcp *tcp = os_zalloc(sizeof(esp_tcp));
    if (pEspconn == NULL || tcp == NULL) {
        DBG("%s: Out of memory for outbound connection\n", DBG_LIB);
        if (pEspconn != NULL) os_free(pEspconn);
        if (tcp != NULL) os_free(tcp);
        releaseSocket(pSocketData);
        return SOCKET_ERR_MEM;
    }

    pSocketData->pEspconn = pEspconn;
    pEspconn->type      = ESPCONN_TCP;
    pEspconn->state     = ESPCONN_NONE;
    pEspconn->proto.tcp = tcp;
    tcp->remote_port    = port;
    tcp->local_port     = espconn_port(); // using 0 doesn't work
    pEspconn->reverse   = pSocketData;
    espconn_set_opt(pEspconn, ESPCONN_NODELAY); // disable nagle, don't need the extra delay

    if (ipAddress == (uint32_t)-1) {
        // We need DNS resolution, kick it off
        int rc = espconn_gethostbyname(pEspconn, savedHostname,
                                       (void*)&pEspconn->proto.tcp->remote_ip, dnsFoundCallback);
        if (rc < 0) {
        }
        DBG("%s: resolving %s\n", DBG_LIB, savedHostname);
        pSocketData->state = SOCKET_STATE_HOST_RESOLVING;
        return pSocketData->socketId;
    } else {
        // No DNS resolution needed, go right ahead
        *(uint32_t *)&pEspconn->proto.tcp->remote_ip = ipAddress;
        return connectSocket(pSocketData);
    }
}
/**
 * Close a socket.
 * This gets called in two situations: when the user requests the close of a socket and as
 * an acknowledgment after we signal the socket library that a connection has closed by
 * returning <0 to a send or recv call.
 */
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.
) {
    struct socketData *pSocketData = getSocketData(socketId);
    assert(pSocketData != NULL); // We had better have found a socket to be closed.
    assert(pSocketData->state != SOCKET_STATE_UNUSED);  // Shouldn't be closing an unused socket.

    if (pSocketData->state == SOCKET_STATE_CLOSED) {
        // In these states we have already freed the espconn structures, so all that's left is to
        // free the socket structure
        //DBG("%s: socket %d close acknowledged\n", DBG_LIB, pSocketData->socketId);
        releaseSocket(pSocketData);
    } else {
        // Looks like this is the user telling us to close a connection, let's do it.
        DBG("%s: socket %d to be closed\n", DBG_LIB, pSocketData->socketId);
        doClose(pSocketData);
    }
}
Beispiel #9
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");
}
Beispiel #10
0
  int
  KeyRemap4MacBook_client::sendmsg(KeyRemap4MacBook_bridge::RequestType type, void* request, uint32_t requestsize, void* reply, uint32_t replysize)
  {
    if (! lock_) { return EIO; }

    IOLockWrapper::ScopedLock lk(lock_);

    if (type == KeyRemap4MacBook_bridge::REQUEST_STATUS_MESSAGE) {
      if (config.general_hide_statusmessage) {
        if (! request) return 0;
        char* p = reinterpret_cast<KeyRemap4MacBook_bridge::StatusMessage::Request*>(request)->message;
        if (p[0] != '\0') {
          return 0;
        }
      }
    }

    // ------------------------------------------------------------
    int result = 0;
    int error = 0;
    socket_t socket;
    bool isMakeSocket = false;

    if (! makeSocket(socket)) {
      result = EIO;
      goto finish;
    }
    isMakeSocket = true;

    if (! connectSocket(socket)) {
      result = EIO;
      goto finish;
    }

    // ----------------------------------------
    struct msghdr msg;
    memset(&msg, 0, sizeof(msg));

    struct iovec aiov[3];
    size_t iolen;
    aiov[0].iov_base = reinterpret_cast<caddr_t>(&type);
    aiov[0].iov_len = sizeof(type);
    if (requestsize <= 0) {
      msg.msg_iovlen = 1;
    } else {
      aiov[1].iov_base = reinterpret_cast<caddr_t>(&requestsize);
      aiov[1].iov_len = sizeof(requestsize);
      aiov[2].iov_base = reinterpret_cast<caddr_t>(request);
      aiov[2].iov_len = requestsize;
      msg.msg_iovlen = 3;
    }
    msg.msg_iov = aiov;

    error = sock_send(socket, &msg, 0, &iolen);
    if (error) {
      printf("KeyRemap4MacBook_client::sendmsg sock_send failed(%d)\n", error);
      result = error;
      goto finish;
    }

    // ----------------------------------------
    if (replysize > 0) {
      memset(&msg, 0, sizeof(msg));

      uint32_t status = -1;
      aiov[0].iov_base = reinterpret_cast<caddr_t>(&status);
      aiov[0].iov_len = sizeof(status);
      aiov[1].iov_base = reinterpret_cast<caddr_t>(reply);
      aiov[1].iov_len = replysize;
      msg.msg_iov = aiov;
      msg.msg_iovlen = 2;

      error = sock_receive(socket, &msg, MSG_WAITALL, &iolen);
      if (error) {
        printf("KeyRemap4MacBook_client::sendmsg sock_receive failed(%d)\n", error);
        result = error;
        goto finish;
      }
    }

  finish:
    if (isMakeSocket) {
      releaseSocket(socket);
    }
    if (result) {
      printf("KeyRemap4MacBook_client::sendmsg error result (%d)\n", result);
    }
    return result;
  }
//URC recognize.
bool GSM3ShieldV1MultiServerProvider::recognizeUnsolicitedEvent(byte oldTail)
{

	int nlength;
	char auxLocate [15];
	
	
	//REMOTE SOCKET CLOSED.
	prepareAuxLocate(PSTR("0, CLOSED\r\n"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		releaseSocket(0);
		socketsAccepted &= ~(0x0001);
		//Serial.println("JCR_DB REMOTE CLOSED");
	}
	
	//REMOTE SOCKET CLOSED.
	
	prepareAuxLocate(PSTR("1, CLOSED\r\n"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		releaseSocket(1);
		socketsAccepted &= ~(0x0002);
	}
	
	//REMOTE SOCKET CLOSED.
	prepareAuxLocate(PSTR("2, CLOSED\r\n"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		releaseSocket(2);
		socketsAccepted &= ~(0x0004);
	}
	
	//REMOTE SOCKET CLOSED.
	prepareAuxLocate(PSTR("3, CLOSED\r\n"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		releaseSocket(3);
		socketsAccepted &= ~(0x0008);
	}
	
	//REMOTE SOCKET CLOSED.
	prepareAuxLocate(PSTR("4, CLOSED\r\n"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		releaseSocket(4);
		socketsAccepted &= ~(0x0010);
	}
	
	//REMOTE SOCKET CLOSED.
	prepareAuxLocate(PSTR("5, CLOSED\r\n"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		releaseSocket(5);
		socketsAccepted &= ~(0x0020);
	}
	
	//REMOTE SOCKET CLOSED.
	prepareAuxLocate(PSTR("6, CLOSED\r\n"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		releaseSocket(6);
		socketsAccepted &= ~(0x0040);
	}
	
	//REMOTE SOCKET CLOSED.
	prepareAuxLocate(PSTR("7, CLOSED\r\n"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		releaseSocket(7);
		socketsAccepted &= ~(0x0080);
	}
	
	//REMOTE SOCKET ACCEPTED.
	prepareAuxLocate(PSTR("0, REMOTE IP"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		theGSM3ShieldV1ModemCore.gss.cb.flush();
		socketsAccepted |= (0x0001);
		return true;
	}
	
	//REMOTE SOCKET ACCEPTED.
	prepareAuxLocate(PSTR("1, REMOTE IP"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		theGSM3ShieldV1ModemCore.gss.cb.flush();
		socketsAccepted |= (0x0002);
		return true;
	}
	
	//REMOTE SOCKET ACCEPTED.
	prepareAuxLocate(PSTR("2, REMOTE IP"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		theGSM3ShieldV1ModemCore.gss.cb.flush();
		socketsAccepted |= (0x0004);
		return true;
	}
	
	//REMOTE SOCKET ACCEPTED.
	prepareAuxLocate(PSTR("3, REMOTE IP"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		theGSM3ShieldV1ModemCore.gss.cb.flush();
		socketsAccepted |= (0x0008);
		return true;
	}
	
	//REMOTE SOCKET ACCEPTED.
	prepareAuxLocate(PSTR("4, REMOTE IP"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		theGSM3ShieldV1ModemCore.gss.cb.flush();
		socketsAccepted |= (0x0010);
		return true;
	}
	
	//REMOTE SOCKET ACCEPTED.
	prepareAuxLocate(PSTR("5, REMOTE IP"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		theGSM3ShieldV1ModemCore.gss.cb.flush();
		socketsAccepted |= (0x0020);
		return true;
	}
	
	//REMOTE SOCKET ACCEPTED.
	prepareAuxLocate(PSTR("6, REMOTE IP"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		theGSM3ShieldV1ModemCore.gss.cb.flush();
		socketsAccepted |= (0x0040);
		return true;
	}
	
	//REMOTE SOCKET ACCEPTED.
	prepareAuxLocate(PSTR("7, REMOTE IP"), auxLocate);
	if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
	{
		//To detect remote socket closed for example inside socket data.
		theGSM3ShieldV1ModemCore.gss.cb.flush();
		socketsAccepted |= (0x0080);
		return true;
	}
	
	
	return false;
}
/**
 * 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;
}
KBEGameSocket::~KBEGameSocket()
{
    releaseSocket();
}