Exemplo n.º 1
0
/** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
 *  control requests that are not handled internally by the USB library (including the CDC control commands,
 *  which are all issued via the control endpoint), so that they can be handled appropriately for the application.
 */
void EVENT_USB_UnhandledControlPacket(void)
{
	uint8_t* LineCodingData = (uint8_t*)&LineCoding;

	/* Process CDC specific control requests */
	switch (USB_ControlRequest.bRequest)
	{
		case REQ_GetLineEncoding:
			if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
			{	
				/* Acknowledge the SETUP packet, ready for data transfer */
				Endpoint_ClearSETUP();

				/* Write the line coding data to the control endpoint */
				Endpoint_Write_Control_Stream_LE(LineCodingData, sizeof(LineCoding));
				
				/* Finalize the stream transfer to send the last packet or clear the host abort */
				Endpoint_ClearOUT();
			}
			
			break;
		case REQ_SetLineEncoding:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				/* Acknowledge the SETUP packet, ready for data transfer */
				Endpoint_ClearSETUP();

				/* Read the line coding data in from the host into the global struct */
				Endpoint_Read_Control_Stream_LE(LineCodingData, sizeof(LineCoding));

				/* Finalize the stream transfer to clear the last packet from the host */
				Endpoint_ClearIN();
				
				/* Reconfigure the USART with the new settings */
				ReconfigureUSART();
			}
	
			break;
		case REQ_SetControlLineState:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{				
				/* Acknowledge the SETUP packet, ready for data transfer */
				Endpoint_ClearSETUP();
				
				/* NOTE: Here you can read in the line state mask from the host, to get the current state of the output handshake
				         lines. The mask is read in from the wValue parameter in USB_ControlRequest, and can be masked against the
						 CONTROL_LINE_OUT_* masks to determine the RTS and DTR line states using the following code:
				*/

				/* Acknowledge status stage */
				while (!(Endpoint_IsINReady()));
				Endpoint_ClearIN();
			}
	
			break;
	}
}
Exemplo n.º 2
0
/**
 * \brief SERIAL request
 *
 * read the new serial number in ascii from the USB and copy it into the
 * 
 */
void	process_serial() {
	Endpoint_ClearSETUP();
	unsigned char	l = USB_ControlRequest.wLength;
	if (l <= 7) {
		Endpoint_Read_Control_Stream_LE(serialbuffer, l);
	}
	Endpoint_ClearIN();
	serialbuffer[l] = '\0';
	newserial = 1;
}
Exemplo n.º 3
0
void CDC_Device_ProcessControlRequest(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
	if (!(Endpoint_IsSETUPReceived()))
	  return;
	  
	if (USB_ControlRequest.wIndex != CDCInterfaceInfo->Config.ControlInterfaceNumber)
	  return;

	switch (USB_ControlRequest.bRequest)
	{
		case CDC_REQ_GetLineEncoding:
			if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();
				Endpoint_Write_Control_Stream_LE(&CDCInterfaceInfo->State.LineEncoding, sizeof(CDCInterfaceInfo->State.LineEncoding));
				Endpoint_ClearOUT();
			}
			
			break;
		case CDC_REQ_SetLineEncoding:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();
				Endpoint_Read_Control_Stream_LE(&CDCInterfaceInfo->State.LineEncoding, sizeof(CDCInterfaceInfo->State.LineEncoding));
				Endpoint_ClearIN();

				EVENT_CDC_Device_LineEncodingChanged(CDCInterfaceInfo);
			}
	
			break;
		case CDC_REQ_SetControlLineState:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{				
				Endpoint_ClearSETUP();
				Endpoint_ClearStatusStage();

				CDCInterfaceInfo->State.ControlLineStates.HostToDevice = USB_ControlRequest.wValue;

				EVENT_CDC_Device_ControLineStateChanged(CDCInterfaceInfo);
			}
	
			break;
		case CDC_REQ_SendBreak:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{				
				Endpoint_ClearSETUP();
				Endpoint_ClearStatusStage();

				EVENT_CDC_Device_BreakSent(CDCInterfaceInfo, (uint8_t)USB_ControlRequest.wValue);
			}

			break;
	}
}
Exemplo n.º 4
0
//LUFA USB Event Processer Functions
void process_SET_GOAL(){
	uint16_t newGoal[6];
	/* Accept command */
	Endpoint_ClearSETUP();
	/* read data from endpoint */
	Endpoint_Read_Control_Stream_LE(newGoal, 16);
	/* and mark the whole request as successful: */
	Endpoint_ClearStatusStage();
	/* process command parameters: */
	//Call changeGoal(newGoal);
}
Exemplo n.º 5
0
static void do_serialnumber(int direction, unsigned int vWalue)
{
	uint32_t snum;

	if (direction == ENDPOINT_DIR_OUT) {
		Endpoint_Read_Control_Stream_LE(&snum, sizeof(snum));
		eeprom_write_dword(&serialno, snum);
	} else if (direction == ENDPOINT_DIR_IN) {
		snum = eeprom_read_dword(&serialno);
		Endpoint_Write_Control_Stream_LE(&snum, sizeof(snum));
	}
}
Exemplo n.º 6
0
static void do_frequency(int direction, unsigned int wValue)
{
	uint32_t freq;

	if (direction == ENDPOINT_DIR_OUT) {
		Endpoint_Read_Control_Stream_LE(&freq, sizeof(freq));
		conf.tx_freq = freq;
		conf.rx_freq = freq;
		conf_set_reconf();
	} else if (direction == ENDPOINT_DIR_IN) {
		Endpoint_Write_Control_Stream_LE(&conf.rx_freq, sizeof(conf.rx_freq)); \
	}
}
Exemplo n.º 7
0
/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
 *  the device from the USB host before passing along unhandled control requests to the library for processing
 *  internally.
 */
