Esempio n. 1
0
    Status WhereMatchExpression::init( const StringData& ns,
                                       const StringData& theCode,
                                       const BSONObj& scope ) {

        if ( ns.size() == 0 )
            return Status( ErrorCodes::BadValue, "ns for $where cannot be empty" );

        if ( theCode.size() == 0 )
            return Status( ErrorCodes::BadValue, "code for $where cannot be empty" );

        _ns = ns.toString();
        _code = theCode.toString();
        _userScope = scope.getOwned();

        NamespaceString nswrapper( _ns );
        _scope = globalScriptEngine->getPooledScope( nswrapper.db().toString(), "where" );
        _func = _scope->createFunction( _code.c_str() );

        if ( !_func )
            return Status( ErrorCodes::BadValue, "$where compile error" );

        return Status::OK();
    }
Esempio n. 2
0
/*
 * Check some of the error conditions
 */
void ArduinoWidgetTest::testErrorConditions() {
  vector<string> packets;

  uint8_t ERROR_CODES[] = {2, 3, 4, 5};
  // test each of the error codes.
  for (unsigned int i = 0; i < sizeof(ERROR_CODES); ++i) {
    // request
    const RDMRequest *request = NewRequest(DESTINATION);
    unsigned int expected_request_frame_size;
    uint8_t *expected_request_frame = PackRDMRequest(
        request,
        &expected_request_frame_size);

    // expected response
    unsigned int response_size;
    uint8_t *response_frame = PackRDMError(
        ERROR_CODES[i],
        &response_size);

    m_endpoint->AddExpectedDataAndReturn(
        expected_request_frame,
        expected_request_frame_size,
        response_frame,
        response_size);

    m_arduino->SendRDMRequest(
        request,
        ola::NewSingleCallback(this,
                               &ArduinoWidgetTest::ValidateStatus,
                               ola::rdm::RDM_FAILED_TO_SEND,
                               packets));
    m_ss.Run();
    m_endpoint->Verify();
    delete[] expected_request_frame;
    delete[] response_frame;
  }
}
Esempio n. 3
0
/*
 * Check that discovery fails for an unknown device.
 */
void RobeWidgetDetectorTest::testUnknownDevice() {
  uint8_t info_data[] = {1, 2, 3, 0, 0};
  uint8_t uid_data[] = {0x52, 0x53, 3, 0, 0, 10};
  m_endpoint->AddExpectedRobeDataAndReturn(
      INFO_REQUEST_LABEL,
      NULL,
      0,
      INFO_RESPONSE_LABEL,
      info_data,
      sizeof(info_data));
  m_endpoint->AddExpectedRobeDataAndReturn(
      UID_REQUEST_LABEL,
      NULL,
      0,
      UID_RESPONSE_LABEL,
      uid_data,
      sizeof(uid_data));

  m_detector->Discover(&m_descriptor);
  m_ss.Run();

  OLA_ASSERT_FALSE(m_found_widget);
  OLA_ASSERT(m_failed_widget);
}
Esempio n. 4
0
void sigHandler( int sigNum ) {
	//Log a message that we caught a signal
	try {
		Logger::info() << "Caught signal " << sigNum << ", shutting down" << endl;
	} catch(...) {
		cerr << "Caught signal " << sigNum << ", shutting down" << endl;
	}
	
	//Kill any sub-threads we have going right now
	for( int i = 0; i < activeThreads.size(); i++ ) {
		if( pthread_cancel(activeThreads[i]) != 0 ) {
			cerr << "Could not pthread_cancel thread " << activeThreads[i] << endl;
		}
	}
	
	//Delete the primary listening socket
	server.release();
	
	//Shutdown the logging mechanism
	Logger::shutdown();
	
	//Exit the program
	exit( 0 );
}
void
FileOperationMetaTest::testFileOperationSubNone(
        auto_ptr<FileOperationInterface> & oper, const fs::path & fileMeta,
        const fs::path & fileEntity,
        const string & tapeName)
{
    string tape;
    char buffer[1024] = "hello,world!\n";
    char bufferRead[sizeof(buffer)];
    size_t size;
    struct stat stat;

    errno = 0;
    CPPUNIT_ASSERT( false == oper->GetStat(stat) );
    CPPUNIT_ASSERT_ERRNO_( ENOENT == errno);
    errno = 0;
    CPPUNIT_ASSERT( false == oper->Read(0,bufferRead,sizeof(bufferRead),size) );
    CPPUNIT_ASSERT_ERRNO_( EBADF == errno);
    errno = 0;
    CPPUNIT_ASSERT( false == oper->Write(0,buffer,sizeof(buffer),size) );
    CPPUNIT_ASSERT_ERRNO_( EBADF == errno);
    CPPUNIT_ASSERT( ! fs::exists(fileMeta) );
    CPPUNIT_ASSERT( ! fs::exists(fileEntity) );

    errno = 0;
    CPPUNIT_ASSERT( false == oper->OpenFile(O_RDWR) );
    CPPUNIT_ASSERT_ERRNO_( ENOENT == errno);
    CPPUNIT_ASSERT_MESSAGE( fileEntity.file_string(),
            true == oper->CreateFile(O_RDWR,0644,false) );
    CPPUNIT_ASSERT( fs::exists(fileMeta) );
    CPPUNIT_ASSERT( fs::exists(fileEntity) );
    CPPUNIT_ASSERT( true == oper->Delete() );
    CPPUNIT_ASSERT( ! fs::exists(fileMeta) );
    CPPUNIT_ASSERT( ! fs::exists(fileEntity) );

    CPPUNIT_ASSERT( true == oper->GetTape(tape) );
    CPPUNIT_ASSERT( tape == tapeName );
}
Esempio n. 6
0
bool WhereMatchExpression::matches(const MatchableDocument* doc, MatchDetails* details) const {
    verify(_func);
    BSONObj obj = doc->toBSON();

    if (!_userScope.isEmpty()) {
        _scope->init(&_userScope);
    }

    _scope->setObject("obj", const_cast<BSONObj&>(obj));
    _scope->setBoolean("fullObject", true);  // this is a hack b/c fullObject used to be relevant

    int err = _scope->invoke(_func, 0, &obj, 1000 * 60, false);
    if (err == -3) {  // INVOKE_ERROR
        stringstream ss;
        ss << "error on invocation of $where function:\n" << _scope->getError();
        uassert(16812, ss.str(), false);
    } else if (err != 0) {  // ! INVOKE_SUCCESS
        uassert(16813, "unknown error in invocation of $where function", false);
    }

    return _scope->getBoolean("__returnValue") != 0;
}
/*
 * Check some of the error conditions
 */
