Add const keyword to the demo function parameters where possible.
[pub/USBasp.git] / Demos / Host / LowLevel / RNDISEthernetHost / Lib / RNDISCommands.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
33 * RNDIS Device commands, to issue RNDIS commands to the device for
34 * the control and data transfer between the host and RNDIS device.
35 */
36
37 #include "RNDISCommands.h"
38
39 /** Current RNDIS Request ID, for associating sent commands with received data */
40 uint32_t RequestID = 0;
41
42
43 /** Function to send the given encapsulated RNDIS command to the device.
44 *
45 * \param[in] Buffer Source command data buffer to send to the device
46 * \param[in] Length Number of bytes to send
47 *
48 * \return A value from the USB_Host_SendControlErrorCodes_t enum
49 */
50 uint8_t RNDIS_SendEncapsulatedCommand(void* const Buffer, const uint16_t Length)
51 {
52 USB_ControlRequest = (USB_Request_Header_t)
53 {
54 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
55 .bRequest = REQ_SendEncapsulatedCommand,
56 .wValue = 0,
57 .wIndex = 0,
58 .wLength = Length,
59 };
60
61 /* Select the control pipe for the request transfer */
62 Pipe_SelectPipe(PIPE_CONTROLPIPE);
63
64 return USB_Host_SendControlRequest(Buffer);
65 }
66
67 /** Function to receive the given encapsulated RNDIS response from the device.
68 *
69 * \param[out] Buffer Destination command data buffer to write read data from the device to
70 * \param[in] Length Number of bytes to read
71 *
72 * \return A value from the USB_Host_SendControlErrorCodes_t enum
73 */
74 uint8_t RNDIS_GetEncapsulatedResponse(void* const Buffer, const uint16_t Length)
75 {
76 USB_ControlRequest = (USB_Request_Header_t)
77 {
78 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
79 .bRequest = REQ_GetEncapsulatedResponse,
80 .wValue = 0,
81 .wIndex = 0,
82 .wLength = Length,
83 };
84
85 /* Select the control pipe for the request transfer */
86 Pipe_SelectPipe(PIPE_CONTROLPIPE);
87
88 return USB_Host_SendControlRequest(Buffer);
89 }
90
91 /** Sends a RNDIS KEEPALIVE command to the device, to ensure that it does not enter standby mode after periods
92 * of long inactivity.
93 *
94 * \return A value from the USB_Host_SendControlErrorCodes_t enum or RNDIS_COMMAND_FAILED if the device returned a
95 * logical command failure
96 */
97 uint8_t RNDIS_SendKeepAlive(void)
98 {
99 uint8_t ErrorCode;
100
101 RNDIS_KeepAlive_Message_t KeepAliveMessage;
102 RNDIS_KeepAlive_Complete_t KeepAliveMessageResponse;
103
104 KeepAliveMessage.MessageType = REMOTE_NDIS_KEEPALIVE_MSG;
105 KeepAliveMessage.MessageLength = sizeof(RNDIS_KeepAlive_Message_t);
106 KeepAliveMessage.RequestId = RequestID++;
107
108 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&KeepAliveMessage,
109 sizeof(RNDIS_KeepAlive_Message_t))) != HOST_SENDCONTROL_Successful)
110 {
111 return ErrorCode;
112 }
113
114 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&KeepAliveMessageResponse,
115 sizeof(RNDIS_KeepAlive_Complete_t))) != HOST_SENDCONTROL_Successful)
116 {
117 return ErrorCode;
118 }
119
120 return HOST_SENDCONTROL_Successful;
121 }
122
123 /** Initializes the attached RNDIS device's RNDIS interface.
124 *
125 * \param[in] HostMaxPacketSize Size of the packet buffer on the host
126 * \param[out] DeviceMaxPacketSize Pointer to where the packet buffer size of the device is to be stored
127 *
128 * \return A value from the USB_Host_SendControlErrorCodes_t enum or RNDIS_COMMAND_FAILED if the device returned a
129 * logical command failure
130 */
131 uint8_t RNDIS_InitializeDevice(const uint16_t HostMaxPacketSize, uint16_t* const DeviceMaxPacketSize)
132 {
133 uint8_t ErrorCode;
134
135 RNDIS_Initialize_Message_t InitMessage;
136 RNDIS_Initialize_Complete_t InitMessageResponse;
137
138 InitMessage.MessageType = REMOTE_NDIS_INITIALIZE_MSG;
139 InitMessage.MessageLength = sizeof(RNDIS_Initialize_Message_t);
140 InitMessage.RequestId = RequestID++;
141
142 InitMessage.MajorVersion = REMOTE_NDIS_VERSION_MAJOR;
143 InitMessage.MinorVersion = REMOTE_NDIS_VERSION_MINOR;
144 InitMessage.MaxTransferSize = HostMaxPacketSize;
145
146 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&InitMessage,
147 sizeof(RNDIS_Initialize_Message_t))) != HOST_SENDCONTROL_Successful)
148 {
149 return ErrorCode;
150 }
151
152 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&InitMessageResponse,
153 sizeof(RNDIS_Initialize_Complete_t))) != HOST_SENDCONTROL_Successful)
154 {
155 return ErrorCode;
156 }
157
158 if (InitMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
159 return RNDIS_COMMAND_FAILED;
160
161 *DeviceMaxPacketSize = InitMessageResponse.MaxTransferSize;
162
163 return HOST_SENDCONTROL_Successful;
164 }
165
166 /** Sets a given RNDIS property of an attached RNDIS device.
167 *
168 * \param[in] Oid OID number of the parameter to set
169 * \param[in] Buffer Pointer to where the property data is to be sourced from
170 * \param[in] Length Length in bytes of the property data to sent to the device
171 *
172 * \return A value from the USB_Host_SendControlErrorCodes_t enum or RNDIS_COMMAND_FAILED if the device returned a
173 * logical command failure
174 */
175 uint8_t RNDIS_SetRNDISProperty(const uint32_t Oid, void* Buffer, const uint16_t Length)
176 {
177 uint8_t ErrorCode;
178
179 struct
180 {
181 RNDIS_Set_Message_t SetMessage;
182 uint8_t ContigiousBuffer[Length];
183 } SetMessageData;
184
185 RNDIS_Set_Complete_t SetMessageResponse;
186
187 SetMessageData.SetMessage.MessageType = REMOTE_NDIS_SET_MSG;
188 SetMessageData.SetMessage.MessageLength = sizeof(RNDIS_Set_Message_t) + Length;
189 SetMessageData.SetMessage.RequestId = RequestID++;
190
191 SetMessageData.SetMessage.Oid = Oid;
192 SetMessageData.SetMessage.InformationBufferLength = Length;
193 SetMessageData.SetMessage.InformationBufferOffset = (sizeof(RNDIS_Set_Message_t) - sizeof(RNDIS_Message_Header_t));
194 SetMessageData.SetMessage.DeviceVcHandle = 0;
195
196 memcpy(&SetMessageData.ContigiousBuffer, Buffer, Length);
197
198 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&SetMessageData,
199 SetMessageData.SetMessage.MessageLength)) != HOST_SENDCONTROL_Successful)
200 {
201 return ErrorCode;
202 }
203
204 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&SetMessageResponse,
205 sizeof(RNDIS_Set_Complete_t))) != HOST_SENDCONTROL_Successful)
206 {
207 return ErrorCode;
208 }
209
210 if (SetMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
211 return RNDIS_COMMAND_FAILED;
212
213 return HOST_SENDCONTROL_Successful;
214 }
215
216 /** Gets a given RNDIS property of an attached RNDIS device.
217 *
218 * \param[in] Oid OID number of the parameter to get
219 * \param[in] Buffer Pointer to where the property data is to be written to
220 * \param[in] MaxLength Length in bytes of the destination buffer size
221 *
222 * \return A value from the USB_Host_SendControlErrorCodes_t enum or RNDIS_COMMAND_FAILED if the device returned a
223 * logical command failure
224 */
225 uint8_t RNDIS_QueryRNDISProperty(const uint32_t Oid, void* Buffer, const uint16_t MaxLength)
226 {
227 uint8_t ErrorCode;
228
229 RNDIS_Query_Message_t QueryMessage;
230
231 struct
232 {
233 RNDIS_Query_Complete_t QueryMessageResponse;
234 uint8_t ContigiousBuffer[MaxLength];
235 } QueryMessageResponseData;
236
237 QueryMessage.MessageType = REMOTE_NDIS_QUERY_MSG;
238 QueryMessage.MessageLength = sizeof(RNDIS_Query_Message_t);
239 QueryMessage.RequestId = RequestID++;
240
241 QueryMessage.Oid = Oid;
242 QueryMessage.InformationBufferLength = 0;
243 QueryMessage.InformationBufferOffset = 0;
244 QueryMessage.DeviceVcHandle = 0;
245
246 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&QueryMessage,
247 sizeof(RNDIS_Query_Message_t))) != HOST_SENDCONTROL_Successful)
248 {
249 return ErrorCode;
250 }
251
252 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&QueryMessageResponseData,
253 sizeof(QueryMessageResponseData))) != HOST_SENDCONTROL_Successful)
254 {
255 return ErrorCode;
256 }
257
258 if (QueryMessageResponseData.QueryMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
259 return RNDIS_COMMAND_FAILED;
260
261 memcpy(Buffer, &QueryMessageResponseData.ContigiousBuffer, MaxLength);
262
263 return HOST_SENDCONTROL_Successful;
264 }
265
266 /** Retrieves the size of a received packet, discarding the remainder of the RNDIS packet header to leave only the
267 * packet contents for processing by the host.
268 *
269 * \param[out] PacketLength Size of the packet currently in the pipe
270 *
271 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
272 */
273 uint8_t RNDIS_GetPacketLength(uint16_t* const PacketLength)
274 {
275 uint8_t ErrorCode;
276
277 Pipe_SelectPipe(RNDIS_DATAPIPE_IN);
278 Pipe_SetPipeToken(PIPE_TOKEN_IN);
279 Pipe_Unfreeze();
280
281 if (!(Pipe_IsReadWriteAllowed()))
282 {
283 *PacketLength = 0;
284 Pipe_Freeze();
285 return PIPE_RWSTREAM_NoError;
286 }
287
288 RNDIS_Packet_Message_t DeviceMessage;
289
290 if ((ErrorCode = Pipe_Read_Stream_LE(&DeviceMessage, sizeof(RNDIS_Packet_Message_t))) != PIPE_RWSTREAM_NoError)
291 {
292 return ErrorCode;
293 }
294
295 *PacketLength = (uint16_t)DeviceMessage.DataLength;
296
297 Pipe_Discard_Stream(DeviceMessage.DataOffset - (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t)));
298
299 Pipe_Freeze();
300
301 return PIPE_RWSTREAM_NoError;
302 }