Example #1
0
int it_list(T_ArgItem *item_arg)
{
   DEBUG_IT_LIST;

   lex_rc = GetToken(&token);
   TOKEN_CHECK;

   if (lex_rc == TOKEN_RBRACKET) {
      DEBUG_IT_LIST_END;
      return EXIT_SUCCESS;
   }

   else if (lex_rc == TOKEN_COMMA) {
      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      CALL_CHECK(item());

      if (item_arg == NULL) {
         errno = SYNTACTIC_ERROR;
         return EXIT_FAILURE;
      }
      CALL_CHECK(T_HTableInsert(id_table, item_arg->data.data, read_var));
      CALL_CHECK(it_list(item_arg->next));
   }

   else if (lex_rc == TOKEN_BROKEN || lex_rc == TOKEN_PROBLEM)
      return EXIT_FAILURE;

   else {
      errno = SYNTACTIC_ERROR;
      return EXIT_FAILURE;
   }
}
Example #2
0
int print_list()
{
   lex_rc = GetToken(&token);
   TOKEN_CHECK;

   if (lex_rc == TOKEN_RBRACKET)
      return EXIT_SUCCESS;

   else if (lex_rc == TOKEN_COMMA) {
      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      CALL_CHECK(item());

      if (T_HTableExport(id_table, "0ret", &write_var) == HASH_NOT_FOUND) {
         errno = INTERNAL_ERROR;
         return EXIT_FAILURE;
      }
      GenerateInstruction(IID_PRINT, read_var, write_var, NULL);
      CALL_CHECK(print_list());
   }

   else if (lex_rc == TOKEN_BROKEN || lex_rc == TOKEN_PROBLEM)
      return EXIT_FAILURE;

   else {
      errno = SYNTACTIC_ERROR;
      return EXIT_FAILURE;
   }
}
Example #3
0
int initalize_ps3(libusb_device_handle *handle)
{
	int r;
	uint8_t input_report[49];
	CALL_CHECK(libusb_control_transfer(handle, 0x80, 0x06, 0x0001, 0x0, input_report, 0x12, 1000));
	CALL_CHECK(libusb_control_transfer(handle, 0x80, 0x06, 0x0002, 0x0, input_report, 0x09, 1000));
	CALL_CHECK(libusb_control_transfer(handle, 0x80, 0x06, 0x0002, 0x0, input_report, 0x29, 1000));
	CALL_CHECK(libusb_control_transfer(handle, 0xa1, 0x01, 0x03f2, 0x0, input_report, 0x11, 1000));
	CALL_CHECK(libusb_control_transfer(handle, 0xa1, 0x01, 0x03f5, 0x0, input_report, 0x08, 1000));
	return 0;
}
Example #4
0
int FIRST_eval(Expr_list**first, Expr_list** last)
{
   Expr_list *pom;
   Expr_list *del;
   T_Var *var;
   //1. **
   if (debug) printf("\n-----* /----\n");
   pom = *first;
   while ((pom != NULL) && (pom != *last)) {
      if (pom->operace==TOKEN_DSTAR)
         if (((pom->value->NID == NID_NUMBER) || (pom->value->NID == NID_UNDEF))
            &&((pom->next->value->NID == NID_NUMBER) || (pom->next->value->NID == NID_UNDEF)))
         {
            if (debug) printf("%f - %f  ** \n",pom->value->vals.d_val ,pom->next->value->vals.d_val );

            CALL_CHECK(GenerateVariable(FALSE, &var));
            var->NID=NID_NUMBER;

            GenerateInstruction(IID_PWR, var, pom->value, pom->next->value);
            pom->value=var;

            MYSHORTCUT;
         }
         else { errno = SEMANTIC_ERROR; if (debug) printf("semantika ** \n"); return EXIT_FAILURE; }
      else pom = pom->next;
   }
   return EXIT_SUCCESS;
}
Example #5
0
//-------------------------------------------------------------------
// VYHODNOCENI LISTU PRO  > , >= ,<= < , != ,==
//-------------------------------------------------------------------
int FOURTH_eval(Expr_list**first, Expr_list** last)
{
   Expr_list *pom;
   Expr_list *del;
   T_Var *var;

   pom = *first;
   if (debug) printf("\n   > , >= , <= < , != , ==   ");
   while ((pom != NULL) && (pom != *last)) {
      // mozne plynule vyhodnocovat prioritne na nejnizi urovni
      // v dane useku zbyly pouze operace relace
      CALL_CHECK(GenerateVariable(FALSE, &var));
      var->NID = NID_BOOL;

      switch (pom->operace) {
      case TOKEN_BIGGER  : T_InstrSet(&instr, IID_BIGGER, var, pom->value, pom->next->value); break;
      case TOKEN_SMALLER : T_InstrSet(&instr, IID_SMALLER, var, pom->value, pom->next->value); break;
      case TOKEN_BEQUAL  : T_InstrSet(&instr, IID_BEQUAL, var, pom->value, pom->next->value); break;
      case TOKEN_SMEQUAL : T_InstrSet(&instr, IID_SMEQUAL, var, pom->value, pom->next->value); break;
      case TOKEN_EQUAL   : T_InstrSet(&instr, IID_EQUAL, var, pom->value, pom->next->value); break;
      case TOKEN_UNEQUAL : T_InstrSet(&instr, IID_UNEQUAL, var, pom->value, pom->next->value); break;
      default : free(var); var = NULL;
      }
      if (var!=NULL) {
         T_ListAppend(inst_list, &instr);
         pom->value=var;
         MYSHORTCUT;
      }
   }
   return EXIT_SUCCESS;
}
Example #6
0
//-------------------------------------------------------------------
// VYHODNOCENI LISTU PRO  +    -
//-------------------------------------------------------------------
int THIRT_eval(Expr_list** first, Expr_list** last)
{
   Expr_list *pom;
   Expr_list *del;

   T_Var *var;
   pom=*first;
   if (debug) printf("\n-----+ - ----");
   while ((pom != NULL) && (pom != *last)) {
      if (pom->operace == TOKEN_MINUS)
      //------------------------NUM - NUM --------------------------
      if (((pom->value->NID == NID_NUMBER) || (pom->value->NID == NID_UNDEF))
          &&((pom->next->value->NID == NID_NUMBER) || (pom->next->value->NID == NID_UNDEF)))
      {
         if (debug) printf("%f - %f  - \n", pom->value->vals.d_val, pom->next->value->vals.d_val);
         CALL_CHECK(GenerateVariable(FALSE, &var));
         var->NID = NID_NUMBER;

         GenerateInstruction(IID_DIFF, var, pom->value, pom->next->value);

         pom->value=var;

         MYSHORTCUT;
      }
      else { errno = SEMANTIC_ERROR; if (debug) printf("semantika -\n"); return EXIT_FAILURE; }

      else if (pom->operace == TOKEN_PLUS)
      //------------------------NUM - NUM --------------------------
      if ((pom->value->NID == NID_NUMBER) || (pom->value->NID == NID_UNDEF) || (pom->value->NID == NID_STRING)) {
         if (debug) printf("%f - %f  + \n", pom->value->vals.d_val, pom->next->value->vals.d_val);
         CALL_CHECK(GenerateVariable(FALSE, &var));
         var->NID = pom->value->NID;

         GenerateInstruction(IID_PLUS, var, pom->value, pom->next->value);
         pom->value = var;

         MYSHORTCUT;
      }
      else { errno = SEMANTIC_ERROR; if (debug) printf("semantika + \n"); return EXIT_FAILURE; }
      else pom = pom->next;
   }
   return EXIT_SUCCESS;
}
Example #7
0
int while_state(T_Var *start_lbl, T_Var *end_lbl)
{
   DEBUG_WHILE;

   lex_rc = GetToken(&token);
   TOKEN_CHECK;
   if (lex_rc == TOKEN_IDENTIFICATOR) {
      if (strcmp(token.data, "end") == 0) {
         GenerateInstruction(IID_JUMP, start_lbl, NULL, NULL);
         GenerateInstruction(IID_LABEL, end_lbl, NULL, NULL);
         T_LabelListAppend(lbl_list, inst_list->last);

         return EXIT_SUCCESS;
      }
   }

   CALL_CHECK(stat(MODE_INSIDE));
   CALL_CHECK(while_state(start_lbl, end_lbl));

   DEBUG_WHILE_END;

   return EXIT_SUCCESS;
}
Example #8
0
int if_state(int mode, T_Var *jmp)
{
   DEBUG_IF;

   lex_rc = GetToken(&token);
   TOKEN_CHECK;
   if (lex_rc == TOKEN_IDENTIFICATOR) {
      if (mode == MODE_OUTSIDE && strcmp(token.data, "else") == 0) {
         DEBUG_IF_ELSE;
         // Expression is true -- jump behind else
         GenerateVariable(TRUE, &write_var);
         GenerateInstruction(IID_JUMP, write_var, NULL, NULL);

         // Expression is false -- else follows
         GenerateInstruction(IID_LABEL, jmp, NULL, NULL);
         T_LabelListAppend(lbl_list, inst_list->last);

         return if_state(MODE_INSIDE, write_var);
      }

      else if (strcmp(token.data, "end") == 0) {
         DEBUG_IF_END;
         // Once end is found, places its label
         GenerateInstruction(IID_LABEL, jmp, NULL, NULL);
         T_LabelListAppend(lbl_list, inst_list->last);

         return EXIT_SUCCESS;
      }
   }

   CALL_CHECK(stat(MODE_INSIDE));

   CALL_CHECK(if_state(mode, jmp));

   DEBUG_IF_END;
   return EXIT_SUCCESS;
}
Example #9
0
static int set_xbox_actuators(libusb_device_handle *handle, uint8_t left, uint8_t right)
{
	int r;
	uint8_t output_report[6];

	printf("\nWriting XBox Controller Output Report...\n");

	memset(output_report, 0, sizeof(output_report));
	output_report[1] = sizeof(output_report);
	output_report[3] = left;
	output_report[5] = right;

	CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_OUT|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
		HID_SET_REPORT, (HID_REPORT_TYPE_OUTPUT<<8)|0x00, 0, output_report, 06, 1000));
	return 0;
}
Example #10
0
int Synan()
{
   DEBUG_SYNAN;

   lex_rc = GetToken(&token);
   TOKEN_CHECK;
   if (lex_rc == TOKEN_EOF) {
      DEBUG_SYNAN_END;
      return EXIT_SUCCESS;
   }
   else
      CALL_CHECK(stat(MODE_OUTSIDE));

   DEBUG_SYNAN_END;
   return Synan();

   errno = SYNTACTIC_ERROR;
   return EXIT_FAILURE;
}
Example #11
0
// The XBOX Controller is really a HID device that got its HID Report Descriptors
// removed by Microsoft.
// Input/Output reports described at http://euc.jp/periphs/xbox-controller.ja.html
static int display_xbox_status(libusb_device_handle *handle)
{
	int r;
	uint8_t input_report[20];
	printf("\nReading XBox Input Report...\n");
	CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
		HID_GET_REPORT, (HID_REPORT_TYPE_INPUT<<8)|0x00, 0, input_report, 20, 1000));
	printf("   D-pad: %02X\n", input_report[2]&0x0F);
	printf("   Start:%d, Back:%d, Left Stick Press:%d, Right Stick Press:%d\n", B(input_report[2]&0x10), B(input_report[2]&0x20),
		B(input_report[2]&0x40), B(input_report[2]&0x80));
	// A, B, X, Y, Black, White are pressure sensitive
	printf("   A:%d, B:%d, X:%d, Y:%d, White:%d, Black:%d\n", input_report[4], input_report[5],
		input_report[6], input_report[7], input_report[9], input_report[8]);
	printf("   Left Trigger: %d, Right Trigger: %d\n", input_report[10], input_report[11]);
	printf("   Left Analog (X,Y): (%d,%d)\n", (int16_t)((input_report[13]<<8)|input_report[12]),
		(int16_t)((input_report[15]<<8)|input_report[14]));
	printf("   Right Analog (X,Y): (%d,%d)\n", (int16_t)((input_report[17]<<8)|input_report[16]),
		(int16_t)((input_report[19]<<8)|input_report[18]));
	return 0;
}
Example #12
0
int item()
{
   DEBUG_ITEM;

   double tmp;
   switch (lex_rc) {
   case TOKEN_STRING:
      DEBUG_ITEM_STR;

      CALL_CHECK(GenerateVariable(FALSE, &read_var));
      T_StringToT_Var(&token, read_var);

      DEBUG_ITEM_END;
      return EXIT_SUCCESS;
   break;
   case TOKEN_NUMBER:
      DEBUG_ITEM_NUM;

      CALL_CHECK(GenerateVariable(FALSE, &read_var));
      tmp = strtod(token.data, NULL);
      CDoubleToT_Var(tmp, read_var);

      DEBUG_ITEM_END;
      return EXIT_SUCCESS;
   break;

   case TOKEN_IDENTIFICATOR:
      if (strcmp(token.data, "true") == 0) {
         DEBUG_ITEM_TRUE;

         CALL_CHECK(GenerateVariable(FALSE, &read_var));
         read_var->NID = NID_BOOL;
         read_var->vals.b_val = 1;

         DEBUG_ITEM_END;
         return EXIT_SUCCESS;
      }
      else if (strcmp(token.data, "false") == 0) {
         DEBUG_ITEM_FALSE;

         CALL_CHECK(GenerateVariable(FALSE, &read_var));
         read_var->NID = NID_BOOL;
         read_var->vals.b_val = 0;

         DEBUG_ITEM_END;
         return EXIT_SUCCESS;
      }
      else if (strcmp(token.data, "nil") == 0) {
         DEBUG_ITEM_NIL;

         CALL_CHECK(GenerateVariable(FALSE, &read_var));
         read_var->NID = NID_NIL;

         DEBUG_ITEM_END;
         return EXIT_SUCCESS;
      }
      else if (T_HTableExport(id_table, token.data, &read_var) == HASH_FOUND) {
         DEBUG_ITEM_ID;
         return EXIT_SUCCESS;
      }
   break;
   }

   errno = SYNTACTIC_ERROR;
   return EXIT_FAILURE;
}
Example #13
0
// The PS3 Controller is really a HID device that got its HID Report Descriptors
// removed by Sony
static int display_ps3_status(libusb_device_handle *handle)
{
	int r;
	uint8_t input_report[49];
	uint8_t master_bt_address[8];
	uint8_t device_bt_address[18];

	// Get the controller's bluetooth address of its master device
	CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
		HID_GET_REPORT, 0x03f5, 0, master_bt_address, sizeof(master_bt_address), 100));
	printf("\nMaster's bluetooth address: %02X:%02X:%02X:%02X:%02X:%02X\n", master_bt_address[2], master_bt_address[3],
		master_bt_address[4], master_bt_address[5], master_bt_address[6], master_bt_address[7]);

	// Get the controller's bluetooth address
	CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
		HID_GET_REPORT, 0x03f2, 0, device_bt_address, sizeof(device_bt_address), 100));
	printf("\nMaster's bluetooth address: %02X:%02X:%02X:%02X:%02X:%02X\n", device_bt_address[4], device_bt_address[5],
		device_bt_address[6], device_bt_address[7], device_bt_address[8], device_bt_address[9]);

	// Get the status of the controller's buttons via its HID report
	printf("\nReading PS3 Input Report...\n");
	CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
		HID_GET_REPORT, (HID_REPORT_TYPE_INPUT<<8)|0x01, 0, input_report, sizeof(input_report), 1000));
	switch(input_report[2]){	/** Direction pad plus start, select, and joystick buttons */
		case 0x01:
			printf("\tSELECT pressed\n");
			break;
		case 0x02:
			printf("\tLEFT 3 pressed\n");
			break;
		case 0x04:
			printf("\tRIGHT 3 pressed\n");
			break;
		case 0x08:
			printf("\tSTART presed\n");
			break;
		case 0x10:
			printf("\tUP pressed\n");
			break;
		case 0x20:
			printf("\tRIGHT pressed\n");
			break;
		case 0x40:
			printf("\tDOWN pressed\n");
			break;
		case 0x80:
			printf("\tLEFT pressed\n");
			break;
	}
	switch(input_report[3]){	/** Shapes plus top right and left buttons */
		case 0x01:
			printf("\tLEFT 2 pressed\n");
			break;
		case 0x02:
			printf("\tRIGHT 2 pressed\n");
			break;
		case 0x04:
			printf("\tLEFT 1 pressed\n");
			break;
		case 0x08:
			printf("\tRIGHT 1 presed\n");
			break;
		case 0x10:
			printf("\tTRIANGLE pressed\n");
			break;
		case 0x20:
			printf("\tCIRCLE pressed\n");
			break;
		case 0x40:
			printf("\tCROSS pressed\n");
			break;
		case 0x80:
			printf("\tSQUARE pressed\n");
			break;
	}
	printf("\tPS button: %d\n", input_report[4]);
	printf("\tLeft Analog (X,Y): (%d,%d)\n", input_report[6], input_report[7]);
	printf("\tRight Analog (X,Y): (%d,%d)\n", input_report[8], input_report[9]);
	printf("\tL2 Value: %d\tR2 Value: %d\n", input_report[18], input_report[19]);
	printf("\tL1 Value: %d\tR1 Value: %d\n", input_report[20], input_report[21]);
	printf("\tRoll (x axis): %d Yaw (y axis): %d Pitch (z axis) %d\n",
			//(((input_report[42] + 128) % 256) - 128),
			(int8_t)(input_report[42]),
			(int8_t)(input_report[44]),
			(int8_t)(input_report[46]));
	printf("\tAcceleration: %d\n\n", (int8_t)(input_report[48]));
	return 0;
}
Example #14
0
int test_device(uint16_t vid, uint16_t pid)
{
	libusb_device_handle *handle;
	libusb_device *dev;
	uint8_t bus, port_path[8];
	struct libusb_config_descriptor *conf_desc;
	const struct libusb_endpoint_descriptor *endpoint;
	int i, j, k, r;
	int iface, nb_ifaces;
#if defined(__linux)
	// Attaching/detaching the kernel driver is only relevant for Linux
	int iface_detached = -1;
#endif
	struct libusb_device_descriptor dev_desc;
	char string[128];
	uint8_t string_index[3];	// indexes of the string descriptors
	uint8_t endpoint_in = 0, endpoint_out = 0;	// default IN and OUT endpoints

	printf("Opening device...\n");
	handle = libusb_open_device_with_vid_pid(NULL, vid, pid);

	if (handle == NULL) {
		perr("  Failed.\n");
		return -1;
	}

	dev = libusb_get_device(handle);
	bus = libusb_get_bus_number(dev);
	r = libusb_get_port_path(NULL, dev, port_path, sizeof(port_path));
	if (r > 0) {
		printf("bus: %d, port path from HCD: %d", bus, port_path[0]);
		for (i=1; i<r; i++) {
			printf("->%d", port_path[i]);
		}
		printf("\n");
	}

	printf("\nReading device descriptor:\n");
	CALL_CHECK(libusb_get_device_descriptor(dev, &dev_desc));
	printf("            length: %d\n", dev_desc.bLength);
	printf("      device class: %d\n", dev_desc.bDeviceClass);
	printf("               S/N: %d\n", dev_desc.iSerialNumber);
	printf("           VID:PID: %04X:%04X\n", dev_desc.idVendor, dev_desc.idProduct);
	printf("         bcdDevice: %04X\n", dev_desc.bcdDevice);
	printf("   iMan:iProd:iSer: %d:%d:%d\n", dev_desc.iManufacturer, dev_desc.iProduct, dev_desc.iSerialNumber);
	printf("          nb confs: %d\n", dev_desc.bNumConfigurations);
	// Copy the string descriptors for easier parsing
	string_index[0] = dev_desc.iManufacturer;
	string_index[1] = dev_desc.iProduct;
	string_index[2] = dev_desc.iSerialNumber;

	printf("\nReading configuration descriptors:\n");
	CALL_CHECK(libusb_get_config_descriptor(dev, 0, &conf_desc));
	nb_ifaces = conf_desc->bNumInterfaces;
	printf("             nb interfaces: %d\n", nb_ifaces);
	for (i=0; i<conf_desc->bNumInterfaces; i++) {
		for (j=0; j<conf_desc->usb_interface[i].num_altsetting; j++) {
			printf("interface[%d].altsetting[%d]: num endpoints = %d\n",
				i, j, conf_desc->usb_interface[i].altsetting[j].bNumEndpoints);
			printf("   Class.SubClass.Protocol: %02X.%02X.%02X\n",
				conf_desc->usb_interface[i].altsetting[j].bInterfaceClass,
				conf_desc->usb_interface[i].altsetting[j].bInterfaceSubClass,
				conf_desc->usb_interface[i].altsetting[j].bInterfaceProtocol);
			if ( (conf_desc->usb_interface[i].altsetting[j].bInterfaceClass == LIBUSB_CLASS_MASS_STORAGE)
			  && ( (conf_desc->usb_interface[i].altsetting[j].bInterfaceSubClass == 0x01)
			  || (conf_desc->usb_interface[i].altsetting[j].bInterfaceSubClass == 0x06) )
			  && (conf_desc->usb_interface[i].altsetting[j].bInterfaceProtocol == 0x50) ) {
				// Mass storage devices that can use basic SCSI commands
				test_mode = USE_SCSI;
			}
			for (k=0; k<conf_desc->usb_interface[i].altsetting[j].bNumEndpoints; k++) {
				endpoint = &conf_desc->usb_interface[i].altsetting[j].endpoint[k];
				printf("       endpoint[%d].address: %02X\n", k, endpoint->bEndpointAddress);
				// Use the first bulk IN/OUT endpoints found as default for testing
				if ((endpoint->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == LIBUSB_TRANSFER_TYPE_BULK) {
					if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) {
						if (!endpoint_in)
							endpoint_in = endpoint->bEndpointAddress;
					} else {
						if (!endpoint_out)
							endpoint_out = endpoint->bEndpointAddress;
					}
				}
				printf("           max packet size: %04X\n", endpoint->wMaxPacketSize);
				printf("          polling interval: %02X\n", endpoint->bInterval);
			}
		}
	}
	libusb_free_config_descriptor(conf_desc);

	for (iface = 0; iface < nb_ifaces; iface++)
	{
		printf("\nClaiming interface %d...\n", iface);
		r = libusb_claim_interface(handle, iface);
#if defined(__linux)
		if ((r != LIBUSB_SUCCESS) && (iface == 0)) {
			// Maybe we need to detach the driver
			perr("   Failed. Trying to detach driver...\n");
			libusb_detach_kernel_driver(handle, iface);
			iface_detached = iface;
			printf("   Claiming interface again...\n");
			r = libusb_claim_interface(handle, iface);
		}
#endif
		if (r != LIBUSB_SUCCESS) {
			perr("   Failed.\n");
		}
	}

	printf("\nReading string descriptors:\n");
	for (i=0; i<3; i++) {
		if (string_index[i] == 0) {
			continue;
		}
		if (libusb_get_string_descriptor_ascii(handle, string_index[i], string, 128) >= 0) {
			printf("   String (0x%02X): \"%s\"\n", string_index[i], string);
		}
	}

	switch(test_mode) {
	case USE_PS3:
		CALL_CHECK(display_ps3_status(handle));
		break;
	case USE_XBOX:
		CALL_CHECK(display_xbox_status(handle));
		CALL_CHECK(set_xbox_actuators(handle, 128, 222));
		msleep(2000);
		CALL_CHECK(set_xbox_actuators(handle, 0, 0));
		break;
	case USE_SCSI:
		CALL_CHECK(test_mass_storage(handle, endpoint_in, endpoint_out));
	default:
		break;
	}

	printf("\n");
	for (iface = 0; iface<nb_ifaces; iface++) {
		printf("Releasing interface %d...\n", iface);
		libusb_release_interface(handle, iface);
	}

#if defined(__linux)
	if (iface_detached >= 0) {
		printf("Re-attaching kernel driver...\n");
		libusb_attach_kernel_driver(handle, iface_detached);
	}
#endif

	printf("Closing device...\n");
	libusb_close(handle);

	return 0;
}
Example #15
0
// The PS3 Controller is really a HID device that got its HID Report Descriptors
// removed by Sony
int display_ps3_status(libusb_device_handle *handle)
{
	int r;
	uint8_t input_report[49];

	//Ensure the controller is initialized and ready to send its state
	initalize_ps3(handle);

	printf("\nReading PS3 Input Report...\n");
	CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
		HID_GET_REPORT, (HID_REPORT_TYPE_INPUT<<8)|0x01, 0, input_report, 49, 1000));
	switch(input_report[2]){	/** Direction pad plus start, select, and joystick buttons */
		case 0x01:
			printf("\tSELECT pressed\n");
			break;
		case 0x02:
			printf("\tLEFT 3 pressed\n");
			break;
		case 0x04:
			printf("\tRIGHT 3 pressed\n");
			break;
		case 0x08:
			printf("\tSTART presed\n");
			break;
		case 0x10:
			printf("\tUP pressed\n");
			break;
		case 0x20:
			printf("\tRIGHT pressed\n");
			break;
		case 0x40:
			printf("\tDOWN pressed\n");
			break;
		case 0x80:
			printf("\tLEFT pressed\n");
			break;
	}
	switch(input_report[3]){	/** Shapes plus top right and left buttons */
		case 0x01:
			printf("\tLEFT 2 pressed\n");
			break;
		case 0x02:
			printf("\tRIGHT 2 pressed\n");
			break;
		case 0x04:
			printf("\tLEFT 1 pressed\n");
			break;
		case 0x08:
			printf("\tRIGHT 1 presed\n");
			break;
		case 0x10:
			printf("\tTRIANGLE pressed\n");
			break;
		case 0x20:
			printf("\tCIRCLE pressed\n");
			break;
		case 0x40:
			printf("\tCROSS pressed\n");
			break;
		case 0x80:
			printf("\tSQUARE pressed\n");
			break;
	}
	printf("\tPS button: %d\n", input_report[4]);
	printf("\tLeft Analog (X,Y): (%d,%d)\n", input_report[6], input_report[7]);
	printf("\tRight Analog (X,Y): (%d,%d)\n", input_report[8], input_report[9]);
	printf("\tL2 Value: %d\tR2 Value: %d\n", input_report[18], input_report[19]);
	printf("\tL1 Value: %d\tR1 Value: %d\n", input_report[20], input_report[21]);
	printf("\tRoll (x axis): %d Yaw (y axis): %d Pitch (z axis) %d\n",
			//(((input_report[42] + 128) % 256) - 128),
			(int8_t)(input_report[42]),
			(int8_t)(input_report[44]),
			(int8_t)(input_report[46]));
	printf("\tAcceleration: %d\n\n", (int8_t)(input_report[48]));
	return 0;
}
Example #16
0
int CallBuiltin()
{
   if (strcmp(token.data, "input") == 0) {
      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      if (lex_rc != TOKEN_LBRACKET) {
         errno = SYNTACTIC_ERROR;
         return EXIT_FAILURE;
      }

      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      if (lex_rc != TOKEN_RBRACKET) {
         errno = SYNTACTIC_ERROR;
         return EXIT_FAILURE;
      }

      if (T_HTableExport(id_table, "0ret", &write_var) == HASH_NOT_FOUND) {
         errno = INTERNAL_ERROR;
         return EXIT_FAILURE;
      }
      GenerateInstruction(IID_INPUT, write_var, NULL, NULL);
      return EXIT_SUCCESS;
   }

   else if (strcmp(token.data, "print") == 0) {
      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      if (lex_rc != TOKEN_LBRACKET) {
         errno = SYNTACTIC_ERROR;
         return EXIT_FAILURE;
      }
      if (T_HTableExport(id_table, "0ret", &write_var) == HASH_NOT_FOUND) {
         errno = INTERNAL_ERROR;
         return EXIT_FAILURE;
      }
      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      CALL_CHECK(item());

      GenerateInstruction(IID_PRINT, read_var, write_var, NULL);
      CALL_CHECK(print_list());
      return EXIT_SUCCESS;
   }

   else if (strcmp(token.data, "numeric") == 0) {
      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      if (lex_rc != TOKEN_LBRACKET) {
         errno = SYNTACTIC_ERROR;
         return EXIT_FAILURE;
      }
      if (T_HTableExport(id_table, "0ret", &write_var) == HASH_NOT_FOUND) {
         errno = INTERNAL_ERROR;
         return EXIT_FAILURE;
      }

      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      CALL_CHECK(item());

      GenerateInstruction(IID_NUMERIC, read_var, write_var, NULL);

      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      if (lex_rc != TOKEN_RBRACKET) {
         errno = SYNTACTIC_ERROR;
         return EXIT_FAILURE;
      }
      return EXIT_SUCCESS;
   }
   else if (strcmp(token.data, "typeOf") == 0) {
      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      if (lex_rc != TOKEN_LBRACKET) {
         errno = SYNTACTIC_ERROR;
         return EXIT_FAILURE;
      }
      if (T_HTableExport(id_table, "0ret", &write_var) == HASH_NOT_FOUND) {
         errno = INTERNAL_ERROR;
         return EXIT_FAILURE;
      }

      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      CALL_CHECK(item());

      GenerateInstruction(IID_TYPEOF, read_var, write_var, NULL);

      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      if (lex_rc != TOKEN_RBRACKET) {
         errno = SYNTACTIC_ERROR;
         return EXIT_FAILURE;
      }

      return EXIT_SUCCESS;
   }
   else if (strcmp(token.data, "len") == 0) {
      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      if (lex_rc != TOKEN_LBRACKET) {
         errno = SYNTACTIC_ERROR;
         return EXIT_FAILURE;
      }
      if (T_HTableExport(id_table, "0ret", &write_var) == HASH_NOT_FOUND) {
         errno = INTERNAL_ERROR;
         return EXIT_FAILURE;
      }

      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      CALL_CHECK(item());

      GenerateInstruction(IID_LEN, read_var, write_var, NULL);

      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      if (lex_rc != TOKEN_RBRACKET) {
         errno = SYNTACTIC_ERROR;
         return EXIT_FAILURE;
      }

      return EXIT_SUCCESS;
   }
   else if (strcmp(token.data, "find") == 0) {
      // TODO
   }
   else if (strcmp(token.data, "sort") == 0) {
      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      if (lex_rc != TOKEN_LBRACKET) {
         errno = SYNTACTIC_ERROR;
         return EXIT_FAILURE;
      }
      if (T_HTableExport(id_table, "0ret", &write_var) == HASH_NOT_FOUND) {
         errno = INTERNAL_ERROR;
         return EXIT_FAILURE;
      }

      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      CALL_CHECK(item());

      GenerateInstruction(IID_SORT, read_var, write_var, NULL);

      lex_rc = GetToken(&token);
      TOKEN_CHECK;
      if (lex_rc != TOKEN_RBRACKET) {
         errno = SYNTACTIC_ERROR;
         return EXIT_FAILURE;
      }

      return EXIT_SUCCESS;
   }

   return EXIT_SUCCESS;
}
Example #17
0
// generator listu z tokenu - vlastnost operace,zavorek,hodnoty/NID
int gener_list(Expr_list **L)
{
   T_Var *var;

   int waitfor=HODNOTA;
   int err;
   parentheses = 0;
   int par = 0;

   Expr_list *pom = NULL;
   Expr_list *last = NULL;
   *L = NULL; //inic listu

   if (debug) printf("list %d\n", *L);

   lex_rc = GetToken(&token);
   TOKEN_CHECK;
   while ((lex_rc != TOKEN_EOF) && (lex_rc != TOKEN_EOL)) {
      switch (lex_rc) {
      //-------------------op == HODNOTA---------------
      //--------------------CISLO---------------------
      case TOKEN_NUMBER:
         if (waitfor == HODNOTA) {
            if (debug) printf("%d cislo\n",lex_rc);

            CALL_CHECK(GenerateVariable(FALSE, &var));
            CDoubleToT_Var(atof(token.data),var);

            GENERSHORT; //viz synan.h

            waitfor=OPERACE;
         }
         else { errno=SYNTACTIC_ERROR; if (debug) printf("cekam operaci"); return EXIT_FAILURE; }
      break;
       //--------------------STRING---------------------
      case TOKEN_STRING:
         if (waitfor == HODNOTA) {
            if (debug) printf("%d string\n",lex_rc);

            CALL_CHECK(GenerateVariable(FALSE, &var));
            T_StringToT_Var(&token,var);

            GENERSHORT;

            waitfor=OPERACE;
         }
      else { errno=SYNTACTIC_ERROR; if (debug) printf("cekam operaci"); return EXIT_FAILURE; }
      break;
       //--------------------ID---------------------
      case TOKEN_IDENTIFICATOR:
         // 1 . nil , false , true
         if (waitfor == HODNOTA) {
            waitfor = OPERACE;
            if (strcmp(token.data, "nil") == 0) {
               if (debug) printf("%d nil\n", lex_rc);

               CALL_CHECK(GenerateVariable(FALSE, &var));
               var->NID = NID_NIL;
               GENERSHORT;
            }

            else if (strcmp(token.data, "false") == 0) {
               if (debug) printf("%d false\n", lex_rc);

               CALL_CHECK(GenerateVariable(FALSE, &var));

               var->NID = NID_BOOL;
               var->vals.b_val = FALSE;

               GENERSHORT;
            }

            else if (strcmp(token.data, "true") == 0) {
               if (debug) printf("%d true\n", lex_rc);

               CALL_CHECK(GenerateVariable(FALSE, &var));

               var->NID = NID_BOOL;
               var->vals.b_val = TRUE;

               GENERSHORT;
            }

            // 2. identifikatory - pouze ukladame ukaz na tabulku
            else if (T_HTableExport(id_table, token.data, &var) == HASH_FOUND) {
               if (debug) printf ("%s identifikator\n", token.data);

               if (var->NID == NID_FUNCTION) { if (debug) printf("funkceeeeeeeeeeeeee");}
               //CALL_CHECK(CallFunction());
               //lex_rc=GetToken(&token);
               //if ((lex_rc==EOL) || (lex_rc==EOF))
               //(*L)->value = nularet;

               GENERSHORT;
            }
         else { errno = UNDEFINED_VARIABLE; if (debug) printf("id neexistuje"); return EXIT_FAILURE; }
         }
         else { errno = SYNTACTIC_ERROR; if (debug) printf("cekam operaci"); return EXIT_FAILURE; }
      break;

      //--------------------zavorka---------------------
      //vaze se k hodnote v pravo - ceka se stale na hodnotu
      case TOKEN_LBRACKET :
         if (waitfor == HODNOTA) {
            par--;
            parentheses--;
            if (debug) printf("%d levaz\n", lex_rc);
         }
         else { errno = SYNTACTIC_ERROR; if (debug) printf("cekam operaci"); return EXIT_FAILURE; }
      break;
      //--------------------op == OPERACE ---------------------
      //--------------------zavorka---------------------
      case TOKEN_RBRACKET :
         if (waitfor == OPERACE) {
            last->zavorka++;
            parentheses++;
            if (debug) printf("%d pravaz\n", lex_rc);
         }
         else { errno = SYNTACTIC_ERROR; if (debug) printf("cekam hodnotu"); return EXIT_FAILURE; }
         if (parentheses > 0) { errno=SYNTACTIC_ERROR; if (debug) printf("cekam hodnotu"); return EXIT_FAILURE; }
      break;
      //--------------------operace ---------------------
      case TOKEN_DSTAR:
      case TOKEN_STAR:   case TOKEN_SLASH:
      case TOKEN_PLUS:   case TOKEN_MINUS:
      case TOKEN_EQUAL:  case TOKEN_UNEQUAL:   case TOKEN_BEQUAL:
      case TOKEN_SMEQUAL:case TOKEN_SMALLER:   case TOKEN_BIGGER:
         if (waitfor==OPERACE) {
            if (debug) printf("%d operace\n", lex_rc);
            last->operace = lex_rc;
            waitfor = HODNOTA;
         }
         else { errno=SYNTACTIC_ERROR; if (debug) printf("cekam hodnotu"); return EXIT_FAILURE; }
      break;
      //------------------------PRAVAZAVORKA----------------------------
      case TOKEN_LSBRACKET:
         if (waitfor==OPERACE) {
            if (debug) printf("%d leva hranata zavorka\n",lex_rc);
            if ((*L!=NULL)&&((*L)->next==NULL)) {
               T_Var *svar;
               T_Var *svar2;

               if ((err = expr_string(&svar ,&svar2)) != EXIT_SUCCESS) return EXIT_FAILURE;
               else {
                  if ((svar!=NULL)||(svar2!=NULL)) {

                     CALL_CHECK(GenerateVariable(FALSE, &read_var));

                     read_var->NID=NID_STRING;
                     GenerateInstruction(IID_COPY, read_var, (*L)->value, NULL);

                     GenerateInstruction(IID_CUT, read_var, svar, svar2);

                     (*L)->value=read_var;
                  }
                  if ((lex_rc == TOKEN_EOL) || (lex_rc == TOKEN_EOF)) return EXIT_SUCCESS;
               }
            }
         }
      else { errno=SYNTACTIC_ERROR; if (debug) printf("cekam hodnotu"); return EXIT_FAILURE;}
      break;

      case TOKEN_BROKEN : if (debug) printf("%d lex chyba kdyz token brken chyba\n",lex_rc); return EXIT_FAILURE;
      default : { errno = SYNTACTIC_ERROR; if (debug) printf("%d syn chyba kdyz token brken chyba\n", lex_rc); return EXIT_FAILURE;} break;
      }

      lex_rc = GetToken(&token);
      TOKEN_CHECK;
   }
   if (*L == NULL) { errno = SYNTACTIC_ERROR; if (debug) printf("prazdno"); return EXIT_FAILURE;}
   if (parentheses != 0) { errno = SYNTACTIC_ERROR; if (debug) printf("spatne zavorky"); return EXIT_FAILURE;}
   return EXIT_SUCCESS;
}
Example #18
0
int CallFunction()
{
   // Saves a name of called function, currently located in token
   T_String f_name;
   T_StringInit(&f_name);
   T_StringCopy(&f_name, &token);

   // Opening bracket must follow
   lex_rc = GetToken(&token);
   TOKEN_CHECK;
   if (lex_rc == TOKEN_LBRACKET) {
      T_HashTable *tmp_table = malloc(sizeof(T_HashTable));
      T_Var *f_var = malloc(sizeof(T_Var));

      if (tmp_table == NULL || f_var == NULL) {
         free(tmp_table);
         free(f_var);
         errno = INTERNAL_ERROR;
         return EXIT_FAILURE;
      }

      T_HTableInit(tmp_table);
      T_VarInit(f_var);

      // Adds current id_table to table list and change its pointer
      T_IdListAppend(table_list, id_table);
      id_table = tmp_table;

      T_Var *var = malloc(sizeof(T_Var));
      PTR_CHECK(var);

      T_VarInit(var);
      var->NID = NID_CONST;

      char *reserved[] = {"as", "def", "directive", "export", "from", "import", "launch", "load", "macro", "input", "print", "numeric", "typeOf", "len", ""};

      T_HTableInit(id_table);

      for (int i = 0; strcmp(reserved[i], "") != 0; i++)
         T_HTableInsert(id_table, reserved[i], var);

      free(var);

      // Gets all function parameters into hash table
      if (write_var->vals.args.size > 0) {
         lex_rc = GetToken(&token);
         TOKEN_CHECK;
         CALL_CHECK(item());
         CALL_CHECK(T_HTableInsert(id_table, write_var->vals.args.first->data.data, read_var));
         CALL_CHECK(it_list(write_var->vals.args.first->next));
      }
      else {
         lex_rc = GetToken(&token);
         TOKEN_CHECK;
         if (lex_rc != TOKEN_RBRACKET) {
            errno = SYNTACTIC_ERROR;
            return EXIT_FAILURE;
         }
      }

      // Calls the function with the saved name
      T_StringToT_Var(&f_name, f_var);
      GenerateInstruction(IID_JUMP, f_var, NULL, NULL);

      // Generates a lable as a return point
      GenerateVariable(TRUE, &write_var);
      GenerateInstruction(IID_LABEL, write_var, NULL, NULL);
      T_LabelListAppend(cal_list, inst_list->last);

      free(f_var);

      return EXIT_SUCCESS;
   }

   errno = SYNTACTIC_ERROR;
   return EXIT_FAILURE;
}
Example #19
0
int stat(int mode)
{
   DEBUG_STAT;
   if (lex_rc == TOKEN_EOL)
      return EXIT_SUCCESS;

   if (lex_rc == TOKEN_IDENTIFICATOR) {
      if (T_HTableSearch(kw_table, token.data) != NULL) { // Keyword
         DEBUG_STAT_KEYWORD;
         return keyword(mode);
      }

      if (T_HTableSearch(builtin_table, token.data) != NULL)
         return CallBuiltin();

      int htable_rc = T_HTableExport(id_table, token.data, &write_var);
      if (htable_rc == HASH_FOUND && write_var->NID == NID_FUNCTION) { // Known function
         DEBUG_STAT_FUNCTION;
         CALL_CHECK(CallFunction());
         DEBUG_STAT_END;
         return EXIT_SUCCESS;
      }
      else { // Variable
         DEBUG_STAT_VAR;
         if (htable_rc == HASH_NOT_FOUND) {
            // Variable doesn't exist, create it as undefined and save into table
            T_Var *tmp_var = malloc(sizeof(T_Var));
            PTR_CHECK(tmp_var);

            T_VarInit(tmp_var);

            if (T_HTableInsert(id_table, token.data, tmp_var) == EXIT_FAILURE) {
               free(tmp_var);
               return EXIT_FAILURE;
            }
            free(tmp_var);
         }

         // Assign write_var pointer to newly created variable
         if (T_HTableExport(id_table, token.data, &write_var) == HASH_NOT_FOUND) {
            errno = INTERNAL_ERROR;
            return EXIT_FAILURE;
         }

         // Next expected token is assignment character
         lex_rc = GetToken(&token);
         TOKEN_CHECK;
         if (lex_rc == TOKEN_ASSIGN) {
            CALL_CHECK(expr());

            // write_var points to variable from the table, read_var points to the expression result
            GenerateInstruction(IID_COPY, write_var, read_var, NULL);

            DEBUG_STAT_END;
            return EXIT_SUCCESS;
         }
      }
   }

   errno = SYNTACTIC_ERROR;
   return EXIT_FAILURE;
}
Example #20
0
// Mass Storage device to test bulk transfers (non destructive test)
static int test_mass_storage(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t endpoint_out)
{
	int r, size;
	uint8_t lun;
	uint32_t expected_tag;
	uint32_t i, max_lba, block_size;
	double device_size;
	uint8_t cdb[16];	// SCSI Command Descriptor Block
	uint8_t buffer[64];
	char vid[9], pid[9], rev[5];
	unsigned char *data;
	FILE *fd;

	printf("Reading Max LUN:\n");
	r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
		BOMS_GET_MAX_LUN, 0, 0, &lun, 1, 1000);
	// Some devices send a STALL instead of the actual value.
	// In such cases we should set lun to 0.
	if (r == 0) {
		lun = 0;
	} else if (r < 0) {
		perr("   Failed: %s", libusb_strerror((enum libusb_error)r));
	}
	printf("   Max LUN = %d\n", lun);

	// Send Inquiry
	printf("Sending Inquiry:\n");
	memset(buffer, 0, sizeof(buffer));
	memset(cdb, 0, sizeof(cdb));
	cdb[0] = 0x12;	// Inquiry
	cdb[4] = INQUIRY_LENGTH;

	send_mass_storage_command(handle, endpoint_out, lun, cdb, LIBUSB_ENDPOINT_IN, INQUIRY_LENGTH, &expected_tag);
	CALL_CHECK(libusb_bulk_transfer(handle, endpoint_in, (unsigned char*)&buffer, INQUIRY_LENGTH, &size, 1000));
	printf("   received %d bytes\n", size);
	// The following strings are not zero terminated
	for (i=0; i<8; i++) {
		vid[i] = buffer[8+i];
		pid[i] = buffer[16+i];
		rev[i/2] = buffer[32+i/2];	// instead of another loop
	}
	vid[8] = 0;
	pid[8] = 0;
	rev[4] = 0;
	printf("   VID:PID:REV \"%8s\":\"%8s\":\"%4s\"\n", vid, pid, rev);
	if (get_mass_storage_status(handle, endpoint_in, expected_tag) == -2) {
		get_sense(handle, endpoint_in, endpoint_out);
	}

	// Read capacity
	printf("Reading Capacity:\n");
	memset(buffer, 0, sizeof(buffer));
	memset(cdb, 0, sizeof(cdb));
	cdb[0] = 0x25;	// Read Capacity

	send_mass_storage_command(handle, endpoint_out, lun, cdb, LIBUSB_ENDPOINT_IN, READ_CAPACITY_LENGTH, &expected_tag);
	CALL_CHECK(libusb_bulk_transfer(handle, endpoint_in, (unsigned char*)&buffer, READ_CAPACITY_LENGTH, &size, 1000));
	printf("   received %d bytes\n", size);
	max_lba = be_to_int32(&buffer[0]);
	block_size = be_to_int32(&buffer[4]);
	device_size = ((double)(max_lba+1))*block_size/(1024*1024*1024);
	printf("   Max LBA: %08X, Block Size: %08X (%.2f GB)\n", max_lba, block_size, device_size);
	if (get_mass_storage_status(handle, endpoint_in, expected_tag) == -2) {
		get_sense(handle, endpoint_in, endpoint_out);
	}

	// coverity[tainted_data]
	data = (unsigned char*) calloc(1, block_size);
	if (data == NULL) {
		perr("   unable to allocate data buffer\n");
		return -1;
	}

	// Send Read
	printf("Attempting to read %d bytes:\n", block_size);
	memset(cdb, 0, sizeof(cdb));

	cdb[0] = 0x28;	// Read(10)
	cdb[8] = 0x01;	// 1 block

	send_mass_storage_command(handle, endpoint_out, lun, cdb, LIBUSB_ENDPOINT_IN, block_size, &expected_tag);
	libusb_bulk_transfer(handle, endpoint_in, data, block_size, &size, 5000);
	printf("   READ: received %d bytes\n", size);
	if (get_mass_storage_status(handle, endpoint_in, expected_tag) == -2) {
		get_sense(handle, endpoint_in, endpoint_out);
	} else {
		display_buffer_hex(data, size);
		if ((binary_dump) && ((fd = fopen(binary_name, "w")) != NULL)) {
			if (fwrite(data, 1, (size_t)size, fd) != (unsigned int)size) {
				perr("   unable to write binary data\n");
			}
			fclose(fd);
		}
	}
	free(data);

	return 0;
}
Example #21
0
int keyword(int mode)
{
   DEBUG_KW;

   if (lex_rc == TOKEN_IDENTIFICATOR) {
      if (strcmp(token.data,"if") == 0 ) {
         DEBUG_KW_IF;

         CALL_CHECK(expr());

         GenerateVariable(TRUE, &write_var);
         GenerateInstruction(IID_BRAFAL, read_var, write_var, NULL);

         CALL_CHECK(if_state(MODE_OUTSIDE, write_var));

         DEBUG_KW_END;
         return EXIT_SUCCESS;
      }

      else if (strcmp(token.data,"while") == 0 ) {
         DEBUG_KW_WHILE;

         T_Var *start_lbl;
         GenerateVariable(TRUE, &start_lbl);
         GenerateInstruction(IID_LABEL, start_lbl, NULL, NULL);
         T_LabelListAppend(lbl_list, inst_list->last);

         CALL_CHECK(expr());

         GenerateVariable(TRUE, &write_var);
         GenerateInstruction(IID_BRAFAL, read_var, write_var, NULL);

         CALL_CHECK(while_state(start_lbl, write_var));

         DEBUG_KW_END;
         return EXIT_SUCCESS;
      }

      else if (strcmp(token.data,"function") == 0 && (mode == MODE_OUTSIDE)) {
         DEBUG_KW_FUNCTION;

         lex_rc = GetToken(&token);
//         TOKEN_CHECK;
//         if (lex_rc == TOKEN_IDENTIFICATOR) {
//            lex_rc = GetToken(&token);
//            TOKEN_CHECK;
//            if (lex_rc == TOKEN_LBRACKET) {
//
//               f_item();
//               CALL_CHECK(f_());
//
//               lex_rc = GetToken(&token);
//               TOKEN_CHECK;
//
//               DEBUG_KW_END;
//               return EXIT_SUCCESS;
//            }
//         }
      }
   }

   errno = SYNTACTIC_ERROR;
   return EXIT_FAILURE;
}
Example #22
0
static int test_device(uint16_t vid, uint16_t pid)
{
	libusb_device_handle *handle;
	libusb_device *dev;
	uint8_t bus, port_path[8];
	struct libusb_bos_descriptor *bos_desc;
	struct libusb_config_descriptor *conf_desc;
	const struct libusb_endpoint_descriptor *endpoint;
	int i, j, k, r;
	int iface, nb_ifaces, first_iface = -1;
	struct libusb_device_descriptor dev_desc;
	const char* speed_name[5] = { "Unknown", "1.5 Mbit/s (USB LowSpeed)", "12 Mbit/s (USB FullSpeed)",
		"480 Mbit/s (USB HighSpeed)", "5000 Mbit/s (USB SuperSpeed)"};
	char string[128];
	uint8_t string_index[3];	// indexes of the string descriptors
	uint8_t endpoint_in = 0, endpoint_out = 0;	// default IN and OUT endpoints

	printf("Opening device %04X:%04X...\n", vid, pid);
	handle = libusb_open_device_with_vid_pid(NULL, vid, pid);

	if (handle == NULL) {
		perr("  Failed.\n");
		return -1;
	}

	dev = libusb_get_device(handle);
	bus = libusb_get_bus_number(dev);
	if (extra_info) {
		r = libusb_get_port_numbers(dev, port_path, sizeof(port_path));
		if (r > 0) {
			printf("\nDevice properties:\n");
			printf("        bus number: %d\n", bus);
			printf("         port path: %d", port_path[0]);
			for (i=1; i<r; i++) {
				printf("->%d", port_path[i]);
			}
			printf(" (from root hub)\n");
		}
		r = libusb_get_device_speed(dev);
		if ((r<0) || (r>4)) r=0;
		printf("             speed: %s\n", speed_name[r]);
	}

	printf("\nReading device descriptor:\n");
	CALL_CHECK(libusb_get_device_descriptor(dev, &dev_desc));
	printf("            length: %d\n", dev_desc.bLength);
	printf("      device class: %d\n", dev_desc.bDeviceClass);
	printf("               S/N: %d\n", dev_desc.iSerialNumber);
	printf("           VID:PID: %04X:%04X\n", dev_desc.idVendor, dev_desc.idProduct);
	printf("         bcdDevice: %04X\n", dev_desc.bcdDevice);
	printf("   iMan:iProd:iSer: %d:%d:%d\n", dev_desc.iManufacturer, dev_desc.iProduct, dev_desc.iSerialNumber);
	printf("          nb confs: %d\n", dev_desc.bNumConfigurations);
	// Copy the string descriptors for easier parsing
	string_index[0] = dev_desc.iManufacturer;
	string_index[1] = dev_desc.iProduct;
	string_index[2] = dev_desc.iSerialNumber;

	printf("\nReading BOS descriptor: ");
	if (libusb_get_bos_descriptor(handle, &bos_desc) == LIBUSB_SUCCESS) {
		printf("%d caps\n", bos_desc->bNumDeviceCaps);
		for (i = 0; i < bos_desc->bNumDeviceCaps; i++)
			print_device_cap(bos_desc->dev_capability[i]);
		libusb_free_bos_descriptor(bos_desc);
	} else {
		printf("no descriptor\n");
	}

	printf("\nReading first configuration descriptor:\n");
	CALL_CHECK(libusb_get_config_descriptor(dev, 0, &conf_desc));
	nb_ifaces = conf_desc->bNumInterfaces;
	printf("             nb interfaces: %d\n", nb_ifaces);
	if (nb_ifaces > 0)
		first_iface = conf_desc->usb_interface[0].altsetting[0].bInterfaceNumber;
	for (i=0; i<nb_ifaces; i++) {
		printf("              interface[%d]: id = %d\n", i,
			conf_desc->usb_interface[i].altsetting[0].bInterfaceNumber);
		for (j=0; j<conf_desc->usb_interface[i].num_altsetting; j++) {
			printf("interface[%d].altsetting[%d]: num endpoints = %d\n",
				i, j, conf_desc->usb_interface[i].altsetting[j].bNumEndpoints);
			printf("   Class.SubClass.Protocol: %02X.%02X.%02X\n",
				conf_desc->usb_interface[i].altsetting[j].bInterfaceClass,
				conf_desc->usb_interface[i].altsetting[j].bInterfaceSubClass,
				conf_desc->usb_interface[i].altsetting[j].bInterfaceProtocol);
			if ( (conf_desc->usb_interface[i].altsetting[j].bInterfaceClass == LIBUSB_CLASS_MASS_STORAGE)
			  && ( (conf_desc->usb_interface[i].altsetting[j].bInterfaceSubClass == 0x01)
			  || (conf_desc->usb_interface[i].altsetting[j].bInterfaceSubClass == 0x06) )
			  && (conf_desc->usb_interface[i].altsetting[j].bInterfaceProtocol == 0x50) ) {
				// Mass storage devices that can use basic SCSI commands
				test_mode = USE_SCSI;
			}
			for (k=0; k<conf_desc->usb_interface[i].altsetting[j].bNumEndpoints; k++) {
				struct libusb_ss_endpoint_companion_descriptor *ep_comp = NULL;
				endpoint = &conf_desc->usb_interface[i].altsetting[j].endpoint[k];
				printf("       endpoint[%d].address: %02X\n", k, endpoint->bEndpointAddress);
				// Use the first interrupt or bulk IN/OUT endpoints as default for testing
				if ((endpoint->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) & (LIBUSB_TRANSFER_TYPE_BULK | LIBUSB_TRANSFER_TYPE_INTERRUPT)) {
					if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) {
						if (!endpoint_in)
							endpoint_in = endpoint->bEndpointAddress;
					} else {
						if (!endpoint_out)
							endpoint_out = endpoint->bEndpointAddress;
					}
				}
				printf("           max packet size: %04X\n", endpoint->wMaxPacketSize);
				printf("          polling interval: %02X\n", endpoint->bInterval);
				libusb_get_ss_endpoint_companion_descriptor(NULL, endpoint, &ep_comp);
				if (ep_comp) {
					printf("                 max burst: %02X   (USB 3.0)\n", ep_comp->bMaxBurst);
					printf("        bytes per interval: %04X (USB 3.0)\n", ep_comp->wBytesPerInterval);
					libusb_free_ss_endpoint_companion_descriptor(ep_comp);
				}
			}
		}
	}
	libusb_free_config_descriptor(conf_desc);

	libusb_set_auto_detach_kernel_driver(handle, 1);
	for (iface = 0; iface < nb_ifaces; iface++)
	{
		printf("\nClaiming interface %d...\n", iface);
		r = libusb_claim_interface(handle, iface);
		if (r != LIBUSB_SUCCESS) {
			perr("   Failed.\n");
		}
	}

	printf("\nReading string descriptors:\n");
	for (i=0; i<3; i++) {
		if (string_index[i] == 0) {
			continue;
		}
		if (libusb_get_string_descriptor_ascii(handle, string_index[i], (unsigned char*)string, 128) >= 0) {
			printf("   String (0x%02X): \"%s\"\n", string_index[i], string);
		}
	}
	// Read the OS String Descriptor
	if (libusb_get_string_descriptor_ascii(handle, 0xEE, (unsigned char*)string, 128) >= 0) {
		printf("   String (0x%02X): \"%s\"\n", 0xEE, string);
		// If this is a Microsoft OS String Descriptor,
		// attempt to read the WinUSB extended Feature Descriptors
		if (strncmp(string, "MSFT100", 7) == 0)
			read_ms_winsub_feature_descriptors(handle, string[7], first_iface);
	}

	switch(test_mode) {
	case USE_PS3:
		CALL_CHECK(display_ps3_status(handle));
		break;
	case USE_XBOX:
		CALL_CHECK(display_xbox_status(handle));
		CALL_CHECK(set_xbox_actuators(handle, 128, 222));
		msleep(2000);
		CALL_CHECK(set_xbox_actuators(handle, 0, 0));
		break;
	case USE_HID:
		test_hid(handle, endpoint_in);
		break;
	case USE_SCSI:
		CALL_CHECK(test_mass_storage(handle, endpoint_in, endpoint_out));
	case USE_GENERIC:
		break;
	}

	printf("\n");
	for (iface = 0; iface<nb_ifaces; iface++) {
		printf("Releasing interface %d...\n", iface);
		libusb_release_interface(handle, iface);
	}

	printf("Closing device...\n");
	libusb_close(handle);

	return 0;
}
Example #23
0
// Mass Storage device to test bulk transfers (non destructive test)
static int test_mass_storage(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t endpoint_out)
{
	int r, size;
	uint8_t lun;
	uint32_t expected_tag;
	uint32_t i, max_lba, block_size;
	double device_size;
	uint8_t cdb[16];	// SCSI Command Descriptor Block
	uint8_t buffer[64];
	char vid[9], pid[9], rev[5];
	
	debug("Reading Max LUN:\n");
	r = sceUsbdControlTransfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
		BOMS_GET_MAX_LUN, 0, 0, &lun, 1, 1000);
	// Some devices send a STALL instead of the actual value.
	// In such cases we should set lun to 0.
	if (r == 0) {
		lun = 0;
	} else if (r < 0) {
		debug("   Failed\n");
	}
	debug("   Max LUN = %d\n", lun);

	// Send Inquiry
	debug("Sending Inquiry:\n");
	memset(buffer, 0, sizeof(buffer));
	memset(cdb, 0, sizeof(cdb));
	cdb[0] = 0x12;	// Inquiry
	cdb[4] = INQUIRY_LENGTH;

	send_mass_storage_command(handle, endpoint_out, lun, cdb, LIBUSB_ENDPOINT_IN, INQUIRY_LENGTH, &expected_tag);
	CALL_CHECK(sceUsbdBulkTransfer(handle, endpoint_in, (unsigned char*)&buffer, INQUIRY_LENGTH, &size, 1000));
	debug("   received %d bytes\n", size);
	// The following strings are not zero terminated
	for (i=0; i<8; i++) {
		vid[i] = buffer[8+i];
		pid[i] = buffer[16+i];
		rev[i/2] = buffer[32+i/2];	// instead of another loop
	}
	vid[8] = 0;
	pid[8] = 0;
	rev[4] = 0;
	debug("   VID:PID:REV \"%8s\":\"%8s\":\"%4s\"\n", vid, pid, rev);
	if (get_mass_storage_status(handle, endpoint_in, expected_tag) == -2) {
		get_sense(handle, endpoint_in, endpoint_out);
	}

	// Read capacity
	debug("Reading Capacity:\n");
	memset(buffer, 0, sizeof(buffer));
	memset(cdb, 0, sizeof(cdb));
	cdb[0] = 0x25;	// Read Capacity

	send_mass_storage_command(handle, endpoint_out, lun, cdb, LIBUSB_ENDPOINT_IN, READ_CAPACITY_LENGTH, &expected_tag);
	CALL_CHECK(sceUsbdBulkTransfer(handle, endpoint_in, (unsigned char*)&buffer, READ_CAPACITY_LENGTH, &size, 1000));
	debug("   received %d bytes\n", size);
	max_lba = be_to_int32(&buffer[0]);
	block_size = be_to_int32(&buffer[4]);
	device_size = ((double)(max_lba+1))*block_size/(1024*1024*1024);
	debug("   Max LBA: %08X, Block Size: %08X (%.2f GB)\n", max_lba, block_size, device_size);
	if (get_mass_storage_status(handle, endpoint_in, expected_tag) == -2) {
		get_sense(handle, endpoint_in, endpoint_out);
	}

	// coverity[tainted_data]
	//data = (unsigned char*) calloc(1, block_size);
	//if (data == NULL) {
	//	debug("   unable to allocate data buffer\n");
	//	return -1;
	//}
	
	// Send Read
	debug("Attempting to read %d bytes:\n", block_size);
	memset(cdb, 0, sizeof(cdb));

	cdb[0] = 0x28;	// Read(10)
	//cdb[8] = 0x01;	// 1 block
	cdb[8] = sizeof(cart) / block_size;	// blocks

	//send_mass_storage_command(handle, endpoint_out, lun, cdb, LIBUSB_ENDPOINT_IN, block_size, &expected_tag);
	send_mass_storage_command(handle, endpoint_out, lun, cdb, LIBUSB_ENDPOINT_IN, sizeof(cart), &expected_tag);
	//sceUsbdBulkTransfer(handle, endpoint_in, cart, block_size, &size, 5000);
	sceUsbdBulkTransfer(handle, endpoint_in, cart, sizeof(cart), &size, 5000);
	debug("   READ: received %d bytes\n", size);
	if (get_mass_storage_status(handle, endpoint_in, expected_tag) == -2) {
		get_sense(handle, endpoint_in, endpoint_out);
	}// else {
		//display_buffer_hex(data, size);
	//}
	//free(data);

	return 0;
}