void DmxterWidgetTest::testErrorConditions() {
  uint8_t RDM_REQUEST_LABEL = 0x80;
  UID source(1, 2);
  UID destination(3, 4);
  UID new_source(0x5253, 0x12345678);
  vector<string> packets;

  const RDMRequest *request = NewRequest(source, destination, NULL, 0);

  unsigned int size = request->Size();
  uint8_t *expected_packet = new uint8_t[size + 1];
  expected_packet[0] = 0xcc;
  CPPUNIT_ASSERT(request->PackWithControllerParams(
        expected_packet + 1,
        &size,
        new_source,
        0,
        1));

  // to small to be valid
  uint8_t return_packet[] = {0x00};

  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_INVALID_RESPONSE,
                             packets));

  m_ss.Run();
  m_endpoint->Verify();

  // check mismatched version
  request = NewRequest(source, destination, NULL, 0);

  CPPUNIT_ASSERT(request->PackWithControllerParams(
        expected_packet + 1,
        &size,
        new_source,
        1,  // increment transaction #
        1));

  // non-0 version
  uint8_t return_packet2[] = {0x01, 0x11, 0xcc};

  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet2,
      sizeof(return_packet2));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_INVALID_RESPONSE,
                             packets));

  delete[] expected_packet;
  m_ss.Run();
  m_endpoint->Verify();
}
/**
 * Check that we send RDM discovery messages correctly.
 */
void DmxterWidgetTest::testSendRDMDUB() {
  uint8_t RDM_DUB_LABEL = 0x83;
  const UID source(0x5253, 0x12345678);
  const UID destination = UID::AllDevices();

  static const uint8_t REQUEST_DATA[] = {
    0x7a, 0x70, 0, 0, 0, 0,
    0x7a, 0x70, 0xff, 0xff, 0xff, 0xff
  };

  // request
  const RDMRequest *rdm_request = new ola::rdm::RDMDiscoveryRequest(
      source,
      destination,
      0,  // transaction #
      1,  // port id
      0,  // message count
      0,  // sub device
      ola::rdm::PID_DISC_UNIQUE_BRANCH,  // param id
      REQUEST_DATA,
      sizeof(REQUEST_DATA));

  unsigned int request_size = rdm_request->Size();
  uint8_t *expected_request_frame = new uint8_t[request_size + 1];
  expected_request_frame[0] = 0xcc;
  CPPUNIT_ASSERT(rdm_request->Pack(expected_request_frame + 1, &request_size));

  // a 4 byte response means a timeout
  static const uint8_t TIMEOUT_RESPONSE[] = {0, 17};

  // add the expected response, send and verify
  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_DUB_LABEL,
      expected_request_frame,
      request_size + 1,
      RDM_DUB_LABEL,
      TIMEOUT_RESPONSE,
      sizeof(TIMEOUT_RESPONSE));

  vector<string> packets;
  m_widget->SendRDMRequest(
      rdm_request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_TIMEOUT,
                             packets));
  m_ss.Run();
  m_endpoint->Verify();

  delete[] expected_request_frame;

  // now try a dub response that returns something
  rdm_request = new ola::rdm::RDMDiscoveryRequest(
      source,
      destination,
      1,  // transaction #
      1,  // port id
      0,  // message count
      0,  // sub device
      ola::rdm::PID_DISC_UNIQUE_BRANCH,  // param id
      REQUEST_DATA,
      sizeof(REQUEST_DATA));

  request_size = rdm_request->Size();
  expected_request_frame = new uint8_t[request_size + 1];
  expected_request_frame[0] = 0xcc;
  CPPUNIT_ASSERT(rdm_request->Pack(expected_request_frame + 1, &request_size));

  // something that looks like a DUB response
  static const uint8_t FAKE_RESPONSE[] = {0x00, 19, 0xfe, 0xfe, 0xaa, 0xaa};

  // add the expected response, send and verify
  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_DUB_LABEL,
      expected_request_frame,
      request_size + 1,
      RDM_DUB_LABEL,
      FAKE_RESPONSE,
      sizeof(FAKE_RESPONSE));

  packets.push_back(
      string(reinterpret_cast<const char*>(&FAKE_RESPONSE[2]),
             sizeof(FAKE_RESPONSE) - 2));
  m_widget->SendRDMRequest(
      rdm_request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_DUB_RESPONSE,
                             packets));
  m_ss.Run();
  m_endpoint->Verify();

  delete[] expected_request_frame;
}
Esempio n. 9
0
	/// Adds a suite to this suite. Tests in added suites will be executed
	/// when run() of the top-level suite is called.
	///
	/// \param suite %Test suite to add.
	///
	void
	Suite::add(auto_ptr<Suite> suite)
	{
		_suites.push_back(suite.release());
	}
Esempio n. 10
0
/**
 * Receive data and terminate if the stream is bad.
 */
void TCPTransportTest::Receive() {
  m_stream_ok = m_transport->Receive();
  if (!m_stream_ok)
    m_ss->Terminate();
}
Esempio n. 11
0
void TCPTransportTest::tearDown() {
  // Close the loopback descriptor and drain the ss
  m_loopback.Close();
  m_ss->RunOnce(0, 0);
}
Esempio n. 12
0
	auto_ptr(auto_ptr<U>& rhs) : pointee(rhs.release())
	{
		NULL;
	}
Esempio n. 13
0
/**
 * Check that we send RDM discovery messages correctly.
 */
void DmxterWidgetTest::testSendRDMDUB() {
  uint8_t RDM_DUB_LABEL = 0x83;
  const UID source(0x4744, 0x12345678);
  const UID destination = UID::AllDevices();

  static const uint8_t REQUEST_DATA[] = {
    0x7a, 0x70, 0, 0, 0, 0,
    0x7a, 0x70, 0xff, 0xff, 0xff, 0xff
  };

  // request
  RDMRequest *rdm_request = new ola::rdm::RDMDiscoveryRequest(
      source,
      destination,
      0,  // transaction #
      1,  // port id
      0,  // sub device
      ola::rdm::PID_DISC_UNIQUE_BRANCH,  // param id
      REQUEST_DATA,
      sizeof(REQUEST_DATA));

  unsigned int request_size = RDMCommandSerializer::RequiredSize(*rdm_request);
  uint8_t *expected_request_frame = new uint8_t[request_size + 1];
  expected_request_frame[0] = 0xcc;
  OLA_ASSERT(RDMCommandSerializer::Pack(*rdm_request,
                                        expected_request_frame + 1,
                                        &request_size));

  // a 4 byte response means a timeout
  static const uint8_t TIMEOUT_RESPONSE[] = {0, 17};

  // add the expected response, send and verify
  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_DUB_LABEL,
      expected_request_frame,
      request_size + 1,
      RDM_DUB_LABEL,
      TIMEOUT_RESPONSE,
      sizeof(TIMEOUT_RESPONSE));

  RDMFrames frames;
  m_widget->SendRDMRequest(
      rdm_request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_TIMEOUT,
                             frames));
  m_ss.Run();
  m_endpoint->Verify();

  delete[] expected_request_frame;

  // now try a dub response that returns something
  rdm_request = new ola::rdm::RDMDiscoveryRequest(
      source,
      destination,
      1,  // transaction #
      1,  // port id
      0,  // sub device
      ola::rdm::PID_DISC_UNIQUE_BRANCH,  // param id
      REQUEST_DATA,
      sizeof(REQUEST_DATA));

  request_size = RDMCommandSerializer::RequiredSize(*rdm_request);
  expected_request_frame = new uint8_t[request_size + 1];
  expected_request_frame[0] = 0xcc;
  OLA_ASSERT(RDMCommandSerializer::Pack(*rdm_request,
                                        expected_request_frame + 1,
                                        &request_size));

  // something that looks like a DUB response
  static const uint8_t FAKE_RESPONSE[] = {0x00, 19, 0xfe, 0xfe, 0xaa, 0xaa};

  // add the expected response, send and verify
  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_DUB_LABEL,
      expected_request_frame,
      request_size + 1,
      RDM_DUB_LABEL,
      FAKE_RESPONSE,
      sizeof(FAKE_RESPONSE));

  frames.push_back(RDMFrame(&FAKE_RESPONSE[2], arraysize(FAKE_RESPONSE) - 2));
  m_widget->SendRDMRequest(
      rdm_request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_DUB_RESPONSE,
                             frames));
  m_ss.Run();
  m_endpoint->Verify();

  delete[] expected_request_frame;
}
Esempio n. 14
0
/**
 * Check that we send messages correctly.
 */
