//------------------------------------------------------------------------------
void MobiCoreDriverDaemon::processNqConnect(
    Connection  *connection
) {
	do
	{
		// Set up the channel for sending SWd notifications to the client
		// MC_DRV_CMD_NQ_CONNECT is only allowed on new connections not
		// associated with a device. If a device is registered to the
		// connection NQ_CONNECT is not allowed.

		// Read entire command data
		mcDrvCmdNqConnectPayload_t  cmdNqConnectPayload;
		size_t rlen = connection->readData(
										&(cmdNqConnectPayload),
										sizeof(cmdNqConnectPayload));
		if (rlen != sizeof(cmdNqConnectPayload))
		{
			LOG_E("processNqConnect(): NqConnect length error: %d",rlen);
			writeResult(connection,MC_DRV_RSP_PAYLOAD_LENGTH_ERROR);
			break;
		}

		// device must be empty
		MobiCoreDevice *device = (MobiCoreDevice *)(connection->connectionData);
		if (NULL != device)
		{
			LOG_E("processNqConnect(): device already set\n");
			writeResult(connection,MC_DRV_RSP_COMMAND_NOT_ALLOWED);
			break;
		}

		// Remove the connection from the list of known client connections
		for (int i = 0; i < MAX_SERVERS; i++) {
			servers[i]->detachConnection(connection);
		}

		device = getDevice(cmdNqConnectPayload.deviceId);
		if (NULL == device)
		{
			//TODO xgal: how about ...NO_SUCH_DEVICE
			LOG_E("processNqConnect(): no device found\n");
			writeResult(connection, MC_DRV_RSP_FAILED);
			break;
		}

		TrustletSession* ts = device->registerTrustletConnection(
								connection,
								&cmdNqConnectPayload);
		if (!ts) {
			LOG_E("processNqConnect(): registerTrustletConnection() failed!");
			writeResult(connection, MC_DRV_RSP_FAILED);
			break;
		}

		writeResult(connection, MC_DRV_RSP_OK);
		ts->processQueuedNotifications();
		

	} while (false);
}
//------------------------------------------------------------------------------
void MobiCoreDriverDaemon::processNqConnect(Connection *connection)
{
    // Set up the channel for sending SWd notifications to the client
    // MC_DRV_CMD_NQ_CONNECT is only allowed on new connections not
    // associated with a device. If a device is registered to the
    // connection NQ_CONNECT is not allowed.

    // Read entire command data
    MC_DRV_CMD_NQ_CONNECT_struct cmd;
    RECV_PAYLOAD_FROM_CLIENT(connection, &cmd);

    // device must be empty since this is a new connection
    MobiCoreDevice *device = (MobiCoreDevice *)(connection->connectionData);
    if (device != NULL) {
        LOG_E("device already set\n");
        writeResult(connection, MC_DRV_ERR_NQ_FAILED);
        return;
    }

    // Remove the connection from the list of known client connections
    for (int i = 0; i < MAX_SERVERS; i++) {
        servers[i]->detachConnection(connection);
    }

    device = getDevice(cmd.deviceId);
    // Check if a device for the given name has been found
    if (NULL == device) {
        LOG_E("invalid deviceId");
        writeResult(connection, MC_DRV_ERR_UNKNOWN_DEVICE);
        return;
    }

    TrustletSession *ts = device->registerTrustletConnection(
                              connection,
                              &cmd);
    if (!ts) {
        LOG_E("registerTrustletConnection() failed!");
        writeResult(connection, MC_DRV_ERR_UNKNOWN);
        return;
    }

    writeResult(connection, MC_DRV_OK);
    ts->processQueuedNotifications();
}