Skip to content
Snippets Groups Projects
USB2.cpp 23.4 KiB
Newer Older
Alessio Igor Bogani's avatar
Alessio Igor Bogani committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
/*----- PROTECTED REGION ID(USB2.cpp) ENABLED START -----*/
//=============================================================================
//
// file :        USB2.cpp
//
// description : C++ source for the USB2 class and its commands.
//               The class is derived from Device. It represents the
//               CORBA servant object which will be accessed from the
//               network. All commands which can be executed on the
//               USB2 are implemented in this file.
//
// project :     
//
// This file is part of Tango device class.
// 
// Tango is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Tango is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Tango.  If not, see <http://www.gnu.org/licenses/>.
// 
// $Author: alessio $
//
// $Revision: 1.5 $
// $Date: 2018-10-24 08:12:41 $
//
// $HeadURL:  $
//
//=============================================================================
//                This file is generated by POGO
//        (Program Obviously used to Generate tango Object)
//=============================================================================


#include <USB2.h>
#include <USB2Class.h>

/*----- PROTECTED REGION END -----*/	//	USB2.cpp

/**
 *  USB2 class description:
 *    
 */

//================================================================
//  The following table gives the correspondence
//  between command and method names.
//
//  Command name  |  Method name
//================================================================
//  Write         |  write
//  Read          |  read
//  ReadUntil     |  read_until
//================================================================

//================================================================
//  Attributes managed are:
//================================================================
//  InputLength    |  Tango::DevLong	Scalar
//  OutputLength   |  Tango::DevLong	Scalar
//  Reconnections  |  Tango::DevLong	Scalar
//================================================================