void DmxterWidgetTest::testSendRDMRequest() {
  uint8_t RDM_REQUEST_LABEL = 0x80;
  uint8_t RDM_BROADCAST_REQUEST_LABEL = 0x81;
  UID source(0x4744, 0x12345678);
  UID destination(3, 4);
  UID bcast_destination(3, 0xffffffff);

  RDMRequest *request = NewRequest(source, destination, NULL, 0);

  unsigned int size = RDMCommandSerializer::RequiredSize(*request);
  uint8_t *expected_packet = new uint8_t[size + 1];
  expected_packet[0] = 0xcc;
  OLA_ASSERT(RDMCommandSerializer::Pack(*request, expected_packet + 1, &size));

  uint8_t return_packet[] = {
    0x00, 14,  // response code 'ok'
    0xcc,
    1, 28,  // sub code & length
    0x47, 0x44, 0x12, 0x34, 0x56, 0x78,   // dst uid
    0, 3, 0, 0, 0, 4,   // src uid
    0, 1, 0, 0, 10,  // transaction, port id, msg count & sub device
    0x21, 0x1, 0x28, 4,  // command, param id, param data length
    0x5a, 0x5a, 0x5a, 0x5a,  // param data
    0x04, 0x50  // checksum
  };

  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this, &DmxterWidgetTest::ValidateResponse));
  m_ss.Run();
  m_endpoint->Verify();

  // now check broadcast
  request = NewRequest(source, bcast_destination, NULL, 0);
  request->SetTransactionNumber(1);
  OLA_ASSERT(RDMCommandSerializer::Pack(*request, expected_packet + 1, &size));

  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_BROADCAST_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_BROADCAST_REQUEST_LABEL,
      static_cast<uint8_t*>(NULL),
      0);

  RDMFrames frames;
  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_WAS_BROADCAST,
                             frames));

  delete[] expected_packet;
  m_ss.Run();
  m_endpoint->Verify();
}
Esempio n. 15
0
void signal_handler(int inSignal)
{
	gLockFile.reset(NULL);
}
void InputWidgetProxyCollection::insert(
    const string&               key,
    auto_ptr<IInputWidgetProxy> proxy)
{
    m_proxies[key] = proxy.release();
}
Esempio n. 17
0
namespace broker {

auto_ptr<QpiddOptions> options;

// Broker real entry; various system-invoked entrypoints call here.
int run_broker(int argc, char *argv[], bool hidden)
{
    try
    {
        BootstrapOptions bootOptions(argv[0]);
        string           defaultPath (bootOptions.module.loadDir);
        // Parse only the common, load, and log options to see which
        // modules need to be loaded.  Once the modules are loaded,
        // the command line will be re-parsed with all of the
        // module-supplied options.
        try {
            bootOptions.parse (argc, argv, bootOptions.common.config, true);
            if (hidden)
                bootOptions.log.sinkOptions->detached();
            qpid::log::Logger::instance().configure(bootOptions.log);
        } catch (const std::exception& e) {
            // Couldn't configure logging so write the message direct to stderr.
            cerr << "Unexpected error: " << e.what() << endl;
            return 1;
        }

        for (vector<string>::iterator iter = bootOptions.module.load.begin();
             iter != bootOptions.module.load.end();
             iter++)
            qpid::tryShlib (iter->data(), false);

        if (!bootOptions.module.noLoad) {
            bool isDefault = defaultPath == bootOptions.module.loadDir;
            qpid::loadModuleDir (bootOptions.module.loadDir, isDefault);
        }

        // Parse options
        options.reset(new QpiddOptions(argv[0]));
        options->parse(argc, argv, options->common.config);

        // Options that just print information.
        if (options->common.help || options->common.version) {
            if (options->common.version) 
                cout << "qpidd (" << qpid::product << ") version "
                     << qpid::version << endl;
            else if (options->common.help)
                options->usage();
            return 0;
        }

        // Everything else is driven by the platform-specific broker
        // logic.
        QpiddBroker broker;
        return broker.execute(options.get());
    }
    catch(const exception& e) {
        QPID_LOG(critical, "Unexpected error: " << e.what());
    }
    return 1;
}
}}
Esempio n. 18
0
/**
 * Check that we handle invalid responses ok
 */
void DmxterWidgetTest::testErrorCodes() {
  uint8_t RDM_REQUEST_LABEL = 0x80;
  UID source(0x4744, 0x12345678);
  UID destination(3, 4);

  RDMFrames frames;

  RDMRequest *request = NewRequest(source, destination, NULL, 0);

  unsigned int size = RDMCommandSerializer::RequiredSize(*request);
  uint8_t *expected_packet = new uint8_t[size + 1];
  expected_packet[0] = 0xcc;
  OLA_ASSERT(RDMCommandSerializer::Pack(*request, expected_packet + 1, &size));

  uint8_t return_packet[] = {
    0x00, 1,  // checksum failure
  };

  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_CHECKSUM_INCORRECT,
                             frames));
  m_ss.Run();
  m_endpoint->Verify();

  return_packet[1] = 8;  // packet too short
  // Update TN & Checksum
  expected_packet[15]++;
  expected_packet[25]++;
  request = NewRequest(source, destination, NULL, 0);
  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_PACKET_TOO_SHORT,
                             frames));
  m_ss.Run();
  m_endpoint->Verify();

  // update transaction # & checksum
  expected_packet[15]++;
  expected_packet[25] = 0xe1;
  return_packet[1] = 12;  // transaction mismatch
  request = NewRequest(source, destination, NULL, 0);
  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_TRANSACTION_MISMATCH,
                             frames));
  m_ss.Run();
  m_endpoint->Verify();

  // update transaction # & checksum
  expected_packet[15]++;
  expected_packet[25] = 0xe2;
  return_packet[1] = 17;  // timeout
  request = NewRequest(source, destination, NULL, 0);
  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_TIMEOUT,
                             frames));
  m_ss.Run();
  m_endpoint->Verify();

  // update transaction # & checksum
  expected_packet[15]++;
  expected_packet[25] = 0xe3;
  return_packet[1] = 41;  // device mismatch
  request = NewRequest(source, destination, NULL, 0);
  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_SRC_UID_MISMATCH,
                             frames));
  m_ss.Run();
  m_endpoint->Verify();

  // update transaction # & checksum
  expected_packet[15]++;
  expected_packet[25] = 0xe4;
  return_packet[1] = 42;  // sub device mismatch
  request = NewRequest(source, destination, NULL, 0);
  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_SUB_DEVICE_MISMATCH,
                             frames));
  m_ss.Run();
  m_endpoint->Verify();

  delete[] expected_packet;
}
Esempio n. 19
0
namespace IBS
{
	// using namespace
	using namespace std;
	using namespace CoreArray;
	using namespace CoreArray::Vectorization;
	using namespace GWAS;