int EVENT_USB_Device_ControlRequest(void)
{
	/* Ignore any requests that aren't directed to the CDC interface */
	if ((USB_ControlRequest.bmRequestType & (CONTROL_REQTYPE_TYPE | CONTROL_REQTYPE_RECIPIENT)) !=
	    (REQTYPE_CLASS | REQREC_INTERFACE))
	{
		return 0;
	}

	/* Activity - toggle indicator LEDs */
	LEDs_ToggleLEDs(LEDS_LED1 | LEDS_LED2);

	/* Process CDC specific control requests */
	switch (USB_ControlRequest.bRequest)
	{
		case CDC_REQ_GetLineEncoding:
			if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();

				/* Write the line coding data to the control endpoint */
				Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
				Endpoint_ClearOUT();
				return 1;
			}

			break;
		case CDC_REQ_SetLineEncoding:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();

				/* Read the line coding data in from the host into the global struct */
				Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
				Endpoint_ClearIN();
				return 1;
			}

			break;
        case CDC_REQ_SetControlLineState:
	        if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
	        {
	            Endpoint_ClearSETUP();
	            Endpoint_ClearStatusStage();
	            return 1;
	        }

	        break;
	}
	return 0;
}
Exemplo n.º 8
0
/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
 *  the device from the USB host before passing along unhandled control requests to the library for processing
 *  internally.
 */
