예제 #1
0
파일: USBDescriptors.c 프로젝트: gstroe/Arm
/** Parses the given descriptor list via customized function.
 *  \param descriptor    Pointer to the start of the whole descriptors list.
 *  \param totalLength   Total size of descriptors in bytes.
 *  \param parseFunction Function to parse each descriptor scanned.
 *                       Return 0 to continue parsing.
 *  \param parseArg      Argument passed to parse function.
 *  \return Pointer to USBGenericDescriptor instance for next descriptor.
 */
USBGenericDescriptor *USBGenericDescriptor_Parse(
	const USBGenericDescriptor *descriptor,
	uint32_t totalLength,
	USBDescriptorParseFunction parseFunction,
	void *parseArg)
{
	int32_t size = totalLength;

	if (size == 0)
		return 0;

	/* Start parsing descriptors */
	while (1) {
		uint32_t parseRC = 0;

		/* Parse current descriptor */
		if (parseFunction)
			parseRC = parseFunction((void *)descriptor, parseArg);

		/* Get next descriptor */
		size -= USBGenericDescriptor_GetLength(descriptor);
		descriptor = USBGenericDescriptor_GetNextDescriptor(descriptor);

		if (size) {
			if (parseRC != 0)

				return (USBGenericDescriptor *)descriptor;
		} else
			break;
	}

	/* No descriptors remaining */
	return 0;
}
예제 #2
0
/** Parses the given Configuration descriptor (followed by relevant
 *  interface, endpoint and class-specific descriptors) into three arrays.
 *  *Each array must have its size equal or greater to the number of
 *  descriptors it stores plus one*. A null-value is inserted after the last
 *  descriptor of each type to indicate the array end.
 *
 *  Note that if the pointer to an array is null (0), nothing is stored in
 *  it.
 *  \param configuration Pointer to the start of the whole Configuration
 *                       descriptor.
 *  \param interfaces    Pointer to the Interface descriptor array.
 *  \param endpoints     Pointer to the Endpoint descriptor array.
 *  \param others        Pointer to the class-specific descriptor array.
 */
void USBConfigurationDescriptor_Parse(
    const USBConfigurationDescriptor *configuration,
    USBInterfaceDescriptor **interfaces,
    USBEndpointDescriptor **endpoints,
    USBGenericDescriptor **others)
{
    /* Get size of configuration to parse */
    int size = USBConfigurationDescriptor_GetTotalLength(configuration);
    size -= sizeof(USBConfigurationDescriptor);

    /* Start parsing descriptors */
    USBGenericDescriptor *descriptor = (USBGenericDescriptor *) configuration;
    while (size > 0) {

        /* Get next descriptor */
        descriptor = USBGenericDescriptor_GetNextDescriptor(descriptor);
        size -= USBGenericDescriptor_GetLength(descriptor);

        /* Store descriptor in correponding array */
        if (USBGenericDescriptor_GetType(descriptor)
             == USBGenericDescriptor_INTERFACE) {

            if (interfaces) {

                *interfaces = (USBInterfaceDescriptor *) descriptor;
                interfaces++;
            }
        }
        else if (USBGenericDescriptor_GetType(descriptor)
                  == USBGenericDescriptor_ENDPOINT) {

            if (endpoints) {

                *endpoints = (USBEndpointDescriptor *) descriptor;
                endpoints++;
            }
        }
        else if (others) {

            *others = descriptor;
            others++;
        }
    }

    /* Null-terminate arrays */
    if (interfaces) {

        *interfaces = 0;
    }
    if (endpoints) {

        *endpoints = 0;
    }
    if (others) {

        *others = 0;
    }
}