/**
 * @brief  USBD_USR_SerialStrDescriptor
 *         return the serial number string descriptor
 * @param  speed : current device speed
 * @param  length : pointer to data length variable
 * @retval pointer to descriptor buffer
 */
uint8_t *  USBD_USR_SerialStrDescriptor( uint8_t speed , uint16_t *length)
{
    uint8_t deviceId[16];
    char deviceIdHex[sizeof(deviceId) * 2 + 1] = {0};
    unsigned deviceIdLen = 0;
    deviceIdLen = HAL_device_ID(deviceId, sizeof(deviceId));
    bytes2hexbuf(deviceId, deviceIdLen, deviceIdHex);
    USBD_GetString (deviceIdHex, USBD_StrDesc, length);
    return USBD_StrDesc;
}
uint8_t SystemControlInterface::handleVendorRequest(HAL_USB_SetupRequest* req) {
  /*
   * This callback should process vendor-specific SETUP requests from the host.
   * NOTE: This callback is called from an ISR.
   *
   * Each request contains the following fields:
   * - bmRequestType - request type bit mask. Since only vendor-specific device requests are forwarded
   *                   to this callback, the only bit that should be of intereset is
   *                   "Data Phase Transfer Direction", easily accessed as req->bmRequestTypeDirection:
   *                   0 - Host to Device
   *                   1 - Device to Host
   * - bRequest - 1 byte, request identifier. The only reserved request that will not be forwarded to this callback
   *              is 0xee, which is used for Microsoft-specific vendor requests.
   * - wIndex - 2 byte index, any value between 0x0000 and 0xffff.
   * - wValue - 2 byte value, any value between 0x0000 and 0xffff.
   * - wLength - each request might have an optional data stage of up to 0xffff (65535) bytes.
   *             Host -> Device requests contain data sent by the host to the device.
   *             Device -> Host requests request the device to send up to wLength bytes of data.
   *
   * This callback should return 0 if the request has been correctly handled or 1 otherwise.
   *
   * When handling Device->Host requests with data stage, this callback should fill req->data buffer with
   * up to wLength bytes of data if req->data != NULL (which should be the case when wLength <= 64)
   * or set req->data to point to some buffer which contains up to wLength bytes of data.
   * wLength may be safely modified to notify that there is less data in the buffer than requested by the host.
   *
   * [1] Host -> Device requests with data stage containing up to 64 bytes can be handled by
   * an internal buffer in HAL. For requests larger than 64 bytes, the vendor request callback
   * needs to provide a buffer of an appropriate size:
   *
   * req->data = buffer; // sizeof(buffer) >= req->wLength
   * return 0;
   *
   * The callback will be called again once the data stage completes
   * It will contain the same bmRequest, bRequest, wIndex, wValue and wLength fields.
   * req->data should contain wLength bytes received from the host.
   */

  /*
   * We are handling only bRequest = 0x50 ('P') requests.
   * The request type itself (enum USBRequestType) should be in wIndex field.
   */
  if (req->bRequest != 0x50)
    return 1;

  if (req->bmRequestTypeDirection == 0) {
    // Host -> Device
    switch (req->wIndex) {
      case USB_REQUEST_RESET: {
        // FIXME: We probably shouldn't reset from an ISR.
        // The host will probably get an error that control request has timed out since we
        // didn't respond to it.
        System.reset(req->wValue);
        break;
      }

      case USB_REQUEST_DFU_MODE: {
        // FIXME: We probably shouldn't enter DFU mode from an ISR.
        // The host will probably get an error that control request has timed out since we
        // didn't respond to it.
        System.dfu(false);
        break;
      }

      case USB_REQUEST_LISTENING_MODE: {
        // FIXME: We probably shouldn't enter listening mode from an ISR.
        // The host will probably get an error that control request has timed out since we
        // didn't respond to it.
        system_set_flag(SYSTEM_FLAG_STARTUP_SAFE_LISTEN_MODE, 1, nullptr);
        System.enterSafeMode();
        break;
      }

      case USB_REQUEST_LOG_CONFIG: {
        return enqueueRequest(req, DATA_FORMAT_JSON);
      }

      case USB_REQUEST_CUSTOM: {
        return enqueueRequest(req);
      }

      default: {
        // Unknown request
        return 1;
      }
    }
  } else {
    // Device -> Host
    switch (req->wIndex) {
      case USB_REQUEST_DEVICE_ID: {
        if (req->wLength == 0 || req->data == NULL) {
          // No data stage or requested > 64 bytes
          return 1;
        }
        if (req->wValue == 0x0001) {
          // Return as buffer
          if (req->wLength < 12)
            return 1;

          HAL_device_ID(req->data, req->wLength);
          req->wLength = 12;
        } else {
          // Return as string
          String id = System.deviceID();
          if (req->wLength < (id.length() + 1))
            return 1;
          strncpy((char*)req->data, id.c_str(), req->wLength);
          req->wLength = id.length() + 1;
        }
        break;
      }

      case USB_REQUEST_SYSTEM_VERSION: {
        if (req->wLength == 0 || req->data == NULL) {
          // No data stage or requested > 64 bytes
          return 1;
        }

        strncpy((char*)req->data, __XSTRING(SYSTEM_VERSION_STRING), req->wLength);
        req->wLength = sizeof(__XSTRING(SYSTEM_VERSION_STRING)) + 1;
        break;
      }

      case USB_REQUEST_LOG_CONFIG:
      case USB_REQUEST_CUSTOM: {
        return fetchRequestResult(req);
      }

      default: {
        // Unknown request
        return 1;
      }
    }
  }

  return 0;
}