	/// Packed size
	static const long _SIZE_ = 256*256;

	/// IBS
	/// The number of IBS 0 in the packed genotype
	UInt8 IBS0_Num_SNP[_SIZE_];
	/// The number of IBS 1 in the packed genotype
	UInt8 IBS1_Num_SNP[_SIZE_];
	/// The number of IBS 2 in the packed genotype
	UInt8 IBS2_Num_SNP[_SIZE_];

	/// Genetic Distance
	/// The distance in the packed genotype
	UInt8 Gen_Dist_SNP[_SIZE_];
	/// The flag of use of allele frequencies
	UInt8 Gen_Both_Valid[_SIZE_];

	/// KING robust estimator
	/// The square value of genotype difference, (X_m^{(i)} - X_m^{(j)})^2
	UInt8 Gen_KING_SqDiff[_SIZE_];
	/// N1_Aa requiring both genotypes are available
	UInt8 Gen_KING_N1_Aa[_SIZE_];
	/// N2_Aa requiring both genotypes are available
	UInt8 Gen_KING_N2_Aa[_SIZE_];
	/// the number of valid loci
	UInt8 Gen_KING_Num_Loci[_SIZE_];


	/// The packed genotype buffer
	auto_ptr<UInt8> GenoPacked;
	/// The allele frequencies
	auto_ptr<double> GenoAlleleFreq;


	/// Thread variables
	const int N_MAX_THREAD = 256;
	// PLINK -- IBS
	IdMatTriD PLINKIBS_Thread_MatIdx[N_MAX_THREAD];
	Int64 PLINKIBS_Thread_MatCnt[N_MAX_THREAD];

	// IBS, KING IBD, Individual Similarity
	IdMatTri IBS_Thread_MatIdx[N_MAX_THREAD];
	Int64 IBS_Thread_MatCnt[N_MAX_THREAD];



	/// The pointer to the variable 'PublicIBS' in the function "DoIBSCalculate"
	/// The structure of IBS states
	struct TIBS_Flag
	{
		UInt32 IBS0;  //< the number of loci sharing no allele
		UInt32 IBS1;  //< the number of loci sharing only one allele
		UInt32 IBS2;  //< the number of loci sharing two alleles
		TIBS_Flag() { IBS0 = IBS1 = IBS2 = 0; }
	};


	/// The pointer to the variable 'PublicKING' in the function "DoKINGCalculate"
	/// The structure of KING IBD estimator
	struct TKINGHomoFlag
	{
		UInt32 IBS0;       //< the number of loci sharing no allele
		UInt32 SumSq;      //< \sum_m (X_m^{(i)} - X_m^{(j)})^2
		double SumAFreq;   //< \sum_m p_m (1 - p_m)
		double SumAFreq2;  //< \sum_m p_m^2 (1 - p_m)^2
		TKINGHomoFlag() { IBS0 = SumSq = 0; SumAFreq = SumAFreq2 = 0; }
	};

	struct TKINGRobustFlag
	{
		UInt32 IBS0;       //< the number of loci sharing no allele
		UInt32 nLoci;      //< the total number of loci
		UInt32 SumSq;      //< \sum_m (X_m^{(i)} - X_m^{(j)})^2
		UInt32 N1_Aa;      //< the number of hetet loci for the first individual
		UInt32 N2_Aa;      //< the number of hetet loci for the second individual
		TKINGRobustFlag() { IBS0 = nLoci = SumSq = N1_Aa = N2_Aa = 0; }
	};


	/// The pointer to the variable 'PublicDist' in the function "DoDistCalculate"
	/// The structure of genetic distance
	struct TDissflag
	{
		Int64 SumGeno;
		double SumAFreq;
		TDissflag() { SumGeno = 0; SumAFreq = 0; }
	};



	// TInit object
	class TInit
	{
	public:
		TInit()
		{
			#define PACKED_COND(cond, var, op)	\
				for (int s=0; s < _SIZE_; s++)	\
				{	\
					int g1 = s/256, g2 = s%256;	\
					int sum = 0;	\
					for (int i=0; i < 4; i++)	\
					{	\
						int b1 = g1 & 0x03, b2 = g2 & 0x03;	\
						if (cond) op;	\
						g1 >>= 2; g2 >>= 2;	\
					}	\
					var[s] = sum;	\
				}

			/// The number of IBS 0 in the packed genotype
			PACKED_COND((b1 < 3) && (b2 < 3) && (abs(b1-b2)==2), IBS0_Num_SNP, sum++);
			/// The number of IBS 1 in the packed genotype
			PACKED_COND((b1 < 3) && (b2 < 3) && (abs(b1-b2)==1), IBS1_Num_SNP, sum++);
			/// The number of IBS 2 in the packed genotype
			PACKED_COND((b1 < 3) && (b2 < 3) && (abs(b1-b2)==0), IBS2_Num_SNP, sum++);
			
			/// The distance in the packed genotype
			PACKED_COND((b1 < 3) && (b2 < 3), Gen_Dist_SNP, sum += b1*(2-b2) + (2-b1)*b2);
			PACKED_COND((b1 < 3) && (b2 < 3), Gen_Both_Valid, sum |= (1 << i));

			/// \sum_m (X_m^{(i)} - X_m^{(j)})^2
			PACKED_COND((b1 < 3) && (b2 < 3), Gen_KING_SqDiff, sum += (b1-b2)*(b1-b2));
			PACKED_COND((b1 < 3) && (b2 < 3), Gen_KING_N1_Aa, sum += (b1==1) ? 1:0);
			PACKED_COND((b1 < 3) && (b2 < 3), Gen_KING_N2_Aa, sum += (b2==1) ? 1:0);
			PACKED_COND((b1 < 3) && (b2 < 3), Gen_KING_Num_Loci, sum ++);
		}
	} InitObj;


	/// detect the effective value for BlockSNP
	void AutoDetectSNPBlockSize(int nSamp, bool Detect=true)
	{
		if (Detect)
		{
			long L2Cache = conf_GetL2CacheMemory();
			if (L2Cache <= 0) L2Cache = 1024*1024; // 1M
			BlockSNP = (L2Cache - 3*256*256 - 8*1024) / nSamp * 4;
		}
		BlockSNP = (BlockSNP / 4) * 4;
		if (BlockSNP < 16) BlockSNP = 16;
	}

	/// Convert the raw genotypes
	static void _Do_IBS_ReadBlock(UInt8 *GenoBuf, long Start, long SNP_Cnt, void* Param)
	{
		// initialize
		const int nSamp = MCWorkingGeno.Space.SampleNum();
		UInt8 *pG = GenoBuf;
		UInt8 *pPack = GenoPacked.get();

		// pack genotypes
		for (long iSamp=0; iSamp < nSamp; iSamp++)
		{
			pPack = PackGenotypes(pG, SNP_Cnt, pPack);
			pG += SNP_Cnt;
		}
	}