void EVENT_USB_Device_ControlRequest(void)
{
  static unsigned char buffer[MAX_CONTROL_TRANSFER_SIZE];

  /* Handle HID Class specific requests */
	switch (USB_ControlRequest.bRequest)
	{
    case REQ_GetReport:
      if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
      {
        uint8_t reportType = USB_ControlRequest.wValue >> 8;
        uint8_t reportId = USB_ControlRequest.wValue & 0xff;

        if(reportType == REPORT_TYPE_FEATURE)
        {
          switch(reportId)
          {
            default:
              Serial_SendByte(BYTE_DEBUG);
              Serial_SendByte(BYTE_LEN_1_BYTE);
              Serial_SendByte(reportId);
              break;
          }
        }
      }

      break;
    case REQ_SetReport:
      if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
      {
        Endpoint_ClearSETUP();
        Endpoint_Read_Control_Stream_LE(buffer, USB_ControlRequest.wLength);
        Endpoint_ClearIN();

        uint8_t reportType = USB_ControlRequest.wValue >> 8;
        uint8_t reportId = USB_ControlRequest.wValue & 0xff;

        if(reportType == REPORT_TYPE_FEATURE)
        {
          switch(reportId)
          {
            default:
              Serial_SendByte(BYTE_DEBUG);
              Serial_SendByte(sizeof(USB_ControlRequest) + (USB_ControlRequest.wLength & 0xFF));
              Serial_SendData(&USB_ControlRequest, sizeof(USB_ControlRequest));
              Serial_SendData(buffer, USB_ControlRequest.wLength);
              break;
          }
        }
      }
Exemplo n.º 9
0
/** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific
 *  control requests that are not handled internally by the USB library (including the CDC control commands,
 *  which are all issued via the control endpoint), so that they can be handled appropriately for the application.
 */
void EVENT_USB_Device_UnhandledControlRequest(void)
{
	/* Determine which interface's Line Coding data is being set from the wIndex parameter */
	uint8_t* LineEncodingData = (USB_ControlRequest.wIndex == 0) ? (uint8_t*)&LineEncoding1 : (uint8_t*)&LineEncoding2;

	/* Process CDC specific control requests */
	switch (USB_ControlRequest.bRequest)
	{
		case REQ_GetLineEncoding:
			if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
			{	
				/* Acknowledge the SETUP packet, ready for data transfer */
				Endpoint_ClearSETUP();

				/* Write the line coding data to the control endpoint */
				Endpoint_Write_Control_Stream_LE(LineEncodingData, sizeof(CDC_Line_Coding_t));
				
				/* Finalize the stream transfer to send the last packet or clear the host abort */
				Endpoint_ClearOUT();
			}
			
			break;
		case REQ_SetLineEncoding:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				/* Acknowledge the SETUP packet, ready for data transfer */
				Endpoint_ClearSETUP();

				/* Read the line coding data in from the host into the global struct */
				Endpoint_Read_Control_Stream_LE(LineEncodingData, sizeof(CDC_Line_Coding_t));

				/* Finalize the stream transfer to clear the last packet from the host */
				Endpoint_ClearIN();
			}
	
			break;
		case REQ_SetControlLineState:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				/* Acknowledge the SETUP packet, ready for data transfer */
				Endpoint_ClearSETUP();
				
				Endpoint_ClearStatusStage();
			}
	
			break;
	}
}
Exemplo n.º 10
0
static void do_register(int direction, unsigned int regnum)
{
	uint32_t value;
	adf_reg_t reg;

	if (direction == ENDPOINT_DIR_OUT) {
		Endpoint_Read_Control_Stream_LE(&value, sizeof(value));
		value = (value & ~0xf) | (regnum & 0xf);
		reg.whole_reg = value;
		adf_write_reg(&reg);
	} else if (direction == ENDPOINT_DIR_IN) {
		reg = adf_read_reg(regnum);
		value = reg.whole_reg;
		Endpoint_Write_Control_Stream_LE(&value, sizeof(value));
	}
}
Exemplo n.º 11
0
void RNDIS_Device_ProcessControlRequest(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo)
{
	if (!(Endpoint_IsSETUPReceived()))
	  return;
	  
	if (USB_ControlRequest.wIndex != RNDISInterfaceInfo->Config.ControlInterfaceNumber)
	  return;

	switch (USB_ControlRequest.bRequest)
	{
		case REQ_SendEncapsulatedCommand:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();

				Endpoint_Read_Control_Stream_LE(RNDISInterfaceInfo->State.RNDISMessageBuffer, USB_ControlRequest.wLength);
				Endpoint_ClearIN();

				RNDIS_Device_ProcessRNDISControlMessage(RNDISInterfaceInfo);
			}
			
			break;
		case REQ_GetEncapsulatedResponse:
			if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();

				RNDIS_Message_Header_t* MessageHeader = (RNDIS_Message_Header_t*)&RNDISInterfaceInfo->State.RNDISMessageBuffer;

				if (!(MessageHeader->MessageLength))
				{
					RNDISInterfaceInfo->State.RNDISMessageBuffer[0] = 0;
					MessageHeader->MessageLength = 1;
				}

				Endpoint_Write_Control_Stream_LE(RNDISInterfaceInfo->State.RNDISMessageBuffer, MessageHeader->MessageLength);				
				Endpoint_ClearOUT();

				MessageHeader->MessageLength = 0;
			}
	
			break;
	}
}
Exemplo n.º 12
0
/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
 *  the device from the USB host before passing along unhandled control requests to the library for processing
 *  internally.
 */
