Ejemplo n.º 1
0
ChessPlayer* EngineBuilder::create(QObject* receiver,
				   const char* method,
				   QObject* parent) const
{
	QString path(QDir::currentPath());
	EngineProcess* process = new EngineProcess();

	QString workDir = m_config.workingDirectory();
	QString cmd = m_config.command();

	if (workDir.isEmpty())
	{
		process->setWorkingDirectory(QDir::tempPath());

		QFileInfo cmdInfo(cmd);
		if (cmdInfo.isFile())
			cmd = cmdInfo.absoluteFilePath();
	}
	else
	{
		// Make sure the path to the executable is resolved
		// in the engine's working directory
		if (!QDir::setCurrent(workDir))
		{
			qWarning() << "Invalid working directory:" << workDir;
			delete process;
			return 0;
		}
		process->setWorkingDirectory(QDir::currentPath());
	}

	if (!m_config.arguments().isEmpty())
		process->start(cmd, m_config.arguments());
	else
		process->start(cmd);
	bool ok = process->waitForStarted();

	if (!workDir.isEmpty())
		QDir::setCurrent(path);
	if (!ok)
	{
		qWarning() << "Cannot start engine" << m_config.command();
		delete process;
		return 0;
	}

	ChessEngine* engine = EngineFactory::create(m_config.protocol());
	Q_ASSERT(engine != 0);
	engine->setParent(parent);
	if (receiver != 0 && method != 0)
		QObject::connect(engine, SIGNAL(debugMessage(QString)),
				 receiver, method);
	engine->setDevice(process);
	engine->applyConfiguration(m_config);

	engine->start();
	return engine;
}
Ejemplo n.º 2
0
// Sends an Info command to the server to retrieve source location
// information for the function or class specified by the command
// argument. Then updates this command with the source information
// and sends it to the server in order to retrieve the source
// text from the server.
// Returns false if the server was unable to return the information
// needed for this command.
bool CmdList::listFunctionOrClass(DebuggerClient &client) {
  assert(client.argCount() == 1);
  CmdInfoPtr cmdInfo(new CmdInfo());
  DebuggerCommandPtr deleter(cmdInfo);
  string subsymbol;
  cmdInfo->parseOneArg(client, subsymbol);
  CmdInfoPtr cmd = client.xend<CmdInfo>(cmdInfo.get());
  Array info = cmd->getInfo();
  if (info.empty()) return false;
  always_assert(info.size() == 1);
  ArrayIter iter(info);
  Array funcInfo = iter.second().toArray();
  if (!subsymbol.empty()) {
    String key = CmdInfo::FindSubSymbol(funcInfo[s_methods].toArray(),
                                        subsymbol);
    if (key.isNull()) return false;
    funcInfo = funcInfo[s_methods][key].toArray();
  }
  String file = funcInfo[s_file].toString();
  int line1 = funcInfo[s_line1].toInt32();
  int line2 = funcInfo[s_line2].toInt32();
  int line = line1 ? line1 : line2;
  if (file.empty() || !line) return false;
  client.setListLocation(file.data(), line - 1, false);
  line = 0;
  int charFocus0 = 0;
  int lineFocus1 = 0;
  int charFocus1 = 0;
  m_file.clear();
  m_line1 = m_line2 = 0;
  getListLocation(client, line, charFocus0, lineFocus1, charFocus1);
  if (m_file.empty()) {
    listEvalCode(client);
    return true;
  }
  return listFileRange(client, line, charFocus0, lineFocus1, charFocus1);
}
Ejemplo n.º 3
0
ChessPlayer* EngineBuilder::create(QObject* receiver,
				   const char* method,
				   QObject* parent,
				   QString* error) const
{
	QString workDir = m_config.workingDirectory();
	QString cmd = m_config.command().trimmed();
	QString stderrFile = m_config.stderrFile();

	if (cmd.isEmpty())
	{
		setError(error, tr("Empty engine command"));
		return nullptr;
	}

	if (!EngineFactory::protocols().contains(m_config.protocol()))
	{
		setError(error, tr("Unknown chess protocol: %1")
			 .arg(m_config.protocol()));
		return nullptr;
	}

	EngineProcess* process = new EngineProcess();

	if (workDir.isEmpty())
	{
		process->setWorkingDirectory(QDir::tempPath());

		QFileInfo cmdInfo(cmd);
		if (cmdInfo.isFile())
			cmd = cmdInfo.absoluteFilePath();
	}
	else
		process->setWorkingDirectory(workDir);

	if (!stderrFile.isEmpty())
		process->setStandardErrorFile(stderrFile, QIODevice::Append);

	if (!m_config.arguments().isEmpty())
		process->start(cmd, m_config.arguments());
	else
		process->start(cmd);

	bool ok = process->waitForStarted();
	if (!ok)
	{
		setError(error, tr("Cannot execute command: %1")
			 .arg(m_config.command()));
		delete process;
		return nullptr;
	}

	ChessEngine* engine = EngineFactory::create(m_config.protocol());
	Q_ASSERT(engine != nullptr);

	engine->setParent(parent);
	if (receiver != nullptr && method != nullptr)
		QObject::connect(engine, SIGNAL(debugMessage(QString)),
				 receiver, method);
	engine->setDevice(process);
	engine->applyConfiguration(m_config);

	engine->start();
	return engine;
}
Ejemplo n.º 4
0
int main(int argc, char **argv)
{


    int option = 0;      // For getopt temp option

    usbDevice_t *dev;
    ucp_cmd_t buffer;    //room for dummy report ID
    int         err;

    //Initialize globalArgs before we get to work
    globalArgs.rFlag = 0;   //False
    globalArgs.iFlag = 0;   //False
    globalArgs.wFlag = 0;   //False
    globalArgs.fFlag = 0;   //False
    globalArgs.pFlag = 0;   //False
    globalArgs.hashFlag = 0;   //False
    globalArgs.kFlag = 0;   //False
    strncpy(globalArgs.pin,"0000",sizeof(globalArgs.pin)); //Default pin
    globalArgs.offset = 0; //Default
    globalArgs.size = 512; //Default
    globalArgs.fileName = NULL;
    globalArgs.file = NULL;

    //Check if no arguments at all
    if(argc==1) printUsage(argv[0]);

    //If there's some argument, parse them
    while((option = getopt(argc, argv, "rwif:o:p:Pk:s:h?")) !=-1){
    //Check option flags
    switch(option){
        case 'r':
            globalArgs.rFlag = 1; //True
            break;
        case 'w':
            globalArgs.wFlag = 1; //True
            break;
        case 'i':
            globalArgs.iFlag = 1; //True
            break;
        case 'f':
            globalArgs.fFlag = 1; //True
            globalArgs.fileName = optarg;
            printf("File: %s\n",globalArgs.fileName);
            break;
        case 'o':
            globalArgs.offset = atoi(optarg);
            break;
        case 'p':
            globalArgs.pFlag = 1; //True
            strncpy(globalArgs.pin,optarg, sizeof(globalArgs.pin));
            break;
        case 'P':
            globalArgs.hashFlag=1; //True
            break;
        case 'k':
            globalArgs.kFlag = 1; //True
            globalArgs.keyboard = optarg;
            printf("Keyboard: %s\n", globalArgs.keyboard);
        case 's':
            globalArgs.size = atoi(optarg);
            break;
        case 'h':
        case '?':
            printUsage(argv[0]);
            break;
        //Unknown flag, don't know what to do
        default:
            //After getopt prints the error
            printUsage(argv[0]);
            break;
    }
    }

    //Check that only one action is done at a time
    if(globalArgs.rFlag + globalArgs.wFlag + globalArgs.iFlag > 1) printUsage(argv[0]);
    //Check that not set PIN and read HASH are given at a time
    if(globalArgs.pFlag + globalArgs.hashFlag > 1) printUsage(argv[0]);
    //Check that if write from file to device, we are given a file and a pin to encrypt!
    if(globalArgs.wFlag  && !globalArgs.fFlag) printUsage(argv[0]);
    if(globalArgs.wFlag && !globalArgs.pFlag) printUsage(argv[0]);
    //Also check pin for read flag
    if(globalArgs.rFlag && !globalArgs.pFlag) printUsage(argv[0]);

    //Try to open the device, exit if no device present.
    if((dev = openDevice()) == NULL) exit(1);

    //Clean the buffer before working with it
    memset((void*)&buffer, 0, sizeof(buffer));
    

    //Check and perform the desired commands
    if(globalArgs.rFlag){  //READ COMMAND
        buffer.cmd = UCP_CMD_READ;
        deviceRead.offset = globalArgs.offset;
        deviceRead.size = globalArgs.size;
        memcpy((void*)&buffer.buff, (void*)&deviceRead, sizeof(deviceRead));

        //give some feedback
        fprintf(stderr,"Reading from the MemType: offset=%d bytes, size=%d bytes.\n",deviceRead.offset, deviceRead.size);

    }else if(globalArgs.wFlag){  //WRITE COMMAND
        buffer.cmd = UCP_CMD_WRITE;
        deviceRead.offset = globalArgs.offset;
        deviceRead.size = readXML(globalArgs.fileName);
        memcpy((void*)&buffer.buff, (void*)&deviceRead, sizeof(deviceRead));

        //give some feedback
        fprintf(stderr,"Writing to the MemType: offset=%d bytes, size=%d bytes.\n",deviceRead.offset, deviceRead.size);

    }else if(globalArgs.iFlag){  //INFO COMMAND
        buffer.cmd = UCP_CMD_INFO;
    }else if(globalArgs.hashFlag){ //READ PIN HASH COMMAND
        buffer.cmd = UCP_CMD_READ_PIN;
    }else if(globalArgs.pFlag){ //SET PIN COMMAND
        buffer.cmd = UCP_CMD_SET_PIN;
        buffer.buff[0] = 16; //sizeof hash in bytes
    }else if(globalArgs.kFlag){
        buffer.cmd = UCP_CMD_KEYBOARD;
    }
       
        //Add a dummy report ID and send data to device 
        buffer.reportid = 2;
        if((err = usbhidSetReport(dev, (char*)&buffer, sizeof(buffer))) != 0)
            fprintf(stderr, "Error sending data to device: %s\n", usbErrorMessage(err));

        //Read back report 
        int len = sizeof(buffer);
        if((err = usbhidGetReport(dev, 2, (char*)&buffer, &len)) != 0)
        {   //... if not OK, print error
            fprintf(stderr, "Error reading data from device: %s\n", usbErrorMessage(err));
        }
        else //... if OK, do things :)
        {
            fprintf( stderr, "\nMemType CMD Response: ");
            hexdump( (void*)&buffer.cmd, sizeof(buffer.cmd)+sizeof(buffer.buff));
            fprintf( stderr, "Received data from the device: \n");
            switch(buffer.cmd)
            {
                case UCP_CMD_RESET:
                    fprintf( stderr, "RESET\n");
                    break;
                case UCP_CMD_READ:
                    fprintf( stderr, "READ\n");
                    cmdRead(dev, deviceRead.size);
                    break;
                case UCP_CMD_WRITE:
                    fprintf( stderr, "WRITE\n");
                    cmdWrite(dev, flashMemory, deviceRead.size);
                    fprintf(stderr, "[ENCRYPTION TEST] only hexdump");
                    break;
                case UCP_CMD_SET_PIN:
                    fprintf(stderr, "SET PIN\n");
                    cmdSetPin(dev);
                    break;
                case UCP_CMD_READ_PIN:
                    fprintf(stderr, "READ PIN HASH\n");
                    cmdReadPinHash(dev);
                    break;
                case UCP_CMD_KEYBOARD:
                    fprintf(stderr, "KEYBOARD\n");
                    cmdKeyboard(dev);
                    break;
                case UCP_CMD_DATA:
                    fprintf( stderr, "DATA\n");
                    break;
                case UCP_CMD_INFO:
                    fprintf( stderr, "sizeof(info) -> %lu\n", sizeof(deviceInfo));
                    memcpy((void*)&deviceInfo, (void*)buffer.buff, sizeof(deviceInfo));
                    
                    /* Call info */
                    cmdInfo(&deviceInfo);
                    break;
                case UCP_CMD_ERROR:  //Wait! the device returned one error!
                    switch( (unsigned char) buffer.buff[0] )
                    {
                        case UCP_ERR:
                            fprintf( stderr, "GENERIC ERROR\n");
                            break;
                        case UCP_ERR_PACKET:
                            fprintf( stderr, "PACKET ERROR\n");
                            break;
                        case UCP_ERR_CMD:
                            fprintf( stderr, "CMD ERROR\n");
                            break;
                        case UCP_ERR_ADDR:
                            fprintf( stderr, "ADDR ERROR\n");
                            break;
                        case UCP_ERR_SIZE:
                            fprintf( stderr, "SIZE ERRROR\n");
                            break;
                        case UCP_ERR_PROTOCOL:
                            fprintf( stderr, "PROTOCOL ERROR\n");
                            break;
                        case UCP_ERR_LOCKED:
                            fprintf( stderr, "DEVICE LOCKED ERROR\n");
                            break; 
                        default:
                            fprintf( stderr, "UNKNOWN ERROR\n");
                    }
                    break;
                default:
                    fprintf( stderr, "UNKNOWN CMD ERROR\n");
            }
        }

    usbhidCloseDevice(dev);
    
    return 0;
}