Add new RNDIS_Host_IsPacketReceived(), RNDIS_Device_ReadPacket() and RNDIS_Device_WritePacket() functions to the Device RNDIS Class Driver.
Modify RNDIS demos to suit the simplified Ethernet_Frame_Info_t structure.
This commit is contained in:
parent
41de1d1dab
commit
9d733d44b4
15 changed files with 173 additions and 96 deletions
|
@ -68,6 +68,9 @@
|
|||
#endif
|
||||
|
||||
/* Macros: */
|
||||
/** Additional error code for RNDIS functions when a device returns a logical command failure. */
|
||||
#define RNDIS_ERROR_LOGICAL_CMD_FAILED 0x80
|
||||
|
||||
/** Implemented RNDIS Version Major. */
|
||||
#define REMOTE_NDIS_VERSION_MAJOR 0x01
|
||||
|
||||
|
@ -205,26 +208,26 @@
|
|||
};
|
||||
|
||||
/* Type Defines: */
|
||||
/** \brief Ethernet Frame Packet Information Structure.
|
||||
*
|
||||
* Type define for an Ethernet frame buffer data and information structure. This can be used to conveniently
|
||||
* store both the size and data in an Ethernet frame.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t FrameData[ETHERNET_FRAME_SIZE_MAX]; /**< Ethernet frame contents. */
|
||||
uint16_t FrameLength; /**< Length in bytes of the Ethernet frame stored in the buffer. */
|
||||
} Ethernet_Frame_Info_t;
|
||||
|
||||
/** \brief MAC Address Structure.
|
||||
*
|
||||
* Type define for a physical MAC address of a device on a network.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t Octets[6]; /**< Individual bytes of a MAC address */
|
||||
uint8_t Octets[6]; /**< Individual bytes of a MAC address */
|
||||
} ATTR_PACKED MAC_Address_t;
|
||||
|
||||
/** \brief RNDIS Ethernet Frame Packet Information Structure.
|
||||
*
|
||||
* Type define for an Ethernet frame buffer data and information structure.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t FrameData[ETHERNET_FRAME_SIZE_MAX]; /**< Ethernet frame contents. */
|
||||
uint16_t FrameLength; /**< Length in bytes of the Ethernet frame stored in the buffer. */
|
||||
bool FrameInBuffer; /**< Indicates if a frame is currently stored in the buffer. */
|
||||
} ATTR_PACKED Ethernet_Frame_Info_t;
|
||||
|
||||
/** \brief RNDIS Common Message Header Structure.
|
||||
*
|
||||
* Type define for a RNDIS message header, sent before RNDIS messages.
|
||||
|
|
|
@ -163,8 +163,6 @@ void RNDIS_Device_USBTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo
|
|||
if (USB_DeviceState != DEVICE_STATE_Configured)
|
||||
return;
|
||||
|
||||
RNDIS_Message_Header_t* MessageHeader = (RNDIS_Message_Header_t*)&RNDISInterfaceInfo->State.RNDISMessageBuffer;
|
||||
|
||||
Endpoint_SelectEndpoint(RNDISInterfaceInfo->Config.NotificationEndpointNumber);
|
||||
|
||||
if (Endpoint_IsINReady() && RNDISInterfaceInfo->State.ResponseReady)
|
||||
|
@ -184,50 +182,6 @@ void RNDIS_Device_USBTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo
|
|||
|
||||
RNDISInterfaceInfo->State.ResponseReady = false;
|
||||
}
|
||||
|
||||
if ((RNDISInterfaceInfo->State.CurrRNDISState == RNDIS_Data_Initialized) && !(MessageHeader->MessageLength))
|
||||
{
|
||||
RNDIS_Packet_Message_t RNDISPacketHeader;
|
||||
|
||||
Endpoint_SelectEndpoint(RNDISInterfaceInfo->Config.DataOUTEndpointNumber);
|
||||
|
||||
if (Endpoint_IsOUTReceived() && !(RNDISInterfaceInfo->State.FrameIN.FrameInBuffer))
|
||||
{
|
||||
Endpoint_Read_Stream_LE(&RNDISPacketHeader, sizeof(RNDIS_Packet_Message_t), NULL);
|
||||
|
||||
if (RNDISPacketHeader.DataLength > ETHERNET_FRAME_SIZE_MAX)
|
||||
{
|
||||
Endpoint_StallTransaction();
|
||||
return;
|
||||
}
|
||||
|
||||
Endpoint_Read_Stream_LE(RNDISInterfaceInfo->State.FrameIN.FrameData, RNDISPacketHeader.DataLength, NULL);
|
||||
|
||||
Endpoint_ClearOUT();
|
||||
|
||||
RNDISInterfaceInfo->State.FrameIN.FrameLength = RNDISPacketHeader.DataLength;
|
||||
|
||||
RNDISInterfaceInfo->State.FrameIN.FrameInBuffer = true;
|
||||
}
|
||||
|
||||
Endpoint_SelectEndpoint(RNDISInterfaceInfo->Config.DataINEndpointNumber);
|
||||
|
||||
if (Endpoint_IsINReady() && RNDISInterfaceInfo->State.FrameOUT.FrameInBuffer)
|
||||
{
|
||||
memset(&RNDISPacketHeader, 0, sizeof(RNDIS_Packet_Message_t));
|
||||
|
||||
RNDISPacketHeader.MessageType = REMOTE_NDIS_PACKET_MSG;
|
||||
RNDISPacketHeader.MessageLength = (sizeof(RNDIS_Packet_Message_t) + RNDISInterfaceInfo->State.FrameOUT.FrameLength);
|
||||
RNDISPacketHeader.DataOffset = (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t));
|
||||
RNDISPacketHeader.DataLength = RNDISInterfaceInfo->State.FrameOUT.FrameLength;
|
||||
|
||||
Endpoint_Write_Stream_LE(&RNDISPacketHeader, sizeof(RNDIS_Packet_Message_t), NULL);
|
||||
Endpoint_Write_Stream_LE(RNDISInterfaceInfo->State.FrameOUT.FrameData, RNDISPacketHeader.DataLength, NULL);
|
||||
Endpoint_ClearIN();
|
||||
|
||||
RNDISInterfaceInfo->State.FrameOUT.FrameInBuffer = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RNDIS_Device_ProcessRNDISControlMessage(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo)
|
||||
|
@ -493,5 +447,85 @@ static bool RNDIS_Device_ProcessNDISSet(USB_ClassInfo_RNDIS_Device_t* const RNDI
|
|||
}
|
||||
}
|
||||
|
||||
bool RNDIS_Device_IsPacketReceived(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo)
|
||||
{
|
||||
if ((USB_DeviceState != DEVICE_STATE_Configured) ||
|
||||
(RNDISInterfaceInfo->State.CurrRNDISState != RNDIS_Data_Initialized))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Endpoint_SelectEndpoint(RNDISInterfaceInfo->Config.DataOUTEndpointNumber);
|
||||
return Endpoint_IsOUTReceived();
|
||||
}
|
||||
|
||||
uint8_t RNDIS_Device_ReadPacket(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo,
|
||||
void* Buffer,
|
||||
uint16_t* const PacketLength)
|
||||
{
|
||||
if ((USB_DeviceState != DEVICE_STATE_Configured) ||
|
||||
(RNDISInterfaceInfo->State.CurrRNDISState != RNDIS_Data_Initialized))
|
||||
{
|
||||
return ENDPOINT_RWSTREAM_DeviceDisconnected;
|
||||
}
|
||||
|
||||
Endpoint_SelectEndpoint(RNDISInterfaceInfo->Config.DataOUTEndpointNumber);
|
||||
|
||||
*PacketLength = 0;
|
||||
|
||||
if (!(Endpoint_IsOUTReceived()))
|
||||
return ENDPOINT_RWSTREAM_NoError;
|
||||
|
||||
RNDIS_Packet_Message_t RNDISPacketHeader;
|
||||
Endpoint_Read_Stream_LE(&RNDISPacketHeader, sizeof(RNDIS_Packet_Message_t), NULL);
|
||||
|
||||
if (RNDISPacketHeader.DataLength > ETHERNET_FRAME_SIZE_MAX)
|
||||
{
|
||||
Endpoint_StallTransaction();
|
||||
|
||||
return RNDIS_ERROR_LOGICAL_CMD_FAILED;
|
||||
}
|
||||
|
||||
*PacketLength = (uint16_t)RNDISPacketHeader.DataLength;
|
||||
|
||||
Endpoint_Read_Stream_LE(Buffer, RNDISPacketHeader.DataLength, NULL);
|
||||
Endpoint_ClearOUT();
|
||||
|
||||
return ENDPOINT_RWSTREAM_NoError;
|
||||
}
|
||||
|
||||
uint8_t RNDIS_Device_SendPacket(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo,
|
||||
void* Buffer,
|
||||
const uint16_t PacketLength)
|
||||
{
|
||||
uint8_t ErrorCode;
|
||||
|
||||
if ((USB_DeviceState != DEVICE_STATE_Configured) ||
|
||||
(RNDISInterfaceInfo->State.CurrRNDISState != RNDIS_Data_Initialized))
|
||||
{
|
||||
return ENDPOINT_RWSTREAM_DeviceDisconnected;
|
||||
}
|
||||
|
||||
Endpoint_SelectEndpoint(RNDISInterfaceInfo->Config.DataINEndpointNumber);
|
||||
|
||||
if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)
|
||||
return ErrorCode;
|
||||
|
||||
RNDIS_Packet_Message_t RNDISPacketHeader;
|
||||
|
||||
memset(&RNDISPacketHeader, 0, sizeof(RNDIS_Packet_Message_t));
|
||||
|
||||
RNDISPacketHeader.MessageType = REMOTE_NDIS_PACKET_MSG;
|
||||
RNDISPacketHeader.MessageLength = (sizeof(RNDIS_Packet_Message_t) + PacketLength);
|
||||
RNDISPacketHeader.DataOffset = (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t));
|
||||
RNDISPacketHeader.DataLength = PacketLength;
|
||||
|
||||
Endpoint_Write_Stream_LE(&RNDISPacketHeader, sizeof(RNDIS_Packet_Message_t), NULL);
|
||||
Endpoint_Write_Stream_LE(Buffer, PacketLength, NULL);
|
||||
Endpoint_ClearIN();
|
||||
|
||||
return ENDPOINT_RWSTREAM_NoError;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -106,12 +106,6 @@
|
|||
bool ResponseReady; /**< Internal flag indicating if a RNDIS message is waiting to be returned to the host. */
|
||||
uint8_t CurrRNDISState; /**< Current RNDIS state of the adapter, a value from the \ref RNDIS_States_t enum. */
|
||||
uint32_t CurrPacketFilter; /**< Current packet filter mode, used internally by the class driver. */
|
||||
Ethernet_Frame_Info_t FrameIN; /**< Structure holding the last received Ethernet frame from the host, for user
|
||||
* processing.
|
||||
*/
|
||||
Ethernet_Frame_Info_t FrameOUT; /**< Structure holding the next Ethernet frame to send to the host, populated by the
|
||||
* user application.
|
||||
*/
|
||||
} State; /**< State data for the USB class interface within the device. All elements in this section
|
||||
* are reset to their defaults when the interface is enumerated.
|
||||
*/
|
||||
|
@ -146,6 +140,48 @@
|
|||
*/
|
||||
void RNDIS_Device_USBTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
|
||||
|
||||
/** Determines if a packet is currently waiting for the device to read in and process.
|
||||
*
|
||||
* \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or the
|
||||
* call will fail.
|
||||
*
|
||||
* \param[in,out] RNDISInterfaceInfo Pointer to a structure containing an RNDIS Class configuration and state.
|
||||
*
|
||||
* \return Boolean \c true if a packet is waiting to be read in by the host, \c false otherwise.
|
||||
*/
|
||||
bool RNDIS_Device_IsPacketReceived(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo);
|
||||
|
||||
/** Retrieves the next pending packet from the device, discarding the remainder of the RNDIS packet header to leave
|
||||
* only the packet contents for processing by the device in the nominated buffer.
|
||||
*
|
||||
* \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or the
|
||||
* call will fail.
|
||||
*
|
||||
* \param[in,out] RNDISInterfaceInfo Pointer to a structure containing an RNDIS Class configuration and state.
|
||||
* \param[out] Buffer Pointer to a buffer where the packer data is to be written to.
|
||||
* \param[out] PacketLength Pointer to where the length in bytes of the read packet is to be stored.
|
||||
*
|
||||
* \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
|
||||
*/
|
||||
uint8_t RNDIS_Device_ReadPacket(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo,
|
||||
void* Buffer,
|
||||
uint16_t* const PacketLength);
|
||||
|
||||
/** Sends the given packet to the attached RNDIS device, after adding a RNDIS packet message header.
|
||||
*
|
||||
* \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or the
|
||||
* call will fail.
|
||||
*
|
||||
* \param[in,out] RNDISInterfaceInfo Pointer to a structure containing an RNDIS Class configuration and state.
|
||||
* \param[in] Buffer Pointer to a buffer where the packer data is to be read from.
|
||||
* \param[in] PacketLength Length in bytes of the packet to send.
|
||||
*
|
||||
* \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
|
||||
*/
|
||||
uint8_t RNDIS_Device_SendPacket(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo,
|
||||
void* Buffer,
|
||||
const uint16_t PacketLength);
|
||||
|
||||
/* Private Interface - For use in library only: */
|
||||
#if !defined(__DOXYGEN__)
|
||||
/* Function Prototypes: */
|
||||
|
|
|
@ -315,7 +315,7 @@ uint8_t RNDIS_Host_InitializeDevice(USB_ClassInfo_RNDIS_Host_t* const RNDISInter
|
|||
}
|
||||
|
||||
if (InitMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
|
||||
return RNDIS_COMMAND_FAILED;
|
||||
return RNDIS_ERROR_LOGICAL_CMD_FAILED;
|
||||
|
||||
RNDISInterfaceInfo->State.DeviceMaxPacketSize = InitMessageResponse.MaxTransferSize;
|
||||
|
||||
|
@ -361,7 +361,7 @@ uint8_t RNDIS_Host_SetRNDISProperty(USB_ClassInfo_RNDIS_Host_t* const RNDISInter
|
|||
}
|
||||
|
||||
if (SetMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
|
||||
return RNDIS_COMMAND_FAILED;
|
||||
return RNDIS_ERROR_LOGICAL_CMD_FAILED;
|
||||
|
||||
return HOST_SENDCONTROL_Successful;
|
||||
}
|
||||
|
@ -403,7 +403,7 @@ uint8_t RNDIS_Host_QueryRNDISProperty(USB_ClassInfo_RNDIS_Host_t* const RNDISInt
|
|||
}
|
||||
|
||||
if (QueryMessageResponseData.QueryMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
|
||||
return RNDIS_COMMAND_FAILED;
|
||||
return RNDIS_ERROR_LOGICAL_CMD_FAILED;
|
||||
|
||||
memcpy(Buffer, &QueryMessageResponseData.ContiguousBuffer, MaxLength);
|
||||
|
||||
|
@ -458,7 +458,8 @@ uint8_t RNDIS_Host_ReadPacket(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceIn
|
|||
|
||||
*PacketLength = (uint16_t)DeviceMessage.DataLength;
|
||||
|
||||
Pipe_Discard_Stream(DeviceMessage.DataOffset - (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t)),
|
||||
Pipe_Discard_Stream(DeviceMessage.DataOffset -
|
||||
(sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t)),
|
||||
NULL);
|
||||
|
||||
Pipe_Read_Stream_LE(Buffer, *PacketLength, NULL);
|
||||
|
|
|
@ -124,10 +124,6 @@
|
|||
RNDIS_ENUMERROR_PipeConfigurationFailed = 3, /**< One or more pipes for the specified interface could not be configured correctly. */
|
||||
};
|
||||
|
||||
/* Macros: */
|
||||
/** Additional error code for RNDIS functions when a device returns a logical command failure. */
|
||||
#define RNDIS_COMMAND_FAILED 0xC0
|
||||
|
||||
/* Function Prototypes: */
|
||||
/** Host interface configuration routine, to configure a given RNDIS host interface instance using the Configuration
|
||||
* Descriptor read from an attached USB device. This function automatically updates the given RNDIS Host instance's
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue