diff --git a/right/src/usb_protocol_handler.c b/right/src/usb_protocol_handler.c index 2801e21..165333c 100644 --- a/right/src/usb_protocol_handler.c +++ b/right/src/usb_protocol_handler.c @@ -68,51 +68,11 @@ void setTestLed(void) UhkModuleStates[0].isTestLedOn = ledState; } -void writeEeprom(void) -{ - uint8_t i2cPayloadSize = GenericHidInBuffer[1]; - - if (i2cPayloadSize > USB_GENERIC_HID_OUT_BUFFER_LENGTH-2) { - setError(WRITE_EEPROM_RESPONSE_INVALID_PAYLOAD_SIZE); - return; - } - -// I2cWrite(I2C_EEPROM_BUS_BASEADDR, I2C_ADDRESS_EEPROM, GenericHidInBuffer+2, i2cPayloadSize); -} - -void readEeprom(void) -{ - uint8_t i2cPayloadSize = GenericHidInBuffer[1]; - - if (i2cPayloadSize > USB_GENERIC_HID_OUT_BUFFER_LENGTH-1) { - setError(WRITE_EEPROM_RESPONSE_INVALID_PAYLOAD_SIZE); - return; - } - -// I2cWrite(I2C_EEPROM_BUS_BASEADDR, I2C_ADDRESS_EEPROM, GenericHidInBuffer+2, 2); -// I2cRead(I2C_EEPROM_BUS_BASEADDR, I2C_ADDRESS_EEPROM, GenericHidOutBuffer+1, i2cPayloadSize); - - GenericHidOutBuffer[0] = UsbResponse_Success; -} - void readMergeSensor(void) { SetResponseByte(MERGE_SENSOR_IS_MERGED); } -void uploadConfig(void) -{ - uint8_t byteCount = GenericHidInBuffer[1]; - uint16_t memoryOffset = *((uint16_t*)(GenericHidInBuffer+2)); - - if (byteCount > USB_GENERIC_HID_OUT_BUFFER_LENGTH-4) { - setError(UPLOAD_CONFIG_INVALID_PAYLOAD_SIZE); - return; - } - - memcpy(UserConfigBuffer.buffer+memoryOffset, GenericHidInBuffer+4, byteCount); -} - void applyConfig(void) { UserConfigBuffer.offset = 0; @@ -143,6 +103,48 @@ void launchEepromTransfer(void) EEPROM_LaunchTransfer(transferType); } +void readConfiguration(bool isHardware) +{ + uint8_t length = GenericHidInBuffer[1]; + uint16_t offset = *((uint16_t*)GenericHidInBuffer+2); + + if (length > USB_GENERIC_HID_OUT_BUFFER_LENGTH-1) { + setError(ConfigTransferResponse_LengthTooLarge); + return; + } + + uint8_t *buffer = isHardware ? HardwareConfigBuffer.buffer : UserConfigBuffer.buffer; + uint16_t bufferLength = isHardware ? HARDWARE_CONFIG_SIZE : USER_CONFIG_SIZE; + + if (offset + length > bufferLength) { + setError(ConfigTransferResponse_BufferOutOfBounds); + return; + } + + memcpy(GenericHidOutBuffer+1, buffer+offset, length); +} + +void writeConfiguration(bool isHardware) +{ + uint8_t length = GenericHidInBuffer[1]; + uint16_t offset = *((uint16_t*)(GenericHidInBuffer+1+1)); + + if (length > USB_GENERIC_HID_OUT_BUFFER_LENGTH-1-1-2) { + setError(ConfigTransferResponse_LengthTooLarge); + return; + } + + uint8_t *buffer = isHardware ? HardwareConfigBuffer.buffer : UserConfigBuffer.buffer; + uint16_t bufferLength = isHardware ? HARDWARE_CONFIG_SIZE : USER_CONFIG_SIZE; + + if (offset + length > bufferLength) { + setError(ConfigTransferResponse_BufferOutOfBounds); + return; + } + + memcpy(buffer+offset, GenericHidInBuffer+1+1+2, length); +} + // The main protocol handler function void usbProtocolHandler(void) @@ -161,17 +163,12 @@ void usbProtocolHandler(void) break; case UsbCommand_WriteLedDriver: break; - case UsbCommand_WriteEeprom: - writeEeprom(); - break; - case UsbCommand_ReadEeprom: - readEeprom(); - break; case UsbCommand_ReadMergeSensor: readMergeSensor(); break; - case UsbCommand_UploadConfig: - uploadConfig(); + case UsbCommand_WriteUserConfiguration: + writeConfiguration(false); + //uploadConfig(); break; case UsbCommand_ApplyConfig: applyConfig(); @@ -185,6 +182,15 @@ void usbProtocolHandler(void) case UsbCommand_LaunchEepromTransfer: launchEepromTransfer(); break; + case UsbCommand_ReadHardwareConfiguration: + readConfiguration(true); + break; + case UsbCommand_WriteHardwareConfiguration: + writeConfiguration(true); + break; + case UsbCommand_ReadUserConfiguration: + readConfiguration(false); + break; default: break; } diff --git a/right/src/usb_protocol_handler.h b/right/src/usb_protocol_handler.h index e5632bb..948c46b 100644 --- a/right/src/usb_protocol_handler.h +++ b/right/src/usb_protocol_handler.h @@ -3,31 +3,24 @@ // Includes: -#include "usb_interfaces/usb_interface_generic_hid.h" - -// Macros: - - - #define WRITE_LED_DRIVER_RESPONSE_INVALID_ADDRESS 1 - #define WRITE_LED_DRIVER_RESPONSE_INVALID_PAYLOAD_SIZE 2 - #define WRITE_EEPROM_RESPONSE_INVALID_PAYLOAD_SIZE 1 - #define UPLOAD_CONFIG_INVALID_PAYLOAD_SIZE 1 + #include "usb_interfaces/usb_interface_generic_hid.h" // Typedefs: typedef enum { - UsbCommand_GetSystemProperty = 0, - UsbCommand_Reenumerate = 1, - UsbCommand_SetTestLed = 2, - UsbCommand_WriteLedDriver = 3, - UsbCommand_WriteEeprom = 5, - UsbCommand_ReadEeprom = 6, - UsbCommand_ReadMergeSensor = 7, - UsbCommand_UploadConfig = 8, - UsbCommand_ApplyConfig = 9, - UsbCommand_SetLedPwm = 10, - UsbCommand_GetAdcValue = 11, - UsbCommand_LaunchEepromTransfer = 12, + UsbCommand_GetSystemProperty = 0, + UsbCommand_Reenumerate = 1, + UsbCommand_SetTestLed = 2, + UsbCommand_WriteLedDriver = 3, + UsbCommand_ReadMergeSensor = 7, + UsbCommand_WriteUserConfiguration = 8, + UsbCommand_ApplyConfig = 9, + UsbCommand_SetLedPwm = 10, + UsbCommand_GetAdcValue = 11, + UsbCommand_LaunchEepromTransfer = 12, + UsbCommand_ReadHardwareConfiguration = 13, + UsbCommand_WriteHardwareConfiguration = 14, + UsbCommand_ReadUserConfiguration = 15, } usb_command_t; typedef enum { @@ -42,6 +35,11 @@ UsbResponse_GenericError = 1, } usb_response_t; + typedef enum { + ConfigTransferResponse_LengthTooLarge = 1, + ConfigTransferResponse_BufferOutOfBounds = 2, + } config_transfer_response_t; + // Functions: extern void usbProtocolHandler();