void EVENT_USB_Device_ControlRequest(void)
{
	/* Process RNDIS class commands */
	switch (USB_ControlRequest.bRequest)
	{
		case RNDIS_REQ_SendEncapsulatedCommand:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();

				/* Read in the RNDIS message into the message buffer */
				Endpoint_Read_Control_Stream_LE(RNDISMessageBuffer, USB_ControlRequest.wLength);
				Endpoint_ClearIN();

				/* Process the RNDIS message */
				ProcessRNDISControlMessage();
			}

			break;
		case RNDIS_REQ_GetEncapsulatedResponse:
			if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				/* Check if a response to the last message is ready */
				if (!(MessageHeader->MessageLength))
				{
					/* Set the response to a single 0x00 byte to indicate that no response is ready */
					RNDISMessageBuffer[0] = 0;
					MessageHeader->MessageLength = 1;
				}

				Endpoint_ClearSETUP();

				/* Write the message response data to the endpoint */
				Endpoint_Write_Control_Stream_LE(RNDISMessageBuffer, MessageHeader->MessageLength);
				Endpoint_ClearOUT();

				/* Reset the message header once again after transmission */
				MessageHeader->MessageLength = 0;
			}

			break;
	}
}
Exemplo n.º 13
0
static void do_acf(int direction, unsigned int vWalue)
{
	struct afc_request req;

	if (direction == ENDPOINT_DIR_OUT) {
		Endpoint_Read_Control_Stream_LE(&req, sizeof(req));
		conf.afc_enable = !!req.state;
		conf.afc_range = req.range ? req.range : conf.afc_range;
		conf.afc_kp = req.p ? req.p : conf.afc_kp;
		conf.afc_ki = req.i ? req.i : conf.afc_ki;
		conf_set_reconf();
	} else if (direction == ENDPOINT_DIR_IN) {
		req.state = conf.afc_enable;
		req.range = conf.afc_range;
		req.p = conf.afc_kp;
		req.i = conf.afc_ki;
		Endpoint_Write_Control_Stream_LE(&req, sizeof(req));
	}
}
Exemplo n.º 14
0
/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
 *  the device from the USB host before passing along unhandled control requests to the library for processing
 *  internally.
 */
void EVENT_USB_Device_ControlRequest(void)
{
	/* Process CDC specific control requests */
	switch (USB_ControlRequest.bRequest)
	{
		case CDC_REQ_GetLineEncoding:
			if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();

				/* Write the line coding data to the control endpoint */
				Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
				Endpoint_ClearOUT();
			}

			break;
		case CDC_REQ_SetLineEncoding:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();

				/* Read the line coding data in from the host into the global struct */
				Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
				Endpoint_ClearIN();
			}

			break;
		case CDC_REQ_SetControlLineState:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();
				Endpoint_ClearStatusStage();

				/* NOTE: Here you can read in the line state mask from the host, to get the current state of the output handshake
				         lines. The mask is read in from the wValue parameter in USB_ControlRequest, and can be masked against the
						 CONTROL_LINE_OUT_* masks to determine the RTS and DTR line states using the following code:
				*/
			}

			break;
	}
}
Exemplo n.º 15
0
/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
 *  the device from the USB host before passing along unhandled control requests to the library for processing
 *  internally.
 */
void EVENT_USB_Device_ControlRequest(void)
{
	/* Determine which interface's Line Coding data is being set from the wIndex parameter */
	void* LineEncodingData = (USB_ControlRequest.wIndex == 0) ? &LineEncoding1 : &LineEncoding2;

	/* Process CDC specific control requests */
	switch (USB_ControlRequest.bRequest)
	{
		case CDC_REQ_GetLineEncoding:
			if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();

				/* Write the line coding data to the control endpoint */
				Endpoint_Write_Control_Stream_LE(LineEncodingData, sizeof(CDC_LineEncoding_t));
				Endpoint_ClearOUT();
			}

			break;
		case CDC_REQ_SetLineEncoding:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();

				/* Read the line coding data in from the host into the global struct */
				Endpoint_Read_Control_Stream_LE(LineEncodingData, sizeof(CDC_LineEncoding_t));
				Endpoint_ClearIN();
			}

			break;
		case CDC_REQ_SetControlLineState:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();
				Endpoint_ClearStatusStage();
			}

			break;
	}
}
Exemplo n.º 16
0
/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
 *  the device from the USB host before passing along unhandled control requests to the library for processing
 *  internally.
 */