	/// Compute the pairwise IBS matrix for PLINK
	static void _Do_PLINKIBS_Compute(int ThreadIndex, long Start, long SNP_Cnt, void* Param)
	{
		long Cnt = PLINKIBS_Thread_MatCnt[ThreadIndex];
		IdMatTriD I = PLINKIBS_Thread_MatIdx[ThreadIndex];
		TIBS_Flag *p = ((TIBS_Flag*)Param) + I.Offset();
		long _PackSNPLen = (SNP_Cnt / 4) + (SNP_Cnt % 4 ? 1 : 0);

		for (; Cnt > 0; Cnt--, ++I, p++)
		{
			UInt8 *p1 = GenoPacked.get() + I.Row()*_PackSNPLen;
			UInt8 *p2 = GenoPacked.get() + I.Column()*_PackSNPLen;
			for (long k=_PackSNPLen; k > 0; k--, p1++, p2++)
			{
				size_t t = (size_t(*p1) << 8) | (*p2);
				p->IBS0 += IBS0_Num_SNP[t];
				p->IBS1 += IBS1_Num_SNP[t];
				p->IBS2 += IBS2_Num_SNP[t];
			}
		}
	}

	/// Compute the pairwise IBS matrix
	static void _Do_IBS_Compute(int ThreadIndex, long Start, long SNP_Cnt, void* Param)
	{
		long Cnt = IBS_Thread_MatCnt[ThreadIndex];
		IdMatTri I = IBS_Thread_MatIdx[ThreadIndex];
		TIBS_Flag *p = ((TIBS_Flag*)Param) + I.Offset();
		long _PackSNPLen = (SNP_Cnt / 4) + (SNP_Cnt % 4 ? 1 : 0);

		for (; Cnt > 0; Cnt--, ++I, p++)
		{
			UInt8 *p1 = GenoPacked.get() + I.Row()*_PackSNPLen;
			UInt8 *p2 = GenoPacked.get() + I.Column()*_PackSNPLen;
			for (long k=_PackSNPLen; k > 0; k--, p1++, p2++)
			{
				size_t t = (size_t(*p1) << 8) | (*p2);
				p->IBS0 += IBS0_Num_SNP[t];
				p->IBS1 += IBS1_Num_SNP[t];
				p->IBS2 += IBS2_Num_SNP[t];
			}
		}
	}

	/// Calculate the IBS matrix for PLINK
	void DoPLINKIBSCalculate(CdMatTriDiag<TIBS_Flag> &PublicIBS, int NumThread,
		const char *Info, bool verbose)
	{
		// Initialize ...
		GenoPacked.reset(new UInt8[BlockSNP * PublicIBS.N()]);
		memset(PublicIBS.get(), 0, sizeof(TIBS_Flag)*PublicIBS.Size());

		MCWorkingGeno.Progress.Info = Info;
		MCWorkingGeno.Progress.Show() = verbose;
		MCWorkingGeno.InitParam(true, true, BlockSNP);

		MCWorkingGeno.SplitJobs(NumThread, PublicIBS.N(), PLINKIBS_Thread_MatIdx, PLINKIBS_Thread_MatCnt);
		MCWorkingGeno.Run(NumThread, &_Do_IBS_ReadBlock, &_Do_PLINKIBS_Compute, PublicIBS.get());
	}

	/// Calculate the IBS matrix
	void DoIBSCalculate(CdMatTri<TIBS_Flag> &PublicIBS, int NumThread,
		const char *Info, bool verbose)
	{
		// Initialize ...
		GenoPacked.reset(new UInt8[BlockSNP * PublicIBS.N()]);
		memset(PublicIBS.get(), 0, sizeof(TIBS_Flag)*PublicIBS.Size());

		MCWorkingGeno.Progress.Info = Info;
		MCWorkingGeno.Progress.Show() = verbose;
		MCWorkingGeno.InitParam(true, true, BlockSNP);

		MCWorkingGeno.SplitJobs(NumThread, PublicIBS.N(), IBS_Thread_MatIdx, IBS_Thread_MatCnt);
		MCWorkingGeno.Run(NumThread, &_Do_IBS_ReadBlock, &_Do_IBS_Compute, PublicIBS.get());
	}


	/// *********************************************************************************
	/// **  KING robust estimator  **
	/// *********************************************************************************

	/// Convert the raw genotypes
	static void _Do_KING_ReadBlock(UInt8 *GenoBuf, long Start, long SNP_Cnt, void* Param)
	{
		// initialize
		const int nSamp = MCWorkingGeno.Space.SampleNum();
		UInt8 *pG = GenoBuf;
		UInt8 *pPack = GenoPacked.get();

		// pack genotypes
		for (long iSamp=0; iSamp < nSamp; iSamp++)
		{
			pPack = PackGenotypes(pG, SNP_Cnt, pPack);
			pG += SNP_Cnt;
		}
		// calculate the allele frequencies
		for (long iSNP=0; iSNP < SNP_Cnt; iSNP++)
		{
			UInt8 *p = GenoBuf + iSNP;
			double &Freq = GenoAlleleFreq.get()[iSNP];
			int n = 0; Freq = 0;
			for (long iSamp=0; iSamp < nSamp; iSamp++)
			{
				if (*p < 3) { Freq += *p; n += 2; }
				p += SNP_Cnt;
			}
			Freq = (n > 0) ? Freq/n : 0;
			Freq = Freq * (1 - Freq);
		}
	}

	/// Compute IBD estimator in KING-h**o
	static void _Do_KING_Homo_Compute(int ThreadIndex, long Start, long SNP_Cnt, void* Param)
	{
		long Cnt = IBS_Thread_MatCnt[ThreadIndex];
		IdMatTri I = IBS_Thread_MatIdx[ThreadIndex];
		TKINGHomoFlag *p = ((TKINGHomoFlag*)Param) + I.Offset();
		long _PackSNPLen = (SNP_Cnt / 4) + (SNP_Cnt % 4 ? 1 : 0);

		for (; Cnt > 0; Cnt--, ++I, p++)
		{
			UInt8 *p1 = GenoPacked.get() + I.Row()*_PackSNPLen;
			UInt8 *p2 = GenoPacked.get() + I.Column()*_PackSNPLen;
			for (long k=0; k < _PackSNPLen; k++, p1++, p2++)
			{
				size_t t = (size_t(*p1) << 8) | (*p2);

				p->IBS0 += IBS0_Num_SNP[t];
				p->SumSq += Gen_KING_SqDiff[t];

				UInt8 flag = Gen_Both_Valid[t];
				if (flag & 0x01)
				{
					double f = GenoAlleleFreq.get()[4*k + 0];
					p->SumAFreq += f; p->SumAFreq2 += f*f;
				}
				if (flag & 0x02)
				{
					double f = GenoAlleleFreq.get()[4*k + 1];
					p->SumAFreq += f; p->SumAFreq2 += f*f;
				}
				if (flag & 0x04)
				{
					double f = GenoAlleleFreq.get()[4*k + 2];
					p->SumAFreq += f; p->SumAFreq2 += f*f;
				}
				if (flag & 0x08)
				{
					double f = GenoAlleleFreq.get()[4*k + 3];
					p->SumAFreq += f; p->SumAFreq2 += f*f;
				}
			}
		}
	}

