3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
33 * RNDIS Device commands, to issue RNDIS commands to the device for
34 * the control and data transfer between the host and RNDIS device.
37 #include "RNDISCommands.h"
39 /** Current RNDIS Request ID, for associating sent commands with received data */
40 uint32_t RequestID
= 0;
43 /** Function to send the given encapsulated RNDIS command to the device.
45 * \param[in] Buffer Source command data buffer to send to the device
46 * \param[in] Bytes Number of bytes to send
48 * \return A value from the USB_Host_SendControlErrorCodes_t enum
50 uint8_t RNDIS_SendEncapsulatedCommand(void* Buffer
, uint16_t Length
)
52 USB_ControlRequest
= (USB_Request_Header_t
)
54 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
55 .bRequest
= REQ_SendEncapsulatedCommand
,
61 /* Select the control pipe for the request transfer */
62 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
64 return USB_Host_SendControlRequest(Buffer
);
67 /** Function to receive the given encapsulated RNDIS response from the device.
69 * \param[out] Buffer Destination command data buffer to write read data from the device to
70 * \param[in] Bytes Number of bytes to read
72 * \return A value from the USB_Host_SendControlErrorCodes_t enum
74 uint8_t RNDIS_GetEncapsulatedResponse(void* Buffer
, uint16_t Length
)
76 USB_ControlRequest
= (USB_Request_Header_t
)
78 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
79 .bRequest
= REQ_GetEncapsulatedResponse
,
85 /* Select the control pipe for the request transfer */
86 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
88 return USB_Host_SendControlRequest(Buffer
);
91 /** Sends a RNDIS KEEPALIVE command to the device, to ensure that it does not enter standby mode after periods
94 * \return A value from the USB_Host_SendControlErrorCodes_t enum
96 uint8_t RNDIS_SendKeepAlive(void)
100 RNDIS_KeepAlive_Message_t KeepAliveMessage
;
101 RNDIS_KeepAlive_Complete_t KeepAliveMessageResponse
;
103 KeepAliveMessage
.MessageType
= REMOTE_NDIS_KEEPALIVE_MSG
;
104 KeepAliveMessage
.MessageLength
= sizeof(RNDIS_KeepAlive_Message_t
);
105 KeepAliveMessage
.RequestId
= RequestID
++;
107 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(&KeepAliveMessage
,
108 sizeof(RNDIS_KeepAlive_Message_t
))) != HOST_SENDCONTROL_Successful
)
113 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(&KeepAliveMessageResponse
,
114 sizeof(RNDIS_KeepAlive_Complete_t
))) != HOST_SENDCONTROL_Successful
)
119 return HOST_SENDCONTROL_Successful
;
122 /** Initializes the attached RNDIS device's RNDIS interface.
124 * \param[in] HostMaxPacketSize Size of the packet buffer on the host
125 * \param[out] DeviceMaxPacketSize Pointer to where the packet buffer size of the device is to be stored
127 * \return A value from the USB_Host_SendControlErrorCodes_t enum
129 uint8_t RNDIS_InitializeDevice(uint16_t HostMaxPacketSize
, uint16_t* DeviceMaxPacketSize
)
133 RNDIS_Initialize_Message_t InitMessage
;
134 RNDIS_Initialize_Complete_t InitMessageResponse
;
136 InitMessage
.MessageType
= REMOTE_NDIS_INITIALIZE_MSG
;
137 InitMessage
.MessageLength
= sizeof(RNDIS_Initialize_Message_t
);
138 InitMessage
.RequestId
= RequestID
++;
140 InitMessage
.MajorVersion
= REMOTE_NDIS_VERSION_MAJOR
;
141 InitMessage
.MinorVersion
= REMOTE_NDIS_VERSION_MINOR
;
142 InitMessage
.MaxTransferSize
= HostMaxPacketSize
;
144 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(&InitMessage
,
145 sizeof(RNDIS_Initialize_Message_t
))) != HOST_SENDCONTROL_Successful
)
150 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(&InitMessageResponse
,
151 sizeof(RNDIS_Initialize_Complete_t
))) != HOST_SENDCONTROL_Successful
)
156 if (InitMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
157 return RNDIS_COMMAND_FAILED
;
159 *DeviceMaxPacketSize
= InitMessageResponse
.MaxTransferSize
;
161 return HOST_SENDCONTROL_Successful
;
164 /** Sets a given RNDIS property of an attached RNDIS device.
166 * \param[in] Oid OID number of the parameter to set
167 * \param[in] Buffer Pointer to where the property data is to be sourced from
168 * \param[in] Length Length in bytes of the property data to sent to the device
170 * \return A value from the USB_Host_SendControlErrorCodes_t enum
172 uint8_t RNDIS_SetRNDISProperty(uint32_t Oid
, void* Buffer
, uint16_t Length
)
178 RNDIS_Set_Message_t SetMessage
;
179 uint8_t ContigiousBuffer
[Length
];
182 RNDIS_Set_Complete_t SetMessageResponse
;
184 SetMessageData
.SetMessage
.MessageType
= REMOTE_NDIS_SET_MSG
;
185 SetMessageData
.SetMessage
.MessageLength
= sizeof(RNDIS_Set_Message_t
) + Length
;
186 SetMessageData
.SetMessage
.RequestId
= RequestID
++;
188 SetMessageData
.SetMessage
.Oid
= Oid
;
189 SetMessageData
.SetMessage
.InformationBufferLength
= Length
;
190 SetMessageData
.SetMessage
.InformationBufferOffset
= (sizeof(RNDIS_Set_Message_t
) - sizeof(RNDIS_Message_Header_t
));
191 SetMessageData
.SetMessage
.DeviceVcHandle
= 0;
193 memcpy(&SetMessageData
.ContigiousBuffer
, Buffer
, Length
);
195 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(&SetMessageData
,
196 SetMessageData
.SetMessage
.MessageLength
)) != HOST_SENDCONTROL_Successful
)
201 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(&SetMessageResponse
,
202 sizeof(RNDIS_Set_Complete_t
))) != HOST_SENDCONTROL_Successful
)
207 if (SetMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
208 return RNDIS_COMMAND_FAILED
;
210 return HOST_SENDCONTROL_Successful
;
213 /** Gets a given RNDIS property of an attached RNDIS device.
215 * \param[in] Oid OID number of the parameter to get
216 * \param[in] Buffer Pointer to where the property data is to be written to
217 * \param[in] MaxLength Length in bytes of the destination buffer size
219 * \return A value from the USB_Host_SendControlErrorCodes_t enum
221 uint8_t RNDIS_QueryRNDISProperty(uint32_t Oid
, void* Buffer
, uint16_t MaxLength
)
225 RNDIS_Query_Message_t QueryMessage
;
229 RNDIS_Query_Complete_t QueryMessageResponse
;
230 uint8_t ContigiousBuffer
[MaxLength
];
231 } QueryMessageResponseData
;
233 QueryMessage
.MessageType
= REMOTE_NDIS_QUERY_MSG
;
234 QueryMessage
.MessageLength
= sizeof(RNDIS_Query_Message_t
);
235 QueryMessage
.RequestId
= RequestID
++;
237 QueryMessage
.Oid
= Oid
;
238 QueryMessage
.InformationBufferLength
= 0;
239 QueryMessage
.InformationBufferOffset
= 0;
240 QueryMessage
.DeviceVcHandle
= 0;
242 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(&QueryMessage
,
243 sizeof(RNDIS_Query_Message_t
))) != HOST_SENDCONTROL_Successful
)
248 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(&QueryMessageResponseData
,
249 sizeof(QueryMessageResponseData
))) != HOST_SENDCONTROL_Successful
)
254 if (QueryMessageResponseData
.QueryMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
255 return RNDIS_COMMAND_FAILED
;
257 memcpy(Buffer
, &QueryMessageResponseData
.ContigiousBuffer
, MaxLength
);
259 return HOST_SENDCONTROL_Successful
;
262 /** Retrieves the size of a received packet, discarding the remainder of the RNDIS packet header to leave only the
263 * packet contents for processing by the host.
265 * \param[out] PacketLength Size of the packet currently in the pipe
267 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
269 uint8_t RNDIS_GetPacketLength(uint16_t* PacketLength
)
273 Pipe_SelectPipe(RNDIS_DATAPIPE_IN
);
274 Pipe_SetPipeToken(PIPE_TOKEN_IN
);
277 if (!(Pipe_IsReadWriteAllowed()))
281 return PIPE_RWSTREAM_NoError
;
284 RNDIS_Packet_Message_t DeviceMessage
;
286 if ((ErrorCode
= Pipe_Read_Stream_LE(&DeviceMessage
, sizeof(RNDIS_Packet_Message_t
))) != PIPE_RWSTREAM_NoError
)
291 *PacketLength
= (uint16_t)DeviceMessage
.DataLength
;
293 Pipe_Discard_Stream(DeviceMessage
.DataOffset
- (sizeof(RNDIS_Packet_Message_t
) - sizeof(RNDIS_Message_Header_t
)));
297 return PIPE_RWSTREAM_NoError
;