Exemplo n.º 1
0
/*!
  Loads power data from the file and sets the in xml format as
  the response data.
*/
void InfoLogger::loadPwrData(TasResponse& response, TasCommand* command)
{
    if(mPwr){
        response.setData(loadData(mPwr, "pwrUsage", command));
        if(command->parameter(ACTION) == "stop"){
            delete mPwr;
            mPwr = 0;
            mState ^= PwrLogging;
        }
    }
    else{
        response.setErrorMessage("No data collected!");
    }
}
Exemplo n.º 2
0
/*!
  Loads gpu data from the file and sets the in xml format as
  the response data.
*/
void InfoLogger::loadGpuData(TasResponse& response, TasCommand* command)
{
    if(mGpu){
        response.setData(loadData(mGpu, "gpuMemUsage", command));            
        if(command->parameter(ACTION) == "stop"){
            delete mGpu;
            mGpu = 0;
            mState ^= GpuLogging;
        }
    }
    else{
        response.setErrorMessage("No data collected!");
    }
}
void ShellCommandService::killTask(qint64 pid, TasResponse& response)
{
    ShellTask* task = mTasks.value(pid);
    if (task) {
        TasLogger::logger()->debug("ShellCommandService::terminating task");
        task->endTask(); // Deleteafter in terminate
        mTasks.remove(pid);
        TasLogger::logger()->debug("ShellCommandService::killTask Pid " + QString::number(pid) + " was killed.");
        response.setData("Pid " + QString::number(pid) + " was killed.");
    } else {
        TasLogger::logger()->debug("ShellCommandService::killTask Pid " + QString::number(pid) + " was not found.");
        response.setData("Pid " + QString::number(pid) + " was not found.");
    }

}
Exemplo n.º 4
0
/*!
  Loads mem data from the file and sets the in xml format as
  the response data.
*/
void InfoLogger::loadMemData(TasResponse& response, TasCommand* command)
{
    if(mMem){
        response.setData(loadData(mMem, "memUsage", command));    
        if(command->parameter(ACTION) == "stop"){
            delete mMem;
            mMem = 0;
            mState ^= MemLogging;
        }
    }
    else{
        TasLogger::logger()->debug("InfoLogger::loadMemData no file to load");
        response.setErrorMessage("No data collected!");
    }
}
bool ShellCommandService::executeService(TasCommandModel& model, TasResponse& response)
{
    if(model.service() == serviceName() ){
        TasCommand* command = getCommandParameters(model, "shellCommand");
        if(command && !command->text().isEmpty()){
            if (command->parameter("detached") == "true"){
                detachedShellCommand(command->text(), response);
            }
            else if (command->parameter("threaded") == "true") {
                shellTask(command->text(), response);
            }
            else if (command->parameter("status") == "true") {
                TasCommand* command = getCommandParameters(model, "shellCommand");
                if(command && !command->text().isEmpty()){
                    qint64 pid = command->text().toInt();
                    if (command->parameter("kill") == "true") {
                        killTask(pid, response);
                    } else {
                        shellStatus(pid, response);
                    }
                }
            }
            else{
                shellCommand(command->text(), response);
            }
        }
        else{
            response.setErrorMessage(NO_COMMAND_TO_EXECUTE);
        }
        return true;
    }
    else{
        return false;
    }
}
Exemplo n.º 6
0
bool RegisterService::executeService(TasCommandModel& model, TasResponse& response)
{
    if(model.service() == serviceName() ){
        TasCommand* command = getCommandParameters(model, COMMAND_REGISTER);
        if(command){
            ClientDetails client;
            bool ok;
            //this should not really ever fail (app pid used to generate in client side)
            quint64 clientPid = command->parameter(PLUGIN_ID).toULongLong(&ok); 
            client.processId = clientPid;
            client.processName = command->parameter(PLUGIN_NAME);
#ifdef Q_OS_SYMBIAN
            client.applicationUid = command->parameter(APP_UID);
#endif
            client.pluginType = command->parameter(PLUGIN_TYPE);
            client.socket = response.requester();
            registerPlugin(client);
        }
        else{
            command = getCommandParameters(model, COMMAND_UNREGISTER);
            if(command){
                unRegisterPlugin(*command);
            }
        }
        return true;
    }
    else{
        return false;
    }
}
/*!

Executes a command as a process
Output written to given socket as response.
*/
void ShellCommandService::detachedShellCommand(QString message, TasResponse& response)
{
    TasLogger::logger()->debug("ShellCommandService::detachedShellCommand: " + message);
    QProcess process;
    process.setReadChannelMode(QProcess::MergedChannels);
    process.setEnvironment(QProcess::systemEnvironment());
    bool started = process.startDetached(message);

    if(!started){
        response.setErrorMessage("Failed to start process!");
    }
}
/*!

Executes a command as a process
Output written to given socket as response.
*/
void ShellCommandService::shellCommand(QString message, TasResponse& response)
{
    TasLogger::logger()->debug("ShellCommandService::shellCommand: " + message);

    QProcess process;
    process.setReadChannelMode(QProcess::MergedChannels);
    process.setEnvironment(QProcess::systemEnvironment());
    process.start(message);

    process.closeWriteChannel();
    process.waitForFinished(4000);

    QByteArray output = process.readAll();
    response.setData(output);
}
void ShellCommandService::shellTask(const QString&  command, TasResponse &response)
{
    TasLogger::logger()->debug("ShellCommandService::shellTask: " + command);

    ShellTask* task = new ShellTask(command);
    connect(task, SIGNAL(finished()), this, SLOT(finished()));
    task->start();
    // Wait for the thread to start.
    TasCoreUtils::wait(1000);
    qint64 pid = task->pid();
    if (pid != 0) {
        mTasks[task->pid()] = task;
    } else {
        delete task;
    }
    response.setData(QString::number(pid));
}
bool ScreenshotService::executeService(TasCommandModel& model, TasResponse& response)
{
    if(model.service() == serviceName() ){
        TasLogger::logger()->debug("ScreenshotService::executeService in");
        QGuiApplication *app = qobject_cast<QGuiApplication*>(qApp);
        if(app){
            getScreenshot(model, response);
        }
        else{
            TasLogger::logger()->debug("ScreenshotService::executeService application has no ui!");
            response.setErrorMessage(NO_UI_ERROR);
        }
        return true;
    }
    else{
        return false;
    }
}
void ShellCommandService::shellStatus(qint64 pid, TasResponse& response)
{
    TasLogger::logger()->debug("ShellCommandService::service: looking for pid " +
                               QString::number(pid));

    if (mTasks.contains(pid)) {
        TasLogger::logger()->debug("ShellCommandService::got it");

        ShellTask* task = mTasks.value(pid);

        TasLogger::logger()->debug("ShellCommandService::setting run data");
        TasDataModel* model = new TasDataModel();
        QString qtVersion = "Qt" + QString(qVersion());
        TasObjectContainer& container = model->addNewObjectContainer(1, qtVersion, "qt");

        TasObject& output = container.addNewObject("2","Response","Response");

        ShellTask::Status status = task->status();

        switch (status) {
        case ShellTask::ERR:
            output.addAttribute("status", "ERROR");
            break;
        case ShellTask::RUNNING:
            output.addAttribute("status", "RUNNING");
            break;
        case ShellTask::FINISHED:
            output.addAttribute("status", "FINISHED");
            output.addAttribute("exitCode", task->returnCode());
            break;
        case ShellTask::NOT_STARTED:
        default:
            output.addAttribute("status", "NOT_STARTED");
            break;

        }
        output.addAttribute("output", QString(task->responseData()));

        // Clean up if process is done.
        if (status != ShellTask::RUNNING) {
            mTasks.remove(pid);
            if (task->isRunning()) {
                task->endTask();
            } else {
                task->deleteLater();
                task = 0;
            }


            TasLogger::logger()->debug("ShellCommandService::service: deleting");

            TasLogger::logger()->debug("ShellCommandService::service: donne");
        }

        QByteArray xml;
        model->serializeModel(xml);
        delete model;
        response.setData(xml);
    }

    TasLogger::logger()->debug("ShellCommandService::service: shell status done");
}
void ObjectService::performObjectService(TasCommandModel& model, TasResponse& response)
{
    TasLogger::logger()->debug(QString("TasCommander::performObjectService entry"));
    QListIterator<TasTarget*> i(model.targetList());
    QString errorString;
    while (i.hasNext()){
        TasTarget* commandTarget = i.next();
        QString targetId = commandTarget->id();
        QString targetType = commandTarget->type();
        QObject* target = 0;

        if(targetType == TYPE_GRAPHICS_VIEW){
            QGraphicsItem* item = findGraphicsItem(targetId);
            //target = TestabilityUtils::castToGraphicsWidget(item);
            target = item->toGraphicsObject();
        }
        else if(targetType == TYPE_STANDARD_VIEW){
            target = findWidget(targetId);

            //If Type is Standard View check QObjects in cache too.
            if(!target) {
              target = findObject(targetId);
            }
        }
        else if(targetType == TYPE_QSCENEGRAPH){
            target = findQuickItem(targetId);
        }
        else if(targetType == TYPE_APPLICATION_VIEW ){
            target = qApp;
        }
        // TODO: add support
        //else if(targetType == TYPE_ACTION_VIEW = "Action";
        //else if(targetType == TYPE_LAYOUT = "Layout";
        //else if(targetType == TYPE_LAYOUT_ITEM = "LayoutItem";
        //else if(targetType == TYPE_WEB = "Web";
        //else if(targetType == TYPE_QWEB = "QWeb";
        else {
            errorString.append(targetType + " target type not supported. ");
        }

        TasLogger::logger()->debug(
                    QString("TasCommander::performObjectService %1 %2 found: %3")
                    .arg(targetType).arg(targetId).arg(target!=NULL));

        if (target) {
            QListIterator<TasCommand*> j(commandTarget->commandList());
            while (j.hasNext()){
                TasCommand* command = j.next();
                if(command->name() == "SetAttribute"){
                    doSetAttribute(command, target, errorString);
                }
                else if (command->name() == "CallMethod"){
                    response.setData(doCallMethod(command, target, errorString));
                }
                else {
                    TasLogger::logger()->debug(
                                QString("TasCommander::performObjectService %1 %2: unsupported %3")
                                .arg(targetType).arg(targetId).arg(command->name()));
                }
            }
        }
    }

    if(!errorString.isEmpty()){
        response.setErrorMessage(errorString);
        TasLogger::logger()->debug(
                    QString("TasCommander::performObjectService errors: %1")
                    .arg(errorString));
    }
    else {
        TasLogger::logger()->debug(
                    QString("TasCommander::performObjectService, no errors."));
    }
}
void ScreenshotService::getScreenshot(TasCommandModel& model, TasResponse& response)
{
    QListIterator<TasTarget*> i(model.targetList());
    QString errorMsg = PARSE_ERROR;
    QImage screenshot;
    QString pictureFormat = "PNG";
    while (i.hasNext()) {
        TasTarget* commandTarget = i.next();
        QString targetId = commandTarget->id();
        QString targetType = commandTarget->type();
        TasCommand* command = commandTarget->findCommand("Screenshot");

        // are required for command completion
        if (targetId.isEmpty() || targetType.isEmpty() || !command) {
            continue;
        }

        if (!command->parameter("format").isEmpty()) {
            pictureFormat = command->parameter("format");
        }

        if (!isFormatSupported(pictureFormat)) {
            errorMsg = "Given format " + pictureFormat + "is not supported. Supported formats are: PNG, JPEG and BMP.";
            break;
        }

        bool draw = (command->parameter("draw") == "true");

        QWidget* widget = 0;
        QQuickWindow* qtQuickWindow = 0;
        WId winId = 0;
        QRect rect(0,0,-1,-1);

        errorMsg = "Taking screenshot failed!";

        if (targetType == TYPE_GRAPHICS_VIEW) {
            //TasLogger::logger()->debug("TYPE_GRAPHICS_VIEW Target id:" + targetId);
            QGraphicsItem* item = findGraphicsItem(targetId);

            if (item) {
                QGraphicsView* view = getViewForItem(item);
                if(view) {
                    ItemLocationDetails locationDetails = TestabilityUtils::getItemLocationDetails(item);
                    rect = QRect(locationDetails.windowPoint.x(),
                                 locationDetails.windowPoint.y(),
                                 locationDetails.width,
                                 locationDetails.height);

                    if (draw) {
                        widget = view->window();
                    } else {
                        winId = view->window()->winId();
                    }
                } else {
                    errorMsg = "Could not find a GraphicsView for the GraphicsItem!";
                }
            } else {
                errorMsg = "Could not find the GraphicsItem!";
            }
        } else if (targetType == TYPE_STANDARD_VIEW) {
            //TasLogger::logger()->debug("TYPE_STANDARD_VIEW about to find widget Target id:" + targetId);
            widget = findWidget(targetId);

            if (widget) {
                if ((widget->isWindow() && !draw) || widget->inherits("QDesktopWidget")) {
                    winId = widget->winId();
                    widget = 0;
                } else if (!draw) {
                    QPoint point = widget->mapToGlobal(QPoint(0,0));
                    QPoint windowPoint = widget->window()->mapFromGlobal(point);

                    rect = QRect(windowPoint.x(),
                                 windowPoint.y(),
                                 widget->rect().width(),
                                 widget->rect().width());
                    winId = widget->window()->winId();
                    widget = 0;
                }
            } else {
                TasLogger::logger()->debug("ScreenshotService::executeService application has no visible ui!");
                errorMsg = "Application has no visible ui!";
            }
        } else if (targetType == TYPE_QSCENEGRAPH) {
            QQuickItem* item = TestabilityUtils::findQuickItem(targetId);

            if (item) {
                QPointF offset = item->mapToScene(QPointF(0,0));
                rect = QRect(-offset.x(), -offset.y(), item->width(), item->height());
                qtQuickWindow = item->window();
            }
        } else {
            //TasLogger::logger()->debug("TYPE_APPLICATION_VIEW about to find application window Target id:" + targetId);
            widget = getApplicationWidget();

            if (!widget) {
                QWindow *window = getApplicationWindow();
                //in case no window false, return the desktop
                qtQuickWindow = qobject_cast<QQuickWindow *>(window);

                if (!window) {
                    widget = qApp->desktop();
                }
            }
        }

        if (widget) {
            screenshot = widget->grab(rect).toImage();

            if (!screenshot.isNull()) {
                screenshot.setText("tas_id", objectId(widget));
            }
        } else if (qtQuickWindow) {
            screenshot = qtQuickWindow->grabWindow();
            if (!screenshot.isNull()) {
                screenshot.setText("tas_id", objectId(qtQuickWindow));
            }
        } else if (winId) {
            screenshot = QPixmap::grabWindow(winId, rect.x(), rect.y(), rect.width(), rect.height()).toImage();

            if (!screenshot.isNull()) {
                screenshot.setText("tas_id", QString::number(winId));
            }
        }

        break;
    }

    if (!screenshot.isNull()) {
        QByteArray bytes;
        QBuffer buffer(&bytes);
        buffer.open(QIODevice::WriteOnly);
        screenshot.save(&buffer, pictureFormat.toLatin1());
        response.setData(bytes);
        buffer.close();
    } else {
        response.setErrorMessage(errorMsg);
    }

}
Exemplo n.º 14
0
/*!
  Passes service directed to plugins on to the correct plugin.
*/
void InfoLogger::performLogService(TasCommandModel& model, TasResponse& response)
{
    TasTarget* target = model.findTarget(APPLICATION_TARGET);
    if(!mTimer.isActive() && model.interval() > 100){
        mTimer.setInterval(model.interval());
    }
    if(target){
        TasCommand* command = target->findCommand(CPU);
        if(command){
            if(command->parameter(ACTION) == "start"){
                QString fileName;
                if(!makeFileName(command, CPU, fileName)){
                    response.setErrorMessage("File path must be defined for cpu logging!");
                }
                else{
                    mLastCpuTime = mDeviceUtils->currentProcessCpuTime();
                    mInterval.start();
                    mState |= CpuLogging;     
                    if(mCpu){
                        delete mCpu;
                        mCpu = 0;
                    }
                    mCpu = openFile(fileName, command);
                }
            }
            else if(command->parameter(ACTION) == "stop" || command->parameter(ACTION) == "load"){
                loadCpuData(response, command);
            }
        }
        command = target->findCommand(MEM);
        if(command){
            if(command->parameter(ACTION) == "start"){
                QString fileName;
                if(!makeFileName(command, MEM, fileName)){
                    response.setErrorMessage("File path must be defined for mem logging!");
                }
                else{
                    mState |= MemLogging;
                    if(mMem){
                        delete mMem;
                        mMem = 0;
                    }
                    mMem = openFile(fileName, command);
                }
            }
            else if(command->parameter(ACTION) == "stop" || command->parameter(ACTION) == "load"){
                loadMemData(response, command);
            }

        }
        command = target->findCommand(GPU);
        if(command){
            if(command->parameter(ACTION) == "start"){
                QString fileName;
                if(!makeFileName(command, GPU, fileName)){
                    response.setErrorMessage("File path must be defined for mem logging!");
                }
                else{
                    mState |= GpuLogging;
                    if(mGpu){
                        delete mGpu;
                        mGpu = 0;
                    }
                    mGpu = openFile(fileName, command);
                }

            }
            else if(command->parameter(ACTION) == "stop" || command->parameter(ACTION) == "load"){
                loadGpuData(response, command);
            }

        }
        command = target->findCommand(PWR);
        if(command){
            if(command->parameter(ACTION) == "start"){
                QString fileName;
                if(!makeFileName(command, PWR, fileName)){
                    response.setErrorMessage("File path must be defined for pwr logging!");
                }
                else{
                    mState |= PwrLogging;
                    if(mPwr){
                        delete mPwr;
                        mPwr = 0;
                    }
                    mPwr = openFile(fileName, command);
                }
                //to launch measuring if not running
                mDeviceUtils->pwrDetails();
            }
            else if(command->parameter(ACTION) == "stop" || command->parameter(ACTION) == "load"){
                loadPwrData(response, command);
                if(command->parameter(ACTION) == "stop" ){
                    mDeviceUtils->stopPwrData();
                }
            }

        }
        //determines that does the interval need to be started or stopped
        checkLoggerState();
    }
}