namespace USB2_ns
{
/*----- PROTECTED REGION ID(USB2::namespace_starting) ENABLED START -----*/

//	static initializations

/*----- PROTECTED REGION END -----*/	//	USB2::namespace_starting

//--------------------------------------------------------
/**
 *	Method      : USB2::USB2()
 *	Description : Constructors for a Tango device
 *                implementing the classUSB2
 */
//--------------------------------------------------------
USB2::USB2(Tango::DeviceClass *cl, string &s)
 : TANGO_BASE_CLASS(cl, s.c_str())
{
	/*----- PROTECTED REGION ID(USB2::constructor_1) ENABLED START -----*/
	reconnections = -1;
	init_device();
	
	/*----- PROTECTED REGION END -----*/	//	USB2::constructor_1
}
//--------------------------------------------------------
USB2::USB2(Tango::DeviceClass *cl, const char *s)
 : TANGO_BASE_CLASS(cl, s)
{
	/*----- PROTECTED REGION ID(USB2::constructor_2) ENABLED START -----*/
	reconnections = -1;
	init_device();
	
	/*----- PROTECTED REGION END -----*/	//	USB2::constructor_2
}
//--------------------------------------------------------
USB2::USB2(Tango::DeviceClass *cl, const char *s, const char *d)
 : TANGO_BASE_CLASS(cl, s, d)
{
	/*----- PROTECTED REGION ID(USB2::constructor_3) ENABLED START -----*/
	reconnections = -1;
	init_device();
	
	/*----- PROTECTED REGION END -----*/	//	USB2::constructor_3
}

//--------------------------------------------------------
/**
 *	Method      : USB2::delete_device()
 *	Description : will be called at device destruction or at init command
 */
//--------------------------------------------------------
void USB2::delete_device()
{
	DEBUG_STREAM << "USB2::delete_device() " << device_name << endl;
	/*----- PROTECTED REGION ID(USB2::delete_device) ENABLED START -----*/
	
	close();
	delete[] buffer;

	/*----- PROTECTED REGION END -----*/	//	USB2::delete_device
	delete[] attr_InputLength_read;
	delete[] attr_OutputLength_read;
	delete[] attr_Reconnections_read;
}

//--------------------------------------------------------
/**
 *	Method      : USB2::init_device()
 *	Description : will be called at device initialization.
 */
//--------------------------------------------------------
void USB2::init_device()
{
	DEBUG_STREAM << "USB2::init_device() create device " << device_name << endl;
	/*----- PROTECTED REGION ID(USB2::init_device_before) ENABLED START -----*/
	
	//	Initialization before get_device_property() call
	ctx = NULL;
	dev_handle = NULL;
	buffer = NULL;
	init_error.clear();
	
	/*----- PROTECTED REGION END -----*/	//	USB2::init_device_before
	

	//	Get the device properties from database
	get_device_property();
	
	attr_InputLength_read = new Tango::DevLong[1];
	attr_OutputLength_read = new Tango::DevLong[1];
	attr_Reconnections_read = new Tango::DevLong[1];
	//	No longer if mandatory property not set. 
	if (mandatoryNotDefined)
		return;

	/*----- PROTECTED REGION ID(USB2::init_device) ENABLED START -----*/

	//	Initialize device
	try
	{
		buffer = new unsigned char[maxPacketSize];
		open();
	}
	catch (Tango::DevFailed &e)
	{
		init_error = "Initialization failed: " + string(e.errors[0].desc);
	}
	catch (...)
	{
		init_error = "Initialization failed: Unknown error";
	}

	/*----- PROTECTED REGION END -----*/	//	USB2::init_device
}

//--------------------------------------------------------
/**
 *	Method      : USB2::get_device_property()
 *	Description : Read database to initialize property data members.
 */
//--------------------------------------------------------
void USB2::get_device_property()
{
	/*----- PROTECTED REGION ID(USB2::get_device_property_before) ENABLED START -----*/
	
	//	Initialize property data members
	
	/*----- PROTECTED REGION END -----*/	//	USB2::get_device_property_before

	mandatoryNotDefined = false;

	//	Read device properties from database.
	Tango::DbData	dev_prop;
	dev_prop.push_back(Tango::DbDatum("Timeout"));
	dev_prop.push_back(Tango::DbDatum("VendorID"));
	dev_prop.push_back(Tango::DbDatum("ProductID"));
	dev_prop.push_back(Tango::DbDatum("Interface"));
	dev_prop.push_back(Tango::DbDatum("EndpointUP"));
	dev_prop.push_back(Tango::DbDatum("EndpointDOWN"));
	dev_prop.push_back(Tango::DbDatum("MaxPacketSize"));

	//	is there at least one property to be read ?
	if (dev_prop.size()>0)
	{
		//	Call database and extract values
		if (Tango::Util::instance()->_UseDb==true)
			get_db_device()->get_property(dev_prop);
	
		//	get instance on USB2Class to get class property
		Tango::DbDatum	def_prop, cl_prop;
		USB2Class	*ds_class =
			(static_cast<USB2Class *>(get_device_class()));
		int	i = -1;

		//	Try to initialize Timeout from class property
		cl_prop = ds_class->get_class_property(dev_prop[++i].name);
		if (cl_prop.is_empty()==false)	cl_prop  >>  timeout;
		else {
			//	Try to initialize Timeout from default device value
			def_prop = ds_class->get_default_device_property(dev_prop[i].name);
			if (def_prop.is_empty()==false)	def_prop  >>  timeout;
		}
		//	And try to extract Timeout value from database
		if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  timeout;
		//	Property StartDsPath is mandatory, check if has been defined in database.
		check_mandatory_property(cl_prop, dev_prop[i]);

		//	Try to initialize VendorID from class property
		cl_prop = ds_class->get_class_property(dev_prop[++i].name);
		if (cl_prop.is_empty()==false)	cl_prop  >>  vendorID;
		else {
			//	Try to initialize VendorID from default device value
			def_prop = ds_class->get_default_device_property(dev_prop[i].name);
			if (def_prop.is_empty()==false)	def_prop  >>  vendorID;
		}
		//	And try to extract VendorID value from database
		if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  vendorID;
		//	Property StartDsPath is mandatory, check if has been defined in database.
		check_mandatory_property(cl_prop, dev_prop[i]);

		//	Try to initialize ProductID from class property
		cl_prop = ds_class->get_class_property(dev_prop[++i].name);
		if (cl_prop.is_empty()==false)	cl_prop  >>  productID;
		else {
			//	Try to initialize ProductID from default device value
			def_prop = ds_class->get_default_device_property(dev_prop[i].name);
			if (def_prop.is_empty()==false)	def_prop  >>  productID;
		}
		//	And try to extract ProductID value from database
		if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  productID;
		//	Property StartDsPath is mandatory, check if has been defined in database.
		check_mandatory_property(cl_prop, dev_prop[i]);

		//	Try to initialize Interface from class property
		cl_prop = ds_class->get_class_property(dev_prop[++i].name);
		if (cl_prop.is_empty()==false)	cl_prop  >>  interface;
		else {
			//	Try to initialize Interface from default device value
			def_prop = ds_class->get_default_device_property(dev_prop[i].name);
			if (def_prop.is_empty()==false)	def_prop  >>  interface;
		}
		//	And try to extract Interface value from database
		if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  interface;
		//	Property StartDsPath is mandatory, check if has been defined in database.
		check_mandatory_property(cl_prop, dev_prop[i]);

		//	Try to initialize EndpointUP from class property
		cl_prop = ds_class->get_class_property(dev_prop[++i].name);
		if (cl_prop.is_empty()==false)	cl_prop  >>  endpointUP;
		else {
			//	Try to initialize EndpointUP from default device value
			def_prop = ds_class->get_default_device_property(dev_prop[i].name);
			if (def_prop.is_empty()==false)	def_prop  >>  endpointUP;
		}
		//	And try to extract EndpointUP value from database
		if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  endpointUP;
		//	Property StartDsPath is mandatory, check if has been defined in database.
		check_mandatory_property(cl_prop, dev_prop[i]);

		//	Try to initialize EndpointDOWN from class property
		cl_prop = ds_class->get_class_property(dev_prop[++i].name);
		if (cl_prop.is_empty()==false)	cl_prop  >>  endpointDOWN;
		else {
			//	Try to initialize EndpointDOWN from default device value
			def_prop = ds_class->get_default_device_property(dev_prop[i].name);
			if (def_prop.is_empty()==false)	def_prop  >>  endpointDOWN;
		}
		//	And try to extract EndpointDOWN value from database
		if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  endpointDOWN;
		//	Property StartDsPath is mandatory, check if has been defined in database.
		check_mandatory_property(cl_prop, dev_prop[i]);

		//	Try to initialize MaxPacketSize from class property
		cl_prop = ds_class->get_class_property(dev_prop[++i].name);
		if (cl_prop.is_empty()==false)	cl_prop  >>  maxPacketSize;
		else {
			//	Try to initialize MaxPacketSize from default device value
			def_prop = ds_class->get_default_device_property(dev_prop[i].name);
			if (def_prop.is_empty()==false)	def_prop  >>  maxPacketSize;
		}
		//	And try to extract MaxPacketSize value from database
		if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  maxPacketSize;
		//	Property StartDsPath is mandatory, check if has been defined in database.
		check_mandatory_property(cl_prop, dev_prop[i]);

	}

	/*----- PROTECTED REGION ID(USB2::get_device_property_after) ENABLED START -----*/
	
	//	Check device property data members init
	vid = stoi(vendorID, NULL, 16);
	pid = stoi(productID, NULL, 16);

	ep_up = stoi(endpointUP, NULL, 16);
	ep_down = stoi(endpointDOWN, NULL, 16);

	/*----- PROTECTED REGION END -----*/	//	USB2::get_device_property_after
}
//--------------------------------------------------------
/**
 *	Method      : USB2::check_mandatory_property()
 *	Description : For mandatory properties check if defined in database.
 */
//--------------------------------------------------------
void USB2::check_mandatory_property(Tango::DbDatum &class_prop, Tango::DbDatum &dev_prop)
{
	//	Check if all properties are empty
	if (class_prop.is_empty() && dev_prop.is_empty())
	{
		TangoSys_OMemStream	tms;
		tms << endl <<"Property \'" << dev_prop.name;
		if (Tango::Util::instance()->_UseDb==true)
			tms << "\' is mandatory but not defined in database";
		else
			tms << "\' is mandatory but cannot be defined without database";
		string	status(get_status());
		status += tms.str();
		set_status(status);
		mandatoryNotDefined = true;
		/*----- PROTECTED REGION ID(USB2::check_mandatory_property) ENABLED START -----*/
		cerr << tms.str() << " for " << device_name << endl;
		
		/*----- PROTECTED REGION END -----*/	//	USB2::check_mandatory_property
	}
}


//--------------------------------------------------------
/**
 *	Method      : USB2::always_executed_hook()
 *	Description : method always executed before any command is executed
 */
//--------------------------------------------------------
void USB2::always_executed_hook()
{
	DEBUG_STREAM << "USB2::always_executed_hook()  " << device_name << endl;
	if (mandatoryNotDefined)
	{
		string	status(get_status());
		Tango::Except::throw_exception(
					(const char *)"PROPERTY_NOT_SET",
					status.c_str(),
					(const char *)"USB2::always_executed_hook()");
	}
	/*----- PROTECTED REGION ID(USB2::always_executed_hook) ENABLED START -----*/
	
	//	code always executed before all requests
	if (! init_error.empty())
	{
		set_state(Tango::FAULT);
		set_status(init_error);
		return;
	}

	try {
		if (! dev_handle) {
			open();
		}
	} catch(Tango::DevFailed &e) {
		set_state(Tango::FAULT);
		set_status(string(e.errors[0].desc));
	} catch(...) {
		set_state(Tango::FAULT);
		set_status("The device is in FAULT state for unknown reason");
	}

	/*----- PROTECTED REGION END -----*/	//	USB2::always_executed_hook
}

//--------------------------------------------------------
/**
 *	Method      : USB2::read_attr_hardware()
 *	Description : Hardware acquisition for attributes
 */
//--------------------------------------------------------
void USB2::read_attr_hardware(TANGO_UNUSED(vector<long> &attr_list))
{
	DEBUG_STREAM << "USB2::read_attr_hardware(vector<long> &attr_list) entering... " << endl;
	/*----- PROTECTED REGION ID(USB2::read_attr_hardware) ENABLED START -----*/
	
	//	Add your own code
	
	/*----- PROTECTED REGION END -----*/	//	USB2::read_attr_hardware
}

//--------------------------------------------------------
/**
 *	Read attribute InputLength related method
 *	Description: 
 *
 *	Data type:	Tango::DevLong
 *	Attr type:	Scalar
 */
//--------------------------------------------------------
void USB2::read_InputLength(Tango::Attribute &attr)
{
	DEBUG_STREAM << "USB2::read_InputLength(Tango::Attribute &attr) entering... " << endl;
	/*----- PROTECTED REGION ID(USB2::read_InputLength) ENABLED START -----*/
	//	Set the attribute value
	attr_InputLength_read[0] = input_queue_length(); 
	attr.set_value(attr_InputLength_read);
	
	/*----- PROTECTED REGION END -----*/	//	USB2::read_InputLength
}
//--------------------------------------------------------
/**
 *	Read attribute OutputLength related method
 *	Description: 
 *
 *	Data type:	Tango::DevLong
 *	Attr type:	Scalar
 */
//--------------------------------------------------------
void USB2::read_OutputLength(Tango::Attribute &attr)
{
	DEBUG_STREAM << "USB2::read_OutputLength(Tango::Attribute &attr) entering... " << endl;
	/*----- PROTECTED REGION ID(USB2::read_OutputLength) ENABLED START -----*/
	//	Set the attribute value
	attr_OutputLength_read[0] = output_queue_length();
	attr.set_value(attr_OutputLength_read);
	
	/*----- PROTECTED REGION END -----*/	//	USB2::read_OutputLength
}
//--------------------------------------------------------
/**
 *	Read attribute Reconnections related method
 *	Description: 
 *
 *	Data type:	Tango::DevLong
 *	Attr type:	Scalar
 */
//--------------------------------------------------------
void USB2::read_Reconnections(Tango::Attribute &attr)
{
	DEBUG_STREAM << "USB2::read_Reconnections(Tango::Attribute &attr) entering... " << endl;
	/*----- PROTECTED REGION ID(USB2::read_Reconnections) ENABLED START -----*/
	//	Set the attribute value
	attr_Reconnections_read[0] = reconnections;
	attr.set_value(attr_Reconnections_read);
	
	/*----- PROTECTED REGION END -----*/	//	USB2::read_Reconnections
}

//--------------------------------------------------------
/**
 *	Method      : USB2::add_dynamic_attributes()
 *	Description : Create the dynamic attributes if any
 *                for specified device.
 */
//--------------------------------------------------------
void USB2::add_dynamic_attributes()
{
	/*----- PROTECTED REGION ID(USB2::add_dynamic_attributes) ENABLED START -----*/
	
	//	Add your own code to create and add dynamic attributes if any
	
	/*----- PROTECTED REGION END -----*/	//	USB2::add_dynamic_attributes
}

//--------------------------------------------------------
/**
 *	Command Write related method
 *	Description: 
 *
 *	@param argin 
 */
//--------------------------------------------------------
void USB2::write(const Tango::DevVarCharArray *argin)
{
	DEBUG_STREAM << "USB2::Write()  - " << device_name << endl;
	/*----- PROTECTED REGION ID(USB2::write) ENABLED START -----*/
	
	check_init();

	if(!timeout) {
		Tango::Except::throw_exception( "",
				"Invalid timeout",
				"USB2::write()");
	}

	unsigned char *argin_data = new unsigned char[argin->length()];
	for(unsigned int i=0; i<argin->length(); ++i)
	{
		argin_data[i] = (*argin)[i];
	}

	int bytes_total = 0, bytes_to_write  = argin->length();
	while(bytes_total != bytes_to_write) {
		int bytes_written;
		int ret = libusb_bulk_transfer(dev_handle,
				ep_down, argin_data + bytes_total,
				bytes_to_write - bytes_total, &bytes_written, timeout);

		if(ret != 0 || bytes_written != bytes_to_write - bytes_total) {
			delete argin_data;

			string error_mesg = "Unable to send request to device";

			close();
			open();
			DEBUG_STREAM << error_mesg << endl;
			set_state( Tango::FAULT );
			set_status( error_mesg );

			Tango::Except::throw_exception( "",
					error_mesg,
					"USB2::write()");
		}
		bytes_total += bytes_written;
	}
	delete argin_data;

	/*----- PROTECTED REGION END -----*/	//	USB2::write
}
//--------------------------------------------------------
/**
 *	Command Read related method
 *	Description: 
 *
 *	@param argin 
 *	@returns 
 */
//--------------------------------------------------------
Tango::DevVarCharArray *USB2::read(Tango::DevLong argin)
{
	Tango::DevVarCharArray *argout;
	DEBUG_STREAM << "USB2::Read()  - " << device_name << endl;
	/*----- PROTECTED REGION ID(USB2::read) ENABLED START -----*/
	
	check_init();
	
	if(!timeout) {
		Tango::Except::throw_exception( "",
				"Invalid timeout",
				"USB2::write()");
	}
	
	if (argin < 0)
	{
		Tango::Except::throw_exception("",
				"Input has to be in positive range",
				"USB2::read()");
	}

	while( (size_t)argin > data.size() )
	{
		int bytes_readed;
		int ret = libusb_bulk_transfer(dev_handle, ep_up, buffer,
				maxPacketSize, &bytes_readed, timeout);
		if(ret != 0 || bytes_readed == 0) {
			string error_mesg( "No response from device" );
			DEBUG_STREAM << error_mesg << endl;
			Tango::Except::throw_exception("",
					error_mesg, "USB2::read()");
		}

		for(int i=0;i<bytes_readed;++i) {
			data.push_back(buffer[i]);
		}
	}

	argout = new Tango::DevVarCharArray();
	argout->length(argin);
	for( int i=0; i<argin; ++i )
	{
		(*argout)[i] = data[i];
	}
	data.erase( data.begin(), data.begin() + argin );

	/*----- PROTECTED REGION END -----*/	//	USB2::read
	return argout;
}
//--------------------------------------------------------
/**
 *	Command ReadUntil related method
 *	Description: 
 *
 *	@param argin 
 *	@returns 
 */
//--------------------------------------------------------
Tango::DevVarCharArray *USB2::read_until(const Tango::DevVarCharArray *argin)
{
	Tango::DevVarCharArray *argout;
	DEBUG_STREAM << "USB2::ReadUntil()  - " << device_name << endl;
	/*----- PROTECTED REGION ID(USB2::read_until) ENABLED START -----*/
	
	check_init();
	
	if(!timeout) {
		Tango::Except::throw_exception( "",
				"Invalid timeout",
				"USB2::write()");
	}
	
	if (argin->length() != 1)
	{
		Tango::Except::throw_exception("",
				"Delimiter has to be one byte length",
				"USB2::read_until()");
	}

	char delim = (*argin)[0];
	bool found = false;
	size_t pos;

	while( ! found )
	{
		for( pos = 0; pos < data.size(); ++pos )
		{
			if (memcmp(&data[pos], &delim, 1) == 0)
			{
				found = true;
				break;
			}
		}
		if ( ! found )
		{
			int bytes_readed;
			int ret = libusb_bulk_transfer(dev_handle, ep_up, buffer,
					maxPacketSize, &bytes_readed, timeout);
			if(ret != 0 || bytes_readed == 0) {
				string error_mesg( "No response from device" );
				DEBUG_STREAM << error_mesg << endl;
				Tango::Except::throw_exception("",
						error_mesg, "USB2::read_until()");
			}

			for(int i=0;i<bytes_readed;++i) {
				data.push_back(buffer[i]);
			}
		}
	}

	argout = new Tango::DevVarCharArray();
	argout->length( pos+1 );
	for( size_t i = 0; i < pos + 1; ++i )
	{
		(*argout)[i] = data[i];
	}
	data.erase( data.begin(), data.begin() + pos + 1 );

	/*----- PROTECTED REGION END -----*/	//	USB2::read_until
	return argout;
}
//--------------------------------------------------------
/**
 *	Method      : USB2::add_dynamic_commands()
 *	Description : Create the dynamic commands if any
 *                for specified device.
 */
//--------------------------------------------------------
void USB2::add_dynamic_commands()
{
	/*----- PROTECTED REGION ID(USB2::add_dynamic_commands) ENABLED START -----*/
	
	//	Add your own code to create and add dynamic commands if any
	
	/*----- PROTECTED REGION END -----*/	//	USB2::add_dynamic_commands
}

/*----- PROTECTED REGION ID(USB2::namespace_ending) ENABLED START -----*/

//	Additional Methods
void USB2::check_init()
{
	if (! init_error.empty() )
	{
		DEBUG_STREAM << init_error << endl;
		Tango::Except::throw_exception( "",
				init_error.c_str(),
				"USB2::check_init()");
	}
}

void USB2::open()
{
	DEBUG_STREAM << "Opening USB device..." << endl;

	if(libusb_init(&ctx) < 0) {
		string error_mesg = "Failed to initialize libusb";
		ERROR_STREAM << error_mesg << endl;
		assert( false);
		Tango::Except::throw_exception( "",
				error_mesg,
				"USB2::open()");
	}

#ifndef NDEBUG
	libusb_set_debug(ctx, LIBUSB_LOG_LEVEL_WARNING);
#endif

	dev_handle = libusb_open_device_with_vid_pid(ctx, vid, pid);
	if(dev_handle == NULL) {
		libusb_exit(ctx);

		string error_mesg = "Unable to open device";
		ERROR_STREAM << error_mesg << endl;
		assert( false);
		Tango::Except::throw_exception( "",
				error_mesg,
				"USB2::open()");
	}

	if(libusb_claim_interface(dev_handle, interface) < 0) {
		libusb_release_interface(dev_handle, interface);
		libusb_close(dev_handle);
		libusb_exit(ctx);

		string error_mesg = "Failed to claim interface";
		ERROR_STREAM << error_mesg << endl;
		assert( false);
		Tango::Except::throw_exception( "",
				error_mesg,
				"USB2::open()");
	}
  
	DEBUG_STREAM << "USB device opened." << endl;
	reconnections++;

	set_state(Tango::ON);
	set_status("The device in ON state");
}

void USB2::close()
{
	DEBUG_STREAM << "Closing USB device..." << endl;

	int input_len = input_queue_length();
	int output_len = output_queue_length();

	if( input_len + output_len)
	{
		WARN_STREAM << " Bytes dropped: " << input_len << " input, "
			<< output_len << " output" << endl;
	}

	if(dev_handle)
	{
		libusb_release_interface(dev_handle, interface);
		libusb_close(dev_handle);
		libusb_exit(ctx);

		dev_handle = NULL;
	}

	data.clear();

	DEBUG_STREAM << "USB device closed" << endl;
}

int USB2::input_queue_length()
{
	return data.size();
}

int USB2::output_queue_length()
{
	return 0;
}

/*----- PROTECTED REGION END -----*/	//	USB2::namespace_ending
} //	namespace