	/// Compute IBD estimator in KING-robust
	static void _Do_KING_Robust_Compute(int ThreadIndex, long Start, long SNP_Cnt, void* Param)
	{
		long Cnt = IBS_Thread_MatCnt[ThreadIndex];
		IdMatTri I = IBS_Thread_MatIdx[ThreadIndex];
		TKINGRobustFlag *p = ((TKINGRobustFlag*)Param) + I.Offset();
		long _PackSNPLen = (SNP_Cnt / 4) + (SNP_Cnt % 4 ? 1 : 0);

		for (; Cnt > 0; Cnt--, ++I, p++)
		{
			UInt8 *p1 = GenoPacked.get() + I.Row()*_PackSNPLen;
			UInt8 *p2 = GenoPacked.get() + I.Column()*_PackSNPLen;
			for (long k=0; k < _PackSNPLen; k++, p1++, p2++)
			{
				size_t t = (size_t(*p1) << 8) | (*p2);
				p->IBS0 += IBS0_Num_SNP[t];
				p->nLoci += Gen_KING_Num_Loci[t];
				p->SumSq += Gen_KING_SqDiff[t];
				p->N1_Aa += Gen_KING_N1_Aa[t];
				p->N2_Aa += Gen_KING_N2_Aa[t];
			}
		}
	}

	/// Calculate KING IBD estimators
	void DoKINGCalculate(CdMatTri<TKINGHomoFlag> &PublicKING, int NumThread,
		const char *Info, bool verbose)
	{
		// Initialize ...
		GenoPacked.reset(new UInt8[BlockSNP * PublicKING.N()]);
		memset(PublicKING.get(), 0, sizeof(TKINGHomoFlag)*PublicKING.Size());
		GenoAlleleFreq.reset(new double[BlockSNP]);

		MCWorkingGeno.Progress.Info = Info;
		MCWorkingGeno.Progress.Show() = verbose;
		MCWorkingGeno.InitParam(true, true, BlockSNP);

		MCWorkingGeno.SplitJobs(NumThread, PublicKING.N(), IBS_Thread_MatIdx, IBS_Thread_MatCnt);
		MCWorkingGeno.Run(NumThread, &_Do_KING_ReadBlock, &_Do_KING_Homo_Compute, PublicKING.get());
	}

	/// Calculate KING IBD estimators
	void DoKINGCalculate(CdMatTri<TKINGRobustFlag> &PublicKING, int NumThread,
		const char *Info, bool verbose)
	{
		// Initialize ...
		GenoPacked.reset(new UInt8[BlockSNP * PublicKING.N()]);
		memset(PublicKING.get(), 0, sizeof(TKINGRobustFlag)*PublicKING.Size());
		GenoAlleleFreq.reset(new double[BlockSNP]);

		MCWorkingGeno.Progress.Info = Info;
		MCWorkingGeno.Progress.Show() = verbose;
		MCWorkingGeno.InitParam(true, true, BlockSNP);

		MCWorkingGeno.SplitJobs(NumThread, PublicKING.N(), IBS_Thread_MatIdx, IBS_Thread_MatCnt);
		MCWorkingGeno.Run(NumThread, &_Do_KING_ReadBlock, &_Do_KING_Robust_Compute, PublicKING.get());
	}



	/// *********************************************************************************
	/// **  Individual Dissimilarity  **
	/// *********************************************************************************

	/// Convert the raw genotypes
	static void _Do_Diss_ReadBlock(UInt8 *GenoBuf, long Start, long SNP_Cnt, void* Param)
	{
		// initialize
		const int nSamp = MCWorkingGeno.Space.SampleNum();
		UInt8 *pG = GenoBuf;
		UInt8 *pPack = GenoPacked.get();

		// pack genotypes
		for (long iSamp=0; iSamp < nSamp; iSamp++)
		{
			pPack = PackGenotypes(pG, SNP_Cnt, pPack);
			pG += SNP_Cnt;
		}
		// calculate the allele frequencies
		for (long iSNP=0; iSNP < SNP_Cnt; iSNP++)
		{
			UInt8 *p = GenoBuf + iSNP;
			double &Freq = GenoAlleleFreq.get()[iSNP];
			int n = 0; Freq = 0;
			for (long iSamp=0; iSamp < nSamp; iSamp++)
			{
				if (*p < 3) { Freq += *p; n += 2; }
				p += SNP_Cnt;
			}
			Freq = (n > 0) ? Freq/n : 0;
			Freq = 8 * Freq * (1 - Freq);
		}
	}

	/// Compute the covariate matrix
	static void _Do_Diss_Compute(int ThreadIndex, long Start, long SNP_Cnt, void* Param)
	{
		long Cnt = IBS_Thread_MatCnt[ThreadIndex];
		IdMatTri I = IBS_Thread_MatIdx[ThreadIndex];
		TDissflag *p = ((TDissflag*)Param) + I.Offset();
		long _PackSNPLen = (SNP_Cnt / 4) + (SNP_Cnt % 4 ? 1 : 0);

		for (; Cnt > 0; Cnt--, ++I, p++)
		{
			UInt8 *p1 = GenoPacked.get() + I.Row()*_PackSNPLen;
			UInt8 *p2 = GenoPacked.get() + I.Column()*_PackSNPLen;
			for (long k=0; k < _PackSNPLen; k++, p1++, p2++)
			{
				size_t t = (size_t(*p1) << 8) | (*p2);
				p->SumGeno += Gen_Dist_SNP[t];

				UInt8 flag = Gen_Both_Valid[t];
				if (flag & 0x01) p->SumAFreq += GenoAlleleFreq.get()[4*k];
				if (flag & 0x02) p->SumAFreq += GenoAlleleFreq.get()[4*k+1];
				if (flag & 0x04) p->SumAFreq += GenoAlleleFreq.get()[4*k+2];
				if (flag & 0x08) p->SumAFreq += GenoAlleleFreq.get()[4*k+3];
			}
		}
	}

	/// Calculate the genetic distance matrix
	void DoDissCalculate(CdMatTri<TDissflag> &PublicDist, int NumThread,
		const char *Info, bool verbose)
	{
		// Initialize ...
		GenoPacked.reset(new UInt8[BlockSNP * PublicDist.N()]);
		memset(PublicDist.get(), 0, sizeof(TDissflag)*PublicDist.Size());
		GenoAlleleFreq.reset(new double[BlockSNP]);

		MCWorkingGeno.Progress.Info = Info;
		MCWorkingGeno.Progress.Show() = verbose;
		MCWorkingGeno.InitParam(true, true, BlockSNP);

		MCWorkingGeno.SplitJobs(NumThread, PublicDist.N(), IBS_Thread_MatIdx, IBS_Thread_MatCnt);
		MCWorkingGeno.Run(NumThread, &_Do_Diss_ReadBlock, &_Do_Diss_Compute, PublicDist.get());
	}
}
Esempio n. 20
0
/*
 * Check some of the error conditions
 */