void EVENT_USB_Device_ControlRequest(void)
{
	/* Handle HID Class specific requests */
	switch (USB_ControlRequest.bRequest)
	{
		case HID_REQ_GetReport:
			if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				uint8_t GenericData[GENERIC_REPORT_SIZE];
				CreateGenericHIDReport(GenericData);

				Endpoint_ClearSETUP();

				/* Write the report data to the control endpoint */
				Endpoint_Write_Control_Stream_LE(&GenericData, sizeof(GenericData));
				Endpoint_ClearOUT();
			}

			break;
		case HID_REQ_SetReport:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				uint8_t GenericData[GENERIC_REPORT_SIZE];

				Endpoint_ClearSETUP();

				/* Read the report data from the control endpoint */
				Endpoint_Read_Control_Stream_LE(&GenericData, sizeof(GenericData));
				Endpoint_ClearIN();

				ProcessGenericHIDReport(GenericData);
			}

			break;
	}
}
Exemplo n.º 17
0
/** Event handler for the library USB Control Request reception event. */
void EVENT_USB_Device_ControlRequest(void)
{
  static uint8_t success = 1;

  uint8_t bmRequestType = USB_ControlRequest.bmRequestType;
  uint8_t bRequest      = USB_ControlRequest.bRequest;
  //uint8_t wValue        = USB_ControlRequest.wValue;
  char data[51];

  HID_Device_ProcessControlRequest(&Keyboard_HID_Interface);

  if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_VENDOR | REQREC_DEVICE))
  {
    char lock;
    uint16_t wLength = USB_ControlRequest.wLength;
    char pw[32];
    success = 1; /* default */

    eeprom_read(ADDR_LOCK, &lock, 1);
    if(lock == 0 || lock == 255)
      lock = 0;
    else
    {
      if(bRequest != OPENKUBUS_GET_NONCE && bRequest != OPENKUBUS_SET_TIMESTAMP && bRequest != OPENKUBUS_RESET)
      {
        success = 0;
        return;
      }
    }

    // read data
    if(wLength)
    {
      Endpoint_ClearSETUP();
      Endpoint_Read_Control_Stream_LE(data, MIN(sizeof(data), wLength));
      Endpoint_ClearIN();
    }

    switch(bRequest)
    {
      case OPENKUBUS_SET_LOCK:
        lock = 1;
        eeprom_write(ADDR_LOCK, &lock, 1);
        break;

      
      case OPENKUBUS_SET_OWNER:
        if(wLength)
          eeprom_write(ADDR_OWNER, data, wLength);
        else
          success = 0;
        
        break;
      

      case OPENKUBUS_SET_COMPANY:
        if(wLength)
          eeprom_write(ADDR_COMPANY, data, wLength);
        else
          success = 0;
        
        break;
      

      case OPENKUBUS_SET_DESCRIPTION:
        if(wLength)
          eeprom_write(ADDR_DESCRIPTION, data, wLength);
        else
          success = 0;
        
        break;


      case OPENKUBUS_SET_ID:
        if(wLength == 4)
          eeprom_write(ADDR_ID, data, wLength);
        else
          success = 0;
        
        break;


      case OPENKUBUS_SET_TIMESTAMP:
        if(wLength == 16)
        {
          aes256_ctx_t ctx;

          memset(&ctx, 0, sizeof(aes256_ctx_t));
          eeprom_read(ADDR_SEED, pw, sizeof(pw));

          aes256_init(pw, &ctx);
          aes256_dec(data, &ctx);

          if(strncmp(nonce, data, sizeof(nonce)) == 0)
          {
            set_timestamp(array2int((uint8_t *)&data[12]));
            update_nonce();
          }
          else
            success = 0;
        }
        else
          success = 0;

        break;


      case OPENKUBUS_RESET:
        if(wLength == 16)
        {
          aes256_ctx_t ctx;

          memset(&ctx, 0, sizeof(aes256_ctx_t));
          memcpy_P(pw, MASTER_PASSWORD, sizeof(pw));

          aes256_init(pw, &ctx);
          aes256_dec(data, &ctx);

          if(strncmp(nonce, data, sizeof(nonce)) == 0)
          {
            clear_eeprom();
            wdt_enable(WDTO_15MS);
            while(1);
          }
          else
            success = 0;
        }
        else
          success = 0;

        break;


      case OPENKUBUS_SET_SEED:
        if(wLength == LEN_SEED)
          eeprom_write(ADDR_SEED, data, LEN_SEED);
        else
          success = 0;

        break;


      case OPENKUBUS_SET_COUNTER:
        if(wLength == LEN_COUNTER)
          eeprom_write(ADDR_COUNTER, data, LEN_COUNTER);
        else
          success = 0;

        break;


      default:
        success = 0;
        break;
    }
  }
  else if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_VENDOR | REQREC_DEVICE))
  {
    uint8_t length = 0;
    uint8_t i;
    uint32_t temp32 = 0;
    char c;

    switch(bRequest)
    {
      case OPENKUBUS_GET_SUCCESS:
        data[length++] = success;
        break;


      case OPENKUBUS_GET_NONCE:
        for(i = 0; i < sizeof(nonce); i++)
          data[length++] = nonce[i];

        break;


      case OPENKUBUS_GET_TEMPERATURE:
        #ifdef RTC
          data[length++] = rtc_get_temperature();
        #else
          data[length++] = 0xFF;
        #endif
        break;


      case OPENKUBUS_GET_ID:
        for(i = 0; i < LEN_ID; i++)
        {
          eeprom_read(ADDR_ID+i, &c, 1); 
          data[length++] = c;
        }

        break;


      case OPENKUBUS_GET_TIME:
        temp32 = get_timestamp();

        data[length++] = temp32 >> 24;
        data[length++] = temp32 >> 16;
        data[length++] = temp32 >> 8;
        data[length++] = temp32;
        break;


      case OPENKUBUS_GET_SERIAL:
        for(i = 0x0e; i <= 0x18; i++)
          data[length++] = boot_signature_byte_get(i);
        break;


      case OPENKUBUS_GET_DESCRIPTION:
        for(i = 0; i < LEN_DESCRIPTION; i++)
        {
          eeprom_read(ADDR_DESCRIPTION+i, &c, 1); 
          data[length++] = c;
          if(c == 0)
            break;
        }

        break;


      case OPENKUBUS_GET_COMPANY:
        for(i = 0; i < LEN_COMPANY; i++)
        {
          eeprom_read(ADDR_COMPANY+i, &c, 1); 
          data[length++] = c;
          if(c == 0)
            break;
        }

        break;


      case OPENKUBUS_GET_OWNER:
        for(i = 0; i < LEN_OWNER; i++)
        {
          eeprom_read(ADDR_OWNER+i, &c, 1); 
          data[length++] = c;
          if(c == 0)
            break;
        }

        break;

      default:
        data[length++] = 0;
    }

    // send data
    Endpoint_ClearSETUP();
    Endpoint_Write_Control_Stream_LE(data, length);
    Endpoint_ClearOUT();
  }