void DmxterWidgetTest::testErrorConditions() {
  uint8_t RDM_REQUEST_LABEL = 0x80;
  UID source(0x4744, 0x12345678);
  UID destination(3, 4);
  RDMFrames frames;

  RDMRequest *request = NewRequest(source, destination, NULL, 0);

  unsigned int size = RDMCommandSerializer::RequiredSize(*request);
  uint8_t *expected_packet = new uint8_t[size + 1];
  expected_packet[0] = 0xcc;
  OLA_ASSERT(RDMCommandSerializer::Pack(*request, expected_packet + 1, &size));

  // to small to be valid
  uint8_t return_packet[] = {0x00};

  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_INVALID_RESPONSE,
                             frames));

  m_ss.Run();
  m_endpoint->Verify();

  // check mismatched version
  request = NewRequest(source, destination, NULL, 0);
  request->SetTransactionNumber(1);

  OLA_ASSERT(RDMCommandSerializer::Pack(*request, expected_packet + 1, &size));

  // non-0 version
  uint8_t return_packet2[] = {0x01, 0x11, 0xcc};

  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet2,
      sizeof(return_packet2));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_INVALID_RESPONSE,
                             frames));

  delete[] expected_packet;
  m_ss.Run();
  m_endpoint->Verify();
}
Esempio n. 21
0
/**
 * Called when the descriptor is closed.
 */
void TCPTransportTest::Stop() {
  if (m_ss.get())
    m_ss->Terminate();
}
 static PlanCache* getPlanCache()
 {
     if( NULL == planCache.get())
         planCache.reset(new PlanCache());
     return planCache.get();
 }
void SVGPaintServerPattern::setTile(auto_ptr<ImageBuffer> tile)
{
    m_tile.set(tile.release());
}
Esempio n. 24
0
/**
 * Check that we send RDM messages correctly.
 */
void RobeWidgetTest::testSendRDMRequest() {
  // request
  const RDMRequest *rdm_request = NewRequest(DESTINATION);
  unsigned int expected_request_frame_size;
  uint8_t *expected_request_frame = PackRDMRequest(
      rdm_request,
      &expected_request_frame_size);

  // response
  auto_ptr<const RDMResponse> response(
    GetResponseFromData(rdm_request, TEST_RDM_DATA, sizeof(TEST_RDM_DATA)));
  unsigned int response_size;
  uint8_t *response_frame = PackRDMResponse(response.get(), &response_size);

  // add the expected response, send and verify
  m_endpoint->AddExpectedRobeDataAndReturn(
      BaseRobeWidget::RDM_REQUEST,
      expected_request_frame,
      expected_request_frame_size,
      BaseRobeWidget::RDM_RESPONSE,
      response_frame,
      response_size);

  m_widget->SendRDMRequest(
      rdm_request,
      ola::NewSingleCallback(this, &RobeWidgetTest::ValidateResponse));
  m_ss.Run();
  m_endpoint->Verify();

  delete[] expected_request_frame;
  delete[] response_frame;

  // now check broadcast messages
  // request
  rdm_request = NewRequest(BCAST_DESTINATION);
  uint8_t *expected_bcast_request_frame = PackRDMRequest(
      rdm_request,
      &expected_request_frame_size);

  // add the expected response, send and verify
  m_endpoint->AddExpectedRobeMessage(
      BaseRobeWidget::RDM_REQUEST,
      expected_bcast_request_frame,
      expected_request_frame_size,
      ola::NewSingleCallback(this, &RobeWidgetTest::Terminate));

  vector<string> packets;
  // This is a bit confusing, the ValidateStatus is invoked immediately, but
  // we still need to call m_ss.Run() to ensure the correct packet was sent.
  m_widget->SendRDMRequest(
      rdm_request,
      ola::NewSingleCallback(this,
                             &RobeWidgetTest::ValidateStatus,
                             ola::rdm::RDM_WAS_BROADCAST,
                             packets));
  CPPUNIT_ASSERT_EQUAL(ola::rdm::RDM_WAS_BROADCAST, m_received_code);
  m_ss.Run();
  m_endpoint->Verify();

  // cleanup time
  delete[] expected_bcast_request_frame;
}
Esempio n. 25
0
/**
 * Check that we send messages correctly.
 */
void DmxterWidgetTest::testSendRDMRequest() {
  uint8_t RDM_REQUEST_LABEL = 0x80;
  uint8_t RDM_BROADCAST_REQUEST_LABEL = 0x81;
  UID source(1, 2);
  UID destination(3, 4);
  UID bcast_destination(3, 0xffffffff);
  UID new_source(0x5253, 0x12345678);
  vector<string> packets;

  const RDMRequest *request = NewRequest(source, destination, NULL, 0);

  unsigned int size = request->Size();
  uint8_t *expected_packet = new uint8_t[size + 1];
  expected_packet[0] = 0xcc;
  CPPUNIT_ASSERT(request->PackWithControllerParams(
        expected_packet + 1,
        &size,
        new_source,
        0,
        1));

  uint8_t return_packet[] = {
    0x00, 14,  // response code 'ok'
    0xcc,
    1, 28,  // sub code & length
    0x52, 0x53, 0x12, 0x34, 0x56, 0x78,   // dst uid
    0, 3, 0, 0, 0, 4,   // src uid
    0, 1, 0, 0, 0,  // transaction, port id, msg count & sub device
    0x21, 0x1, 0x28, 4,  // command, param id, param data length
    0x5a, 0x5a, 0x5a, 0x5a,  // param data
    0x04, 0x60  // checksum, filled in below
  };

  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateResponse));
  m_ss.Run();
  m_endpoint->Verify();

  // now check broadcast
  request = NewRequest(source, bcast_destination, NULL, 0);

  CPPUNIT_ASSERT(request->PackWithControllerParams(
        expected_packet + 1,
        &size,
        new_source,
        1,  // increment transaction #
        1));

  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_BROADCAST_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_BROADCAST_REQUEST_LABEL,
      static_cast<uint8_t*>(NULL),
      0);

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_WAS_BROADCAST,
                             packets));

  delete[] expected_packet;
  m_ss.Run();
  m_endpoint->Verify();
}
Esempio n. 26
0
/**
 * Test mute device
 */
void RobeWidgetTest::testMuteDevice() {
  // first test when a device doesn't respond
  const MuteRequest mute_request(SOURCE,
                                 DESTINATION,
                                 m_transaction_number++);
  unsigned int expected_request_frame_size;
  uint8_t *expected_request_frame = PackDiscoveryReqest(
      &mute_request,
      &expected_request_frame_size);

  // response, we get PADDING_SIZE bytes when nothing else is returned
  uint8_t response_frame[PADDING_SIZE];
  memset(response_frame, 0, PADDING_SIZE);

  // add the expected response, send and verify
  m_endpoint->AddExpectedRobeDataAndReturn(
      BaseRobeWidget::RDM_REQUEST,
      expected_request_frame,
      expected_request_frame_size,
      BaseRobeWidget::RDM_RESPONSE,
      response_frame,
      PADDING_SIZE);

  m_widget.get()->m_impl->MuteDevice(
      DESTINATION,
      ola::NewSingleCallback(this,
                             &RobeWidgetTest::ValidateMuteStatus,
                             false));
  m_ss.Run();
  m_endpoint->Verify();
  delete[] expected_request_frame;

  // now try an actual mute response
  const MuteRequest mute_request2(SOURCE,
                                  DESTINATION,
                                  m_transaction_number++);
  expected_request_frame = PackDiscoveryReqest(
      &mute_request2,
      &expected_request_frame_size);

  // We can really return anything as long as it's > 4 bytes
  // TODO(simon): make this better
  uint8_t mute_response_frame[] = {0, 0, 0, 0, 0, 0};

  // add the expected response, send and verify
  m_endpoint->AddExpectedRobeDataAndReturn(
      BaseRobeWidget::RDM_REQUEST,
      expected_request_frame,
      expected_request_frame_size,
      BaseRobeWidget::RDM_RESPONSE,
      mute_response_frame,
      sizeof(mute_response_frame));

  m_widget.get()->m_impl->MuteDevice(
      DESTINATION,
      ola::NewSingleCallback(this,
                             &RobeWidgetTest::ValidateMuteStatus,
                             true));
  m_ss.Run();
  m_endpoint->Verify();
  delete[] expected_request_frame;
}
Esempio n. 27
0
/**
 * Check that we handle invalid responses ok
 */
void DmxterWidgetTest::testErrorCodes() {
  uint8_t RDM_REQUEST_LABEL = 0x80;
  UID source(1, 2);
  UID destination(3, 4);
  UID new_source(0x5253, 0x12345678);

  vector<string> packets;

  const RDMRequest *request = NewRequest(source, destination, NULL, 0);

  unsigned int size = request->Size();
  uint8_t *expected_packet = new uint8_t[size + 1];
  expected_packet[0] = 0xcc;
  CPPUNIT_ASSERT(request->PackWithControllerParams(
        expected_packet + 1,
        &size,
        new_source,
        0,
        1));

  uint8_t return_packet[] = {
    0x00, 1,  // checksum failure
  };

  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_CHECKSUM_INCORRECT,
                             packets));
  m_ss.Run();
  m_endpoint->Verify();

  // update transaction # & checksum
  expected_packet[15]++;
  expected_packet[25] = 0xfa;
  return_packet[1] = 8;  // packet too short
  request = NewRequest(source, destination, NULL, 0);
  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_PACKET_TOO_SHORT,
                             packets));
  m_ss.Run();
  m_endpoint->Verify();

  // update transaction # & checksum
  expected_packet[15]++;
  expected_packet[25] = 0xfb;
  return_packet[1] = 12;  // transaction mismatch
  request = NewRequest(source, destination, NULL, 0);
  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_TRANSACTION_MISMATCH,
                             packets));
  m_ss.Run();
  m_endpoint->Verify();

  // update transaction # & checksum
  expected_packet[15]++;
  expected_packet[25] = 0xfc;
  return_packet[1] = 17;  // timeout
  request = NewRequest(source, destination, NULL, 0);
  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_TIMEOUT,
                             packets));
  m_ss.Run();
  m_endpoint->Verify();

  // update transaction # & checksum
  expected_packet[15]++;
  expected_packet[25] = 0xfd;
  return_packet[1] = 41;  // device mismatch
  request = NewRequest(source, destination, NULL, 0);
  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_SRC_UID_MISMATCH,
                             packets));
  m_ss.Run();
  m_endpoint->Verify();

  // update transaction # & checksum
  expected_packet[15]++;
  expected_packet[25] = 0xfe;
  return_packet[1] = 42;  // sub device mismatch
  request = NewRequest(source, destination, NULL, 0);
  m_endpoint->AddExpectedUsbProDataAndReturn(
      RDM_REQUEST_LABEL,
      expected_packet,
      size + 1,
      RDM_REQUEST_LABEL,
      return_packet,
      sizeof(return_packet));

  m_widget->SendRDMRequest(
      request,
      ola::NewSingleCallback(this,
                             &DmxterWidgetTest::ValidateStatus,
                             ola::rdm::RDM_SUB_DEVICE_MISMATCH,
                             packets));
  m_ss.Run();
  m_endpoint->Verify();

  delete[] expected_packet;
}
Esempio n. 28
0
/**
 * Test the DUB request works
 */
void RobeWidgetTest::testBranch() {
  // first test when no devices respond
  const DiscoveryUniqueBranchRequest discovery_request(
      SOURCE,
      UID(0, 0),
      UID::AllDevices(),
      m_transaction_number++);
  unsigned int expected_request_frame_size;
  uint8_t *expected_request_frame = PackDiscoveryReqest(
      &discovery_request,
      &expected_request_frame_size);

  // response, we get PADDING_SIZE bytes when nothing else is returned
  uint8_t response_frame[PADDING_SIZE];
  memset(response_frame, 0, PADDING_SIZE);

  // add the expected response, send and verify
  m_endpoint->AddExpectedRobeDataAndReturn(
      BaseRobeWidget::RDM_DISCOVERY,
      expected_request_frame,
      expected_request_frame_size,
      BaseRobeWidget::RDM_DISCOVERY_RESPONSE,
      NULL,
      0);

  m_widget.get()->m_impl->Branch(
      UID(0, 0),
      UID::AllDevices(),
      ola::NewSingleCallback(this,
                             &RobeWidgetTest::ValidateBranchStatus,
                             static_cast<const uint8_t*>(NULL),
                             static_cast<unsigned int>(0)));
  m_ss.Run();
  m_endpoint->Verify();
  delete[] expected_request_frame;

  // now try an actual response, the data doesn't actually have to be valid
  // because it's just passed straight to the callback.
  const DiscoveryUniqueBranchRequest discovery_request2(
      SOURCE,
      UID(0, 0),
      UID::AllDevices(),
      m_transaction_number++);
  expected_request_frame = PackDiscoveryReqest(
      &discovery_request2,
      &expected_request_frame_size);

  // the response, can be anything really, last 4 bytes is trimmed
  uint8_t response_frame2[] = {1, 2, 3, 4, 0, 0, 0, 0};

  // add the expected response, send and verify
  m_endpoint->AddExpectedRobeDataAndReturn(
      BaseRobeWidget::RDM_DISCOVERY,
      expected_request_frame,
      expected_request_frame_size,
      BaseRobeWidget::RDM_DISCOVERY_RESPONSE,
      response_frame2,
      sizeof(response_frame2));

  m_widget.get()->m_impl->Branch(
      UID(0, 0),
      UID::AllDevices(),
      ola::NewSingleCallback(
        this,
        &RobeWidgetTest::ValidateBranchStatus,
        static_cast<const uint8_t*>(response_frame2),
        // minus the 4 padding bytes
        static_cast<unsigned int>(sizeof(response_frame2) - 4)));
  m_ss.Run();
  m_endpoint->Verify();
  delete[] expected_request_frame;
}
void CameraFactoryRegistrar::register_factory(auto_ptr<FactoryType> factory)
{
    const string model = factory->get_model();
    impl->m_registrar.insert(model, factory);
}
Esempio n. 30
0
	LineSegmentVisitor& operator=(const LineSegmentVisitor& o)
	{
		if ( this == &o ) return *this;
		querySeg = o.querySeg;
		items.reset(new vector<LineSegment*>(*(o.items.get())));
	}