Exemplo n.º 18
0
/** Event handler for the library USB Unhandled Control Packet event. */
void EVENT_USB_Device_UnhandledControlRequest(void)
{
    const uint8_t SerialNumber[5] = { 0, 0, 0, 0, 1 };
	uint8_t ControlData[2]        = { 0, 0 };

    switch (USB_ControlRequest.bRequest)
	{
		case 0x09:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				LEDs_ToggleLEDs(LEDS_LED1);

				Endpoint_ClearSETUP();

				Endpoint_Read_Control_Stream_LE(ControlData, sizeof(ControlData));
				Endpoint_ClearIN();

				switch (USB_ControlRequest.wValue)
				{
					case 0x303:
						if (ControlData[1]) PORTC &= ~RELAY1; else PORTC |= RELAY1;
						break;
					case 0x306:
						if (ControlData[1]) PORTC &= ~RELAY2; else PORTC |= RELAY2;
						break;
					case 0x309:
						if (ControlData[1]) PORTC &= ~RELAY3; else PORTC |= RELAY3;
						break;
					case 0x30c:
						if (ControlData[1]) PORTC &= ~RELAY4; else PORTC |= RELAY4;
						break;
				}
			}
			
			break;
		case 0x01:
			if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				LEDs_ToggleLEDs(LEDS_LED1);

				Endpoint_ClearSETUP();

				switch (USB_ControlRequest.wValue)
				{
					case 0x301:
						Endpoint_Write_Control_Stream_LE(SerialNumber, sizeof(SerialNumber));
						break;
					case 0x303:
						ControlData[1] = (PORTC & RELAY1) ? 2 : 3;
						break;
					case 0x306:
						ControlData[1] = (PORTC & RELAY2) ? 2 : 3;
						break;
					case 0x309:
						ControlData[1] = (PORTC & RELAY3) ? 2 : 3;
						break;
					case 0x30c:
						ControlData[1] = (PORTC & RELAY4) ? 2 : 3;
						break;
				}
				
				if (ControlData[1])
				  Endpoint_Write_Control_Stream_LE(ControlData, sizeof(ControlData));

				Endpoint_ClearOUT();
			}

			break;
	}
}